BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
beam.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "beam.h"
20
21#include <algorithm>
22#include <iostream>
23#include <sstream>
24
25using namespace GMAD;
26
27Beam::Beam():
28 BeamBase()
29{
30 PublishMembers();
31}
32
33Beam::Beam(const GMAD::BeamBase& options):
34 BeamBase(options)
35{
36 PublishMembers();
37}
38
39double Beam::get_value(std::string property_name) const
40{
41 double value;
42 try
43 {value = get<double>(this,property_name);}
44 catch (const std::runtime_error&)
45 {
46 try
47 {value = (double)get<int>(this,property_name);} // try int and convert
48 catch (const std::runtime_error&)
49 {
50 try
51 {value = (double)get<long>(this,property_name);} // try long and convert
52 catch (const std::runtime_error&)
53 {
54 std::cerr << "beam.cc> Error: unknown property \"" << property_name
55 << "\" (only works on numerical properties)" << std::endl;
56 exit(1);
57 }
58 }
59 }
60 return value;
61}
62
63std::string Beam::get_value_string(std::string property_name) const
64{
65 try {
66 double value = get<double>(this, property_name);
67 std::ostringstream strs;
68 strs << value;
69 return strs.str();
70 }
71 catch (...) {
72 try {
73 int value = get<int>(this, property_name);
74 std::ostringstream strs;
75 strs << value;
76 return strs.str();
77 }
78 catch (...) {
79 try {
80 std::string value = get<std::string>(this, property_name);
81 return value;
82 }
83 catch (...) {
84 try {
85 bool value = get<bool>(this, property_name);
86 std::ostringstream strs;
87 strs << std::boolalpha << value;
88 return strs.str();
89 }
90 catch (...)
91 {std::cerr << "Error " << property_name << std::endl; exit(1);}
92 }
93 }
94 }
95}
96
97void Beam::Amalgamate(const Beam& beamIn, bool override, int startFromEvent)
98{
99 if (override)
100 {
101 for (auto const& key : beamIn.setKeys)
102 {
103 try
104 {
105 set(this, &beamIn, key);
106 setKeys.push_back(key);
107 }
108 catch (const std::runtime_error&)
109 {
110 std::cerr << "Error: Amalgamate unknown beam parameter \"" << key << "\"" << std::endl;
111 exit(1);
112 }
113 }
114 // if we're recreating from a file, still load external file but
115 // advance to the event number
116 nlinesIgnore += startFromEvent;
117 }
118 else
119 {// don't override - ie give preference to ones set in this instance
120 for (auto const& key : beamIn.setKeys)
121 {
122 auto const& ok = setKeys; // shortcut
123 auto result = std::find(ok.begin(), ok.end(), key);
124 if (result == ok.end())
125 {//it wasn't found so ok to copy
126 try
127 {
128 set(this, &beamIn, key);
129 setKeys.push_back(key);
130 }
131 catch (const std::runtime_error&)
132 {
133 std::cerr << "Error: Amalgamate unknown beam parameter \"" << key << "\""
134 << std::endl;
135 exit(1);
136 }
137 }
138 }
139 }
140}
141
142bool Beam::HasBeenSet(const std::string& name) const
143{
144 auto result = std::find(setKeys.begin(), setKeys.end(), name);
145 if (result == setKeys.end())
146 {return false;}
147 else
148 {return true;}
149}
150
152{
153 publish("particle", &Beam::particle);
154 publish("particleName", &Beam::particle);
155 publish("beamParticle", &Beam::beamParticleName);
156 publish("beamParticleName", &Beam::beamParticleName);
157 publish("energy", &Beam::beamEnergy);
158 publish("kineticEnergy", &Beam::beamKineticEnergy);
159 publish("momentum", &Beam::beamMomentum);
160 publish("distrType", &Beam::distrType);
161 publish("xDistrType", &Beam::xDistrType);
162 publish("yDistrType", &Beam::yDistrType);
163 publish("zDistrType", &Beam::zDistrType);
164 publish("spaceDistrType", &Beam::spaceDistrType);
165 publish("directionDistrType", &Beam::directionDistrType);
166 publish("energyDistrType", &Beam::energyDistrType);
167 publish("distrFile", &Beam::distrFile);
168 publish("distrFileFormat", &Beam::distrFileFormat);
169 publish("distrFileFromExecOptions", &Beam::distrFileFromExecOptions);
170 publish("matchDistrFileLength", &Beam::matchDistrFileLength);
171 publish("removeUnstableWithoutDecay", &Beam::removeUnstableWithoutDecay);
172 publish("nlinesIgnore", &Beam::nlinesIgnore);
173 publish("nLinesIgnore", &Beam::nlinesIgnore); // for consistency
174 publish("nlinesSkip", &Beam::nlinesSkip);
175 publish("nLinesSkip", &Beam::nlinesSkip); // for consistency
176
177 // aliases
178 publish("distribution", &Beam::distrType);
179 publish("xDistribution", &Beam::xDistrType);
180 publish("yDistribution", &Beam::yDistrType);
181 publish("zDistribution", &Beam::zDistrType);
182 publish("spaceDistribution", &Beam::spaceDistrType);
183 publish("directionDistribution",&Beam::directionDistrType);
184 publish("energyDistribution", &Beam::energyDistrType);
185
186 // central values
187 publish("X0", &Beam::X0);
188 publish("Y0", &Beam::Y0);
189 publish("Z0", &Beam::Z0);
190 publish("S0", &Beam::S0);
191 publish("Xp0", &Beam::Xp0);
192 publish("Yp0", &Beam::Yp0);
193 publish("Zp0", &Beam::Zp0);
194 publish("tilt", &Beam::tilt);
195 publish("T0", &Beam::T0);
196 publish("E0", &Beam::E0);
197 publish("Ek0", &Beam::Ek0);
198 publish("P0", &Beam::P0);
199
200 publish("sigmaT", &Beam::sigmaT);
201 publish("sigmaE", &Beam::sigmaE);
202 publish("sigmaEk", &Beam::sigmaEk);
203 publish("sigmaP", &Beam::sigmaP);
204
205 // for gausstwiss
206 publish("betx", &Beam::betx);
207 publish("bety", &Beam::bety);
208 publish("alfx", &Beam::alfx);
209 publish("alfy", &Beam::alfy);
210 publish("emitx", &Beam::emitx);
211 publish("emity", &Beam::emity);
212 publish("emitnx",&Beam::emitNX);
213 publish("emitny",&Beam::emitNY);
214 publish("dispx", &Beam::dispx);
215 publish("dispy", &Beam::dispy);
216 publish("dispxp",&Beam::dispxp);
217 publish("dispyp",&Beam::dispyp);
218
219 // aliases
220 publish("betaX", &Beam::betx);
221 publish("betaY", &Beam::bety);
222 publish("alphaX", &Beam::alfx);
223 publish("alphaY", &Beam::alfy);
224 publish("emitX", &Beam::emitx);
225 publish("emitY", &Beam::emity);
226 publish("dispX", &Beam::dispx);
227 publish("dispXp", &Beam::dispxp);
228 publish("dispY", &Beam::dispy);
229 publish("dispYp", &Beam::dispyp);
230
231 // options for beam distrType="gauss"
232 publish("sigmaX", &Beam::sigmaX);
233 publish("sigmaXp",&Beam::sigmaXp);
234 publish("sigmaY", &Beam::sigmaY);
235 publish("sigmaYp",&Beam::sigmaYp);
236
237 // options for beam distrType="square" or distrType="circle"
238 publish("envelopeX", &Beam::envelopeX);
239 publish("envelopeXp",&Beam::envelopeXp);
240 publish("envelopeY", &Beam::envelopeY);
241 publish("envelopeYp",&Beam::envelopeYp);
242 publish("envelopeZ", &Beam::envelopeZ);
243 publish("envelopeZp",&Beam::envelopeZp);
244 publish("envelopeT", &Beam::envelopeT);
245 publish("envelopeE", &Beam::envelopeE);
246 publish("envelopeR", &Beam::envelopeR);
247 publish("envelopeRp",&Beam::envelopeRp);
248
249 // options for beam distrType="gaussmatrix"
250 publish("sigma11",&Beam::sigma11);
251 publish("sigma12",&Beam::sigma12);
252 publish("sigma13",&Beam::sigma13);
253 publish("sigma14",&Beam::sigma14);
254 publish("sigma15",&Beam::sigma15);
255 publish("sigma16",&Beam::sigma16);
256 publish("sigma22",&Beam::sigma22);
257 publish("sigma23",&Beam::sigma23);
258 publish("sigma24",&Beam::sigma24);
259 publish("sigma25",&Beam::sigma25);
260 publish("sigma26",&Beam::sigma26);
261 publish("sigma33",&Beam::sigma33);
262 publish("sigma34",&Beam::sigma34);
263 publish("sigma35",&Beam::sigma35);
264 publish("sigma36",&Beam::sigma36);
265 publish("sigma44",&Beam::sigma44);
266 publish("sigma45",&Beam::sigma45);
267 publish("sigma46",&Beam::sigma46);
268 publish("sigma55",&Beam::sigma55);
269 publish("sigma56",&Beam::sigma56);
270 publish("sigma66",&Beam::sigma66);
271
272 // options for beam distrType="eshell"
273 publish("shellX", &Beam::shellX);
274 publish("shellXp", &Beam::shellXp);
275 publish("shellY", &Beam::shellY);
276 publish("shellYp", &Beam::shellYp);
277 publish("shellXWidth", &Beam::shellXWidth);
278 publish("shellXpWidth",&Beam::shellXpWidth);
279 publish("shellYWidth", &Beam::shellYWidth);
280 publish("shellYpWidth",&Beam::shellYpWidth);
281
282 // options for beam distrType="ring"
283 publish("Rmin",&Beam::Rmin);
284 publish("Rmax",&Beam::Rmax);
285
286 // options for beam distrType="halo"
287 publish("haloNSigmaXInner", &Beam::haloNSigmaXInner);
288 publish("haloNSigmaXOuter", &Beam::haloNSigmaXOuter);
289 publish("haloNSigmaYInner", &Beam::haloNSigmaYInner);
290 publish("haloNSigmaYOuter", &Beam::haloNSigmaYOuter);
291 publish("haloXCutInner", &Beam::haloXCutInner);
292 publish("haloYCutInner", &Beam::haloYCutInner);
293 publish("haloXCutOuter", &Beam::haloXCutOuter);
294 publish("haloYCutOuter", &Beam::haloYCutOuter);
295 publish("haloXpCutInner", &Beam::haloXpCutInner);
296 publish("haloYpCutInner", &Beam::haloYpCutInner);
297 publish("haloXpCutOuter", &Beam::haloXpCutOuter);
298 publish("haloYpCutOuter", &Beam::haloYpCutOuter);
299 publish("haloPSWeightParameter", &Beam::haloPSWeightParameter);
300 publish("haloPSWeightFunction", &Beam::haloPSWeightFunction);
301
302 publish("offsetSampleMean", &Beam::offsetSampleMean);
303
304 publish("eventGeneratorMinX", &Beam::eventGeneratorMinX);
305 publish("eventGeneratorMaxX", &Beam::eventGeneratorMaxX);
306 publish("eventGeneratorMinY", &Beam::eventGeneratorMinY);
307 publish("eventGeneratorMaxY", &Beam::eventGeneratorMaxY);
308 publish("eventGeneratorMinZ", &Beam::eventGeneratorMinZ);
309 publish("eventGeneratorMaxZ", &Beam::eventGeneratorMaxZ);
310 publish("eventGeneratorMinXp", &Beam::eventGeneratorMinXp);
311 publish("eventGeneratorMaxXp", &Beam::eventGeneratorMaxXp);
312 publish("eventGeneratorMinYp", &Beam::eventGeneratorMinYp);
313 publish("eventGeneratorMaxYp", &Beam::eventGeneratorMaxYp);
314 publish("eventGeneratorMinZp", &Beam::eventGeneratorMinZp);
315 publish("eventGeneratorMaxZp", &Beam::eventGeneratorMaxZp);
316 publish("eventGeneratorMinRp", &Beam::eventGeneratorMinRp);
317 publish("eventGeneratorMaxRp", &Beam::eventGeneratorMaxRp);
318 publish("eventGeneratorMinT", &Beam::eventGeneratorMinT);
319 publish("eventGeneratorMaxT", &Beam::eventGeneratorMinT);
320 publish("eventGeneratorMinEK", &Beam::eventGeneratorMinEK);
321 publish("eventGeneratorMaxEK", &Beam::eventGeneratorMaxEK);
322 publish("eventGeneratorParticles", &Beam::eventGeneratorParticles);
323 publish("eventGeneratorWarnSkippedParticles", &Beam::eventGeneratorWarnSkippedParticles);
324}
Options for a beam distribution.
Definition: beamBase.h:35
double dispy
initial twiss parameters
Definition: beamBase.h:82
bool matchDistrFileLength
beam parameters
Definition: beamBase.h:55
double alfy
initial twiss parameters
Definition: beamBase.h:82
std::string particle
beam parameters
Definition: beamBase.h:40
double eventGeneratorMaxYp
Event generator file filter.
Definition: beamBase.h:143
double sigma45
for the gaussian sigma matrix distribution
Definition: beamBase.h:100
double eventGeneratorMaxXp
Event generator file filter.
Definition: beamBase.h:141
double sigmaE
for the gaussian, elliptic shell, ring distributions
Definition: beamBase.h:77
double emity
initial twiss parameters
Definition: beamBase.h:82
double haloXCutOuter
for the halo distribution
Definition: beamBase.h:121
double sigma16
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double haloNSigmaYInner
for the halo distribution
Definition: beamBase.h:117
double beamKineticEnergy
beam parameters
Definition: beamBase.h:43
double envelopeRp
for the circle/square/box beam distribution
Definition: beamBase.h:93
double eventGeneratorMaxEK
Event generator file filter.
Definition: beamBase.h:151
double haloXpCutOuter
for the halo distribution
Definition: beamBase.h:125
double sigmaX
for the gaussian beam distribution
Definition: beamBase.h:87
std::string distrFileFormat
beam parameters
Definition: beamBase.h:53
double beamEnergy
beam parameters
Definition: beamBase.h:42
double envelopeZp
for the circle/square/box beam distribution
Definition: beamBase.h:91
bool eventGeneratorWarnSkippedParticles
Event generator file filter.
Definition: beamBase.h:153
double shellXp
for the elliptic shell distribution
Definition: beamBase.h:106
double sigma66
for the gaussian sigma matrix distribution
Definition: beamBase.h:102
double haloXCutInner
for the halo distribution
Definition: beamBase.h:119
double envelopeZ
for the circle/square/box beam distribution
Definition: beamBase.h:91
double sigma35
for the gaussian sigma matrix distribution
Definition: beamBase.h:99
double sigma44
for the gaussian sigma matrix distribution
Definition: beamBase.h:100
double haloYpCutInner
for the halo distribution
Definition: beamBase.h:124
double emitNY
initial twiss parameters
Definition: beamBase.h:83
double envelopeE
for the circle/square/box beam distribution
Definition: beamBase.h:92
double eventGeneratorMaxZp
Event generator file filter.
Definition: beamBase.h:145
double sigmaXp
for the gaussian beam distribution
Definition: beamBase.h:87
double eventGeneratorMinYp
Event generator file filter.
Definition: beamBase.h:142
double sigma13
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double sigma34
for the gaussian sigma matrix distribution
Definition: beamBase.h:99
double betx
initial twiss parameters
Definition: beamBase.h:82
std::string distrFile
beam parameters
Definition: beamBase.h:52
double eventGeneratorMinEK
Event generator file filter.
Definition: beamBase.h:150
std::string directionDistrType
beam parameters
Definition: beamBase.h:50
double haloYCutOuter
for the halo distribution
Definition: beamBase.h:122
double sigmaYp
for the gaussian beam distribution
Definition: beamBase.h:87
double haloNSigmaXInner
for the halo distribution
Definition: beamBase.h:115
bool distrFileFromExecOptions
Required to know how to build the absolute path properly.
Definition: beamBase.h:54
double dispxp
initial twiss parameters
Definition: beamBase.h:82
double envelopeYp
for the circle/square/box beam distribution
Definition: beamBase.h:91
double eventGeneratorMaxZ
Event generator file filter.
Definition: beamBase.h:139
double eventGeneratorMinY
Event generator file filter.
Definition: beamBase.h:136
double sigma12
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double Ek0
initial beam centroid
Definition: beamBase.h:66
std::string spaceDistrType
beam parameters
Definition: beamBase.h:49
double haloNSigmaYOuter
for the halo distribution
Definition: beamBase.h:118
std::string beamParticleName
beam parameters
Definition: beamBase.h:41
bool removeUnstableWithoutDecay
beam parameters
Definition: beamBase.h:56
double sigma26
for the gaussian sigma matrix distribution
Definition: beamBase.h:98
double haloYCutInner
for the halo distribution
Definition: beamBase.h:120
double tilt
tilt of beam applied as rotation about unit local z
Definition: beamBase.h:71
double sigma25
for the gaussian sigma matrix distribution
Definition: beamBase.h:98
double shellXpWidth
for the elliptic shell distribution
Definition: beamBase.h:107
std::string distrType
beam parameters
Definition: beamBase.h:45
double sigma36
for the gaussian sigma matrix distribution
Definition: beamBase.h:99
double sigma46
for the gaussian sigma matrix distribution
Definition: beamBase.h:100
double eventGeneratorMaxRp
Event generator file filter.
Definition: beamBase.h:147
int nlinesIgnore
Ignore first lines in the input bunch file.
Definition: beamBase.h:58
double emitNX
initial twiss parameters
Definition: beamBase.h:83
double eventGeneratorMinZ
Event generator file filter.
Definition: beamBase.h:138
double envelopeR
for the circle/square/box beam distribution
Definition: beamBase.h:93
double S0
initial beam centroid
Definition: beamBase.h:62
double eventGeneratorMinXp
Event generator file filter.
Definition: beamBase.h:140
double shellYp
for the elliptic shell distribution
Definition: beamBase.h:106
double dispyp
initial twiss parameters
Definition: beamBase.h:82
double sigma14
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double sigma23
for the gaussian sigma matrix distribution
Definition: beamBase.h:98
double sigma11
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double eventGeneratorMinZp
Event generator file filter.
Definition: beamBase.h:144
double envelopeX
for the circle/square/box beam distribution
Definition: beamBase.h:91
double shellX
for the elliptic shell distribution
Definition: beamBase.h:106
double P0
initial beam centroid
Definition: beamBase.h:67
std::string zDistrType
beam parameters
Definition: beamBase.h:48
double haloNSigmaXOuter
for the halo distribution
Definition: beamBase.h:116
std::vector< std::string > setKeys
A list of all the keys that have been set in this instance.
Definition: beamBase.h:157
double dispx
initial twiss parameters
Definition: beamBase.h:82
double alfx
initial twiss parameters
Definition: beamBase.h:82
double emitx
initial twiss parameters
Definition: beamBase.h:82
std::string eventGeneratorParticles
Event generator file filter.
Definition: beamBase.h:152
double sigmaT
bunch length
Definition: beamBase.h:74
double sigma22
for the gaussian sigma matrix distribution
Definition: beamBase.h:98
double bety
initial twiss parameters
Definition: beamBase.h:82
std::string yDistrType
beam parameters
Definition: beamBase.h:47
double Zp0
initial beam centroid
Definition: beamBase.h:63
double Rmax
for the ring beam distribution
Definition: beamBase.h:111
double envelopeXp
for the circle/square/box beam distribution
Definition: beamBase.h:91
double Z0
initial beam centroid
Definition: beamBase.h:62
double sigma33
for the gaussian sigma matrix distribution
Definition: beamBase.h:99
std::string energyDistrType
beam parameters
Definition: beamBase.h:51
double eventGeneratorMaxY
Event generator file filter.
Definition: beamBase.h:137
double eventGeneratorMinT
Event generator file filter.
Definition: beamBase.h:148
double sigma15
for the gaussian sigma matrix distribution
Definition: beamBase.h:97
double envelopeY
for the circle/square/box beam distribution
Definition: beamBase.h:91
double haloPSWeightParameter
for the halo distribution
Definition: beamBase.h:127
double sigma55
for the gaussian sigma matrix distribution
Definition: beamBase.h:101
double sigma56
for the gaussian sigma matrix distribution
Definition: beamBase.h:101
double X0
initial beam centroid
Definition: beamBase.h:62
double eventGeneratorMaxX
Event generator file filter.
Definition: beamBase.h:135
double envelopeT
for the circle/square/box beam distribution
Definition: beamBase.h:92
int nlinesSkip
Number of event lines to skip after the ignore lines.
Definition: beamBase.h:59
double eventGeneratorMinRp
Event generator file filter.
Definition: beamBase.h:146
double beamMomentum
beam parameters
Definition: beamBase.h:44
double Xp0
initial beam centroid
Definition: beamBase.h:63
double sigma24
for the gaussian sigma matrix distribution
Definition: beamBase.h:98
double eventGeneratorMinX
Event generator file filter.
Definition: beamBase.h:134
std::string xDistrType
beam parameters
Definition: beamBase.h:46
double shellY
for the elliptic shell distribution
Definition: beamBase.h:106
double Yp0
initial beam centroid
Definition: beamBase.h:63
std::string haloPSWeightFunction
for the halo distribution
Definition: beamBase.h:128
double shellYpWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double haloXpCutInner
for the halo distribution
Definition: beamBase.h:123
double Y0
initial beam centroid
Definition: beamBase.h:62
double haloYpCutOuter
for the halo distribution
Definition: beamBase.h:126
double Rmin
for the ring beam distribution
Definition: beamBase.h:111
double T0
initial beam centroid
Definition: beamBase.h:64
double sigmaY
for the gaussian beam distribution
Definition: beamBase.h:87
double shellXWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double shellYWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double E0
initial beam centroid
Definition: beamBase.h:65
Beam class.
Definition: beam.h:44
void PublishMembers()
publish members so these can be looked up from parser
Definition: beam.cc:151
void Amalgamate(const Beam &optionsIn, bool override, int startFromEvent=0)
Definition: beam.cc:97
bool HasBeenSet(const std::string &name) const
Whether a parameter has been set using the set_value method or not.
Definition: beam.cc:142
void publish(const std::string &name, T C::*mp)
Make pointer to member from class C and type T with accessible with a name.
Definition: published.h:92
void set(BeamBase *instance, const std::string &name, double value)
Definition: published.h:99
Parser namespace for GMAD language. Combination of Geant4 and MAD.