OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
OpalVariableRFCavityFringeField.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, 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 "Physics/Physics.h"
30 #include "Attributes/Attributes.h"
36 
37 extern Inform *gmsg;
38 
40  std::string("The \"VARIABLE_RF_CAVITY_FRINGE_FIELD\" element defines an RF cavity ")+
41  std::string("with time dependent frequency, phase and amplitude.");
42 
44  OpalElement(SIZE, "VARIABLE_RF_CAVITY_FRINGE_FIELD", doc_string.c_str()) {
46  "The name of the phase time dependence model, which should give the phase in [rad].");
47  itsAttr[AMPLITUDE_MODEL] = Attributes::makeString("AMPLITUDE_MODEL",
48  "The name of the amplitude time dependence model, which should give the field in [MV/m]");
49  itsAttr[FREQUENCY_MODEL] = Attributes::makeString("FREQUENCY_MODEL",
50  "The name of the frequency time dependence model, which should give the field in [MHz].");
52  "Full width of the cavity [m].");
54  "Full height of the cavity [m].");
55  itsAttr[CENTRE_LENGTH] = Attributes::makeReal("CENTRE_LENGTH",
56  "Length of the cavity field flat top [m].");
57  itsAttr[END_LENGTH] = Attributes::makeReal("END_LENGTH",
58  "Length of the cavity fringe fields [m].");
59  itsAttr[CAVITY_CENTRE] = Attributes::makeReal("CAVITY_CENTRE",
60  "Offset of the cavity centre from the beginning of the cavity [m].");
61  itsAttr[MAX_ORDER] = Attributes::makeReal("MAX_ORDER",
62  "Maximum power of y that will be evaluated in field calculations.");
63 
64  registerStringAttribute("PHASE_MODEL");
65  registerStringAttribute("AMPLITUDE_MODEL");
66  registerStringAttribute("FREQUENCY_MODEL");
67  registerRealAttribute("WIDTH");
68  registerRealAttribute("HEIGHT");
69  registerRealAttribute("CENTRE_LENGTH");
70  registerRealAttribute("END_LENGTH");
71  registerRealAttribute("CAVITY_CENTRE");
72  registerRealAttribute("MAX_ORDER");
73 
75 
76  setElement((new VariableRFCavityFringeField("VARIABLE_RF_CAVITY_FRINGE_FIELD"))->
77  makeAlignWrapper());
78 }
79 
81  const std::string &name,
83  ) : OpalElement(name, parent) {
84  VariableRFCavityFringeField *cavity = dynamic_cast
86  setElement((new VariableRFCavityFringeField(*cavity))->makeAlignWrapper());
87 }
88 
90 }
91 
93  const std::string &name) {
94  return new OpalVariableRFCavityFringeField(name, this);
95 }
96 
98  return new OpalVariableRFCavityFringeField(this->getOpalName(), this);
99 }
100 
104  const VariableRFCavityFringeField* cavity =
105  dynamic_cast<const VariableRFCavityFringeField*>(&base);
106  if (cavity == NULL) {
107  throw OpalException("OpalVariableRFCavityFringeField::fillRegisteredAttributes",
108  "Failed to cast ElementBase to a VariableRFCavityFringeField");
109  }
110  std::shared_ptr<endfieldmodel::EndFieldModel> model = cavity->getEndField();
111  endfieldmodel::Tanh* tanh = dynamic_cast<endfieldmodel::Tanh*>(model.get());
112  if (tanh == NULL) {
113  throw OpalException("OpalVariableRFCavityFringeField::fillRegisteredAttributes",
114  "Failed to cast EndField to a Tanh model");
115  }
116 
117 
118  attributeRegistry["L"]->setReal(cavity->getLength());
119  std::shared_ptr<AbstractTimeDependence> phase_model = cavity->getPhaseModel();
120  std::shared_ptr<AbstractTimeDependence> freq_model = cavity->getFrequencyModel();
121  std::shared_ptr<AbstractTimeDependence> amp_model = cavity->getAmplitudeModel();
122  std::string phase_name = AbstractTimeDependence::getName(phase_model);
123  std::string amp_name = AbstractTimeDependence::getName(amp_model);
124  std::string freq_name = AbstractTimeDependence::getName(freq_model);
125  attributeRegistry["PHASE_MODEL"]->setString(phase_name);
126  attributeRegistry["AMPLITUDE_MODEL"]->setString(amp_name);
127  attributeRegistry["FREQUENCY_MODEL"]->setString(freq_name);
128  attributeRegistry["WIDTH"]->setReal(cavity->getWidth());
129  attributeRegistry["HEIGHT"]->setReal(cavity->getHeight());
130  // flat top length is 2*x0
131  attributeRegistry["CENTRE_LENGTH"]->setReal(tanh->getX0()/2.);
132  attributeRegistry["END_LENGTH"]->setReal(tanh->getLambda());
133  attributeRegistry["CAVITY_CENTRE"]->setReal(cavity->getCavityCentre());
134  attributeRegistry["MAX_ORDER"]->setReal(cavity->getMaxOrder());
135 }
136 
137 
138 
141 
142  VariableRFCavityFringeField *cavity = dynamic_cast
144  double length = Attributes::getReal(itsAttr[LENGTH]);
145  cavity->setLength(length);
146  std::string phaseName = Attributes::getString(itsAttr[PHASE_MODEL]);
147  cavity->setPhaseName(phaseName);
148  std::string ampName = Attributes::getString(itsAttr[AMPLITUDE_MODEL]);
149  cavity->setAmplitudeName(ampName);
150  std::string freqName = Attributes::getString(itsAttr[FREQUENCY_MODEL]);
151  cavity->setFrequencyName(freqName);
152  double width = Attributes::getReal(itsAttr[WIDTH]);
153  cavity->setWidth(width);
154  double height = Attributes::getReal(itsAttr[HEIGHT]);
155  cavity->setHeight(height);
156  double maxOrderReal = Attributes::getReal(itsAttr[MAX_ORDER]);
157  size_t maxOrder = convertToUnsigned(maxOrderReal, "MAX_ORDER");
158  cavity->setMaxOrder(maxOrder);
159  double cavity_centre = Attributes::getReal(itsAttr[CAVITY_CENTRE]);
160  cavity->setCavityCentre(cavity_centre); // mm
161  // convert to mm; x0 is double length of flat top so divide 2
162  double centreLength = Attributes::getReal(itsAttr[CENTRE_LENGTH])*1e3;
163  double endLength = Attributes::getReal(itsAttr[END_LENGTH])*1e3;
164  endfieldmodel::Tanh* tanh = new endfieldmodel::Tanh(centreLength/2.,
165  endLength,
166  maxOrder+1);
167  std::shared_ptr<endfieldmodel::EndFieldModel> end(tanh);
168  cavity->setEndField(end);
169 
170  setElement(cavity->makeAlignWrapper());
171 }
172 
173 
175  std::string name) {
176  value += unsignedTolerance; // prevent rounding error
177  if (fabs(floor(value) - value) > 2*unsignedTolerance) {
178  throw OpalException("OpalVariableRFCavityFringeField::convertToUnsigned",
179  "Value for "+name+
180  " should be an unsigned int but a real value was found");
181  }
182  if (floor(value) < -0.5) {
183  throw OpalException("OpalVariableRFCavityFringeField::convertToUnsigned",
184  "Value for "+name+" should be 0 or more");
185  }
186  size_t ret(floor(value));
187  return ret;
188 }
virtual void setFrequencyName(std::string frequency)
virtual std::shared_ptr< endfieldmodel::EndFieldModel > getEndField() const
virtual std::shared_ptr< AbstractTimeDependence > getFrequencyModel() const
static AttCell * registerStringAttribute(const std::string &name)
Register a ``string&#39;&#39; element attribute.
OpalVariableRFCavityFringeField * clone()
virtual void setCavityCentre(double zCentre)
Interface for basic beam line object.
Definition: ElementBase.h:128
virtual void fillRegisteredAttributes(const ElementBase &, ValueFlag)
Fill in all registered attributes.
virtual void setMaxOrder(size_t maxOrder)
ValueFlag
Switch for value desired on ATTLIST command.
Definition: OpalElement.h:71
virtual std::shared_ptr< AbstractTimeDependence > getAmplitudeModel() const
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:815
The base class for all OPAL exceptions.
Definition: OpalException.h:28
virtual ElementBase * removeWrappers()
Return the design element.
Inform * gmsg
Definition: Main.cpp:21
virtual void setLength(double length)
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
static std::map< std::string, OwnPtr< AttCell > > attributeRegistry
The registry for named attributes.
Definition: OpalElement.h:172
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
virtual void setWidth(double fullWidth)
static size_t convertToUnsigned(double value, std::string name)
virtual double getHeight() const
virtual void fillRegisteredAttributes(const ElementBase &, ValueFlag)
virtual void setAmplitudeName(std::string amplitude)
Base class for all beam line elements.
Definition: OpalElement.h:41
virtual double getLength() const
void setElement(ElementBase *)
Assign new CLASSIC element.
Definition: Element.h:133
virtual void setEndField(std::shared_ptr< endfieldmodel::EndFieldModel > endField)
virtual std::shared_ptr< AbstractTimeDependence > getPhaseModel() const
void registerOwnership() const
virtual void update()
Update the embedded CLASSIC element.
static std::string getName(std::shared_ptr< AbstractTimeDependence > time_dep)
virtual void setPhaseName(std::string phase)
ElementBase * getElement() const
Return the embedded CLASSIC element.
Definition: Element.h:128
virtual double getWidth() const
const std::string name
virtual ElementBase * makeAlignWrapper()
Allow misalignment.
virtual void setHeight(double fullHeight)
double getX0() const
Definition: Tanh.h:95
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
double getLambda() const
Definition: Tanh.h:92
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Tps< T > tanh(const Tps< T > &x)
Hyperbolic tangent.
Definition: TpsMath.h:240
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
static AttCell * registerRealAttribute(const std::string &name)
Register a ``real&#39;&#39; element attribute.
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307