gfields
Loading...
Searching...
No Matches
gfield.h
Go to the documentation of this file.
1#pragma once
2
3// G4 headers
4#include "G4MagneticField.hh"
5#include "G4FieldManager.hh"
6
7// gemc
8#include "gfactory.h"
9#include "gbase.h"
10
11
12constexpr const char* GFIELD_LOGGER = "gfield";
13constexpr const char* GMAGNETO_LOGGER = "gmagneto";
14
30 GFieldDefinition() = default;
31
33 std::string name;
34
37
39 double minimum_step = 0.0;
40
42 std::string type;
43
45 std::map<std::string, std::string> field_parameters;
46
52 void add_map_parameter(const std::string& key, const std::string& value) { field_parameters[key] = value; }
53
62 std::string gfieldPluginName() const { return "gfield" + type + "Factory"; }
63
72 friend std::ostream& operator<<(std::ostream& stream, GFieldDefinition gfd) {
73 stream << " > Field name: " << gfd.name << std::endl;
74 stream << " - integration stepper " << gfd.integration_stepper << std::endl;
75 stream << " - minimum step " << gfd.minimum_step << " mm" << std::endl;
76 stream << " - type " << gfd.type << std::endl;
77
78 // Print the field parameters, aligning keys for readability.
79 for (auto& field_parameter : gfd.field_parameters) {
80 stream << " - " << std::left << std::setw(21) << field_parameter.first << field_parameter.second << std::endl;
81 }
82
83 return stream;
84 }
85};
86
99class GField : public GBase<GField>, public G4MagneticField {
100
101public:
106 explicit GField(const std::shared_ptr<GOptions>& gopt) : GBase(gopt, GFIELD_LOGGER) {}
107
115 virtual void GetFieldValue(const double x[3], double* bfield) const = 0;
116
129 G4FieldManager* create_FieldManager();
130
138
144 int get_field_parameter_int(const std::string& key) { return stoi(gfield_definitions.field_parameters[key]); }
145
151 double get_field_parameter_double(const std::string& key) { return stod(gfield_definitions.field_parameters[key]); }
152
159 void set_loggers([[ maybe_unused ]] const std::shared_ptr<GOptions>& g) {
160 }
161
162private:
169 std::vector<std::string> SUPPORTED_STEPPERS = {
170 "G4DormandPrince745",
171 "G4ClassicalRK4",
172 "G4SimpleRunge",
173 "G4HelixExplicitEuler",
174 "G4HelixImplicitEuler",
175 "G4CashKarpRKF45",
176 "G4RKG3_Stepper",
177 "G4SimpleHeum",
178 "G4NystromRK4",
179 "G4ImplicitEuler",
180 "G4ExplicitEuler"
181 };
182
183protected:
186
187public:
197 static GField* instantiate(const dlhandle h, const std::shared_ptr<GOptions> g) {
198 if (!h) return nullptr;
199 using fptr = GField* (*)(const std::shared_ptr<GOptions>&);
200
201 // Must match the extern "C" declaration in the derived factories.
202 auto sym = dlsym(h, "GFieldFactory");
203 if (!sym) return nullptr;
204
205 auto func = reinterpret_cast<fptr>(sym);
206 return func(g);
207 }
208};
Abstract base class representing a magnetic field.
Definition gfield.h:99
virtual void GetFieldValue(const double x[3], double *bfield) const =0
Compute the magnetic field vector at a given position.
GFieldDefinition gfield_definitions
Stored field definition used for configuration and logging.
Definition gfield.h:185
static GField * instantiate(const dlhandle h, const std::shared_ptr< GOptions > g)
Instantiate a field object from a plugin handle.
Definition gfield.h:197
G4FieldManager * create_FieldManager()
Create a G4FieldManager configured for this field.
Definition gfield.cc:24
GField(const std::shared_ptr< GOptions > &gopt)
Construct the field base with the provided options (for logging and configuration access).
Definition gfield.h:106
void set_loggers(const std::shared_ptr< GOptions > &g)
Hook for configuring module loggers from options.
Definition gfield.h:159
virtual void load_field_definitions(GFieldDefinition gfd)
Store the field definition used to configure this field instance.
Definition gfield.h:137
double get_field_parameter_double(const std::string &key)
Convenience accessor for floating-point parameters stored in field_parameters.
Definition gfield.h:151
int get_field_parameter_int(const std::string &key)
Convenience accessor for integer-valued parameters stored in field_parameters.
Definition gfield.h:144
void * dlhandle
constexpr const char * GFIELD_LOGGER
Definition gfield.h:12
constexpr const char * GMAGNETO_LOGGER
Definition gfield.h:13
Lightweight configuration carrier used to load and configure a GField plugin.
Definition gfield.h:26
std::string gfieldPluginName() const
Derive the plugin name for the field definition.
Definition gfield.h:62
double minimum_step
Minimum step size used when constructing the G4ChordFinder (Geant4 length units).
Definition gfield.h:39
friend std::ostream & operator<<(std::ostream &stream, GFieldDefinition gfd)
Stream formatter used for logging/debug output.
Definition gfield.h:72
std::string name
Field name key used by GMagneto maps.
Definition gfield.h:33
std::map< std::string, std::string > field_parameters
Field parameters stored as string expressions (often including unit expressions).
Definition gfield.h:45
GFieldDefinition()=default
Default constructor.
void add_map_parameter(const std::string &key, const std::string &value)
Add or overwrite a parameter in the field-parameter map.
Definition gfield.h:52
std::string integration_stepper
Integration stepper name (string) used when creating the G4ChordFinder.
Definition gfield.h:36
std::string type
Field type discriminator used to derive the plugin factory name (e.g. "multipoles").
Definition gfield.h:42