OPAL (Object Oriented Parallel Accelerator Library)  2024.1
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"
71 #include "Structure/BoundingBox.h"
74 
75 #include <boost/optional.hpp>
76 
77 #include <map>
78 #include <queue>
79 #include <string>
80 
81 class BeamlineVisitor;
82 class BoundaryGeometry;
83 class Channel;
84 class ConstChannel;
86 class WakeFunction;
87 
88 enum class ElementType: unsigned short {
89  ANY,
90  BEAMLINE,
92  CORRECTOR,
93  CYCLOTRON,
94  DEGRADER,
95  DRIFT,
97  MARKER,
98  MONITOR,
100  MULTIPOLE,
101  MULTIPOLET,
102  OFFSET,
103  OUTPUTPLANE,
104  PROBE,
105  RBEND,
106  RBEND3D,
107  RFCAVITY,
108  RING,
109  SBEND,
110  SBEND3D,
111  SEPTUM,
112  SOLENOID,
113  SOURCE,
114  STRIPPER,
116  UNDULATOR,
117  VACUUM,
119 };
120 
121 enum class ApertureType: unsigned short {
122  RECTANGULAR,
123  ELLIPTICAL,
126 };
127 
128 class ElementBase: public RCObject {
129 
130 public:
132  explicit ElementBase(const std::string &name);
133 
134  ElementBase();
135  ElementBase(const ElementBase &);
136  virtual ~ElementBase();
137 
139  virtual const std::string &getName() const;
140 
142  virtual void setName(const std::string &name);
143 
145  virtual ElementType getType() const = 0;
146 
147  std::string getTypeString() const;
148  static std::string getTypeString(ElementType type);
149 
151  // Return the element geometry.
152  // Version for non-constant object.
153  virtual BGeometryBase &getGeometry() = 0;
154 
156  // Return the element geometry
157  // Version for constant object.
158  virtual const BGeometryBase &getGeometry() const = 0;
159 
161  // Return the entire arc length measured along the design orbit
162  virtual double getArcLength() const;
163 
165  // Return the design length defined by the geometry.
166  // This may be the arc length or the straight length.
167  virtual double getElementLength() const;
168 
170  // Set the design length defined by the geometry.
171  // This may be the arc length or the straight length.
172  virtual void setElementLength(double length);
173 
174  virtual void getElementDimensions(double &begin,
175  double &end) const {
176  begin = 0.0;
177  end = getElementLength();
178  }
179 
181  // Return the arc length from the entrance to the origin of the element
182  // (origin >= 0)
183  virtual double getOrigin() const;
184 
186  // Return the arc length from the origin to the entrance of the element
187  // (entrance <= 0)
188  virtual double getEntrance() const;
189 
191  // Return the arc length from the origin to the exit of the element
192  // (exit >= 0)
193  virtual double getExit() const;
194 
196  // Return the transform of the local coordinate system from the
197  // position [b]fromS[/b] to the position [b]toS[/b].
198  virtual Euclid3D getTransform(double fromS, double toS) const;
199 
201  // Equivalent to getTransform(0.0, s).
202  // Return the transform of the local coordinate system from the
203  // origin and [b]s[/b].
204  virtual Euclid3D getTransform(double s) const;
205 
207  // Equivalent to getTransform(getEntrance(), getExit()).
208  // Return the transform of the local coordinate system from the
209  // entrance to the exit of the element.
210  virtual Euclid3D getTotalTransform() const;
211 
213  // Equivalent to getTransform(0.0, getEntrance()).
214  // Return the transform of the local coordinate system from the
215  // origin to the entrance of the element.
216  virtual Euclid3D getEntranceFrame() const;
217 
219  // Equivalent to getTransform(0.0, getExit()).
220  // Return the transform of the local coordinate system from the
221  // origin to the exit of the element.
222  virtual Euclid3D getExitFrame() const;
223 
225  // Returns the entrance patch (transformation) which is used to transform
226  // the global geometry to the local geometry for a misaligned element
227  // at its entrance. The default behaviour returns identity transformation.
228  // This function should be overridden by derived concrete classes which
229  // model complex geometries.
230  virtual Euclid3D getEntrancePatch() const;
231 
233  // Returns the entrance patch (transformation) which is used to transform
234  // the local geometry to the global geometry for a misaligned element
235  // at its exit. The default behaviour returns identity transformation.
236  // This function should be overridden by derived concrete classes which
237  // model complex geometries.
238  virtual Euclid3D getExitPatch() const;
239 
241  // If the attribute does not exist, return zero.
242  virtual double getAttribute(const std::string &aKey) const;
243 
245  // If the attribute exists, return true, otherwise false.
246  virtual bool hasAttribute(const std::string &aKey) const;
247 
249  virtual void removeAttribute(const std::string &aKey);
250 
252  virtual void setAttribute(const std::string &aKey, double val);
253 
255  // This method constructs a Channel permitting read/write access to
256  // the attribute [b]aKey[/b] and returns it.
257  // If the attribute does not exist, it returns nullptr.
258  virtual Channel *getChannel(const std::string &aKey, bool create = false);
259 
261  // This method constructs a Channel permitting read-only access to
262  // the attribute [b]aKey[/b] and returns it.
263  // If the attribute does not exist, it returns nullptr.
264  virtual const ConstChannel *getConstChannel(const std::string &aKey) const;
265 
267  // This method must be overridden by derived classes. It should call the
268  // method of the visitor corresponding to the element class.
269  // If any error occurs, this method throws an exception.
270  virtual void accept(BeamlineVisitor &visitor) const = 0;
271 
273  // Return an identical deep copy of the element.
274  virtual ElementBase *clone() const = 0;
275 
277  // Return a fresh copy of any beam line structure is made,
278  // but sharable elements remain shared.
279  virtual ElementBase *copyStructure();
280 
282  bool isSharable() const;
283 
285  // The whole structure depending on [b]this[/b] is marked as sharable.
286  // After this call a [b]copyStructure()[/b] call reuses the element.
287  virtual void makeSharable();
288 
290  // This method stores all attributes contained in the AttributeSet to
291  // "*this". The return value [b]true[/b] indicates success.
292  bool update(const AttributeSet &);
293 
295  void setElementPosition(double elemedge);
296  double getElementPosition() const;
297  bool isElementPositionSet() const;
300  virtual void setBoundaryGeometry(BoundaryGeometry *geo);
301 
303  virtual BoundaryGeometry *getBoundaryGeometry() const;
304 
305  virtual bool hasBoundaryGeometry() const;
306 
308  virtual void setWake(WakeFunction *wf);
309 
311  virtual WakeFunction *getWake() const;
312 
313  virtual bool hasWake() const;
314 
316 
318 
319  virtual bool hasParticleMatterInteraction() const;
320 
323  void releasePosition();
324  void fixPosition();
325  bool isPositioned() const;
326 
327  virtual CoordinateSystemTrafo getEdgeToBegin() const;
328  virtual CoordinateSystemTrafo getEdgeToEnd() const;
329 
330  void setAperture(const ApertureType& type, const std::vector<double> &args);
331  std::pair<ApertureType, std::vector<double> > getAperture() const;
332 
333  virtual bool isInside(const Vector_t &r) const;
334 
335  void setMisalignment(const CoordinateSystemTrafo &cst);
336 
337  void getMisalignment(double &x, double &y, double &s) const;
339 
340  void setActionRange(const std::queue<std::pair<double, double> > &range);
341  void setCurrentSCoordinate(double s);
342 
344  void setRotationAboutZ(double rotation);
345  double getRotationAboutZ() const;
346 
347  virtual BoundingBox getBoundingBoxInLabCoords() const;
348 
349  virtual int getRequiredNumberOfTimeSteps() const;
350 
352  void setOutputFN(std::string fn);
354  std::string getOutputFN() const;
355 
356  void setFlagDeleteOnTransverseExit(bool = true);
357  bool getFlagDeleteOnTransverseExit() const;
358 
359 protected:
360  bool isInsideTransverse(const Vector_t &r) const;
361 
362  // Sharable flag.
363  // If this flag is true, the element is always shared.
364  mutable bool shareFlag;
365 
368 
369  std::pair<ApertureType, std::vector<double> > aperture_m;
370 
372 
374 
375 
376 private:
377  // Not implemented.
378  void operator=(const ElementBase &);
379 
380  // The element's name
381  std::string elementID;
382 
383  static const std::map<ElementType, std::string> elementTypeToString_s;
384 
385  // The user-defined set of attributes.
387 
389 
391 
393 
399  std::queue<std::pair<double, double> > actionRange_m;
400 
401  std::string outputfn_m;
404 };
405 
406 
407 // Inline functions.
408 // ------------------------------------------------------------------------
409 
410 inline
412 { return getGeometry().getArcLength(); }
413 
414 inline
416 { return getGeometry().getElementLength(); }
417 
418 inline
419 void ElementBase::setElementLength(double length)
420 { getGeometry().setElementLength(length); }
421 
422 inline
424 { return getGeometry().getOrigin(); }
425 
426 inline
428 { return getGeometry().getEntrance(); }
429 
430 inline
431 double ElementBase::getExit() const
432 { return getGeometry().getExit(); }
433 
434 inline
435 Euclid3D ElementBase::getTransform(double fromS, double toS) const
436 { return getGeometry().getTransform(fromS, toS); }
437 
438 inline
440 { return getGeometry().getTotalTransform(); }
441 
442 inline
444 { return getGeometry().getTransform(s); }
445 
446 inline
448 { return getGeometry().getEntranceFrame(); }
449 
450 inline
452 { return getGeometry().getExitFrame(); }
453 
454 inline
456 { return getGeometry().getEntrancePatch(); }
457 
458 inline
460 { return getGeometry().getExitPatch(); }
461 
462 inline
464 { return shareFlag; }
465 
466 inline
468 { return wake_m; }
469 
470 inline
472 { return wake_m != nullptr; }
473 
474 inline
476 { return bgeometry_m; }
477 
478 inline
480 { return bgeometry_m != nullptr; }
481 
482 inline
484 { return parmatint_m; }
485 
486 inline
488 { return parmatint_m != nullptr; }
489 
490 inline
492  if (positionIsFixed) return;
493 
494  csTrafoGlobal2Local_m = trafo;
495 }
496 
497 inline
499  return csTrafoGlobal2Local_m;
500 }
501 
502 inline
504  CoordinateSystemTrafo ret(Vector_t(0, 0, 0),
505  Quaternion(1, 0, 0, 0));
506 
507  return ret;
508 }
509 
510 inline
513  Quaternion(1, 0, 0, 0));
514 
515  return ret;
516 }
517 
518 inline
519 void ElementBase::setAperture(const ApertureType& type, const std::vector<double> &args) {
520  aperture_m.first = type;
521  aperture_m.second = args;
522 }
523 
524 inline
525 std::pair<ApertureType, std::vector<double> > ElementBase::getAperture() const {
526  return aperture_m;
527 }
528 
529 inline
530 bool ElementBase::isInside(const Vector_t &r) const {
531  const double length = getElementLength();
532  return r(2) >= 0.0 && r(2) < length && isInsideTransverse(r);
533 }
534 
535 inline
537  misalignment_m = cst;
538 }
539 
540 inline
542  return misalignment_m;
543 }
544 
545 inline
547  positionIsFixed = false;
548 }
549 
550 inline
552  positionIsFixed = true;
553 }
554 
555 inline
557  return positionIsFixed;
558 }
559 
560 inline
561 void ElementBase::setActionRange(const std::queue<std::pair<double, double> > &range) {
562  actionRange_m = range;
563 
564  if (!actionRange_m.empty())
565  elementEdge_m = actionRange_m.front().first;
566 }
567 
568 inline
569 void ElementBase::setRotationAboutZ(double rotation) {
570  rotationZAxis_m = rotation;
571 }
572 
573 inline
575  return rotationZAxis_m;
576 }
577 
578 inline
579 std::string ElementBase::getTypeString() const
580 { return getTypeString(getType());}
581 
582 inline
583 void ElementBase::setElementPosition(double elemedge) {
584  elementPosition_m = elemedge;
585  elemedgeSet_m = true;
586 }
587 
588 inline
590  if (elemedgeSet_m)
591  return elementPosition_m;
592 
593  throw GeneralClassicException("ElementBase::getElementPosition()",
594  std::string("ELEMEDGE for \"") + getName() + "\" not set");
595 }
596 
597 inline
599  return elemedgeSet_m;
600 }
601 
602 inline
604  return 10;
605 }
606 
607 inline
609 {
611 }
612 
613 inline
615 {
617 }
618 
619 #endif // CLASSIC_ElementBase_HH
static const std::map< ElementType, std::string > elementTypeToString_s
Definition: ElementBase.h:383
virtual BGeometryBase & getGeometry()=0
Get geometry.
virtual double getElementLength() const =0
Get geometry length.
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: ElementBase.h:455
virtual void setBoundaryGeometry(BoundaryGeometry *geo)
Abstract base class for reference counted objects.
Definition: RCObject.h:40
bool shareFlag
Definition: ElementBase.h:364
virtual double getArcLength() const
Get arc length.
Definition: ElementBase.h:411
virtual bool isInside(const Vector_t &r) const
Definition: ElementBase.h:530
CoordinateSystemTrafo getCSTrafoGlobal2Local() const
Definition: ElementBase.h:498
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
std::string elementID
Definition: ElementBase.h:381
void setCSTrafoGlobal2Local(const CoordinateSystemTrafo &ori)
Definition: ElementBase.h:491
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: Geometry.cpp:51
virtual const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.
bool positionIsFixed
Definition: ElementBase.h:394
virtual BoundaryGeometry * getBoundaryGeometry() const
return the attached boundary geometrt object if there is any
Definition: ElementBase.h:475
void setActionRange(const std::queue< std::pair< double, double > > &range)
Definition: ElementBase.h:561
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
virtual CoordinateSystemTrafo getEdgeToEnd() const
Definition: ElementBase.h:511
Definition: TSVMeta.h:24
ParticleMatterInteractionHandler * parmatint_m
Definition: ElementBase.h:392
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
bool isElementPositionSet() const
Definition: ElementBase.h:598
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: Geometry.cpp:71
void operator=(const ElementBase &)
Abstract base class for accelerator geometry classes.
Definition: Geometry.h:43
virtual double getExit() const
Get exit position.
Definition: ElementBase.h:431
virtual double getArcLength() const =0
Get arc length.
virtual Euclid3D getExitFrame() const
Get transform.
Definition: Geometry.cpp:66
std::string getTypeString() const
Definition: ElementBase.h:579
virtual Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
virtual Euclid3D getExitPatch() const
Get patch.
Definition: ElementBase.h:459
virtual const std::string & getName() const
Get element name.
clearpage the user may choose between constant or variable radius This model includes fringe fields begin
Definition: multipole_t.tex:6
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual Euclid3D getExitFrame() const
Get transform.
Definition: ElementBase.h:451
ApertureType
Definition: ElementBase.h:121
virtual void makeSharable()
Set sharable flag.
virtual double getOrigin() const
Get origin position.
Definition: ElementBase.h:423
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
void releasePosition()
Definition: ElementBase.h:546
double elementEdge_m
Definition: ElementBase.h:371
void setFlagDeleteOnTransverseExit(bool=true)
Definition: ElementBase.h:608
virtual bool hasBoundaryGeometry() const
Definition: ElementBase.h:479
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: ElementBase.h:447
bool isSharable() const
Test if the element can be shared.
Definition: ElementBase.h:463
virtual bool hasParticleMatterInteraction() const
Definition: ElementBase.h:487
virtual Euclid3D getExitPatch() const
Get patch.
Definition: Geometry.cpp:76
bool deleteOnTransverseExit_m
Definition: ElementBase.h:403
BoundaryGeometry * bgeometry_m
Definition: ElementBase.h:390
bool getFlagDeleteOnTransverseExit() const
Definition: ElementBase.h:614
void setRotationAboutZ(double rotation)
Set rotation about z axis in bend frame.
Definition: ElementBase.h:569
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
virtual CoordinateSystemTrafo getEdgeToBegin() const
Definition: ElementBase.h:503
CoordinateSystemTrafo csTrafoGlobal2Local_m
Definition: ElementBase.h:366
bool elemedgeSet_m
Definition: ElementBase.h:397
void setOutputFN(std::string fn)
Set output filename.
std::string outputfn_m
Definition: ElementBase.h:401
std::queue< std::pair< double, double > > actionRange_m
Definition: ElementBase.h:399
virtual WakeFunction * getWake() const
return the attached wake object if there is any
Definition: ElementBase.h:467
double rotationZAxis_m
Definition: ElementBase.h:373
Abstract interface for read/write access to variable.
Definition: Channel.h:32
virtual ~ElementBase()
void setMisalignment(const CoordinateSystemTrafo &cst)
Definition: ElementBase.h:536
ElementType
Definition: ElementBase.h:88
virtual ElementType getType() const =0
Get element type std::string.
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:435
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:415
void setElementPosition(double elemedge)
Access to ELEMEDGE attribute.
Definition: ElementBase.h:583
double getElementPosition() const
Definition: ElementBase.h:589
double getRotationAboutZ() const
Definition: ElementBase.h:574
bool isPositioned() const
Definition: ElementBase.h:556
virtual void getElementDimensions(double &begin, double &end) const
Definition: ElementBase.h:174
virtual double getEntrance() const
Get entrance position.
Definition: Geometry.cpp:41
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: Geometry.cpp:61
virtual bool hasAttribute(const std::string &aKey) const
Test for existence of an attribute.
CoordinateSystemTrafo getMisalignment() const
Definition: ElementBase.h:541
virtual ElementBase * copyStructure()
Make a structural copy.
AttributeSet userAttribs
Definition: ElementBase.h:386
bool isInsideTransverse(const Vector_t &r) const
std::pair< ApertureType, std::vector< double > > getAperture() const
Definition: ElementBase.h:525
virtual void setElementLength(double length)
Set geometry length.
Definition: Geometry.cpp:32
const std::string name
Map of std::string versus double value.
Definition: AttributeSet.h:41
virtual int getRequiredNumberOfTimeSteps() const
Definition: ElementBase.h:603
virtual Euclid3D getTransform(double fromS, double toS) const =0
Get transform.
virtual ElementBase * clone() const =0
Return clone.
WakeFunction * wake_m
Definition: ElementBase.h:388
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:419
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:483
Displacement and rotation in space.
Definition: Euclid3D.h:68
virtual void setName(const std::string &name)
Set element name.
CoordinateSystemTrafo misalignment_m
Definition: ElementBase.h:367
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: ElementBase.h:439
void setAperture(const ApertureType &type, const std::vector< double > &args)
Definition: ElementBase.h:519
bool update(const AttributeSet &)
Update element.
void setCurrentSCoordinate(double s)
virtual BoundingBox getBoundingBoxInLabCoords() const
std::string getOutputFN() const
Get output filename.
double elementPosition_m
ELEMEDGE attribute.
Definition: ElementBase.h:396
std::pair< ApertureType, std::vector< double > > aperture_m
Definition: ElementBase.h:369
SDDS1 &description type
Definition: test.stat:4
end
Definition: multipole_t.tex:9
virtual double getOrigin() const
Get origin position.
Definition: Geometry.cpp:36
virtual double getEntrance() const
Get entrance position.
Definition: ElementBase.h:427
void fixPosition()
Definition: ElementBase.h:551
virtual bool hasWake() const
Definition: ElementBase.h:471
virtual double getExit() const
Get exit position.
Definition: Geometry.cpp:46
virtual void removeAttribute(const std::string &aKey)
Remove an existing attribute.
Abstract interface for read-only access to variable.
Definition: ConstChannel.h:29