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) Royal Holloway,
3University of London 2001 - 2024.
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 "BDSFieldManager.hh"
20#include "BDSGlobalConstants.hh"
21#include "BDSFieldInfo.hh"
22#include "BDSFieldObjects.hh"
23
24#include "G4ChordFinder.hh"
25#include "G4ElectroMagneticField.hh"
26#include "G4Field.hh"
27#include "G4FieldManager.hh"
28#include "G4LogicalVolume.hh"
29#include "G4MagIntegratorDriver.hh" // for G4MagInt_Driver
30#include "G4MagIntegratorStepper.hh"
31#include "G4MagneticField.hh"
32#include "G4Version.hh"
33
34#include <vector>
35
36BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
37 G4Field* fieldIn,
38 G4EquationOfMotion* equationOfMotionIn,
39 G4MagIntegratorStepper* magIntegratorStepperIn,
40 G4ChordFinder* chordFinderIn,
41 G4FieldManager* fieldManagerIn):
42 info(infoIn),
43 field(fieldIn),
44 equationOfMotion(equationOfMotionIn),
45 magIntegratorStepper(magIntegratorStepperIn),
46 chordFinder(chordFinderIn),
47 fieldManager(fieldManagerIn),
48 magIntDriver(nullptr)
49{;}
50
51BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
52 G4ElectroMagneticField* fieldIn,
53 G4EquationOfMotion* equationOfMotionIn,
54 G4MagIntegratorStepper* magIntegratorStepperIn):
55 info(infoIn),
56 field(fieldIn),
57 equationOfMotion(equationOfMotionIn),
58 magIntegratorStepper(magIntegratorStepperIn)
59{
60 G4double chordStepMinimum = info->ChordStepMinimum();
61 if (chordStepMinimum <= 0)
62 {chordStepMinimum = BDSGlobalConstants::Instance()->ChordStepMinimum();}
63
64 magIntDriver = new G4MagInt_Driver(chordStepMinimum,
66 magIntegratorStepper->GetNumberOfVariables());
67
68 chordFinder = new G4ChordFinder(magIntDriver);
69
70 // We use our custom field manager that is a thin wrapper for the Geant4 one
71 // that only identifies whether we have a primary track or not for BDSIntegratorMag
73 if (info->IsThin())
75 else
77}
78
79#if G4VERSION_NUMBER > 1049
80BDSFieldObjects::BDSFieldObjects(const BDSFieldInfo* infoIn,
81 G4MagneticField* fieldIn,
82 G4EquationOfMotion* equationOfMotionIn,
83 G4MagIntegratorStepper* magIntegratorStepperIn):
84 info(infoIn),
85 field(fieldIn),
86 equationOfMotion(equationOfMotionIn),
87 magIntegratorStepper(magIntegratorStepperIn)
88{
89 G4double chordStepMinimum = info->ChordStepMinimum();
90 if (chordStepMinimum <= 0)
91 {chordStepMinimum = BDSGlobalConstants::Instance()->ChordStepMinimum();}
92
93 magIntDriver = new G4MagInt_Driver(chordStepMinimum,
95 magIntegratorStepper->GetNumberOfVariables());
96
97 chordFinder = new G4ChordFinder(magIntDriver);
98 fieldManager = new G4FieldManager(field, chordFinder);
99
100 if (info->IsThin())
102 else
104}
105#endif
106
108{
109 delete field;
110 delete fieldManager;
111 delete chordFinder;
113 delete equationOfMotion;
114 //delete magIntDriver; // not needed since deleted by chordFinder
115}
116
117void BDSFieldObjects::AttachToVolume(G4LogicalVolume* volume,
118 G4bool penetrateToDaughterVolumes) const
119{
120 volume->SetFieldManager(fieldManager, penetrateToDaughterVolumes);
121 if (!info) // may not always exist
122 {return;}
123
124 // optionally attach user limits
125 auto ul = info->UserLimits();
126 if (ul)
127 {AttachUserLimitsToVolume(volume, ul, penetrateToDaughterVolumes);}
128}
129
130void BDSFieldObjects::AttachToVolume(const std::vector<G4LogicalVolume*>& volumes,
131 G4bool penetrateToDaughterVolumes) const
132{
133 for (auto volume : volumes)
134 {AttachToVolume(volume, penetrateToDaughterVolumes);}
135}
136
137void BDSFieldObjects::AttachUserLimitsToVolume(G4LogicalVolume* volume,
138 G4UserLimits* userLimits,
139 G4bool penetrateToDaughterVolumes) const
140{
141 volume->SetUserLimits(userLimits);
142 if (penetrateToDaughterVolumes)
143 {
144 for (G4int i = 0; i < (G4int)volume->GetNoDaughters(); i++)
145 {AttachUserLimitsToVolume(volume->GetDaughter(i)->GetLogicalVolume(), userLimits, penetrateToDaughterVolumes);}
146 }
147}
148
150{
152 fieldManager->SetDeltaIntersection(globals->DeltaIntersection());
153 fieldManager->SetMinimumEpsilonStep(globals->MinimumEpsilonStep());
154 fieldManager->SetMaximumEpsilonStep(globals->MaximumEpsilonStep());
155 fieldManager->SetDeltaOneStep(globals->DeltaOneStep());
156}
157
159{
161 fieldManager->SetDeltaIntersection(globals->DeltaIntersection());
162 fieldManager->SetMinimumEpsilonStep(globals->MinimumEpsilonStepThin());
163 fieldManager->SetMaximumEpsilonStep(globals->MaximumEpsilonStepThin());
164 fieldManager->SetDeltaOneStep(globals->DeltaOneStep());
165}
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.