27#include <boost/filesystem.hpp>
28#include <boost/regex.hpp>
34 containsSource_m(false)
42 containsSource_m(false),
43 coordTransformationTo_m(origin, rotation)
52 std::set<std::shared_ptr<Component> > elementSet;
55 for (; it !=
end; ++ it) {
56 std::shared_ptr<Component> element = (*it).getElement();
57 Vector_t r = element->getCSTrafoGlobal2Local().transformTo(x);
59 if (element->isInside(r)) {
60 elementSet.insert(element);
69 unsigned long rtv = 0x00;
79 unsigned long rtv = 0x00;
83 std::set<std::shared_ptr<Component>>::const_iterator it =
elements.begin();
84 const std::set<std::shared_ptr<Component>>::const_iterator
end =
elements.end();
86 for (; it !=
end; ++ it) {
96 (*it)->applyToReferenceParticle(localR, localP, t, localE, localB);
119 double spos = (
max +
min) / 2.;
120 if(!nomonitors && spos < (*flit).getStart()) {
121 if(!(*flit).isOn() &&
max > (*flit).getStart()) {
122 (*flit).setOn(kineticEnergy);
127 if(!(*flit).isOn() &&
max > (*flit).getStart() &&
min < (*flit).getEnd()) {
128 (*flit).setOn(kineticEnergy);
136 ((*fprev).getEnd() + 0.01 > (*flit).getStart()) ) {
137 (*flit).setOn(kineticEnergy);
185 if((*fit).getElement()->getType() ==
type) {
186 elements_of_requested_type.push_back((*fit));
189 return elements_of_requested_type;
193 if (!element->isPositioned()) {
197 element->releasePosition();
201 element->setCSTrafoGlobal2Local(toElement);
202 element->fixPosition();
206 static unsigned int order = 0;
209 unsigned int minOrder = order;
211 double endPriorPathLength = 0.0;
215 for (; it !=
end; ++ it) {
216 std::shared_ptr<Component> element = (*it).getElement();
217 if (element->isPositioned()) {
220 (*it).order_m = minOrder;
228 double beginThisPathLength = element->getElementPosition();
229 Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
234 double arcLength = (thisLength *
std::abs(bendAngle) / (2 *
sin(
std::abs(bendAngle) / 2)));
238 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
244 sin(0.5 * bendAngle) * effectiveRotationAxis);
246 sin(0.25 * bendAngle) * effectiveRotationAxis);
248 sin(0.5 * entranceAngle) * effectiveRotationAxis);
251 std::vector<Vector_t> truePath = bendElement->
getDesignPath();
252 Quaternion_t directionExitHardEdge(
cos(0.5 * (0.5 * bendAngle - entranceAngle)),
253 sin(0.5 * (0.5 * bendAngle - entranceAngle)) * effectiveRotationAxis);
255 double distanceEntryHETruePath =
euclidean_norm(truePath.front());
256 double distanceExitHETruePath =
euclidean_norm(rotationAboutZ.
rotate(truePath.back()) - exitHardEdge);
257 double pathLengthTruePath = (*it).getEnd() - (*it).getStart();
258 arcLength = pathLengthTruePath - distanceEntryHETruePath - distanceExitHETruePath;
262 Vector_t endThis3D = beginThis3D + chord;
263 double endThisPathLength = beginThisPathLength + arcLength;
266 (entryFaceRotation * rotationAboutZ).conjugate());
270 element->setCSTrafoGlobal2Local(fromEndLastToBeginThis * currentCoordTrafo);
272 currentCoordTrafo = (fromEndLastToEndThis * currentCoordTrafo);
274 endPriorPathLength = endThisPathLength;
278 double endPriorPathLength = 0.0;
282 for (; it !=
end; ++ it) {
283 std::shared_ptr<Component> element = (*it).getElement();
284 if (element->isPositioned())
continue;
286 (*it).order_m = order ++;
288 double beginThisPathLength = element->getElementPosition();
289 double thisLength = element->getElementLength();
290 Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
293 beginThis3D(2) -= thisLength;
307 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
313 sin(0.5 * bendAngle) * effectiveRotationAxis);
315 sin(0.25 * bendAngle) * effectiveRotationAxis);
317 double arcLength = (thisLength *
std::abs(bendAngle) /
318 (2 *
sin(bendAngle / 2)));
320 std::vector<Vector_t> truePath = bendElement->
getDesignPath();
322 Quaternion_t directionExitHardEdge(
cos(0.5 * (0.5 * bendAngle - entranceAngle)),
323 sin(0.5 * (0.5 * bendAngle - entranceAngle)) * effectiveRotationAxis);
325 double distanceEntryHETruePath =
euclidean_norm(truePath.front());
326 double distanceExitHETruePath =
euclidean_norm(rotationAboutZ.
rotate(truePath.back()) - exitHardEdge);
327 double pathLengthTruePath = (*it).getEnd() - (*it).getStart();
328 arcLength = pathLengthTruePath - distanceEntryHETruePath - distanceExitHETruePath;
331 endThis3D = (beginThis3D +
335 currentCoordTrafo = fromEndLastToEndThis * currentCoordTrafo;
337 endPriorPathLength = beginThisPathLength + arcLength;
339 double rotationAngleAboutZ = (*it).getElement()->getRotationAboutZ();
341 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
345 element->setCSTrafoGlobal2Local(fromLastToThis * currentCoordTrafo);
348 element->fixPosition();
368 boost::filesystem::exists(fileName)) {
369 pos.open(fileName, std::ios_base::app);
375 for (; it !=
end; ++ it) {
376 std::shared_ptr<Component> element = (*it).getElement();
382 mesh.
add(*(element.get()));
387 Bend2D * bendElement =
static_cast<Bend2D*
>(element.get());
388 std::vector<Vector_t> designPath = bendElement->
getDesignPath();
392 unsigned int size = designPath.size();
393 unsigned int minNumSteps =
std::max(20.0,
395 unsigned int frequency =
std::floor((
double)size / minNumSteps);
397 pos << std::setw(30) << std::left << std::string(
"\"ENTRY EDGE: ") + element->getName() + std::string(
"\"")
398 << std::setw(18) << std::setprecision(10) << entry3D(2)
399 << std::setw(18) << std::setprecision(10) << entry3D(0)
400 << std::setw(18) << std::setprecision(10) << entry3D(1)
403 Vector_t position = element->getCSTrafoGlobal2Local().transformFrom(designPath.front());
404 pos << std::setw(30) << std::left << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
405 << std::setw(18) << std::setprecision(10) << position(2)
406 << std::setw(18) << std::setprecision(10) << position(0)
407 << std::setw(18) << std::setprecision(10) << position(1)
410 for (
unsigned int i = frequency; i + 1 < size; i += frequency) {
412 position = element->getCSTrafoGlobal2Local().transformFrom(designPath[i]);
413 pos << std::setw(30) << std::left << std::string(
"\"MID: ") + element->getName() + std::string(
"\"")
414 << std::setw(18) << std::setprecision(10) << position(2)
415 << std::setw(18) << std::setprecision(10) << position(0)
416 << std::setw(18) << std::setprecision(10) << position(1)
420 position = element->getCSTrafoGlobal2Local().transformFrom(designPath.back());
421 pos << std::setw(30) << std::left << std::string(
"\"END: ") + element->getName() + std::string(
"\"")
422 << std::setw(18) << std::setprecision(10) << position(2)
423 << std::setw(18) << std::setprecision(10) << position(0)
424 << std::setw(18) << std::setprecision(10) << position(1)
427 pos << std::setw(30) << std::left << std::string(
"\"EXIT EDGE: ") + element->getName() + std::string(
"\"")
428 << std::setw(18) << std::setprecision(10) << exit3D(2)
429 << std::setw(18) << std::setprecision(10) << exit3D(0)
430 << std::setw(18) << std::setprecision(10) << exit3D(1)
433 pos << std::setw(30) << std::left << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
434 << std::setw(18) << std::setprecision(10) << entry3D(2)
435 << std::setw(18) << std::setprecision(10) << entry3D(0)
436 << std::setw(18) << std::setprecision(10) << entry3D(1)
439 pos << std::setw(30) << std::left << std::string(
"\"END: ") + element->getName() + std::string(
"\"")
440 << std::setw(18) << std::setprecision(10) << exit3D(2)
441 << std::setw(18) << std::setprecision(10) << exit3D(0)
442 << std::setw(18) << std::setprecision(10) << exit3D(1)
451 std::string parseInput() {
454 std::string source(
"");
457 const std::string commentFormat(
"");
458 const boost::regex empty(
"^[ \t]*$");
459 const boost::regex lineEnd(
";");
460 const std::string lineEndFormat(
";\n");
461 const boost::regex cppCommentExpr(
"//.*");
462 const boost::regex cCommentExpr(
"/\\*.*?\\*/");
463 bool priorEmpty =
true;
469 std::getline(in, str);
470 str = boost::regex_replace(str, cppCommentExpr, commentFormat);
471 str = boost::regex_replace(str, empty, commentFormat);
475 }
else if (!priorEmpty) {
476 source +=
"##EMPTY_LINE##";
483 source = boost::regex_replace(source, cCommentExpr, commentFormat);
484 source = boost::regex_replace(source, lineEnd, lineEndFormat, boost::match_default | boost::format_all);
489 const boost::regex lineCommand(
"(LINE[ \t]*=[ \t]*\\([^\\)]*\\))[ \t]*,[^;]*;", boost::regex::icase);
490 const std::string firstSubExpression(
"\\1;");
491 source = boost::regex_replace(source, lineCommand, firstSubExpression);
496 unsigned int getMinimalSignificantDigits(
double num,
const unsigned int maxDigits) {
498 snprintf(buf, 32,
"%.*f", maxDigits + 1, num);
499 std::string numStr(buf);
500 unsigned int length = numStr.length();
502 unsigned int numDigits = maxDigits;
504 while (i < maxDigits + 1 && numStr[length - i] ==
'0') {
512 std::string round2string(
double num,
const unsigned int maxDigits) {
515 snprintf(buf, 64,
"%.*f", getMinimalSignificantDigits(num, maxDigits), num);
517 return std::string(buf);
527 std::string input = parseInput();
532 std::ofstream pos(fname);
534 for (; it !=
end; ++ it) {
535 std::shared_ptr<Component> element = (*it).getElement();
536 std::string elementName = element->getName();
537 const boost::regex replacePSI(
"(" + elementName +
"\\s*:[^\\n]*)PSI\\s*=[^,;]*,?", boost::regex::icase);
538 input = boost::regex_replace(input, replacePSI,
"\\1\\2");
540 const boost::regex replaceELEMEDGE(
"(" + elementName +
"\\s*:[^\\n]*)ELEMEDGE\\s*=[^,;]*(.)", boost::regex::icase);
545 for (
unsigned int d = 0; d < 3; ++ d)
548 std::string x = (
std::abs(origin(0)) > 1
e-10?
"X = " + round2string(origin(0), 10) +
", ":
"");
549 std::string y = (
std::abs(origin(1)) > 1
e-10?
"Y = " + round2string(origin(1), 10) +
", ":
"");
550 std::string z = (
std::abs(origin(2)) > 1
e-10?
"Z = " + round2string(origin(2), 10) +
", ":
"");
552 std::string theta = (orient(0) > 1
e-10?
"THETA = " + round2string(orient(0), 6) +
" * PI / 180, ":
"");
553 std::string phi = (orient(1) > 1
e-10?
"PHI = " + round2string(orient(1), 6) +
" * PI / 180, ":
"");
554 std::string psi = (orient(2) > 1
e-10?
"PSI = " + round2string(orient(2), 6) +
" * PI / 180, ":
"");
555 std::string coordTrafo = x + y + z + theta + phi + psi;
556 if (coordTrafo.length() > 2) {
557 coordTrafo = coordTrafo.substr(0, coordTrafo.length() - 2);
560 std::string position = (
"\\1" + coordTrafo +
"\\2");
562 input = boost::regex_replace(input, replaceELEMEDGE, position);
566 const Bend2D* dipole =
static_cast<const Bend2D*
>(element.get());
571 const boost::regex angleR(
"(" + elementName +
"\\s*:[^\\n]*ANGLE\\s*=)[^,;]*(.)");
572 const std::string angleF(
"\\1 " + round2string(angle *
Units::rad2deg, 6) +
" / 180 * PI\\2");
573 const boost::regex E1R(
"(" + elementName +
"\\s*:[^\\n]*E1\\s*=)[^,;]*(.)");
574 const std::string E1F(
"\\1 " + round2string(E1 *
Units::rad2deg, 6) +
" / 180 * PI\\2");
575 const boost::regex E2R(
"(" + elementName +
"\\s*:[^\\n]*E2\\s*=)[^,;]*(.)");
576 const std::string E2F(
"\\1 " + round2string(E2 *
Units::rad2deg, 6) +
" / 180 * PI\\2");
577 const boost::regex noRotation(
"(" + elementName +
"\\s*:[^\\n]*),\\s*ROTATION\\s*=[^,;]*(.)");
578 const std::string noRotationFormat(
"\\1\\2 ");
580 input = boost::regex_replace(input, angleR, angleF);
581 input = boost::regex_replace(input, E1R, E1F);
582 input = boost::regex_replace(input, E2R, E2F);
583 input = boost::regex_replace(input, noRotation, noRotationFormat);
587 const boost::regex empty(
"##EMPTY_LINE##");
588 const std::string emptyFormat(
"\n");
589 input = boost::regex_replace(input, empty, emptyFormat);
598 double designEnergy = 0.0;
599 for (; it !=
end; ++ it) {
600 std::shared_ptr<Component> element = (*it).getElement();
603 Bend2D * bendElement =
static_cast<Bend2D*
>(element.get());
606 (*it).setOn(designEnergy);
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
std::list< ClassicField > FieldList
Tps< T > cos(const Tps< T > &x)
Cosine.
Tps< T > sin(const Tps< T > &x)
Sine.
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_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
constexpr double e
The value of.
std::string::iterator iterator
std::string combineFilePath(std::initializer_list< std::string > ilist)
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
boost::function< boost::tuple< double, bool >(arguments_t)> type
std::string getInputBasename()
get input file name without extension
OpenMode getOpenMode() const
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
virtual double getExitAngle() const override
CoordinateSystemTrafo getBeginToEnd_local() const
std::vector< Vector_t > getDesignPath() const
double getChordLength() const
double getBendAngle() const
double getDesignEnergy() const
double getEntranceAngle() const
double getRotationAboutZ() const
Vector_t getOrigin() const
Quaternion getRotation() const
Vector_t rotate(const Vector_t &) const
Quaternion conjugate() const
void add(const ElementBase &element)
void write(const std::string &fname)
static bool SortAsc(const ClassicField &fle1, const ClassicField &fle2)
FieldList getElementByType(ElementType)
Vector_t rotateToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
void positionElementRelative(std::shared_ptr< ElementBase >)
void merge(OpalBeamline &rhs)
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 coordTransformationTo_m
void print(Inform &) const
void switchElements(const double &, const double &, const double &kineticEnergy, const bool &nomonitors=false)
unsigned long getFieldAt(const unsigned int &, const Vector_t &, const long &, const double &, Vector_t &, Vector_t &)
std::set< std::shared_ptr< Component > > getElements(const Vector_t &x)
void swap(OpalBeamline &rhs)
Vektor< double, 3 > Vector_t