BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSIntegratorSolenoid.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 "BDSDebug.hh"
19#include "BDSIntegratorSolenoid.hh"
20#include "BDSMagnetStrength.hh"
21#include "BDSStep.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh" // geant4 types / globals.hh
25#include "G4Mag_EqRhs.hh"
26#include "G4ThreeVector.hh"
27
28#include <cmath>
29#include <limits>
30
32 G4double brho,
33 G4Mag_EqRhs* eqOfMIn):
34 BDSIntegratorMag(eqOfMIn, 6)
35{
36 bField = brho * (*strength)["ks"];
37 zeroStrength = !BDS::IsFinite(bField);
38#ifdef BDSDEBUG
39 G4cout << __METHOD_NAME__ << "B (local) = " << bField << G4endl;
40#endif
41}
42
43void BDSIntegratorSolenoid::Stepper(const G4double yIn[],
44 const G4double dydx[],
45 G4double h,
46 G4double yOut[],
47 G4double yErr[])
48{
49 // in case of zero field or neutral particles do a linear step
50 const G4double fcof = eqOfM->FCof();
51 if (zeroStrength || !BDS::IsFiniteStrength(fcof) || h < 1e-9)
52 {
53 AdvanceDriftMag(yIn,h,yOut,yErr);
54 SetDistChord(0);
55 return;
56 }
57
58 G4ThreeVector mom = G4ThreeVector(yIn[3], yIn[4], yIn[5]);
59 G4double momMag = mom.mag();
60
61 // kappa unit is m^-1, so scale to mm.
62 G4double kappa = 0.5*fcof*bField/momMag / CLHEP::m;
63
64 // neutral particle or no strength - advance as a drift.
65 if (std::abs(kappa)<1e-20)
66 {
67 AdvanceDriftMag(yIn, h, yOut, yErr);
68 SetDistChord(0);
69 return;
70 }
71
72 G4ThreeVector pos = G4ThreeVector(yIn[0], yIn[1], yIn[2]);
73 G4ThreeVector momUnit = mom.unit();
74 G4double h2 = h*h;
75 BDSStep localPosMom = GlobalToCurvilinear(pos, momUnit, h, true);
76 G4ThreeVector localPos = localPosMom.PreStepPoint();
77 G4ThreeVector localMom = localPosMom.PostStepPoint();
78 G4ThreeVector localMomUnit = localMom.unit();
79
80 // finite strength - treat as a solenoid
81 G4double x0 = localPos.x();
82 G4double y0 = localPos.y();
83 G4double z0 = localPos.z();
84 G4double xp0 = localMomUnit.x();
85 G4double yp0 = localMomUnit.y();
86 G4double zp0 = localMomUnit.z();
87
88 // only proceed with thick matrix if particle is paraxial
89 // judged by forward momentum > 1-limit and |transverse| < limit (default limit=0.1)
90 if (zp0 < (1.0-backupStepperMomLimit) || std::abs(xp0) > backupStepperMomLimit || std::abs(yp0) > backupStepperMomLimit)
91 {
92 backupStepper->Stepper(yIn, dydx, h, yOut, yErr);
93 SetDistChord(backupStepper->DistChord());
94 return;
95 }
96
97 // local r'' (for curvature)
98 G4ThreeVector localA;
99 localA.setX(-zp0*x0);
100 localA.setY( zp0*y0);
101 localA.setZ( x0*xp0 - y0*yp0);
102 localA *= kappa;
103
104 // determine effective curvature
105 G4double localAMag = localA.mag();
106
107 // low strength - drift
108 if (localAMag < 1e-15)
109 {
110 AdvanceDriftMag(yIn, h, yOut, yErr);
111 SetDistChord(0);
112 return;
113 }
114
115 G4double radiusOfCurvature = std::numeric_limits<double>::max();
116 if (BDS::IsFiniteStrength(localAMag))
117 {radiusOfCurvature = 1./localAMag;} // avoid division by 0
118
119 // chord distance (simple quadratic approx)
120 G4double dc = h2/(8*radiusOfCurvature);
121 if (std::isnan(dc))
122 {SetDistChord(0);}
123 else
124 {SetDistChord(dc);}
125
126 // From USPAS lecture (http://uspas.fnal.gov/materials/13Duke/SCL_Chap3.pdf)
127 // (1 , S / 2K , 0 , (1-C) / 2K ) (x )
128 // (0 , C , 0 , S ) (x')
129 // (0 , -(1-C) / 2K , 1 , S / 2K ) (y )
130 // (0 , -S , 0 , C ) (y')
131 //
132 // K = B_0 / 2*Brho
133 // B_0 is field in solenoid
134 // Brho is momentum of central trajectory
135 // C = cos( 2KL )
136 // S = sin( 2KL )
137
138 G4double C = std::cos(2 * kappa * h);
139 G4double S = std::sin(2 * kappa * h);
140 G4double S2oK = S / kappa;
141 G4double OmC2oK = (1.0 - C) / kappa;
142
143 G4double X11 = 1, X12 = 0.5*S2oK, X13 = 0, X14 = 0.5*OmC2oK;
144 G4double X21 = 0, X22 = C, X23 = 0, X24 = S;
145 G4double X31 = 0, X32 = -0.5*OmC2oK, X33 = 1, X34 = 0.5*S2oK;
146 G4double X41 = 0, X42 = -S, X43 = 0, X44 = C ;
147
148 G4double x1 = x0*X11 + xp0*X12 + y0*X13 + yp0*X14;
149 G4double xp1 = x0*X21 + xp0*X22 + y0*X23 + yp0*X24;
150 G4double y1 = x0*X31 + xp0*X32 + y0*X33 + yp0*X34;
151 G4double yp1 = x0*X41 + xp0*X42 + y0*X43 + yp0*X44;
152
153 G4double z1 = z0 + h;
154 // ensure normalisation for vector
155 G4double zp1 = std::sqrt(1 - xp1*xp1 - yp1*yp1);
156 if (std::isnan(zp1))
157 {zp1 = zp0;}
158
159 localPos.setX(x1);
160 localPos.setY(y1);
161 localPos.setZ(z1);
162 localMomUnit.setX(xp1);
163 localMomUnit.setY(yp1);
164 localMomUnit.setZ(zp1);
165
166 // normalise from unit momentum to absolute momentum
167 G4ThreeVector localMomOut = localMomUnit * momMag;
168
169 // convert to global coordinates
170 BDSStep globalPosMom = CurvilinearToGlobal(localPos, localMomOut, true);
171 G4ThreeVector globalPosOut = globalPosMom.PreStepPoint();
172 G4ThreeVector globalMomOut = globalPosMom.PostStepPoint();
173
174 // error along direction of travel really
175 G4ThreeVector globalMomOutU = globalMomOut.unit();
176 globalMomOutU *= 1e-8;
177
178 // write out coordinates and errors to arrays
179 for (G4int i = 0; i < 3; i++)
180 {
181 yOut[i] = globalPosOut[i];
182 yOut[i+3] = globalMomOut[i];
183 yErr[i] = globalMomOutU[i]*1e-10; // empirically this has to be very small
184 yErr[i+3] = 1e-40;
185 }
186}
BDSStep GlobalToCurvilinear(const G4double fieldArcLength, const G4ThreeVector &unitField, const G4double angle, const G4ThreeVector &position, const G4ThreeVector &unitMomentum, const G4double h, const G4bool useCurvilinearWorld, const G4double FCof, const G4double tilt=0)
void AdvanceDriftMag(const G4double yIn[], const G4double h, G4double yOut[], G4double yErr[]) const
Error array [6] all 0.
Common functionality to BDSIM integrators.
G4Mag_EqRhs * eqOfM
void SetDistChord(G4double distChordIn)
Setter for distChord to private member.
G4MagIntegratorStepper * backupStepper
virtual void Stepper(const G4double y[], const G4double dydx[], const G4double h, G4double yOut[], G4double yErr[])
Calculate the particle motion along step length l in the paraxial approximation.
BDSIntegratorSolenoid()
Private default constructor to enforce use of supplied constructor.
G4double bField
The field strength.
Efficient storage of magnet strengths.
A simple class to represent the positions of a step.
Definition BDSStep.hh:32
G4ThreeVector PostStepPoint() const
Accessor.
Definition BDSStep.hh:42
G4ThreeVector PreStepPoint() const
Accessor.
Definition BDSStep.hh:41
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4bool IsFiniteStrength(G4double variable)