BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldMag.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 "BDSFieldMag.hh"
19#include "BDSModulator.hh"
20
21#include "globals.hh"
22#include "G4ThreeVector.hh"
23#include "G4Transform3D.hh"
24
25#include <cmath>
26
28 finiteStrength(true),
29 transform(G4Transform3D::Identity),
30 transformIsNotIdentity(false),
31 modulator(nullptr),
32 inverseTransform(G4Transform3D::Identity)
33{;}
34
35BDSFieldMag::BDSFieldMag(G4Transform3D transformIn):
36 finiteStrength(true),
37 transform(transformIn),
38 transformIsNotIdentity(transformIn != G4Transform3D::Identity),
39 modulator(nullptr),
40 inverseTransform(transformIn.inverse())
41{;}
42
43G4ThreeVector BDSFieldMag::GetFieldTransformed(const G4ThreeVector& position,
44 const G4double t) const
45{
46 if (!finiteStrength)
47 {return G4ThreeVector();} // quicker than query
49 {
50 G4ThreeVector transformedPosition = inverseTransform * (HepGeom::Point3D<G4double>)position;
51 G4ThreeVector field = GetField(transformedPosition, t);
52 if (modulator)
53 {
54 G4double factor = modulator->Factor(transformedPosition, t);
55 field *= factor;
56 }
57 G4ThreeVector transformedField = transform * (HepGeom::Vector3D<G4double>)field;
58 return transformedField;
59 }
60 else
61 {
62 G4ThreeVector field = GetField(position, t);
63 if (modulator)
64 {
65 G4double factor = modulator->Factor(position, t);
66 field *= factor;
67 }
68 return field;
69 }
70}
71
72void BDSFieldMag::GetFieldValue(const G4double point[4],
73 G4double* field) const
74{
75 G4double t = point[3];
76 if (std::isnan(t))
77 {t = 0;}
78 G4ThreeVector fieldValue = GetFieldTransformed(G4ThreeVector(point[0], point[1], point[2]), t);
79 field[0] = fieldValue[0]; // B_x
80 field[1] = fieldValue[1]; // B_y
81 field[2] = fieldValue[2]; // B_z
82 field[3] = 0; // E_x
83 field[4] = 0; // E_y
84 field[5] = 0; // E_z
85}
86
87void BDSFieldMag::SetTransform(const G4Transform3D& transformIn)
88{
89 transform = transformIn;
90 transformIsNotIdentity = transformIn != G4Transform3D::Identity;
91 inverseTransform = transformIn.inverse();
92}
BDSModulator * modulator
Optional modulator;.
virtual void SetTransform(const G4Transform3D &transformIn)
virtual void GetFieldValue(const G4double point[4], G4double *field) const
virtual G4ThreeVector GetFieldTransformed(const G4ThreeVector &position, const G4double t) const
Get the field value after applying transform for local offset.
virtual G4ThreeVector GetField(const G4ThreeVector &position, const G4double t=0) const =0
G4bool finiteStrength
Flag to cache whether finite nor not.
G4Transform3D inverseTransform
The complimentary transform used to initially rotate the point of query.
G4Transform3D transform
Transform to apply for the field relative to the local coordinates of the geometry.
G4bool transformIsNotIdentity
Cache of whether to use transform at all.
virtual G4double Factor(const G4ThreeVector &xyz, G4double T) const =0