gfields
Loading...
Searching...
No Matches
gfield_asciimap.cc
Go to the documentation of this file.
1// dladdr (used to locate this plugin's own path) needs _GNU_SOURCE on glibc.
2#ifndef _GNU_SOURCE
3#define _GNU_SOURCE
4#endif
5
6// plugin header
7#include "gfield_asciimap.h"
8
9// gemc gfield framework
10#include "gfieldConventions.h"
11
12// geant4 / CLHEP
13#include "G4ThreeVector.hh"
14#include "CLHEP/Units/SystemOfUnits.h"
15
16// c++
17#include <cmath>
18#include <cstdlib>
19#include <dlfcn.h>
20#include <fstream>
21#include <sstream>
22
23using namespace CLHEP;
24
25// Tells the loader how to create a GField in this plugin .so/.dylib.
26extern "C" GField* GFieldFactory(const std::shared_ptr<GOptions>& g) {
27 return static_cast<GField*>(new GField_AsciiMapFactory(g));
28}
29
30
31// ---------------------------------------------------------------------------------------------------
32// Configuration helpers
33// ---------------------------------------------------------------------------------------------------
34
35std::string GField_AsciiMapFactory::param_string(const std::string& key, const std::string& dflt) const {
36 auto it = gfield_definitions.field_parameters.find(key);
37 return (it != gfield_definitions.field_parameters.end() && !it->second.empty()) ? it->second : dflt;
38}
39
40double GField_AsciiMapFactory::param_g4number(const std::string& key, const std::string& dflt) const {
41 return gutilities::getG4Number(param_string(key, dflt));
42}
43
44std::string GField_AsciiMapFactory::field_maps_directory() const {
45 // Locate this plugin's own shared object on disk and point at the sibling "fields" directory
46 // (<plugin_dir>/../fields), the layout produced by `meson install`. No environment variable is used.
47 Dl_info info;
48 if (dladdr(reinterpret_cast<const void*>(&GFieldFactory), &info) != 0 && info.dli_fname != nullptr) {
49 const std::string plugin_path = info.dli_fname;
50 const auto slash = plugin_path.find_last_of('/');
51 if (slash != std::string::npos) { return plugin_path.substr(0, slash) + "/../fields"; }
52 }
53 log->warning("GField_AsciiMapFactory: could not resolve plugin path; using ./fields for the map.");
54 return "fields";
55}
56
57int GField_AsciiMapFactory::axis_of_coordinate(const std::string& name) const {
58 switch (symmetry) {
59 case Symmetry::dipole_x:
60 case Symmetry::dipole_y:
61 case Symmetry::dipole_z:
62 if (name == "longitudinal") return 0;
63 if (name == "transverse") return 1;
64 return -1;
65 case Symmetry::cyl_x:
66 case Symmetry::cyl_y:
67 case Symmetry::cyl_z:
68 if (name == "transverse") return 0;
69 if (name == "longitudinal") return 1;
70 return -1;
71 case Symmetry::phi_segmented:
72 if (name == "azimuthal") return 0;
73 if (name == "transverse") return 1;
74 if (name == "longitudinal") return 2;
75 return -1;
76 case Symmetry::cartesian_3d:
77 case Symmetry::cartesian_3d_quadrant:
78 if (name == "X") return 0;
79 if (name == "Y") return 1;
80 if (name == "Z") return 2;
81 return -1;
82 }
83 return -1;
84}
85
86
87// ---------------------------------------------------------------------------------------------------
88// Loading
89// ---------------------------------------------------------------------------------------------------
90
91void GField_AsciiMapFactory::load_coordinate(const std::string& key) {
92 const std::string value = param_string(key, "");
93 if (value.empty()) {
95 "GField_AsciiMapFactory: missing coordinate <", key, "> for field <",
97 }
98
99 // Split "name, npoints, min, max" on commas.
100 const auto tokens = gutilities::getStringVectorFromStringWithDelimiter(value, ",");
101 if (tokens.size() != 4) {
103 "GField_AsciiMapFactory: coordinate <", key, "> must be \"name, npoints, min, max\". Got <",
104 value, ">.");
105 }
106
107 const std::string name = tokens[0];
108 const int npoints = std::stoi(tokens[1]);
109 const double min = gutilities::getG4Number(tokens[2]);
110 const double max = gutilities::getG4Number(tokens[3]);
111
112 const int axis = axis_of_coordinate(name);
113 if (axis < 0) {
115 "GField_AsciiMapFactory: coordinate name <", name, "> is not valid for symmetry <",
116 gfield_definitions.field_parameters["symmetry"], ">.");
117 }
118 if (npoints < 2) {
120 "GField_AsciiMapFactory: coordinate <", name, "> needs at least 2 points; got ", npoints, ".");
121 }
122
123 np[axis] = static_cast<unsigned>(npoints);
124 startMap[axis] = min;
125 endMap[axis] = max;
126 cellSize[axis] = (max - min) / (npoints - 1);
127
128 // The map-file column for this coordinate is read in the same unit used in the min/max expression,
129 // so a bare column value is converted back to Geant4 units with that factor.
130 const std::string unit = [&]() {
131 for (const std::string& expr : {tokens[3], tokens[2]}) {
132 const auto star = expr.find('*');
133 if (star != std::string::npos) {
134 return gutilities::removeLeadingAndTrailingSpacesFromString(expr.substr(star + 1));
135 }
136 }
137 return std::string("mm");
138 }();
139
140 columns.push_back(Column{axis, gutilities::getG4Number(1.0, unit)});
141}
142
143void GField_AsciiMapFactory::load_map_file() {
144 const std::string map_name = param_string("map", "");
145 if (map_name.empty()) {
147 "GField_AsciiMapFactory: no 'map' file given for field <", gfield_definitions.name, ">.");
148 }
149
150 // Resolve the map file. An explicit path (containing '/') is used as-is. Otherwise the lookup order
151 // is: the explicit `dir` parameter, then the directory of the YAML that defined the field (so a plain
152 // .yaml run from its own directory or referenced by absolute path just works), then the `fields`
153 // directory installed next to the plugin.
154 std::string path = map_name;
155 if (map_name.find('/') == std::string::npos) {
156 std::vector<std::string> candidates;
157 if (const std::string dir = param_string("dir", ""); !dir.empty()) { candidates.push_back(dir); }
158 if (!gfield_definitions.config_dir.empty()) { candidates.push_back(gfield_definitions.config_dir); }
159 candidates.push_back(field_maps_directory());
160
161 path.clear();
162 for (const auto& dir : candidates) {
163 const std::string trial = dir + "/" + map_name;
164 if (std::ifstream(trial).good()) { path = trial; break; }
165 }
166 if (path.empty()) {
168 "GField_AsciiMapFactory: cannot find map <", map_name, "> for field <",
169 gfield_definitions.name, "> in dir/config/plugin locations.");
170 }
171 }
172
173 std::ifstream in(path);
174 if (!in.good()) {
175 log->error(ERR_MAP_FILE_NOT_FOUND, "GField_AsciiMapFactory: cannot open map file <", path, ">.");
176 }
177 log->info(1, "Loading ASCII field map <", path, "> with symmetry <",
178 gfield_definitions.field_parameters["symmetry"], ">.");
179
180 // Total grid points and field-value scaling.
181 std::size_t total = np[0];
182 for (int d = 1; d < ndim; ++d) { total *= np[d]; }
183
184 const double field_scale = param_g4number("scale", "1") *
185 gutilities::getG4Number(1.0, param_string("field_unit", "gauss"));
186
187 B1.assign(total, 0.0f);
188 if (ncomp >= 2) { B2.assign(total, 0.0f); }
189 if (ncomp >= 3) { B3.assign(total, 0.0f); }
190
191 const double tolerance = 0.001; // relative cell-position tolerance for the grid-consistency check
192
193 std::size_t read_points = 0;
194 std::string line;
195 int line_number = 0;
196 while (std::getline(in, line)) {
197 ++line_number;
199 if (trimmed.empty() || trimmed[0] == '#') { continue; }
200
201 std::istringstream tokens(trimmed);
202
203 // Read the coordinate columns (file-column order), convert them to Geant4 units and turn each
204 // into its canonical axis index.
205 unsigned grid_index[3] = {0, 0, 0};
206 bool line_ok = true;
207 for (int c = 0; c < ndim; ++c) {
208 double raw = 0.0;
209 if (!(tokens >> raw)) { line_ok = false; break; }
210 const double coord = raw * columns[c].unitFactor;
211 const int axis = columns[c].axis;
212 const unsigned i = static_cast<unsigned>(
213 std::floor((coord - startMap[axis] + cellSize[axis] / 2) / cellSize[axis]));
214 grid_index[axis] = i;
215
216 const double expected = startMap[axis] + i * cellSize[axis];
217 if (cellSize[axis] != 0.0 && std::fabs(expected - coord) > tolerance * std::fabs(cellSize[axis])) {
218 log->warning("GField_AsciiMapFactory: ", path, ":", line_number,
219 " axis ", axis, " value ", coord, " does not match grid point ", expected, ".");
220 }
221 }
222
223 double comp[3] = {0.0, 0.0, 0.0};
224 for (int k = 0; k < ncomp && line_ok; ++k) {
225 if (!(tokens >> comp[k])) { line_ok = false; }
226 }
227 if (!line_ok) {
228 log->error(ERR_MAP_FILE_NOT_FOUND, "GField_AsciiMapFactory: ", path, ":", line_number,
229 " has fewer than ", ndim + ncomp, " numbers.");
230 }
231
232 std::size_t flat = (ndim == 2) ? idx2(grid_index[0], grid_index[1])
233 : idx3(grid_index[0], grid_index[1], grid_index[2]);
234 if (flat >= total) {
235 log->warning("GField_AsciiMapFactory: ", path, ":", line_number, " maps outside the grid; skipped.");
236 continue;
237 }
238
239 B1[flat] = static_cast<float>(comp[0] * field_scale);
240 if (ncomp >= 2) { B2[flat] = static_cast<float>(comp[1] * field_scale); }
241 if (ncomp >= 3) { B3[flat] = static_cast<float>(comp[2] * field_scale); }
242 ++read_points;
243 }
244
245 if (read_points != total) {
246 log->warning("GField_AsciiMapFactory: ", path, " provided ", read_points,
247 " points but the grid expects ", total, ".");
248 }
249 log->info(1, "ASCII field map <", gfield_definitions.name, "> loaded: ", read_points, " points.");
250}
251
253 gfield_definitions = gfd;
254
255 // Decode the symmetry once. ndim/ncomp follow from it.
256 const std::string sym = param_string("symmetry", "");
257 if (sym == "dipole-x") { symmetry = Symmetry::dipole_x; ndim = 2; ncomp = 1; }
258 else if (sym == "dipole-y") { symmetry = Symmetry::dipole_y; ndim = 2; ncomp = 1; }
259 else if (sym == "dipole-z") { symmetry = Symmetry::dipole_z; ndim = 2; ncomp = 1; }
260 else if (sym == "cylindrical-x") { symmetry = Symmetry::cyl_x; ndim = 2; ncomp = 2; }
261 else if (sym == "cylindrical-y") { symmetry = Symmetry::cyl_y; ndim = 2; ncomp = 2; }
262 else if (sym == "cylindrical-z") { symmetry = Symmetry::cyl_z; ndim = 2; ncomp = 2; }
263 else if (sym == "phi-segmented") { symmetry = Symmetry::phi_segmented; ndim = 3; ncomp = 3; }
264 else if (sym == "cartesian_3D") { symmetry = Symmetry::cartesian_3d; ndim = 3; ncomp = 3; }
265 else if (sym == "cartesian_3D_quadrant") { symmetry = Symmetry::cartesian_3d_quadrant; ndim = 3; ncomp = 3; }
266 else {
268 "GField_AsciiMapFactory: unknown symmetry <", sym, "> for field <",
270 }
271
272 // Grid coordinates, in map-file column order.
273 columns.clear();
274 columns.reserve(ndim);
275 load_coordinate("coordinate1");
276 load_coordinate("coordinate2");
277 if (ndim == 3) { load_coordinate("coordinate3"); }
278
279 // Interpolation.
280 linear = (param_string("interpolation", "linear") != "none");
281
282 // Overall placement.
283 mapOrigin[0] = param_g4number("vx", "0");
284 mapOrigin[1] = param_g4number("vy", "0");
285 mapOrigin[2] = param_g4number("vz", "0");
286 mapRotation[0] = param_g4number("rx", "0*deg");
287 mapRotation[1] = param_g4number("ry", "0*deg");
288 mapRotation[2] = param_g4number("rz", "0*deg");
289 sinAlpha = std::sin(mapRotation[0]); cosAlpha = std::cos(mapRotation[0]);
290 sinBeta = std::sin(mapRotation[1]); cosBeta = std::cos(mapRotation[1]);
291 sinGamma = std::sin(mapRotation[2]); cosGamma = std::cos(mapRotation[2]);
292
293 load_map_file();
294}
295
296
297// ---------------------------------------------------------------------------------------------------
298// Field evaluation
299// ---------------------------------------------------------------------------------------------------
300
301void GField_AsciiMapFactory::GetFieldValue(const double pos[3], G4double* bfield) const {
302 bfield[0] = bfield[1] = bfield[2] = 0.0;
303
304 if (std::isnan(pos[0]) || std::isnan(pos[1]) || std::isnan(pos[2])) {
305 log->warning("GField_AsciiMapFactory: field coordinates requested are nan.");
306 return;
307 }
308
309 // Shift to the map frame.
310 const double x[3] = {pos[0] - mapOrigin[0], pos[1] - mapOrigin[1], pos[2] - mapOrigin[2]};
311
312 switch (symmetry) {
313 case Symmetry::dipole_x:
314 case Symmetry::dipole_y:
315 case Symmetry::dipole_z:
316 value_dipole(x, bfield);
317 break;
318 case Symmetry::cyl_x:
319 case Symmetry::cyl_y:
320 case Symmetry::cyl_z:
321 value_cylindrical(x, bfield);
322 break;
323 case Symmetry::phi_segmented:
324 value_phi_segmented(x, bfield);
325 break;
326 case Symmetry::cartesian_3d:
327 case Symmetry::cartesian_3d_quadrant:
328 value_cartesian3d(x, bfield);
329 break;
330 }
331
332 log->info(2, "ASCII field <", gfield_definitions.name, "> at (",
333 pos[0] / cm, ", ", pos[1] / cm, ", ", pos[2] / cm, ") cm = (",
334 bfield[0] / gauss, ", ", bfield[1] / gauss, ", ", bfield[2] / gauss, ") gauss");
335}
336
337
338void GField_AsciiMapFactory::value_dipole(const double x[3], double* bfield) const {
339 // Axis 0 = longitudinal, axis 1 = transverse.
340 double LC = 0.0, TC = 0.0;
341 if (symmetry == Symmetry::dipole_z) { TC = std::fabs(x[0]); LC = x[1]; }
342 else if (symmetry == Symmetry::dipole_x) { TC = std::fabs(x[1]); LC = x[2]; }
343 else /* dipole_y */ { TC = std::fabs(x[0]); LC = x[2]; }
344
345 if (LC < startMap[0] || TC < startMap[1]) { return; }
346 unsigned IL = static_cast<unsigned>(std::floor((LC - startMap[0]) / cellSize[0]));
347 unsigned IT = static_cast<unsigned>(std::floor((TC - startMap[1]) / cellSize[1]));
348 if (IL >= np[0] - 1 || IT >= np[1] - 1) { return; }
349
350 double b = 0.0;
351 if (!linear) {
352 if (std::fabs(startMap[0] + IL * cellSize[0] - LC) > std::fabs(startMap[0] + (IL + 1) * cellSize[0] - LC)) IL++;
353 if (std::fabs(startMap[1] + IT * cellSize[1] - TC) > std::fabs(startMap[1] + (IT + 1) * cellSize[1] - TC)) IT++;
354 b = B1[idx2(IL, IT)];
355 }
356 else {
357 const double xlr = (LC - (startMap[0] + IL * cellSize[0])) / cellSize[0];
358 const double xtr = (TC - (startMap[1] + IT * cellSize[1])) / cellSize[1];
359 const double b10 = B1[idx2(IL, IT)] * (1.0 - xtr) + B1[idx2(IL, IT + 1)] * xtr;
360 const double b11 = B1[idx2(IL + 1, IT)] * (1.0 - xtr) + B1[idx2(IL + 1, IT + 1)] * xtr;
361 b = b10 * (1.0 - xlr) + b11 * xlr;
362 }
363
364 if (symmetry == Symmetry::dipole_x) { bfield[0] = b; }
365 else if (symmetry == Symmetry::dipole_y) { bfield[1] = b; }
366 else { bfield[2] = b; }
367
368 rotate_field(bfield);
369}
370
371
372void GField_AsciiMapFactory::value_cylindrical(const double x[3], double* bfield) const {
373 // Axis 0 = transverse (radial), axis 1 = longitudinal.
374 double LC = 0.0, TC = 0.0, phi = 0.0;
375 if (symmetry == Symmetry::cyl_z) {
376 LC = x[2]; TC = std::sqrt(x[0] * x[0] + x[1] * x[1]); phi = G4ThreeVector(x[0], x[1], x[2]).phi();
377 }
378 else if (symmetry == Symmetry::cyl_x) {
379 LC = x[0]; TC = std::sqrt(x[1] * x[1] + x[2] * x[2]); phi = G4ThreeVector(x[1], x[2], x[0]).phi();
380 }
381 else /* cyl_y */ {
382 LC = x[1]; TC = std::sqrt(x[0] * x[0] + x[2] * x[2]); phi = G4ThreeVector(x[2], x[0], x[1]).phi();
383 }
384
385 if (TC < startMap[0] || LC < startMap[1]) { return; }
386 unsigned IT = static_cast<unsigned>(std::floor((TC - startMap[0]) / cellSize[0]));
387 unsigned IL = static_cast<unsigned>(std::floor((LC - startMap[1]) / cellSize[1]));
388 if (IT >= np[0] - 1 || IL >= np[1] - 1) { return; }
389
390 double b1 = 0.0, b2 = 0.0;
391 if (!linear) {
392 if (std::fabs(startMap[0] + IT * cellSize[0] - TC) > std::fabs(startMap[0] + (IT + 1) * cellSize[0] - TC)) IT++;
393 if (std::fabs(startMap[1] + IL * cellSize[1] - LC) > std::fabs(startMap[1] + (IL + 1) * cellSize[1] - LC)) IL++;
394 b1 = B1[idx2(IT, IL)];
395 b2 = B2[idx2(IT, IL)];
396 }
397 else {
398 const double xtr = (TC - (startMap[0] + IT * cellSize[0])) / cellSize[0];
399 const double xlr = (LC - (startMap[1] + IL * cellSize[1])) / cellSize[1];
400 const double b10 = B1[idx2(IT, IL)] * (1.0 - xtr) + B1[idx2(IT + 1, IL)] * xtr;
401 const double b11 = B1[idx2(IT, IL + 1)] * (1.0 - xtr) + B1[idx2(IT + 1, IL + 1)] * xtr;
402 b1 = b10 * (1.0 - xlr) + b11 * xlr;
403 const double b20 = B2[idx2(IT, IL)] * (1.0 - xtr) + B2[idx2(IT + 1, IL)] * xtr;
404 const double b21 = B2[idx2(IT, IL + 1)] * (1.0 - xtr) + B2[idx2(IT + 1, IL + 1)] * xtr;
405 b2 = b20 * (1.0 - xlr) + b21 * xlr;
406 }
407
408 if (symmetry == Symmetry::cyl_z) {
409 bfield[0] = b1 * std::cos(phi); bfield[1] = b1 * std::sin(phi); bfield[2] = b2;
410 }
411 else if (symmetry == Symmetry::cyl_x) {
412 bfield[0] = b2; bfield[1] = b1 * std::cos(phi); bfield[2] = b1 * std::sin(phi);
413 }
414 else /* cyl_y */ {
415 bfield[1] = b2; bfield[0] = b1 * std::sin(phi); bfield[2] = b1 * std::cos(phi);
416 }
417
418 rotate_field(bfield);
419}
420
421
422void GField_AsciiMapFactory::value_phi_segmented(const double x[3], double* bfield) const {
423 // Axis 0 = azimuthal, axis 1 = transverse, axis 2 = longitudinal. Fields are stored in the local
424 // (first-segment) frame and rotated back to the query phi.
425 double aC = std::atan2(x[1], x[0]) * rad; // phi
426 if (aC < 0) { aC += 360 * deg; }
427 const double tC = std::sqrt(x[0] * x[0] + x[1] * x[1]); // R
428 const double lC = x[2]; // Z
429
430 // Fold into the first 60-degree segment, keeping the rotation back to the lab.
431 double aLC = aC;
432 while (aLC / deg > 30) { aLC -= 60 * deg; }
433 const double dphi = aC - aLC;
434 const double aaLC = std::fabs(aLC);
435 const int sign = (aLC >= 0 ? 1 : -1);
436
437 if (aC < startMap[0] || tC < startMap[1] || lC < startMap[2]) { return; }
438 unsigned aI = static_cast<unsigned>(std::floor((aaLC - startMap[0]) / cellSize[0]));
439 unsigned tI = static_cast<unsigned>(std::floor((tC - startMap[1]) / cellSize[1]));
440 unsigned lI = static_cast<unsigned>(std::floor((lC - startMap[2]) / cellSize[2]));
441 if (aI >= np[0] - 1 || tI >= np[1] - 1 || lI >= np[2] - 1) { return; }
442
443 double mfield[3] = {0.0, 0.0, 0.0};
444 if (!linear) {
445 if (std::fabs(startMap[0] + aI * cellSize[0] - aaLC) > std::fabs(startMap[0] + (aI + 1) * cellSize[0] - aaLC)) aI++;
446 if (std::fabs(startMap[1] + tI * cellSize[1] - tC) > std::fabs(startMap[1] + (tI + 1) * cellSize[1] - tC)) tI++;
447 if (std::fabs(startMap[2] + lI * cellSize[2] - lC) > std::fabs(startMap[2] + (lI + 1) * cellSize[2] - lC)) lI++;
448 mfield[0] = B1[idx3(aI, tI, lI)];
449 mfield[1] = B2[idx3(aI, tI, lI)];
450 mfield[2] = B3[idx3(aI, tI, lI)];
451 }
452 else {
453 const double xaz = (aaLC - (startMap[0] + aI * cellSize[0])) / cellSize[0];
454 const double xtr = (tC - (startMap[1] + tI * cellSize[1])) / cellSize[1];
455 const double xlr = (lC - (startMap[2] + lI * cellSize[2])) / cellSize[2];
456
457 const std::vector<float>* comps[3] = {&B1, &B2, &B3};
458 for (int k = 0; k < 3; ++k) {
459 const std::vector<float>& B = *comps[k];
460 const double b00 = B[idx3(aI, tI, lI)] * (1 - xaz) + B[idx3(aI + 1, tI, lI)] * xaz;
461 const double b01 = B[idx3(aI, tI, lI + 1)] * (1 - xaz) + B[idx3(aI + 1, tI, lI + 1)] * xaz;
462 const double b10 = B[idx3(aI, tI + 1, lI)] * (1 - xaz) + B[idx3(aI + 1, tI + 1, lI)] * xaz;
463 const double b11 = B[idx3(aI, tI + 1, lI + 1)] * (1 - xaz) + B[idx3(aI + 1, tI + 1, lI + 1)] * xaz;
464 const double b0 = b00 * (1 - xtr) + b10 * xtr;
465 const double b1 = b01 * (1 - xtr) + b11 * xtr;
466 mfield[k] = b0 * (1 - xlr) + b1 * xlr;
467 }
468 }
469
470 // Rotate the local field back to the query azimuth.
471 bfield[0] = sign * mfield[0] * std::cos(dphi / rad) - mfield[1] * std::sin(dphi / rad);
472 bfield[1] = sign * mfield[0] * std::sin(dphi / rad) + mfield[1] * std::cos(dphi / rad);
473 bfield[2] = sign * mfield[2];
474
475 rotate_field(bfield);
476}
477
478
479void GField_AsciiMapFactory::value_cartesian3d(const double x[3], double* bfield) const {
480 // Axis 0 = X, axis 1 = Y, axis 2 = Z.
481 double XX = x[0], YY = x[1], ZZ = x[2];
482
483 if (symmetry == Symmetry::cartesian_3d_quadrant) {
484 // Fold the query point into the stored first quadrant (x>=0, y>=0).
485 if (x[0] >= 0 && x[1] >= 0) { XX = x[0]; YY = x[1]; }
486 else if (x[0] >= 0 && x[1] < 0) { XX = -x[1]; YY = x[0]; }
487 else if (x[0] < 0 && x[1] < 0) { XX = -x[0]; YY = -x[1]; }
488 else /* x[0] < 0 && x[1] >= 0 */ { XX = x[1]; YY = -x[0]; }
489 ZZ = x[2];
490 if (XX < 0 || YY < 0) { return; }
491 }
492
493 if (XX < startMap[0] || YY < startMap[1] || ZZ < startMap[2]) { return; }
494 if (XX >= endMap[0] || YY >= endMap[1] || ZZ >= endMap[2]) { return; }
495 const unsigned IXX = static_cast<unsigned>(std::floor((XX - startMap[0]) / cellSize[0]));
496 const unsigned IYY = static_cast<unsigned>(std::floor((YY - startMap[1]) / cellSize[1]));
497 const unsigned IZZ = static_cast<unsigned>(std::floor((ZZ - startMap[2]) / cellSize[2]));
498
499 double B[3] = {0.0, 0.0, 0.0};
500 if (!linear) {
501 unsigned ix = IXX, iy = IYY, iz = IZZ;
502 if (std::fabs(startMap[0] + ix * cellSize[0] - XX) > std::fabs(startMap[0] + (ix + 1) * cellSize[0] - XX)) ix++;
503 if (std::fabs(startMap[1] + iy * cellSize[1] - YY) > std::fabs(startMap[1] + (iy + 1) * cellSize[1] - YY)) iy++;
504 if (std::fabs(startMap[2] + iz * cellSize[2] - ZZ) > std::fabs(startMap[2] + (iz + 1) * cellSize[2] - ZZ)) iz++;
505 B[0] = B1[idx3(ix, iy, iz)];
506 B[1] = B2[idx3(ix, iy, iz)];
507 B[2] = B3[idx3(ix, iy, iz)];
508 }
509 else {
510 const double Xd = (XX - (startMap[0] + IXX * cellSize[0])) / cellSize[0];
511 const double Yd = (YY - (startMap[1] + IYY * cellSize[1])) / cellSize[1];
512 const double Zd = (ZZ - (startMap[2] + IZZ * cellSize[2])) / cellSize[2];
513
514 const std::vector<float>* comps[3] = {&B1, &B2, &B3};
515 for (int k = 0; k < 3; ++k) {
516 const std::vector<float>& Bk = *comps[k];
517 const double c00 = Bk[idx3(IXX, IYY, IZZ)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY, IZZ)] * Xd;
518 const double c01 = Bk[idx3(IXX, IYY, IZZ + 1)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY, IZZ + 1)] * Xd;
519 const double c10 = Bk[idx3(IXX, IYY + 1, IZZ)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY + 1, IZZ)] * Xd;
520 const double c11 = Bk[idx3(IXX, IYY + 1, IZZ + 1)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY + 1, IZZ + 1)] * Xd;
521 const double c0 = c00 * (1 - Yd) + c10 * Yd;
522 const double c1 = c01 * (1 - Yd) + c11 * Yd;
523 B[k] = c0 * (1 - Zd) + c1 * Zd;
524 }
525 }
526
527 if (symmetry == Symmetry::cartesian_3d_quadrant) {
528 // Mirror the field components back to the query quadrant.
529 if (x[0] >= 0 && x[1] >= 0) { bfield[0] = B[0]; bfield[1] = B[1]; }
530 else if (x[0] >= 0 && x[1] < 0) { bfield[0] = B[1]; bfield[1] = -B[0]; }
531 else if (x[0] < 0 && x[1] < 0) { bfield[0] = -B[0]; bfield[1] = -B[1]; }
532 else /* x[0] < 0 && x[1] >= 0 */ { bfield[0] = -B[1]; bfield[1] = B[0]; }
533 bfield[2] = B[2];
534 }
535 else {
536 bfield[0] = B[0]; bfield[1] = B[1]; bfield[2] = B[2];
537 }
538
539 rotate_field(bfield);
540}
541
542
543void GField_AsciiMapFactory::rotate_field(double* bfield) const {
544 // Rotate the field axes, not the point: each rotation is the inverse of the point rotation.
545 if (mapRotation[0] != 0) {
546 const double yPrime = bfield[1] * cosAlpha + bfield[2] * sinAlpha;
547 const double zPrime = -bfield[1] * sinAlpha + bfield[2] * cosAlpha;
548 bfield[1] = yPrime; bfield[2] = zPrime;
549 }
550 if (mapRotation[1] != 0) {
551 const double xPrime = bfield[0] * cosBeta - bfield[2] * sinBeta;
552 const double zPrime = bfield[0] * sinBeta + bfield[2] * cosBeta;
553 bfield[0] = xPrime; bfield[2] = zPrime;
554 }
555 if (mapRotation[2] != 0) {
556 const double xPrime = bfield[0] * cosGamma + bfield[1] * sinGamma;
557 const double yPrime = -bfield[0] * sinGamma + bfield[1] * cosGamma;
558 bfield[0] = xPrime; bfield[1] = yPrime;
559 }
560}
std::shared_ptr< GLogger > log
Concrete GField reading a magnetic field from an ASCII map and a YAML definition.
void load_field_definitions(GFieldDefinition gfd) override
Parse the YAML definition, build the grid and read the map file.
void GetFieldValue(const double pos[3], G4double *bfield) const override
Compute the field at the lab-frame point pos, writing {Bx,By,Bz} (Geant4 units) into bfield.
Abstract base class representing a magnetic field.
Definition gfield.h:105
GFieldDefinition gfield_definitions
Stored field definition used for configuration and logging.
Definition gfield.h:191
void warning(Args &&... args) const
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
GField * GFieldFactory(const std::shared_ptr< GOptions > &g)
int line
#define ERR_WRONG_FIELD_SYMMETRY
#define ERR_MAP_FILE_NOT_FOUND
#define ERR_WRONG_COORDINATE_DEF
double getG4Number(const string &v, bool warnIfNotUnit=false)
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
string removeLeadingAndTrailingSpacesFromString(const std::string &input)
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 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