OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
TBeamline.h
Go to the documentation of this file.
1//
2// Class TBeamline
3// Template class for beam lines.
4// Instantiation with different T types allows attachment of additional
5// data to each position in the line.
6//
7// Copyright (c) 200x - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
8// All rights reserved
9//
10// This file is part of OPAL.
11//
12// OPAL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation, either version 3 of the License, or
15// (at your option) any later version.
16//
17// You should have received a copy of the GNU General Public License
18// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
19//
20#ifndef CLASSIC_TBeamline_HH
21#define CLASSIC_TBeamline_HH
22
24#include "Beamlines/Beamline.h"
26
27#include <algorithm>
28#include <list>
29#include <string>
31
32template <class T>
33class TBeamline : public Beamline, public std::list<T> {
34public:
37
39 explicit TBeamline(const std::string& name);
40
41 TBeamline(const TBeamline<T>& right);
42 virtual ~TBeamline();
43
45 virtual void accept(BeamlineVisitor&) const;
46
48 // If the parameter [b]r2l[/b] is true, the line is traversed from
49 // right (s=C) to left (s=0).
50 // If any error occurs, this method may throw an exception.
51 virtual void iterate(BeamlineVisitor&, bool r2l) const;
52
54 virtual TBeamline<T>* clone() const;
55
58
60 // The whole beamline and the elements depending on [b]this[/b] are
61 // marked as sharable. After this call a [b]copyStructure()[/b] call
62 // reuses the element.
63 virtual void makeSharable();
64
66 // Version for non-constant object.
68
70 // Version for constant object.
71 virtual const BeamlineGeometry& getGeometry() const;
72
74 // Return the length of the geometry, measured along the design orbit.
75 virtual double getArcLength() const;
76
78 // Return the length of the geometry, measured along the design polygone.
79 virtual double getElementLength() const;
80
82 // Return the arc length from the entrance to the origin of the
83 // geometry (non-negative).
84 virtual double getOrigin() const;
85
87 // Return the arc length from the origin to the entrance of the
88 // geometry (non-positive).
89 virtual double getEntrance() const;
90
92 // Return the arc length from the origin to the exit of the
93 // geometry (non-negative).
94 virtual double getExit() const;
95
97 // Return the transform of the local coordinate system from the
98 // position [b]fromS[/b] to the position [b]toS[/b].
99 virtual Euclid3D getTransform(double fromS, double toS) const;
100
102 // Equivalent to getTransform(0.0, s).
103 // Return the transform of the local coordinate system from the
104 // origin and [b]s[/b].
105 virtual Euclid3D getTransform(double s) const;
106
108 // Equivalent to getTransform(getEntrance(), getExit()).
109 // Return the transform of the local coordinate system from the
110 // entrance to the exit of the element.
112
114 // Equivalent to getTransform(0.0, getEntrance()).
115 // Return the transform of the local coordinate system from the
116 // origin to the entrance of the element.
117 virtual Euclid3D getEntranceFrame() const;
118
120 // Equivalent to getTransform(0.0, getExit()).
121 // Return the transform of the local coordinate system from the
122 // origin to the exit of the element.
123 virtual Euclid3D getExitFrame() const;
124
126 virtual ElementType getType() const;
127
129 virtual void append(const T&);
130
132 virtual void prepend(const T&);
133
138
139 void setRelativeFlag(bool flag);
140 bool getRelativeFlag() const;
142 size_t size() const;
143
144protected:
146 // Exists to match the interface for ElementBase.
148
152};
153
154// Implementation of template class TBeamline
155// ------------------------------------------------------------------------
156
157template <class T>
159 : Beamline(),
160 std::list<T>(),
161 itsGeometry(*this),
162 itsOrigin_m(0),
163 itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
164 relativePositions_m(false) {
165}
166
167template <class T>
168TBeamline<T>::TBeamline(const std::string& name)
169 : Beamline(name),
170 std::list<T>(),
171 itsGeometry(*this),
172 itsOrigin_m(0),
173 itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
174 relativePositions_m(false) {
175}
176
177template <class T>
186
187template <class T>
190
191template <class T>
193 visitor.visitBeamline(*this);
194}
195
196template <class T>
197void TBeamline<T>::iterate(BeamlineVisitor& visitor, bool r2l) const {
198 if (r2l) {
199 for (typename std::list<T>::const_reverse_iterator op = this->rbegin(); op != this->rend();
200 ++op) {
201 op->accept(visitor);
202 }
203 } else {
204 for (typename std::list<T>::const_iterator op = this->begin(); op != this->end(); ++op) {
205 op->accept(visitor);
206 }
207 }
208}
209
210template <class T>
212 TBeamline<T>* line = new TBeamline(getName());
213
214 for (typename std::list<T>::const_iterator op = this->begin(); op != this->end(); ++op) {
215 // Make copy of the T object containing a deep copy of its child.
216 T newObj(*op);
217 newObj.setElement(op->getElement()->clone());
218 line->append(newObj);
219 }
220
221 line->itsOrigin_m = itsOrigin_m;
224
225 return line;
226}
227
228// 21-6-2000 ada change iterator to const_iterator
229template <class T>
231 if (isSharable()) {
232 return this;
233 } else {
234 TBeamline<T>* line = new TBeamline(getName());
235
236 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end();
237 ++iter) {
238 // The copy constructor ensures proper transmission of data.
239 T newObj(*iter);
240 newObj.setElement(iter->getElement()->copyStructure());
241 line->append(newObj);
242 }
243
244 line->itsOrigin_m = itsOrigin_m;
247
248 return line;
249 }
250}
251
252// 21-6-2000 ada change iterator to const_iterator
253template <class T>
255 shareFlag = true;
256
257 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
258 iter->getElement()->makeSharable();
259 }
260}
261
262template <class T>
266
267template <class T>
269 return itsGeometry;
270}
271
272template <class T>
274 double length = 0.0;
275
276 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
277 length += iter->getElement()->getArcLength();
278 }
279
280 return length;
281}
282
283template <class T>
285 double length = 0.0;
286
287 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
288 length += iter->getElement()->getElementLength();
289 }
290
291 return length;
292}
293
294template <class T>
296 return (getArcLength() / 2.0);
297}
298
299template <class T>
301 return (-getOrigin());
302}
303
304template <class T>
305double TBeamline<T>::getExit() const {
306 return (getArcLength() / 2.0);
307}
308
309template <class T>
310Euclid3D TBeamline<T>::getTransform(double fromS, double toS) const {
311 Euclid3D transform;
312
313 if (fromS < toS) {
314 double s1 = getEntrance();
315 typename std::list<T>::const_iterator iter = this->begin();
316
317 while (iter != this->end() && s1 <= toS) {
318 const ElementBase& element = *iter->getElement();
319 double l = element.getArcLength();
320 double s2 = s1 + l;
321
322 if (s2 > fromS) {
323 double s0 = (s1 + s2) / 2.0;
324 double arc1 = std::max(s1, fromS) - s0;
325 double arc2 = std::min(s2, toS) - s0;
326 transform *= element.getTransform(arc1, arc2);
327 }
328
329 s1 = s2;
330 ++iter;
331 }
332 } else {
333 double s1 = getExit();
334 typename std::list<T>::const_reverse_iterator iter = this->rbegin();
335
336 while (iter != this->rend() && s1 >= toS) {
337 const ElementBase& element = *iter->getElement();
338 double l = element.getArcLength();
339 double s2 = s1 - l;
340
341 if (s2 < fromS) {
342 double s0 = (s1 + s2) / 2.0;
343 double arc1 = std::min(s1, fromS) - s0;
344 double arc2 = std::max(s2, toS) - s0;
345 transform *= element.getTransform(arc1, arc2);
346 }
347
348 s1 = s2;
349 ++iter;
350 }
351 }
352
353 return transform;
354}
355
356template <class T>
358 Euclid3D transform;
359
360 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
361 transform.dotBy(iter->getElement()->getTotalTransform());
362 }
363
364 return transform;
365}
366
367template <class T>
369 return getTransform(0.0, s);
370}
371
372template <class T>
376
377template <class T>
379 return getTransform(0.0, getExit());
380}
381
382template <class T>
386
387template <class T>
388inline void TBeamline<T>::append(const T& obj) {
389 this->push_back(obj);
390}
391
392template <class T>
393inline void TBeamline<T>::prepend(const T& obj) {
394 this->push_front(obj);
395}
396
397template <class T>
399 itsOrigin_m = ori;
400}
401
402template <class T>
406
407template <class T>
409 itsCoordTrafoTo_m = trafoTo;
410}
411
412template <class T>
416
417template <class T>
418inline void TBeamline<T>::setRelativeFlag(bool flag) {
419 relativePositions_m = flag;
420}
421
422template <class T>
423inline bool TBeamline<T>::getRelativeFlag() const {
424 return relativePositions_m;
425}
426
427template <class T>
428size_t TBeamline<T>::size() const {
429 size_t blSize = 0;
430 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
431 blSize++;
432 }
433
434 return blSize;
435}
436
437#endif // CLASSIC_TBeamline_HH
ElementType
Definition ElementBase.h:88
ippl::Vector< T, Dim > Vector_t
PartBunch< T, Dim >::ConstIterator end(PartBunch< T, Dim > const &bunch)
PartBunch< T, Dim >::ConstIterator begin(PartBunch< T, Dim > const &bunch)
double T
Definition datatypes.h:7
STL namespace.
virtual void visitBeamline(const Beamline &)=0
Apply the algorithm to a beam line.
virtual const std::string & getName() const
Get element name.
virtual double getArcLength() const
Get arc length.
ElementBase(const std::string &name)
Constructor with given name.
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
bool isSharable() const
Test if the element can be shared.
Displacement and rotation in space.
Definition Euclid3D.h:68
const Euclid3D & dotBy(const Euclid3D &rhs)
Dot product with assign.
Definition Euclid3D.cpp:102
Beamline(const std::string &name)
Constructor with given name.
Definition Beamline.cpp:40
Implements the composite geometry of a beam line.
bool getRelativeFlag() const
Definition TBeamline.h:423
TBeamline(const TBeamline< T > &right)
Definition TBeamline.h:178
Quaternion getInitialDirection() const
Definition TBeamline.h:413
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition TBeamline.h:373
void setRelativeFlag(bool flag)
Definition TBeamline.h:418
virtual double getArcLength() const
Get arc length.
Definition TBeamline.h:273
virtual TBeamline< T > * clone() const
Make clone.
Definition TBeamline.h:211
virtual double getEntrance() const
Get entrance position.
Definition TBeamline.h:300
size_t size() const
Get the number of elements in the TBeamline.
Definition TBeamline.h:428
virtual const BeamlineGeometry & getGeometry() const
Get geometry.
Definition TBeamline.h:268
void setOrigin3D(const Vector_t< double, 3 > &ori)
Definition TBeamline.h:398
virtual TBeamline< T > * copyStructure()
Make structure copy.
Definition TBeamline.h:230
virtual Euclid3D getTotalTransform() const
Get transform.
Definition TBeamline.h:357
virtual Euclid3D getExitFrame() const
Get transform.
Definition TBeamline.h:378
virtual void prepend(const T &)
Prepend a T object.
Definition TBeamline.h:393
virtual Euclid3D getTransform(double s) const
Get transform.
Definition TBeamline.h:368
virtual void makeSharable()
Set sharable flag.
Definition TBeamline.h:254
virtual double getOrigin() const
Get origin position.
Definition TBeamline.h:295
TBeamline()
Default constructor.
Definition TBeamline.h:158
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition TBeamline.h:310
Vector_t< double, 3 > itsOrigin_m
Definition TBeamline.h:149
void setInitialDirection(const Quaternion &rot)
Definition TBeamline.h:408
virtual void append(const T &)
Append a T object.
Definition TBeamline.h:388
virtual ~TBeamline()
Definition TBeamline.h:188
virtual ElementType getType() const
Get beamline type.
Definition TBeamline.h:383
virtual double getElementLength() const
Get design length.
Definition TBeamline.h:284
Vector_t< double, 3 > getOrigin3D() const
Definition TBeamline.h:403
virtual BeamlineGeometry & getGeometry()
Get geometry.
Definition TBeamline.h:263
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition TBeamline.h:192
TBeamline(const std::string &name)
Constructor with given name.
Definition TBeamline.h:168
virtual double getExit() const
Get exit position.
Definition TBeamline.h:305
virtual void iterate(BeamlineVisitor &, bool r2l) const
Apply visitor to all elements of the line.
Definition TBeamline.h:197
BeamlineGeometry itsGeometry
Definition TBeamline.h:147