BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSBunchCircle.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 "BDSBunchCircle.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22
23#include "parser/beam.h"
24
25#include "Randomize.hh"
26#include "CLHEP/Units/PhysicalConstants.h"
27#include "CLHEP/Units/SystemOfUnits.h"
28
29#include <cmath>
30
31BDSBunchCircle::BDSBunchCircle():
32 BDSBunch("circle"),
33 envelopeR(0.0),
34 envelopeRp(0.0),
35 envelopeT(0.0),
36 envelopeE(0.0)
37{
38 flatGen = new CLHEP::RandFlat(*CLHEP::HepRandom::getTheEngine());
39}
40
41BDSBunchCircle::~BDSBunchCircle()
42{
43 delete flatGen;
44}
45
47 const GMAD::Beam& beam,
48 const BDSBunchType& distrType,
49 G4Transform3D beamlineTransformIn,
50 const G4double beamlineSIn)
51{
52 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
53 envelopeR = beam.envelopeR * CLHEP::m;
54 envelopeRp = beam.envelopeRp * CLHEP::rad;
55 envelopeT = beam.envelopeT * CLHEP::s;
56 envelopeE = beam.envelopeE * CLHEP::GeV;
57}
58
60{
62 if (envelopeR <= 0)
63 {throw BDSException(__METHOD_NAME__, "envelopeR <= 0");}
64 if (envelopeRp < 0)
65 {throw BDSException(__METHOD_NAME__, "envelopeRp <= 0");}
66 if (envelopeT < 0)
67 {throw BDSException(__METHOD_NAME__, "envelopeT < 0");}
68 if (envelopeE < 0)
69 {throw BDSException(__METHOD_NAME__, "envelopeE < 0");}
70}
71
73{
74 G4double dt = envelopeT * (1.-2.*flatGen->shoot());
75 G4double dz = dt * CLHEP::c_light;
76
77 G4double t = T0 + dt;
78 G4double z = Z0 + dz;
79 G4double phiR = flatGen->shoot() * CLHEP::twopi;
80 G4double phiRp = flatGen->shoot() * CLHEP::twopi;
81 G4double r = std::sqrt(flatGen->shoot()) * envelopeR;
82 G4double rp = std::sqrt(flatGen->shoot()) * envelopeRp;
83
84 G4double x = X0 + std::cos(phiR) * r;
85 G4double y = Y0 + std::sin(phiR) * r;
86 G4double xp = Xp0 + std::cos(phiRp) * rp;
87 G4double yp = Yp0 + std::sin(phiRp) * rp;
88 G4double zp = CalculateZp(xp,yp,Zp0);
89 G4double E = E0 + envelopeE * (1-2*flatGen->shoot());
90
91 return BDSParticleCoordsFull(x,y,z,xp,yp,zp,t,S0-dz,E,/*weight=*/1.0);
92}
virtual void CheckParameters()
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, G4double beamlineS=0)
virtual BDSParticleCoordsFull GetNextParticleLocal()
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 envelopeRp
for the circle/square/box beam distribution
Definition: beamBase.h:93
double envelopeE
for the circle/square/box beam distribution
Definition: beamBase.h:92
double envelopeR
for the circle/square/box beam distribution
Definition: beamBase.h:93
double envelopeT
for the circle/square/box beam distribution
Definition: beamBase.h:92
Beam class.
Definition: beam.h:44