BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagDipoleOuterOld.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 "BDSFieldMagDipoleOuterOld.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 BDSFieldMagDipoleOuterOld::transitionLengthScale = 1*CLHEP::cm;
32
33BDSFieldMagDipoleOuterOld::BDSFieldMagDipoleOuterOld(const BDSMagnetStrength* strength,
34 G4double poleTipRadiusIn,
35 G4double arbitraryScaling):
36 poleTipRadius(poleTipRadiusIn),
37 normalisation(1)
38{
39 BDSFieldMagDipole* innerField = new BDSFieldMagDipole(strength); // encapsulates logic about field parameters
40 // store copy of nominal field strength in vector form
41 localField = innerField->GetField(G4ThreeVector(0,0,0)); // doesn't vary with position
42 m = localField.unit(); // ensure unit vector
43
44 G4ThreeVector normalisationPoint = m*poleTipRadius;
45 G4ThreeVector innerFieldValue = innerField->GetField(normalisationPoint);
46 G4ThreeVector outerFieldValue = GetField(normalisationPoint);
47
48 normalisation = arbitraryScaling * innerFieldValue.mag() / outerFieldValue.mag();
49 if (std::isnan(normalisation))
50 {
51 normalisation = 0; // possible for 0 strength -> b inner = 0 / b outer = 0;
52 finiteStrength = false;
53 }
54
55 delete innerField;
56}
57
58G4ThreeVector BDSFieldMagDipoleOuterOld::GetField(const G4ThreeVector& position,
59 const G4double /*t*/) const
60{
61 if (!finiteStrength)
62 {return G4ThreeVector();} // 0,0,0
63 G4double rmag = position.mag();
64
65 if (rmag < 1) // closer than 1mm from dipole
66 {return localField;}
67
68 // calculate the field according to a magnetic dipole m at position r.
69 G4ThreeVector b = 3*position*(m.dot(position))/std::pow(rmag,5) - m/std::pow(rmag,3);
70
71 b *= normalisation;
72
73 // normalise with tanh (sigmoid-type function) between -3 and 3 in x and 0,1 in y.
74 // scaled spatially in r across the transition length scale
75 // tanh(x) goes from -1 to 1 -> change to 0,1 via +1 and /2
76 G4double weight = (std::tanh(3*(rmag - std::abs(0.5*poleTipRadius))/(transitionLengthScale)) + 1) / 2.0;
77 if (std::abs(weight) > 0.99 || std::abs(weight) < 0.01)
78 {weight = std::round(weight);} // tanh is asymptotic - snap to 1.0 when beyond 0.99
79 b = weight*b + (1-weight)*localField; // weighted sum of two components
80
81 return b;
82}
G4ThreeVector m
Dipole moment as unit vector of field direction.
G4double normalisation
Storage of the overal normalisation factor.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const double t=0) const
Access the field value.
G4double poleTipRadius
Used as radial limit for returning normal field.
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.