OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Beam.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Beam.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.3.4.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Beam
10 // The class for the OPAL BEAM command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2003/08/11 22:09:00 $
15 // $Author: dbruhwil $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Structure/Beam.h"
22 #include "Attributes/Attributes.h"
23 #include "Expressions/SAutomatic.h"
24 #include "Expressions/SRefExpr.h"
25 #include "Physics/Physics.h"
27 
28 #include <cmath>
29 #include <iterator>
30 
31 using namespace Expressions;
32 
33 // Class Beam
34 // ------------------------------------------------------------------------
35 
36 // The attributes of class Beam.
37 namespace {
38  enum {
39  // DESCRIPTION OF SINGLE PARTICLE:
40  PARTICLE, // The particle name
41  MASS, // The particle rest mass in GeV
42  CHARGE, // The particle charge in proton charges
43  ENERGY, // The particle energy in GeV
44  PC, // The particle momentum in GeV/c
45  GAMMA, // ENERGY / MASS
46 
47  // BEAM CURRENT AND EMITTANCES:
48  BCURRENT, // Beam current in A
49  EX, // Horizontal emittance
50  EY, // Vertical emittance
51  ET, // Longitudinal emittance
52 
53  // BEAM FREQUENCY
54  BFREQ, // Beam frequency in MHz
55 
56  // DESCRIPTION OF BUNCHES:
57  NPART, // Number of particles per bunch
58  NSLICE, // Number of slices per bunch
59  SIZE
60  };
61 }
62 
63 
64 const double Beam::energy_scale = 1.0e9;
65 
66 
68  Definition(SIZE, "BEAM",
69  "The \"BEAM\" statement defines data for the particles "
70  "in a beam."),
71  reference(1.0, Physics::m_p *energy_scale, 1.0 * energy_scale) {
72 
73  // DESCRIPTION OF SINGLE PARTICLE:
75  ("PARTICLE", "Name of particle to be used");
77  ("MASS", "Particle rest mass in GeV");
79  ("CHARGE", "Particle charge in proton charges");
81  ("ENERGY", "Particle energy in GeV");
83  ("PC", "Particle momentum in GeV/c");
84  PtrToScalar<double> expr = new SRefExpr<double>("P0", "");
85  itsAttr[PC].set(new SAutomatic<double>(expr));
87  ("GAMMA", "ENERGY / MASS");
88 
89  // BEAM CURRENT AND EMITTANCES:
90  itsAttr[BCURRENT] = Attributes::makeReal
91  ("BCURRENT", "Beam current in A (all bunches)");
93  ("EX", "Horizontal emittance");
95  ("EY", "Vertical emittance");
97  ("ET", "Longitudinal emittance");
98 
99  // BEAM FREQUENCY
101  ("BFREQ", "Beam frequency in MHz (all bunches)");
102 
103  // DESCRIPTION OF BUNCHES:
105  ("NPART", "Number of particles in bunch");
106  itsAttr[NSLICE] = Attributes::makeReal
107  ("NSLICE", "Number of slices in bunch");
108 
109  // Set up default beam.
110  Beam *defBeam = clone("UNNAMED_BEAM");
111  defBeam->builtin = true;
112 
113  try {
114  defBeam->update();
115  OpalData::getInstance()->define(defBeam);
116  } catch(...) {
117  delete defBeam;
118  }
119 
121 }
122 
123 
124 Beam::Beam(const std::string &name, Beam *parent):
125  Definition(name, parent),
126  reference(parent->reference)
127 {}
128 
129 
131 {}
132 
133 
134 bool Beam::canReplaceBy(Object *object) {
135  // Can replace only by another BEAM.
136  return dynamic_cast<Beam *>(object) != 0;
137 }
138 
139 
140 Beam *Beam::clone(const std::string &name) {
141  return new Beam(name, this);
142 }
143 
144 
146  update();
147 }
148 
149 
150 Beam *Beam::find(const std::string &name) {
151  Beam *beam = dynamic_cast<Beam *>(OpalData::getInstance()->find(name));
152 
153  if(beam == 0) {
154  throw OpalException("Beam::find()", "Beam \"" + name + "\" not found.");
155  }
156 
157  return beam;
158 }
159 
161  return (size_t)Attributes::getReal(itsAttr[NPART]);
162 }
163 
165  return (size_t)Attributes::getReal(itsAttr[NSLICE]);
166 }
167 
168 double Beam::getEX() const {
169  return Attributes::getReal(itsAttr[EX]);
170 }
171 
172 
173 double Beam::getEY() const {
174  return Attributes::getReal(itsAttr[EY]);
175 }
176 
177 
178 double Beam::getET() const {
179  return Attributes::getReal(itsAttr[ET]);
180 }
181 
182 
183 const PartData &Beam::getReference() const {
184  // Cast away const, to allow logically constant Beam to update.
185  const_cast<Beam *>(this)->update();
186  return reference;
187 }
188 
189 double Beam::getCurrent() const {
190  return Attributes::getReal(itsAttr[BCURRENT]);
191 }
192 
193 double Beam::getCharge() const {
194  return Attributes::getReal(itsAttr[CHARGE]);
195 }
196 
197 double Beam::getMass() const {
198  return Attributes::getReal(itsAttr[MASS]);
199 }
200 
201 std::string Beam::getParticleName() const {
202  return Attributes::getString(itsAttr[PARTICLE]);
203 }
204 
205 double Beam::getFrequency() const {
206  return Attributes::getReal(itsAttr[BFREQ]);
207 }
208 
209 void Beam::setEX(double value) {
210  Attributes::setReal(itsAttr[EX], value);
211 }
212 
213 
214 void Beam::setEY(double value) {
215  Attributes::setReal(itsAttr[EY], value);
216 }
217 
218 
219 void Beam::setET(double value) {
220  Attributes::setReal(itsAttr[ET], value);
221 }
222 
223 
224 void Beam::update() {
225  // Find the particle name.
226  if(itsAttr[PARTICLE]) {
227  static const char *names[] = {
228  "ELECTRON", "PROTON", "POSITRON", "ANTIPROTON", "CARBON", "HMINUS", "URANIUM", "MUON", "DEUTERON", "XENON", "H2P"
229  };
230 
231  static const double masses[] = {
232  Physics::m_e,
233  Physics::m_p,
234  Physics::m_e,
235  Physics::m_p,
236  Physics::m_c,
238  Physics::m_u,
240  Physics::m_d,
243  };
244 
245  static const double charges[] = {
246  -1.0, 1.0, 1.0, -1.0, 12.0, -1.0, 35.0, -1.0, 1.0, 20.0, 1.0
247  };
248  const unsigned int numParticleNames = std::end(names) - std::begin(names);
249 
250  std::string pName = Attributes::getString(itsAttr[PARTICLE]);
251  for(unsigned int i = 0; i < numParticleNames; ++ i) {
252  if(pName == names[i]) {
253  Attributes::setReal(itsAttr[MASS], masses[i]);
254  Attributes::setReal(itsAttr[CHARGE], charges[i]);
255  break;
256  }
257  }
258  }
259 
260  // Set up particle reference; convert all to eV for CLASSIC.
261  double mass =
263  double charge = itsAttr[CHARGE] ? Attributes::getReal(itsAttr[CHARGE]) : 1.0;
264  reference = PartData(charge, mass, 1.0);
265 
266  if(itsAttr[GAMMA]) {
267  double gamma = Attributes::getReal(itsAttr[GAMMA]);
268  if(gamma > 1.0) {
269  reference.setGamma(gamma);
270  } else {
271  throw OpalException("Beam::update()",
272  "\"GAMMA\" should be greater than 1.");
273  }
274  } else if(itsAttr[ENERGY]) {
275  double energy = Attributes::getReal(itsAttr[ENERGY]) * energy_scale;
276  if(energy > reference.getM()) {
277  reference.setE(energy);
278  } else {
279  throw OpalException("Beam::update()",
280  "\"ENERGY\" should be greater than \"MASS\".");
281  }
282  } else if(itsAttr[PC]) {
283  double pc = Attributes::getReal(itsAttr[PC]) * energy_scale;
284  if(pc > 0.0) {
285  reference.setP(pc);
286  } else {
287  throw OpalException("Beam::update()",
288  "\"PC\" should be greater than 0.");
289  }
290  };
291 
292  // Set default name.
293  if(getOpalName().empty()) setOpalName("UNNAMED_BEAM");
294 }
295 
296 
297 //ff
298 double Beam::getGamma() const { //obtain value for gamma
299  return Attributes::getReal(itsAttr[GAMMA]);
300 }
301 
302 //ff
303 double Beam::getPC() const { //obtain value for PC
304  return Attributes::getReal(itsAttr[PC]);
305 }
306 
307 
308 void Beam::print(std::ostream &os) const {
309  double charge = Attributes::getReal(itsAttr[CHARGE]);
310  os << "* ************* B E A M ************************************************************ " << std::endl;
311  os << "* BEAM " << getOpalName() << '\n'
312  << "* PARTICLE " << Attributes::getString(itsAttr[PARTICLE]) << '\n'
313  << "* CURRENT " << Attributes::getReal(itsAttr[BCURRENT]) << " A\n"
314  << "* FREQUENCY " << Attributes::getReal(itsAttr[BFREQ]) << " MHz\n"
315  << "* CHARGE " << (charge > 0 ? '+' : '-') << "e * " << std::abs(charge) << " \n"
316  << "* REST MASS " << Attributes::getReal(itsAttr[MASS]) << " GeV\n"
317  << "* MOMENTUM " << Attributes::getReal(itsAttr[PC]) << '\n'
318  << "* NPART " << Attributes::getReal(itsAttr[NPART]) << '\n';
319  os << "* ********************************************************************************** " << std::endl;
320 }
An expression defined as a reference to a scalar.
Definition: SRefExpr.h:41
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:236
void setEX(double)
Store emittance for mode 1.
Definition: Beam.cpp:209
virtual bool canReplaceBy(Object *object)
Test if replacement is allowed.
Definition: Beam.cpp:134
void setET(double)
Store emittance for mode 3.
Definition: Beam.cpp:219
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::string getParticleName() const
Return Particle&#39;s name.
Definition: Beam.cpp:201
void define(Object *newObject)
Define a new object.
Definition: OpalData.cpp:538
The base class for all OPAL definitions.
Definition: Definition.h:30
constexpr double m_hm
The H- rest mass in GeV.
Definition: Physics.h:115
double getPC() const
Definition: Beam.cpp:303
double getMass() const
Return Particle&#39;s rest mass in GeV.
Definition: Beam.cpp:197
size_t getNumberOfSlices()
Return the number of slices.
Definition: Beam.cpp:164
constexpr double m_d
The deuteron rest mass in GeV.
Definition: Physics.h:124
The base class for all OPAL exceptions.
Definition: OpalException.h:28
constexpr double m_u
The uranium rest mass in GeV.
Definition: Physics.h:118
double getCurrent() const
Return the beam current in A.
Definition: Beam.cpp:189
virtual void execute()
Check the BEAM data.
Definition: Beam.cpp:145
Particle reference data.
Definition: PartData.h:38
constexpr double m_p
The proton rest mass in GeV.
Definition: Physics.h:97
double getET() const
Return emittance for mode 3.
Definition: Beam.cpp:178
constexpr double m_mu
The muon rest mass in GeV.
Definition: Physics.h:121
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
double getEY() const
Return emittance for mode 2.
Definition: Beam.cpp:173
virtual void update()
Update the BEAM data.
Definition: Beam.cpp:224
static const double energy_scale
Definition: Beam.h:123
constexpr double m_e
The electron rest mass in GeV.
Definition: Physics.h:85
size_t getNumberOfParticles()
Return the number of (macro)particles.
Definition: Beam.cpp:160
static OpalData * getInstance()
Definition: OpalData.cpp:209
virtual ~Beam()
Definition: Beam.cpp:130
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
double getCharge() const
Return the charge number in elementary charge.
Definition: Beam.cpp:193
double getGamma() const
Definition: Beam.cpp:298
void setP(double p)
Set reference momentum.
Definition: PartData.cpp:44
Object attribute with an ``automatic&#39;&#39; scalar value.
Definition: SAutomatic.h:38
void print(std::ostream &os) const
Print the object.
Definition: Beam.cpp:308
static Beam * find(const std::string &name)
Find named BEAM.
Definition: Beam.cpp:150
double getFrequency() const
Return the beam frequency in MHz.
Definition: Beam.cpp:205
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
void setE(double E)
Set reference energy.
Definition: PartData.cpp:61
double getEX() const
Return emittance for mode 1.
Definition: Beam.cpp:168
constexpr double m_h2p
The H2+ rest mass in GeV.
Definition: Physics.h:133
virtual Beam * clone(const std::string &name)
Make clone.
Definition: Beam.cpp:140
const PartData & getReference() const
Return the embedded CLASSIC PartData.
Definition: Beam.cpp:183
double getM() const
The constant mass per particle.
Definition: PartData.h:112
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
The base class for all OPAL objects.
Definition: Object.h:48
The BEAM definition.
Definition: Beam.h:35
const std::string name
void setGamma(double gamma)
Set gamma.
Definition: PartData.cpp:83
void setOpalName(const std::string &name)
Set object name.
Definition: Object.cpp:305
PartData reference
Definition: Beam.h:120
constexpr double m_xe
The xenon rest mass in GeV.
Definition: Physics.h:127
bool builtin
Built-in flag.
Definition: Object.h:231
void setEY(double)
Store emittance for mode 2.
Definition: Beam.cpp:214
constexpr double m_c
The carbon rest mass in GeV.
Definition: Physics.h:112
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
Beam()
Exemplar constructor.
Definition: Beam.cpp:67
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307