OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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
23#include "Beamlines/Beamline.h"
26#include "Algorithms/Vektor.h"
28#include <algorithm>
29#include <string>
30#include <list>
31
32template <class T>
33class TBeamline: public Beamline, public std::list<T> {
34
35public:
36
38 TBeamline();
39
41 explicit TBeamline(const std::string &name);
42
43 TBeamline(const TBeamline<T> &right);
44 virtual ~TBeamline();
45
47 virtual void accept(BeamlineVisitor &) const;
48
50 // If the parameter [b]r2l[/b] is true, the line is traversed from
51 // right (s=C) to left (s=0).
52 // If any error occurs, this method may throw an exception.
53 virtual void iterate(BeamlineVisitor &, bool r2l) const;
54
56 virtual TBeamline<T> *clone() const;
57
59 virtual TBeamline<T> *copyStructure();
60
62 // The whole beamline and the elements depending on [b]this[/b] are
63 // marked as sharable. After this call a [b]copyStructure()[/b] call
64 // reuses the element.
65 virtual void makeSharable();
66
68 // Version for non-constant object.
70
72 // Version for constant object.
73 virtual const BeamlineGeometry &getGeometry() const;
74
76 // Return the length of the geometry, measured along the design orbit.
77 virtual double getArcLength() const;
78
80 // Return the length of the geometry, measured along the design polygone.
81 virtual double getElementLength() const;
82
84 // Return the arc length from the entrance to the origin of the
85 // geometry (non-negative).
86 virtual double getOrigin() const;
87
89 // Return the arc length from the origin to the entrance of the
90 // geometry (non-positive).
91 virtual double getEntrance() const;
92
94 // Return the arc length from the origin to the exit of the
95 // geometry (non-negative).
96 virtual double getExit() const;
97
99 // Return the transform of the local coordinate system from the
100 // position [b]fromS[/b] to the position [b]toS[/b].
101 virtual Euclid3D getTransform(double fromS, double toS) const;
102
104 // Equivalent to getTransform(0.0, s).
105 // Return the transform of the local coordinate system from the
106 // origin and [b]s[/b].
107 virtual Euclid3D getTransform(double s) const;
108
110 // Equivalent to getTransform(getEntrance(), getExit()).
111 // Return the transform of the local coordinate system from the
112 // entrance to the exit of the element.
113 virtual Euclid3D getTotalTransform() const;
114
116 // Equivalent to getTransform(0.0, getEntrance()).
117 // Return the transform of the local coordinate system from the
118 // origin to the entrance of the element.
119 virtual Euclid3D getEntranceFrame() const;
120
122 // Equivalent to getTransform(0.0, getExit()).
123 // Return the transform of the local coordinate system from the
124 // origin to the exit of the element.
125 virtual Euclid3D getExitFrame() const;
126
128 virtual ElementType getType() const;
129
131 virtual void append(const T &);
132
134 virtual void prepend(const T &);
135
136 void setOrigin3D(const Vector_t& ori);
137 Vector_t getOrigin3D() const;
138 void setInitialDirection(const Quaternion& rot);
140
141 void setRelativeFlag(bool flag);
142 bool getRelativeFlag() const;
144 size_t size() const;
145protected:
146
148 // Exists to match the interface for ElementBase.
150
154};
155
156
157// Implementation of template class TBeamline
158// ------------------------------------------------------------------------
159
160template <class T>
162 Beamline(),
163 std::list<T>(),
164 itsGeometry(*this),
165 itsOrigin_m(0),
166 itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
167 relativePositions_m(false)
168{}
169
170
171template <class T>
172TBeamline<T>::TBeamline(const std::string &name):
173 Beamline(name),
174 std::list<T>(),
175 itsGeometry(*this),
176 itsOrigin_m(0),
177 itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
178 relativePositions_m(false)
179{}
180
181
182template <class T>
184 Beamline(rhs),
185 std::list<T>(rhs),
186 itsGeometry(*this),
187 itsOrigin_m(rhs.itsOrigin_m),
188 itsCoordTrafoTo_m(rhs.itsCoordTrafoTo_m),
189 relativePositions_m(rhs.relativePositions_m)
190{}
191
192
193template <class T>
195{}
196
197
198template <class T>
200 visitor.visitBeamline(*this);
201}
202
203
204template <class T>
205void TBeamline<T>::iterate(BeamlineVisitor &visitor, bool r2l) const {
206 if(r2l) {
207 for(typename std::list<T>::const_reverse_iterator op = this->rbegin();
208 op != this->rend(); ++op) {
209 op->accept(visitor);
210 }
211 } else {
212 for(typename std::list<T>::const_iterator op = this->begin();
213 op != this->end(); ++op) {
214 op->accept(visitor);
215 }
216 }
217}
218
219
220template <class T>
222 TBeamline<T> *line = new TBeamline(getName());
223
224 for(typename std::list<T>::const_iterator op = this->begin();
225 op != this->end(); ++op) {
226 // Make copy of the T object containing a deep copy of its child.
227 T newObj(*op);
228 newObj.setElement(op->getElement()->clone());
229 line->append(newObj);
230 }
231
232 line->itsOrigin_m = itsOrigin_m;
233 line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
234 line->relativePositions_m = relativePositions_m;
235
236 return line;
237}
238
239// 21-6-2000 ada change iterator to const_iterator
240template <class T>
242 if(isSharable()) {
243 return this;
244 } else {
245 TBeamline<T> *line = new TBeamline(getName());
246
247 for(typename std::list<T>::const_iterator iter = this->begin();
248 iter != this->end(); ++iter) {
249 // The copy constructor ensures proper transmission of data.
250 T newObj(*iter);
251 newObj.setElement(iter->getElement()->copyStructure());
252 line->append(newObj);
253 }
254
255 line->itsOrigin_m = itsOrigin_m;
256 line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
257 line->relativePositions_m = relativePositions_m;
258
259 return line;
260 }
261}
262
263// 21-6-2000 ada change iterator to const_iterator
264template <class T> inline
266 shareFlag = true;
267
268 for(typename std::list<T>::const_iterator iter = this->begin();
269 iter != this->end(); ++iter) {
270 iter->getElement()->makeSharable();
271 }
272}
273
274
275template <class T> inline
277 return itsGeometry;
278}
279
280
281template <class T> inline
283 return itsGeometry;
284}
285
286
287template <class T>
289 double length = 0.0;
290
291 for(typename std::list<T>::const_iterator iter = this->begin();
292 iter != this->end(); ++iter) {
293 length += iter->getElement()->getArcLength();
294 }
295
296 return length;
297}
298
299
300template <class T>
302 double length = 0.0;
303
304 for(typename std::list<T>::const_iterator iter = this->begin();
305 iter != this->end(); ++iter) {
306 length += iter->getElement()->getElementLength();
307 }
308
309 return length;
310}
311
312
313template <class T>
315 return (getArcLength() / 2.0);
316}
317
318
319template <class T>
321 return (- getOrigin());
322}
323
324
325template <class T>
326double TBeamline<T>::getExit() const {
327 return (getArcLength() / 2.0);
328}
329
330
331template <class T>
332Euclid3D TBeamline<T>::getTransform(double fromS, double toS) const {
333 Euclid3D transform;
334
335 if(fromS < toS) {
336 double s1 = getEntrance();
337 typename std::list<T>::const_iterator iter = this->begin();
338
339 while(iter != this->end() && s1 <= toS) {
340 const ElementBase &element = *iter->getElement();
341 double l = element.getArcLength();
342 double s2 = s1 + l;
343
344 if(s2 > fromS) {
345 double s0 = (s1 + s2) / 2.0;
346 double arc1 = std::max(s1, fromS) - s0;
347 double arc2 = std::min(s2, toS) - s0;
348 transform *= element.getTransform(arc1, arc2);
349 }
350
351 s1 = s2;
352 ++iter;
353 }
354 } else {
355 double s1 = getExit();
356 typename std::list<T>::const_reverse_iterator iter = this->rbegin();
357
358 while(iter != this->rend() && s1 >= toS) {
359 const ElementBase &element = *iter->getElement();
360 double l = element.getArcLength();
361 double s2 = s1 - l;
362
363 if(s2 < fromS) {
364 double s0 = (s1 + s2) / 2.0;
365 double arc1 = std::min(s1, fromS) - s0;
366 double arc2 = std::max(s2, toS) - s0;
367 transform *= element.getTransform(arc1, arc2);
368 }
369
370 s1 = s2;
371 ++iter;
372 }
373 }
374
375 return transform;
376}
377
378
379template <class T>
381 Euclid3D transform;
382
383 for(typename std::list<T>::const_iterator iter = this->begin();
384 iter != this->end(); ++iter) {
385 transform.dotBy(iter->getElement()->getTotalTransform());
386 }
387
388 return transform;
389}
390
391
392template <class T>
394 return getTransform(0.0, s);
395}
396
397
398template <class T>
400 return getTransform(0.0, getEntrance());
401}
402
403
404template <class T>
406 return getTransform(0.0, getExit());
407}
408
409
410template <class T> inline
413}
414
415
416template <class T> inline
417void TBeamline<T>::append(const T &obj) {
418 this->push_back(obj);
419}
420
421
422template <class T> inline
423void TBeamline<T>::prepend(const T &obj) {
424 this->push_front(obj);
425}
426
427template <class T> inline
429 itsOrigin_m = ori;
430}
431
432template <class T> inline
434 return itsOrigin_m;
435}
436
437template <class T> inline
439 itsCoordTrafoTo_m = trafoTo;
440}
441
442template <class T> inline
444 return itsCoordTrafoTo_m;
445}
446
447template <class T> inline
449 relativePositions_m = flag;
450}
451
452template <class T> inline
454 return relativePositions_m;
455}
456
457template <class T>
458size_t TBeamline<T>::size() const {
459 size_t blSize = 0;
460 for(typename std::list<T>::const_iterator iter = this->begin();
461 iter != this->end(); ++iter) {
462 blSize++;
463 }
464
465 return blSize;
466}
467
468#endif // CLASSIC_TBeamline_HH
ElementType
Definition: ElementBase.h:88
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:76
const std::string name
virtual void visitBeamline(const Beamline &)=0
Apply the algorithm to a beam line.
virtual double getArcLength() const
Get arc length.
Definition: ElementBase.h:410
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:434
Displacement and rotation in space.
Definition: Euclid3D.h:68
const Euclid3D & dotBy(const Euclid3D &rhs)
Dot product with assign.
Definition: Euclid3D.cpp:102
An abstract sequence of beam line components.
Definition: Beamline.h:34
Implements the composite geometry of a beam line.
bool getRelativeFlag() const
Definition: TBeamline.h:453
Quaternion getInitialDirection() const
Definition: TBeamline.h:443
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: TBeamline.h:399
void setRelativeFlag(bool flag)
Definition: TBeamline.h:448
virtual double getArcLength() const
Get arc length.
Definition: TBeamline.h:288
virtual TBeamline< T > * clone() const
Make clone.
Definition: TBeamline.h:221
Vector_t itsOrigin_m
Definition: TBeamline.h:151
virtual double getEntrance() const
Get entrance position.
Definition: TBeamline.h:320
size_t size() const
Get the number of elements in the TBeamline.
Definition: TBeamline.h:458
virtual TBeamline< T > * copyStructure()
Make structure copy.
Definition: TBeamline.h:241
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: TBeamline.h:380
virtual Euclid3D getExitFrame() const
Get transform.
Definition: TBeamline.h:405
virtual void prepend(const T &)
Prepend a T object.
Definition: TBeamline.h:423
virtual void makeSharable()
Set sharable flag.
Definition: TBeamline.h:265
virtual double getOrigin() const
Get origin position.
Definition: TBeamline.h:314
TBeamline()
Default constructor.
Definition: TBeamline.h:161
void setOrigin3D(const Vector_t &ori)
Definition: TBeamline.h:428
bool relativePositions_m
Definition: TBeamline.h:153
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: TBeamline.h:332
void setInitialDirection(const Quaternion &rot)
Definition: TBeamline.h:438
virtual void append(const T &)
Append a T object.
Definition: TBeamline.h:417
virtual ~TBeamline()
Definition: TBeamline.h:194
virtual ElementType getType() const
Get beamline type.
Definition: TBeamline.h:411
Quaternion itsCoordTrafoTo_m
Definition: TBeamline.h:152
virtual double getElementLength() const
Get design length.
Definition: TBeamline.h:301
Vector_t getOrigin3D() const
Definition: TBeamline.h:433
virtual BeamlineGeometry & getGeometry()
Get geometry.
Definition: TBeamline.h:276
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition: TBeamline.h:199
virtual double getExit() const
Get exit position.
Definition: TBeamline.h:326
virtual void iterate(BeamlineVisitor &, bool r2l) const
Apply visitor to all elements of the line.
Definition: TBeamline.h:205
BeamlineGeometry itsGeometry
The beamline geometry.
Definition: TBeamline.h:149