36#include <boost/filesystem.hpp>
42#define HITMATERIAL 0x80000000
44#define EVERYTHINGFINE 0x00000000
55 stepSizes_m(stepSizes),
56 zstop_m(stepSizes.getFinalZStop() +
std::copysign(1.0, dt) * 2 * maxDiffZBunch),
57 itsOpalBeamline_m(bl),
62 if (
ippl::Comm->rank() == 0 && !opal->isOptimizerRun()) {
64 {opal->getAuxiliaryOutputDirectory(), opal->getInputBasename() +
"_DesignPath.dat"});
66 || !boost::filesystem::exists(fileName)) {
68 logger_m <<
"#" << std::setw(17) <<
"1 - s" << std::setw(18) <<
"2 - Rx"
69 << std::setw(18) <<
"3 - Ry" << std::setw(18) <<
"4 - Rz" << std::setw(18)
70 <<
"5 - Px" << std::setw(18) <<
"6 - Py" << std::setw(18) <<
"7 - Pz"
71 << std::setw(18) <<
"8 - Efx" << std::setw(18) <<
"9 - Efy" << std::setw(18)
72 <<
"10 - Efz" << std::setw(18) <<
"11 - Bfx" << std::setw(18) <<
"12 - Bfy"
73 << std::setw(18) <<
"13 - Bfz" << std::setw(18) <<
"14 - Ekin" << std::setw(18)
76 logger_m.open(fileName, std::ios_base::app);
102 for (
const std::shared_ptr<Component>& field : fields) {
103 double length = field->getElementLength();
104 int numSteps = field->getRequiredNumberOfTimeSteps();
106 if (length < numSteps * driftLength) {
108 "The time step is too long compared to the length of the\n"
109 "element '" + field->getName() +
"'\n" +
110 "The length of the element is: " + std::to_string(length) +
"\n"
111 "The distance the particles drift in " + std::to_string(numSteps) +
112 " time step(s) is: " + std::to_string(numSteps * driftLength));
121 std::set<std::string> visitedElements;
133 std::set<std::shared_ptr<Component>> intersection, currentSet;
151 *
gmsg <<
"OrbitThreader maxDistance= " << maxDistance <<
endl;
152 *
gmsg <<
"OrbitThreader #elements = " << elementSet.size() <<
endl;
164 IndexMap::value_t::const_iterator it = elementSet.begin();
165 const IndexMap::value_t::const_iterator
end = elementSet.end();
166 for (; it !=
end; ++it) {
167 visitedElements.insert((*it)->getName());
172 currentSet = elementSet;
180 intersection.clear();
181 std::set_intersection(
182 currentSet.begin(), currentSet.end(), elementSet.begin(), elementSet.end(),
183 std::inserter(intersection, intersection.begin()));
187 && !(elementSet.empty() || currentSet.empty())));
201 IndexMap::value_t::const_iterator it = activeSet.begin();
202 const IndexMap::value_t::const_iterator
end = activeSet.end();
210 std::string names(
"\t");
211 for (; it !=
end; ++it) {
216 if ((*it)->applyToReferenceParticle(
217 localR, localP,
time_m + 0.5 *
dt_m, localE, localB)) {
221 names += (*it)->getName() +
", ";
232 logger_m << std::setw(18) << std::setprecision(8)
234 << std::setprecision(8) <<
r_m(0) << std::setw(18) << std::setprecision(8)
235 <<
r_m(1) << std::setw(18) << std::setprecision(8) <<
r_m(2) << std::setw(18)
236 << std::setprecision(8) <<
p_m(0) << std::setw(18) << std::setprecision(8)
237 <<
p_m(1) << std::setw(18) << std::setprecision(8) <<
p_m(2) << std::setw(18)
238 << std::setprecision(8) << Ef(0) << std::setw(18) << std::setprecision(8)
239 << Ef(1) << std::setw(18) << std::setprecision(8) << Ef(2) << std::setw(18)
240 << std::setprecision(8) << Bf(0) << std::setw(18) << std::setprecision(8)
241 << Bf(1) << std::setw(18) << std::setprecision(8) << Bf(2) << std::setw(18)
242 << std::setprecision(8)
264 if (activeSet.empty()
276 IndexMap::value_t::const_iterator it = activeSet.begin();
277 const IndexMap::value_t::const_iterator
end = activeSet.end();
279 for (; it !=
end; ++it) {
290 const IndexMap::value_t& activeSet,
const std::set<std::string>& visitedElements) {
294 IndexMap::value_t::const_iterator it = activeSet.begin();
295 const IndexMap::value_t::const_iterator
end = activeSet.end();
297 for (; it !=
end; ++it) {
300 && visitedElements.find((*it)->getName()) == visitedElements.end()) {
311 IndexMap::value_t::const_iterator it = elementSet.begin();
312 const IndexMap::value_t::const_iterator
end = elementSet.end();
314 double designEnergy = 0.0;
315 for (; it !=
end; ++it) {
357 IndexMap::value_t::const_iterator it = elementSet.begin();
358 const IndexMap::value_t::const_iterator
end = elementSet.end();
360 for (; it !=
end; ++it) {
362 std::string name = (*it)->getName();
365 for (
auto pit = prior.first; pit != prior.second; ++pit) {
366 if (std::abs((*pit).second.endField_m - start) < 1
e-10) {
378 double elementEdge = start - initialR(2) *
euclidean_norm(initialP) / initialP(2);
386 std::map<std::string, std::set<elementPosition, elementPositionComp>> tmpRegistry;
389 const std::string& name = (*it).first;
392 auto prior = tmpRegistry.find(name);
393 if (prior == tmpRegistry.end()) {
394 std::set<elementPosition, elementPositionComp> tmpSet;
396 tmpRegistry.insert(std::make_pair(name, tmpSet));
400 std::set<elementPosition, elementPositionComp>& set = (*prior).second;
407 for (; it !=
end; ++it) {
408 std::string name = (*it).getElement()->getName();
410 auto trit = tmpRegistry.find(name);
411 if (trit == tmpRegistry.end())
414 std::queue<std::pair<double, double>> range;
415 std::set<elementPosition, elementPositionComp>& set = (*trit).second;
417 for (
auto sit = set.begin(); sit != set.end(); ++sit) {
418 range.push(std::make_pair((*sit).elementEdge_m, (*sit).endField_m));
420 (*it).getElement()->setActionRange(range);
425 FieldList& allElements,
const std::set<std::string>& visitedElements) {
430 for (; it !=
end; ++it) {
431 std::shared_ptr<Component> element = (*it).getElement();
432 if (visitedElements.find(element->getName()) == visitedElements.end()
436 element->setDesignEnergy(kineticEnergyeV);
445 for (; it !=
end; ++it) {
449 BoundingBox other = it->getBoundingBoxInLabCoords();
457 std::array<Vector_t<double, 3>, 2> positions = {
r_m - 10 * dR,
r_m + 10 * dR};
471 boost::optional<Vector_t<double, 3>> intersectionPoint =
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
std::list< ClassicField > FieldList
KOKKOS_INLINE_FUNCTION double euclidean_norm(const Vector_t< T, D > &v)
Inform & endl(Inform &inf)
Inform & level1(Inform &inf)
constexpr double e
The value of.
constexpr double c
The velocity of light in m/s.
std::string::iterator iterator
std::string combineFilePath(std::initializer_list< std::string > ilist)
double getGamma(ippl::Vector< double, 3 > p)
KOKKOS_INLINE_FUNCTION Vector< T, Dim > max(const Vector< T, Dim > &a, const Vector< T, Dim > &b)
KOKKOS_INLINE_FUNCTION Vector< T, Dim > min(const Vector< T, Dim > &a, const Vector< T, Dim > &b)
std::unique_ptr< mpi::Communicator > Comm
virtual double getDesignEnergy() const override
static OpalData * getInstance()
double getPhaseAtMaxEnergy(const Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, double t, double dt)
void add(key_t::first_type initialStep, key_t::second_type finalStep, const value_t &val)
void tidyUp(double zstop)
std::set< std::shared_ptr< Component > > value_t
void saveSDDS(double startS) const
void processElementRegister()
Vector_t< double, 3 > r_m
position of reference particle in lab coordinates
Vector_t< double, 3 > p_m
momentum of reference particle
void updateBoundingBoxWithCurrentPosition()
void checkElementLengths(const std::set< std::shared_ptr< Component > > &elements)
void registerElement(const IndexMap::value_t &elementSet, double, const Vector_t< double, 3 > &r, const Vector_t< double, 3 > &p)
double distTrackBack_m
distance to track back before tracking forward (length of bunch but not beyond cathode)
double computeDriftLengthToBoundingBox(const std::set< std::shared_ptr< Component > > &elements, const Vector_t< double, 3 > &position, const Vector_t< double, 3 > &direction) const
void setDesignEnergy(FieldList &allElements, const std::set< std::string > &visitedElements)
ValueRange< double > pathLengthRange_m
ValueRange< long > stepRange_m
double time_m
the simulated time
OpalBeamline & itsOpalBeamline_m
std::multimap< std::string, elementPosition > elementRegistry_m
const PartData & reference_m
BoundingBox globalBoundingBox_m
bool containsCavity(const IndexMap::value_t &activeSet)
void integrate(const IndexMap::value_t &activeSet, double maxDrift=10.0)
StepSizeConfig stepSizes_m
final position in path length
double getMaxDesignEnergy(const IndexMap::value_t &elementSet) const
double pathLength_m
position of reference particle in path length
void autophaseCavities(const IndexMap::value_t &activeSet, const std::set< std::string > &visitedElements)
void computeBoundingBox()
size_t loggingFrequency_m
OrbitThreader(const PartData &ref, const Vector_t< double, 3 > &r, const Vector_t< double, 3 > &p, double s, double maxDiffZBunch, double t, double dT, StepSizeConfig stepSizes, OpalBeamline &bl)
KOKKOS_INLINE_FUNCTION double getM() const
The constant mass per particle.
unsigned long long getNumStepsFinestResolution() const
ValueRange< double > getPathLengthRange() const
FieldList getElementByType(ElementType)
Vector_t< double, 3 > rotateToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
CoordinateSystemTrafo getCSTrafoLab2Local(const std::shared_ptr< Component > &comp) const
Vector_t< double, 3 > transformToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
std::set< std::shared_ptr< Component > > getElements(const Vector_t< double, 3 > &x)
Vector_t< double, 3 > rotateFromLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
KOKKOS_INLINE_FUNCTION void kick(const Vector_t< double, 3 > &R, Vector_t< double, 3 > &P, const Vector_t< double, 3 > &Ef, const Vector_t< double, 3 > &Bf, const double &dt) const
KOKKOS_INLINE_FUNCTION void push(Vector_t< double, 3 > &R, const Vector_t< double, 3 > &P, const double &dt) const
boost::optional< Vector_t< double, 3 > > getIntersectionPoint(const Vector_t< double, 3 > &position, const Vector_t< double, 3 > &direction) const
void enlargeToContainPosition(const Vector_t< double, 3 > &position)
void enlargeToContainBoundingBox(const BoundingBox &boundingBox)
bool isOutside(T value) const
void enlargeIfOutside(T value)
bool isInside(T value) const
The base class for all OPAL exceptions.