26 #include <boost/filesystem.hpp>
27 #include <boost/regex.hpp>
33 containsSource_m(false)
41 containsSource_m(false),
42 coordTransformationTo_m(origin, rotation)
51 std::set<std::shared_ptr<Component> > elementSet;
54 for (; it !=
end; ++ it) {
55 std::shared_ptr<Component> element = (*it).getElement();
56 Vector_t r = element->getCSTrafoGlobal2Local().transformTo(x);
58 if (element->isInside(r)) {
59 elementSet.insert(element);
68 unsigned long rtv = 0x00;
78 unsigned long rtv = 0x00;
82 std::set<std::shared_ptr<Component>>::const_iterator it =
elements.begin();
83 const std::set<std::shared_ptr<Component>>::const_iterator
end =
elements.end();
85 for (; it !=
end; ++ it) {
95 (*it)->applyToReferenceParticle(localR, localP, t, localE, localB);
118 double spos = (
max +
min) / 2.;
119 if(!nomonitors && spos < (*flit).getStart()) {
120 if(!(*flit).isOn() &&
max > (*flit).getStart()) {
121 (*flit).setOn(kineticEnergy);
126 if(!(*flit).isOn() &&
max > (*flit).getStart() &&
min < (*flit).getEnd()) {
127 (*flit).setOn(kineticEnergy);
135 ((*fprev).getEnd() + 0.01 > (*flit).getStart()) ) {
136 (*flit).setOn(kineticEnergy);
184 if((*fit).getElement()->getType() ==
type) {
185 elements_of_requested_type.push_back((*fit));
188 return elements_of_requested_type;
192 if (!element->isPositioned()) {
196 element->releasePosition();
200 element->setCSTrafoGlobal2Local(toElement);
201 element->fixPosition();
205 static unsigned int order = 0;
208 unsigned int minOrder = order;
210 double endPriorPathLength = 0.0;
214 for (; it !=
end; ++ it) {
215 std::shared_ptr<Component> element = (*it).getElement();
216 if (element->isPositioned()) {
219 (*it).order_m = minOrder;
227 double beginThisPathLength = element->getElementPosition();
228 Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
233 double arcLength = (thisLength *
std::abs(bendAngle) / (2 *
sin(
std::abs(bendAngle) / 2)));
237 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
243 sin(0.5 * bendAngle) * effectiveRotationAxis);
245 sin(0.25 * bendAngle) * effectiveRotationAxis);
247 sin(0.5 * entranceAngle) * effectiveRotationAxis);
250 std::vector<Vector_t> truePath = bendElement->
getDesignPath();
251 Quaternion_t directionExitHardEdge(
cos(0.5 * (0.5 * bendAngle - entranceAngle)),
252 sin(0.5 * (0.5 * bendAngle - entranceAngle)) * effectiveRotationAxis);
254 double distanceEntryHETruePath =
euclidean_norm(truePath.front());
255 double distanceExitHETruePath =
euclidean_norm(rotationAboutZ.
rotate(truePath.back()) - exitHardEdge);
256 double pathLengthTruePath = (*it).getEnd() - (*it).getStart();
257 arcLength = pathLengthTruePath - distanceEntryHETruePath - distanceExitHETruePath;
261 Vector_t endThis3D = beginThis3D + chord;
262 double endThisPathLength = beginThisPathLength + arcLength;
265 (entryFaceRotation * rotationAboutZ).conjugate());
269 element->setCSTrafoGlobal2Local(fromEndLastToBeginThis * currentCoordTrafo);
271 currentCoordTrafo = (fromEndLastToEndThis * currentCoordTrafo);
273 endPriorPathLength = endThisPathLength;
277 double endPriorPathLength = 0.0;
281 for (; it !=
end; ++ it) {
282 std::shared_ptr<Component> element = (*it).getElement();
283 if (element->isPositioned())
continue;
285 (*it).order_m = order ++;
287 double beginThisPathLength = element->getElementPosition();
288 double thisLength = element->getElementLength();
289 Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
292 beginThis3D(2) -= thisLength;
306 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
312 sin(0.5 * bendAngle) * effectiveRotationAxis);
314 sin(0.25 * bendAngle) * effectiveRotationAxis);
316 double arcLength = (thisLength *
std::abs(bendAngle) /
317 (2 *
sin(bendAngle / 2)));
319 std::vector<Vector_t> truePath = bendElement->
getDesignPath();
321 Quaternion_t directionExitHardEdge(
cos(0.5 * (0.5 * bendAngle - entranceAngle)),
322 sin(0.5 * (0.5 * bendAngle - entranceAngle)) * effectiveRotationAxis);
324 double distanceEntryHETruePath =
euclidean_norm(truePath.front());
325 double distanceExitHETruePath =
euclidean_norm(rotationAboutZ.
rotate(truePath.back()) - exitHardEdge);
326 double pathLengthTruePath = (*it).getEnd() - (*it).getStart();
327 arcLength = pathLengthTruePath - distanceEntryHETruePath - distanceExitHETruePath;
330 endThis3D = (beginThis3D +
334 currentCoordTrafo = fromEndLastToEndThis * currentCoordTrafo;
336 endPriorPathLength = beginThisPathLength + arcLength;
338 double rotationAngleAboutZ = (*it).getElement()->getRotationAboutZ();
340 sin(-0.5 * rotationAngleAboutZ) *
Vector_t(0, 0, 1));
344 element->setCSTrafoGlobal2Local(fromLastToThis * currentCoordTrafo);
347 element->fixPosition();
367 boost::filesystem::exists(fileName)) {
368 pos.open(fileName, std::ios_base::app);
374 for (; it !=
end; ++ it) {
375 std::shared_ptr<Component> element = (*it).getElement();
381 mesh.
add(*(element.get()));
386 Bend2D * bendElement =
static_cast<Bend2D*
>(element.get());
387 std::vector<Vector_t> designPath = bendElement->
getDesignPath();
391 unsigned int size = designPath.size();
392 unsigned int minNumSteps =
std::max(20.0,
394 unsigned int frequency =
std::floor((
double)size / minNumSteps);
396 pos << std::setw(30) << std::left << std::string(
"\"ENTRY EDGE: ") + element->getName() + std::string(
"\"")
397 << std::setw(18) << std::setprecision(10) << entry3D(2)
398 << std::setw(18) << std::setprecision(10) << entry3D(0)
399 << std::setw(18) << std::setprecision(10) << entry3D(1)
402 Vector_t position = element->getCSTrafoGlobal2Local().transformFrom(designPath.front());
403 pos << std::setw(30) << std::left << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
404 << std::setw(18) << std::setprecision(10) << position(2)
405 << std::setw(18) << std::setprecision(10) << position(0)
406 << std::setw(18) << std::setprecision(10) << position(1)
409 for (
unsigned int i = frequency; i + 1 < size; i += frequency) {
411 position = element->getCSTrafoGlobal2Local().transformFrom(designPath[i]);
412 pos << std::setw(30) << std::left << std::string(
"\"MID: ") + element->getName() + std::string(
"\"")
413 << std::setw(18) << std::setprecision(10) << position(2)
414 << std::setw(18) << std::setprecision(10) << position(0)
415 << std::setw(18) << std::setprecision(10) << position(1)
419 position = element->getCSTrafoGlobal2Local().transformFrom(designPath.back());
420 pos << std::setw(30) << std::left << std::string(
"\"END: ") + element->getName() + std::string(
"\"")
421 << std::setw(18) << std::setprecision(10) << position(2)
422 << std::setw(18) << std::setprecision(10) << position(0)
423 << std::setw(18) << std::setprecision(10) << position(1)
426 pos << std::setw(30) << std::left << std::string(
"\"EXIT EDGE: ") + element->getName() + std::string(
"\"")
427 << std::setw(18) << std::setprecision(10) << exit3D(2)
428 << std::setw(18) << std::setprecision(10) << exit3D(0)
429 << std::setw(18) << std::setprecision(10) << exit3D(1)
432 pos << std::setw(30) << std::left << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
433 << std::setw(18) << std::setprecision(10) << entry3D(2)
434 << std::setw(18) << std::setprecision(10) << entry3D(0)
435 << std::setw(18) << std::setprecision(10) << entry3D(1)
438 pos << std::setw(30) << std::left << std::string(
"\"END: ") + element->getName() + std::string(
"\"")
439 << std::setw(18) << std::setprecision(10) << exit3D(2)
440 << std::setw(18) << std::setprecision(10) << exit3D(0)
441 << std::setw(18) << std::setprecision(10) << exit3D(1)
450 std::string parseInput() {
453 std::string source(
"");
456 const std::string commentFormat(
"");
457 const boost::regex empty(
"^[ \t]*$");
458 const boost::regex lineEnd(
";");
459 const std::string lineEndFormat(
";\n");
460 const boost::regex cppCommentExpr(
"//.*");
461 const boost::regex cCommentExpr(
"/\\*.*?\\*/");
462 bool priorEmpty =
true;
468 std::getline(in, str);
469 str = boost::regex_replace(str, cppCommentExpr, commentFormat);
470 str = boost::regex_replace(str, empty, commentFormat);
471 if (str.size() > 0) {
474 }
else if (!priorEmpty) {
475 source +=
"##EMPTY_LINE##";
482 source = boost::regex_replace(source, cCommentExpr, commentFormat);
483 source = boost::regex_replace(source, lineEnd, lineEndFormat, boost::match_default | boost::format_all);
488 const boost::regex lineCommand(
"(LINE[ \t]*=[ \t]*\\([^\\)]*\\))[ \t]*,[^;]*;", boost::regex::icase);
489 const std::string firstSubExpression(
"\\1;");
490 source = boost::regex_replace(source, lineCommand, firstSubExpression);
495 unsigned int getMinimalSignificantDigits(
double num,
const unsigned int maxDigits) {
497 snprintf(buf, 32,
"%.*f", maxDigits + 1, num);
498 std::string numStr(buf);
499 unsigned int length = numStr.length();
501 unsigned int numDigits = maxDigits;
503 while (i < maxDigits + 1 && numStr[length - i] ==
'0') {
511 std::string round2string(
double num,
const unsigned int maxDigits) {
514 snprintf(buf, 64,
"%.*f", getMinimalSignificantDigits(num, maxDigits), num);
516 return std::string(buf);
526 std::string input = parseInput();
531 std::ofstream pos(fname);
533 for (; it !=
end; ++ it) {
534 std::shared_ptr<Component> element = (*it).getElement();
535 std::string elementName = element->getName();
536 const boost::regex replacePSI(
"(" + elementName +
"\\s*:[^\\n]*)PSI\\s*=[^,;]*,?", boost::regex::icase);
537 input = boost::regex_replace(input, replacePSI,
"\\1\\2");
539 const boost::regex replaceELEMEDGE(
"(" + elementName +
"\\s*:[^\\n]*)ELEMEDGE\\s*=[^,;]*(.)", boost::regex::icase);
544 for (
unsigned int d = 0; d < 3; ++ d)
547 std::string x = (
std::abs(origin(0)) > 1
e-10?
"X = " + round2string(origin(0), 10) +
", ":
"");
548 std::string y = (
std::abs(origin(1)) > 1
e-10?
"Y = " + round2string(origin(1), 10) +
", ":
"");
549 std::string z = (
std::abs(origin(2)) > 1
e-10?
"Z = " + round2string(origin(2), 10) +
", ":
"");
551 std::string theta = (orient(0) > 1
e-10?
"THETA = " + round2string(orient(0), 6) +
" * PI / 180, ":
"");
552 std::string phi = (orient(1) > 1
e-10?
"PHI = " + round2string(orient(1), 6) +
" * PI / 180, ":
"");
553 std::string psi = (orient(2) > 1
e-10?
"PSI = " + round2string(orient(2), 6) +
" * PI / 180, ":
"");
554 std::string coordTrafo = x + y + z + theta + phi + psi;
555 if (coordTrafo.length() > 2) {
556 coordTrafo = coordTrafo.substr(0, coordTrafo.length() - 2);
559 std::string position = (
"\\1" + coordTrafo +
"\\2");
561 input = boost::regex_replace(input, replaceELEMEDGE, position);
565 const Bend2D* dipole =
static_cast<const Bend2D*
>(element.get());
570 const boost::regex angleR(
"(" + elementName +
"\\s*:[^\\n]*ANGLE\\s*=)[^,;]*(.)");
571 const std::string angleF(
"\\1 " + round2string(angle * 180 /
Physics::pi, 6) +
" / 180 * PI\\2");
572 const boost::regex E1R(
"(" + elementName +
"\\s*:[^\\n]*E1\\s*=)[^,;]*(.)");
573 const std::string E1F(
"\\1 " + round2string(E1 * 180 /
Physics::pi, 6) +
" / 180 * PI\\2");
574 const boost::regex E2R(
"(" + elementName +
"\\s*:[^\\n]*E2\\s*=)[^,;]*(.)");
575 const std::string E2F(
"\\1 " + round2string(E2 * 180 /
Physics::pi, 6) +
" / 180 * PI\\2");
576 const boost::regex noRotation(
"(" + elementName +
"\\s*:[^\\n]*),\\s*ROTATION\\s*=[^,;]*(.)");
577 const std::string noRotationFormat(
"\\1\\2 ");
579 input = boost::regex_replace(input, angleR, angleF);
580 input = boost::regex_replace(input, E1R, E1F);
581 input = boost::regex_replace(input, E2R, E2F);
582 input = boost::regex_replace(input, noRotation, noRotationFormat);
586 const boost::regex empty(
"##EMPTY_LINE##");
587 const std::string emptyFormat(
"\n");
588 input = boost::regex_replace(input, empty, emptyFormat);
597 double designEnergy = 0.0;
598 for (; it !=
end; ++ it) {
599 std::shared_ptr<Component> element = (*it).getElement();
602 Bend2D * bendElement =
static_cast<Bend2D*
>(element.get());
605 (*it).setOn(designEnergy);
Tps< T > cos(const Tps< T > &x)
Cosine.
Tps< T > sin(const Tps< T > &x)
Sine.
T euclidean_norm(const Vector< T > &)
Euclidean norm.
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
std::list< ClassicField > FieldList
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< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Inform & endl(Inform &inf)
constexpr double e
The value of.
constexpr double pi
The value of.
constexpr double rad2deg
The conversion factor from radians to degrees.
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(ElementBase::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