BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchEShell.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 "BDSBunchEShell.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#include "CLHEP/Units/SystemOfUnits.h"
29
30#include <cmath>
31
32BDSBunchEShell::BDSBunchEShell():
33 BDSBunch("eshell"),
34 shellX(0.0),
35 shellXp(0.0),
36 shellY(0.0),
37 shellYp(0.0),
38 shellXWidth(0.0),
39 shellXpWidth(0.0),
40 shellYWidth(0.0),
41 shellYpWidth(0.0)
42{
43 flatGen = new CLHEP::RandFlat(*CLHEP::HepRandom::getTheEngine());
44}
45
46BDSBunchEShell::~BDSBunchEShell()
47{
48 delete flatGen;
49}
50
52 const GMAD::Beam& beam,
53 const BDSBunchType& distrType,
54 G4Transform3D beamlineTransformIn,
55 const G4double beamlineSIn)
56{
57 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
58 shellX = beam.shellX * CLHEP::m;
59 shellY = beam.shellY * CLHEP::m;
60 shellXp = beam.shellXp * CLHEP::rad;
61 shellYp = beam.shellYp * CLHEP::rad;
62 shellXWidth = beam.shellXWidth * CLHEP::m;
63 shellXpWidth = beam.shellXpWidth * CLHEP::rad;
64 shellYWidth = beam.shellYWidth * CLHEP::m;
65 shellYpWidth = beam.shellYpWidth * CLHEP::rad;
66}
67
69{
71 if (shellX <= 0)
72 {throw BDSException(__METHOD_NAME__, "shellX <= 0");}
73 if (shellY <= 0)
74 {throw BDSException(__METHOD_NAME__, "shellY <= 0");}
75 if (shellXp <= 0)
76 {throw BDSException(__METHOD_NAME__, "shellXp <= 0");}
77 if (shellYp <= 0)
78 {throw BDSException(__METHOD_NAME__, "shellYp <= 0");}
79
80 // widths can be zero but can't be negative
81 if (shellXWidth < 0)
82 {throw BDSException(__METHOD_NAME__, "shellXWidth < 0");}
83 if (shellYWidth < 0)
84 {throw BDSException(__METHOD_NAME__, "shellYWidth < 0");}
85 if (shellXpWidth < 0)
86 {throw BDSException(__METHOD_NAME__, "shellXpWidth < 0");}
87 if (shellYpWidth < 0)
88 {throw BDSException(__METHOD_NAME__, "shellYpWidth < 0");}
89}
90
92{
93 G4double phi = 2 * CLHEP::pi * flatGen->shoot();
94 G4double xamp = 0.5 - flatGen->shoot();
95 G4double yamp = 0.5 - flatGen->shoot();
96 G4double xpamp = 0.5 - flatGen->shoot();
97 G4double ypamp = 0.5 - flatGen->shoot();
98
99 G4double x = X0 + (std::sin(phi) * shellX) + xamp * shellXWidth;
100 G4double xp = Xp0 + (std::cos(phi) * shellXp) + xpamp * shellXpWidth;
101 phi = 2 * CLHEP::pi * flatGen->shoot();
102 G4double y = Y0 + (std::sin(phi) * shellY) + yamp * shellYWidth;
103 G4double yp = Yp0 + (std::cos(phi) * shellYp) + ypamp * shellYpWidth;
104 G4double zp = CalculateZp(xp,yp,Zp0);
105 G4double E = E0 * (1 + sigmaE/2. * (1. -2. * flatGen->shoot()));
106
107 return BDSParticleCoordsFull(x,y,Z0,xp,yp,zp,T0,S0,E,/*weight=*/1.0);
108}
109
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 sigmaE
Centre of distributions.
Definition: BDSBunch.hh:173
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 shellXp
for the elliptic shell distribution
Definition: beamBase.h:106
double shellXpWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double shellYp
for the elliptic shell distribution
Definition: beamBase.h:106
double shellX
for the elliptic shell distribution
Definition: beamBase.h:106
double shellY
for the elliptic shell distribution
Definition: beamBase.h:106
double shellYpWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double shellXWidth
for the elliptic shell distribution
Definition: beamBase.h:107
double shellYWidth
for the elliptic shell distribution
Definition: beamBase.h:107
Beam class.
Definition: beam.h:44