BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagMultipoleOuter.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 "BDSFieldMagMultipoleOuter.hh"
20#include "BDSUtilities.hh"
21
22#include "globals.hh"
23#include "G4RotationMatrix.hh"
24#include "G4ThreeVector.hh"
25#include "G4TwoVector.hh"
26
27#include "CLHEP/Units/PhysicalConstants.h"
28
29#include <cmath>
30#include <vector>
31
32BDSFieldMagMultipoleOuter::BDSFieldMagMultipoleOuter(G4int orderIn,
33 G4double poleTipRadiusIn,
34 const BDSFieldMag* innerFieldIn,
35 G4bool kPositive,
36 G4double brho,
37 G4double arbitraryScaling):
38 order(orderIn),
39 phiOffset(0),
40 spatialLimit(1),
41 normalisation(1), // we have to get field first to calculate the normalisation which uses it, so start with 1
42 positiveField(kPositive),
43 poleNOffset(0),
44 poleTipRadius(poleTipRadiusIn),
45 maxField(1e100),
46 initialisationPhase(true)
47{
48 // index of which current to start at when summing - will in effect start us at + or - current
49 // and therefore control the sign of the field.
50 poleNOffset = brho > 0 ? 1 : 0;
51
52 // we're assuming order > 0
53 G4int nPoles = 2*order;
54
55 // In the special case of a dipole, it can be inconsistent which parameter upstream
56 // gives the field direction. e.g. we can have the field vector (0,-1,0) and +ve magnitude
57 // in the "field" parameter and also "angle"=0. The parameterisation is also different from
58 // sbends to kickers and kickers have a variety of their own. Therefore, we simply follow
59 // the field direction, whatever it is. We get the nominal dipole field direction, then
60 // build our currents in a loop starting from that angular offset in the x,y plane. Note,
61 // phi = 0 is for the +x axis, and we want to start at the 12 o'clock position so a pi/2
62 // offset is introduced. This is agnostic of upstream parameterisation.
63 // This only applies for a dipole field.
64 const auto innerDipoleField = dynamic_cast<const BDSFieldMagDipole*>(innerFieldIn);
65 if (innerDipoleField)
66 {
67 poleNOffset = 0; // for consistency no matter brho - already in inner field direction
68 positiveField = false; // don't fiddle the sign of the field at the end
69 G4ThreeVector innerB = innerDipoleField->FieldValue();
70 G4double phiXY = innerB.phi();
71 phiOffset = phiXY - CLHEP::halfpi;
72 }
73
74 // prepare vector of infinite wire current sources
75 G4TwoVector firstCurrent(poleTipRadius, 0);
76 for (G4int i = 0; i < nPoles; ++i)
77 {
78 G4TwoVector c = firstCurrent; // copy it
79 c.rotate(phiOffset + (G4double)i*CLHEP::twopi / nPoles); // rotate copy
80 currents.push_back(c);
81 }
82
83 // work out a radial extent close to a current source where we artificially saturate
84 // do this based on the distance between two current sources
85 G4TwoVector pointA(poleTipRadius, 0);
86 G4TwoVector pointB = pointA;
87 pointB.rotate(CLHEP::twopi / nPoles);
88 G4double interPoleDistance = (pointB - pointA).mag();
89 // arbitrary -> let's say 5% of the distance between poles for saturation
90 spatialLimit = 0.05*interPoleDistance;
91
92 // query inner field at pole tip radius
93 // choose a point to query carefully though
94 // beside a current source (and with intialisationPhase=true) the function is unbound
95 // so we choose a point halfway in between the first two current sources
96 G4double angleC1 = currents[0].phi();
97 G4double angleC2 = currents[1].phi();
98 G4double angleAvg = (angleC1 + angleC2) / 2.0;
99 G4TwoVector queryPoint;
100 queryPoint.setPolar(poleTipRadius, angleAvg);
101 // technically we should also rotate by phiOffset here, but that's only used for a dipole
102 // and in that case it's always uniform, making no difference here, so skip it.
103 G4ThreeVector poleTipPoint(queryPoint.x(), queryPoint.y(), 0);
104
105 G4ThreeVector fieldAtPoleTip = innerFieldIn->GetField(poleTipPoint,/*t=*/0);
106 G4double fieldAtPoleTipMag = fieldAtPoleTip.mag();
107
108 finiteStrength = !BDS::IsFinite(fieldAtPoleTipMag) || innerFieldIn->FiniteStrength();
109
110 // include arbitrary scaling here so it can obey an arbitrary scaling
111 maxField = fieldAtPoleTipMag * arbitraryScaling;
112
113 // we query this field object but with the normalisation initialised to 1 so it won't
114 // affect the result despite using the same code
115 // the inner field is by default OFF just now so won't affect this
116 G4ThreeVector rawOuterlFieldAtPoleTip = GetField(poleTipPoint);
117 G4double rawOuterlFieldAtPoleTipMag = rawOuterlFieldAtPoleTip.mag();
118
119 // normalisation
120 normalisation = fieldAtPoleTipMag / rawOuterlFieldAtPoleTipMag;
121 normalisation *= arbitraryScaling;
122 if (!std::isfinite(normalisation))
123 {
124 normalisation = 0;
125 finiteStrength = false;
126 }
127 initialisationPhase = false;
128}
129
130G4ThreeVector BDSFieldMagMultipoleOuter::GetField(const G4ThreeVector& position,
131 const G4double /*t*/) const
132{
133 G4TwoVector pos(position.x(), position.y());
134
135 // temporary variables
136 G4TwoVector result;
137 G4TwoVector cToPos;
138 G4double cToPosMag = 0;
139 G4double reciprocal = 0;
140 G4TwoVector cToPosPerp;
141
142 // loop over linear sum from all infinite wire sources
143 G4int pole = 1; // counter
144 G4bool closeToPole = false;
145 for (const auto& c : currents)
146 {
147 cToPos = pos - c; // distance to this wire
148 cToPosMag = cToPos.mag();
149 if (cToPosMag < spatialLimit)
150 {// we're close to a pole
151 // for the contribution from this pole, resample at spatial limit r
152 // from the current point - will give same direction
153 pos += cToPos.unit() * spatialLimit;
154 cToPos = pos - c;
155 cToPosMag = cToPos.mag();
156 closeToPole = true;
157 }
158
159 reciprocal = 1/cToPosMag;
160 if (!std::isnormal(reciprocal))
161 {reciprocal = 1.0;} // protect against bad values
162 cToPosPerp = G4TwoVector(-cToPos.y(), cToPos.x());
163 G4TwoVector val = std::pow(-1, pole+poleNOffset)*cToPosPerp.unit() * reciprocal;
164 if (std::isfinite(val.x()) && std::isfinite(val.y())) // tolerate bad values
165 {result += val;}
166 pole++;
167 }
168
169 // get sign right to match convention
170 if (positiveField)
171 {result *= -1;}
172
173 // normalisation
174 result *= normalisation;
175
176 // limit to pole tip maximum
178 {
179 if ((result.mag() > maxField) || closeToPole)
180 {result = result.unit() * maxField;}
181 }
182
183 return G4ThreeVector(result.x(), result.y(), 0);
184}
A uniform dipole field.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const double t=0) const
Access the field value.
G4double spatialLimit
Radius from any current source within which the field is artificially saturated.
std::vector< G4TwoVector > currents
Locations of infinite wire current sources.
G4int poleNOffset
Offset for pole to start at - in effect this flips the sign of the field.
G4double maxField
Any field beyond this will curtailed to this value.
G4bool initialisationPhase
Need a way to control cludge normalisation behaviour during initial normalisation calculation.
G4double normalisation
Storage of the overall normalisation factor.
G4bool positiveField
Sign of magnetic field.
Interface for static magnetic fields that may or may not be local.
G4bool FiniteStrength() const
Accessor.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const =0
T mag(const T &v)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())