42#define HITMATERIAL 0x80000000
44#define EVERYTHINGFINE 0x00000000
71 opal->getAuxiliaryOutputDirectory(),
72 opal->getInputBasename() +
"_DesignPath.dat"
75 !std::filesystem::exists(fileName)) {
78 << std::setw(17) <<
"1 - s"
79 << std::setw(18) <<
"2 - Rx"
80 << std::setw(18) <<
"3 - Ry"
81 << std::setw(18) <<
"4 - Rz"
82 << std::setw(18) <<
"5 - Px"
83 << std::setw(18) <<
"6 - Py"
84 << std::setw(18) <<
"7 - Pz"
85 << std::setw(18) <<
"8 - Efx"
86 << std::setw(18) <<
"9 - Efy"
87 << std::setw(18) <<
"10 - Efz"
88 << std::setw(18) <<
"11 - Bfx"
89 << std::setw(18) <<
"12 - Bfy"
90 << std::setw(18) <<
"13 - Bfz"
91 << std::setw(18) <<
"14 - Ekin"
92 << std::setw(18) <<
"15 - t"
95 logger_m.open(fileName, std::ios_base::app);
119 for (
const std::shared_ptr<Component>& field : fields) {
120 double length = field->getElementLength();
121 int numSteps = field->getRequiredNumberOfTimeSteps();
123 if (length < numSteps * driftLength) {
125 "The time step is too long compared to the length of the\n"
126 "element '" + field->getName() +
"'\n" +
127 "The length of the element is: " + std::to_string(length) +
"\n"
128 "The distance the particles drift in " + std::to_string(numSteps) +
129 " time step(s) is: " + std::to_string(numSteps * driftLength));
138 std::set<std::string> visitedElements;
151 std::set<std::shared_ptr<Component>> intersection, currentSet;
175 IndexMap::value_t::const_iterator it = elementSet.begin();
176 const IndexMap::value_t::const_iterator
end = elementSet.end();
177 for (; it !=
end; ++ it) {
178 visitedElements.insert((*it)->getName());
183 currentSet = elementSet;
191 intersection.clear();
192 std::set_intersection(currentSet.begin(), currentSet.end(),
193 elementSet.begin(), elementSet.end(),
194 std::inserter(intersection, intersection.begin()));
199 intersection.empty() && !(elementSet.empty() || currentSet.empty())));
203 imap_m.saveSDDS(initialPathLength);
214 IndexMap::value_t::const_iterator it = activeSet.begin();
215 const IndexMap::value_t::const_iterator
end = activeSet.end();
223 std::string names(
"\t");
224 for (; it !=
end; ++ it) {
229 if ((*it)->applyToReferenceParticle(localR, localP,
time_m + 0.5 *
dt_m, localE, localB)) {
233 names += (*it)->getName() +
", ";
244 << std::setw(18) << std::setprecision(8) <<
r_m(0)
245 << std::setw(18) << std::setprecision(8) <<
r_m(1)
246 << std::setw(18) << std::setprecision(8) <<
r_m(2)
247 << std::setw(18) << std::setprecision(8) <<
p_m(0)
248 << std::setw(18) << std::setprecision(8) <<
p_m(1)
249 << std::setw(18) << std::setprecision(8) <<
p_m(2)
250 << std::setw(18) << std::setprecision(8) << Ef(0)
251 << std::setw(18) << std::setprecision(8) << Ef(1)
252 << std::setw(18) << std::setprecision(8) << Ef(2)
253 << std::setw(18) << std::setprecision(8) << Bf(0)
254 << std::setw(18) << std::setprecision(8) << Bf(1)
255 << std::setw(18) << std::setprecision(8) << Bf(2)
275 if (activeSet.empty() &&
287 IndexMap::value_t::const_iterator it = activeSet.begin();
288 const IndexMap::value_t::const_iterator
end = activeSet.end();
290 for (; it !=
end; ++ it) {
301 const std::set<std::string> &visitedElements) {
304 IndexMap::value_t::const_iterator it = activeSet.begin();
305 const IndexMap::value_t::const_iterator
end = activeSet.end();
307 for (; it !=
end; ++ it) {
310 visitedElements.find((*it)->getName()) == visitedElements.end()) {
327 IndexMap::value_t::const_iterator it = elementSet.begin();
328 const IndexMap::value_t::const_iterator
end = elementSet.end();
330 double designEnergy = 0.0;
331 for (; it !=
end; ++ it) {
375 IndexMap::value_t::const_iterator it = elementSet.begin();
376 const IndexMap::value_t::const_iterator
end = elementSet.end();
378 for (; it !=
end; ++ it) {
380 std::string
name = (*it)->getName();
383 for (
auto pit = prior.first; pit != prior.second; ++ pit) {
384 if (std::abs((*pit).second.endField_m - start) < 1e-10) {
395 double elementEdge = start - initialR(2) *
euclidean_norm(initialP) / initialP(2);
403 std::map<std::string, std::set<elementPosition, elementPositionComp> > tmpRegistry;
406 const std::string &
name = (*it).first;
409 auto prior = tmpRegistry.find(
name);
410 if (prior == tmpRegistry.end()) {
411 std::set<elementPosition, elementPositionComp> tmpSet;
413 tmpRegistry.insert(std::make_pair(
name, tmpSet));
417 std::set<elementPosition, elementPositionComp> &set = (*prior).second;
422 FieldList::iterator it = allElements.begin();
423 const FieldList::iterator
end = allElements.end();
424 for (; it !=
end; ++ it) {
425 std::string
name = (*it).getElement()->getName();
426 auto trit = tmpRegistry.find(
name);
427 if (trit == tmpRegistry.end())
continue;
429 std::queue<std::pair<double, double> > range;
430 std::set<elementPosition, elementPositionComp> &set = (*trit).second;
432 for (
auto sit = set.begin(); sit != set.end(); ++ sit) {
433 range.push(std::make_pair((*sit).elementEdge_m, (*sit).endField_m));
435 (*it).getElement()->setActionRange(range);
442 FieldList::iterator it = allElements.begin();
443 const FieldList::iterator
end = allElements.end();
444 for (; it !=
end; ++ it) {
445 std::shared_ptr<Component> element = (*it).getElement();
446 if (visitedElements.find(element->getName()) == visitedElements.end() &&
450 element->setDesignEnergy(kineticEnergyeV);
457 FieldList::iterator it = allElements.begin();
458 const FieldList::iterator
end = allElements.end();
460 for (; it !=
end; ++ it) {
464 BoundingBox other = it->getBoundingBoxInLabCoords();
484 boost::optional<Vector_t> intersectionPoint =
globalBoundingBox_m.getIntersectionPoint(position, direction);
486 return intersectionPoint ?
euclidean_norm(intersectionPoint.get() - position): 10.0;
489 return std::numeric_limits<double>::max();
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
std::list< ClassicField > FieldList
Tps< T > sqrt(const Tps< T > &x)
Square root.
T euclidean_norm(const Vector< T > &)
Euclidean norm.
PETE_TBTree< FnCopysign, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > copysign(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
Inform & endl(Inform &inf)
Inform & level1(Inform &inf)
constexpr double c
The velocity of light in m/s.
std::string combineFilePath(std::initializer_list< std::string > ilist)
double getGamma(Vector_t p)
static OpalData * getInstance()
double getPhaseAtMaxEnergy(const Vector_t &R, const Vector_t &P, double t, double dt)
std::set< std::shared_ptr< Component > > value_t
void processElementRegister()
void updateBoundingBoxWithCurrentPosition()
void checkElementLengths(const std::set< std::shared_ptr< Component > > &elements)
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
double computeDriftLengthToBoundingBox(const std::set< std::shared_ptr< Component > > &elements, const Vector_t &position, const Vector_t &direction) const
std::multimap< std::string, elementPosition > elementRegistry_m
Vector_t r_m
position of reference particle in lab coordinates
const PartData & reference_m
BoundingBox globalBoundingBox_m
Vector_t p_m
momentum of reference particle
bool containsCavity(const IndexMap::value_t &activeSet)
void integrate(const IndexMap::value_t &activeSet, double maxDrift=10.0)
OrbitThreader(const PartData &ref, const Vector_t &r, const Vector_t &p, double s, double maxDiffZBunch, double t, double dT, StepSizeConfig stepSizes, OpalBeamline &bl)
StepSizeConfig stepSizes_m
final position in path length
double getMaxDesignEnergy(const IndexMap::value_t &elementSet) const
void registerElement(const IndexMap::value_t &elementSet, double, const Vector_t &r, const Vector_t &p)
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
virtual double getDesignEnergy() const override
The base class for all OPAL exceptions.
Vektor< double, 3 > Vector_t