OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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"
66
67#include <boost/filesystem.hpp>
68
69const std::map<ElementType, std::string> ElementBase::elementTypeToString_s = {
70 {ElementType::ANY, "Any"},
71 {ElementType::BEAMLINE, "Beamline"},
72 {ElementType::DRIFT, "Drift"},
73 {ElementType::MARKER, "Marker"},
74 {ElementType::MULTIPOLE, "Multipole"},
75 {ElementType::RFCAVITY, "RFCavity"},
76 {ElementType::TRAVELINGWAVE, "TravelingWave"},
77 {ElementType::SBEND, "SBEND"},
78 {ElementType::RBEND, "RBEND"},
79 {ElementType::RBEND3D, "RBEND3D"},
80 {ElementType::RING, "Ring"},
81 {ElementType::SOURCE, "SOURCE"},
82 {ElementType::SOLENOID, "SOLENOID"},
83 {ElementType::PROBE, "Probe"},
84 {ElementType::VACUUM, "Vacuum"}};
85
88
108
109ElementBase::ElementBase(const std::string& name)
110 : RCObject(),
111 shareFlag(true),
114 elementEdge_m(0),
115 rotationZAxis_m(0.0),
116 elementID(name),
117 userAttribs(),
118 wake_m(nullptr),
119 bgeometry_m(nullptr),
120 parmatint_m(nullptr),
121 positionIsFixed(false),
123 elemedgeSet_m(false),
125}
126
128
129{
130}
131
132const std::string& ElementBase::getName() const {
133 return elementID;
134}
135
136void ElementBase::setName(const std::string& name) {
137 elementID = name;
138}
139
140void ElementBase::setOutputFN(const std::string fn) {
141 outputfn_m = fn;
142}
143
144std::string ElementBase::getOutputFN() const {
145 if (outputfn_m.empty()) {
146 return getName();
147 } else {
148 boost::filesystem::path filePath(outputfn_m);
149 return filePath.replace_extension().native();
150 }
151}
152
153double ElementBase::getAttribute(const std::string& aKey) const {
154 const ConstChannel* aChannel = getConstChannel(aKey);
155
156 if (aChannel != nullptr) {
157 double val = *aChannel;
158 delete aChannel;
159 return val;
160 } else {
161 return 0.0;
162 }
163}
164
165bool ElementBase::hasAttribute(const std::string& aKey) const {
166 const ConstChannel* aChannel = getConstChannel(aKey);
167
168 if (aChannel != nullptr) {
169 delete aChannel;
170 return true;
171 } else {
172 return false;
173 }
174}
175
176void ElementBase::removeAttribute(const std::string& aKey) {
177 userAttribs.removeAttribute(aKey);
178}
179
180void ElementBase::setAttribute(const std::string& aKey, double val) {
181 Channel* aChannel = getChannel(aKey, true);
182
183 if (aChannel != nullptr && aChannel->isSettable()) {
184 *aChannel = val;
185 delete aChannel;
186 } else
187 std::cout << "Channel nullptr or not Settable" << std::endl;
188}
189
190Channel* ElementBase::getChannel(const std::string& aKey, bool create) {
191 return userAttribs.getChannel(aKey, create);
192}
193
194const ConstChannel* ElementBase::getConstChannel(const std::string& aKey) const {
195 // Use const_cast to allow calling the non-const method GetChannel().
196 // The const return value of this method will nevertheless inhibit set().
197 return const_cast<ElementBase*>(this)->getChannel(aKey);
198}
199
201 return elementTypeToString_s.at(type);
202}
203
205 if (isSharable()) {
206 return this;
207 } else {
208 return clone();
209 }
210}
211
213 shareFlag = true;
214}
215
217 for (AttributeSet::const_iterator i = set.begin(); i != set.end(); ++i) {
218 setAttribute(i->first, i->second);
219 }
220
221 return true;
222}
223
224void ElementBase::setWake(WakeFunction* wk) {
225 wake_m = wk; //->clone(getName() + std::string("_wake")); }
227
229 bgeometry_m = geo; //->clone(getName() + std::string("_wake")); }
231
232void ElementBase::setParticleMatterInteraction(ParticleMatterInteractionHandler* parmatint) {
233 parmatint_m = parmatint;
234}
235
237 if (!actionRange_m.empty() && actionRange_m.front().second < s) {
238 actionRange_m.pop();
239 if (!actionRange_m.empty()) {
240 elementEdge_m = actionRange_m.front().first;
241 }
243}
244
246 const double& xLimit = aperture_m.second[0];
247 const double& yLimit = aperture_m.second[1];
248 double factor = 1.0;
251 Vector_t<double, 3> rRelativeToBegin = getEdgeToBegin().transformTo(r);
252 double fractionLength = rRelativeToBegin(2) / getElementLength();
253 factor = fractionLength * aperture_m.second[2];
254 }
255
256 switch (aperture_m.first) {
258 return (std::abs(r[0]) < xLimit && std::abs(r[1]) < yLimit);
260 return (std::pow(r[0] / xLimit, 2) + std::pow(r[1] / yLimit, 2) < 1.0);
262 return (std::abs(r[0]) < factor * xLimit && std::abs(r[1]) < factor * yLimit);
264 return (
265 std::pow(r[0] / (factor * xLimit), 2) + std::pow(r[1] / (factor * yLimit), 2)
266 < 1.0);
267 default:
268 return false;
269 }
270}
271
275
276 const double& x = aperture_m.second[0];
277 const double& y = aperture_m.second[1];
278 const double& f = aperture_m.second[2];
279
280 std::vector<Vector_t<double, 3>> corners(8);
281 for (int i = -1; i < 2; i += 2) {
282 for (int j = -1; j < 2; j += 2) {
283 unsigned int idx = (i + 1) / 2 + (j + 1);
284 corners[idx] = toBegin.transformFrom(Vector_t<double, 3>({i * x, j * y, 0.0}));
285 corners[idx + 4] =
286 toEnd.transformFrom(Vector_t<double, 3>({i * f * x, j * f * y, 0.0}));
287 }
288 }
289
290 return BoundingBox::getBoundingBox(corners);
291}
ElementType
Definition ElementBase.h:88
ippl::Vector< T, Dim > Vector_t
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:69
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.
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)
bool isInsideTransverse(const Vector_t< double, 3 > &r) const
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
ippl::Vector< double, 3 > transformFrom(const ippl::Vector< double, 3 > &r) const
ippl::Vector< double, 3 > transformTo(const ippl::Vector< double, 3 > &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.
RCObject()
Default constructor.
Definition RCObject.h:98
static BoundingBox getBoundingBox(const std::vector< Vector_t< double, 3 > > &positions)