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