OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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"
27 #include "Algorithms/Quaternion.h"
28 #include <algorithm>
29 #include <string>
30 #include <list>
31 
32 template <class T>
33 class TBeamline: public Beamline, public std::list<T> {
34 
35 public:
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.
69  virtual BeamlineGeometry &getGeometry();
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 ElementBase::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;
143 protected:
144 
146  // Exists to match the interface for ElementBase.
148 
152 };
153 
154 
155 // Implementation of template class TBeamline
156 // ------------------------------------------------------------------------
157 
158 template <class T>
160  Beamline(),
161  std::list<T>(),
162  itsGeometry(*this),
163  itsOrigin_m(0),
164  itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
165  relativePositions_m(false)
166 {}
167 
168 
169 template <class T>
170 TBeamline<T>::TBeamline(const std::string &name):
171  Beamline(name),
172  std::list<T>(),
173  itsGeometry(*this),
174  itsOrigin_m(0),
175  itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
176  relativePositions_m(false)
177 {}
178 
179 
180 template <class T>
182  Beamline(rhs),
183  std::list<T>(rhs),
184  itsGeometry(*this),
185  itsOrigin_m(rhs.itsOrigin_m),
186  itsCoordTrafoTo_m(rhs.itsCoordTrafoTo_m),
187  relativePositions_m(rhs.relativePositions_m)
188 {}
189 
190 
191 template <class T>
193 {}
194 
195 
196 template <class T>
197 void TBeamline<T>::accept(BeamlineVisitor &visitor) const {
198  visitor.visitBeamline(*this);
199 }
200 
201 
202 template <class T>
203 void TBeamline<T>::iterate(BeamlineVisitor &visitor, bool r2l) const {
204  if(r2l) {
205  for(typename std::list<T>::const_reverse_iterator op = this->rbegin();
206  op != this->rend(); ++op) {
207  op->accept(visitor);
208  }
209  } else {
210  for(typename std::list<T>::const_iterator op = this->begin();
211  op != this->end(); ++op) {
212  op->accept(visitor);
213  }
214  }
215 }
216 
217 
218 template <class T>
220  TBeamline<T> *line = new TBeamline(getName());
221 
222  for(typename std::list<T>::const_iterator op = this->begin();
223  op != this->end(); ++op) {
224  // Make copy of the T object containing a deep copy of its child.
225  T newObj(*op);
226  newObj.setElement(op->getElement()->clone());
227  line->append(newObj);
228  }
229 
230  line->itsOrigin_m = itsOrigin_m;
231  line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
232  line->relativePositions_m = relativePositions_m;
233 
234  return line;
235 }
236 
237 // 21-6-2000 ada change iterator to const_iterator
238 template <class T>
240  if(isSharable()) {
241  return this;
242  } else {
243  TBeamline<T> *line = new TBeamline(getName());
244 
245  for(typename std::list<T>::const_iterator iter = this->begin();
246  iter != this->end(); ++iter) {
247  // The copy constructor ensures proper transmission of data.
248  T newObj(*iter);
249  newObj.setElement(iter->getElement()->copyStructure());
250  line->append(newObj);
251  }
252 
253  line->itsOrigin_m = itsOrigin_m;
254  line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
255  line->relativePositions_m = relativePositions_m;
256 
257  return line;
258  }
259 }
260 
261 // 21-6-2000 ada change iterator to const_iterator
262 template <class T> inline
264  shareFlag = true;
265 
266  for(typename std::list<T>::const_iterator iter = this->begin();
267  iter != this->end(); ++iter) {
268  iter->getElement()->makeSharable();
269  }
270 }
271 
272 
273 template <class T> inline
275  return itsGeometry;
276 }
277 
278 
279 template <class T> inline
281  return itsGeometry;
282 }
283 
284 
285 template <class T>
287  double length = 0.0;
288 
289  for(typename std::list<T>::const_iterator iter = this->begin();
290  iter != this->end(); ++iter) {
291  length += iter->getElement()->getArcLength();
292  }
293 
294  return length;
295 }
296 
297 
298 template <class T>
300  double length = 0.0;
301 
302  for(typename std::list<T>::const_iterator iter = this->begin();
303  iter != this->end(); ++iter) {
304  length += iter->getElement()->getElementLength();
305  }
306 
307  return length;
308 }
309 
310 
311 template <class T>
312 double TBeamline<T>::getOrigin() const {
313  return (getArcLength() / 2.0);
314 }
315 
316 
317 template <class T>
319  return (- getOrigin());
320 }
321 
322 
323 template <class T>
324 double TBeamline<T>::getExit() const {
325  return (getArcLength() / 2.0);
326 }
327 
328 
329 template <class T>
330 Euclid3D TBeamline<T>::getTransform(double fromS, double toS) const {
331  Euclid3D transform;
332 
333  if(fromS < toS) {
334  double s1 = getEntrance();
335  typename std::list<T>::const_iterator iter = this->begin();
336 
337  while(iter != this->end() && s1 <= toS) {
338  const ElementBase &element = *iter->getElement();
339  double l = element.getArcLength();
340  double s2 = s1 + l;
341 
342  if(s2 > fromS) {
343  double s0 = (s1 + s2) / 2.0;
344  double arc1 = std::max(s1, fromS) - s0;
345  double arc2 = std::min(s2, toS) - s0;
346  transform *= element.getTransform(arc1, arc2);
347  }
348 
349  s1 = s2;
350  ++iter;
351  }
352  } else {
353  double s1 = getExit();
354  typename std::list<T>::const_reverse_iterator iter = this->rbegin();
355 
356  while(iter != this->rend() && s1 >= toS) {
357  const ElementBase &element = *iter->getElement();
358  double l = element.getArcLength();
359  double s2 = s1 - l;
360 
361  if(s2 < fromS) {
362  double s0 = (s1 + s2) / 2.0;
363  double arc1 = std::min(s1, fromS) - s0;
364  double arc2 = std::max(s2, toS) - s0;
365  transform *= element.getTransform(arc1, arc2);
366  }
367 
368  s1 = s2;
369  ++iter;
370  }
371  }
372 
373  return transform;
374 }
375 
376 
377 template <class T>
379  Euclid3D transform;
380 
381  for(typename std::list<T>::const_iterator iter = this->begin();
382  iter != this->end(); ++iter) {
383  transform.dotBy(iter->getElement()->getTotalTransform());
384  }
385 
386  return transform;
387 }
388 
389 
390 template <class T>
392  return getTransform(0.0, s);
393 }
394 
395 
396 template <class T>
398  return getTransform(0.0, getEntrance());
399 }
400 
401 
402 template <class T>
404  return getTransform(0.0, getExit());
405 }
406 
407 
408 template <class T> inline
410  return BEAMLINE;
411 }
412 
413 
414 template <class T> inline
415 void TBeamline<T>::append(const T &obj) {
416  this->push_back(obj);
417 }
418 
419 
420 template <class T> inline
421 void TBeamline<T>::prepend(const T &obj) {
422  this->push_front(obj);
423 }
424 
425 template <class T> inline
427  itsOrigin_m = ori;
428 }
429 
430 template <class T> inline
432  return itsOrigin_m;
433 }
434 
435 template <class T> inline
437  itsCoordTrafoTo_m = trafoTo;
438 }
439 
440 template <class T> inline
442  return itsCoordTrafoTo_m;
443 }
444 
445 template <class T> inline
447  relativePositions_m = flag;
448 }
449 
450 template <class T> inline
452  return relativePositions_m;
453 }
454 #endif // CLASSIC_TBeamline_HH
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:428
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:452
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:451
Quaternion getInitialDirection() const
Definition: TBeamline.h:441
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: TBeamline.h:397
void setRelativeFlag(bool flag)
Definition: TBeamline.h:446
virtual double getArcLength() const
Get arc length.
Definition: TBeamline.h:286
virtual TBeamline< T > * clone() const
Make clone.
Definition: TBeamline.h:219
Vector_t itsOrigin_m
Definition: TBeamline.h:149
virtual double getEntrance() const
Get entrance position.
Definition: TBeamline.h:318
virtual TBeamline< T > * copyStructure()
Make structure copy.
Definition: TBeamline.h:239
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: TBeamline.h:378
virtual Euclid3D getExitFrame() const
Get transform.
Definition: TBeamline.h:403
virtual void prepend(const T &)
Prepend a T object.
Definition: TBeamline.h:421
virtual void makeSharable()
Set sharable flag.
Definition: TBeamline.h:263
virtual double getOrigin() const
Get origin position.
Definition: TBeamline.h:312
TBeamline()
Default constructor.
Definition: TBeamline.h:159
void setOrigin3D(const Vector_t &ori)
Definition: TBeamline.h:426
bool relativePositions_m
Definition: TBeamline.h:151
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: TBeamline.h:330
void setInitialDirection(const Quaternion &rot)
Definition: TBeamline.h:436
virtual void append(const T &)
Append a T object.
Definition: TBeamline.h:415
virtual ~TBeamline()
Definition: TBeamline.h:192
Quaternion itsCoordTrafoTo_m
Definition: TBeamline.h:150
virtual double getElementLength() const
Get design length.
Definition: TBeamline.h:299
Vector_t getOrigin3D() const
Definition: TBeamline.h:431
virtual BeamlineGeometry & getGeometry()
Get geometry.
Definition: TBeamline.h:274
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition: TBeamline.h:197
virtual ElementBase::ElementType getType() const
Get beamline type.
Definition: TBeamline.h:409
virtual double getExit() const
Get exit position.
Definition: TBeamline.h:324
virtual void iterate(BeamlineVisitor &, bool r2l) const
Apply visitor to all elements of the line.
Definition: TBeamline.h:203
BeamlineGeometry itsGeometry
The beamline geometry.
Definition: TBeamline.h:147