BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagSolenoidSheet.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 "BDSFieldMagSolenoidSheet.hh"
20#include "BDSMagnetStrength.hh"
21#include "BDSSpecialFunctions.hh"
22#include "BDSUtilities.hh"
23
24#include "G4ThreeVector.hh"
25#include "G4Types.hh"
26
27#include "CLHEP/Units/PhysicalConstants.h"
28#include "CLHEP/Units/SystemOfUnits.h"
29
30#include <algorithm>
31#include <cmath>
32
33BDSFieldMagSolenoidSheet::BDSFieldMagSolenoidSheet(BDSMagnetStrength const* strength,
34 G4double radiusIn, G4double toleranceIn):
35 BDSFieldMagSolenoidSheet((*strength)["field"], false, radiusIn, (*strength)["length"], toleranceIn)
36{;}
37
38BDSFieldMagSolenoidSheet::BDSFieldMagSolenoidSheet(G4double strength,
39 G4bool strengthIsCurrent,
40 G4double sheetRadius,
41 G4double fullLength,
42 G4double toleranceIn):
43 a(sheetRadius),
44 halfLength(0.5*fullLength),
45 B0(0.0),
46 I(0.0),
47 spatialLimit(std::min(1e-5*sheetRadius, 1e-5*fullLength)),
48 normalisation(1.0) ,
49 coilTolerance(toleranceIn)
50{
51 finiteStrength = BDS::IsFinite(std::abs(strength));
52 // apply relationship B0 = mu_0 I / 2 a for on-axis rho=0,z=0
53 if (strengthIsCurrent)
54 {
55 I = strength;
56 B0 = CLHEP::mu0 * strength / (CLHEP::pi*2* halfLength);
57 }
58 else
59 {// strength is B0 -> calculate current
60 B0 = strength;
61 I = B0 *(CLHEP::pi*2* halfLength) / CLHEP::mu0;
62 }
63
64 // The field inside the current cylinder is actually slightly parabolic in rho.
65 // The equation for the field takes B0 as the peak magnetic field at the current
66 // cylinder sheet. So we evaluate it here then normalise. ~<1% adjustment in magnitude.
67 //G4double testBz = OnAxisBz(halfLength, -halfLength);
68 //normalisation = B0 / testBz;
69}
70
71G4ThreeVector BDSFieldMagSolenoidSheet::GetField(const G4ThreeVector& position,
72 const G4double /*t*/) const
73{
74 G4double z = position.z();
75 G4double rho = position.perp();
76 G4double phi = position.phi(); // angle about z axis
77
78 // check if close to current source - function not well-behaved at exactly the rho of
79 // the current source or at the boundary of +- halfLength
80 if (std::abs(rho - a) < spatialLimit && (std::abs(z) < halfLength+2*spatialLimit))
81 {return G4ThreeVector();} // close to radius and inside +- z
82 G4double zp = z + halfLength;
83 G4double zm = z - halfLength;
84
85 G4double Brho = 0;
86 G4double Bz = 0;
87
88 // approximation for on-axis
89 if (std::abs(OnAxisBz(zp, zm))< coilTolerance)
90 {
91 Brho = 0.0;
92 Bz = 0.0;
93 }
94 else if (std::abs(rho) < spatialLimit)
95 {Bz = OnAxisBz(zp, zm);}
96 else
97 {
98 G4double zpSq = zp*zp;
99 G4double zmSq = zm*zm;
100
101 G4double rhoPlusA = rho + a;
102 G4double rhoPlusASq = rhoPlusA * rhoPlusA;
103 G4double aMinusRho = a - rho;
104 G4double aMinusRhoSq = aMinusRho*aMinusRho;
105
106 G4double denominatorP = std::sqrt(zpSq + rhoPlusASq);
107 G4double denominatorM = std::sqrt(zmSq + rhoPlusASq);
108
109 G4double alphap = a / denominatorP;
110 G4double alpham = a / denominatorM;
111
112 G4double betap = zp / denominatorP;
113 G4double betam = zm / denominatorM;
114
115 G4double gamma = (a - rho) / (rhoPlusA);
116 G4double gammaSq = gamma * gamma;
117
118 G4double kp = std::sqrt(zpSq + aMinusRhoSq) / denominatorP;
119 G4double km = std::sqrt(zmSq + aMinusRhoSq) / denominatorM;
120
121 Brho = B0 * (alphap * BDS::CEL(kp, 1, 1, -1) - alpham * BDS::CEL(km, 1, 1, -1));
122 Bz = ((B0 * a) / (rhoPlusA)) * (betap * BDS::CEL(kp, gammaSq, 1, gamma) - betam * BDS::CEL(km, gammaSq, 1, gamma));
123 // technically possible for integral to return nan, so protect against it and default to B0 along z
124 if (std::isnan(Brho))
125 {Brho = 0;}
126 if (std::isnan(Bz))
127 {Bz = B0;}
128 }
129 // we have to be consistent with the phi we calculated at the beginning,
130 // so unit rho is in the x direction.
131 G4ThreeVector result = G4ThreeVector(Brho,0,Bz)* normalisation;
132 result = result.rotateZ(phi);
133 return result;
134}
135
137 G4double zm) const
138{
139 G4double f1 = zp / std::sqrt( zp*zp + a*a );
140 G4double f2 = zm / std::sqrt( zm*zm + a*a );
141 G4double Bz = 0.5*B0 *CLHEP::pi* (f1 - f2);
142 return Bz;
143}
Class that provides the magnetic field due to a cylinder of current.
G4double OnAxisBz(G4double zp, G4double zm) const
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const
Calculate the field value.
G4bool finiteStrength
Flag to cache whether finite nor not.
Efficient storage of magnet strengths.
G4double CEL(G4double kc, G4double p, G4double c, G4double s, G4int nIterationLimit=1000)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
STL namespace.