BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchSquare.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 "BDSBunchSquare.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSParticleCoordsFull.hh"
23
24#include "parser/beam.h"
25
26#include "Randomize.hh"
27#include "CLHEP/Units/PhysicalConstants.h"
28
29BDSBunchSquare::BDSBunchSquare():
30 BDSBunch("square"),
31 envelopeX(0.0),
32 envelopeY(0.0),
33 envelopeXp(0.0),
34 envelopeYp(0.0),
35 envelopeT(0.0),
36 envelopeE(0.0),
37 envelopeZ(0.0),
38 useEnvelopeZ(false)
39{
40 flatGen = new CLHEP::RandFlat(*CLHEP::HepRandom::getTheEngine());
41}
42
43BDSBunchSquare::~BDSBunchSquare()
44{
45 delete flatGen;
46}
47
49 const GMAD::Beam& beam,
50 const BDSBunchType& distrType,
51 G4Transform3D beamlineTransformIn,
52 const G4double beamlineSIn)
53{
54 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
55 envelopeX = beam.envelopeX * CLHEP::m;
56 envelopeY = beam.envelopeY * CLHEP::m;
57 envelopeXp = beam.envelopeXp * CLHEP::rad;
58 envelopeYp = beam.envelopeYp * CLHEP::rad;
59 envelopeT = beam.envelopeT * CLHEP::s;
60 envelopeE = beam.envelopeE * CLHEP::GeV;
61 envelopeZ = beam.envelopeZ * CLHEP::m;
62 useEnvelopeZ = beam.HasBeenSet("envelopeZ");
63}
64
66{
68 if (envelopeX < 0)
69 {throw BDSException(__METHOD_NAME__, "envelopeX < 0");}
70 if (envelopeXp < 0)
71 {throw BDSException(__METHOD_NAME__, "envelopeXp < 0");}
72 if (envelopeY < 0)
73 {throw BDSException(__METHOD_NAME__, "envelopeY < 0");}
74 if (envelopeYp < 0)
75 {throw BDSException(__METHOD_NAME__, "envelopeYp < 0");}
76 if (envelopeT < 0)
77 {throw BDSException(__METHOD_NAME__, "envelopeT < 0");}
78 if (envelopeE < 0)
79 {throw BDSException(__METHOD_NAME__, "envelopeE < 0");}
80 if (envelopeZ < 0)
81 {throw BDSException(__METHOD_NAME__, "envelopeZ < 0");}
82}
83
85{
86 G4double x = X0 + envelopeX * (1-2*flatGen->shoot());
87 G4double y = Y0 + envelopeY * (1-2*flatGen->shoot());
88 G4double xp = Xp0 + envelopeXp * (1-2*flatGen->shoot());
89 G4double yp = Yp0 + envelopeYp * (1-2*flatGen->shoot());
90 G4double zp = CalculateZp(xp,yp,Zp0);
91 G4double dt = envelopeT * (1.-2.*flatGen->shoot());
92 G4double t = T0 + dt;
93 G4double dz;
94 if (useEnvelopeZ)
95 {dz = envelopeZ * (1.-2*flatGen->shoot());}
96 else
97 {dz = dt * CLHEP::c_light;}
98 G4double z = Z0 + dz;
99 G4double E = E0 + envelopeE * (1 - 2*flatGen->shoot());
100
101 return BDSParticleCoordsFull(x,y,z,xp,yp,zp,t,S0+dz,E,/*weight=*/1.0);
102}
virtual BDSParticleCoordsFull GetNextParticleLocal()
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
virtual void CheckParameters()
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
G4double Yp0
Centre of distributions.
Definition: BDSBunch.hh:166
G4double T0
Centre of distributions.
Definition: BDSBunch.hh:164
G4double S0
Centre of distributions.
Definition: BDSBunch.hh:163
G4double Z0
Centre of distributions.
Definition: BDSBunch.hh:162
G4double X0
Centre of distributions.
Definition: BDSBunch.hh:160
G4double Zp0
Centre of distributions.
Definition: BDSBunch.hh:167
G4double Xp0
Centre of distributions.
Definition: BDSBunch.hh:165
G4double E0
Centre of distributions.
Definition: BDSBunch.hh:168
G4double Y0
Centre of distributions.
Definition: BDSBunch.hh:161
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition: BDSBunch.cc:78
static G4double CalculateZp(G4double xp, G4double yp, G4double Zp0)
Calculate zp safely based on other components.
Definition: BDSBunch.cc:317
virtual void CheckParameters()
Definition: BDSBunch.cc:195
General exception with possible name of object and message.
Definition: BDSException.hh:35
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
double envelopeZ
for the circle/square/box beam distribution
Definition: beamBase.h:91
double envelopeE
for the circle/square/box beam distribution
Definition: beamBase.h:92
double envelopeYp
for the circle/square/box beam distribution
Definition: beamBase.h:91
double envelopeX
for the circle/square/box beam distribution
Definition: beamBase.h:91
double envelopeXp
for the circle/square/box beam distribution
Definition: beamBase.h:91
double envelopeY
for the circle/square/box beam distribution
Definition: beamBase.h:91
double envelopeT
for the circle/square/box beam distribution
Definition: beamBase.h:92
Beam class.
Definition: beam.h:44
bool HasBeenSet(const std::string &name) const
Whether a parameter has been set using the set_value method or not.
Definition: beam.cc:142