OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
VariableRFCavity.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 
29 
30 #include "Physics/Physics.h"
34 
35 const double VariableRFCavity::lengthUnit_m = 1e3; // metres -> mm
36 
37 VariableRFCavity::VariableRFCavity(const std::string &name) : Component(name) {
38  initNull(); // initialise everything to NULL
39 }
40 
42  initNull(); // initialise everything to NULL
43 }
44 
46  initNull(); // initialise everything to NULL
47  *this = var;
48 }
49 
51  if (&rhs == this) {
52  return *this;
53  }
54  setName(rhs.getName());
55  setPhaseModel(NULL);
56  setAmplitudeModel(NULL);
57  setFrequencyModel(NULL);
58  if (rhs.phaseTD_m != NULL)
59  setPhaseModel(std::shared_ptr<AbstractTimeDependence>(rhs.phaseTD_m->clone()));
60  if (rhs.amplitudeTD_m != NULL) {
61  setAmplitudeModel(std::shared_ptr<AbstractTimeDependence>(rhs.amplitudeTD_m->clone()));
62  }
63  if (rhs.frequencyTD_m != NULL)
64  setFrequencyModel(std::shared_ptr<AbstractTimeDependence>(rhs.frequencyTD_m->clone()));
71  return *this;
72 }
73 
75  // shared_ptr should self-destruct when they are ready
76 }
77 
79  _length = 0.;
80  phaseName_m = "";
81  amplitudeName_m = "";
82  frequencyName_m = "";
83  halfHeight_m = 0.;
84  halfWidth_m = 0;
85  RefPartBunch_m = NULL;
86 }
87 
88 std::shared_ptr<AbstractTimeDependence> VariableRFCavity::getAmplitudeModel() const {
89  return amplitudeTD_m;
90 }
91 
92 std::shared_ptr<AbstractTimeDependence> VariableRFCavity::getPhaseModel() const {
93  return phaseTD_m;
94 }
95 
96 std::shared_ptr<AbstractTimeDependence> VariableRFCavity::getFrequencyModel() const {
97  return frequencyTD_m;
98 }
99 
100 void VariableRFCavity::setAmplitudeModel(std::shared_ptr<AbstractTimeDependence> amplitude_td) {
101  amplitudeTD_m = amplitude_td;
102 }
103 
104 void VariableRFCavity::setPhaseModel(std::shared_ptr<AbstractTimeDependence> phase_td) {
105  phaseTD_m = phase_td;
106 }
107 
108 void VariableRFCavity::setFrequencyModel(std::shared_ptr<AbstractTimeDependence> frequency_td) {
109  frequencyTD_m = frequency_td;
110 }
111 
113  return geometry;
114 }
115 
117  return geometry;
118 }
119 
121  throw GeneralClassicException("VariableRFCavity",
122  "No field defined for VariableRFCavity");
123 }
124 
126  throw GeneralClassicException("VariableRFCavity",
127  "No field defined for VariableRFCavity");
128 }
129 
130 
131 bool VariableRFCavity::apply(const size_t &i, const double &t,
132  Vector_t &E, Vector_t &B) {
133  return apply(RefPartBunch_m->R[i], RefPartBunch_m->P[i], t, E, B);
134 }
135 
136 // If this is too slow: a quicker implementation would be to use templates not
137 // inheritance (vtable lookup is removed). This is in the inner
138 // tracking loop, so low level optimisation is possibly worthwhile.
139 //
140 // Do I need bound checking here? I have no "radius" parameter, but I do have a
141 // "length".
143  const double &t, Vector_t &E, Vector_t &B) {
144  if (R[2] >= 0. && R[2] < _length) {
145  if (std::abs(R[0]) > halfWidth_m || std::abs(R[1]) > halfHeight_m) {
146  return true;
147  }
148 
149  double E0 = amplitudeTD_m->getValue(t);
150  double f = frequencyTD_m->getValue(t) * 1.0E-3; // need GHz on the element we have MHz
151  double phi = phaseTD_m->getValue(t);
152  E = Vector_t(0., 0., E0*sin(Physics::two_pi * f * t + phi));
153  return false;
154  }
155  return true;
156 }
157 
159  const double &t, Vector_t &E, Vector_t &B) {
160  return apply(R, P, t, E, B);
161 }
162 
163 void VariableRFCavity::initialise(PartBunchBase<double, 3> *bunch, double &startField, double &endField) {
164  RefPartBunch_m = bunch;
165 }
166 
168  RefPartBunch_m = NULL;
169 }
170 
172  return new VariableRFCavity(*this);
173 }
174 
176  initialise();
177  visitor.visitVariableRFCavity(*this);
178 }
179 
181  VariableRFCavity* cavity = const_cast<VariableRFCavity*>(this);
182  std::shared_ptr<AbstractTimeDependence> phaseTD =
184  cavity->setPhaseModel(std::shared_ptr<AbstractTimeDependence>(phaseTD->clone()));
185  std::shared_ptr<AbstractTimeDependence> frequencyTD =
187  cavity->setFrequencyModel(std::shared_ptr<AbstractTimeDependence>(frequencyTD->clone()));
188  std::shared_ptr<AbstractTimeDependence> amplitudeTD =
190  cavity->setAmplitudeModel(std::shared_ptr<AbstractTimeDependence>(amplitudeTD->clone()));
191 
192  if (halfHeight_m < 1e-9 || halfWidth_m < 1e-9)
193  throw GeneralClassicException("VariableRFCavity::accept",
194  "Height or width was not set on VariableRFCavity");
195 }
196 
197 void VariableRFCavity::setLength(double length) {
198  _length = length*lengthUnit_m;
200 }
StraightGeometry geometry
The cavity&#39;s geometry.
ParticleAttrib< Vector_t > P
virtual ElementBase * clone() const override
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
virtual std::shared_ptr< AbstractTimeDependence > getFrequencyModel() const
constexpr double e
The value of .
Definition: Physics.h:40
Interface for basic beam line object.
Definition: ElementBase.h:128
virtual std::shared_ptr< AbstractTimeDependence > getAmplitudeModel() const
virtual void setPhaseModel(std::shared_ptr< AbstractTimeDependence > time_dep)
virtual void setName(const std::string &name)
Set element name.
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
constexpr double two_pi
The value of .
Definition: Physics.h:34
virtual void setLength(double length)
static const double lengthUnit_m
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
virtual bool applyToReferenceParticle(const Vector_t &R, const Vector_t &P, const double &t, Vector_t &E, Vector_t &B) override
std::shared_ptr< AbstractTimeDependence > phaseTD_m
std::string frequencyName_m
virtual ~VariableRFCavity()
virtual bool apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) override
VariableRFCavity & operator=(const VariableRFCavity &)
virtual EMField & getField() override
Not implemented.
virtual void setElementLength(double length)
Set design length.
virtual void finalise() override
std::string amplitudeName_m
PartBunchBase< double, 3 > * RefPartBunch_m
Definition: Component.h:200
virtual std::shared_ptr< AbstractTimeDependence > getPhaseModel() const
Vektor< double, 3 > Vector_t
Definition: Vektor.h:6
virtual void setFrequencyModel(std::shared_ptr< AbstractTimeDependence > time_dep)
void initialise() const
Abstract base class for electromagnetic fields.
Definition: EMField.h:188
std::shared_ptr< AbstractTimeDependence > frequencyTD_m
virtual StraightGeometry & getGeometry() override
A geometry representing a straight line.
virtual void visitVariableRFCavity(const VariableRFCavity &)=0
Apply the algorithm to a variable RF cavity.
const std::string name
virtual void setAmplitudeModel(std::shared_ptr< AbstractTimeDependence > time_dep)
ParticlePos_t & R
std::shared_ptr< AbstractTimeDependence > amplitudeTD_m
Interface for a single beam element.
Definition: Component.h:51
Abstract algorithm.
static std::shared_ptr< AbstractTimeDependence > getTimeDependence(std::string name)
std::string phaseName_m
virtual void accept(BeamlineVisitor &) const override