From 8361affe136db5d1d85bc3d865b7a2eb7f551244 Mon Sep 17 00:00:00 2001 From: Zhiwen Zhao Date: Sat, 14 Nov 2026 16:34:19 -0500 Subject: [PATCH] fix: correct higher multipole field components --- .../multipoles/gfield_multipoles.cc | 10 +- tests/meson.build | 13 +++ tests/multipole_correctness.cc | 97 +++++++++++++++++++ tests/multipole_correctness.yaml | 43 ++++++++ 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 tests/multipole_correctness.cc create mode 100644 tests/multipole_correctness.yaml diff --git a/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc b/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc index 98496719..693cf8c0 100644 --- a/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc +++ b/gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc @@ -111,12 +111,10 @@ void GField_MultipolesFactory::GetFieldValue(const double pos[3], G4double* bfie ra = std::pow(r / r0, static_cast(a)); } - // "Normal" multipole angular dependence: - // Bu = strength * r^(n-1) * cos((n-1)*phi) - // Bv = strength * r^(n-1) * sin((n-1)*phi) - // This yields a constant transverse dipole for n=1. - const double Bu = strength * ra * std::cos(a * phi); - const double Bv = strength * ra * std::sin(a * phi); + // Normal higher multipoles: a quadrupole has Bu proportional to v and Bv to u. + // Preserve the existing dipole direction along u when n=1 (a=0). + const double Bu = (a == 0) ? strength : strength * ra * std::sin(a * phi); + const double Bv = (a == 0) ? 0.0 : strength * ra * std::cos(a * phi); // Place (Bu,Bv) into the correct transverse components of B_local; axial component = 0 G4ThreeVector B_local(0, 0, 0); diff --git a/tests/meson.build b/tests/meson.build index 32b204b1..0032b78f 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -35,6 +35,19 @@ test('gfield_random_state', gfield_random_state, depends: [plugin_target_maps['gfieldmultipolesFactory']], env: project_test_env, suite: 'gfields') +multipole_correctness = executable( + 'test_multipole_correctness', + 'multipole_correctness.cc', + include_directories: gemc_includes, + dependencies: [yaml_cpp_dep, clhep_deps, geant4_core_deps, expat_dep, zlib_dep], + link_with: [lib_target_maps['gfields'], lib_target_maps['gfactory'], + lib_target_maps['glogging'], lib_target_maps['goptions'], lib_target_maps['guts']], + export_dynamic: true) +test('multipole_correctness', multipole_correctness, + args: [files('multipole_correctness.yaml'), '-plugin_path=' + build_root], + depends: [plugin_target_maps['gfieldmultipolesFactory']], + env: project_test_env, suite: 'gfields') + test('gui_catalogs', python_exe, args: [files('qt_catalogs.py')], env: project_test_env, suite: 'gui') test('gui_catalog_freshness', python_exe, diff --git a/tests/multipole_correctness.cc b/tests/multipole_correctness.cc new file mode 100644 index 00000000..42342764 --- /dev/null +++ b/tests/multipole_correctness.cc @@ -0,0 +1,97 @@ +#include +#include + +#include "G4SystemOfUnits.hh" + +#include +#include +#include +#include +#include +#include + +namespace { + +using Vector = std::array; + +struct Sample { + const char* name; + Vector position; + Vector expected; +}; + +Vector evaluate(const std::shared_ptr& field, const Vector& position) { + Vector value{}; + field->GetFieldValue(position.data(), value.data()); + return value; +} + +bool near(double value, double expected, double tolerance, const std::string& label) { + if (std::isfinite(value) && std::abs(value - expected) <= tolerance) { return true; } + std::cerr << label << ": expected " << expected << ", got " << value << '\n'; + return false; +} + +bool check_maxwell(const std::string& name, const std::shared_ptr& field) { + const Vector position{0.1*m, 0.2*m, 0.3*m}; + const double h = 0.1*mm; + double derivative[3][3]{}; + for (int axis = 0; axis < 3; ++axis) { + auto plus = position; + auto minus = position; + plus[axis] += h; + minus[axis] -= h; + const auto bp = evaluate(field, plus); + const auto bm = evaluate(field, minus); + for (int component = 0; component < 3; ++component) { + derivative[component][axis] = (bp[component] - bm[component]) / (2*h); + } + } + + const double tolerance = 1e-9*tesla/m; + bool passed = near(derivative[0][0] + derivative[1][1] + derivative[2][2], + 0.0, tolerance, name + " divergence"); + passed &= near(derivative[2][1] - derivative[1][2], 0.0, tolerance, name + " curl X"); + passed &= near(derivative[0][2] - derivative[2][0], 0.0, tolerance, name + " curl Y"); + passed &= near(derivative[1][0] - derivative[0][1], 0.0, tolerance, name + " curl Z"); + return passed; +} + +} // namespace + +int main(int argc, char* argv[]) { + auto options = std::make_shared(argc, argv, gfields::defineOptions()); + GMagneto magneto(options); + + // Independent reference vectors for G=1 T/m, and the normal sextupole at r0=1 m. + const Sample samples[] = { + {"quad_z", {10*mm, 20*mm, 0}, {0.02*tesla, 0.01*tesla, 0}}, + {"quad_z", {-20*mm, 10*mm, 0}, {0.01*tesla, -0.02*tesla, 0}}, + {"quad_z", {0, 0, 0}, {0, 0, 0}}, + {"quad_x", {0, 10*mm, 20*mm}, {0, 0.02*tesla, 0.01*tesla}}, + {"quad_y", {20*mm, 0, 10*mm}, {0.01*tesla, 0, 0.02*tesla}}, + {"quad_shifted", {40*mm, -20*mm, 50*mm}, {0.02*tesla, 0.01*tesla, 0}}, + {"quad_rolled", {10*mm, 20*mm, 0}, {-0.01*tesla, 0.02*tesla, 0}}, + {"sextupole", {0.1*m, 0.2*m, 0}, {0.04*tesla, -0.03*tesla, 0}}, + {"sextupole", {-0.1*m, 0.2*m, 0}, {-0.04*tesla, -0.03*tesla, 0}}, + {"dipole", {10*mm, 20*mm, 30*mm}, {1*tesla, 0, 0}}, + {"dipole_rolled", {10*mm, 20*mm, 30*mm}, + {0.7071067811865475*tesla, 0.7071067811865475*tesla, 0}}, + {"longitudinal", {10*mm, 20*mm, 30*mm}, {0, 1*tesla, 0}}, + }; + + bool passed = true; + for (const auto& sample : samples) { + const auto value = evaluate(magneto.getField(sample.name), sample.position); + for (int component = 0; component < 3; ++component) { + passed &= near(value[component], sample.expected[component], 1e-10*tesla, + std::string(sample.name) + " component " + std::to_string(component)); + } + } + for (const auto& name : magneto.getFieldNames()) { + passed &= check_maxwell(name, magneto.getField(name)); + } + if (!passed) { return EXIT_FAILURE; } + std::cout << "Multipole reference fields, divergence/curl, and dipole preservation passed.\n"; + return EXIT_SUCCESS; +} diff --git a/tests/multipole_correctness.yaml b/tests/multipole_correctness.yaml new file mode 100644 index 00000000..189266f6 --- /dev/null +++ b/tests/multipole_correctness.yaml @@ -0,0 +1,43 @@ +gmultipoles: + - name: quad_z + pole_number: 4 + rotaxis: Z + strength: 1*tesla + - name: quad_x + pole_number: 4 + rotaxis: X + strength: 1*tesla + - name: quad_y + pole_number: 4 + rotaxis: Y + strength: 1*tesla + - name: quad_shifted + pole_number: 4 + rotaxis: Z + strength: 1*tesla + vx: 30*mm + vy: -40*mm + vz: 50*mm + - name: quad_rolled + pole_number: 4 + rotaxis: Z + strength: 1*tesla + rotation_angle: 45*deg + - name: sextupole + pole_number: 6 + rotaxis: Z + strength: 1*tesla + - name: dipole + pole_number: 2 + rotaxis: Z + strength: 1*tesla + - name: dipole_rolled + pole_number: 2 + rotaxis: Z + strength: 1*tesla + rotation_angle: 45*deg + - name: longitudinal + pole_number: 2 + rotaxis: Y + strength: 1*tesla + longitudinal: true