Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,8 @@ void GField_MultipolesFactory::load_field_definitions(GFieldDefinition gfd) {

log->info(1, "Rotation axis: %1", rotaxis);
strength = get_field_parameter_double("strength");
// Bare strengths are documented in Tesla; explicit expressions already carry units.
if (gfield_definitions.field_parameters.at("strength").find('*') == std::string::npos) {
strength *= CLHEP::tesla;
}
}
5 changes: 5 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,8 @@ material_optics = executable('test_material_optics', 'material_optics.cc',
dependencies: gui_probe_dependencies,
link_with: all_libs)
test('material_optics', material_optics, suite: 'gsystem')

test('multipole_units', python_exe,
args: [files('multipole_units.py'), gemc, '--plugin-path=' + build_root],
depends: plugin_target_maps.values(),
env: project_test_env, suite: 'gfields')
28 changes: 28 additions & 0 deletions tests/multipole_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Bare strengths use the documented Tesla default; explicit units retain meaning."""
import math
from pathlib import Path
import re
import subprocess
import sys
import os
import tempfile

for argument in sys.argv[2:]:
if argument.startswith("--plugin-path="):
os.environ["GEMC_PLUGIN_PATH"] = argument.split("=", 1)[1]

binary = str(Path(sys.argv[1]).resolve())
units = {'T': 1, 'mT': 1e-3, 'G': 1e-4, 'kG': 0.1, 'uT': 1e-6, 'nT': 1e-9}
with tempfile.TemporaryDirectory() as directory:
for expression, expected in [('1', 1), ('1*tesla', 1), ('10000*gauss', 1), ('-1', -1), ('0', 0), ('0.5*tesla', 0.5)]:
result = subprocess.run([binary, '-gmultipoles=[{name: field, pole_number: 2, rotaxis: Z, strength: '+expression+'}]', '-fieldAt=0*mm 0*mm 0*mm'], cwd=directory, capture_output=True, text=True, check=True)
match = re.search(r'Bx=([-+\deE.]+) (\w+)', result.stdout)
assert match, result.stdout
actual = float(match[1])*units[match[2]]
assert math.isclose(actual, expected, rel_tol=1e-6, abs_tol=1e-12), (expression, expected, actual)
result = subprocess.run([binary, '-gmultipoles=[{name: centered, pole_number: 4, rotaxis: Z, strength: 1*tesla, vx: 10*cm}]', '-fieldAt=100*mm 0*mm 0*mm'], cwd=directory, capture_output=True, text=True, check=True)
assert 'Bx=0 G' in result.stdout and 'By=0 G' in result.stdout, result.stdout
rotated=subprocess.run([binary,'-gmultipoles=[{name: rotated, pole_number: 2, rotaxis: Z, strength: 1*tesla, rotation_angle: 90*deg}]','-fieldAt=0*mm 0*mm 0*mm'],cwd=directory,capture_output=True,text=True,check=True)
match=re.search(r'By=([-+\deE.]+) (\w+)',rotated.stdout)
assert match and math.isclose(float(match[1])*units[match[2]],1,rel_tol=1e-6),rotated.stdout
print('Bare/explicit field strengths and existing origin units passed.')
Loading