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