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 <gemc/gfactory/gfactory.h>
9#include <gemc/gbase/gbase.h>
10#include <gemc/guts/gutilities.h>
11
12
13constexpr const char* GFIELD_LOGGER = "gfield";
14constexpr const char* GMAGNETO_LOGGER = "gmagneto";
15
31 GFieldDefinition() = default;
32
34 std::string name;
35
38
40 double minimum_step = 0.0;
41
43 std::string type;
44
48 std::string config_dir;
49
51 std::map<std::string, std::string> field_parameters;
52
58 void add_map_parameter(const std::string& key, const std::string& value) { field_parameters[key] = value; }
59
68 std::string gfieldPluginName() const { return "gfield" + type + "Factory"; }
69
78 friend std::ostream& operator<<(std::ostream& stream, GFieldDefinition gfd) {
79 stream << " > Field name: " << gfd.name << std::endl;
80 stream << " - integration stepper " << gfd.integration_stepper << std::endl;
81 stream << " - minimum step " << gfd.minimum_step << " mm" << std::endl;
82 stream << " - type " << gfd.type << std::endl;
83
84 // Print the field parameters, aligning keys for readability.
85 for (auto& field_parameter : gfd.field_parameters) {
86 stream << " - " << std::left << std::setw(21) << field_parameter.first << field_parameter.second << std::endl;
87 }
88
89 return stream;
90 }
91};
92
105class GField : public GBase<GField>, public G4MagneticField {
106
107public:
112 explicit GField(const std::shared_ptr<GOptions>& gopt) : GBase(gopt, GFIELD_LOGGER) {}
113
121 virtual void GetFieldValue(const double x[3], double* bfield) const = 0;
122
135 G4FieldManager* create_FieldManager();
136
144
150 int get_field_parameter_int(const std::string& key) { return stoi(gfield_definitions.field_parameters[key]); }
151
158
165 void set_loggers([[ maybe_unused ]] const std::shared_ptr<GOptions>& g) {
166 }
167
168private:
175 std::vector<std::string> SUPPORTED_STEPPERS = {
176 "G4DormandPrince745",
177 "G4ClassicalRK4",
178 "G4SimpleRunge",
179 "G4HelixExplicitEuler",
180 "G4HelixImplicitEuler",
181 "G4CashKarpRKF45",
182 "G4RKG3_Stepper",
183 "G4SimpleHeum",
184 "G4NystromRK4",
185 "G4ImplicitEuler",
186 "G4ExplicitEuler"
187 };
188
189protected:
192
193public:
203 static GField* instantiate(const dlhandle h, const std::shared_ptr<GOptions> g) {
204 if (!h) return nullptr;
205 using fptr = GField* (*)(const std::shared_ptr<GOptions>&);
206
207 // Must match the extern "C" declaration in the derived factories.
208 auto sym = dlsym(h, "GFieldFactory");
209 if (!sym) return nullptr;
210
211 auto func = reinterpret_cast<fptr>(sym);
212 return func(g);
213 }
214};
Abstract base class representing a magnetic field.
Definition gfield.h:105
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:191
static GField * instantiate(const dlhandle h, const std::shared_ptr< GOptions > g)
Instantiate a field object from a plugin handle.
Definition gfield.h:203
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:112
void set_loggers(const std::shared_ptr< GOptions > &g)
Hook for configuring module loggers from options.
Definition gfield.h:165
virtual void load_field_definitions(GFieldDefinition gfd)
Store the field definition used to configure this field instance.
Definition gfield.h:143
double get_field_parameter_double(const std::string &key)
Convenience accessor for floating-point parameters stored in field_parameters.
Definition gfield.h:157
int get_field_parameter_int(const std::string &key)
Convenience accessor for integer-valued parameters stored in field_parameters.
Definition gfield.h:150
void * dlhandle
constexpr const char * GFIELD_LOGGER
Definition gfield.h:13
constexpr const char * GMAGNETO_LOGGER
Definition gfield.h:14
double getG4Number(const string &v, bool warnIfNotUnit=false)
Lightweight configuration carrier used to load and configure a GField plugin.
Definition gfield.h:27
std::string config_dir
Definition gfield.h:48
std::string gfieldPluginName() const
Derive the plugin name for the field definition.
Definition gfield.h:68
double minimum_step
Minimum step size used when constructing the G4ChordFinder (Geant4 length units).
Definition gfield.h:40
friend std::ostream & operator<<(std::ostream &stream, GFieldDefinition gfd)
Stream formatter used for logging/debug output.
Definition gfield.h:78
std::string name
Field name key used by GMagneto maps.
Definition gfield.h:34
std::map< std::string, std::string > field_parameters
Field parameters stored as string expressions (often including unit expressions).
Definition gfield.h:51
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:58
std::string integration_stepper
Integration stepper name (string) used when creating the G4ChordFinder.
Definition gfield.h:37
std::string type
Field type discriminator used to derive the plugin factory name (e.g. "multipoles").
Definition gfield.h:43