// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include #include #include "../common/Definitions.h" #include "../common/JFJochMath.h" #include "../image_analysis/scale_merge/IceRingIntensity.h" namespace { // A Wilson crystal (B = 20 A^2) to 1.8 A, acentric intensities with 10% plus constant noise, with // an additive powder ring of strength `ring(d)` (in units of the local mean) on the core of // every hexagonal-ice ring - or, with `whole_band`, over core and flanks alike. template std::vector Crystal(F ring, bool whole_band = false) { std::mt19937 rng(4321); std::exponential_distribution wilson(1.0); std::normal_distribution noise(0.0, 1.0); std::uniform_real_distribution s2(1.0 / (8.0 * 8.0), 1.0 / (1.8 * 1.8)); std::vector out; for (int i = 0; i < 40000; ++i) { MergedReflection r; r.d = static_cast(1.0 / std::sqrt(s2(rng))); const double I_mean = 1000.0 * std::exp(-0.5 * 20.0 / (r.d * r.d)); double I_true = I_mean * wilson(rng); const double q = 2.0 * PI / r.d; for (const float ice_d : ICE_RING_RES_A) if (std::fabs(q - 2.0 * PI / ice_d) < (whole_band ? ICE_INTENSITY_BAND_Q : ICE_INTENSITY_CORE_Q)) I_true += ring(ice_d) * I_mean; r.sigma = static_cast(std::sqrt(0.01 * I_true * I_true + 25.0)); r.I = static_cast(I_true + r.sigma * noise(rng)); out.push_back(r); } return out; } } TEST_CASE("IceRingIntensity_clean_crystal_is_not_ice") { const auto m = MeasureIceRingIntensity(Crystal([](float) { return 0.0; })); REQUIRE(m.n_rings >= 5); CHECK(!m.Detected()); CHECK(std::fabs(m.amplitude_z) < 3.0); CHECK(std::fabs(m.contrast_z) < 3.0); for (const auto &r : m.rings) CHECK(std::fabs(r.z) < 4.0); } TEST_CASE("IceRingIntensity_hexagonal_ring_system_is_detected") { // Ice in proportion to its line strengths: the 3.90 A ring adds 100% of the local mean. const auto m = MeasureIceRingIntensity(Crystal([](float d) { return d > 3.8f ? 1.0 : d > 3.6f ? 0.75 : d > 3.4f ? 0.53 : d > 2.2f && d < 2.3f ? 0.39 : 0.0; })); REQUIRE(m.Detected()); CHECK(m.amplitude_z > 8.0); CHECK(m.contrast_z > 5.0); for (const auto &r : m.rings) if (r.ring_d > 3.8f) CHECK(r.ratio == Catch::Approx(2.0).margin(0.2)); } TEST_CASE("IceRingIntensity_one_strong_shell_is_not_ice") { // A crystal whose transform is strong over a shell at one ring position, wider than the ring: the // cores stand out against the Wilson line but not against their flanks. const auto m = MeasureIceRingIntensity(Crystal([](float d) { return d > 2.6f && d < 2.7f ? 1.0 : 0.0; }, true)); for (const auto &r : m.rings) if (r.ring_d > 2.6f && r.ring_d < 2.7f) { CHECK(r.z > 5.0); CHECK(r.ratio_flank == Catch::Approx(r.ratio).margin(0.2)); } CHECK(!m.Detected()); } TEST_CASE("IceRingIntensity_depressed_cores_are_a_ring_too") { // A ring in the background annulus beside the spots takes intensity out of the cores instead. const auto m = MeasureIceRingIntensity(Crystal([](float d) { return d > 3.8f ? -0.5 : d > 3.6f ? -0.4 : d > 3.4f ? -0.3 : d > 2.2f && d < 2.3f ? -0.25 : 0.0; })); REQUIRE(m.Detected()); CHECK(m.contrast_z < -5.0); }