BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMagVectorSum.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 "BDSFieldMag.hh"
22#include "BDSFieldMagVectorSum.hh"
23
24#include "G4ThreeVector.hh"
25#include "G4Types.hh"
26
27#include <vector>
28
29BDSFieldMagVectorSum::BDSFieldMagVectorSum(const std::vector<BDSFieldMag*>& fieldsIn,
30 const std::vector<G4ThreeVector>& fieldOffsetsIn):
31 fields(fieldsIn),
32 fieldOffsets(fieldOffsetsIn)
33{
34 if (fields.size() != fieldOffsets.size())
35 {throw BDSException(__METHOD_NAME__, "number of fields and number of offsets do not match");}
36}
37
38BDSFieldMagVectorSum::~BDSFieldMagVectorSum()
39{
40 for (auto f : fields)
41 {delete f;}
42}
43
44G4ThreeVector BDSFieldMagVectorSum::GetField(const G4ThreeVector& position,
45 const G4double t) const
46{
47 G4ThreeVector result;
48 for (G4int i = 0; i < (G4int)fields.size(); i++)
49 {
50 G4ThreeVector p = position - fieldOffsets[i];
51 G4ThreeVector v = fields[i]->GetField(p,t);
52 result += v;
53 }
54 return result;
55}
General exception with possible name of object and message.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const
Calculate the field value.