OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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"
69#include <filesystem>
70
71const 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
107
108
110 RCObject(),
111 shareFlag(true),
114 aperture_m(right.aperture_m),
117 elementID(right.elementID),
119 wake_m(right.wake_m),
125 outputfn_m(right.outputfn_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
138ElementBase::ElementBase(const std::string &name):
140 shareFlag(true),
143 elementEdge_m(0),
144 rotationZAxis_m(0.0),
146 userAttribs(),
147 wake_m(nullptr),
148 bgeometry_m(nullptr),
149 parmatint_m(nullptr),
150 positionIsFixed(false),
152 elemedgeSet_m(false),
153 outputfn_m("")
154{}
155
156
160
161
162const std::string &ElementBase::getName() const {
163 return elementID;
164}
165
166
167void ElementBase::setName(const std::string &name) {
168 elementID = name;
169}
170
171
172void ElementBase::setOutputFN(const std::string fn) {
173 outputfn_m = fn;
174}
175
176
177std::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
187double 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
200bool 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
212void ElementBase::removeAttribute(const std::string &aKey) {
213 userAttribs.removeAttribute(aKey);
214}
215
216
217void 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
228Channel *ElementBase::getChannel(const std::string &aKey, bool create) {
229 return userAttribs.getChannel(aKey, create);
230}
231
232
233const 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
243
245 if (isSharable()) {
246 return this;
247 } else {
248 return clone();
250}
251
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;
265
267 wake_m = wk;//->clone(getName() + std::string("_wake")); }
268}
269
271 bgeometry_m = geo;//->clone(getName() + std::string("_wake")); }
272}
273
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}
ElementType
Definition ElementBase.h:88
const std::string name
Map of std::string versus double value.
const_iterator begin() const
Iterator accessing first member.
NameMap::const_iterator const_iterator
An iterator for a map of name versus value.
const_iterator end() const
Iterator marking the end of the list.
virtual void setBoundaryGeometry(BoundaryGeometry *geo)
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.
static const std::map< ElementType, std::string > elementTypeToString_s
Definition ElementBase.h:71
virtual double getElementLength() const
Get design length.
bool update(const AttributeSet &)
Update element.
std::string outputfn_m
ParticleMatterInteractionHandler * parmatint_m
CoordinateSystemTrafo misalignment_m
virtual const ConstChannel * getConstChannel(const std::string &aKey) const
Construct a read-only channel.
ElementBase(const std::string &name)
Constructor with given name.
bool deleteOnTransverseExit_m
virtual ElementBase * clone() const =0
Return clone.
bool elemedgeSet_m
virtual void setAttribute(const std::string &aKey, double val)
Set value of an attribute.
bool isInsideTransverse(const Vector_t &r) const
std::string getOutputFN() const
Get output filename.
void setOutputFN(std::string fn)
Set output filename.
std::pair< ApertureType, std::vector< double > > aperture_m
double elementPosition_m
ELEMEDGE attribute.
virtual void setParticleMatterInteraction(ParticleMatterInteractionHandler *spys)
virtual void makeSharable()
Set sharable flag.
virtual bool hasAttribute(const std::string &aKey) const
Test for existence of an attribute.
WakeFunction * wake_m
bool positionIsFixed
std::queue< std::pair< double, double > > actionRange_m
virtual CoordinateSystemTrafo getEdgeToBegin() const
std::string getTypeString() const
AttributeSet userAttribs
virtual ElementBase * copyStructure()
Make a structural copy.
BoundaryGeometry * bgeometry_m
bool isSharable() const
Test if the element can be shared.
void setCurrentSCoordinate(double s)
std::string elementID
virtual double getAttribute(const std::string &aKey) const
Get attribute value.
virtual CoordinateSystemTrafo getEdgeToEnd() const
double rotationZAxis_m
virtual void setWake(WakeFunction *wf)
attach a wake field to the element
double elementEdge_m
virtual BoundingBox getBoundingBoxInLabCoords() const
CoordinateSystemTrafo csTrafoGlobal2Local_m
Vector_t transformFrom(const Vector_t &r) const
Vector_t transformTo(const Vector_t &r) const
Abstract interface for read/write access to variable.
Definition Channel.h:32
virtual bool isSettable() const
Test if settable.
Definition Channel.cpp:36
Abstract interface for read-only access to variable.
Abstract base class for reference counted objects.
Definition RCObject.h:40
RCObject()
Default constructor.
Definition RCObject.h:98
static BoundingBox getBoundingBox(const std::vector< Vector_t > &positions)
Vektor< double, 3 > Vector_t