BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSFieldMagGradient.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSDebug.hh"
20#include "BDSFieldMag.hh"
21#include "BDSFieldMagGradient.hh"
22#include "BDSFieldMagSkew.hh"
23#include "BDSInterpolatorType.hh"
24#include "BDSMagnetStrength.hh"
25
26#include "globals.hh"
27#include "G4ThreeVector.hh"
28
29#include "CLHEP/Units/SystemOfUnits.h"
30
31#include <string>
32#include <vector>
33
34BDSFieldMagGradient::BDSFieldMagGradient()
35{;}
36
37BDSFieldMagGradient::~BDSFieldMagGradient()
38{;}
39
40G4double BDSFieldMagGradient::GetBy(BDSFieldMag* field, G4double x, G4double y) const
41{
42 G4ThreeVector position(x, y, 0);
43 G4ThreeVector fieldAtXY = field->GetField(position);
44 G4double by = fieldAtXY[1]/CLHEP::tesla;
45 return by;
46}
47
49 G4int order,
50 G4double Brho)
51{
52 BDSMagnetStrength* outputstrengths = new BDSMagnetStrength();
53 G4double h =0.5; //distance apart in CLHEP distance units (mm) to place query points.
54
55 G4double brhoinv = 1./(Brho / CLHEP::tesla / CLHEP::m);
56 G4int centreIndex = 0;
57 std::vector<G4double> d = PrepareValues(BField, 5, 0, h, centreIndex);
58 G4int centreIndexSkew = 0;
59 std::vector<std::vector<G4double>> dskew = PrepareSkewValues(BField,5,0,h,centreIndexSkew);
60 // o+1 as we start from k1 upwards - ie, 0th order isn't calculated
61 for (G4int o = 0; o < order; ++o)
62 {
63 (*outputstrengths)["k" + std::to_string(o+1)] = Derivative(d, o+1, centreIndex, h) * brhoinv;
64 (*outputstrengths)["k" + std::to_string(o+1) + "s"] = Derivative(dskew[o], o+1, centreIndex, h) * brhoinv;
65
66#ifdef BDSDEBUG
67 G4cout << "k" << o+1 << " = " << (*outputstrengths)["k" + std::to_string(o+1)] << G4endl;
68 G4cout << "k" << o+1 << "s"<< " = " << (*outputstrengths)["k" + std::to_string(o+1) + "s"]<< G4endl;
69#endif
70 }
71 return outputstrengths;
72}
73
75 G4int order,
76 G4double centreX,
77 G4double h,
78 G4int& centreIndex) const
79{
80
81 G4int maxN = 2*order + 1;
82 centreIndex = maxN; // write out maxN to centre index
83 std::vector<G4double> data(2*maxN+1); // must initialise vector as not using push_back
84
85 for (G4int i = -maxN; i <= maxN; i++)
86 {data[maxN + i] = GetBy(field, centreX+(G4double)i*h);}
87 return data;
88}
89
90std::vector<std::vector<G4double>> BDSFieldMagGradient::PrepareSkewValues(BDSFieldMag* field,
91 G4int order,
92 G4double centreX,
93 G4double h,
94 G4int& centreIndex) const
95{
96 G4double rotation[5] = {CLHEP::pi/4, CLHEP::pi/6, CLHEP::pi/8, CLHEP::pi/10, CLHEP::pi/12};
97 std::vector<std::vector<G4double>> skewValues(order);
98 for (G4int j=0; j<order; j++)
99 {
100 BDSFieldMagSkew* skewField = new BDSFieldMagSkew(field, rotation[j]);
101 skewValues[j] = PrepareValues(skewField, order, centreX, h, centreIndex);
102 delete skewField;
103 }
104 return skewValues;
105}
106
107G4double BDSFieldMagGradient::Derivative(const std::vector<G4double>& data,
108 const G4int order,
109 const G4int startIndex,
110 const G4double h) const
111{
112 if (order == 0)
113 {return data.at(startIndex);}
114 G4int subOrder = order-1;
115 G4double result = -Derivative(data, subOrder,startIndex+2,h)
116 + 8*Derivative(data, subOrder,startIndex+1, h)
117 - 8*Derivative(data, subOrder,startIndex-1, h)
118 + Derivative(data, subOrder,startIndex-2, h);
119 result /= 12*(h/CLHEP::m);
120 return result;
121}
G4double Derivative(const std::vector< G4double > &data, const G4int order, const G4int startIndex, const G4double h) const
std::vector< G4double > PrepareValues(BDSFieldMag *field, G4int order, G4double centreX, G4double h, G4int &centreIndex) const
BDSMagnetStrength * CalculateMultipoles(BDSFieldMag *BField, G4int order, G4double Brho=4)
Find the Magnet strengths of a multipole field to nth order.
std::vector< std::vector< G4double > > PrepareSkewValues(BDSFieldMag *field, G4int order, G4double centreX, G4double h, G4int &centreIndex) const
G4double GetBy(BDSFieldMag *BField, G4double x, G4double y=0) const
Query a point on x axis (by default) for By.
A wrapper class for BDSFieldMag that rotates it.
Interface for static magnetic fields that may or may not be local.
Definition: BDSFieldMag.hh:37
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const =0
Efficient storage of magnet strengths.