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
22 GFieldDefinition() = default;
23
24 std::string name;
25 std::string integration_stepper;
26 double minimum_step;
27 std::string type;
28
29 std::map<std::string, std::string> field_parameters;
30
36 void add_map_parameter(const std::string& key, const std::string& value) { field_parameters[key] = value; }
37
42 std::string gfieldPluginName() const { return "gfield" + type + "Factory"; }
43
44 // overload << to print the field definition
45 friend std::ostream& operator<<(std::ostream& stream, GFieldDefinition gfd) {
46 stream << " > Field name: " << gfd.name << std::endl;
47 stream << " - integration stepper " << gfd.integration_stepper << std::endl;
48 stream << " - minimum step " << gfd.minimum_step << " mm" << std::endl;
49 stream << " - type " << gfd.type << std::endl;
50
51 // print the field parameters
52 // align the keys to the left
53 for (auto& field_parameter : gfd.field_parameters) { stream << " - " << std::left << std::setw(21) << field_parameter.first << field_parameter.second << std::endl; }
54
55 return stream;
56 }
57};
58
62class GField : public GBase<GField>, public G4MagneticField {
63
64public:
68 //GField() = default;
69 explicit GField(const std::shared_ptr<GOptions>& gopt) : GBase(gopt, GFIELD_LOGGER) {}
70
76 virtual void GetFieldValue(const double x[3], double* bfield) const = 0;
77
82 G4FieldManager* create_FieldManager();
83
89
90 int get_field_parameter_int(const std::string& key) { return stoi(gfield_definitions.field_parameters[key]); }
91 double get_field_parameter_double(const std::string& key) { return stod(gfield_definitions.field_parameters[key]); }
92
93 void set_loggers([[ maybe_unused ]] const std::shared_ptr<GOptions>& g) {
94 }
95
96private:
97 // TODO: make this list automatic
98 std::vector<std::string> SUPPORTED_STEPPERS = {
99 "G4DormandPrince745",
100 "G4ClassicalRK4",
101 "G4SimpleRunge",
102 "G4HelixExplicitEuler",
103 "G4HelixImplicitEuler",
104 "G4CashKarpRKF45",
105 "G4RKG3_Stepper",
106 "G4SimpleHeum",
107 "G4NystromRK4",
108 "G4ImplicitEuler",
109 "G4ExplicitEuler"
110 };
111
112protected:
114
115public:
116 static GField* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
117 if (!h) return nullptr;
118 using fptr = GField* (*)(std::shared_ptr<GOptions>);
119
120 // Must match the extern "C" declaration in the derived factories.
121 auto sym = dlsym(h, "GFieldFactory");
122 if (!sym) return nullptr;
123
124 auto func = reinterpret_cast<fptr>(sym);
125 return func(g);
126 }
127
128};
Abstract base class representing a magnetic field.
Definition gfield.h:62
virtual void GetFieldValue(const double x[3], double *bfield) const =0
Pure virtual function to get the magnetic field value.
GFieldDefinition gfield_definitions
Definition gfield.h:113
G4FieldManager * create_FieldManager()
Creates the G4FieldManager for the field.
Definition gfield.cc:23
GField(const std::shared_ptr< GOptions > &gopt)
Default constructor.
Definition gfield.h:69
void set_loggers(const std::shared_ptr< GOptions > &g)
Definition gfield.h:93
virtual void load_field_definitions(GFieldDefinition gfd)
Sets the field definition for the field.
Definition gfield.h:88
double get_field_parameter_double(const std::string &key)
Definition gfield.h:91
int get_field_parameter_int(const std::string &key)
Definition gfield.h:90
static GField * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Definition gfield.h:116
constexpr const char * GFIELD_LOGGER
Definition gfield.h:12
constexpr const char * GMAGNETO_LOGGER
Definition gfield.h:13
Utility struct to load GFields from options.
Definition gfield.h:18
std::string gfieldPluginName() const
Gets the plugin name for the field.
Definition gfield.h:42
double minimum_step
Minimum step size for integration.
Definition gfield.h:26
friend std::ostream & operator<<(std::ostream &stream, GFieldDefinition gfd)
Definition gfield.h:45
std::string name
Key in the gmagneto maps.
Definition gfield.h:24
std::map< std::string, std::string > field_parameters
Field parameters as key-value pairs.
Definition gfield.h:29
GFieldDefinition()=default
Default constructor.
void add_map_parameter(const std::string &key, const std::string &value)
Adds a parameter to the field parameters map.
Definition gfield.h:36
std::string integration_stepper
Type of integration stepper.
Definition gfield.h:25
std::string type
Type of the field.
Definition gfield.h:27