BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSBunchRing.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSBunchRing.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSParticleCoordsFull.hh"
22#include "BDSUtilities.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#include <string>
32
33BDSBunchRing::BDSBunchRing():
34 BDSBunch("ring"),
35 rMin(0),
36 rMax(0),
37 rpMin(0),
38 rpMax(0),
39 rpSingleValue(0),
40 anyNonZeroRp(false),
41 useRpRange(false)
42{;}
43
44BDSBunchRing::~BDSBunchRing()
45{;}
46
48 const GMAD::Beam& beam,
49 const BDSBunchType& distrType,
50 G4Transform3D beamlineTransformIn,
51 const G4double beamlineSIn)
52{
53 BDSBunch::SetOptions(beamParticle, beam, distrType, beamlineTransformIn, beamlineSIn);
54 rMin = beam.Rmin * CLHEP::m;
55 rMax = beam.Rmax * CLHEP::m;
56 rpMin = beam.Rpmin * CLHEP::rad;
57 rpMax = beam.Rpmax * CLHEP::rad;
58 anyNonZeroRp = rpMin > 0 || rpMax > 0;
59 useRpRange = rpMin != rpMax;
60 if (anyNonZeroRp)
61 {rpSingleValue = BDS::IsFinite(rpMin) ? rpMin : rpMax;}
62}
63
65{
67 if (rMin < 0)
68 {throw BDSException(__METHOD_NAME__, "Rmin: " + std::to_string(rMin) + " < 0");}
69 if (rMax < 0)
70 {throw BDSException(__METHOD_NAME__, "Rmax: " + std::to_string(rMin) + " < 0");}
71 if (rMax <= rMin)
72 {throw BDSException(__METHOD_NAME__, "Rmax: " + std::to_string(rMax) + " < Rmin: " + std::to_string(rMin));}
73 if (rpMin < 0)
74 {throw BDSException(__METHOD_NAME__, "Rpmin: " + std::to_string(rpMin) + " < 0");}
75 if (rpMax < 0)
76 {throw BDSException(__METHOD_NAME__, "Rpmax: " + std::to_string(rpMax) + " < 0");}
77 if (rpMax < rpMin)
78 {throw BDSException(__METHOD_NAME__, "Rpmax: " + std::to_string(rpMin) + " < Rpmin: " + std::to_string(rpMin));}
79 if (rpMin > 1)
80 {throw BDSException(__METHOD_NAME__, "Rpmin: " + std::to_string(rpMin) + " > 1");}
81 if (rpMax > 1)
82 {throw BDSException(__METHOD_NAME__, "Rpmax: " + std::to_string(rpMin) + " > 1");}
83}
84
86{
87 G4double r = std::sqrt(G4RandFlat::shoot(std::pow(rMin,2), std::pow(rMax,2)));
88 G4double phi = 2 * CLHEP::pi * G4RandFlat::shoot();
89 G4double x = X0 + r * std::sin(phi);
90 G4double y = Y0 + r * std::cos(phi);
91 G4double xp = Xp0;
92 G4double yp = Yp0;
93 if (useRpRange)
94 {
95 G4double rp = std::sqrt(G4RandFlat::shoot(std::pow(rpMin,2), std::pow(rpMax,2)));
96 xp += rp * std::sin(phi);
97 yp += rp * std::cos(phi);
98 }
99 else if (anyNonZeroRp)
100 {
101 xp += rpSingleValue * std::sin(phi);
102 yp += rpSingleValue * std::cos(phi);
103 } // else no rp used
104 G4double zp = CalculateZp(xp,yp,Zp0);
105 return BDSParticleCoordsFull(x,y,Z0,xp,yp,zp,T0,S0,E0,/*weight=*/1.0);
106}
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()
G4double rpMin
Used for a range of rp.
G4double rpMax
Used for a range of rp.
The base class for bunch distribution generators.
Definition BDSBunch.hh:46
G4double Yp0
Centre of distributions.
Definition BDSBunch.hh:176
G4double T0
Centre of distributions.
Definition BDSBunch.hh:174
G4double S0
Centre of distributions.
Definition BDSBunch.hh:173
G4double Z0
Centre of distributions.
Definition BDSBunch.hh:172
G4double X0
Centre of distributions.
Definition BDSBunch.hh:170
G4double Zp0
Centre of distributions.
Definition BDSBunch.hh:177
G4double Xp0
Centre of distributions.
Definition BDSBunch.hh:175
G4double E0
Centre of distributions.
Definition BDSBunch.hh:178
G4double Y0
Centre of distributions.
Definition BDSBunch.hh:171
virtual void SetOptions(const BDSParticleDefinition *beamParticle, const GMAD::Beam &beam, const BDSBunchType &distrType, G4Transform3D beamlineTransformIn=G4Transform3D::Identity, const G4double beamlineS=0)
Definition BDSBunch.cc:81
static G4double CalculateZp(G4double xp, G4double yp, G4double Zp0)
Calculate zp safely based on other components.
Definition BDSBunch.cc:373
virtual void CheckParameters()
Definition BDSBunch.cc:222
General exception with possible name of object and message.
A set of particle coordinates including energy and weight.
Wrapper for particle definition.
Improve type-safety of native enum data type in C++.
double Rmax
for the ring beam distribution
Definition beamBase.h:119
double Rpmax
for the ring beam distribution
Definition beamBase.h:120
double Rpmin
for the ring beam distribution
Definition beamBase.h:120
double Rmin
for the ring beam distribution
Definition beamBase.h:119
Beam class.
Definition beam.h:43
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())