OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
ElementBase.cpp
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 //
64 
65 #include "Channels/Channel.h"
67 #include "Solvers/WakeFunction.h"
69 #include <filesystem>
70 
71 const std::map<ElementType, std::string> ElementBase::elementTypeToString_s = {
72  {ElementType::ANY, "Any"},
73  {ElementType::BEAMLINE, "Beamline"},
74  {ElementType::CCOLLIMATOR, "CCollimator"},
75  {ElementType::CORRECTOR, "Corrector"},
76  {ElementType::CYCLOTRON, "Cyclotron"},
77  {ElementType::DEGRADER, "Degrader"},
78  {ElementType::DRIFT, "Drift"},
79  {ElementType::FLEXIBLECOLLIMATOR, "FlexibleCollimator"},
80  {ElementType::MARKER, "Marker"},
81  {ElementType::MONITOR, "Monitor"},
82  {ElementType::MPSPLITINTEGRATOR, "MPSplitIntegrator"},
83  {ElementType::MULTIPOLE, "Multipole"},
84  {ElementType::MULTIPOLET, "MultipoleT"},
85  {ElementType::OFFSET, "Offset"},
86  {ElementType::OUTPUTPLANE, "OutputPlane"},
87  {ElementType::PROBE, "Probe"},
88  {ElementType::RBEND, "RBend"},
89  {ElementType::RBEND3D, "RBend3D"},
90  {ElementType::RFCAVITY, "RFCavity"},
91  {ElementType::RING, "Ring"},
92  {ElementType::SBEND, "SBend"},
93  {ElementType::SBEND3D, "SBend3D"},
94  {ElementType::SEPTUM, "Septum"},
95  {ElementType::SOLENOID, "Solenoid"},
96  {ElementType::SOURCE, "Source"},
97  {ElementType::STRIPPER, "Stripper"},
98  {ElementType::TRAVELINGWAVE, "TravelingWave"},
99  {ElementType::UNDULATOR, "Undulator"},
100  {ElementType::VACUUM, "Vacuum"},
101  {ElementType::VARIABLERFCAVITY, "VariableRFCavity"}
102 };
103 
105  ElementBase("")
106 {}
107 
108 
110  RCObject(),
111  shareFlag(true),
112  csTrafoGlobal2Local_m(right.csTrafoGlobal2Local_m),
113  misalignment_m(right.misalignment_m),
114  aperture_m(right.aperture_m),
115  elementEdge_m(right.elementEdge_m),
116  rotationZAxis_m(right.rotationZAxis_m),
117  elementID(right.elementID),
118  userAttribs(right.userAttribs),
119  wake_m(right.wake_m),
120  bgeometry_m(right.bgeometry_m),
121  parmatint_m(right.parmatint_m),
122  positionIsFixed(right.positionIsFixed),
123  elementPosition_m(right.elementPosition_m),
124  elemedgeSet_m(right.elemedgeSet_m),
125  outputfn_m(right.outputfn_m),
126  deleteOnTransverseExit_m(right.deleteOnTransverseExit_m)
127 {
128 
129  if (parmatint_m) {
130  parmatint_m->updateElement(this);
131  }
132  if (bgeometry_m) {
133  bgeometry_m->updateElement(this);
134  }
135 }
136 
137 
138 ElementBase::ElementBase(const std::string &name):
139  RCObject(),
140  shareFlag(true),
141  csTrafoGlobal2Local_m(),
142  misalignment_m(),
143  elementEdge_m(0),
144  rotationZAxis_m(0.0),
145  elementID(name),
146  userAttribs(),
147  wake_m(nullptr),
148  bgeometry_m(nullptr),
149  parmatint_m(nullptr),
150  positionIsFixed(false),
151  elementPosition_m(0.0),
152  elemedgeSet_m(false),
153  outputfn_m("")
154 {}
155 
156 
158 
159 {}
160 
161 
162 const std::string &ElementBase::getName() const {
163  return elementID;
164 }
165 
166 
167 void ElementBase::setName(const std::string &name) {
168  elementID = name;
169 }
170 
171 
172 void ElementBase::setOutputFN(const std::string fn) {
173  outputfn_m = fn;
174 }
175 
176 
177 std::string ElementBase::getOutputFN() const {
178  if (outputfn_m.empty()) {
179  return getName();
180  } else {
181  std::filesystem::path filePath(outputfn_m);
182  return filePath.replace_extension().native();
183  }
184 }
185 
186 
187 double ElementBase::getAttribute(const std::string &aKey) const {
188  const ConstChannel *aChannel = getConstChannel(aKey);
189 
190  if (aChannel != nullptr) {
191  double val = *aChannel;
192  delete aChannel;
193  return val;
194  } else {
195  return 0.0;
196  }
197 }
198 
199 
200 bool ElementBase::hasAttribute(const std::string &aKey) const {
201  const ConstChannel *aChannel = getConstChannel(aKey);
202 
203  if (aChannel != nullptr) {
204  delete aChannel;
205  return true;
206  } else {
207  return false;
208  }
209 }
210 
211 
212 void ElementBase::removeAttribute(const std::string &aKey) {
214 }
215 
216 
217 void ElementBase::setAttribute(const std::string &aKey, double val) {
218  Channel *aChannel = getChannel(aKey, true);
219 
220  if (aChannel != nullptr && aChannel->isSettable()) {
221  *aChannel = val;
222  delete aChannel;
223  } else
224  std::cout << "Channel nullptr or not Settable" << std::endl;
225 }
226 
227 
228 Channel *ElementBase::getChannel(const std::string &aKey, bool create) {
229  return userAttribs.getChannel(aKey, create);
230 }
231 
232 
233 const ConstChannel *ElementBase::getConstChannel(const std::string &aKey) const {
234  // Use const_cast to allow calling the non-const method GetChannel().
235  // The const return value of this method will nevertheless inhibit set().
236  return const_cast<ElementBase *>(this)->getChannel(aKey);
237 }
238 
239 
241  return elementTypeToString_s.at(type);
242 }
243 
245  if (isSharable()) {
246  return this;
247  } else {
248  return clone();
249  }
250 }
251 
252 
254  shareFlag = true;
255 }
256 
257 
259  for (AttributeSet::const_iterator i = set.begin(); i != set.end(); ++i) {
260  setAttribute(i->first, i->second);
261  }
262 
263  return true;
264 }
265 
267  wake_m = wk;//->clone(getName() + std::string("_wake")); }
268 }
269 
271  bgeometry_m = geo;//->clone(getName() + std::string("_wake")); }
272 }
273 
275  parmatint_m = parmatint;
276 }
277 
279  if (!actionRange_m.empty() && actionRange_m.front().second < s) {
280  actionRange_m.pop();
281  if (!actionRange_m.empty()) {
282  elementEdge_m = actionRange_m.front().first;
283  }
284  }
285 }
286 
288 {
289  const double &xLimit = aperture_m.second[0];
290  const double &yLimit = aperture_m.second[1];
291  double factor = 1.0;
294  Vector_t rRelativeToBegin = getEdgeToBegin().transformTo(r);
295  double fractionLength = rRelativeToBegin(2) / getElementLength();
296  factor = fractionLength * aperture_m.second[2];
297  }
298 
299  switch(aperture_m.first) {
301  return (std::abs(r[0]) < xLimit && std::abs(r[1]) < yLimit);
303  return (std::pow(r[0] / xLimit, 2) + std::pow(r[1] / yLimit, 2) < 1.0);
305  return (std::abs(r[0]) < factor * xLimit && std::abs(r[1]) < factor * yLimit);
307  return (std::pow(r[0] / (factor * xLimit), 2) + std::pow(r[1] / (factor * yLimit), 2) < 1.0);
308  default:
309  return false;
310  }
311 }
312 
316 
317  const double &x = aperture_m.second[0];
318  const double &y = aperture_m.second[1];
319  const double &f = aperture_m.second[2];
320 
321  std::vector<Vector_t> corners(8);
322  for (int i = -1; i < 2; i += 2) {
323  for (int j = -1; j < 2; j += 2) {
324  unsigned int idx = (i + 1)/2 + (j + 1);
325  corners[idx] = toBegin.transformFrom(Vector_t(i * x, j * y, 0.0));
326  corners[idx + 4] = toEnd.transformFrom(Vector_t(i * f * x, j * f * y, 0.0));
327  }
328  }
329 
330  return BoundingBox::getBoundingBox(corners);
331 }
static const std::map< ElementType, std::string > elementTypeToString_s
Definition: ElementBase.h:383
virtual void setBoundaryGeometry(BoundaryGeometry *geo)
Abstract base class for reference counted objects.
Definition: RCObject.h:40
const_iterator begin() const
Iterator accessing first member.
Definition: AttributeSet.h:105
bool shareFlag
Definition: ElementBase.h:364
const_iterator end() const
Iterator marking the end of the list.
Definition: AttributeSet.h:108
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::string elementID
Definition: ElementBase.h:381
virtual const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
virtual CoordinateSystemTrafo getEdgeToEnd() const
Definition: ElementBase.h:511
ParticleMatterInteractionHandler * parmatint_m
Definition: ElementBase.h:392
std::string getTypeString() const
Definition: ElementBase.h:579
virtual Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
virtual const std::string & getName() const
Get element name.
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Vector_t transformFrom(const Vector_t &r) const
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
virtual void makeSharable()
Set sharable flag.
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
double elementEdge_m
Definition: ElementBase.h:371
bool isSharable() const
Test if the element can be shared.
Definition: ElementBase.h:463
BoundaryGeometry * bgeometry_m
Definition: ElementBase.h:390
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
static BoundingBox getBoundingBox(const std::vector< Vector_t > &positions)
Definition: BoundingBox.cpp:31
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
set(_SRCS Action.cpp Attribute.cpp AttributeBase.cpp AttributeHandler.cpp BeamSequence.cpp Definition.cpp Directory.cpp Element.cpp Invalidator.cpp OpalData.cpp Object.cpp ObjectFunction.cpp PlaceRep.cpp RangeRep.cpp Table.cpp TableRowRep.cpp ValueDefinition.cpp) include_directories($
Definition: CMakeLists.txt:1
Abstract interface for read/write access to variable.
Definition: Channel.h:32
virtual ~ElementBase()
ElementType
Definition: ElementBase.h:88
NameMap::const_iterator const_iterator
An iterator for a map of name versus value.
Definition: AttributeSet.h:49
virtual double getElementLength() const
Get design length.
Definition: ElementBase.h:415
Vector_t transformTo(const Vector_t &r) const
virtual bool hasAttribute(const std::string &aKey) const
Test for existence of an attribute.
virtual ElementBase * copyStructure()
Make a structural copy.
AttributeSet userAttribs
Definition: ElementBase.h:386
bool isInsideTransverse(const Vector_t &r) const
const std::string name
Map of std::string versus double value.
Definition: AttributeSet.h:41
virtual ElementBase * clone() const =0
Return clone.
void removeAttribute(const std::string &aKey)
Remove an existing attribute.
WakeFunction * wake_m
Definition: ElementBase.h:388
virtual bool isSettable() const
Test if settable.
Definition: Channel.cpp:36
virtual void setName(const std::string &name)
Set element name.
void updateElement(ElementBase *element)
bool update(const AttributeSet &)
Update element.
void setCurrentSCoordinate(double s)
virtual BoundingBox getBoundingBoxInLabCoords() const
std::string getOutputFN() const
Get output filename.
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Channel * getChannel(const std::string &aKey, bool create=false)
Construct a read/write channel.
std::pair< ApertureType, std::vector< double > > aperture_m
Definition: ElementBase.h:369
SDDS1 &description type
Definition: test.stat:4
virtual void removeAttribute(const std::string &aKey)
Remove an existing attribute.
Abstract interface for read-only access to variable.
Definition: ConstChannel.h:29