From db4af06a2b588c8b8dceff53900bab9f00e2a1ab Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 5 Sep 2026 14:29:22 +0200 Subject: [PATCH] integration: the sensor efficiency reaches the stills GPU predictor too The efficiency correction was added to the rotation predictor on both the CPU and the GPU, and to the stills predictor on the CPU, but not to the stills predictor on the GPU - which the factory selects for stills whenever a card is present. So the correction was applied on a machine without a GPU and silently skipped on every machine with one: 6.6% apart on the geometry the tests use, 23.9% at 18 keV through a 450 micron sensor. The parity test that should have caught this is the reason it was not caught: it compares the predicted position and resolution of every reflection and nothing else, so a factor carried alongside them is invisible to it. It now compares the prescaling factor as well, with an assertion that the factor is not uniformly one so the comparison cannot pass vacuously, and the rotation path - which had no parity test at all - gets the same one. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01EFEJG6WBQv8th4UJFNe53N --- docs/CHANGELOG.md | 1 + .../bragg_prediction/BraggPredictionGPU.cu | 26 ++++++- .../bragg_prediction/BraggPredictionGPU.h | 5 ++ tests/CalcBraggPredictionTest.cpp | 67 +++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3ffead23b..1ec333f6b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -12,6 +12,7 @@ * The rugnux results report omits `FITTED_RESOLUTION` where the CC1/2 curve it is fitted on never falls off, instead of asserting a resolution the shell table refutes. * `rugnux` corrects each reflection for the sensor's quantum efficiency at the angle the diffracted beam meets the detector. +* `rugnux` applies the sensor quantum-efficiency correction to still images processed on a GPU, which were left uncorrected while the same data on a CPU-only machine was corrected. * `rugnux` takes the sensor attenuation length from tabulated coefficients rather than a wavelength-cubed approximation, which corrects the parallax term of the spot-width variance on CdTe sensors. * `rugnux --model` treats the model as a hypothesis: it decides the enantiomorph and the indexing only where its R-work beats that of the same model in random orientations, and a model the data reject is still scored, placed and mapped, but leaves the reflection files byte for byte what a run with no model writes. * `rugnux --model` places the model against the data as a rigid body before scoring it, writes sigma_A-weighted 2mFo-DFc and mFo-DFc maps in place of the unweighted 2Fo-Fc and Fo-Fc, and writes the model as it was placed - `_model.cif`, and `_model.pdb` where the PDB format can express the cell - in the cell and space group of the reflection files beside it. diff --git a/image_analysis/bragg_prediction/BraggPredictionGPU.cu b/image_analysis/bragg_prediction/BraggPredictionGPU.cu index 0dbb1e7b1..d0ea647ad 100644 --- a/image_analysis/bragg_prediction/BraggPredictionGPU.cu +++ b/image_analysis/bragg_prediction/BraggPredictionGPU.cu @@ -4,6 +4,7 @@ #include #include "../../common/JFJochMath.h" #include "BraggPredictionGPU.h" +#include "../SensorAbsorption.h" #ifdef JFJOCH_USE_CUDA #include "../indexing/CUDAMemHelpers.h" @@ -117,6 +118,20 @@ namespace { float x = C.beam_x + Srx * coeff; float y = C.beam_y + Sry * coeff; if (x < 0.0f || x >= C.det_width_pxl || y < 0.0f || y >= C.det_height_pxl) return false; + + // Sensor quantum efficiency at this reflection's angle of incidence on the detector. + // Sr is the diffracted direction in the detector's own frame, so Srz over its length is + // the cosine to the detector NORMAL - which carries detector tilt for free. Mirrors + // sensor_absorption::SensorQE::Factor on the CPU side. + float qe_corr = 1.0f; + if (C.qe_a0 > 0.0f) { + float cos_alpha = Srz / S_len; + if (cos_alpha > 1e-3f) { + float qe = 1.0f - expf(-C.qe_a0 / cos_alpha); + if (qe > 0.0f) qe_corr = C.qe_qe0 / qe; + } + } + out.h = h; out.k = k; out.l = l; @@ -127,10 +142,10 @@ namespace { out.observed_y = NAN; out.d = 1.0f / sqrtf(recip_sq); out.dist_ewald = dist_ewald; - out.prescaling_corr = 1.0f; + out.prescaling_corr = qe_corr; out.partiality = 1.0f; out.zeta = 1.0f; - out.image_scale_corr = 1.0f; + out.image_scale_corr = qe_corr; return true; } @@ -171,6 +186,13 @@ namespace { kc.one_over_wavelength = 1.0f / geom.GetWavelength_A(); kc.ewald_cutoff = ewald_dist_cutoff; kc.bandwidth_sigma = bandwidth_sigma; + + const auto &det = experiment.GetDetectorSetup(); + const auto sensor_qe = sensor_absorption::SensorQE::Build( + det.GetSensorMaterial(), det.GetSensorThickness_um(), geom.GetWavelength_A()); + kc.qe_a0 = sensor_qe.active ? sensor_qe.a0 : 0.0f; + kc.qe_qe0 = sensor_qe.qe0; + kc.Astar = lattice.Astar(); kc.Bstar = lattice.Bstar(); kc.Cstar = lattice.Cstar(); diff --git a/image_analysis/bragg_prediction/BraggPredictionGPU.h b/image_analysis/bragg_prediction/BraggPredictionGPU.h index 5fab081c3..b391d0b5a 100644 --- a/image_analysis/bragg_prediction/BraggPredictionGPU.h +++ b/image_analysis/bragg_prediction/BraggPredictionGPU.h @@ -19,6 +19,11 @@ struct KernelConsts { float one_over_dmax_sq; float ewald_cutoff; float bandwidth_sigma; // relative Δλ/λ (sigma); 0 = monochromatic + // Angle-dependent sensor efficiency, reduced to the two per-dataset numbers the kernel needs: + // the optical thickness at normal incidence and the absorbed fraction there. qe_a0 = 0 leaves + // every reflection untouched (see sensor_absorption::SensorQE). + float qe_a0; + float qe_qe0; Coord Astar, Bstar, Cstar, S0; float rot[9]; char centering; diff --git a/tests/CalcBraggPredictionTest.cpp b/tests/CalcBraggPredictionTest.cpp index 49f19e28f..a1f2df524 100644 --- a/tests/CalcBraggPredictionTest.cpp +++ b/tests/CalcBraggPredictionTest.cpp @@ -205,6 +205,10 @@ TEST_CASE("BraggPrediction_systematic_absences") { #ifdef JFJOCH_USE_CUDA #include "../image_analysis/bragg_prediction/BraggPredictionGPU.h" +#include "../image_analysis/bragg_prediction/BraggPredictionRotGPU.h" +#include "../image_analysis/bragg_prediction/BraggPredictionRot.h" +#include +#include TEST_CASE("BraggPredictionGPU") { DiffractionExperiment experiment(DetJF4M()); @@ -398,6 +402,7 @@ TEST_CASE("BraggPrediction_CPU_GPU_consistency_tilted") { // Check that each CPU reflection has a matching GPU reflection int matched = 0; + float min_corr = 1.0f; for (int i = 0; i < cpu_count; ++i) { const auto& cpu_r = cpu_pred.GetReflections().at(i); auto key = std::make_tuple(cpu_r.h, cpu_r.k, cpu_r.l); @@ -407,11 +412,73 @@ TEST_CASE("BraggPrediction_CPU_GPU_consistency_tilted") { CHECK(cpu_r.predicted_x == Catch::Approx(gpu_r.predicted_x).margin(0.1)); CHECK(cpu_r.predicted_y == Catch::Approx(gpu_r.predicted_y).margin(0.1)); CHECK(cpu_r.d == Catch::Approx(gpu_r.d).margin(0.01)); + // The prescaling factor is part of the prediction, not a downstream product: the + // sensor-efficiency term lived on the CPU path alone for a while because nothing here + // compared it. + CHECK(cpu_r.prescaling_corr == Catch::Approx(gpu_r.prescaling_corr).epsilon(1e-4)); + CHECK(cpu_r.image_scale_corr == Catch::Approx(gpu_r.image_scale_corr).epsilon(1e-4)); + min_corr = std::min(min_corr, cpu_r.prescaling_corr); matched++; } } // Most reflections should match (allow for some numerical differences at boundaries) CHECK(matched > cpu_count * 0.95); + // ... and the comparison above must not be vacuous: on this geometry (320 um Si at 12 keV, + // reflections out to 2 A) the sensor correction is several per cent, so a factor that stayed + // at 1 on both sides would mean the correction had been dropped from BOTH paths. + CHECK(min_corr < 0.99f); +} + +TEST_CASE("BraggPredictionRot_CPU_GPU_consistency_tilted") { + // The rotation counterpart of the test above. Same purpose: every per-reflection quantity the + // two implementations both produce has to agree, the prescaling factor included. + DiffractionExperiment experiment(DetJF4M()); + experiment.DetectorDistance_mm(100.0).BeamX_pxl(1500.0).BeamY_pxl(1000.0) + .PoniRot1_rad(0.04).PoniRot2_rad(-0.025) + .IncidentEnergy_keV(12.0) + .Goniometer(GoniometerAxis("omega", 0.0f, 0.1f, Coord(-1, 0, 0), {})); + + CrystalLattice lattice(Coord{30, 10, 0}, Coord{-15, 45, 0}, Coord{0, 0, 150}); + + BraggPredictionSettings settings{ + .high_res_A = 2.0, + .ewald_dist_cutoff = 0.0015, + .max_h = 30, .max_k = 30, .max_l = 30 + }; + + BraggPredictionRot cpu_pred; + BraggPredictionRotGPU gpu_pred; + + int cpu_count = cpu_pred.Calc(experiment, lattice, settings); + int gpu_count = gpu_pred.Calc(experiment, lattice, settings); + + REQUIRE(cpu_count > 0); + REQUIRE(gpu_count > 0); + + std::map, const Reflection*> gpu_refl_map; + for (int i = 0; i < gpu_count; ++i) { + const auto& r = gpu_pred.GetReflections().at(i); + gpu_refl_map[{r.h, r.k, r.l}] = &r; + } + + int matched = 0; + float min_corr = 1.0f; + for (int i = 0; i < cpu_count; ++i) { + const auto& cpu_r = cpu_pred.GetReflections().at(i); + auto it = gpu_refl_map.find(std::make_tuple(cpu_r.h, cpu_r.k, cpu_r.l)); + if (it != gpu_refl_map.end()) { + const auto& gpu_r = *it->second; + CHECK(cpu_r.predicted_x == Catch::Approx(gpu_r.predicted_x).margin(0.1)); + CHECK(cpu_r.predicted_y == Catch::Approx(gpu_r.predicted_y).margin(0.1)); + CHECK(cpu_r.d == Catch::Approx(gpu_r.d).margin(0.01)); + CHECK(cpu_r.prescaling_corr == Catch::Approx(gpu_r.prescaling_corr).epsilon(1e-3)); + min_corr = std::min(min_corr, cpu_r.prescaling_corr); + matched++; + } + } + + CHECK(matched > cpu_count * 0.95); + CHECK(min_corr < 0.99f); } #endif