Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4FieldBuilder.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4FieldBuilder
27//
28// Class description:
29//
30// The manager class for building magnetic or other fields
31// using the configuration in field parameters.
32//
33// Purpose: Provide a single 'place' to configure field & integration
34//
35// - It can configure a global field, and field(s) local to a (logical) volume
36// - The parameter values can be configured by the user (else use a default)
37// - They can be set/changed via a messenger provided or in the code
38// of the user detector construciton
39// - It retains ownership of the following object(s):
40// field parameters and field setups, field
41//
42// Note MT: an object of the builder class should be created on master only
43// (in DetectorConstruction constructor)
44// The functions SetGlobal/LocalField and ConstructFieldSetup should
45// be called on workers (in DetectorConstruction::ConstructSDandField )
46//
47// This design/implementation covers the most common use cases.
48// It cannot be used to create some complex setups such as
49// - equations templated on the field type,
50// - steppers/drivers templated on the equation and field types.
51
52// Author: Ivana Hrivnacova (IJCLab, Orsay), 2024.
53// --------------------------------------------------------------------
54#ifndef G4FIELDBUILDER_HH
55#define G4FIELDBUILDER_HH
56
57#include "G4Cache.hh"
58#include "G4FieldParameters.hh"
59#include "globals.hh"
60
61#include <vector>
62
63class G4Field;
65class G4FieldSetup;
66class G4LogicalVolume;
69
70/**
71 * @brief G4FieldBuilder is a singleton manager class for building magnetic
72 * or other fields, using the configuration in G4FieldParameters.
73 */
74
76{
77 public:
78
79 /**
80 * Destructor. Deletes associated messenger.
81 */
83
84 /**
85 * Copy constructor and assignment operator not allowed.
86 */
87 G4FieldBuilder(const G4FieldBuilder& right) = delete;
88 G4FieldBuilder& operator=(const G4FieldBuilder& right) = delete;
89
90 // Static access methods
91
92 /**
93 * Creates the class instance, if it does not exist; simply returns it
94 * on the next calls.
95 * @returns A pointer to the singleton instance.
96 */
97 static G4FieldBuilder* Instance();
98
99 /**
100 * Tells if the singleton instance exists.
101 * @returns true if the singleton instance exists.
102 */
103 static G4bool IsInstance();
104
105 // Functions for constructing field setup
106
107 /**
108 * Creates the local magnetic field parameters (configuration) which can
109 * be then configured by the user via UI commands.
110 * The parameters are used in geometry only if a local magnetic field is
111 * associated with the volumes with the given name.
112 * @param[in] fieldVolName Volume name.
113 * @returns A pointer to the field parameters.
114 */
116
117 /**
118 * Constructs the setup for all registered fields.
119 */
120 void ConstructFieldSetup();
121
122 /**
123 * Updates the magnetic field. It must be called if the field parameters
124 * were changed in other than PreInit> phase.
125 */
126 void UpdateField();
127
128 /**
129 * Reinitialises if geometry has been modified. This method is called
130 * by G4RunManager during ReinitializeGeometry().
131 */
132 void Reinitialize();
133
134 // Set methods
135
136 /**
137 * The default field type is set to "kMagnetic". This method should be
138 * called for other than magnetic field, in order to update the default
139 * equation and stepper types.
140 * @param[in] fieldType The field type-ID.
141 */
142 void SetFieldType(G4FieldType fieldType);
143
144 /**
145 * Sets or resets the global field. It updates the field objects,
146 * if the field was already constructed.
147 * @param[in] field Pointer to the global field.
148 * @param[in] warn If flag is true, issues a warning if the previous
149 * field is deleted.
150 */
151 void SetGlobalField(G4Field* field, G4bool warn=false);
152
153 /**
154 * Registers the local field in the map. It updates the field objects,
155 * if the field was already constructed.
156 * The field is propagated to all volume daughters regardless if they
157 * have already assigned a field manager or not.
158 * When multiple local fields are defined (by calling this method multiple
159 * times), they will be applied in the order they were set.
160 * @param[in] field Pointer to the global field.
161 * @param[in] lv Pointer to the logical volume.
162 * @param[in] warn If flag is true, issues a warning if the previous
163 * field is deleted.
164 */
165 void SetLocalField(G4Field* field, G4LogicalVolume* lv, G4bool warn=false);
166
167 /**
168 * Sets the user equation of motion.
169 * @param[in] equation Pointer to the equation of motion algorithm.
170 * @param[in] volumeName Optional volume name.
171 */
173 const G4String& volumeName = "");
174
175 /**
176 * Sets the user stepper.
177 * @param[in] stepper Pointer to the stepper algorithm.
178 * @param[in] volumeName Optional volume name.
179 */
181 const G4String& volumeName = "");
182
183 /**
184 * Sets the verbosity level.
185 */
186 void SetVerboseLevel(G4int value);
187
188 // Get methods
189
190 /**
191 * Gets a pointer to the field parameters with the given 'volumeName'.
192 * Return global field parameters, if volume name is empty.
193 */
194 G4FieldParameters* GetFieldParameters(const G4String& volumeName = "") const;
195
196 private:
197
198 /**
199 * Private constructor.
200 */
202
203 /**
204 * Gets the pointer to field parameters with the given 'volumeName'
205 * or creates them if they do not exist yet.
206 */
207 G4FieldParameters* GetOrCreateFieldParameters(const G4String& volumeName);
208
209 /**
210 * Gets the pointer to the field setup with the given logical volume.
211 */
212 G4FieldSetup* GetFieldSetup(G4LogicalVolume* lv);
213
214 /**
215 * Creates magnetic, electromagnetic or gravity field setup.
216 */
217 void CreateFieldSetup(G4Field* field, G4FieldParameters* fieldParameters,
218 G4LogicalVolume* lv);
219
220 /**
221 * Constructs global magnetic field setup.
222 */
223 void ConstructGlobalField();
224
225 /**
226 * Constructs local magnetic field setups from the local fields map.
227 */
228 void ConstructLocalFields();
229
230 /**
231 * Updates all field setups.
232 */
233 void UpdateFieldSetups();
234
235 /**
236 * Helper methods.
237 */
238 inline std::vector<G4FieldSetup*>& GetFieldSetups();
239 inline std::vector<std::pair<G4LogicalVolume*, G4Field*>>& GetLocalFields();
240
241 private: // Data members
242
243 /** Information if an instance exists. */
244 inline static G4ThreadLocal G4bool fgIsInstance { false };
245
246 /** Messenger for this class. */
247 G4FieldBuilderMessenger* fMessenger = nullptr;
248
249 /** Field parameters. */
250 std::vector<G4FieldParameters*> fFieldParameters;
251
252 /** Field setups. */
254
255 /** Registered global field. */
256 static G4ThreadLocal G4Field* fGlobalField;
257
258 /** Registered local fields. */
260
261 /** Info if field objects were constructed. */
262 static G4ThreadLocal G4bool fIsConstructed;
263
264 /** Verbose level. */
265 G4int fVerboseLevel = 1;
266};
267
268// Inline methods -------------------------------------------------------------
269
270inline std::vector<G4FieldSetup*>& G4FieldBuilder::GetFieldSetups()
271{
272 // Return reference to field setups from G4Cache
273 return *fFieldSetups.Get();
274}
275
276inline std::vector<std::pair<G4LogicalVolume*, G4Field*>>& G4FieldBuilder::GetLocalFields()
277{
278 // Return reference to local fields map from G4Cache
279 return *fLocalFields.Get();
280}
281
282#endif
G4FieldType
G4FieldType defines the available fields in Geant4.
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4EquationOfMotion is the abstract base class for the right hand size of the equation of motion of a ...
G4FieldBuilderMessenger is messenger class that defines commands for G4FieldBuilder.
G4FieldParameters * CreateFieldParameters(const G4String &fieldVolName)
G4FieldBuilder & operator=(const G4FieldBuilder &right)=delete
void SetUserEquationOfMotion(G4EquationOfMotion *equation, const G4String &volumeName="")
void SetUserStepper(G4MagIntegratorStepper *stepper, const G4String &volumeName="")
G4FieldParameters * GetFieldParameters(const G4String &volumeName="") const
void SetLocalField(G4Field *field, G4LogicalVolume *lv, G4bool warn=false)
static G4bool IsInstance()
static G4FieldBuilder * Instance()
G4FieldBuilder(const G4FieldBuilder &right)=delete
void SetFieldType(G4FieldType fieldType)
void SetVerboseLevel(G4int value)
void SetGlobalField(G4Field *field, G4bool warn=false)
G4FieldParameters defines the type of equation of motion of a particle in a field and the integration...
G4FieldSetup is a class for constructing magnetic, electromagnetic and gravity fields which strength ...
G4Field is the abstract class for any kind of field. It allows any kind of field (vector,...
Definition G4Field.hh:67
G4LogicalVolume represents a leaf node or unpositioned subtree in the geometry hierarchy....
G4MagIntegratorStepper is an abstract base class for integrator of particle's equation of motion,...
#define G4ThreadLocal
Definition tls.hh:77