BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSIntegratorRMatrixThin.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
19#include "BDSDebug.hh"
20#include "BDSIntegratorRMatrixThin.hh"
21#include "BDSStep.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh"
25#include "G4Mag_EqRhs.hh"
26
27BDSIntegratorRMatrixThin::BDSIntegratorRMatrixThin(BDSMagnetStrength const* strength,
28 G4Mag_EqRhs* eqOfMIn,
29 G4double maximumRadiusIn):
30 BDSIntegratorMag(eqOfMIn, 6),
31 maximumRadius(maximumRadiusIn)
32{
33 kick1 = (*strength)["kick1"];
34 kick2 = (*strength)["kick2"];
35 kick3 = (*strength)["kick3"];
36 kick4 = (*strength)["kick4"];
37 rmat11 = (*strength)["rmat11"];
38 rmat12 = (*strength)["rmat12"];
39 rmat13 = (*strength)["rmat13"];
40 rmat14 = (*strength)["rmat14"];
41 rmat21 = (*strength)["rmat21"];
42 rmat22 = (*strength)["rmat22"];
43 rmat23 = (*strength)["rmat23"];
44 rmat24 = (*strength)["rmat24"];
45 rmat31 = (*strength)["rmat31"];
46 rmat32 = (*strength)["rmat32"];
47 rmat33 = (*strength)["rmat33"];
48 rmat34 = (*strength)["rmat34"];
49 rmat41 = (*strength)["rmat41"];
50 rmat42 = (*strength)["rmat42"];
51 rmat43 = (*strength)["rmat43"];
52 rmat44 = (*strength)["rmat44"];
53
54#ifdef BDSDEBUG
55 G4cout << "RMatrix " << rmat11 << " " << rmat12 << " " << rmat13 << " " << rmat14 << " " << kick1 << G4endl;
56 G4cout << "RMatrix " << rmat21 << " " << rmat22 << " " << rmat23 << " " << rmat24 << " " << kick2 << G4endl;
57 G4cout << "RMatrix " << rmat31 << " " << rmat32 << " " << rmat33 << " " << rmat34 << " " << kick3 << G4endl;
58 G4cout << "RMatrix " << rmat41 << " " << rmat42 << " " << rmat43 << " " << rmat44 << " " << kick4 << G4endl;
59#endif
60}
61
62void BDSIntegratorRMatrixThin::Stepper(const G4double yIn[],
63 const G4double /*dydx*/[],
64 const G4double h,
65 G4double yOut[],
66 G4double yErr[])
67{
68 for (G4int i = 0; i < 3; i++)
69 {
70 yErr[i] = 0;
71 yErr[i+3] = 0;
72 }
73
74 // check if beam particle, if so step as drift
75 const G4double fcof = eqOfM->FCof();
76 G4double lengthFraction = h / thinElementLength;
77
78 // only apply the kick if we're taking a step longer than half the length of the item,
79 // in which case, apply the full kick. This appears more robust than scaling the kick
80 // by h / thinElementLength as the precise geometrical length depends on the geometry
81 // ie if there's a beam pipe etc -> more length safetys. The geometry layout should
82 // prevent more than one step begin taken, but occasionally, a very small initial step
83 // can be taken resulting in a double kick.
84 if (lengthFraction < 0.51 || !BDS::IsFiniteStrength(fcof))
85 {
86 AdvanceDriftMag(yIn, h, yOut, yErr);
87 SetDistChord(0);
88 return;
89 }
90
91 G4ThreeVector pos = G4ThreeVector( yIn[0], yIn[1], yIn[2]);
92 G4ThreeVector mom = G4ThreeVector(yIn[3], yIn[4], yIn[5]);
93 G4double momMag = mom.mag();
94
95 BDSStep localPosMom = ConvertToLocal(pos, mom, h, false);
96 G4ThreeVector localPos = localPosMom.PreStepPoint();
97 G4ThreeVector localMom = localPosMom.PostStepPoint();
98 G4ThreeVector localMomUnit = localMom.unit();
99
100 G4double x0 = localPos.x();
101 G4double y0 = localPos.y();
102 G4double z0 = localPos.z();
103
104 G4double xp = localMomUnit.x();
105 G4double yp = localMomUnit.y();
106 G4double zp = localMomUnit.z();
107
108 // only proceed with thin matrix if particle is paraxial
109 // judged by forward momentum > 1-limit and |transverse| < limit (default limit=0.1)
110 if (zp < (1.0-backupStepperMomLimit) || std::abs(xp) > backupStepperMomLimit || std::abs(yp) > backupStepperMomLimit)
111 {
112 AdvanceDriftMag(yIn, h, yOut, yErr);
113 SetDistChord(0);
114 return;
115 }
116
117 G4double x1 = rmat11 * x0 + rmat12 * xp * CLHEP::m + rmat13 * y0 + rmat14 * yp * CLHEP::m + kick1;
118 G4double xp1 = rmat21 * x0 / CLHEP::m + rmat22 * xp + rmat23 * y0 / CLHEP::m + rmat24 * yp + kick2;
119 G4double y1 = rmat31 * x0 + rmat32 * xp * CLHEP::meter + rmat33 * y0 + rmat34 * yp * CLHEP::m + kick3;
120 G4double yp1 = rmat41 * x0 / CLHEP::m + rmat42 * xp + rmat43 * y0 / CLHEP::m + rmat44 * yp + kick4;
121 G4double z1 = z0 + h;
122 G4double zp1 = std::sqrt(1 - std::pow(xp1,2) - std::pow(yp1,2));
123
124 // need to check against aperture before returning
125 if(x1 > maximumRadius)
126 {x1 = maximumRadius;}
127 else if( x1 < -maximumRadius)
128 {x1 = -maximumRadius;}
129 if(y1 > maximumRadius)
130 {y1 = maximumRadius;}
131 else if( y1 < -maximumRadius)
132 {y1 = -maximumRadius;}
133
134 G4ThreeVector localPosOut = G4ThreeVector(x1, y1, z1);
135 G4ThreeVector localMomUnitOut = G4ThreeVector(xp1, yp1, zp1);
136 ConvertToGlobal(localPosOut, localMomUnitOut, yOut, yErr, momMag);
137}
BDSStep ConvertToLocal(G4Step const *const step, G4bool useCurvilinear=true) const
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.
static G4double thinElementLength
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
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 IsFiniteStrength(G4double variable)