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