gfields
gfield.h
Go to the documentation of this file.
1 #ifndef GFIELDS_H
2 #define GFIELDS_H 1
3 
4 // G4 headers
5 #include "G4MagneticField.hh"
6 #include "G4FieldManager.hh"
7 #include "G4MagIntegratorStepper.hh"
8 
9 // gemc
10 #include "gfactory.h"
11 #include "goptions.h"
12 #include "gfieldConventions.h"
13 
22 
23  std::string name;
24  std::string integration_stepper;
25  double minimum_step;
26  std::string type;
27  int verbosity;
28 
29  map <std::string, string> field_parameters;
30 
36  void add_map_parameter(std::string key, std::string value) {
37  field_parameters[key] = value;
38  }
39 
44  string gfieldPluginName() {
45  return "gfield" + type + "Factory";
46  }
47 
48  // overload << to print the field definition
49  friend std::ostream &operator<<(std::ostream &stream, GFieldDefinition gfd) {
50  stream << " > Field name: " << gfd.name << std::endl;
51  stream << " - integration stepper " << gfd.integration_stepper << std::endl;
52  stream << " - minimum step " << gfd.minimum_step << " mm" << std::endl;
53  stream << " - type " << gfd.type << std::endl;
54  stream << " - verbosity " << gfd.verbosity << std::endl;
55 
56  // print the field parameters
57  // align the keys to the left
58  for (auto &field_parameter: gfd.field_parameters) {
59  stream << " - " << std::left << std::setw(21) << field_parameter.first << field_parameter.second << std::endl;
60  }
61 
62  return stream;
63  }
64 };
65 
69 class GField : public G4MagneticField {
70 
71 public:
75  GField() = default;
76 
80  virtual ~GField() = default;
81 
87  virtual void GetFieldValue(const double x[3], double *bfield) const = 0;
88 
93  G4FieldManager *create_FieldManager();
94 
100  gfield_definitions = gfd;
101  }
102 
107  void gFLogMessage(std::string message) {
108  gLogMessage(GFIELDLOGHEADER + gfield_definitions.name + " " + message);
109  }
110 
111  int get_field_parameter_int(std::string key) {
112  return stoi(gfield_definitions.field_parameters[key]);
113  }
114  double get_field_parameter_double(std::string key) {
115  return stod(gfield_definitions.field_parameters[key]);
116  }
117 
118 private:
119 
120 
121  std::vector <std::string> SUPPORTED_STEPPERS = {
122  "G4DormandPrince745", "G4ClassicalRK4", "G4SimpleRunge", "G4HelixExplicitEuler",
123  "G4HelixImplicitEuler", "G4CashKarpRKF45", "G4RKG3_Stepper", "G4SimpleHeum",
124  "G4NystromRK4", "G4ImplicitEuler", "G4ExplicitEuler"
125  };
126 
127 
128 protected:
130 
131 public:
132 
133  static GField *instantiate(const dlhandle handle) {
134 
135  if (handle == nullptr) return nullptr;
136 
137  // must match the extern C declaration in the derived factories
138  void *maker = dlsym(handle, "GFieldFactory");
139 
140  if (maker == nullptr) return nullptr;
141 
142  typedef GField *(*fptr)();
143 
144  // static_cast not allowed here
145  // see http://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast
146  // need to run the DLL GMediaFactory function that returns the factory
147  fptr func = reinterpret_cast<fptr>(reinterpret_cast<void *>(maker));
148 
149  return func();
150  }
151 };
152 
153 
154 #endif
Abstract base class representing a magnetic field.
Definition: gfield.h:69
void gFLogMessage(std::string message)
Logs a message with the field context.
Definition: gfield.h:107
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:129
int get_field_parameter_int(std::string key)
Definition: gfield.h:111
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:133
virtual void load_field_definitions(GFieldDefinition gfd)
Sets the field definition for the field.
Definition: gfield.h:99
virtual ~GField()=default
Virtual destructor.
double get_field_parameter_double(std::string key)
Definition: gfield.h:114
#define GFIELDLOGHEADER
Utility struct to load GFields from options.
Definition: gfield.h:17
GFieldDefinition()
Default constructor.
Definition: gfield.h:21
double minimum_step
Minimum step size for integration.
Definition: gfield.h:25
std::string name
Key in the gmagneto maps.
Definition: gfield.h:23
map< std::string, string > field_parameters
Field parameters as key-value pairs.
Definition: gfield.h:29
friend std::ostream & operator<<(std::ostream &stream, GFieldDefinition gfd)
Definition: gfield.h:49
void add_map_parameter(std::string key, 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:24
string gfieldPluginName()
Gets the plugin name for the field.
Definition: gfield.h:44
int verbosity
Verbosity level.
Definition: gfield.h:27
std::string type
Type of the field.
Definition: gfield.h:26