OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 OPALX_TBeamline_HH
21#define OPALX_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:
36 TBeamline();
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
57 virtual TBeamline<T>* copyStructure();
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.
111 virtual Euclid3D getTotalTransform() const;
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
134 void setOrigin3D(const Vector_t<double, 3>& ori);
136 void setInitialDirection(const Quaternion& rot);
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
166template <class T>
167TBeamline<T>::TBeamline(const std::string& name)
168 : Beamline(name),
169 std::list<T>(),
170 itsGeometry(*this),
171 itsOrigin_m(0),
172 itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
173 relativePositions_m(false) {}
174
175template <class T>
177 : Beamline(rhs),
178 std::list<T>(rhs),
179 itsGeometry(*this),
180 itsOrigin_m(rhs.itsOrigin_m),
181 itsCoordTrafoTo_m(rhs.itsCoordTrafoTo_m),
182 relativePositions_m(rhs.relativePositions_m) {}
183
184template <class T>
186
187template <class T>
189 visitor.visitBeamline(*this);
190}
191
192template <class T>
193void TBeamline<T>::iterate(BeamlineVisitor& visitor, bool r2l) const {
194 if (r2l) {
195 for (typename std::list<T>::const_reverse_iterator op = this->rbegin(); op != this->rend();
196 ++op) {
197 op->accept(visitor);
198 }
199 } else {
200 for (typename std::list<T>::const_iterator op = this->begin(); op != this->end(); ++op) {
201 op->accept(visitor);
202 }
203 }
204}
205
206template <class T>
208 TBeamline<T>* line = new TBeamline(getName());
209
210 for (typename std::list<T>::const_iterator op = this->begin(); op != this->end(); ++op) {
211 // Make copy of the T object containing a deep copy of its child.
212 T newObj(*op);
213 newObj.setElement(op->getElement()->clone());
214 line->append(newObj);
215 }
216
217 line->itsOrigin_m = itsOrigin_m;
218 line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
219 line->relativePositions_m = relativePositions_m;
220
221 return line;
222}
223
224// 21-6-2000 ada change iterator to const_iterator
225template <class T>
227 if (isSharable()) {
228 return this;
229 } else {
230 TBeamline<T>* line = new TBeamline(getName());
231
232 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end();
233 ++iter) {
234 // The copy constructor ensures proper transmission of data.
235 T newObj(*iter);
236 newObj.setElement(iter->getElement()->copyStructure());
237 line->append(newObj);
238 }
239
240 line->itsOrigin_m = itsOrigin_m;
241 line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
242 line->relativePositions_m = relativePositions_m;
243
244 return line;
245 }
246}
247
248// 21-6-2000 ada change iterator to const_iterator
249template <class T>
251 shareFlag = true;
252
253 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
254 iter->getElement()->makeSharable();
255 }
256}
257
258template <class T>
260 return itsGeometry;
261}
262
263template <class T>
265 return itsGeometry;
266}
267
268template <class T>
270 double length = 0.0;
271
272 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
273 length += iter->getElement()->getArcLength();
274 }
275
276 return length;
277}
278
279template <class T>
281 double length = 0.0;
282
283 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
284 length += iter->getElement()->getElementLength();
285 }
286
287 return length;
288}
289
290template <class T>
292 return (getArcLength() / 2.0);
293}
294
295template <class T>
297 return (-getOrigin());
298}
299
300template <class T>
301double TBeamline<T>::getExit() const {
302 return (getArcLength() / 2.0);
303}
304
305template <class T>
306Euclid3D TBeamline<T>::getTransform(double fromS, double toS) const {
307 Euclid3D transform;
308
309 if (fromS < toS) {
310 double s1 = getEntrance();
311 typename std::list<T>::const_iterator iter = this->begin();
312
313 while (iter != this->end() && s1 <= toS) {
314 const ElementBase& element = *iter->getElement();
315 double l = element.getArcLength();
316 double s2 = s1 + l;
317
318 if (s2 > fromS) {
319 double s0 = (s1 + s2) / 2.0;
320 double arc1 = std::max(s1, fromS) - s0;
321 double arc2 = std::min(s2, toS) - s0;
322 transform *= element.getTransform(arc1, arc2);
323 }
324
325 s1 = s2;
326 ++iter;
327 }
328 } else {
329 double s1 = getExit();
330 typename std::list<T>::const_reverse_iterator iter = this->rbegin();
331
332 while (iter != this->rend() && s1 >= toS) {
333 const ElementBase& element = *iter->getElement();
334 double l = element.getArcLength();
335 double s2 = s1 - l;
336
337 if (s2 < fromS) {
338 double s0 = (s1 + s2) / 2.0;
339 double arc1 = std::min(s1, fromS) - s0;
340 double arc2 = std::max(s2, toS) - s0;
341 transform *= element.getTransform(arc1, arc2);
342 }
343
344 s1 = s2;
345 ++iter;
346 }
347 }
348
349 return transform;
350}
351
352template <class T>
354 Euclid3D transform;
355
356 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
357 transform.dotBy(iter->getElement()->getTotalTransform());
358 }
359
360 return transform;
361}
362
363template <class T>
365 return getTransform(0.0, s);
366}
367
368template <class T>
370 return getTransform(0.0, getEntrance());
371}
372
373template <class T>
375 return getTransform(0.0, getExit());
376}
377
378template <class T>
382
383template <class T>
384inline void TBeamline<T>::append(const T& obj) {
385 this->push_back(obj);
386}
387
388template <class T>
389inline void TBeamline<T>::prepend(const T& obj) {
390 this->push_front(obj);
391}
392
393template <class T>
395 itsOrigin_m = ori;
396}
397
398template <class T>
400 return itsOrigin_m;
401}
402
403template <class T>
405 itsCoordTrafoTo_m = trafoTo;
406}
407
408template <class T>
410 return itsCoordTrafoTo_m;
411}
412
413template <class T>
414inline void TBeamline<T>::setRelativeFlag(bool flag) {
415 relativePositions_m = flag;
416}
417
418template <class T>
419inline bool TBeamline<T>::getRelativeFlag() const {
420 return relativePositions_m;
421}
422
423template <class T>
424size_t TBeamline<T>::size() const {
425 size_t blSize = 0;
426 for (typename std::list<T>::const_iterator iter = this->begin(); iter != this->end(); ++iter) {
427 blSize++;
428 }
429
430 return blSize;
431}
432
433#endif // OPALX_TBeamline_HH
ippl::Vector< T, Dim > Vector_t
ElementType
Definition ElementBase.h:94
double T
Definition OPALTypes.h:8
Implements the composite geometry of a beam line.
virtual double getArcLength() const
Get arc length.
virtual void visitBeamline(const Beamline &)=0
Apply the algorithm to a beam line.
An abstract sequence of beam line components.
Definition Beamline.h:34
virtual double getArcLength() const
Get arc length.
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Displacement and rotation in space.
Definition Euclid3D.h:67
const Euclid3D & dotBy(const Euclid3D &rhs)
Dot product with assign.
Definition Euclid3D.cpp:70
Quaternion storage and rotation algebra used by OPALX geometry code.
bool getRelativeFlag() const
Definition TBeamline.h:419
Quaternion getInitialDirection() const
Definition TBeamline.h:409
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition TBeamline.h:369
void setRelativeFlag(bool flag)
Definition TBeamline.h:414
virtual double getArcLength() const
Get arc length.
Definition TBeamline.h:269
virtual TBeamline< T > * clone() const
Make clone.
Definition TBeamline.h:207
virtual double getEntrance() const
Get entrance position.
Definition TBeamline.h:296
size_t size() const
Get the number of elements in the TBeamline.
Definition TBeamline.h:424
void setOrigin3D(const Vector_t< double, 3 > &ori)
Definition TBeamline.h:394
virtual TBeamline< T > * copyStructure()
Make structure copy.
Definition TBeamline.h:226
virtual Euclid3D getTotalTransform() const
Get transform.
Definition TBeamline.h:353
virtual Euclid3D getExitFrame() const
Get transform.
Definition TBeamline.h:374
virtual void prepend(const T &)
Prepend a T object.
Definition TBeamline.h:389
virtual void makeSharable()
Set sharable flag.
Definition TBeamline.h:250
virtual double getOrigin() const
Get origin position.
Definition TBeamline.h:291
TBeamline()
Default constructor.
Definition TBeamline.h:158
bool relativePositions_m
Definition TBeamline.h:151
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition TBeamline.h:306
Vector_t< double, 3 > itsOrigin_m
Definition TBeamline.h:149
void setInitialDirection(const Quaternion &rot)
Definition TBeamline.h:404
virtual void append(const T &)
Append a T object.
Definition TBeamline.h:384
virtual ~TBeamline()
Definition TBeamline.h:185
virtual ElementType getType() const
Get beamline type.
Definition TBeamline.h:379
Quaternion itsCoordTrafoTo_m
Definition TBeamline.h:150
virtual double getElementLength() const
Get design length.
Definition TBeamline.h:280
Vector_t< double, 3 > getOrigin3D() const
Definition TBeamline.h:399
virtual BeamlineGeometry & getGeometry()
Get geometry.
Definition TBeamline.h:259
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition TBeamline.h:188
virtual double getExit() const
Get exit position.
Definition TBeamline.h:301
virtual void iterate(BeamlineVisitor &, bool r2l) const
Apply visitor to all elements of the line.
Definition TBeamline.h:193
BeamlineGeometry itsGeometry
The beamline geometry.
Definition TBeamline.h:147
STL namespace.