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) 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 "BDSFieldMagDipole.hh"
20#include "BDSFieldMagDipoleOuter.hh"
21#include "BDSMagnetStrength.hh"
22
23#include "globals.hh"
24#include "G4ThreeVector.hh"
25
26#include "CLHEP/Units/PhysicalConstants.h"
27#include "CLHEP/Units/SystemOfUnits.h"
28
29#include <cmath>
30#include <vector>
31
32G4double BDSFieldMagDipoleOuter::transitionLengthScale = 1*CLHEP::cm;
33
34BDSFieldMagDipoleOuter::BDSFieldMagDipoleOuter(const BDSMagnetStrength* strength,
35 const G4double& poleTipRadiusIn,
36 G4double arbitraryScaling):
37 spatialLimit(0.05*poleTipRadiusIn),
38 poleTipRadius(poleTipRadiusIn),
39 normalisation(1),
40 maxField(0),
41 initialisationPhase(true)
42{
43 BDSFieldMagDipole* innerField = new BDSFieldMagDipole(strength); // encapsulates logic about field parameters
44 // store copy of nominal field strength in vector form
45 localField = innerField->GetField(G4ThreeVector(0,0,0)); // doesn't vary with position
46 m = localField.unit(); // ensure unit vector
47
48 G4ThreeVector normalisationPoint = m*poleTipRadius;
49 G4ThreeVector innerFieldValue = innerField->GetField(normalisationPoint);
50 G4ThreeVector outerFieldValue = GetField(normalisationPoint);
51
52 // include arbitrary scaling here so it can obey an arbitrary scaling
53 maxField = innerFieldValue.mag() * arbitraryScaling;
54
55 normalisation = maxField / outerFieldValue.mag();
56 normalisation *= arbitraryScaling;
57 if (std::isnan(normalisation))
58 {
59 normalisation = 0; // possible for 0 strength -> b inner = 0 / b outer = 0;
60 finiteStrength = false;
61 }
62 initialisationPhase = false;
63 delete innerField;
64}
65
66G4ThreeVector BDSFieldMagDipoleOuter::GetField(const G4ThreeVector& position,
67 const G4double /*t*/) const
68{
69 if (!finiteStrength)
70 {return G4ThreeVector();} // 0,0,0
71 G4double rmag = position.mag();
72
73 if (rmag < spatialLimit) // too close to current source
74 {return localField;}
75
76 // calculate the field according to a magnetic dipole m at position r.
77 G4ThreeVector b = 3*position*(m.dot(position))/std::pow(rmag,5) - m/std::pow(rmag,3);
78
79 // normalise with tanh (sigmoid-type function) between -3 and 3 in x and 0,1 in y.
80 // scaled spatially in r across the transition length scale
81 // tanh(x) goes from -1 to 1 -> change to 0,1 via +1 and /2
82 G4double weight = (std::tanh(3*(rmag - std::abs(0.5*poleTipRadius))/(transitionLengthScale)) + 1) / 2.0;
83 if (std::abs(weight) > 0.99 || std::abs(weight) < 0.01)
84 {weight = std::round(weight);} // tanh is asymptotic - snap to 1.0 when beyond 0.99
85 b = weight*b + (1-weight)*localField; // weighted sum of two components
86
87 b *= normalisation;
88
90 {
91 if (b.mag() > maxField)
92 {b = b.unit() * maxField;}
93 }
94
95 return b;
96}
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.
Definition: BDSFieldMag.hh:83
Efficient storage of magnet strengths.