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
57std::optional<unsigned> 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 std::nullopt;
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 std::nullopt;
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 std::nullopt;
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 std::nullopt;
82 }
83 return std::nullopt;
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 <",
96 gfield_definitions.name, ">.");
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 auto axis = axis_of_coordinate(name);
113 if (!axis) {
115 "GField_AsciiMapFactory: coordinate name <", name, "> is not valid for symmetry <",
116 gfield_definitions.field_parameters["symmetry"], ">.");
117 return;
118 }
119 if (npoints < 2) {
121 "GField_AsciiMapFactory: coordinate <", name, "> needs at least 2 points; got ", npoints, ".");
122 }
123
124 np[*axis] = static_cast<unsigned>(npoints);
125 startMap[*axis] = min;
126 endMap[*axis] = max;
127 cellSize[*axis] = (max - min) / (npoints - 1);
128
129 // The map-file column for this coordinate is read in the same unit used in the min/max expression,
130 // so a bare column value is converted back to Geant4 units with that factor.
131 const std::string unit = [&]() {
132 for (const std::string& expr : {tokens[3], tokens[2]}) {
133 const auto star = expr.find('*');
134 if (star != std::string::npos) {
135 return gutilities::removeLeadingAndTrailingSpacesFromString(expr.substr(star + 1));
136 }
137 }
138 return std::string("mm");
139 }();
140
141 columns.push_back(Column{static_cast<int>(*axis), gutilities::getG4Number(1.0, unit)});
142}
143
144void GField_AsciiMapFactory::load_map_file() {
145 const std::string map_name = param_string("map", "");
146 if (map_name.empty()) {
148 "GField_AsciiMapFactory: no 'map' file given for field <", gfield_definitions.name, ">.");
149 }
150
151 // Resolve the map file. An explicit path (containing '/') is used as-is. Otherwise the lookup order
152 // is: the explicit `dir` parameter, then the directory of the YAML that defined the field (so a plain
153 // .yaml run from its own directory or referenced by absolute path just works), then the `fields`
154 // directory installed next to the plugin.
155 std::string path = map_name;
156 if (map_name.find('/') == std::string::npos) {
157 std::vector<std::string> candidates;
158 if (const std::string dir = param_string("dir", ""); !dir.empty()) { candidates.push_back(dir); }
159 if (!gfield_definitions.config_dir.empty()) { candidates.push_back(gfield_definitions.config_dir); }
160 candidates.push_back(field_maps_directory());
161
162 path.clear();
163 for (const auto& dir : candidates) {
164 const std::string trial = dir + "/" + map_name;
165 if (std::ifstream(trial).good()) { path = trial; break; }
166 }
167 if (path.empty()) {
169 "GField_AsciiMapFactory: cannot find map <", map_name, "> for field <",
170 gfield_definitions.name, "> in dir/config/plugin locations.");
171 }
172 }
173
174 std::ifstream in(path);
175 if (!in.good()) {
176 log->error(gfields::ERR_MAP_FILE_NOT_FOUND, "GField_AsciiMapFactory: cannot open map file <", path, ">.");
177 }
178 log->info(1, "Loading ASCII field map <", path, "> with symmetry <",
179 gfield_definitions.field_parameters["symmetry"], ">.");
180
181 // Total grid points and field-value scaling.
182 std::size_t total = np[0];
183 for (int d = 1; d < ndim; ++d) { total *= np[d]; }
184
185 const double field_scale = param_g4number("scale", "1") *
186 gutilities::getG4Number(1.0, param_string("field_unit", "gauss"));
187
188 B1.assign(total, 0.0f);
189 if (ncomp >= 2) { B2.assign(total, 0.0f); }
190 if (ncomp >= 3) { B3.assign(total, 0.0f); }
191
192 const double tolerance = 0.001; // relative cell-position tolerance for the grid-consistency check
193
194 std::size_t read_points = 0;
195 std::string line;
196 int line_number = 0;
197 while (std::getline(in, line)) {
198 ++line_number;
200 if (trimmed.empty() || trimmed[0] == '#') { continue; }
201
202 std::istringstream tokens(trimmed);
203
204 // Read the coordinate columns (file-column order), convert them to Geant4 units and turn each
205 // into its canonical axis index.
206 unsigned grid_index[3] = {0, 0, 0};
207 bool line_ok = true;
208 for (int c = 0; c < ndim; ++c) {
209 double raw = 0.0;
210 if (!(tokens >> raw)) { line_ok = false; break; }
211 const double coord = raw * columns[c].unitFactor;
212 const int axis = columns[c].axis;
213 const unsigned i = static_cast<unsigned>(
214 std::floor((coord - startMap[axis] + cellSize[axis] / 2) / cellSize[axis]));
215 grid_index[axis] = i;
216
217 const double expected = startMap[axis] + i * cellSize[axis];
218 if (cellSize[axis] != 0.0 && std::fabs(expected - coord) > tolerance * std::fabs(cellSize[axis])) {
219 log->warning("GField_AsciiMapFactory: ", path, ":", line_number,
220 " axis ", axis, " value ", coord, " does not match grid point ", expected, ".");
221 }
222 }
223
224 double comp[3] = {0.0, 0.0, 0.0};
225 for (int k = 0; k < ncomp && line_ok; ++k) {
226 if (!(tokens >> comp[k])) { line_ok = false; }
227 }
228 if (!line_ok) {
229 log->error(gfields::ERR_MAP_FILE_NOT_FOUND, "GField_AsciiMapFactory: ", path, ":", line_number,
230 " has fewer than ", ndim + ncomp, " numbers.");
231 }
232
233 std::size_t flat = (ndim == 2) ? idx2(grid_index[0], grid_index[1])
234 : idx3(grid_index[0], grid_index[1], grid_index[2]);
235 if (flat >= total) {
236 log->warning("GField_AsciiMapFactory: ", path, ":", line_number, " maps outside the grid; skipped.");
237 continue;
238 }
239
240 B1[flat] = static_cast<float>(comp[0] * field_scale);
241 if (ncomp >= 2) { B2[flat] = static_cast<float>(comp[1] * field_scale); }
242 if (ncomp >= 3) { B3[flat] = static_cast<float>(comp[2] * field_scale); }
243 ++read_points;
244 }
245
246 if (read_points != total) {
247 log->warning("GField_AsciiMapFactory: ", path, " provided ", read_points,
248 " points but the grid expects ", total, ".");
249 }
250 log->info(1, "ASCII field map <", gfield_definitions.name, "> loaded: ", read_points, " points.");
251}
252
254 gfield_definitions = gfd;
255
256 // Decode the symmetry once. ndim/ncomp follow from it.
257 const std::string sym = param_string("symmetry", "");
258 if (sym == "dipole-x") { symmetry = Symmetry::dipole_x; ndim = 2; ncomp = 1; }
259 else if (sym == "dipole-y") { symmetry = Symmetry::dipole_y; ndim = 2; ncomp = 1; }
260 else if (sym == "dipole-z") { symmetry = Symmetry::dipole_z; ndim = 2; ncomp = 1; }
261 else if (sym == "cylindrical-x") { symmetry = Symmetry::cyl_x; ndim = 2; ncomp = 2; }
262 else if (sym == "cylindrical-y") { symmetry = Symmetry::cyl_y; ndim = 2; ncomp = 2; }
263 else if (sym == "cylindrical-z") { symmetry = Symmetry::cyl_z; ndim = 2; ncomp = 2; }
264 else if (sym == "phi-segmented") { symmetry = Symmetry::phi_segmented; ndim = 3; ncomp = 3; }
265 else if (sym == "cartesian_3D") { symmetry = Symmetry::cartesian_3d; ndim = 3; ncomp = 3; }
266 else if (sym == "cartesian_3D_quadrant") { symmetry = Symmetry::cartesian_3d_quadrant; ndim = 3; ncomp = 3; }
267 else {
269 "GField_AsciiMapFactory: unknown symmetry <", sym, "> for field <",
270 gfield_definitions.name, ">.");
271 }
272
273 // Grid coordinates, in map-file column order.
274 columns.clear();
275 columns.reserve(ndim);
276 load_coordinate("coordinate1");
277 load_coordinate("coordinate2");
278 if (ndim == 3) { load_coordinate("coordinate3"); }
279
280 // Interpolation.
281 linear = (param_string("interpolation", "linear") != "none");
282
283 // Overall placement.
284 mapOrigin[0] = param_g4number("vx", "0");
285 mapOrigin[1] = param_g4number("vy", "0");
286 mapOrigin[2] = param_g4number("vz", "0");
287 mapRotation[0] = param_g4number("rx", "0*deg");
288 mapRotation[1] = param_g4number("ry", "0*deg");
289 mapRotation[2] = param_g4number("rz", "0*deg");
290 sinAlpha = std::sin(mapRotation[0]); cosAlpha = std::cos(mapRotation[0]);
291 sinBeta = std::sin(mapRotation[1]); cosBeta = std::cos(mapRotation[1]);
292 sinGamma = std::sin(mapRotation[2]); cosGamma = std::cos(mapRotation[2]);
293
294 load_map_file();
295}
296
297
298// ---------------------------------------------------------------------------------------------------
299// Field evaluation
300// ---------------------------------------------------------------------------------------------------
301
302void GField_AsciiMapFactory::GetFieldValue(const double pos[3], G4double* bfield) const {
303 bfield[0] = bfield[1] = bfield[2] = 0.0;
304
305 if (std::isnan(pos[0]) || std::isnan(pos[1]) || std::isnan(pos[2])) {
306 log->warning("GField_AsciiMapFactory: field coordinates requested are nan.");
307 return;
308 }
309
310 // Shift to the map frame.
311 const double x[3] = {pos[0] - mapOrigin[0], pos[1] - mapOrigin[1], pos[2] - mapOrigin[2]};
312
313 switch (symmetry) {
314 case Symmetry::dipole_x:
315 case Symmetry::dipole_y:
316 case Symmetry::dipole_z:
317 value_dipole(x, bfield);
318 break;
319 case Symmetry::cyl_x:
320 case Symmetry::cyl_y:
321 case Symmetry::cyl_z:
322 value_cylindrical(x, bfield);
323 break;
324 case Symmetry::phi_segmented:
325 value_phi_segmented(x, bfield);
326 break;
327 case Symmetry::cartesian_3d:
328 case Symmetry::cartesian_3d_quadrant:
329 value_cartesian3d(x, bfield);
330 break;
331 }
332
333 log->info(2, "ASCII field <", gfield_definitions.name, "> at (",
334 pos[0] / cm, ", ", pos[1] / cm, ", ", pos[2] / cm, ") cm = (",
335 bfield[0] / gauss, ", ", bfield[1] / gauss, ", ", bfield[2] / gauss, ") gauss");
336}
337
338
339void GField_AsciiMapFactory::value_dipole(const double x[3], double* bfield) const {
340 // Axis 0 = longitudinal, axis 1 = transverse.
341 double LC = 0.0, TC = 0.0;
342 if (symmetry == Symmetry::dipole_z) { TC = std::fabs(x[0]); LC = x[1]; }
343 else if (symmetry == Symmetry::dipole_x) { TC = std::fabs(x[1]); LC = x[2]; }
344 else /* dipole_y */ { TC = std::fabs(x[0]); LC = x[2]; }
345
346 if (LC < startMap[0] || TC < startMap[1]) { return; }
347 unsigned IL = static_cast<unsigned>(std::floor((LC - startMap[0]) / cellSize[0]));
348 unsigned IT = static_cast<unsigned>(std::floor((TC - startMap[1]) / cellSize[1]));
349 if (IL >= np[0] - 1 || IT >= np[1] - 1) { return; }
350
351 double b = 0.0;
352 if (!linear) {
353 if (std::fabs(startMap[0] + IL * cellSize[0] - LC) > std::fabs(startMap[0] + (IL + 1) * cellSize[0] - LC)) IL++;
354 if (std::fabs(startMap[1] + IT * cellSize[1] - TC) > std::fabs(startMap[1] + (IT + 1) * cellSize[1] - TC)) IT++;
355 b = B1[idx2(IL, IT)];
356 }
357 else {
358 const double xlr = (LC - (startMap[0] + IL * cellSize[0])) / cellSize[0];
359 const double xtr = (TC - (startMap[1] + IT * cellSize[1])) / cellSize[1];
360 const double b10 = B1[idx2(IL, IT)] * (1.0 - xtr) + B1[idx2(IL, IT + 1)] * xtr;
361 const double b11 = B1[idx2(IL + 1, IT)] * (1.0 - xtr) + B1[idx2(IL + 1, IT + 1)] * xtr;
362 b = b10 * (1.0 - xlr) + b11 * xlr;
363 }
364
365 if (symmetry == Symmetry::dipole_x) { bfield[0] = b; }
366 else if (symmetry == Symmetry::dipole_y) { bfield[1] = b; }
367 else { bfield[2] = b; }
368
369 rotate_field(bfield);
370}
371
372
373void GField_AsciiMapFactory::value_cylindrical(const double x[3], double* bfield) const {
374 // Axis 0 = transverse (radial), axis 1 = longitudinal.
375 double LC = 0.0, TC = 0.0, phi = 0.0;
376 if (symmetry == Symmetry::cyl_z) {
377 LC = x[2]; TC = std::sqrt(x[0] * x[0] + x[1] * x[1]); phi = G4ThreeVector(x[0], x[1], x[2]).phi();
378 }
379 else if (symmetry == Symmetry::cyl_x) {
380 LC = x[0]; TC = std::sqrt(x[1] * x[1] + x[2] * x[2]); phi = G4ThreeVector(x[1], x[2], x[0]).phi();
381 }
382 else /* cyl_y */ {
383 LC = x[1]; TC = std::sqrt(x[0] * x[0] + x[2] * x[2]); phi = G4ThreeVector(x[2], x[0], x[1]).phi();
384 }
385
386 if (TC < startMap[0] || LC < startMap[1]) { return; }
387 unsigned IT = static_cast<unsigned>(std::floor((TC - startMap[0]) / cellSize[0]));
388 unsigned IL = static_cast<unsigned>(std::floor((LC - startMap[1]) / cellSize[1]));
389 if (IT >= np[0] - 1 || IL >= np[1] - 1) { return; }
390
391 double b1 = 0.0, b2 = 0.0;
392 if (!linear) {
393 if (std::fabs(startMap[0] + IT * cellSize[0] - TC) > std::fabs(startMap[0] + (IT + 1) * cellSize[0] - TC)) IT++;
394 if (std::fabs(startMap[1] + IL * cellSize[1] - LC) > std::fabs(startMap[1] + (IL + 1) * cellSize[1] - LC)) IL++;
395 b1 = B1[idx2(IT, IL)];
396 b2 = B2[idx2(IT, IL)];
397 }
398 else {
399 const double xtr = (TC - (startMap[0] + IT * cellSize[0])) / cellSize[0];
400 const double xlr = (LC - (startMap[1] + IL * cellSize[1])) / cellSize[1];
401 const double b10 = B1[idx2(IT, IL)] * (1.0 - xtr) + B1[idx2(IT + 1, IL)] * xtr;
402 const double b11 = B1[idx2(IT, IL + 1)] * (1.0 - xtr) + B1[idx2(IT + 1, IL + 1)] * xtr;
403 b1 = b10 * (1.0 - xlr) + b11 * xlr;
404 const double b20 = B2[idx2(IT, IL)] * (1.0 - xtr) + B2[idx2(IT + 1, IL)] * xtr;
405 const double b21 = B2[idx2(IT, IL + 1)] * (1.0 - xtr) + B2[idx2(IT + 1, IL + 1)] * xtr;
406 b2 = b20 * (1.0 - xlr) + b21 * xlr;
407 }
408
409 if (symmetry == Symmetry::cyl_z) {
410 bfield[0] = b1 * std::cos(phi); bfield[1] = b1 * std::sin(phi); bfield[2] = b2;
411 }
412 else if (symmetry == Symmetry::cyl_x) {
413 bfield[0] = b2; bfield[1] = b1 * std::cos(phi); bfield[2] = b1 * std::sin(phi);
414 }
415 else /* cyl_y */ {
416 bfield[1] = b2; bfield[0] = b1 * std::sin(phi); bfield[2] = b1 * std::cos(phi);
417 }
418
419 rotate_field(bfield);
420}
421
422
423void GField_AsciiMapFactory::value_phi_segmented(const double x[3], double* bfield) const {
424 // Axis 0 = azimuthal, axis 1 = transverse, axis 2 = longitudinal. Fields are stored in the local
425 // (first-segment) frame and rotated back to the query phi.
426 double aC = std::atan2(x[1], x[0]) * rad; // phi
427 if (aC < 0) { aC += 360 * deg; }
428 const double tC = std::sqrt(x[0] * x[0] + x[1] * x[1]); // R
429 const double lC = x[2]; // Z
430
431 // Fold into the first 60-degree segment, keeping the rotation back to the lab.
432 double aLC = aC;
433 while (aLC / deg > 30) { aLC -= 60 * deg; }
434 const double dphi = aC - aLC;
435 const double aaLC = std::fabs(aLC);
436 const int sign = (aLC >= 0 ? 1 : -1);
437
438 if (aC < startMap[0] || tC < startMap[1] || lC < startMap[2]) { return; }
439 unsigned aI = static_cast<unsigned>(std::floor((aaLC - startMap[0]) / cellSize[0]));
440 unsigned tI = static_cast<unsigned>(std::floor((tC - startMap[1]) / cellSize[1]));
441 unsigned lI = static_cast<unsigned>(std::floor((lC - startMap[2]) / cellSize[2]));
442 if (aI >= np[0] - 1 || tI >= np[1] - 1 || lI >= np[2] - 1) { return; }
443
444 double mfield[3] = {0.0, 0.0, 0.0};
445 if (!linear) {
446 if (std::fabs(startMap[0] + aI * cellSize[0] - aaLC) > std::fabs(startMap[0] + (aI + 1) * cellSize[0] - aaLC)) aI++;
447 if (std::fabs(startMap[1] + tI * cellSize[1] - tC) > std::fabs(startMap[1] + (tI + 1) * cellSize[1] - tC)) tI++;
448 if (std::fabs(startMap[2] + lI * cellSize[2] - lC) > std::fabs(startMap[2] + (lI + 1) * cellSize[2] - lC)) lI++;
449 mfield[0] = B1[idx3(aI, tI, lI)];
450 mfield[1] = B2[idx3(aI, tI, lI)];
451 mfield[2] = B3[idx3(aI, tI, lI)];
452 }
453 else {
454 const double xaz = (aaLC - (startMap[0] + aI * cellSize[0])) / cellSize[0];
455 const double xtr = (tC - (startMap[1] + tI * cellSize[1])) / cellSize[1];
456 const double xlr = (lC - (startMap[2] + lI * cellSize[2])) / cellSize[2];
457
458 const std::vector<float>* comps[3] = {&B1, &B2, &B3};
459 for (int k = 0; k < 3; ++k) {
460 const std::vector<float>& B = *comps[k];
461 const double b00 = B[idx3(aI, tI, lI)] * (1 - xaz) + B[idx3(aI + 1, tI, lI)] * xaz;
462 const double b01 = B[idx3(aI, tI, lI + 1)] * (1 - xaz) + B[idx3(aI + 1, tI, lI + 1)] * xaz;
463 const double b10 = B[idx3(aI, tI + 1, lI)] * (1 - xaz) + B[idx3(aI + 1, tI + 1, lI)] * xaz;
464 const double b11 = B[idx3(aI, tI + 1, lI + 1)] * (1 - xaz) + B[idx3(aI + 1, tI + 1, lI + 1)] * xaz;
465 const double b0 = b00 * (1 - xtr) + b10 * xtr;
466 const double b1 = b01 * (1 - xtr) + b11 * xtr;
467 mfield[k] = b0 * (1 - xlr) + b1 * xlr;
468 }
469 }
470
471 // Rotate the local field back to the query azimuth.
472 bfield[0] = sign * mfield[0] * std::cos(dphi / rad) - mfield[1] * std::sin(dphi / rad);
473 bfield[1] = sign * mfield[0] * std::sin(dphi / rad) + mfield[1] * std::cos(dphi / rad);
474 bfield[2] = sign * mfield[2];
475
476 rotate_field(bfield);
477}
478
479
480void GField_AsciiMapFactory::value_cartesian3d(const double x[3], double* bfield) const {
481 // Axis 0 = X, axis 1 = Y, axis 2 = Z.
482 double XX = x[0], YY = x[1], ZZ = x[2];
483
484 if (symmetry == Symmetry::cartesian_3d_quadrant) {
485 // Fold the query point into the stored first quadrant (x>=0, y>=0).
486 if (x[0] >= 0 && x[1] >= 0) { XX = x[0]; YY = x[1]; }
487 else if (x[0] >= 0 && x[1] < 0) { XX = -x[1]; YY = x[0]; }
488 else if (x[0] < 0 && x[1] < 0) { XX = -x[0]; YY = -x[1]; }
489 else /* x[0] < 0 && x[1] >= 0 */ { XX = x[1]; YY = -x[0]; }
490 ZZ = x[2];
491 if (XX < 0 || YY < 0) { return; }
492 }
493
494 if (XX < startMap[0] || YY < startMap[1] || ZZ < startMap[2]) { return; }
495 if (XX >= endMap[0] || YY >= endMap[1] || ZZ >= endMap[2]) { return; }
496 const unsigned IXX = static_cast<unsigned>(std::floor((XX - startMap[0]) / cellSize[0]));
497 const unsigned IYY = static_cast<unsigned>(std::floor((YY - startMap[1]) / cellSize[1]));
498 const unsigned IZZ = static_cast<unsigned>(std::floor((ZZ - startMap[2]) / cellSize[2]));
499
500 double B[3] = {0.0, 0.0, 0.0};
501 if (!linear) {
502 unsigned ix = IXX, iy = IYY, iz = IZZ;
503 if (std::fabs(startMap[0] + ix * cellSize[0] - XX) > std::fabs(startMap[0] + (ix + 1) * cellSize[0] - XX)) ix++;
504 if (std::fabs(startMap[1] + iy * cellSize[1] - YY) > std::fabs(startMap[1] + (iy + 1) * cellSize[1] - YY)) iy++;
505 if (std::fabs(startMap[2] + iz * cellSize[2] - ZZ) > std::fabs(startMap[2] + (iz + 1) * cellSize[2] - ZZ)) iz++;
506 B[0] = B1[idx3(ix, iy, iz)];
507 B[1] = B2[idx3(ix, iy, iz)];
508 B[2] = B3[idx3(ix, iy, iz)];
509 }
510 else {
511 const double Xd = (XX - (startMap[0] + IXX * cellSize[0])) / cellSize[0];
512 const double Yd = (YY - (startMap[1] + IYY * cellSize[1])) / cellSize[1];
513 const double Zd = (ZZ - (startMap[2] + IZZ * cellSize[2])) / cellSize[2];
514
515 const std::vector<float>* comps[3] = {&B1, &B2, &B3};
516 for (int k = 0; k < 3; ++k) {
517 const std::vector<float>& Bk = *comps[k];
518 const double c00 = Bk[idx3(IXX, IYY, IZZ)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY, IZZ)] * Xd;
519 const double c01 = Bk[idx3(IXX, IYY, IZZ + 1)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY, IZZ + 1)] * Xd;
520 const double c10 = Bk[idx3(IXX, IYY + 1, IZZ)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY + 1, IZZ)] * Xd;
521 const double c11 = Bk[idx3(IXX, IYY + 1, IZZ + 1)] * (1 - Xd) + Bk[idx3(IXX + 1, IYY + 1, IZZ + 1)] * Xd;
522 const double c0 = c00 * (1 - Yd) + c10 * Yd;
523 const double c1 = c01 * (1 - Yd) + c11 * Yd;
524 B[k] = c0 * (1 - Zd) + c1 * Zd;
525 }
526 }
527
528 if (symmetry == Symmetry::cartesian_3d_quadrant) {
529 // Mirror the field components back to the query quadrant.
530 if (x[0] >= 0 && x[1] >= 0) { bfield[0] = B[0]; bfield[1] = B[1]; }
531 else if (x[0] >= 0 && x[1] < 0) { bfield[0] = B[1]; bfield[1] = -B[0]; }
532 else if (x[0] < 0 && x[1] < 0) { bfield[0] = -B[0]; bfield[1] = -B[1]; }
533 else /* x[0] < 0 && x[1] >= 0 */ { bfield[0] = -B[1]; bfield[1] = B[0]; }
534 bfield[2] = B[2];
535 }
536 else {
537 bfield[0] = B[0]; bfield[1] = B[1]; bfield[2] = B[2];
538 }
539
540 rotate_field(bfield);
541}
542
543
544void GField_AsciiMapFactory::rotate_field(double* bfield) const {
545 // Rotate the field axes, not the point: each rotation is the inverse of the point rotation.
546 if (mapRotation[0] != 0) {
547 const double yPrime = bfield[1] * cosAlpha + bfield[2] * sinAlpha;
548 const double zPrime = -bfield[1] * sinAlpha + bfield[2] * cosAlpha;
549 bfield[1] = yPrime; bfield[2] = zPrime;
550 }
551 if (mapRotation[1] != 0) {
552 const double xPrime = bfield[0] * cosBeta - bfield[2] * sinBeta;
553 const double zPrime = bfield[0] * sinBeta + bfield[2] * cosBeta;
554 bfield[0] = xPrime; bfield[2] = zPrime;
555 }
556 if (mapRotation[2] != 0) {
557 const double xPrime = bfield[0] * cosGamma + bfield[1] * sinGamma;
558 const double yPrime = -bfield[0] * sinGamma + bfield[1] * cosGamma;
559 bfield[0] = xPrime; bfield[1] = yPrime;
560 }
561}
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
GField * GFieldFactory(const std::shared_ptr< GOptions > &g)
constexpr int ERR_WRONG_FIELD_SYMMETRY
constexpr int ERR_MAP_FILE_NOT_FOUND
constexpr int 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