OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Offset.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Chris Rogers
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  * 3. Neither the name of STFC nor the names of its contributors may be used to
12  * endorse or promote products derived from this software without specific
13  * prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "AbsBeamline/Offset.h"
29 
30 #include <cmath>
31 
35 #include "Physics/Physics.h"
36 
37 const double Offset::lengthUnits_m = 1e3;
38 double Offset::float_tolerance = 1e-12;
39 
40 Offset::Offset(const std::string& name)
41  : Component(name), _is_local(false), geometry_m(NULL) {
44 }
45 
47  : Offset("")
48 {}
49 
50 Offset::Offset(std::string name, const Offset& rhs)
51  : Component(name), _is_local(false), geometry_m(NULL) {
52  *this = rhs;
53  setName(name);
54 }
55 
57  : Component(rhs.getName()), _is_local(false), geometry_m(NULL) {
58  *this = rhs;
59 }
60 
62  delete geometry_m;
63 }
64 
66  if (&rhs == this) {
67  return *this;
68  }
69  setName(rhs.getName());
72  _is_local = rhs._is_local;
73 
74  if (geometry_m != NULL)
75  delete geometry_m;
76  if (rhs.geometry_m == NULL) {
77  geometry_m = NULL;
78  } else {
80  }
81  setAperture(rhs.getAperture().first, rhs.getAperture().second);
82  setElType(rhs.getElType());
83  return *this;
84 }
85 
86 void Offset::accept(BeamlineVisitor & visitor) const {
87  visitor.visitOffset(*this);
88 }
89 
91  throw GeneralClassicException("Offset::getField()",
92  "No field defined for Offset");
93 }
94 
95 const EMField &Offset::getField() const {
96  throw GeneralClassicException("Offset::getField() const",
97  "No field defined for Offset");
98 }
99 
100 void Offset::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
101  RefPartBunch_m = bunch;
102 }
103 
105  RefPartBunch_m = NULL;
106 }
107 
109  return new Offset(*this);
110 }
111 
113  _end_position = position;
114 }
115 
117  return _end_position;
118 }
119 
121  _end_direction = direction;
122 }
123 
125  return _end_direction;
126 }
127 
128 void Offset::setIsLocal(bool isLocal) {
129  _is_local = isLocal;
130 }
131 
132 bool Offset::getIsLocal() const {
133  return _is_local;
134 }
135 
137  return *geometry_m;
138 }
139 
141  return *geometry_m;
142 }
143 
144 // std::ostream& operator<<(std::ostream& out, const Vector_t& vec) {
145 // out << "(" << vec(0) << ", " << vec(1) << ", " << vec(2) << ")";
146 // return out;
147 // }
148 
149 double Offset::getTheta(Vector_t vec1, Vector_t vec2) {
150  if (fabs(vec1(2)) > 1e-9 || fabs(vec2(2)) > 1e-9)
151  throw GeneralClassicException("Offset::getTheta(...)",
152  "Rotations out of midplane are not implemented");
153  // probably not the most efficient, but only called at set up
154  double theta = atan2(vec2(1), vec2(0))-atan2(vec1(1), vec1(0));
155  if (theta < -Physics::pi)
156  theta += 2.*Physics::pi; // force into domain -pi < theta < pi
157  return theta;
158 }
159 
161  double s = sin(theta);
162  double c = cos(theta);
163  return Vector_t(+vec(0)*c-vec(1)*s,
164  +vec(0)*s+vec(1)*c,
165  0.);
166 }
167 
169  if (!_is_local)
170  throw GeneralClassicException("Offset::updateGeometry(...)",
171  "Global offset needs a local coordinate system");
172  Vector_t translation = getEndPosition();
173  double length = sqrt(translation(0)*translation(0)+
174  translation(1)*translation(1)+
175  translation(2)*translation(2));
176  double theta_in = getTheta(Vector_t(1., 0., 0.), translation);
177  double theta_out = getTheta(Vector_t(1., 0., 0.), getEndDirection());
178  Euclid3D euclid3D(-sin(theta_in)*length, 0., cos(theta_in)*length,
179  0., -theta_out, 0.);
180  if (geometry_m != NULL)
181  delete geometry_m;
182  geometry_m = new Euclid3DGeometry(euclid3D);
183 }
184 
185 void Offset::updateGeometry(Vector_t startPosition, Vector_t startDirection) {
186  if (!_is_local) {
187  Vector_t translationGlobal = _end_position;
188  double theta_g2l = getTheta(startDirection, Vector_t(1, 0, 0));
189  _end_position = rotate(translationGlobal, theta_g2l);
190  _end_direction = rotate(_end_direction, theta_g2l);
191  _is_local = true;
192  }
193  updateGeometry();
194 }
195 
197  return geometry_m != NULL;
198 }
199 
200 bool operator==(const Offset& off1, const Offset& off2) {
201  const double tol = Offset::float_tolerance;
202  if (off1.getName() != off2.getName() ||
203  off1.getIsLocal() != off2.getIsLocal()) {
204  return false;
205  }
206  for (int i = 0; i < 3; ++i) {
207  if ( (fabs(off1.getEndPosition()(i)-off2.getEndPosition()(i)) > tol) ||
208  (fabs(off1.getEndDirection()(i)-off2.getEndDirection()(i)) > tol))
209  return false;
210  }
211  if ( (!off1.isGeometryAllocated() && off2.isGeometryAllocated()) ||
212  (!off2.isGeometryAllocated() && off1.isGeometryAllocated()))
213  return false;
214  Euclid3D transform1 = off1.getGeometry().getTotalTransform();
215  Euclid3D transform2 = off2.getGeometry().getTotalTransform();
216  Vector3D dTranslation = transform1.getVector() - transform2.getVector();
217  Vector3D dRotation = transform1.getRotation().getAxis() -
218  transform2.getRotation().getAxis();
219  for (size_t i = 0; i < 3; ++i)
220  if (fabs(dTranslation(i)) > tol || fabs(dRotation(i)) > tol)
221  return false;
222  return true;
223 }
224 
225 bool operator!=(const Offset& off1, const Offset& off2) {
226  return !(off1 == off2);
227 }
228 
229 std::ostream& operator<<(std::ostream& out, const Offset& off) {
230  out << "Offset " << off.getName() << " local " << off.getIsLocal()
231  << " end pos: " << off.getEndPosition()
232  << " end dir: " << off.getEndDirection() << std::endl;
233  return out;
234 }
235 
236 bool Offset::bends() const {
237  if (geometry_m == NULL) {
238  throw GeneralClassicException("Offset::bends",
239  "Try to determine if Offset bends when geometry_m not allocated");
240  }
242  for (size_t i = 0; i < 3; ++i)
243  if (fabs(rotation.getAxis()(i)) > float_tolerance) {
244  return true;
245  }
247  if (fabs(vector(0)) > float_tolerance || fabs(vector(1)) > float_tolerance) {
248  return true;
249  }
250  return false;
251 }
252 
253 
255  double phi_in,
256  double phi_out,
257  double displacement) {
258  Offset off(name);
259  displacement *= lengthUnits_m;
260  off.setEndPosition(Vector_t(cos(phi_in)*displacement,
261  sin(phi_in)*displacement,
262  0.));
263  off.setEndDirection(Vector_t(cos(phi_in+phi_out), sin(phi_in+phi_out), 0.));
264  off.setIsLocal(true);
265  off.updateGeometry();
266  return off;
267 }
268 
270  double radius_out,
271  double phi_out,
272  double theta_out) {
273  Offset off(name);
274  radius_out *= lengthUnits_m;
275  off.setEndPosition(Vector_t(cos(phi_out)*radius_out,
276  sin(phi_out)*radius_out,
277  0.));
278  off.setEndDirection(Vector_t(sin(phi_out+theta_out),
279  cos(phi_out+theta_out),
280  0.));
281  off.setIsLocal(false);
282  return off;
283 }
284 
286  Vector_t end_position,
287  Vector_t end_direction) {
288  Offset off(name);
289  end_position *= lengthUnits_m;
290  off.setEndPosition(end_position);
291  off.setEndDirection(end_direction);
292  off.setIsLocal(true);
293  off.updateGeometry();
294  return off;
295 }
296 
298  Vector_t end_position,
299  Vector_t end_direction) {
300  Offset off(name);
301  end_position *= lengthUnits_m;
302  off.setEndPosition(end_position);
303  off.setEndDirection(end_direction);
304  off.setIsLocal(false);
305  return off;
306 }
void finalise() override
Definition: Offset.cpp:104
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
constexpr double e
The value of .
Definition: Physics.h:40
Interface for basic beam line object.
Definition: ElementBase.h:128
static double float_tolerance
Definition: Offset.h:206
Definition: Offset.h:66
static Offset localCylindricalOffset(std::string name, double theta_in, double theta_out, double displacement)
Definition: Offset.cpp:254
Vector_t getEndPosition() const
Definition: Offset.cpp:116
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
virtual void setName(const std::string &name)
Set element name.
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
ElemType getElType() const
returns element type as enumeration needed in the envelope tracker
Definition: ElementBase.h:587
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
virtual void visitOffset(const Offset &)=0
Apply the algorithm to an Offset (placement).
static Offset globalCylindricalOffset(std::string name, double radius_out, double phi_out, double theta_out)
Definition: Offset.cpp:269
void setEndDirection(Vector_t direction)
Definition: Offset.cpp:120
void initialise(PartBunchBase< double, 3 > *bunch, double &startField, double &endField) override
Definition: Offset.cpp:100
Euclid3DGeometry & getGeometry() override
Get geometry.
Definition: Offset.cpp:136
~Offset()
Definition: Offset.cpp:61
void updateGeometry()
Definition: Offset.cpp:168
constexpr double pi
The value of .
Definition: Physics.h:31
void setElType(ElemType elt)
set the element type as enumeration needed in the envelope tracker
Definition: ElementBase.h:591
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
Displacement and rotation in space.
Definition: Euclid3D.h:68
ElementBase * clone() const override
Definition: Offset.cpp:108
static Offset globalCartesianOffset(std::string name, Vector_t end_position, Vector_t end_direction)
Definition: Offset.cpp:297
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:200
Offset & operator=(const Offset &)
Definition: Offset.cpp:65
PETE_TBTree< FnArcTan2, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > atan2(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
Vector_t _end_position
Definition: Offset.h:208
bool _is_local
Definition: Offset.h:210
const Rotation3D & getRotation() const
Get rotation.
Definition: Euclid3D.cpp:64
static Offset localCartesianOffset(std::string name, Vector_t end_position, Vector_t end_direction)
Definition: Offset.cpp:285
bool bends() const override
Definition: Offset.cpp:236
Abstract base class for electromagnetic fields.
Definition: EMField.h:188
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
Offset()
Definition: Offset.cpp:46
A 3-dimension vector.
Definition: Vector3D.h:31
bool operator!=(const Offset &off1, const Offset &off2)
Definition: Offset.cpp:225
Definition: Vec.h:21
bool getIsLocal() const
Definition: Offset.cpp:132
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
const std::string name
Vector_t _end_direction
Definition: Offset.h:209
void setAperture(const ApertureType &type, const std::vector< double > &args)
Definition: ElementBase.h:625
void updateGeometry(Vector_t startPosition, Vector_t startDirection)
Definition: Offset.cpp:185
virtual Euclid3D getTotalTransform() const
Get total transform from beginning to end.
Euclid3DGeometry * geometry_m
Definition: Offset.h:212
static const double lengthUnits_m
Definition: Offset.h:213
Vector_t getEndDirection() const
Definition: Offset.cpp:124
void accept(BeamlineVisitor &) const override
Definition: Offset.cpp:86
Interface for a single beam element.
Definition: Component.h:51
std::pair< ElementBase::ApertureType, std::vector< double > > getAperture() const
Definition: ElementBase.h:632
Abstract algorithm.
Rotation in 3-dimensional space.
Definition: Rotation3D.h:46
void setIsLocal(bool isLocal)
Definition: Offset.cpp:128
static double getTheta(Vector_t vec1, Vector_t vec2)
Definition: Offset.cpp:149
EMField & getField() override
Not implemented - throws GeneralClassicException.
Definition: Offset.cpp:90
Vector3D getAxis() const
Get axis vector.
Definition: Rotation3D.cpp:91
void setEndPosition(Vector_t position)
Definition: Offset.cpp:112
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
bool isGeometryAllocated() const
Definition: Offset.cpp:196
const Vector3D & getVector() const
Get displacement.
Definition: Euclid3D.cpp:59
bool operator==(const TwoPolynomial &left, const TwoPolynomial &right)
static Vector_t rotate(Vector_t vec, double theta)
Definition: Offset.cpp:160