OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
74
75#include <boost/optional.hpp>
76
77#include <map>
78#include <queue>
79#include <string>
80
81class BeamlineVisitor;
83class Channel;
84class ConstChannel;
86class WakeFunction;
87
88enum class ElementType: unsigned short {
89 ANY,
95 DRIFT,
97 MARKER,
98 MONITOR,
100 MULTIPOLE,
102 OFFSET,
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
121enum class ApertureType: unsigned short {
126};
127
128class ElementBase: public RCObject {
129
130public:
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.
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;
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
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
348
349 virtual int getRequiredNumberOfTimeSteps() const;
350
352 void setOutputFN(std::string fn);
354 std::string getOutputFN() const;
355
356 void setFlagDeleteOnTransverseExit(bool = true);
358
359protected:
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
376private:
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
410inline
412{ return getGeometry().getArcLength(); }
413
414inline
416{ return getGeometry().getElementLength(); }
417
418inline
420{ getGeometry().setElementLength(length); }
421
422inline
424{ return getGeometry().getOrigin(); }
425
426inline
428{ return getGeometry().getEntrance(); }
429
430inline
432{ return getGeometry().getExit(); }
433
434inline
435Euclid3D ElementBase::getTransform(double fromS, double toS) const
436{ return getGeometry().getTransform(fromS, toS); }
437
438inline
440{ return getGeometry().getTotalTransform(); }
441
442inline
444{ return getGeometry().getTransform(s); }
445
446inline
448{ return getGeometry().getEntranceFrame(); }
449
450inline
452{ return getGeometry().getExitFrame(); }
453
454inline
456{ return getGeometry().getEntrancePatch(); }
457
458inline
460{ return getGeometry().getExitPatch(); }
461
462inline
464{ return shareFlag; }
465
466inline
468{ return wake_m; }
469
470inline
472{ return wake_m != nullptr; }
473
474inline
476{ return bgeometry_m; }
477
478inline
480{ return bgeometry_m != nullptr; }
481
482inline
484{ return parmatint_m; }
485
486inline
488{ return parmatint_m != nullptr; }
489
490inline
492 if (positionIsFixed) return;
493
494 csTrafoGlobal2Local_m = trafo;
495}
496
497inline
500}
501
502inline
504 CoordinateSystemTrafo ret(Vector_t(0, 0, 0),
505 Quaternion(1, 0, 0, 0));
506
507 return ret;
508}
509
510inline
513 Quaternion(1, 0, 0, 0));
514
515 return ret;
516}
517
518inline
519void ElementBase::setAperture(const ApertureType& type, const std::vector<double> &args) {
520 aperture_m.first = type;
521 aperture_m.second = args;
522}
523
524inline
525std::pair<ApertureType, std::vector<double> > ElementBase::getAperture() const {
526 return aperture_m;
527}
528
529inline
530bool 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
535inline
537 misalignment_m = cst;
538}
539
540inline
542 return misalignment_m;
543}
544
545inline
547 positionIsFixed = false;
548}
549
550inline
552 positionIsFixed = true;
553}
554
555inline
557 return positionIsFixed;
558}
559
560inline
561void 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
568inline
569void ElementBase::setRotationAboutZ(double rotation) {
570 rotationZAxis_m = rotation;
571}
572
573inline
575 return rotationZAxis_m;
576}
577
578inline
579std::string ElementBase::getTypeString() const
580{ return getTypeString(getType());}
581
582inline
583void ElementBase::setElementPosition(double elemedge) {
584 elementPosition_m = elemedge;
585 elemedgeSet_m = true;
586}
587
588inline
590 if (elemedgeSet_m)
591 return elementPosition_m;
592
593 throw GeneralClassicException("ElementBase::getElementPosition()",
594 std::string("ELEMEDGE for \"") + getName() + "\" not set");
595}
596
597inline
599 return elemedgeSet_m;
600}
601
602inline
604 return 10;
605}
606
607inline
609{
611}
612
613inline
615{
617}
618
619#endif // CLASSIC_ElementBase_HH
ElementType
Definition: ElementBase.h:88
ApertureType
Definition: ElementBase.h:121
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
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)
attach a boundary geometry field to the element
void setElementPosition(double elemedge)
Access to ELEMEDGE attribute.
Definition: ElementBase.h:583
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:411
void fixPosition()
Definition: ElementBase.h:551
static const std::map< ElementType, std::string > elementTypeToString_s
Definition: ElementBase.h:383
virtual double getExit() const
Get exit position.
Definition: ElementBase.h:431
void setAperture(const ApertureType &type, const std::vector< double > &args)
Definition: ElementBase.h:519
virtual const BGeometryBase & getGeometry() const =0
Get geometry.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:483
bool getFlagDeleteOnTransverseExit() const
Definition: ElementBase.h:614
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:415
bool update(const AttributeSet &)
Update element.
virtual Euclid3D getExitPatch() const
Get patch.
Definition: ElementBase.h:459
bool isPositioned() const
Definition: ElementBase.h:556
std::pair< ApertureType, std::vector< double > > getAperture() const
Definition: ElementBase.h:525
std::string outputfn_m
The name of the outputfile.
Definition: ElementBase.h:401
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:419
void releasePosition()
Definition: ElementBase.h:546
ParticleMatterInteractionHandler * parmatint_m
Definition: ElementBase.h:392
virtual Euclid3D getExitFrame() const
Get transform.
Definition: ElementBase.h:451
CoordinateSystemTrafo misalignment_m
Definition: ElementBase.h:367
void setMisalignment(const CoordinateSystemTrafo &cst)
Definition: ElementBase.h:536
CoordinateSystemTrafo getMisalignment() const
Definition: ElementBase.h:541
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 &)
bool deleteOnTransverseExit_m
Definition: ElementBase.h:403
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
virtual ElementBase * clone() const =0
Return clone.
bool elemedgeSet_m
Definition: ElementBase.h:397
CoordinateSystemTrafo getCSTrafoGlobal2Local() const
Definition: ElementBase.h:498
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual double getOrigin() const
Get origin position.
Definition: ElementBase.h:423
bool isInsideTransverse(const Vector_t &r) const
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: ElementBase.h:447
std::string getOutputFN() const
Get output filename.
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:530
std::pair< ApertureType, std::vector< double > > aperture_m
Definition: ElementBase.h:369
virtual bool hasBoundaryGeometry() const
Definition: ElementBase.h:479
double elementPosition_m
ELEMEDGE attribute.
Definition: ElementBase.h:396
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
void setFlagDeleteOnTransverseExit(bool=true)
Definition: ElementBase.h:608
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:435
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:388
bool isElementPositionSet() const
Definition: ElementBase.h:598
bool positionIsFixed
Definition: ElementBase.h:394
void setRotationAboutZ(double rotation)
Set rotation about z axis in bend frame.
Definition: ElementBase.h:569
std::queue< std::pair< double, double > > actionRange_m
Definition: ElementBase.h:399
void setCSTrafoGlobal2Local(const CoordinateSystemTrafo &ori)
Definition: ElementBase.h:491
virtual BoundaryGeometry * getBoundaryGeometry() const
return the attached boundary geometrt object if there is any
Definition: ElementBase.h:475
virtual double getEntrance() const
Get entrance position.
Definition: ElementBase.h:427
virtual CoordinateSystemTrafo getEdgeToBegin() const
Definition: ElementBase.h:503
std::string getTypeString() const
Definition: ElementBase.h:579
double getElementPosition() const
Definition: ElementBase.h:589
AttributeSet userAttribs
Definition: ElementBase.h:386
virtual ElementBase * copyStructure()
Make a structural copy.
double getRotationAboutZ() const
Definition: ElementBase.h:574
BoundaryGeometry * bgeometry_m
Definition: ElementBase.h:390
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: ElementBase.h:439
bool isSharable() const
Test if the element can be shared.
Definition: ElementBase.h:463
bool shareFlag
Definition: ElementBase.h:364
void setActionRange(const std::queue< std::pair< double, double > > &range)
Definition: ElementBase.h:561
virtual int getRequiredNumberOfTimeSteps() const
Definition: ElementBase.h:603
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: ElementBase.h:455
virtual WakeFunction * getWake() const
return the attached wake object if there is any
Definition: ElementBase.h:467
void setCurrentSCoordinate(double s)
std::string elementID
Definition: ElementBase.h:381
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
virtual bool hasParticleMatterInteraction() const
Definition: ElementBase.h:487
virtual CoordinateSystemTrafo getEdgeToEnd() const
Definition: ElementBase.h:511
virtual bool hasWake() const
Definition: ElementBase.h:471
virtual void getElementDimensions(double &begin, double &end) const
Definition: ElementBase.h:174
double rotationZAxis_m
Definition: ElementBase.h:373
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
virtual BGeometryBase & getGeometry()=0
Get geometry.
double elementEdge_m
Definition: ElementBase.h:371
virtual BoundingBox getBoundingBoxInLabCoords() const
CoordinateSystemTrafo csTrafoGlobal2Local_m
Definition: ElementBase.h:366
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