OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
OpalBeamline.cpp
Go to the documentation of this file.
1 //
2 // Class OpalBeamline
3 // :FIXME: add class description
4 //
5 // Copyright (c) 200x - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
18 #include "Elements/OpalBeamline.h"
19 
20 #include "Utilities/Options.h"
21 #include "Utilities/Util.h"
23 #include "AbsBeamline/Bend2D.h"
25 
26 #include <boost/filesystem.hpp>
27 #include <boost/regex.hpp>
28 #include <fstream>
29 
31  elements_m(),
32  prepared_m(false),
33  containsSource_m(false)
34 {
35 }
36 
38  const Quaternion& rotation):
39  elements_m(),
40  prepared_m(false),
41  containsSource_m(false),
42  coordTransformationTo_m(origin, rotation)
43 {
44 }
45 
47  elements_m.clear();
48 }
49 
50 std::set<std::shared_ptr<Component>> OpalBeamline::getElements(const Vector_t &x) {
51  std::set<std::shared_ptr<Component> > elementSet;
52  FieldList::iterator it = elements_m.begin();
53  const FieldList::iterator end = elements_m.end();
54  for (; it != end; ++ it) {
55  std::shared_ptr<Component> element = (*it).getElement();
56  Vector_t r = element->getCSTrafoGlobal2Local().transformTo(x);
57 
58  if (element->isInside(r)) {
59  elementSet.insert(element);
60  }
61  }
62 
63  return elementSet;
64 }
65 
66 unsigned long OpalBeamline::getFieldAt(const unsigned int &/*index*/, const Vector_t &/*pos*/, const long &/*sindex*/, const double &/*t*/, Vector_t &/*E*/, Vector_t &/*B*/) {
67 
68  unsigned long rtv = 0x00;
69 
70  return rtv;
71 }
72 
73 unsigned long OpalBeamline::getFieldAt(const Vector_t &position,
74  const Vector_t &momentum,
75  const double &t,
76  Vector_t &Ef,
77  Vector_t &Bf) {
78  unsigned long rtv = 0x00;
79 
80  std::set<std::shared_ptr<Component>> elements = getElements(position);
81 
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();
84 
85  for (; it != end; ++ it) {
86  ElementBase::ElementType type = (*it)->getType();
87  if (type == ElementBase::MONITOR ||
89  type == ElementBase::CCOLLIMATOR) continue;
90 
91  Vector_t localR = transformToLocalCS(*it, position);
92  Vector_t localP = rotateToLocalCS(*it, momentum);
93  Vector_t localE(0.0), localB(0.0);
94 
95  (*it)->applyToReferenceParticle(localR, localP, t, localE, localB);
96 
97  Ef += rotateFromLocalCS(*it, localE);
98  Bf += rotateFromLocalCS(*it, localB);
99  }
100 
101  // if(section.hasWake()) {
102  // rtv |= BEAMLINE_WAKE;
103  // }
104  // if(section.hasParticleMatterInteraction()) {
105  // rtv |= BEAMLINE_PARTICLEMATTERINTERACTION;
106  // }
107 
108  return rtv;
109 }
110 
111 void OpalBeamline::switchElements(const double &min, const double &max, const double &kineticEnergy, const bool &nomonitors) {
112 
113  FieldList::iterator fprev;
114  for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit) {
115  // don't set online monitors if the centroid of the bunch is allready inside monitor
116  // or if explicitly not desired (eg during auto phasing)
117  if(flit->getElement()->getType() == ElementBase::MONITOR) {
118  double spos = (max + min) / 2.;
119  if(!nomonitors && spos < (*flit).getStart()) {
120  if(!(*flit).isOn() && max > (*flit).getStart()) {
121  (*flit).setOn(kineticEnergy);
122  }
123  }
124 
125  } else {
126  if(!(*flit).isOn() && max > (*flit).getStart() && min < (*flit).getEnd()) {
127  (*flit).setOn(kineticEnergy);
128  }
129 
130  //check if multiple degraders follow one another with no other elements in between
131  //if element is off and it is a degrader
132  if (!(*flit).isOn() && flit->getElement()->getType() == ElementBase::DEGRADER) {
133  //check if previous element: is on, is a degrader, ends where new element starts
134  if ((*fprev).isOn() && fprev->getElement()->getType() == ElementBase::DEGRADER &&
135  ((*fprev).getEnd() + 0.01 > (*flit).getStart()) ) {
136  (*flit).setOn(kineticEnergy);
137  }
138  }
139  }
140 
141  fprev = flit;
142  }
143 }
144 
146  for(FieldList::iterator flit = elements_m.begin(); flit != elements_m.end(); ++ flit)
147  (*flit).setOff();
148 }
149 
151  if (elements_m.size() == 0) {
152  prepared_m = true;
153  return;
154  }
156  prepared_m = true;
157 }
158 
159 void OpalBeamline::print(Inform &/*msg*/) const {
160 }
161 
163  std::swap(elements_m, rhs.elements_m);
164  std::swap(prepared_m, rhs.prepared_m);
166 }
167 
169  elements_m.insert(elements_m.end(),
170  rhs.elements_m.begin(),
171  rhs.elements_m.end());
172  prepared_m = false;
174 }
175 
176 
178  if (type == ElementBase::ANY) {
179  return elements_m;
180  }
181 
182  FieldList elements_of_requested_type;
183  for(FieldList::iterator fit = elements_m.begin(); fit != elements_m.end(); ++ fit) {
184  if((*fit).getElement()->getType() == type) {
185  elements_of_requested_type.push_back((*fit));
186  }
187  }
188  return elements_of_requested_type;
189 }
190 
191 void OpalBeamline::positionElementRelative(std::shared_ptr<ElementBase> element) {
192  if (!element->isPositioned()) {
193  return;
194  }
195 
196  element->releasePosition();
197  CoordinateSystemTrafo toElement = element->getCSTrafoGlobal2Local();
198  toElement *= coordTransformationTo_m;
199 
200  element->setCSTrafoGlobal2Local(toElement);
201  element->fixPosition();
202 }
203 
205  static unsigned int order = 0;
206  const FieldList::iterator end = elements_m.end();
207 
208  unsigned int minOrder = order;
209  {
210  double endPriorPathLength = 0.0;
212 
213  FieldList::iterator it = elements_m.begin();
214  for (; it != end; ++ it) {
215  std::shared_ptr<Component> element = (*it).getElement();
216  if (element->isPositioned()) {
217  continue;
218  }
219  (*it).order_m = minOrder;
220 
221  if (element->getType() != ElementBase::SBEND &&
222  element->getType() != ElementBase::RBEND &&
223  element->getType() != ElementBase::RBEND3D) {
224  continue;
225  }
226 
227  double beginThisPathLength = element->getElementPosition();
228  Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
229  BendBase * bendElement = static_cast<BendBase*>(element.get());
230  double thisLength = bendElement->getChordLength();
231  double bendAngle = bendElement->getBendAngle();
232  double entranceAngle = bendElement->getEntranceAngle();
233  double arcLength = (thisLength * std::abs(bendAngle) / (2 * sin(std::abs(bendAngle) / 2)));
234 
235  double rotationAngleAboutZ = bendElement->getRotationAboutZ();
236  Quaternion_t rotationAboutZ(cos(0.5 * rotationAngleAboutZ),
237  sin(-0.5 * rotationAngleAboutZ) * Vector_t(0, 0, 1));
238 
239  Vector_t effectiveRotationAxis = rotationAboutZ.rotate(Vector_t(0, -1, 0));
240  effectiveRotationAxis /= euclidean_norm(effectiveRotationAxis);
241 
242  Quaternion_t rotationAboutAxis(cos(0.5 * bendAngle),
243  sin(0.5 * bendAngle) * effectiveRotationAxis);
244  Quaternion_t halfRotationAboutAxis(cos(0.25 * bendAngle),
245  sin(0.25 * bendAngle) * effectiveRotationAxis);
246  Quaternion_t entryFaceRotation(cos(0.5 * entranceAngle),
247  sin(0.5 * entranceAngle) * effectiveRotationAxis);
248 
249  if (!Options::idealized) {
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);
253  Vector_t exitHardEdge = thisLength * directionExitHardEdge.rotate(Vector_t(0, 0, 1));
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;
258  }
259 
260  Vector_t chord = thisLength * halfRotationAboutAxis.rotate(Vector_t(0, 0, 1));
261  Vector_t endThis3D = beginThis3D + chord;
262  double endThisPathLength = beginThisPathLength + arcLength;
263 
264  CoordinateSystemTrafo fromEndLastToBeginThis(beginThis3D,
265  (entryFaceRotation * rotationAboutZ).conjugate());
266  CoordinateSystemTrafo fromEndLastToEndThis(endThis3D,
267  rotationAboutAxis.conjugate());
268 
269  element->setCSTrafoGlobal2Local(fromEndLastToBeginThis * currentCoordTrafo);
270 
271  currentCoordTrafo = (fromEndLastToEndThis * currentCoordTrafo);
272 
273  endPriorPathLength = endThisPathLength;
274  }
275  }
276 
277  double endPriorPathLength = 0.0;
279 
280  FieldList::iterator it = elements_m.begin();
281  for (; it != end; ++ it) {
282  std::shared_ptr<Component> element = (*it).getElement();
283  if (element->isPositioned()) continue;
284 
285  (*it).order_m = order ++;
286 
287  double beginThisPathLength = element->getElementPosition();
288  double thisLength = element->getElementLength();
289  Vector_t beginThis3D(0, 0, beginThisPathLength - endPriorPathLength);
290 
291  if (element->getType() == ElementBase::SOURCE) {
292  beginThis3D(2) -= thisLength;
293  }
294 
295  Vector_t endThis3D;
296  if (element->getType() == ElementBase::SBEND ||
297  element->getType() == ElementBase::RBEND ||
298  element->getType() == ElementBase::RBEND3D) {
299 
300  BendBase * bendElement = static_cast<BendBase*>(element.get());
301  thisLength = bendElement->getChordLength();
302  double bendAngle = bendElement->getBendAngle();
303 
304  double rotationAngleAboutZ = bendElement->getRotationAboutZ();
305  Quaternion_t rotationAboutZ(cos(0.5 * rotationAngleAboutZ),
306  sin(-0.5 * rotationAngleAboutZ) * Vector_t(0, 0, 1));
307 
308  Vector_t effectiveRotationAxis = rotationAboutZ.rotate(Vector_t(0, -1, 0));
309  effectiveRotationAxis /= euclidean_norm(effectiveRotationAxis);
310 
311  Quaternion_t rotationAboutAxis(cos(0.5 * bendAngle),
312  sin(0.5 * bendAngle) * effectiveRotationAxis);
313  Quaternion halfRotationAboutAxis(cos(0.25 * bendAngle),
314  sin(0.25 * bendAngle) * effectiveRotationAxis);
315 
316  double arcLength = (thisLength * std::abs(bendAngle) /
317  (2 * sin(bendAngle / 2)));
318  if (!Options::idealized) {
319  std::vector<Vector_t> truePath = bendElement->getDesignPath();
320  double entranceAngle = bendElement->getEntranceAngle();
321  Quaternion_t directionExitHardEdge(cos(0.5 * (0.5 * bendAngle - entranceAngle)),
322  sin(0.5 * (0.5 * bendAngle - entranceAngle)) * effectiveRotationAxis);
323  Vector_t exitHardEdge = thisLength * directionExitHardEdge.rotate(Vector_t(0, 0, 1));
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;
328  }
329 
330  endThis3D = (beginThis3D +
331  halfRotationAboutAxis.rotate(Vector_t(0, 0, thisLength)));
332  CoordinateSystemTrafo fromEndLastToEndThis(endThis3D,
333  rotationAboutAxis.conjugate());
334  currentCoordTrafo = fromEndLastToEndThis * currentCoordTrafo;
335 
336  endPriorPathLength = beginThisPathLength + arcLength;
337  } else {
338  double rotationAngleAboutZ = (*it).getElement()->getRotationAboutZ();
339  Quaternion_t rotationAboutZ(cos(0.5 * rotationAngleAboutZ),
340  sin(-0.5 * rotationAngleAboutZ) * Vector_t(0, 0, 1));
341 
342  CoordinateSystemTrafo fromLastToThis(beginThis3D, rotationAboutZ);
343 
344  element->setCSTrafoGlobal2Local(fromLastToThis * currentCoordTrafo);
345  }
346 
347  element->fixPosition();
348  }
349 }
350 
352  if (Ippl::myNode() != 0 || OpalData::getInstance()->isOptimizerRun()) return;
353 
354  elements_m.sort([](const ClassicField& a, const ClassicField& b) {
355  return a.order_m < b.order_m;
356  });
357 
358  FieldList::iterator it = elements_m.begin();
360 
361  std::ofstream pos;
362  std::string fileName = Util::combineFilePath({
364  OpalData::getInstance()->getInputBasename() + "_ElementPositions.txt"
365  });
367  boost::filesystem::exists(fileName)) {
368  pos.open(fileName, std::ios_base::app);
369  } else {
370  pos.open(fileName);
371  }
372 
373  MeshGenerator mesh;
374  for (; it != end; ++ it) {
375  std::shared_ptr<Component> element = (*it).getElement();
376  CoordinateSystemTrafo toBegin = element->getEdgeToBegin() * element->getCSTrafoGlobal2Local();
377  CoordinateSystemTrafo toEnd = element->getEdgeToEnd() * element->getCSTrafoGlobal2Local();
378  Vector_t entry3D = toBegin.getOrigin();
379  Vector_t exit3D = toEnd.getOrigin();
380 
381  mesh.add(*(element.get()));
382 
383  if (element->getType() == ElementBase::SBEND ||
384  element->getType() == ElementBase::RBEND) {
385 
386  Bend2D * bendElement = static_cast<Bend2D*>(element.get());
387  std::vector<Vector_t> designPath = bendElement->getDesignPath();
388  toEnd = bendElement->getBeginToEnd_local() * element->getCSTrafoGlobal2Local();
389  exit3D = toEnd.getOrigin();
390 
391  unsigned int size = designPath.size();
392  unsigned int minNumSteps = std::max(20.0,
393  std::abs(bendElement->getBendAngle() / Physics::pi * 180));
394  unsigned int frequency = std::floor((double)size / minNumSteps);
395 
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)
400  << "\n";
401 
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)
407  << std::endl;
408 
409  for (unsigned int i = frequency; i + 1 < size; i += frequency) {
410 
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)
416  << std::endl;
417  }
418 
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)
424  << std::endl;
425 
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)
430  << std::endl;
431  } else {
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)
436  << "\n";
437 
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)
442  << std::endl;
443  }
444  }
446  mesh.write(OpalData::getInstance()->getInputBasename());
447 }
448 
449 namespace {
450  std::string parseInput() {
451 
452  std::ifstream in(OpalData::getInstance()->getInputFn());
453  std::string source("");
454  std::string str;
455  char testBit;
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("/\\*.*?\\*/"); // "/\\*(?>[^*/]+|\\*[^/]|/[^*])*(?>(?R)(?>[^*/]+|\\*[^/]|/[^*])*)*\\*/"
462  bool priorEmpty = true;
463 
464  in.get(testBit);
465  while (!in.eof()) {
466  in.putback(testBit);
467 
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) {
472  source += str;// + '\n';
473  priorEmpty = false;
474  } else if (!priorEmpty) {
475  source += "##EMPTY_LINE##";
476  priorEmpty = true;
477  }
478 
479  in.get(testBit);
480  }
481 
482  source = boost::regex_replace(source, cCommentExpr, commentFormat);
483  source = boost::regex_replace(source, lineEnd, lineEndFormat, boost::match_default | boost::format_all);
484 
485  // Since the positions of the elements are absolute in the laboratory coordinate system we have to make
486  // sure that the line command doesn't provide an origin and orientation. Everything after the sequence of
487  // elements can be deleted and only "LINE = (...);", the first sub-expression (denoted by '\1'), should be kept.
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);
491 
492  return source;
493  }
494 
495  unsigned int getMinimalSignificantDigits(double num, const unsigned int maxDigits) {
496  char buf[32];
497  snprintf(buf, 32, "%.*f", maxDigits + 1, num);
498  std::string numStr(buf);
499  unsigned int length = numStr.length();
500 
501  unsigned int numDigits = maxDigits;
502  unsigned int i = 2;
503  while (i < maxDigits + 1 && numStr[length - i] == '0') {
504  --numDigits;
505  ++ i;
506  }
507 
508  return numDigits;
509  }
510 
511  std::string round2string(double num, const unsigned int maxDigits) {
512  char buf[64];
513 
514  snprintf(buf, 64, "%.*f", getMinimalSignificantDigits(num, maxDigits), num);
515 
516  return std::string(buf);
517  }
518 }
519 
521  if (Ippl::myNode() != 0 || OpalData::getInstance()->isOptimizerRun()) return;
522 
523  FieldList::iterator it = elements_m.begin();
525 
526  std::string input = parseInput();
527  std::string fname = Util::combineFilePath({
529  OpalData::getInstance()->getInputBasename() + "_3D.opal"
530  });
531  std::ofstream pos(fname);
532 
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");
538 
539  const boost::regex replaceELEMEDGE("(" + elementName + "\\s*:[^\\n]*)ELEMEDGE\\s*=[^,;]*(.)", boost::regex::icase);
540 
541  CoordinateSystemTrafo cst = element->getCSTrafoGlobal2Local();
542  Vector_t origin = cst.getOrigin();
543  Vector_t orient = Util::getTaitBryantAngles(cst.getRotation().conjugate(), elementName);
544  for (unsigned int d = 0; d < 3; ++ d)
545  orient(d) *= Physics::rad2deg;
546 
547  std::string x = (std::abs(origin(0)) > 1e-10? "X = " + round2string(origin(0), 10) + ", ": "");
548  std::string y = (std::abs(origin(1)) > 1e-10? "Y = " + round2string(origin(1), 10) + ", ": "");
549  std::string z = (std::abs(origin(2)) > 1e-10? "Z = " + round2string(origin(2), 10) + ", ": "");
550 
551  std::string theta = (orient(0) > 1e-10? "THETA = " + round2string(orient(0), 6) + " * PI / 180, ": "");
552  std::string phi = (orient(1) > 1e-10? "PHI = " + round2string(orient(1), 6) + " * PI / 180, ": "");
553  std::string psi = (orient(2) > 1e-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); // remove last ', '
557  }
558 
559  std::string position = ("\\1" + coordTrafo + "\\2");
560 
561  input = boost::regex_replace(input, replaceELEMEDGE, position);
562 
563  if (element->getType() == ElementBase::RBEND ||
564  element->getType() == ElementBase::SBEND) {
565  const Bend2D* dipole = static_cast<const Bend2D*>(element.get());
566  double angle = dipole->getBendAngle();
567  double E1 = dipole->getEntranceAngle();
568  double E2 = dipole->getExitAngle();
569 
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 ");
578 
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);
583  }
584  }
585 
586  const boost::regex empty("##EMPTY_LINE##");
587  const std::string emptyFormat("\n");
588  input = boost::regex_replace(input, empty, emptyFormat);
589 
590  pos << input << std::endl;
591 }
592 
594  auto it = elements_m.begin();
595  const auto end = elements_m.end();
596 
597  double designEnergy = 0.0;
598  for (; it != end; ++ it) {
599  std::shared_ptr<Component> element = (*it).getElement();
600  if (element->getType() == ElementBase::SBEND ||
601  element->getType() == ElementBase::RBEND) {
602  Bend2D * bendElement = static_cast<Bend2D*>(element.get());
603  designEnergy = bendElement->getDesignEnergy() * 1e-6;
604  }
605  (*it).setOn(designEnergy);
606  // element->goOnline(designEnergy);
607  }
608 }
elements
Definition: IndexMap.cpp:163
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
std::list< ClassicField > FieldList
Definition: ClassicField.h:42
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
std::complex< double > a
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)
Definition: PETE.h:733
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
constexpr double e
The value of.
Definition: Physics.h:39
constexpr double pi
The value of.
Definition: Physics.h:30
constexpr double rad2deg
The conversion factor from radians to degrees.
Definition: Physics.h:45
std::string::iterator iterator
Definition: MSLang.h:16
bool idealized
Definition: Options.cpp:93
std::string combineFilePath(std::initializer_list< std::string > ilist)
Definition: Util.cpp:139
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
Definition: Util.cpp:102
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
std::string getInputBasename()
get input file name without extension
Definition: OpalData.cpp:658
OPENMODE getOpenMode() const
Definition: OpalData.cpp:352
static OpalData * getInstance()
Definition: OpalData.cpp:195
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
Definition: OpalData.cpp:650
Definition: Bend2D.h:51
virtual double getExitAngle() const override
Definition: Bend2D.h:326
CoordinateSystemTrafo getBeginToEnd_local() const
Definition: Bend2D.h:354
std::vector< Vector_t > getDesignPath() const
Definition: BendBase.cpp:43
double getChordLength() const
Definition: BendBase.h:82
double getBendAngle() const
Definition: BendBase.h:92
double getDesignEnergy() const
Definition: BendBase.h:126
double getEntranceAngle() const
Definition: BendBase.h:103
double getRotationAboutZ() const
Definition: ElementBase.h:591
Quaternion getRotation() const
Vector_t rotate(const Vector_t &) const
Definition: Quaternion.cpp:122
Quaternion conjugate() const
Definition: Quaternion.h:105
void add(const ElementBase &element)
void write(const std::string &fname)
static bool SortAsc(const ClassicField &fle1, const ClassicField &fle2)
Definition: ClassicField.h:23
unsigned int order_m
Definition: ClassicField.h:34
FieldList getElementByType(ElementBase::ElementType)
Vector_t rotateToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
Definition: OpalBeamline.h:178
void positionElementRelative(std::shared_ptr< ElementBase >)
bool containsSource_m
Definition: OpalBeamline.h:99
void merge(OpalBeamline &rhs)
Vector_t transformToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
Definition: OpalBeamline.h:166
Vector_t rotateFromLocalCS(const std::shared_ptr< Component > &comp, const Vector_t &r) const
Definition: OpalBeamline.h:184
CoordinateSystemTrafo coordTransformationTo_m
Definition: OpalBeamline.h:101
void prepareSections()
void print(Inform &) const
void compute3DLattice()
void switchElements(const double &, const double &, const double &kineticEnergy, const bool &nomonitors=false)
void switchElementsOff()
void save3DInput()
unsigned long getFieldAt(const unsigned int &, const Vector_t &, const long &, const double &, Vector_t &, Vector_t &)
FieldList elements_m
Definition: OpalBeamline.h:97
void save3DLattice()
void activateElements()
std::set< std::shared_ptr< Component > > getElements(const Vector_t &x)
void swap(OpalBeamline &rhs)
Definition: Inform.h:42
static int myNode()
Definition: IpplInfo.cpp:691
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6