BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSIntegratorDipoleRodrigues.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
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 "BDSDebug.hh"
20#include "BDSIntegratorDipoleRodrigues.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSMagnetStrength.hh"
23#include "BDSPhysicalConstants.hh"
24#include "BDSStep.hh"
25#include "BDSUtilities.hh"
26
27#include "globals.hh" // geant4 types / globals
28#include "G4AffineTransform.hh"
29#include "G4Mag_EqRhs.hh"
30#include "G4ThreeVector.hh"
31
32#include <cmath>
33#include <utility>
34
35BDSIntegratorDipoleRodrigues::BDSIntegratorDipoleRodrigues(BDSMagnetStrength const* strengthIn,
36 G4double /*brho*/,
37 G4Mag_EqRhs* eqOfMIn):
38 BDSIntegratorMag(eqOfMIn, 6),
39 cOverGeV(BDS::cOverGeV),
40 angle((*strengthIn)["angle"]),
41 length((*strengthIn)["length"]),
42 bField((*strengthIn)["field"]),
43 strength(strengthIn),
44 minimumRadiusOfCurvature(BDSGlobalConstants::Instance()->MinimumRadiusOfCurvature())
45{
46 zeroStrength = !BDS::IsFinite(bField);
47#ifdef BDSDEBUG
48 G4cout << __METHOD_NAME__ << "B Field " << bField << G4endl;
49#endif
50}
51
53 const G4double dydx[],
54 G4double h,
55 G4double yOut[],
56 G4double yErr[])
57{
58 const G4double fcof = eqOfM->FCof();
59
60 // In case of zero field or neutral particles do a linear step:
62 {
63 AdvanceDriftMag(yIn, h, yOut, yErr);
64 SetDistChord(0);
65 return;
66 }
67
68 // Construct variables
69 G4ThreeVector pos = G4ThreeVector(yIn[0], yIn[1], yIn[2]);
70 G4ThreeVector mom = G4ThreeVector(yIn[3], yIn[4], yIn[5]);
71 G4double charge = fcof/CLHEP::c_light;
72 G4double momMag = mom.mag();
73 G4double rho = momMag/CLHEP::GeV/(cOverGeV * bField/CLHEP::tesla * charge) * CLHEP::m;
74
75 if (std::abs(rho) < minimumRadiusOfCurvature)
76 {
77 AdvanceDriftMag(yIn, h, yOut, yErr);
78 SetDistChord(0);
79 return;
80 }
81
82 // global to local
83 // false = use the mass world for the transform
84 BDSStep localPosMom = ConvertToLocal(pos, mom, h, false);
85 G4ThreeVector localMom = localPosMom.PostStepPoint();
86 G4ThreeVector localMomUnit = localMom.unit();
87
88 // only proceed with stepping if particle is paraxial
89 // judged by forward momentum > 1-limit (default limit=0.1)
90 if (localMomUnit.z() < (1.0 - backupStepperMomLimit))
91 {// use a classical Runge Kutta stepper here
92 backupStepper->Stepper(yIn, dydx, h, yOut, yErr);
93 SetDistChord(backupStepper->DistChord());
94 return;
95 }
96
97 G4ThreeVector localPos = localPosMom.PreStepPoint();
98 G4ThreeVector initialLocalPos = localPos;
99 G4ThreeVector intiialLocalMomUnit = localMomUnit;
100
101 // advance the orbit
102 std::pair<G4ThreeVector,G4ThreeVector> RandRp = UpdatePandR(rho,h,localPos,localMomUnit);
103 G4ThreeVector outputLocalPos = RandRp.first;
104 G4ThreeVector outputLocalMomUnit = RandRp.second;
105
106 G4double CosT_ov_2 = std::cos(h/rho/2.0);
107 G4double dc = std::abs(rho)*(1.-CosT_ov_2);
108 if (std::isnan(dc))
109 {SetDistChord(rho);}
110 else
111 {SetDistChord(dc);}
112
113 // This uses the mass world volume for the transform!
114 ConvertToGlobal(outputLocalPos, outputLocalMomUnit, yOut, yErr, momMag);
115
116 //BDSStep localCL = GlobalToCurvilinear(strength, pos, mom, h, false, eqOfM->FCof());
117 //BDSStep globalOut = CurvilinearToGlobal(strength, localCL.PreStepPoint(), localCL.PostStepPoint(), false, eqOfM->FCof());
118
119 // If the radius of curvature is too small, reduce the momentum by 2%. This will
120 // cause artificial spiralling for what must be particles well below the design momenta.
121 // Nominally adding a small z increment along the axis of the helix wasn't reliable,
122 // as there can be inconsistencies in the field vectors resulting in 0 additional offset,
123 // plus Geant4 complained about clearly wrong motion. This way works and produces no
124 // errors. The particle would be lost approximately in the current location anyway.
125 if (std::abs(rho) < minimumRadiusOfCurvature)
126 {
127 G4double momentumReduction = 0.98;
128 yOut[3] *= momentumReduction;
129 yOut[4] *= momentumReduction;
130 yOut[5] *= momentumReduction;
131 }
132 return;
133}
134
136 const G4double dydx[],
137 const G4double h,
138 G4double yOut[],
139 G4double yErr[])
140{
141 G4double err = 1e-10 * h; // very small linear increase with distance
142 for (G4int i = 0; i < nVariables; i++)
143 {yErr[i] = err;}
144
145 AdvanceHelix(yIn, dydx, h, yOut, yErr);
146}
147
148std::pair<G4ThreeVector,G4ThreeVector> BDSIntegratorDipoleRodrigues::UpdatePandR(G4double rho,
149 G4double h,
150 G4ThreeVector localPos,
151 G4ThreeVector localMomUnit)
152{
153 G4ThreeVector yhat(0.,1.,0.);
154 G4ThreeVector vhat = localMomUnit;
155 G4ThreeVector vnorm = vhat.cross(yhat);
156
157 G4double Theta = h/rho;
158
159 G4double CosT_ov_2, SinT_ov_2, CosT, SinT;
160 CosT_ov_2 = std::cos(Theta/2);
161 SinT_ov_2 = std::sin(Theta/2);
162
163 CosT = (CosT_ov_2*CosT_ov_2) - (SinT_ov_2*SinT_ov_2);
164 SinT = 2*CosT_ov_2*SinT_ov_2;
165
166 G4ThreeVector dPos = rho*(SinT*vhat + (1-CosT)*vnorm); // missing component parallel to B
167 G4ThreeVector outputLocalPos = localPos+dPos;
168 G4ThreeVector outputLocalMomUnit = CosT*vhat + SinT*vnorm;
169
170 return std::make_pair(outputLocalPos,outputLocalMomUnit);
171}
BDSStep ConvertToLocal(G4Step const *const step, G4bool useCurvilinear=true) const
A class that holds global options and constants.
void AdvanceHelix(const G4double yIn[], const G4double dydx[], G4double h, G4double yOut[], G4double yErr[])
Calculate the new particle coordinates.
virtual void Stepper(const G4double yIn[], const G4double dydx[], const G4double h, G4double yOut[], G4double yErr[])
const G4double minimumRadiusOfCurvature
Minimum tolerable radius of curvature - used to prevent spiraling particles.
G4double bField
Uniform magnetic field in global Y direction.
G4double cOverGeV
Scaling factor in brho calculation.
void AdvanceDriftMag(const G4double yIn[], const G4double h, G4double yOut[], G4double yErr[]) const
Error array [6] all 0.
Common functionality to BDSIM integrators.
G4double backupStepperMomLimit
G4Mag_EqRhs * eqOfM
void SetDistChord(G4double distChordIn)
Setter for distChord to private member.
void ConvertToGlobal(const G4ThreeVector &localPos, const G4ThreeVector &localMom, G4double yOut[], G4double yErr[], const G4double momScaling=1.0)
scaling of momentum in case localMom is a unit vector
G4MagIntegratorStepper * backupStepper
const G4int nVariables
Cache of the number of variables.
Efficient storage of magnet strengths.
A simple class to represent the positions of a step.
Definition: BDSStep.hh:33
G4ThreeVector PostStepPoint() const
Accessor.
Definition: BDSStep.hh:43
G4ThreeVector PreStepPoint() const
Accessor.
Definition: BDSStep.hh:42
Return either G4Tubs or G4CutTubs depending on flat face.
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4bool IsFiniteStrength(G4double variable)