OPAL (Object Oriented Parallel Accelerator Library) 2022.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
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,
103 PROBE,
104 RBEND,
105 RBEND3D,
106 RFCAVITY,
107 RING,
108 SBEND,
109 SBEND3D,
110 SEPTUM,
111 SOLENOID,
112 SOURCE,
113 STRIPPER,
115 UNDULATOR,
116 VACUUM,
118};
119
120enum class ApertureType: unsigned short {
125};
126
127class ElementBase: public RCObject {
128
129public:
131 explicit ElementBase(const std::string &name);
132
133 ElementBase();
134 ElementBase(const ElementBase &);
135 virtual ~ElementBase();
136
138 virtual const std::string &getName() const;
139
141 virtual void setName(const std::string &name);
142
144 virtual ElementType getType() const = 0;
145
146 std::string getTypeString() const;
147 static std::string getTypeString(ElementType type);
148
150 // Return the element geometry.
151 // Version for non-constant object.
153
155 // Return the element geometry
156 // Version for constant object.
157 virtual const BGeometryBase &getGeometry() const = 0;
158
160 // Return the entire arc length measured along the design orbit
161 virtual double getArcLength() const;
162
164 // Return the design length defined by the geometry.
165 // This may be the arc length or the straight length.
166 virtual double getElementLength() const;
167
169 // Set the design length defined by the geometry.
170 // This may be the arc length or the straight length.
171 virtual void setElementLength(double length);
172
173 virtual void getElementDimensions(double &begin,
174 double &end) const {
175 begin = 0.0;
177 }
178
180 // Return the arc length from the entrance to the origin of the element
181 // (origin >= 0)
182 virtual double getOrigin() const;
183
185 // Return the arc length from the origin to the entrance of the element
186 // (entrance <= 0)
187 virtual double getEntrance() const;
188
190 // Return the arc length from the origin to the exit of the element
191 // (exit >= 0)
192 virtual double getExit() const;
193
195 // Return the transform of the local coordinate system from the
196 // position [b]fromS[/b] to the position [b]toS[/b].
197 virtual Euclid3D getTransform(double fromS, double toS) const;
198
200 // Equivalent to getTransform(0.0, s).
201 // Return the transform of the local coordinate system from the
202 // origin and [b]s[/b].
203 virtual Euclid3D getTransform(double s) const;
204
206 // Equivalent to getTransform(getEntrance(), getExit()).
207 // Return the transform of the local coordinate system from the
208 // entrance to the exit of the element.
209 virtual Euclid3D getTotalTransform() const;
210
212 // Equivalent to getTransform(0.0, getEntrance()).
213 // Return the transform of the local coordinate system from the
214 // origin to the entrance of the element.
215 virtual Euclid3D getEntranceFrame() const;
216
218 // Equivalent to getTransform(0.0, getExit()).
219 // Return the transform of the local coordinate system from the
220 // origin to the exit of the element.
221 virtual Euclid3D getExitFrame() const;
222
224 // Returns the entrance patch (transformation) which is used to transform
225 // the global geometry to the local geometry for a misaligned element
226 // at its entrance. The default behaviour returns identity transformation.
227 // This function should be overridden by derived concrete classes which
228 // model complex geometries.
229 virtual Euclid3D getEntrancePatch() const;
230
232 // Returns the entrance patch (transformation) which is used to transform
233 // the local geometry to the global geometry for a misaligned element
234 // at its exit. The default behaviour returns identity transformation.
235 // This function should be overridden by derived concrete classes which
236 // model complex geometries.
237 virtual Euclid3D getExitPatch() const;
238
240 // If the attribute does not exist, return zero.
241 virtual double getAttribute(const std::string &aKey) const;
242
244 // If the attribute exists, return true, otherwise false.
245 virtual bool hasAttribute(const std::string &aKey) const;
246
248 virtual void removeAttribute(const std::string &aKey);
249
251 virtual void setAttribute(const std::string &aKey, double val);
252
254 // This method constructs a Channel permitting read/write access to
255 // the attribute [b]aKey[/b] and returns it.
256 // If the attribute does not exist, it returns nullptr.
257 virtual Channel *getChannel(const std::string &aKey, bool create = false);
258
260 // This method constructs a Channel permitting read-only access to
261 // the attribute [b]aKey[/b] and returns it.
262 // If the attribute does not exist, it returns nullptr.
263 virtual const ConstChannel *getConstChannel(const std::string &aKey) const;
264
266 // This method must be overridden by derived classes. It should call the
267 // method of the visitor corresponding to the element class.
268 // If any error occurs, this method throws an exception.
269 virtual void accept(BeamlineVisitor &visitor) const = 0;
270
272 // Return an identical deep copy of the element.
273 virtual ElementBase *clone() const = 0;
274
276 // Return a fresh copy of any beam line structure is made,
277 // but sharable elements remain shared.
278 virtual ElementBase *copyStructure();
279
281 bool isSharable() const;
282
284 // The whole structure depending on [b]this[/b] is marked as sharable.
285 // After this call a [b]copyStructure()[/b] call reuses the element.
286 virtual void makeSharable();
287
289 // This method stores all attributes contained in the AttributeSet to
290 // "*this". The return value [b]true[/b] indicates success.
291 bool update(const AttributeSet &);
292
294 void setElementPosition(double elemedge);
295 double getElementPosition() const;
296 bool isElementPositionSet() const;
299 virtual void setBoundaryGeometry(BoundaryGeometry *geo);
300
302 virtual BoundaryGeometry *getBoundaryGeometry() const;
303
304 virtual bool hasBoundaryGeometry() const;
305
307 virtual void setWake(WakeFunction *wf);
308
310 virtual WakeFunction *getWake() const;
311
312 virtual bool hasWake() const;
313
315
317
318 virtual bool hasParticleMatterInteraction() const;
319
322 void releasePosition();
323 void fixPosition();
324 bool isPositioned() const;
325
327 virtual CoordinateSystemTrafo getEdgeToEnd() const;
328
329 void setAperture(const ApertureType& type, const std::vector<double> &args);
330 std::pair<ApertureType, std::vector<double> > getAperture() const;
331
332 virtual bool isInside(const Vector_t &r) const;
333
334 void setMisalignment(const CoordinateSystemTrafo &cst);
335
336 void getMisalignment(double &x, double &y, double &s) const;
338
339 void setActionRange(const std::queue<std::pair<double, double> > &range);
340 void setCurrentSCoordinate(double s);
341
343 void setRotationAboutZ(double rotation);
344 double getRotationAboutZ() const;
345
347
348 virtual int getRequiredNumberOfTimeSteps() const;
349
351 void setOutputFN(std::string fn);
353 std::string getOutputFN() const;
354
355 void setFlagDeleteOnTransverseExit(bool = true);
357
358protected:
359 bool isInsideTransverse(const Vector_t &r) const;
360
361 // Sharable flag.
362 // If this flag is true, the element is always shared.
363 mutable bool shareFlag;
364
367
368 std::pair<ApertureType, std::vector<double> > aperture_m;
369
371
373
374
375private:
376 // Not implemented.
377 void operator=(const ElementBase &);
378
379 // The element's name
380 std::string elementID;
381
382 static const std::map<ElementType, std::string> elementTypeToString_s;
383
384 // The user-defined set of attributes.
386
388
390
392
398 std::queue<std::pair<double, double> > actionRange_m;
399
400 std::string outputfn_m;
403};
404
405
406// Inline functions.
407// ------------------------------------------------------------------------
408
409inline
411{ return getGeometry().getArcLength(); }
412
413inline
415{ return getGeometry().getElementLength(); }
416
417inline
419{ getGeometry().setElementLength(length); }
420
421inline
423{ return getGeometry().getOrigin(); }
424
425inline
427{ return getGeometry().getEntrance(); }
428
429inline
431{ return getGeometry().getExit(); }
432
433inline
434Euclid3D ElementBase::getTransform(double fromS, double toS) const
435{ return getGeometry().getTransform(fromS, toS); }
436
437inline
439{ return getGeometry().getTotalTransform(); }
440
441inline
443{ return getGeometry().getTransform(s); }
444
445inline
447{ return getGeometry().getEntranceFrame(); }
448
449inline
451{ return getGeometry().getExitFrame(); }
452
453inline
455{ return getGeometry().getEntrancePatch(); }
456
457inline
459{ return getGeometry().getExitPatch(); }
460
461inline
463{ return shareFlag; }
464
465inline
467{ return wake_m; }
468
469inline
471{ return wake_m != nullptr; }
472
473inline
475{ return bgeometry_m; }
476
477inline
479{ return bgeometry_m != nullptr; }
480
481inline
483{ return parmatint_m; }
484
485inline
487{ return parmatint_m != nullptr; }
488
489inline
491 if (positionIsFixed) return;
492
493 csTrafoGlobal2Local_m = trafo;
494}
495
496inline
499}
500
501inline
503 CoordinateSystemTrafo ret(Vector_t(0, 0, 0),
504 Quaternion(1, 0, 0, 0));
505
506 return ret;
507}
508
509inline
512 Quaternion(1, 0, 0, 0));
513
514 return ret;
515}
516
517inline
518void ElementBase::setAperture(const ApertureType& type, const std::vector<double> &args) {
519 aperture_m.first = type;
520 aperture_m.second = args;
521}
522
523inline
524std::pair<ApertureType, std::vector<double> > ElementBase::getAperture() const {
525 return aperture_m;
526}
527
528inline
529bool ElementBase::isInside(const Vector_t &r) const {
530 const double length = getElementLength();
531 return r(2) >= 0.0 && r(2) < length && isInsideTransverse(r);
532}
533
534inline
536 misalignment_m = cst;
537}
538
539inline
541 return misalignment_m;
542}
543
544inline
546 positionIsFixed = false;
547}
548
549inline
551 positionIsFixed = true;
552}
553
554inline
556 return positionIsFixed;
557}
558
559inline
560void ElementBase::setActionRange(const std::queue<std::pair<double, double> > &range) {
561 actionRange_m = range;
562
563 if (!actionRange_m.empty())
564 elementEdge_m = actionRange_m.front().first;
565}
566
567inline
568void ElementBase::setRotationAboutZ(double rotation) {
569 rotationZAxis_m = rotation;
570}
571
572inline
574 return rotationZAxis_m;
575}
576
577inline
578std::string ElementBase::getTypeString() const
579{ return getTypeString(getType());}
580
581inline
582void ElementBase::setElementPosition(double elemedge) {
583 elementPosition_m = elemedge;
584 elemedgeSet_m = true;
585}
586
587inline
589 if (elemedgeSet_m)
590 return elementPosition_m;
591
592 throw GeneralClassicException("ElementBase::getElementPosition()",
593 std::string("ELEMEDGE for \"") + getName() + "\" not set");
594}
595
596inline
598 return elemedgeSet_m;
599}
600
601inline
603 return 10;
604}
605
606inline
608{
610}
611
612inline
614{
616}
617
618#endif // CLASSIC_ElementBase_HH
ElementType
Definition: ElementBase.h:88
ApertureType
Definition: ElementBase.h:120
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)
void setElementPosition(double elemedge)
Access to ELEMEDGE attribute.
Definition: ElementBase.h:582
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:410
void fixPosition()
Definition: ElementBase.h:550
static const std::map< ElementType, std::string > elementTypeToString_s
Definition: ElementBase.h:382
virtual double getExit() const
Get exit position.
Definition: ElementBase.h:430
void setAperture(const ApertureType &type, const std::vector< double > &args)
Definition: ElementBase.h:518
virtual const BGeometryBase & getGeometry() const =0
Get geometry.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
Definition: ElementBase.h:482
bool getFlagDeleteOnTransverseExit() const
Definition: ElementBase.h:613
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:414
bool update(const AttributeSet &)
Update element.
virtual Euclid3D getExitPatch() const
Get patch.
Definition: ElementBase.h:458
bool isPositioned() const
Definition: ElementBase.h:555
std::pair< ApertureType, std::vector< double > > getAperture() const
Definition: ElementBase.h:524
std::string outputfn_m
Definition: ElementBase.h:400
virtual void setElementLength(double length)
Set design length.
Definition: ElementBase.h:418
void releasePosition()
Definition: ElementBase.h:545
ParticleMatterInteractionHandler * parmatint_m
Definition: ElementBase.h:391
virtual Euclid3D getExitFrame() const
Get transform.
Definition: ElementBase.h:450
CoordinateSystemTrafo misalignment_m
Definition: ElementBase.h:366
void setMisalignment(const CoordinateSystemTrafo &cst)
Definition: ElementBase.h:535
CoordinateSystemTrafo getMisalignment() const
Definition: ElementBase.h:540
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:402
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
virtual ElementBase * clone() const =0
Return clone.
bool elemedgeSet_m
Definition: ElementBase.h:396
CoordinateSystemTrafo getCSTrafoGlobal2Local() const
Definition: ElementBase.h:497
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual double getOrigin() const
Get origin position.
Definition: ElementBase.h:422
bool isInsideTransverse(const Vector_t &r) const
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: ElementBase.h:446
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:529
std::pair< ApertureType, std::vector< double > > aperture_m
Definition: ElementBase.h:368
virtual bool hasBoundaryGeometry() const
Definition: ElementBase.h:478
double elementPosition_m
ELEMEDGE attribute.
Definition: ElementBase.h:395
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
void setFlagDeleteOnTransverseExit(bool=true)
Definition: ElementBase.h:607
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:434
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:387
bool isElementPositionSet() const
Definition: ElementBase.h:597
bool positionIsFixed
Definition: ElementBase.h:393
void setRotationAboutZ(double rotation)
Set rotation about z axis in bend frame.
Definition: ElementBase.h:568
std::queue< std::pair< double, double > > actionRange_m
Definition: ElementBase.h:398
void setCSTrafoGlobal2Local(const CoordinateSystemTrafo &ori)
Definition: ElementBase.h:490
virtual BoundaryGeometry * getBoundaryGeometry() const
return the attached boundary geometrt object if there is any
Definition: ElementBase.h:474
virtual double getEntrance() const
Get entrance position.
Definition: ElementBase.h:426
virtual CoordinateSystemTrafo getEdgeToBegin() const
Definition: ElementBase.h:502
std::string getTypeString() const
Definition: ElementBase.h:578
double getElementPosition() const
Definition: ElementBase.h:588
AttributeSet userAttribs
Definition: ElementBase.h:385
virtual ElementBase * copyStructure()
Make a structural copy.
double getRotationAboutZ() const
Definition: ElementBase.h:573
BoundaryGeometry * bgeometry_m
Definition: ElementBase.h:389
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: ElementBase.h:438
bool isSharable() const
Test if the element can be shared.
Definition: ElementBase.h:462
bool shareFlag
Definition: ElementBase.h:363
void setActionRange(const std::queue< std::pair< double, double > > &range)
Definition: ElementBase.h:560
virtual int getRequiredNumberOfTimeSteps() const
Definition: ElementBase.h:602
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition: ElementBase.h:454
virtual WakeFunction * getWake() const
return the attached wake object if there is any
Definition: ElementBase.h:466
void setCurrentSCoordinate(double s)
std::string elementID
Definition: ElementBase.h:380
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
virtual bool hasParticleMatterInteraction() const
Definition: ElementBase.h:486
virtual CoordinateSystemTrafo getEdgeToEnd() const
Definition: ElementBase.h:510
virtual bool hasWake() const
Definition: ElementBase.h:470
virtual void getElementDimensions(double &begin, double &end) const
Definition: ElementBase.h:173
double rotationZAxis_m
Definition: ElementBase.h:372
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
virtual BGeometryBase & getGeometry()=0
Get geometry.
double elementEdge_m
Definition: ElementBase.h:370
virtual BoundingBox getBoundingBoxInLabCoords() const
CoordinateSystemTrafo csTrafoGlobal2Local_m
Definition: ElementBase.h:365
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