Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4FieldBuilder Class Reference

G4FieldBuilder is a singleton manager class for building magnetic or other fields, using the configuration in G4FieldParameters. More...

#include <G4FieldBuilder.hh>

Public Member Functions

 ~G4FieldBuilder ()
 G4FieldBuilder (const G4FieldBuilder &right)=delete
G4FieldBuilderoperator= (const G4FieldBuilder &right)=delete
G4FieldParametersCreateFieldParameters (const G4String &fieldVolName)
void ConstructFieldSetup ()
void UpdateField ()
void Reinitialize ()
void SetFieldType (G4FieldType fieldType)
void SetGlobalField (G4Field *field, G4bool warn=false)
void SetLocalField (G4Field *field, G4LogicalVolume *lv, G4bool warn=false)
void SetUserEquationOfMotion (G4EquationOfMotion *equation, const G4String &volumeName="")
void SetUserStepper (G4MagIntegratorStepper *stepper, const G4String &volumeName="")
void SetVerboseLevel (G4int value)
G4FieldParametersGetFieldParameters (const G4String &volumeName="") const

Static Public Member Functions

static G4FieldBuilderInstance ()
static G4bool IsInstance ()

Detailed Description

G4FieldBuilder is a singleton manager class for building magnetic or other fields, using the configuration in G4FieldParameters.

Definition at line 75 of file G4FieldBuilder.hh.

Constructor & Destructor Documentation

◆ ~G4FieldBuilder()

G4FieldBuilder::~G4FieldBuilder ( )

Destructor. Deletes associated messenger.

Definition at line 63 of file G4FieldBuilder.cc.

64{
65 // Destructor
66
67 delete fMessenger;
68
69 for (auto parameters : fFieldParameters)
70 {
71 delete parameters;
72 }
73
74 for (auto setup : GetFieldSetups())
75 {
76 delete setup;
77 }
78
79 fgIsInstance = false;
80
81 // magnetic field objects are deleted via G4 kernel
82}

◆ G4FieldBuilder()

G4FieldBuilder::G4FieldBuilder ( const G4FieldBuilder & right)
delete

Copy constructor and assignment operator not allowed.

Referenced by G4FieldBuilder(), Instance(), and operator=().

Member Function Documentation

◆ ConstructFieldSetup()

void G4FieldBuilder::ConstructFieldSetup ( )

Constructs the setup for all registered fields.

Definition at line 247 of file G4FieldBuilder.cc.

248{
249 // Construct setups for all registered fields.
250
251 if (fVerboseLevel > 1)
252 {
253 G4cout << "G4FieldBuilder::ConstructField" << G4endl;
254 }
255
256 if (fIsConstructed)
257 {
259 "G4FieldBuilder::ConstructField:", "GeomFieldParameters0001",
260 JustWarning, "Field was already constructed.");
261 return;
262 }
263
264 ConstructGlobalField();
265 ConstructLocalFields();
266
267 UpdateFieldSetups();
268
269 fIsConstructed = true;
270}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout

◆ CreateFieldParameters()

G4FieldParameters * G4FieldBuilder::CreateFieldParameters ( const G4String & fieldVolName)

Creates the local magnetic field parameters (configuration) which can be then configured by the user via UI commands. The parameters are used in geometry only if a local magnetic field is associated with the volumes with the given name.

Parameters
[in]fieldVolNameVolume name.
Returns
A pointer to the field parameters.

Definition at line 232 of file G4FieldBuilder.cc.

234{
235 // Create local magnetic field parameters (configuration) which can be then
236 // configured by the user via UI commands.
237 // The parameters are used in geometry only if a local magnetic field is
238 // associated with the volumes with the given name.
239
240 auto fieldParameters = new G4FieldParameters(fieldVolName);
241 fFieldParameters.push_back(fieldParameters);
242
243 return fieldParameters;
244}

◆ GetFieldParameters()

G4FieldParameters * G4FieldBuilder::GetFieldParameters ( const G4String & volumeName = "") const

Gets a pointer to the field parameters with the given 'volumeName'. Return global field parameters, if volume name is empty.

Definition at line 494 of file G4FieldBuilder.cc.

496{
497 // Get field parameters with the given volumeName.
498 // Return global field parameters, if volume name is empty.
499
500 // Get user field parameters
501 for (auto fieldParameters : fFieldParameters)
502 {
503 if (fieldParameters->GetVolumeName() == volumeName)
504 {
505 return fieldParameters;
506 }
507 }
508
510 "G4FieldBuilder::GetFieldParameters:", "GeomFieldParameters0001",
511 JustWarning, "Field parameters not found.");
512 return nullptr;
513}

◆ Instance()

G4FieldBuilder * G4FieldBuilder::Instance ( )
static

Creates the class instance, if it does not exist; simply returns it on the next calls.

Returns
A pointer to the singleton instance.

Definition at line 44 of file G4FieldBuilder.cc.

45{
47 fgIsInstance = true;
48 return &instance;
49}
G4TemplateRNGHelper< G4long > * G4TemplateRNGHelper< G4long >::instance
G4FieldBuilder(const G4FieldBuilder &right)=delete

Referenced by G4RunManager::ReinitializeGeometry().

◆ IsInstance()

G4bool G4FieldBuilder::IsInstance ( )
static

Tells if the singleton instance exists.

Returns
true if the singleton instance exists.

Definition at line 85 of file G4FieldBuilder.cc.

86{
87 // Return the information if an instance exists
88
89 return fgIsInstance;
90}

Referenced by G4RunManager::ReinitializeGeometry().

◆ operator=()

G4FieldBuilder & G4FieldBuilder::operator= ( const G4FieldBuilder & right)
delete

◆ Reinitialize()

void G4FieldBuilder::Reinitialize ( )

Reinitialises if geometry has been modified. This method is called by G4RunManager during ReinitializeGeometry().

Definition at line 297 of file G4FieldBuilder.cc.

298{
299 // Reinitialize if geometry has been modified.
300 // This function is called by G4RunManager during ReinitializeGeometry()
301
302 if (fVerboseLevel > 1)
303 {
304 G4cout << "G4FieldBuilder::Reinitialize" << G4endl;
305 }
306
307 // Delete global field
308 delete fGlobalField;
309 fGlobalField = nullptr;
310
311 // Delete local fields if defined
312 if (fLocalFields.Get() != nullptr)
313 {
314 for (auto vectorElement : GetLocalFields())
315 {
316 delete vectorElement.second;
317 }
318 // Clear local fields map
319 GetLocalFields().clear();
320 }
321
322 // Clear field setups if defined
323 if (fFieldSetups.Get() != nullptr)
324 {
325 for (auto fieldSetup : GetFieldSetups())
326 {
327 fieldSetup->SetG4Field(nullptr);
328 fieldSetup->Clear();
329 }
330 }
331
332 fIsConstructed = false;
333
334 if (fVerboseLevel > 1)
335 {
336 G4cout << "End of G4FieldBuilder::Reinitialize" << G4endl;
337 }
338}

Referenced by G4RunManager::ReinitializeGeometry().

◆ SetFieldType()

void G4FieldBuilder::SetFieldType ( G4FieldType fieldType)

The default field type is set to "kMagnetic". This method should be called for other than magnetic field, in order to update the default equation and stepper types.

Parameters
[in]fieldTypeThe field type-ID.

Definition at line 341 of file G4FieldBuilder.cc.

342{
343// Default field type is set to kMagnetic;
344// this function should be called for other than magnetic field
345// in order to update the default equation and stepper types.
346
347 if (fIsConstructed)
348 {
350 "G4FieldBuilder::SetFieldType:", "GeomFieldParameters0001",
351 JustWarning, "Field was already constructed.");
352 return;
353 }
354
355 fFieldParameters[0]->SetFieldType(fieldType);
356
357 // change default equation and stepper if other than magnetic field
358 if (fieldType == kElectroMagnetic)
359 {
360 fFieldParameters[0]->SetEquationType(kEqElectroMagnetic);
361 fFieldParameters[0]->SetStepperType(kClassicalRK4);
362 }
363}
@ kEqElectroMagnetic
@ kElectroMagnetic
electromagnetic field
@ kClassicalRK4
G4ClassicalRK4.

◆ SetGlobalField()

void G4FieldBuilder::SetGlobalField ( G4Field * field,
G4bool warn = false )

Sets or resets the global field. It updates the field objects, if the field was already constructed.

Parameters
[in]fieldPointer to the global field.
[in]warnIf flag is true, issues a warning if the previous field is deleted.

Definition at line 366 of file G4FieldBuilder.cc.

367{
368 // Set or reset the global field.
369 // Update field objects, if the field was already constructed.
370 // If warn, issue a warning if the previous field is deleted.
371
372 if (fGlobalField != nullptr && warn)
373 {
375 "G4FieldBuilder::SetGlobalField:", "GeomFieldParameters0001",
376 JustWarning, "The global field already exists, it will be deleted.");
377 }
378 delete fGlobalField;
379 fGlobalField = field;
380
381 if (fIsConstructed)
382 {
383 // update the global field objects if already constructed
384 GetFieldSetups()[0]->SetG4Field(field);
385 GetFieldSetups()[0]->Update();
386 }
387}

◆ SetLocalField()

void G4FieldBuilder::SetLocalField ( G4Field * field,
G4LogicalVolume * lv,
G4bool warn = false )

Registers the local field in the map. It updates the field objects, if the field was already constructed. The field is propagated to all volume daughters regardless if they have already assigned a field manager or not. When multiple local fields are defined (by calling this method multiple times), they will be applied in the order they were set.

Parameters
[in]fieldPointer to the global field.
[in]lvPointer to the logical volume.
[in]warnIf flag is true, issues a warning if the previous field is deleted.

Definition at line 390 of file G4FieldBuilder.cc.

392{
393 // Register the local field in the map.
394 // Update field objects, if the field was already constructed.
395 // If warn, issue a warning if the previous field is deleted.
396
397 if (lv == nullptr)
398 {
399 G4cerr << "Cannot register local field without Logical volume." << G4endl;
400 return;
401 }
402
403 if (fLocalFields.Get() == nullptr)
404 {
405 auto localFields = new std::vector<std::pair<G4LogicalVolume*, G4Field*>>();
406 fLocalFields.Put(localFields);
407 }
408
409 auto it = GetLocalFields().begin();
410 for (it = GetLocalFields().begin(); it != GetLocalFields().end(); ++it)
411 {
412 if (it->first == lv) { break; }
413 }
414
415 if (it != GetLocalFields().end())
416 {
417 // replaced field if already in the map
418 if (warn)
419 {
421 descr << "Logical volume " << lv->GetName() << " has already field."
422 " It will be deleted.";
424 "G4FieldBuilder::SetLocalField:", "GeomFieldParameters0001",
425 JustWarning, descr);
426 }
427 delete it->second;
428 it->second = field;
429 }
430 else
431 {
432 // register field in the map
433 GetLocalFields().emplace_back(lv,field);
434 }
435
436 if (fIsConstructed) {
437 // update this local field objects if already constructed
438 auto fieldSetup = GetFieldSetup(lv);
439 if (fieldSetup == nullptr)
440 {
442 "G4FieldBuilder::SetLocalField:", "GeomFieldParameters0001",
443 JustWarning, "Cannot get field setup for a local field.");
444 return;
445 }
446 fieldSetup->SetG4Field(field);
447 fieldSetup->Update();
448 }
449}
std::ostringstream G4ExceptionDescription
G4GLOB_DLL std::ostream G4cerr
const G4String & GetName() const

◆ SetUserEquationOfMotion()

void G4FieldBuilder::SetUserEquationOfMotion ( G4EquationOfMotion * equation,
const G4String & volumeName = "" )

Sets the user equation of motion.

Parameters
[in]equationPointer to the equation of motion algorithm.
[in]volumeNameOptional volume name.

Definition at line 452 of file G4FieldBuilder.cc.

454{
455 // Set user equation of motion
456
457 if (volumeName.empty())
458 {
459 // global field
460 fFieldParameters[0]->SetUserEquationOfMotion(equation);
461 }
462 else
463 {
464 // local field
465 // Get or create user field parameters
466 G4FieldParameters* fieldParameters =
467 GetOrCreateFieldParameters(volumeName);
468 fieldParameters->SetUserEquationOfMotion(equation);
469 }
470}
void SetUserEquationOfMotion(G4EquationOfMotion *equation)

◆ SetUserStepper()

void G4FieldBuilder::SetUserStepper ( G4MagIntegratorStepper * stepper,
const G4String & volumeName = "" )

Sets the user stepper.

Parameters
[in]stepperPointer to the stepper algorithm.
[in]volumeNameOptional volume name.

Definition at line 473 of file G4FieldBuilder.cc.

475{
476 // Set user stepper
477
478 if (volumeName.empty())
479 {
480 // global field
481 fFieldParameters[0]->SetUserStepper(stepper);
482 }
483 else
484 {
485 // local field
486 // Get or create user field parameters
487 G4FieldParameters* fieldParameters =
488 GetOrCreateFieldParameters(volumeName);
489 fieldParameters->SetUserStepper(stepper);
490 }
491}
void SetUserStepper(G4MagIntegratorStepper *stepper)

◆ SetVerboseLevel()

void G4FieldBuilder::SetVerboseLevel ( G4int value)

Sets the verbosity level.

Definition at line 97 of file G4FieldBuilder.cc.

98{
99 // Set verbose level
100
101 fVerboseLevel = value;
102}

◆ UpdateField()

void G4FieldBuilder::UpdateField ( )

Updates the magnetic field. It must be called if the field parameters were changed in other than PreInit> phase.

Definition at line 273 of file G4FieldBuilder.cc.

274{
275 // Update magnetic field.
276 // This function must be called if the field parameters were changed
277 // in other than PreInit> phase.
278
279 if (fFieldSetups.Get() == nullptr)
280 {
282 "G4FieldBuilder::UpdateField", "GeomFieldParameters0001",
283 JustWarning, "No field setup is defined.");
284 return;
285 }
286
287 if (fVerboseLevel > 1)
288 {
289 G4cout << "G4FieldBuilder::UpdateField" << G4endl;
290 }
291
292 // Update the objects defined in G4FieldSetup's
293 UpdateFieldSetups();
294}

The documentation for this class was generated from the following files: