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
10 changes: 4 additions & 6 deletions gemc/gfields/gfieldFactories/multipoles/gfield_multipoles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ void GField_MultipolesFactory::GetFieldValue(const double pos[3], G4double* bfie
ra = std::pow(r / r0, static_cast<double>(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);
Expand Down
13 changes: 13 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
97 changes: 97 additions & 0 deletions tests/multipole_correctness.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <gemc/gfields/gfield_options.h>
#include <gemc/gfields/gmagneto.h>

#include "G4SystemOfUnits.hh"

#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>

namespace {

using Vector = std::array<double, 3>;

struct Sample {
const char* name;
Vector position;
Vector expected;
};

Vector evaluate(const std::shared_ptr<GField>& 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<GField>& 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<GOptions>(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;
}
43 changes: 43 additions & 0 deletions tests/multipole_correctness.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading