37#include <boost/filesystem.hpp>
43#define HITMATERIAL 0x80000000
45#define EVERYTHINGFINE 0x00000000
62 stepSizes_m(stepSizes),
63 zstop_m(stepSizes.getFinalZStop() +
std::
copysign(1.0, dt) * 2 * maxDiffZBunch),
64 itsOpalBeamline_m(bl),
72 opal->getAuxiliaryOutputDirectory(),
73 opal->getInputBasename() +
"_DesignPath.dat"
76 !boost::filesystem::exists(fileName)) {
79 << std::setw(17) <<
"1 - s"
80 << std::setw(18) <<
"2 - Rx"
81 << std::setw(18) <<
"3 - Ry"
82 << std::setw(18) <<
"4 - Rz"
83 << std::setw(18) <<
"5 - Px"
84 << std::setw(18) <<
"6 - Py"
85 << std::setw(18) <<
"7 - Pz"
86 << std::setw(18) <<
"8 - Efx"
87 << std::setw(18) <<
"9 - Efy"
88 << std::setw(18) <<
"10 - Efz"
89 << std::setw(18) <<
"11 - Bfx"
90 << std::setw(18) <<
"12 - Bfy"
91 << std::setw(18) <<
"13 - Bfz"
92 << std::setw(18) <<
"14 - Ekin"
93 << std::setw(18) <<
"15 - t"
96 logger_m.open(fileName, std::ios_base::app);
120 for (
const std::shared_ptr<Component>& field : fields) {
121 double length = field->getElementLength();
122 int numSteps = field->getRequiredNumberOfTimeSteps();
124 if (length < numSteps * driftLength) {
126 "The time step is too long compared to the length of the\n"
127 "element '" + field->getName() +
"'\n" +
128 "The length of the element is: " + std::to_string(length) +
"\n"
129 "The distance the particles drift in " + std::to_string(numSteps) +
130 " time step(s) is: " + std::to_string(numSteps * driftLength));
139 std::set<std::string> visitedElements;
152 std::set<std::shared_ptr<Component>> intersection, currentSet;
176 IndexMap::value_t::const_iterator it = elementSet.begin();
177 const IndexMap::value_t::const_iterator
end = elementSet.end();
178 for (; it !=
end; ++ it) {
179 visitedElements.insert((*it)->getName());
184 currentSet = elementSet;
192 intersection.clear();
193 std::set_intersection(currentSet.begin(), currentSet.end(),
194 elementSet.begin(), elementSet.end(),
195 std::inserter(intersection, intersection.begin()));
200 intersection.empty() && !(elementSet.empty() || currentSet.empty())));
215 IndexMap::value_t::const_iterator it = activeSet.begin();
216 const IndexMap::value_t::const_iterator
end = activeSet.end();
224 std::string names(
"\t");
225 for (; it !=
end; ++ it) {
230 if ((*it)->applyToReferenceParticle(localR, localP,
time_m + 0.5 *
dt_m, localE, localB)) {
234 names += (*it)->getName() +
", ";
245 << std::setw(18) << std::setprecision(8) <<
r_m(0)
246 << std::setw(18) << std::setprecision(8) <<
r_m(1)
247 << std::setw(18) << std::setprecision(8) <<
r_m(2)
248 << std::setw(18) << std::setprecision(8) <<
p_m(0)
249 << std::setw(18) << std::setprecision(8) <<
p_m(1)
250 << std::setw(18) << std::setprecision(8) <<
p_m(2)
251 << std::setw(18) << std::setprecision(8) << Ef(0)
252 << std::setw(18) << std::setprecision(8) << Ef(1)
253 << std::setw(18) << std::setprecision(8) << Ef(2)
254 << std::setw(18) << std::setprecision(8) << Bf(0)
255 << std::setw(18) << std::setprecision(8) << Bf(1)
256 << std::setw(18) << std::setprecision(8) << Bf(2)
276 if (activeSet.empty() &&
288 IndexMap::value_t::const_iterator it = activeSet.begin();
289 const IndexMap::value_t::const_iterator
end = activeSet.end();
291 for (; it !=
end; ++ it) {
302 const std::set<std::string> &visitedElements) {
305 IndexMap::value_t::const_iterator it = activeSet.begin();
306 const IndexMap::value_t::const_iterator
end = activeSet.end();
308 for (; it !=
end; ++ it) {
311 visitedElements.find((*it)->getName()) == visitedElements.end()) {
328 IndexMap::value_t::const_iterator it = elementSet.begin();
329 const IndexMap::value_t::const_iterator
end = elementSet.end();
331 double designEnergy = 0.0;
332 for (; it !=
end; ++ it) {
376 IndexMap::value_t::const_iterator it = elementSet.begin();
377 const IndexMap::value_t::const_iterator
end = elementSet.end();
379 for (; it !=
end; ++ it) {
381 std::string
name = (*it)->getName();
384 for (
auto pit = prior.first; pit != prior.second; ++ pit) {
385 if (
std::abs((*pit).second.endField_m - start) < 1
e-10) {
396 double elementEdge = start - initialR(2) *
euclidean_norm(initialP) / initialP(2);
404 std::map<std::string, std::set<elementPosition, elementPositionComp> > tmpRegistry;
407 const std::string &
name = (*it).first;
410 auto prior = tmpRegistry.find(
name);
411 if (prior == tmpRegistry.end()) {
412 std::set<elementPosition, elementPositionComp> tmpSet;
414 tmpRegistry.insert(std::make_pair(
name, tmpSet));
418 std::set<elementPosition, elementPositionComp> &set = (*prior).second;
425 for (; it !=
end; ++ it) {
426 std::string
name = (*it).getElement()->getName();
427 auto trit = tmpRegistry.find(
name);
428 if (trit == tmpRegistry.end())
continue;
430 std::queue<std::pair<double, double> > range;
431 std::set<elementPosition, elementPositionComp> &set = (*trit).second;
433 for (
auto sit = set.begin(); sit != set.end(); ++ sit) {
434 range.push(std::make_pair((*sit).elementEdge_m, (*sit).endField_m));
436 (*it).getElement()->setActionRange(range);
445 for (; it !=
end; ++ it) {
446 std::shared_ptr<Component> element = (*it).getElement();
447 if (visitedElements.find(element->getName()) == visitedElements.end() &&
451 element->setDesignEnergy(kineticEnergyeV);
461 for (; it !=
end; ++ it) {
465 BoundingBox other = it->getBoundingBoxInLabCoords();
487 return intersectionPoint ?
euclidean_norm(intersectionPoint.get() - position): 10.0;
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.
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
PETE_TBTree< FnCopysign, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > copysign(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
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(Vector_t p)
static OpalData * getInstance()
double getPhaseAtMaxEnergy(const Vector_t &R, const Vector_t &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()
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
unsigned long long getNumStepsFinestResolution() const
ValueRange< double > getPathLengthRange() const
virtual double getDesignEnergy() const override
double getM() const
The constant mass per particle.
boost::optional< Vector_t > getIntersectionPoint(const Vector_t &position, const Vector_t &direction) const
void enlargeToContainBoundingBox(const BoundingBox &boundingBox)
void enlargeToContainPosition(const Vector_t &position)
bool isOutside(T value) const
void enlargeIfOutside(T value)
bool isInside(T value) const
FieldList getElementByType(ElementType)
Vector_t rotateToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
Vector_t transformToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
Vector_t rotateFromLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
CoordinateSystemTrafo getCSTrafoLab2Local(const std::shared_ptr< Component > &comp) const
std::set< std::shared_ptr< Component > > getElements(const Vector_t &x)
void kick(const Vector_t &R, Vector_t &P, const Vector_t &Ef, const Vector_t &Bf, const double &dt) const
void push(Vector_t &R, const Vector_t &P, const double &dt) const
The base class for all OPAL exceptions.