OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
ElementBase.h
Go to the documentation of this file.
1 //
2 // Class ElementBase
3 // The very base class for beam line representation objects. A beam line
4 // is modelled as a composite structure having a single root object
5 // (the top level beam line), which contains both ``single'' leaf-type
6 // elements (Components), as well as sub-lines (composites).
7 //
8 // Interface for basic beam line object.
9 // This class defines the abstract interface for all objects which can be
10 // contained in a beam line. ElementBase forms the base class for two distinct
11 // but related hierarchies of objects:
12 // [OL]
13 // [LI]
14 // A set of concrete accelerator element classes, which compose the standard
15 // accelerator component library (SACL).
16 // [LI]
17 // [/OL]
18 // Instances of the concrete classes for single elements are by default
19 // sharable. Instances of beam lines and integrators are by
20 // default non-sharable, but they may be made sharable by a call to
21 // [b]makeSharable()[/b].
22 // [p]
23 // An ElementBase object can return two lengths, which may be different:
24 // [OL]
25 // [LI]
26 // The arc length along the geometry.
27 // [LI]
28 // The design length, often measured along a straight line.
29 // [/OL]
30 // Class ElementBase contains a map of name versus value for user-defined
31 // attributes (see file AbsBeamline/AttributeSet.hh). The map is primarily
32 // intended for processes that require algorithm-specific data in the
33 // accelerator model.
34 // [P]
35 // The class ElementBase has as base class the abstract class RCObject.
36 // Virtual derivation is used to make multiple inheritance possible for
37 // derived concrete classes. ElementBase implements three copy modes:
38 // [OL]
39 // [LI]
40 // Copy by reference: Call RCObject::addReference() and use [b]this[/b].
41 // [LI]
42 // Copy structure: use ElementBase::copyStructure().
43 // During copying of the structure, all sharable items are re-used, while
44 // all non-sharable ones are cloned.
45 // [LI]
46 // Copy by cloning: use ElementBase::clone().
47 // This returns a full deep copy.
48 // [/OL]
49 //
50 // Copyright (c) 200x - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
51 // All rights reserved
52 //
53 // This file is part of OPAL.
54 //
55 // OPAL is free software: you can redistribute it and/or modify
56 // it under the terms of the GNU General Public License as published by
57 // the Free Software Foundation, either version 3 of the License, or
58 // (at your option) any later version.
59 //
60 // You should have received a copy of the GNU General Public License
61 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
62 //
63 #ifndef CLASSIC_ElementBase_HH
64 #define CLASSIC_ElementBase_HH
65 
68 #include "Algorithms/Quaternion.h"
73 
74 #include <boost/optional.hpp>
75 
76 #include <string>
77 #include <queue>
78 
79 class BeamlineVisitor;
80 class BoundaryGeometry;
81 class Channel;
82 class ConstChannel;
84 class WakeFunction;
85 
86 class ElementBase: public RCObject {
87 
88 public:
90  explicit ElementBase(const std::string &name);
91 
92  ElementBase();
93  ElementBase(const ElementBase &);
94  virtual ~ElementBase();
95 
97  virtual const std::string &getName() const;
98 
100  virtual void setName(const std::string &name);
101 
106  };
107 
136  , ANY};
137 
139  virtual ElementType getType() const = 0;
140 
141  std::string getTypeString() const;
142  static std::string getTypeString(ElementType type);
143 
145  // Return the element geometry.
146  // Version for non-constant object.
147  virtual BGeometryBase &getGeometry() = 0;
148 
150  // Return the element geometry
151  // Version for constant object.
152  virtual const BGeometryBase &getGeometry() const = 0;
153 
155  // Return the entire arc length measured along the design orbit
156  virtual double getArcLength() const;
157 
159  // Return the design length defined by the geometry.
160  // This may be the arc length or the straight length.
161  virtual double getElementLength() const;
162 
164  // Set the design length defined by the geometry.
165  // This may be the arc length or the straight length.
166  virtual void setElementLength(double length);
167 
168  virtual void getElementDimensions(double &begin,
169  double &end) const {
170  begin = 0.0;
171  end = getElementLength();
172  }
173 
175  // Return the arc length from the entrance to the origin of the element
176  // (origin >= 0)
177  virtual double getOrigin() const;
178 
180  // Return the arc length from the origin to the entrance of the element
181  // (entrance <= 0)
182  virtual double getEntrance() const;
183 
185  // Return the arc length from the origin to the exit of the element
186  // (exit >= 0)
187  virtual double getExit() const;
188 
190  // Return the transform of the local coordinate system from the
191  // position [b]fromS[/b] to the position [b]toS[/b].
192  virtual Euclid3D getTransform(double fromS, double toS) const;
193 
195  // Equivalent to getTransform(0.0, s).
196  // Return the transform of the local coordinate system from the
197  // origin and [b]s[/b].
198  virtual Euclid3D getTransform(double s) const;
199 
201  // Equivalent to getTransform(getEntrance(), getExit()).
202  // Return the transform of the local coordinate system from the
203  // entrance to the exit of the element.
204  virtual Euclid3D getTotalTransform() const;
205 
207  // Equivalent to getTransform(0.0, getEntrance()).
208  // Return the transform of the local coordinate system from the
209  // origin to the entrance of the element.
210  virtual Euclid3D getEntranceFrame() const;
211 
213  // Equivalent to getTransform(0.0, getExit()).
214  // Return the transform of the local coordinate system from the
215  // origin to the exit of the element.
216  virtual Euclid3D getExitFrame() const;
217 
219  // Returns the entrance patch (transformation) which is used to transform
220  // the global geometry to the local geometry for a misaligned element
221  // at its entrance. The default behaviour returns identity transformation.
222  // This function should be overridden by derived concrete classes which
223  // model complex geometries.
224  virtual Euclid3D getEntrancePatch() const;
225 
227  // Returns the entrance patch (transformation) which is used to transform
228  // the local geometry to the global geometry for a misaligned element
229  // at its exit. The default behaviour returns identity transformation.
230  // This function should be overridden by derived concrete classes which
231  // model complex geometries.
232  virtual Euclid3D getExitPatch() const;
233 
235  // If the attribute does not exist, return zero.
236  virtual double getAttribute(const std::string &aKey) const;
237 
239  // If the attribute exists, return true, otherwise false.
240  virtual bool hasAttribute(const std::string &aKey) const;
241 
243  virtual void removeAttribute(const std::string &aKey);
244 
246  virtual void setAttribute(const std::string &aKey, double val);
247 
249  // This method constructs a Channel permitting read/write access to
250  // the attribute [b]aKey[/b] and returns it.
251  // If the attribute does not exist, it returns NULL.
252  virtual Channel *getChannel(const std::string &aKey, bool create = false);
253 
255  // This method constructs a Channel permitting read-only access to
256  // the attribute [b]aKey[/b] and returns it.
257  // If the attribute does not exist, it returns NULL.
258  virtual const ConstChannel *getConstChannel(const std::string &aKey) const;
259 
261  // This method must be overridden by derived classes. It should call the
262  // method of the visitor corresponding to the element class.
263  // If any error occurs, this method throws an exception.
264  virtual void accept(BeamlineVisitor &visitor) const = 0;
265 
267  // Return an identical deep copy of the element.
268  virtual ElementBase *clone() const = 0;
269 
271  // Return a fresh copy of any beam line structure is made,
272  // but sharable elements remain shared.
273  virtual ElementBase *copyStructure();
274 
276  bool isSharable() const;
277 
279  // The whole structure depending on [b]this[/b] is marked as sharable.
280  // After this call a [b]copyStructure()[/b] call reuses the element.
281  virtual void makeSharable();
282 
284  // This method stores all attributes contained in the AttributeSet to
285  // "*this". The return value [b]true[/b] indicates success.
286  bool update(const AttributeSet &);
287 
289  void setElementPosition(double elemedge);
290  double getElementPosition() const;
291  bool isElementPositionSet() const;
294  virtual void setBoundaryGeometry(BoundaryGeometry *geo);
295 
297  virtual BoundaryGeometry *getBoundaryGeometry() const;
298 
299  virtual bool hasBoundaryGeometry() const;
300 
302  virtual void setWake(WakeFunction *wf);
303 
305  virtual WakeFunction *getWake() const;
306 
307  virtual bool hasWake() const;
308 
310 
312 
313  virtual bool hasParticleMatterInteraction() const;
314 
317  void releasePosition();
318  void fixPosition();
319  bool isPositioned() const;
320 
321  virtual CoordinateSystemTrafo getEdgeToBegin() const;
322  virtual CoordinateSystemTrafo getEdgeToEnd() const;
323 
324  void setAperture(const ApertureType& type, const std::vector<double> &args);
325  std::pair<ElementBase::ApertureType, std::vector<double> > getAperture() const;
326 
327  virtual bool isInside(const Vector_t &r) const;
328 
329  void setMisalignment(const CoordinateSystemTrafo &cst);
330 
331  void getMisalignment(double &x, double &y, double &s) const;
333 
334  void setActionRange(const std::queue<std::pair<double, double> > &range);
335  void setCurrentSCoordinate(double s);
336 
338  void setRotationAboutZ(double rotation);
339  double getRotationAboutZ() const;
340 
341  struct BoundingBox {
344 
345  static BoundingBox getBoundingBox(const std::vector<Vector_t> & points);
346 
347  void getCombinedBoundingBox(const BoundingBox & other) {
348  for (unsigned int d = 0; d < 3; ++ d) {
351  }
352  }
353 
354  bool isInside(const Vector_t &) const;
355 
356  void print(std::ostream &) const;
357 
366  boost::optional<Vector_t> getPointOfIntersection(const Vector_t & position,
367  const Vector_t & direction) const;
368  };
369 
370  virtual BoundingBox getBoundingBoxInLabCoords() const;
371 
372  virtual int getRequiredNumberOfTimeSteps() const;
373 
375  void setOutputFN(std::string fn);
377  std::string getOutputFN() const;
378 
379 
380 protected:
381  bool isInsideTransverse(const Vector_t &r) const;
382 
383  // Sharable flag.
384  // If this flag is true, the element is always shared.
385  mutable bool shareFlag;
386 
389 
390  std::pair<ApertureType, std::vector<double> > aperture_m;
391 
393 
395 
396 
397 private:
398  // Not implemented.
399  void operator=(const ElementBase &);
400 
401  // The element's name
402  std::string elementID;
403 
404  // The user-defined set of attributes.
406 
408 
410 
412 
418  std::queue<std::pair<double, double> > actionRange_m;
419 
420  std::string outputfn_m;
421 };
422 
423 
424 // Inline functions.
425 // ------------------------------------------------------------------------
426 
427 inline
429 { return getGeometry().getArcLength(); }
430 
431 inline
433 { return getGeometry().getElementLength(); }
434 
435 inline
436 void ElementBase::setElementLength(double length)
437 { getGeometry().setElementLength(length); }
438 
439 inline
441 { return getGeometry().getOrigin(); }
442 
443 inline
445 { return getGeometry().getEntrance(); }
446 
447 inline
448 double ElementBase::getExit() const
449 { return getGeometry().getExit(); }
450 
451 inline
452 Euclid3D ElementBase::getTransform(double fromS, double toS) const
453 { return getGeometry().getTransform(fromS, toS); }
454 
455 inline
457 { return getGeometry().getTotalTransform(); }
458 
459 inline
461 { return getGeometry().getTransform(s); }
462 
463 inline
465 { return getGeometry().getEntranceFrame(); }
466 
467 inline
469 { return getGeometry().getExitFrame(); }
470 
471 inline
473 { return getGeometry().getEntrancePatch(); }
474 
475 inline
477 { return getGeometry().getExitPatch(); }
478 
479 inline
481 { return shareFlag; }
482 
483 inline
485 { return wake_m; }
486 
487 inline
489 { return wake_m != NULL; }
490 
491 inline
493 { return bgeometry_m; }
494 
495 inline
497 { return bgeometry_m != NULL; }
498 
499 inline
501 { return parmatint_m; }
502 
503 inline
505 { return parmatint_m != NULL; }
506 
507 inline
509  if (positionIsFixed) return;
510 
511  csTrafoGlobal2Local_m = trafo;
512 }
513 
514 inline
516  return csTrafoGlobal2Local_m;
517 }
518 
519 inline
521  CoordinateSystemTrafo ret(Vector_t(0, 0, 0),
522  Quaternion(1, 0, 0, 0));
523 
524  return ret;
525 }
526 
527 inline
530  Quaternion(1, 0, 0, 0));
531 
532  return ret;
533 }
534 
535 inline
536 void ElementBase::setAperture(const ApertureType& type, const std::vector<double> &args) {
537  aperture_m.first = type;
538  aperture_m.second = args;
539 }
540 
541 inline
542 std::pair<ElementBase::ApertureType, std::vector<double> > ElementBase::getAperture() const {
543  return aperture_m;
544 }
545 
546 inline
547 bool ElementBase::isInside(const Vector_t &r) const {
548  const double length = getElementLength();
549  return r(2) >= 0.0 && r(2) < length && isInsideTransverse(r);
550 }
551 
552 inline
554  misalignment_m = cst;
555 }
556 
557 inline
559  return misalignment_m;
560 }
561 
562 inline
564  positionIsFixed = false;
565 }
566 
567 inline
569  positionIsFixed = true;
570 }
571 
572 inline
574  return positionIsFixed;
575 }
576 
577 inline
578 void ElementBase::setActionRange(const std::queue<std::pair<double, double> > &range) {
579  actionRange_m = range;
580 
581  if (actionRange_m.size() > 0)
582  elementEdge_m = actionRange_m.front().first;
583 }
584 
585 inline
586 void ElementBase::setRotationAboutZ(double rotation) {
587  rotationZAxis_m = rotation;
588 }
589 
590 inline
592  return rotationZAxis_m;
593 }
594 
595 inline
596 std::string ElementBase::getTypeString() const
597 { return getTypeString(getType());}
598 
599 inline
600 void ElementBase::setElementPosition(double elemedge) {
601  elementPosition_m = elemedge;
602  elemedgeSet_m = true;
603 }
604 
605 inline
607  if (elemedgeSet_m)
608  return elementPosition_m;
609 
610  throw GeneralClassicException("ElementBase::getElementPosition()",
611  std::string("ELEMEDGE for \"") + getName() + "\" not set");
612 }
613 
614 inline
616  return elemedgeSet_m;
617 }
618 
619 inline
621  return 10;
622 }
623 
624 #endif // CLASSIC_ElementBase_HH
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
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
const std::string name
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
Map of std::string versus double value.
Definition: AttributeSet.h:41
virtual void setBoundaryGeometry(BoundaryGeometry *geo)
void setElementPosition(double elemedge)
Access to ELEMEDGE attribute.
Definition: ElementBase.h:600
virtual ~ElementBase()
virtual Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
virtual void setName(const std::string &name)
Set element name.
virtual const std::string & getName() const
Get element name.
virtual void removeAttribute(const std::string &aKey)
Remove an existing attribute.
virtual double getArcLength() const
Get arc length.
Definition: ElementBase.h:428
virtual const BGeometryBase & getGeometry() const =0
Get geometry.
void fixPosition()
Definition: ElementBase.h:568
virtual double getExit() const
Get exit position.
Definition: ElementBase.h:448
void setAperture(const ApertureType &type, const std::vector< double > &args)
Definition: ElementBase.h:536
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:500
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:432
bool update(const AttributeSet &)
Update element.
virtual Euclid3D getExitPatch() const
Get patch.
Definition: ElementBase.h:476
bool isPositioned() const
Definition: ElementBase.h:573
std::string outputfn_m
Definition: ElementBase.h:420
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:436
void releasePosition()
Definition: ElementBase.h:563
ParticleMatterInteractionHandler * parmatint_m
Definition: ElementBase.h:411
virtual Euclid3D getExitFrame() const
Get transform.
Definition: ElementBase.h:468
CoordinateSystemTrafo misalignment_m
Definition: ElementBase.h:388
void setMisalignment(const CoordinateSystemTrafo &cst)
Definition: ElementBase.h:553
CoordinateSystemTrafo getMisalignment() const
Definition: ElementBase.h:558
void getMisalignment(double &x, double &y, double &s) const
virtual const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.
void operator=(const ElementBase &)
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
bool elemedgeSet_m
Definition: ElementBase.h:416
CoordinateSystemTrafo getCSTrafoGlobal2Local() const
Definition: ElementBase.h:515
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual double getOrigin() const
Get origin position.
Definition: ElementBase.h:440
virtual BoundingBox getBoundingBoxInLabCoords() const
bool isInsideTransverse(const Vector_t &r) const
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: ElementBase.h:464
std::string getOutputFN() const
Get output filename.
@ FLEXIBLECOLLIMATOR
Definition: ElementBase.h:114
virtual ElementType getType() const =0
Get element type std::string.
void setOutputFN(std::string fn)
Set output filename.
virtual bool isInside(const Vector_t &r) const
Definition: ElementBase.h:547
std::pair< ApertureType, std::vector< double > > aperture_m
Definition: ElementBase.h:390
virtual bool hasBoundaryGeometry() const
Definition: ElementBase.h:496
double elementPosition_m
ELEMEDGE attribute.
Definition: ElementBase.h:415
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:452
virtual void makeSharable()
Set sharable flag.
virtual bool hasAttribute(const std::string &aKey) const
Test for existence of an attribute.
WakeFunction * wake_m
Definition: ElementBase.h:407
bool isElementPositionSet() const
Definition: ElementBase.h:615
bool positionIsFixed
Definition: ElementBase.h:413
void setRotationAboutZ(double rotation)
Set rotation about z axis in bend frame.
Definition: ElementBase.h:586
std::queue< std::pair< double, double > > actionRange_m
Definition: ElementBase.h:418
void setCSTrafoGlobal2Local(const CoordinateSystemTrafo &ori)
Definition: ElementBase.h:508
virtual BoundaryGeometry * getBoundaryGeometry() const
return the attached boundary geometrt object if there is any
Definition: ElementBase.h:492
virtual double getEntrance() const
Get entrance position.
Definition: ElementBase.h:444
virtual CoordinateSystemTrafo getEdgeToBegin() const
Definition: ElementBase.h:520
std::string getTypeString() const
Definition: ElementBase.h:596
double getElementPosition() const
Definition: ElementBase.h:606
AttributeSet userAttribs
Definition: ElementBase.h:405
virtual ElementBase * clone() const =0
Return clone.
virtual ElementBase * copyStructure()
Make a structural copy.
double getRotationAboutZ() const
Definition: ElementBase.h:591
BoundaryGeometry * bgeometry_m
Definition: ElementBase.h:409
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: ElementBase.h:456
std::pair< ElementBase::ApertureType, std::vector< double > > getAperture() const
Definition: ElementBase.h:542
bool isSharable() const
Test if the element can be shared.
Definition: ElementBase.h:480
bool shareFlag
Definition: ElementBase.h:385
void setActionRange(const std::queue< std::pair< double, double > > &range)
Definition: ElementBase.h:578
virtual BGeometryBase & getGeometry()=0
Get geometry.
virtual int getRequiredNumberOfTimeSteps() const
Definition: ElementBase.h:620
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: ElementBase.h:472
virtual WakeFunction * getWake() const
return the attached wake object if there is any
Definition: ElementBase.h:484
void setCurrentSCoordinate(double s)
std::string elementID
Definition: ElementBase.h:402
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
virtual bool hasParticleMatterInteraction() const
Definition: ElementBase.h:504
virtual CoordinateSystemTrafo getEdgeToEnd() const
Definition: ElementBase.h:528
virtual bool hasWake() const
Definition: ElementBase.h:488
virtual void getElementDimensions(double &begin, double &end) const
Definition: ElementBase.h:168
double rotationZAxis_m
Definition: ElementBase.h:394
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
double elementEdge_m
Definition: ElementBase.h:392
CoordinateSystemTrafo csTrafoGlobal2Local_m
Definition: ElementBase.h:387
bool isInside(const Vector_t &) const
static BoundingBox getBoundingBox(const std::vector< Vector_t > &points)
void getCombinedBoundingBox(const BoundingBox &other)
Definition: ElementBase.h:347
void print(std::ostream &) const
boost::optional< Vector_t > getPointOfIntersection(const Vector_t &position, const Vector_t &direction) const
Displacement and rotation in space.
Definition: Euclid3D.h:68
Abstract base class for accelerator geometry classes.
Definition: Geometry.h:43
virtual Euclid3D getTransform(double fromS, double toS) const =0
Get transform.
virtual Euclid3D getExitFrame() const
Get transform.
Definition: Geometry.cpp:66
virtual Euclid3D getExitPatch() const
Get patch.
Definition: Geometry.cpp:76
virtual void setElementLength(double length)
Set geometry length.
Definition: Geometry.cpp:32
virtual double getEntrance() const
Get entrance position.
Definition: Geometry.cpp:41
virtual double getExit() const
Get exit position.
Definition: Geometry.cpp:46
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: Geometry.cpp:51
virtual double getElementLength() const =0
Get geometry length.
virtual double getOrigin() const
Get origin position.
Definition: Geometry.cpp:36
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: Geometry.cpp:61
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: Geometry.cpp:71
virtual double getArcLength() const =0
Get arc length.
Abstract interface for read/write access to variable.
Definition: Channel.h:32
Abstract interface for read-only access to variable.
Definition: ConstChannel.h:29
Abstract base class for reference counted objects.
Definition: RCObject.h:40
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6