BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldEMVectorSum.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 "BDSException.hh"
21#include "BDSFieldEM.hh"
22#include "BDSFieldEMVectorSum.hh"
23
24#include "G4ThreeVector.hh"
25#include "G4Types.hh"
26
27#include <utility>
28#include <vector>
29
30BDSFieldEMVectorSum::BDSFieldEMVectorSum()
31{}
32
33BDSFieldEMVectorSum::BDSFieldEMVectorSum(const std::vector<BDSFieldEM*>& fieldsIn,
34 const std::vector<G4ThreeVector>& fieldOffsetsIn,
35 const std::vector<G4double>& timeOffsetsIn, //type change from earlier version
36 const std::vector<double>& zLengthsIn):
37 fields(fieldsIn),
38 fieldOffsets(fieldOffsetsIn),
39 timeOffsets(timeOffsetsIn),
40 zLengthsOver2(zLengthsIn)
41{
42 if (fields.size() != fieldOffsets.size())
43 {throw BDSException(__METHOD_NAME__, "number of fields and number of position offsets do not match");}
44 if (fields.size() != timeOffsets.size())
45 {throw BDSException(__METHOD_NAME__, "number of fields and number of time offsets do not match");}
46 if (fields.size() != zLengthsOver2.size())
47 {throw BDSException(__METHOD_NAME__, "number of fields and number of z lengths do not match");}
48
49 for (auto it = zLengthsOver2.begin(); it < zLengthsOver2.end(); ++it)
50 {
51 *it /= 2.;
52 }
53}
54
55BDSFieldEMVectorSum::~BDSFieldEMVectorSum()
56{
57 for (auto f : fields)
58 {delete f;}
59}
60
61void BDSFieldEMVectorSum::PushBackField(const G4ThreeVector& positionOffset,
62 double timeOffset,
63 double zLength,
64 BDSFieldEM* emfield) {
65 fields.push_back(emfield); // memory is now owned by this
66 fieldOffsets.push_back(positionOffset);
67 timeOffsets.push_back(timeOffset);
68 zLengthsOver2.push_back(zLength/2);
69}
70
71std::pair<G4ThreeVector,G4ThreeVector> BDSFieldEMVectorSum::GetField(const G4ThreeVector& position,
72 const G4double t) const
73{
74 std::pair<G4ThreeVector, G4ThreeVector> result;
75 for (G4int i = 0; i < (G4int)fields.size(); i++)
76 {
77 G4ThreeVector dr = position - fieldOffsets[i];
78 if (fabs(dr.z()) > zLengthsOver2[i])
79 {
80 continue; // out of bounding box
81 }
82 G4double dt = t - timeOffsets[i];
83 auto deltaField = fields[i]->GetField(dr,dt);
84 result.first += deltaField.first;
85 result.second += deltaField.second;
86
87 }
88 return result;
89}
General exception with possible name of object and message.
virtual std::pair< G4ThreeVector, G4ThreeVector > GetField(const G4ThreeVector &position, const G4double t=0) const
Calculate the field value.
Interface for BDSIM electro-magnetic fields that may or may not be local.
Definition BDSFieldEM.hh:41