OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 is a base class.
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: Use std::shared_ptr to share ownership.
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 OPALX_ElementBase_HH
64#define OPALX_ElementBase_HH
65
78#include "VectorMath.h"
79
80#include <map>
81#include <memory>
82#include <queue>
83#include <string>
84#include <utility>
85#include <vector>
86
87class BeamlineVisitor;
89class Channel;
90class ConstChannel;
91class ParticleMatterInteractionHandler;
92class WakeFunction;
93
94enum class ElementType : unsigned short {
95 ANY,
97 DRIFT,
98 LASER,
99 MARKER,
100 MONITOR,
101 MULTIPOLE,
103 RFCAVITY,
105 SBEND,
106 RBEND,
107 RBEND3D,
108 RING,
109 PROBE,
110 VACUUM,
111 SOLENOID,
112 SOURCE,
114};
115
116enum class ApertureType : unsigned short {
121};
122
123class ElementBase : public std::enable_shared_from_this<ElementBase> {
124public:
126 explicit ElementBase(const std::string& name);
127
128 ElementBase();
129 ElementBase(const ElementBase&);
130 virtual ~ElementBase();
131
133 virtual const std::string& getName() const;
134
136 virtual void setName(const std::string& name);
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.
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
178 virtual void getElementDimensions(double& begin, double& end) const {
179 begin = 0.0;
180 end = getElementLength();
181 }
182
184 // Return the arc length from the entrance to the origin of the element
185 // (origin >= 0)
186 virtual double getOrigin() const;
187
189 // Return the arc length from the origin to the entrance of the element
190 // (entrance <= 0)
191 virtual double getEntrance() const;
192
194 // Return the arc length from the origin to the exit of the element
195 // (exit >= 0)
196 virtual double getExit() const;
197
199 // Return the transform of the local coordinate system from the
200 // position [b]fromS[/b] to the position [b]toS[/b].
201 virtual Euclid3D getTransform(double fromS, double toS) const;
202
204 // Equivalent to getTransform(0.0, s).
205 // Return the transform of the local coordinate system from the
206 // origin and [b]s[/b].
207 virtual Euclid3D getTransform(double s) const;
208
210 // Equivalent to getTransform(getEntrance(), getExit()).
211 // Return the transform of the local coordinate system from the
212 // entrance to the exit of the element.
213 virtual Euclid3D getTotalTransform() const;
214
216 // Equivalent to getTransform(0.0, getEntrance()).
217 // Return the transform of the local coordinate system from the
218 // origin to the entrance of the element.
219 virtual Euclid3D getEntranceFrame() const;
220
222 // Equivalent to getTransform(0.0, getExit()).
223 // Return the transform of the local coordinate system from the
224 // origin to the exit of the element.
225 virtual Euclid3D getExitFrame() const;
226
228 // Returns the entrance patch (transformation) which is used to transform
229 // the global geometry to the local geometry for a misaligned element
230 // at its entrance. The default behaviour returns identity transformation.
231 // This function should be overridden by derived concrete classes which
232 // model complex geometries.
233 virtual Euclid3D getEntrancePatch() const;
234
236 // Returns the entrance patch (transformation) which is used to transform
237 // the local geometry to the global geometry for a misaligned element
238 // at its exit. The default behaviour returns identity transformation.
239 // This function should be overridden by derived concrete classes which
240 // model complex geometries.
241 virtual Euclid3D getExitPatch() const;
242
244 // If the attribute does not exist, return zero.
245 virtual double getAttribute(const std::string& aKey) const;
246
248 // If the attribute exists, return true, otherwise false.
249 virtual bool hasAttribute(const std::string& aKey) const;
250
252 virtual void removeAttribute(const std::string& aKey);
253
255 virtual void setAttribute(const std::string& aKey, double val);
256
258 // This method constructs a Channel permitting read/write access to
259 // the attribute [b]aKey[/b] and returns it.
260 // If the attribute does not exist, it returns nullptr.
261 virtual Channel* getChannel(const std::string& aKey, bool create = false);
262
264 // This method constructs a Channel permitting read-only access to
265 // the attribute [b]aKey[/b] and returns it.
266 // If the attribute does not exist, it returns nullptr.
267 virtual const ConstChannel* getConstChannel(const std::string& aKey) const;
268
270 // This method must be overridden by derived classes. It should call the
271 // method of the visitor corresponding to the element class.
272 // If any error occurs, this method throws an exception.
273 virtual void accept(BeamlineVisitor& visitor) const = 0;
274
276 // Return an identical deep copy of the element.
277 virtual ElementBase* clone() const = 0;
278
280 // Return a fresh copy of any beam line structure is made,
281 // but sharable elements remain shared.
282 virtual ElementBase* copyStructure();
283
285 bool isSharable() const;
286
288 // The whole structure depending on [b]this[/b] is marked as sharable.
289 // After this call a [b]copyStructure()[/b] call reuses the element.
290 virtual void makeSharable();
291
293 // This method stores all attributes contained in the AttributeSet to
294 // "*this". The return value [b]true[/b] indicates success.
295 bool update(const AttributeSet&);
296
298 void setElementPosition(double elemedge);
299 double getElementPosition() const;
300 bool isElementPositionSet() const;
303 virtual void setBoundaryGeometry(BoundaryGeometry* geo);
304
306 virtual BoundaryGeometry* getBoundaryGeometry() const;
307
308 virtual bool hasBoundaryGeometry() const;
309
311 virtual void setWake(WakeFunction* wf);
312
314 virtual WakeFunction* getWake() const;
315
316 virtual bool hasWake() const;
317
318 virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler* spys);
319
320 virtual ParticleMatterInteractionHandler* getParticleMatterInteraction() const;
321
322 virtual bool hasParticleMatterInteraction() const;
323
326 void releasePosition();
327 void fixPosition();
328 bool isPositioned() const;
329
331 virtual CoordinateSystemTrafo getEdgeToEnd() const;
332
341 virtual Port getEntryPort() const;
342
350 virtual Port getBodyPort() const;
351
360 virtual Port getExitPort() const;
361
370
377 void setPlacementPose(const PlacementPose& pose);
378
381
394
397
400
401 void setAperture(const ApertureType& type, const std::vector<double>& args);
402 std::pair<ApertureType, std::vector<double> > getAperture() const;
403
404 virtual bool isInside(const Vector_t<double, 3>& r) const;
405
406 void setMisalignment(const CoordinateSystemTrafo& cst);
407
408 void getMisalignment(double& x, double& y, double& s) const;
410
411 void setActionRange(const std::queue<std::pair<double, double> >& range);
412 void setCurrentSCoordinate(double s);
413
415 void setRotationAboutZ(double rotation);
416 double getRotationAboutZ() const;
417
419
420 virtual int getRequiredNumberOfTimeSteps() const;
421
423 void setOutputFN(std::string fn);
425 std::string getOutputFN() const;
426
427 void setFlagDeleteOnTransverseExit(bool = true);
429
430protected:
431 bool isInsideTransverse(const Vector_t<double, 3>& r) const;
432
433 // Sharable flag.
434 // If this flag is true, the element is always shared.
435 mutable bool shareFlag;
436
439
440 std::pair<ApertureType, std::vector<double> > aperture_m;
441
443
445
446private:
447 // Not implemented.
448 void operator=(const ElementBase&);
449
450 // The element's name
451 std::string elementID;
452
453 static const std::map<ElementType, std::string> elementTypeToString_s;
454
455 // The user-defined set of attributes.
457
458 WakeFunction* wake_m;
459
461
462 ParticleMatterInteractionHandler* parmatint_m;
463
469 std::queue<std::pair<double, double> > actionRange_m;
470
471 std::string outputfn_m;
474};
475
476// Inline functions.
477// ------------------------------------------------------------------------
478
479inline double ElementBase::getArcLength() const { return getGeometry().getArcLength(); }
480
481inline double ElementBase::getElementLength() const { return getGeometry().getElementLength(); }
482
483inline void ElementBase::setElementLength(double length) { getGeometry().setElementLength(length); }
484
485inline double ElementBase::getOrigin() const { return getGeometry().getOrigin(); }
486
487inline double ElementBase::getEntrance() const { return getGeometry().getEntrance(); }
488
489inline double ElementBase::getExit() const { return getGeometry().getExit(); }
490
491inline Euclid3D ElementBase::getTransform(double fromS, double toS) const {
492 return getGeometry().getTransform(fromS, toS);
493}
494
496
497inline Euclid3D ElementBase::getTransform(double s) const { return getGeometry().getTransform(s); }
498
500
502
504
506
507inline bool ElementBase::isSharable() const { return shareFlag; }
508
509inline WakeFunction* ElementBase::getWake() const { return wake_m; }
510
511inline bool ElementBase::hasWake() const { return wake_m != nullptr; }
512
514
515inline bool ElementBase::hasBoundaryGeometry() const { return bgeometry_m != nullptr; }
516
517inline ParticleMatterInteractionHandler* ElementBase::getParticleMatterInteraction() const {
518 return parmatint_m;
519}
520
521inline bool ElementBase::hasParticleMatterInteraction() const { return parmatint_m != nullptr; }
522
524 if (positionIsFixed) return;
525
526 csTrafoGlobal2Local_m = trafo;
527}
528
532
534 CoordinateSystemTrafo ret(Vector_t<double, 3>({0, 0, 0}), Quaternion(1, 0, 0, 0));
535 return ret;
536}
537
540 Vector_t<double, 3>({0, 0, getElementLength()}), Quaternion(1, 0, 0, 0));
541
542 return ret;
543}
544
545inline Port ElementBase::getEntryPort() const { return Port("entry", getEdgeToBegin()); }
546
547inline Port ElementBase::getBodyPort() const { return Port("body", CoordinateSystemTrafo()); }
548
549inline Port ElementBase::getExitPort() const { return Port("exit", getEdgeToEnd()); }
550
554
558
562
566
570
576
577inline void ElementBase::setAperture(const ApertureType& type, const std::vector<double>& args) {
578 aperture_m.first = type;
579 aperture_m.second = args;
580}
581
582inline std::pair<ApertureType, std::vector<double> > ElementBase::getAperture() const {
583 return aperture_m;
584}
585
586inline bool ElementBase::isInside(const Vector_t<double, 3>& r) const {
587 const double length = getElementLength();
588 return r(2) >= 0.0 && r(2) < length && isInsideTransverse(r);
589}
590
592
594
596
598
599inline bool ElementBase::isPositioned() const { return positionIsFixed; }
600
601inline void ElementBase::setActionRange(const std::queue<std::pair<double, double> >& range) {
602 actionRange_m = range;
603
604 if (!actionRange_m.empty()) elementEdge_m = actionRange_m.front().first;
605}
606
607inline void ElementBase::setRotationAboutZ(double rotation) { rotationZAxis_m = rotation; }
608
609inline double ElementBase::getRotationAboutZ() const { return rotationZAxis_m; }
610
611inline std::string ElementBase::getTypeString() const { return getTypeString(getType()); }
612
613inline void ElementBase::setElementPosition(double elemedge) {
614 elementPosition_m = elemedge;
615 elemedgeSet_m = true;
616}
617
618inline double ElementBase::getElementPosition() const {
620
622 "ElementBase::getElementPosition()",
623 std::string("ELEMEDGE for \"") + getName() + "\" not set");
624}
625
627
628inline int ElementBase::getRequiredNumberOfTimeSteps() const { return 10; }
629
633
635
636#endif // OPALX_ElementBase_HH
ippl::Vector< T, Dim > Vector_t
ElementType
Definition ElementBase.h:94
@ CONSTANTEFIELDCAVITY
ApertureType
Map of std::string versus double value.
Abstract base class for accelerator geometry classes.
Definition Geometry.h:42
virtual Euclid3D getTransform(double fromS, double toS) const =0
Get transform.
virtual Euclid3D getExitFrame() const
Get transform.
Definition Geometry.cpp:43
virtual Euclid3D getExitPatch() const
Get patch.
Definition Geometry.cpp:47
virtual void setElementLength(double length)
Set geometry length.
Definition Geometry.cpp:29
virtual double getEntrance() const
Get entrance position.
Definition Geometry.cpp:33
virtual double getExit() const
Get exit position.
Definition Geometry.cpp:35
virtual Euclid3D getTotalTransform() const
Get transform.
Definition Geometry.cpp:37
virtual double getElementLength() const =0
Get geometry length.
virtual double getOrigin() const
Get origin position.
Definition Geometry.cpp:31
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition Geometry.cpp:41
virtual Euclid3D getEntrancePatch() const
Get patch.
Definition Geometry.cpp:45
virtual double getArcLength() const =0
Get arc length.
Abstract interface for read/write access to variable.
Definition Channel.h:30
Abstract interface for read-only access to variable.
Rigid spatial transform between a parent frame and a local frame.
virtual void setBoundaryGeometry(BoundaryGeometry *geo)
void setElementPosition(double elemedge)
Access to ELEMEDGE attribute.
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.
void fixPosition()
static const std::map< ElementType, std::string > elementTypeToString_s
Definition ElementBase.h:73
virtual double getExit() const
Get exit position.
void setAperture(const ApertureType &type, const std::vector< double > &args)
virtual const BGeometryBase & getGeometry() const =0
Get geometry.
virtual ParticleMatterInteractionHandler * getParticleMatterInteraction() const
bool getFlagDeleteOnTransverseExit() const
virtual double getElementLength() const
Get design length.
bool update(const AttributeSet &)
Update element.
virtual Euclid3D getExitPatch() const
Get patch.
void setPlacementPose(const PlacementPose &pose)
Set the nominal rigid placement transform of the element.
bool isPositioned() const
std::pair< ApertureType, std::vector< double > > getAperture() const
std::string outputfn_m
virtual void setElementLength(double length)
Set design length.
void releasePosition()
ParticleMatterInteractionHandler * parmatint_m
virtual Euclid3D getExitFrame() const
Get transform.
CoordinateSystemTrafo misalignment_m
void setMisalignment(const CoordinateSystemTrafo &cst)
CoordinateSystemTrafo getMisalignment() const
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
virtual void accept(BeamlineVisitor &visitor) const =0
Apply visitor.
virtual ElementBase * clone() const =0
Return clone.
bool elemedgeSet_m
CoordinateSystemTrafo getCSTrafoGlobal2Local() const
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual double getOrigin() const
Get origin position.
virtual Euclid3D getEntranceFrame() const
Get transform.
std::string getOutputFN() const
Get output filename.
virtual ElementType getType() const =0
Get element type std::string.
virtual Port getEntryPort() const
Return the entrance port of the canonical local chart.
void setOutputFN(std::string fn)
Set output filename.
std::pair< ApertureType, std::vector< double > > aperture_m
virtual bool hasBoundaryGeometry() const
double elementPosition_m
ELEMEDGE attribute.
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
void setFlagDeleteOnTransverseExit(bool=true)
ElementGeometry getPlacementGeometry() const
Return the bridge geometry ports assembled from legacy edge state.
bool isInsideTransverse(const Vector_t< double, 3 > &r) const
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
virtual void makeSharable()
Set sharable flag.
virtual bool hasAttribute(const std::string &aKey) const
Test for existence of an attribute.
WakeFunction * wake_m
bool isElementPositionSet() const
bool positionIsFixed
void setRotationAboutZ(double rotation)
Set rotation about z axis in bend frame.
std::queue< std::pair< double, double > > actionRange_m
void setCSTrafoGlobal2Local(const CoordinateSystemTrafo &ori)
virtual Port getBodyPort() const
Return the body port of the canonical local chart.
virtual BoundaryGeometry * getBoundaryGeometry() const
return the attached boundary geometrt object if there is any
Misalignment getPlacementMisalignment() const
Return the local nominal-to-actual correction stored for the element.
virtual double getEntrance() const
Get entrance position.
virtual CoordinateSystemTrafo getEdgeToBegin() const
std::string getTypeString() const
double getElementPosition() const
AttributeSet userAttribs
virtual ElementBase * copyStructure()
Make a structural copy.
PlacementPose getPlacementPose() const
Return the nominal rigid placement transform of the element.
double getRotationAboutZ() const
BoundaryGeometry * bgeometry_m
virtual Port getExitPort() const
Return the exit port of the canonical local chart.
virtual Euclid3D getTotalTransform() const
Get transform.
bool isSharable() const
Test if the element can be shared.
void setActionRange(const std::queue< std::pair< double, double > > &range)
virtual int getRequiredNumberOfTimeSteps() const
virtual Euclid3D getEntrancePatch() const
Get patch.
virtual WakeFunction * getWake() const
return the attached wake object if there is any
void setCurrentSCoordinate(double s)
std::string elementID
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
SupportPlacement getPlacementSupport() const
Return the support-frame bridge object. The default is the body frame.
virtual bool hasParticleMatterInteraction() const
virtual CoordinateSystemTrafo getEdgeToEnd() const
virtual bool hasWake() const
PlacedElement getPlacedElement() const
Return a placed-element view assembled from the current bridge objects.
virtual void getElementDimensions(double &begin, double &end) const
Return the nominal body extent of the element.
double rotationZAxis_m
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
virtual BGeometryBase & getGeometry()=0
Get geometry.
double elementEdge_m
virtual BoundingBox getBoundingBoxInLabCoords() const
virtual bool isInside(const Vector_t< double, 3 > &r) const
CoordinateSystemTrafo csTrafoGlobal2Local_m
Named body-relative ports used for placement and chaining.
Displacement and rotation in space.
Definition Euclid3D.h:67
Local nominal-to-actual correction transform.
Geometric placement record for an element instance.
Nominal rigid placement transform.
const CoordinateSystemTrafo & getParentToNominal() const
Named local frame attached to an element body.
Definition Port.h:15
Quaternion storage and rotation algebra used by OPALX geometry code.
Optional offset from the canonical body frame to the support frame.