gfields
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 #include "G4MagIntegratorStepper.hh"
7 
8 // gemc
9 #include "gfactory.h"
10 #include "goptions.h"
11 #include "gfieldConventions.h"
12 
21 
22  std::string name;
23  std::string integration_stepper;
24  double minimum_step;
25  std::string type;
26 
27  map <std::string, string> field_parameters;
28 
34  void add_map_parameter(std::string key, std::string value) {
35  field_parameters[key] = value;
36  }
37 
42  string gfieldPluginName() {
43  return "gfield" + type + "Factory";
44  }
45 
46  // overload << to print the field definition
47  friend std::ostream &operator<<(std::ostream &stream, GFieldDefinition gfd) {
48  stream << " > Field name: " << gfd.name << std::endl;
49  stream << " - integration stepper " << gfd.integration_stepper << std::endl;
50  stream << " - minimum step " << gfd.minimum_step << " mm" << std::endl;
51  stream << " - type " << gfd.type << std::endl;
52 
53  // print the field parameters
54  // align the keys to the left
55  for (auto &field_parameter: gfd.field_parameters) {
56  stream << " - " << std::left << std::setw(21) << field_parameter.first << field_parameter.second << std::endl;
57  }
58 
59  return stream;
60  }
61 };
62 
66 class GField : public G4MagneticField {
67 
68 public:
72  GField() = default;
73 
77  virtual ~GField() = default;
78 
84  virtual void GetFieldValue(const double x[3], double *bfield) const = 0;
85 
90  G4FieldManager *create_FieldManager();
91 
97  gfield_definitions = gfd;
98  }
99 
104  void gFLogMessage(std::string message) {
105  gLogMessage(GFIELDLOGHEADER + gfield_definitions.name + " " + message);
106  }
107 
108  int get_field_parameter_int(std::string key) {
109  return stoi(gfield_definitions.field_parameters[key]);
110  }
111  double get_field_parameter_double(std::string key) {
112  return stod(gfield_definitions.field_parameters[key]);
113  }
114 
115 private:
116 
117 
118  std::vector <std::string> SUPPORTED_STEPPERS = {
119  "G4DormandPrince745", "G4ClassicalRK4", "G4SimpleRunge", "G4HelixExplicitEuler",
120  "G4HelixImplicitEuler", "G4CashKarpRKF45", "G4RKG3_Stepper", "G4SimpleHeum",
121  "G4NystromRK4", "G4ImplicitEuler", "G4ExplicitEuler"
122  };
123 
124 
125 protected:
127 
128 public:
129 
130  static GField *instantiate(const dlhandle handle) {
131 
132  if (handle == nullptr) return nullptr;
133 
134  // must match the extern C declaration in the derived factories
135  void *maker = dlsym(handle, "GFieldFactory");
136 
137  if (maker == nullptr) return nullptr;
138 
139  typedef GField *(*fptr)();
140 
141  // static_cast not allowed here
142  // see http://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast
143  // need to run the DLL GMediaFactory function that returns the factory
144  fptr func = reinterpret_cast<fptr>(reinterpret_cast<void *>(maker));
145 
146  return func();
147  }
148 };
Abstract base class representing a magnetic field.
Definition: gfield.h:66
void gFLogMessage(std::string message)
Logs a message with the field context.
Definition: gfield.h:104
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:126
int get_field_parameter_int(std::string key)
Definition: gfield.h:108
G4FieldManager * create_FieldManager()
Creates the G4FieldManager for the field.
Definition: gfield.cc:21
GField()=default
Default constructor.
static GField * instantiate(const dlhandle handle)
Definition: gfield.h:130
virtual void load_field_definitions(GFieldDefinition gfd)
Sets the field definition for the field.
Definition: gfield.h:96
virtual ~GField()=default
Virtual destructor.
double get_field_parameter_double(std::string key)
Definition: gfield.h:111
Utility struct to load GFields from options.
Definition: gfield.h:16
GFieldDefinition()
Default constructor.
Definition: gfield.h:20
double minimum_step
Minimum step size for integration.
Definition: gfield.h:24
std::string name
Key in the gmagneto maps.
Definition: gfield.h:22
map< std::string, string > field_parameters
Field parameters as key-value pairs.
Definition: gfield.h:27
friend std::ostream & operator<<(std::ostream &stream, GFieldDefinition gfd)
Definition: gfield.h:47
void add_map_parameter(std::string key, std::string value)
Adds a parameter to the field parameters map.
Definition: gfield.h:34
std::string integration_stepper
Type of integration stepper.
Definition: gfield.h:23
string gfieldPluginName()
Gets the plugin name for the field.
Definition: gfield.h:42
std::string type
Type of the field.
Definition: gfield.h:25