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