GEMC  2.3
Geant4 Monte-Carlo Framework
field.cc
Go to the documentation of this file.
1 // gemc headers
2 #include "field.h"
3 #include "string_utilities.h"
4 #include "fieldFactory.h"
5 #include "multipoleField.h"
6 
7 // G4 headers
8 #include "G4UniformMagField.hh"
9 #include "G4ChordFinder.hh"
10 #include "G4ClassicalRK4.hh"
11 #include "G4HelixSimpleRunge.hh"
12 #include "G4HelixImplicitEuler.hh"
13 #include "G4HelixExplicitEuler.hh"
14 #include "G4CachedMagneticField.hh"
15 
16 // this class serves as a dispatcher for the
17 // various format of magnetic fields
18 // availiable formats:
19 // - simple (uniform fields)
20 // - map
21 void gfield::create_MFM()
22 {
23 
24  // fields can be uniform, mapped
25  if (format == "simple" && symmetry == "uniform")
27 
28  // fields can be multipole, mapped
29  if (format == "simple" && symmetry == "multipole")
31 
32  if (format == "map")
33  {
35 
36  G4Mag_UsualEqRhs* iEquation = new G4Mag_UsualEqRhs(map);
37  G4MagIntegratorStepper* iStepper = createStepper(integration, iEquation);
38  G4ChordFinder* iChordFinder = new G4ChordFinder(map, minStep, iStepper);
39 
40  // caching does not seem to help for dipole-y
41  // will it help for other field maps?
42  G4MagneticField *pCachedMagField = new G4CachedMagneticField(map, 1 * m);
43  MFM = new G4FieldManager(pCachedMagField, iChordFinder);
44 
45  G4double minEps= 0.1; // Minimum & value for smallest steps
46  G4double maxEps= 1.0; // Maximum & value for largest steps
47 
48  MFM->SetMinimumEpsilonStep( minEps );
49  MFM->SetMaximumEpsilonStep( maxEps );
50  MFM->SetDeltaOneStep(0.01 * mm);
51  MFM->SetDeltaIntersection(0.01 * mm);
52  }
53 
54 }
55 
57 {
58  vector < string > dim = get_strings(dimensions);
59 
60  if (dim.size() != 3)
61  cout << " !!! Error: dimension of field " << name << " are wrong: "
62  << dimensions << " has dimension " << dim.size() << endl;
63 
64  const G4ThreeVector constField(get_number(dim[0]), get_number(dim[1]), get_number(dim[2]));
65  G4UniformMagField* magField = new G4UniformMagField(constField);
66 
67  G4Mag_UsualEqRhs* iEquation = new G4Mag_UsualEqRhs(magField);
68  G4MagIntegratorStepper* iStepper = createStepper(integration, iEquation);
69  G4ChordFinder* iChordFinder = new G4ChordFinder(magField, minStep, iStepper);
70 
71  MFM = new G4FieldManager(magField, iChordFinder);
72 
73  if (verbosity > 1)
74  {
75  cout << " > <" << name << ">: uniform magnetic field is built." << endl;
76  }
77 }
78 
80 {
81  vector < string > dim = get_strings(dimensions);
82 
83  if (dim.size() != 7)
84  cout << " !!! Error: dimension of field " << name << " are wrong: "
85  << dimensions << " has dimension " << dim.size() << endl;
86 
87  multipoleField* magField = new multipoleField(atoi(dim[0].c_str()), get_number(dim[1]), get_number(dim[2]), get_number(dim[3]),
88  get_number(dim[4]), get_number(dim[5]), dim[6]);
89 
90 
91  G4Mag_UsualEqRhs* iEquation = new G4Mag_UsualEqRhs(magField);
92  G4MagIntegratorStepper* iStepper = createStepper(integration, iEquation);
93  G4ChordFinder* iChordFinder = new G4ChordFinder(magField, minStep, iStepper);
94 
95  MFM = new G4FieldManager(magField, iChordFinder);
96 
97  if (verbosity > 1)
98  {
99  cout << " > <" << name << ">: multipole magnetic field is built with "
100  << dimensions << endl;
101  }
102 }
103 
104 G4MagIntegratorStepper *createStepper(string sname, G4Mag_UsualEqRhs* ie)
105 {
106  if (sname == "ClassicalRK4") return new G4ClassicalRK4(ie);
107  if (sname == "RungeKutta") return new G4HelixSimpleRunge(ie);
108  if (sname == "ImplicitEuler") return new G4HelixImplicitEuler(ie);
109  if (sname == "ExplicitEuler") return new G4HelixExplicitEuler(ie);
110 
111  // if requested is not found return NULL
112  cout << " !!! Error: stepper " << sname << " is not defined " << endl;
113  return NULL;
114 }
115 
117 {
118  string hd_msg = " >> fields Init: ";
119  vector<aopt> FIELD_SCALES_OPTION = Opt.getArgs("SCALE_FIELD");
120 
121  for (unsigned int f = 0; f < FIELD_SCALES_OPTION.size(); f++)
122  {
123  vector < string > scales = get_strings(FIELD_SCALES_OPTION[f].args, ",");
124  if (scales.size() == 2)
125  {
126  if (scales[0].find(name) != string::npos)
127  scaleFactor = get_number(scales[1]);
128  }
129  }
130  if (map)
131  {
133  map->initializeMap();
135  }
136 }
137 
139 ostream &operator<<(ostream &stream, gfield gf)
140 {
141  cout << " > Field name: " << gf.name << endl;
142  cout << " - factory: " << gf.factory << endl;
143  cout << " - comment: " << gf.description << endl;
144  cout << " - format: " << gf.format << endl;
145  cout << " - symmetry: " << gf.symmetry << endl;
146  cout << " - scale factor: " << gf.scaleFactor << endl;
147  cout << " - integration method: " << gf.integration << endl;
148  cout << " - minimum Step: " << gf.minStep << endl;
149  if (gf.dimensions != "na" && gf.format == "simple")
150  cout << " - dimensions: " << gf.dimensions << endl;
151 
152  if (gf.dimensions == "na" && gf.format == "map")
153  {
154  cout << " - map identifier: " << gf.map->identifier << endl;
155 
156  for (unsigned int i = 0; i < gf.map->coordinates.size(); i++)
157  cout << " - Coordinate: " << gf.map->coordinates[i];
158 
159  cout << " - Map Field Unit: " << gf.map->unit << endl;
160  cout << " - Map Interpolation: " << gf.map->interpolation << endl;
161 
162  cout << " - map origin: x=" << gf.map->mapOrigin[0]
163  << "mm, y=" << gf.map->mapOrigin[1]
164  << "mm, z=" << gf.map->mapOrigin[2] << "mm" << endl;
165  }
166  return stream;
167 }
168 
169 
170 
171 
double mapOrigin[3]
Displacement of map. This is used in GetFieldValue.
Definition: mappedField.h:95
string description
Field Description.
Definition: field.h:76
gMappedField * map
Mapped Field.
Definition: field.h:92
vector< string > get_strings(string input)
returns a vector of strings from a stringstream, space is delimiter
int verbosity
map verbosity
Definition: mappedField.h:99
void create_simple_MFM()
Definition: field.cc:56
string unit
field unit in the map
Definition: mappedField.h:97
string integration
Integration Method.
Definition: field.h:78
string format
Field format (available: simple (for uniform) and map)
Definition: field.h:74
double scaleFactor
copy of the gfield scaleFactor
Definition: mappedField.h:96
string symmetry
Field symmetry.
Definition: field.h:73
vector< aopt > getArgs(string)
get a vector of arguments matching a string
Definition: options.cc:427
double get_number(string v, int warn_no_unit)
Returns number with dimension from string, i.e. 100*cm.
friend ostream & operator<<(ostream &stream, gfield gf)
Definition: field.cc:139
string name
Field name - used as key in the map<string, gfield>
Definition: field.h:72
G4MagIntegratorStepper * createStepper(string sname, G4Mag_UsualEqRhs *ie)
Definition: field.cc:104
void create_simple_multipole_MFM()
Definition: field.cc:79
string dimensions
Field dimensions (with units), for non-mapped fields.
Definition: field.h:77
void initializeMap()
Definition: mappedField.cc:61
string factory
Field factory (format of magnetic field)
Definition: field.h:75
vector< gcoord > coordinates
Vector size depend on the symmetry.
Definition: mappedField.h:93
double verbosity
Log verbosity.
Definition: field.h:79
void initialize(goptions)
Overloaded "<<" for gfield class. Dumps infos on screen.
Definition: field.cc:116
Definition: field.h:52
string interpolation
map interpolation technique. Choices are "none", "linear", "quadratic"
Definition: mappedField.h:98
fieldFactory * fFactory
fieldFactory that created the field
Definition: field.h:93
double minStep
Minimum Step for the G4ChordFinder.
Definition: field.h:80
virtual void loadFieldMap(gMappedField *, double)=0
double scaleFactor
Definition: field.h:84
string identifier
Pointer to map in factory (for example, hostname / filename with path / date)
Definition: mappedField.h:92