25#include <boost/filesystem.hpp>
26#include <boost/regex.hpp>
41 std::set<std::shared_ptr<Component>> elementSet;
44 for (; it !=
end; ++it) {
45 std::shared_ptr<Component> element = (*it).getElement();
48 if (element->isInside(r)) {
49 elementSet.insert(element);
59 unsigned long rtv = 0x00;
67 unsigned long rtv = 0x00;
71 std::set<std::shared_ptr<Component>>::const_iterator it =
elements.begin();
72 const std::set<std::shared_ptr<Component>>::const_iterator
end =
elements.end();
74 for (; it !=
end; ++it) {
83 (*it)->applyToReferenceParticle(localR, localP, t, localE, localB);
100 const double& min,
const double& max,
const double& kineticEnergy,
const bool& nomonitors) {
101 FieldList::iterator fprev;
105 if (!(*flit).isOn() && max > (*flit).getStart() && min < (*flit).getEnd()) {
106 (*flit).setOn(kineticEnergy);
148 if ((*fit).getElement()->getType() == type) {
149 elements_of_requested_type.push_back((*fit));
152 return elements_of_requested_type;
156 if (!element->isPositioned()) {
160 element->releasePosition();
164 element->setCSTrafoGlobal2Local(toElement);
165 element->fixPosition();
169 static unsigned int order = 0;
172 unsigned int minOrder = order;
174 double endPriorPathLength = 0.0;
178 for (; it !=
end; ++it) {
179 std::shared_ptr<Component> element = (*it).getElement();
180 if (element->isPositioned()) {
183 (*it).order_m = minOrder;
190 double beginThisPathLength = element->getElementPosition();
193 double thisLength = 0.;
194 double bendAngle = 0.0;
195 double entranceAngle = 0.0;
199 double rotationAngleAboutZ = 0.0;
201 cos(0.5 * rotationAngleAboutZ),
206 effectiveRotationAxis = effectiveRotationAxis /
euclidean_norm(effectiveRotationAxis);
209 cos(0.5 * bendAngle), sin(0.5 * bendAngle) * effectiveRotationAxis);
211 cos(0.25 * bendAngle), sin(0.25 * bendAngle) * effectiveRotationAxis);
213 cos(0.5 * entranceAngle), sin(0.5 * entranceAngle) * effectiveRotationAxis);
232 double endThisPathLength = beginThisPathLength + arcLength;
235 beginThis3D, (entryFaceRotation * rotationAboutZ).conjugate());
238 element->setCSTrafoGlobal2Local(fromEndLastToBeginThis * currentCoordTrafo);
240 currentCoordTrafo = (fromEndLastToEndThis * currentCoordTrafo);
242 endPriorPathLength = endThisPathLength;
246 double endPriorPathLength = 0.0;
250 for (; it !=
end; ++it) {
251 std::shared_ptr<Component> element = (*it).getElement();
252 if (element->isPositioned())
255 (*it).order_m = order++;
257 double beginThisPathLength = element->getElementPosition();
258 double thisLength = element->getElementLength();
262 beginThis3D(2) -= thisLength;
270 double bendAngle = 0.0;
272 double rotationAngleAboutZ = 0.0;
274 cos(0.5 * rotationAngleAboutZ),
279 effectiveRotationAxis = effectiveRotationAxis /
euclidean_norm(effectiveRotationAxis);
282 cos(0.5 * bendAngle), sin(0.5 * bendAngle) * effectiveRotationAxis);
284 cos(0.25 * bendAngle), sin(0.25 * bendAngle) * effectiveRotationAxis);
286 double arcLength = (thisLength * std::abs(bendAngle) / (2 * sin(bendAngle / 2)));
305 currentCoordTrafo = fromEndLastToEndThis * currentCoordTrafo;
307 endPriorPathLength = beginThisPathLength + arcLength;
309 double rotationAngleAboutZ = (*it).getElement()->getRotationAboutZ();
311 cos(0.5 * rotationAngleAboutZ),
316 element->setCSTrafoGlobal2Local(fromLastToThis * currentCoordTrafo);
319 element->fixPosition();
339 && boost::filesystem::exists(fileName)) {
340 pos.open(fileName, std::ios_base::app);
346 for (; it !=
end; ++it) {
347 std::shared_ptr<Component> element = (*it).getElement();
349 element->getEdgeToBegin() * element->getCSTrafoGlobal2Local();
354 mesh.
add(*(element.get()));
358 std::vector<Vector_t<double, 3>> designPath;
362 unsigned int size = 0;
364 unsigned int minNumSteps =
368 unsigned int frequency = std::floor((
double)size / minNumSteps);
370 pos << std::setw(30) << std::left
371 << std::string(
"\"ENTRY EDGE: ") + element->getName() + std::string(
"\"")
372 << std::setw(18) << std::setprecision(10) << entry3D(2) << std::setw(18)
373 << std::setprecision(10) << entry3D(0) << std::setw(18) << std::setprecision(10)
374 << entry3D(1) <<
"\n";
377 element->getCSTrafoGlobal2Local().transformFrom(designPath.front());
378 pos << std::setw(30) << std::left
379 << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
380 << std::setw(18) << std::setprecision(10) << position(2) << std::setw(18)
381 << std::setprecision(10) << position(0) << std::setw(18) << std::setprecision(10)
382 << position(1) << std::endl;
384 for (
unsigned int i = frequency; i + 1 < size; i += frequency) {
385 position = element->getCSTrafoGlobal2Local().transformFrom(designPath[i]);
386 pos << std::setw(30) << std::left
387 << std::string(
"\"MID: ") + element->getName() + std::string(
"\"")
388 << std::setw(18) << std::setprecision(10) << position(2) << std::setw(18)
389 << std::setprecision(10) << position(0) << std::setw(18)
390 << std::setprecision(10) << position(1) << std::endl;
393 position = element->getCSTrafoGlobal2Local().transformFrom(designPath.back());
394 pos << std::setw(30) << std::left
395 << std::string(
"\"END: ") + element->getName() + std::string(
"\"") << std::setw(18)
396 << std::setprecision(10) << position(2) << std::setw(18) << std::setprecision(10)
397 << position(0) << std::setw(18) << std::setprecision(10) << position(1)
400 pos << std::setw(30) << std::left
401 << std::string(
"\"EXIT EDGE: ") + element->getName() + std::string(
"\"")
402 << std::setw(18) << std::setprecision(10) << exit3D(2) << std::setw(18)
403 << std::setprecision(10) << exit3D(0) << std::setw(18) << std::setprecision(10)
404 << exit3D(1) << std::endl;
406 pos << std::setw(30) << std::left
407 << std::string(
"\"BEGIN: ") + element->getName() + std::string(
"\"")
408 << std::setw(18) << std::setprecision(10) << entry3D(2) << std::setw(18)
409 << std::setprecision(10) << entry3D(0) << std::setw(18) << std::setprecision(10)
410 << entry3D(1) <<
"\n";
412 pos << std::setw(30) << std::left
413 << std::string(
"\"END: ") + element->getName() + std::string(
"\"") << std::setw(18)
414 << std::setprecision(10) << exit3D(2) << std::setw(18) << std::setprecision(10)
415 << exit3D(0) << std::setw(18) << std::setprecision(10) << exit3D(1) << std::endl;
423 std::string parseInput() {
425 std::string source(
"");
428 const std::string commentFormat(
"");
429 const boost::regex empty(
"^[ \t]*$");
430 const boost::regex lineEnd(
";");
431 const std::string lineEndFormat(
";\n");
432 const boost::regex cppCommentExpr(
"//.*");
433 const boost::regex cCommentExpr(
435 bool priorEmpty =
true;
441 std::getline(in, str);
442 str = boost::regex_replace(str, cppCommentExpr, commentFormat);
443 str = boost::regex_replace(str, empty, commentFormat);
447 }
else if (!priorEmpty) {
448 source +=
"##EMPTY_LINE##";
455 source = boost::regex_replace(source, cCommentExpr, commentFormat);
456 source = boost::regex_replace(
457 source, lineEnd, lineEndFormat, boost::match_default | boost::format_all);
463 const boost::regex lineCommand(
464 "(LINE[ \t]*=[ \t]*\\([^\\)]*\\))[ \t]*,[^;]*;", boost::regex::icase);
465 const std::string firstSubExpression(
"\\1;");
466 source = boost::regex_replace(source, lineCommand, firstSubExpression);
471 unsigned int getMinimalSignificantDigits(
double num,
const unsigned int maxDigits) {
473 snprintf(buf, 32,
"%.*f", maxDigits + 1, num);
474 std::string numStr(buf);
475 unsigned int length = numStr.length();
477 unsigned int numDigits = maxDigits;
479 while (i < maxDigits + 1 && numStr[length - i] ==
'0') {
487 std::string round2string(
double num,
const unsigned int maxDigits) {
490 snprintf(buf, 64,
"%.*f", getMinimalSignificantDigits(num, maxDigits), num);
492 return std::string(buf);
503 std::string input = parseInput();
507 std::ofstream pos(fname);
509 for (; it !=
end; ++it) {
510 std::shared_ptr<Component> element = (*it).getElement();
511 std::string elementName = element->getName();
512 const boost::regex replacePSI(
513 "(" + elementName +
"\\s*:[^\\n]*)PSI\\s*=[^,;]*,?", boost::regex::icase);
514 input = boost::regex_replace(input, replacePSI,
"\\1\\2");
516 const boost::regex replaceELEMEDGE(
517 "(" + elementName +
"\\s*:[^\\n]*)ELEMEDGE\\s*=[^,;]*(.)", boost::regex::icase);
523 for (
unsigned int d = 0; d < 3; ++d)
527 (std::abs(origin(0)) > 1e-10 ?
"X = " + round2string(origin(0), 10) +
", " :
"");
529 (std::abs(origin(1)) > 1e-10 ?
"Y = " + round2string(origin(1), 10) +
", " :
"");
531 (std::abs(origin(2)) > 1e-10 ?
"Z = " + round2string(origin(2), 10) +
", " :
"");
534 (orient(0) > 1e-10 ?
"THETA = " + round2string(orient(0), 6) +
" * PI / 180, " :
"");
536 (orient(1) > 1e-10 ?
"PHI = " + round2string(orient(1), 6) +
" * PI / 180, " :
"");
538 (orient(2) > 1e-10 ?
"PSI = " + round2string(orient(2), 6) +
" * PI / 180, " :
"");
539 std::string coordTrafo = x + y + z + theta + phi + psi;
540 if (coordTrafo.length() > 2) {
541 coordTrafo = coordTrafo.substr(0, coordTrafo.length() - 2);
544 std::string position = (
"\\1" + coordTrafo +
"\\2");
546 input = boost::regex_replace(input, replaceELEMEDGE, position);
549 const boost::regex empty(
"##EMPTY_LINE##");
550 const std::string emptyFormat(
"\n");
551 input = boost::regex_replace(input, empty, emptyFormat);
553 pos << input << std::endl;
559 double designEnergy = 0.0;
560 for (; it !=
end; ++it) {
561 std::shared_ptr<Component> element = (*it).getElement();
562 (*it).setOn(designEnergy);
563 element->goOnline(designEnergy);
ippl::Vector< T, Dim > Vector_t
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
std::list< ClassicField > FieldList
KOKKOS_INLINE_FUNCTION double euclidean_norm(const Vector_t< T, D > &v)
std::string combineFilePath(std::initializer_list< std::string > ilist)
Vector_t< double, 3 > getTaitBryantAngles(Quaternion rotation, const std::string &)
std::string getInputBasename()
get input file name without extension
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
ippl::Vector< double, 3 > getOrigin() const
Quaternion getRotation() const
Quaternion conjugate() const
ippl::Vector< double, 3 > rotate(const ippl::Vector< double, 3 > &) const
FieldList getElementByType(ElementType)
Vector_t< double, 3 > rotateToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
void positionElementRelative(std::shared_ptr< ElementBase >)
void merge(OpalBeamline &rhs)
unsigned long getFieldAt(const unsigned int &, const Vector_t< double, 3 > &, const long &, const double &, Vector_t< double, 3 > &, Vector_t< double, 3 > &)
CoordinateSystemTrafo coordTransformationTo_m
void print(Inform &) const
Vector_t< double, 3 > transformToLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
std::set< std::shared_ptr< Component > > getElements(const Vector_t< double, 3 > &x)
void switchElements(const double &, const double &, const double &kineticEnergy, const bool &nomonitors=false)
Vector_t< double, 3 > rotateFromLocalCS(const std::shared_ptr< Component > &comp, const Vector_t< double, 3 > &r) const
void swap(OpalBeamline &rhs)
void add(const ElementBase &element)
void write(const std::string &fname)
static bool SortAsc(const ClassicField &fle1, const ClassicField &fle2)