diff --git a/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc b/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc index 98496719..d8d832fe 100644 --- a/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc +++ b/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc @@ -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; + } } diff --git a/tests/meson.build b/tests/meson.build index 32b204b1..b92809e7 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -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') diff --git a/tests/multipole_units.py b/tests/multipole_units.py new file mode 100644 index 00000000..181b2efc --- /dev/null +++ b/tests/multipole_units.py @@ -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.')