BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSFieldObjects.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 "BDSFieldManager.hh"
19#include "BDSGlobalConstants.hh"
20#include "BDSFieldInfo.hh"
21#include "BDSFieldObjects.hh"
22
23#include "G4ChordFinder.hh"
24#include "G4ElectroMagneticField.hh"
25#include "G4Field.hh"
26#include "G4FieldManager.hh"
27#include "G4LogicalVolume.hh"
28#include "G4MagIntegratorDriver.hh" // for G4MagInt_Driver
29#include "G4MagIntegratorStepper.hh"
30#include "G4MagneticField.hh"
31#include "G4Version.hh"
32
33#include <vector>
34
35BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
36 G4Field* fieldIn,
37 G4EquationOfMotion* equationOfMotionIn,
38 G4MagIntegratorStepper* magIntegratorStepperIn,
39 G4ChordFinder* chordFinderIn,
40 G4FieldManager* fieldManagerIn):
41 info(infoIn),
42 field(fieldIn),
43 equationOfMotion(equationOfMotionIn),
44 magIntegratorStepper(magIntegratorStepperIn),
45 chordFinder(chordFinderIn),
46 fieldManager(fieldManagerIn),
47 magIntDriver(nullptr)
48{;}
49
50BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
51 G4ElectroMagneticField* fieldIn,
52 G4EquationOfMotion* equationOfMotionIn,
53 G4MagIntegratorStepper* magIntegratorStepperIn):
54 info(infoIn),
55 field(fieldIn),
56 equationOfMotion(equationOfMotionIn),
57 magIntegratorStepper(magIntegratorStepperIn)
58{
59 G4double chordStepMinimum = info->ChordStepMinimum();
60 if (chordStepMinimum <= 0)
61 {chordStepMinimum = BDSGlobalConstants::Instance()->ChordStepMinimum();}
62
63 magIntDriver = new G4MagInt_Driver(chordStepMinimum,
65 magIntegratorStepper->GetNumberOfVariables());
66
67 chordFinder = new G4ChordFinder(magIntDriver);
68
69 // We use our custom field manager that is a thin wrapper for the Geant4 one
70 // that only identifies whether we have a primary track or not for BDSIntegratorMag
72 if (info->IsThin())
74 else
76}
77
78#if G4VERSION_NUMBER > 1049
79BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
80 G4MagneticField* fieldIn,
81 G4EquationOfMotion* equationOfMotionIn,
82 G4MagIntegratorStepper* magIntegratorStepperIn):
83 info(infoIn),
84 field(fieldIn),
85 equationOfMotion(equationOfMotionIn),
86 magIntegratorStepper(magIntegratorStepperIn)
87{
88 G4double chordStepMinimum = info->ChordStepMinimum();
89 if (chordStepMinimum <= 0)
90 {chordStepMinimum = BDSGlobalConstants::Instance()->ChordStepMinimum();}
91
92 magIntDriver = new G4MagInt_Driver(chordStepMinimum,
94 magIntegratorStepper->GetNumberOfVariables());
95
96 chordFinder = new G4ChordFinder(magIntDriver);
97 fieldManager = new G4FieldManager(field, chordFinder);
98
99 if (info->IsThin())
101 else
103}
104#endif
105
107{
108 delete field;
109 delete fieldManager;
110 delete chordFinder;
112 delete equationOfMotion;
113 //delete magIntDriver; // not needed since deleted by chordFinder
114}
115
116void BDSFieldObjects::AttachToVolume(G4LogicalVolume* volume,
117 G4bool penetrateToDaughterVolumes) const
118{
119 volume->SetFieldManager(fieldManager, penetrateToDaughterVolumes);
120 if (!info) // may not always exist
121 {return;}
122
123 // optionally attach user limits
124 auto ul = info->UserLimits();
125 if (ul)
126 {AttachUserLimitsToVolume(volume, ul, penetrateToDaughterVolumes);}
127}
128
129void BDSFieldObjects::AttachToVolume(const std::vector<G4LogicalVolume*>& volumes,
130 G4bool penetrateToDaughterVolumes) const
131{
132 for (auto volume : volumes)
133 {AttachToVolume(volume, penetrateToDaughterVolumes);}
134}
135
136void BDSFieldObjects::AttachUserLimitsToVolume(G4LogicalVolume* volume,
137 G4UserLimits* userLimits,
138 G4bool penetrateToDaughterVolumes) const
139{
140 volume->SetUserLimits(userLimits);
141 if (penetrateToDaughterVolumes)
142 {
143 for (G4int i = 0; i < (G4int)volume->GetNoDaughters(); i++)
144 {AttachUserLimitsToVolume(volume->GetDaughter(i)->GetLogicalVolume(), userLimits, penetrateToDaughterVolumes);}
145 }
146}
147
149{
151 fieldManager->SetDeltaIntersection(globals->DeltaIntersection());
152 fieldManager->SetMinimumEpsilonStep(globals->MinimumEpsilonStep());
153 fieldManager->SetMaximumEpsilonStep(globals->MaximumEpsilonStep());
154 fieldManager->SetDeltaOneStep(globals->DeltaOneStep());
155}
156
158{
160 fieldManager->SetDeltaIntersection(globals->DeltaIntersection());
161 fieldManager->SetMinimumEpsilonStep(globals->MinimumEpsilonStepThin());
162 fieldManager->SetMaximumEpsilonStep(globals->MaximumEpsilonStepThin());
163 fieldManager->SetDeltaOneStep(globals->DeltaOneStep());
164}
All info required to build complete field of any type.
G4double ChordStepMinimum() const
Accessor.
G4bool IsThin() const
Accessor.
G4UserLimits * UserLimits() const
Accessor.
Wrapper for Geant4's G4FieldManager to distinguish primaries.
void AttachToVolume(G4LogicalVolume *volume, G4bool penetrateToDaughterVolumes=true) const
Interface to easily attach to logical volume.
void SetFieldManagerOptions()
Field manager settings.
~BDSFieldObjects()
Destructor deletes all objects apart from the magnetic field.
G4FieldManager * fieldManager
Field manager.
G4Field * field
Reference to field this instance is based on.
void SetFieldManagerOptionsThin()
Field manager settings for thin elements.
const BDSFieldInfo * info
The complete information required to build this field.
G4MagIntegratorStepper * magIntegratorStepper
Stepper, selectable depending on smoothness of the field etc.
G4ChordFinder * chordFinder
Chord manager.
G4EquationOfMotion * equationOfMotion
Equation of motion, typically G4Mag_UsualEqRhs instance.
void AttachUserLimitsToVolume(G4LogicalVolume *volume, G4UserLimits *userLimits, G4bool penetrateToDaughterVolumes=true) const
G4MagInt_Driver * magIntDriver
EM field integrator driver (optional) - only for EM fields.
A class that holds global options and constants.
static BDSGlobalConstants * Instance()
Access method.