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