BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagDipoleOuter.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 "BDSFieldMagDipole.hh"
19#include "BDSFieldMagDipoleOuter.hh"
20#include "BDSMagnetStrength.hh"
21
22#include "globals.hh"
23#include "G4ThreeVector.hh"
24
25#include "CLHEP/Units/PhysicalConstants.h"
26#include "CLHEP/Units/SystemOfUnits.h"
27
28#include <cmath>
29#include <vector>
30
31G4double BDSFieldMagDipoleOuter::transitionLengthScale = 1*CLHEP::cm;
32
33BDSFieldMagDipoleOuter::BDSFieldMagDipoleOuter(const BDSMagnetStrength* strength,
34 const G4double& poleTipRadiusIn,
35 G4double arbitraryScaling):
36 spatialLimit(0.05*poleTipRadiusIn),
37 poleTipRadius(poleTipRadiusIn),
38 normalisation(1),
39 maxField(0),
40 initialisationPhase(true)
41{
42 BDSFieldMagDipole* innerField = new BDSFieldMagDipole(strength); // encapsulates logic about field parameters
43 // store copy of nominal field strength in vector form
44 localField = innerField->GetField(G4ThreeVector(0,0,0)); // doesn't vary with position
45 m = localField.unit(); // ensure unit vector
46
47 G4ThreeVector normalisationPoint = m*poleTipRadius;
48 G4ThreeVector innerFieldValue = innerField->GetField(normalisationPoint);
49 G4ThreeVector outerFieldValue = GetField(normalisationPoint);
50
51 // include arbitrary scaling here so it can obey an arbitrary scaling
52 maxField = innerFieldValue.mag() * arbitraryScaling;
53
54 normalisation = maxField / outerFieldValue.mag();
55 normalisation *= arbitraryScaling;
56 if (std::isnan(normalisation))
57 {
58 normalisation = 0; // possible for 0 strength -> b inner = 0 / b outer = 0;
59 finiteStrength = false;
60 }
61 initialisationPhase = false;
62 delete innerField;
63}
64
65G4ThreeVector BDSFieldMagDipoleOuter::GetField(const G4ThreeVector& position,
66 const G4double /*t*/) const
67{
68 if (!finiteStrength)
69 {return G4ThreeVector();} // 0,0,0
70 G4double rmag = position.mag();
71
72 if (rmag < spatialLimit) // too close to current source
73 {return localField;}
74
75 // calculate the field according to a magnetic dipole m at position r.
76 G4ThreeVector b = 3*position*(m.dot(position))/std::pow(rmag,5) - m/std::pow(rmag,3);
77
78 // normalise with tanh (sigmoid-type function) between -3 and 3 in x and 0,1 in y.
79 // scaled spatially in r across the transition length scale
80 // tanh(x) goes from -1 to 1 -> change to 0,1 via +1 and /2
81 G4double weight = (std::tanh(3*(rmag - std::abs(0.5*poleTipRadius))/(transitionLengthScale)) + 1) / 2.0;
82 if (std::abs(weight) > 0.99 || std::abs(weight) < 0.01)
83 {weight = std::round(weight);} // tanh is asymptotic - snap to 1.0 when beyond 0.99
84 b = weight*b + (1-weight)*localField; // weighted sum of two components
85
86 b *= normalisation;
87
89 {
90 if (b.mag() > maxField)
91 {b = b.unit() * maxField;}
92 }
93
94 return b;
95}
virtual G4ThreeVector GetField(const G4ThreeVector &position, const double t=0) const
Access the field value.
G4ThreeVector m
Dipole moment as unit vector of field direction.
G4double normalisation
Storage of the overal normalisation factor.
G4bool initialisationPhase
Need a way to control cludge normalisation behaviour during initial normalisation calculation.
G4double poleTipRadius
Used as radial limit for returning normal field.
G4double maxField
Any field beyond this will curtailed to this value.
G4double spatialLimit
Limit for getting too close to a current source.
G4ThreeVector localField
Nominal dipole field.
A uniform dipole field.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const
Access the field value.
G4bool finiteStrength
Flag to cache whether finite nor not.
Efficient storage of magnet strengths.