rugnux: model validation does the same work in less time

--model validation (battery-only for users) was 18% of the battery's time. Every
number it produces is unchanged to the bit (p.mtz, maps, placed model and every
model-validation line of the report md5/diff-identical on 11 open sets); only
when and where the work runs changes:

- The bulk-solvent grid fit (FitModelScale, most of the CPU time) fits each
  solvent pair on a copy of gemmi::Scaling's target that takes
  |Fcalc + k_sol exp(-b_sol s^2) Fmask| once per pair instead of at every
  solver evaluation; same expressions, same types (new test checks a grid
  point against gemmi's own Scaling fit with ==).
- Fcalc density and the solvent mask are made on two threads; the model's
  structure factors beside the GPU engine reservation.
- The indexing probe fits the relabellings concurrently.
- The null's replicates run beside the real model's placement (they start
  from a snapshot of the model as read); one GPU engine per replicate plus
  one for the real fit instead of a cap of 4 (engines are interchangeable
  and deterministic).
- The 2mFo-DFc, mFo-DFc and anomalous maps are made and written
  concurrently; the placed model is written beside the reflection files.
- A rigid-body zone whose solvent-mask grid needs gemmi's shrink is sent to
  the CPU when the engines are reserved (ModelMaskGPU::ShrinkIsNoOp), instead
  of failing on the GPU and validating everything again on the CPU - the
  same CPU result, without the wasted first attempt.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-09 00:01:14 +02:00
1 parent 42555a55fe
commit f956eb25e8
11 files changed
+374 -134

No files matched your search

+69
View File
@@ -6,8 +6,11 @@
#include <cmath>
#include <algorithm>
#include <complex>
#include <random>
#include <utility>
#include "../image_analysis/structure_refinement/ModelScaling.h"
#include "gemmi/asumask.hpp"
#include "gemmi/scaling.hpp"
#include "gemmi/symmetry.hpp"
#include "gemmi/unitcell.hpp"
@@ -164,3 +167,69 @@ TEST_CASE("ModelScaling is the same on any number of threads") {
CHECK(serial.b_star.u22 == parallel.b_star.u22);
CHECK(serial.b_star.u33 == parallel.b_star.u33);
}
// FitModelScale fits each solvent pair of its grid on a copy of gemmi::Scaling's target that takes the
// solvent term once per pair (ModelScaling.cpp). On a box of a single pair it has to give exactly what
// gemmi's own Scaling gives for that pair - to the bit, since the validation's numbers are made of it.
TEST_CASE("ModelScaling fits a grid point exactly as gemmi's Scaling does") {
const gemmi::UnitCell cell(61.0, 72.0, 83.0, 90.0, 101.0, 90.0);
const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 1 21 1");
REQUIRE(sg != nullptr);
const gemmi::GroupOps gops = sg->operations();
const gemmi::ReciprocalAsu asu(sg);
gemmi::Scaling<float> scaling(cell, sg);
scaling.use_solvent = true;
std::mt19937 rng(20261008);
std::uniform_real_distribution<double> uniform(0.0, 1.0);
const gemmi::SMat33<double> b_star = gemmi::SMat33<double>{22, 31, 27, 0, 3, 0}.transformed_by(cell.frac.mat);
const gemmi::Miller lim = cell.get_hkl_limits(2.5);
for (int h = -lim[0]; h <= lim[0]; h++)
for (int k = -lim[1]; k <= lim[1]; k++)
for (int l = -lim[2]; l <= lim[2]; l++) {
const gemmi::Miller m{{h, k, l}};
if ((h == 0 && k == 0 && l == 0) || cell.calculate_d(m) < 2.5 || !asu.is_in(m)
|| gops.is_systematically_absent(m))
continue;
const double stol2 = cell.calculate_stol_sq(m);
const std::complex<float> fc(std::polar(100.0 * std::exp(-8.0 * stol2) * (0.2 + uniform(rng)),
6.283 * uniform(rng)));
const std::complex<float> fm(std::polar(900.0 * std::exp(-20.0 * stol2), 6.283 * uniform(rng)));
const std::complex<double> total = std::complex<double>(fc)
+ 0.4 * std::exp(-50.0 * stol2) * std::complex<double>(fm);
const double fobs = 2.5 * std::exp(-0.25 * b_star.r_u_r(m)) * std::abs(total)
* (0.9 + 0.2 * uniform(rng));
gemmi::Scaling<float>::Point p{};
p.hkl = m;
p.stol2 = stol2;
p.fcmol = fc;
p.fmask = fm;
p.fobs = static_cast<float>(fobs);
p.sigma = static_cast<float>(0.05 * fobs + 0.5);
scaling.points.push_back(p);
}
REQUIRE(scaling.points.size() > 1000);
for (const auto &[k_sol, b_sol] : {std::pair{0.35, 40.0}, std::pair{0.20, 70.0}}) {
gemmi::Scaling<float> ours = scaling;
FitModelScale(ours, ModelScaleBox{k_sol, k_sol, b_sol, b_sol}, 4);
gemmi::Scaling<float> gemmis = scaling;
gemmis.fix_k_sol = true;
gemmis.fix_b_sol = true;
gemmis.k_sol = k_sol;
gemmis.b_sol = b_sol;
gemmis.fit_isotropic_b_approximately();
gemmis.fit_parameters();
CHECK(ours.k_sol == k_sol);
CHECK(ours.b_sol == b_sol);
CHECK(ours.k_overall == gemmis.k_overall);
CHECK(ours.b_star.u11 == gemmis.b_star.u11);
CHECK(ours.b_star.u22 == gemmis.b_star.u22);
CHECK(ours.b_star.u33 == gemmis.b_star.u33);
CHECK(ours.b_star.u12 == gemmis.b_star.u12);
CHECK(ours.b_star.u13 == gemmis.b_star.u13);
CHECK(ours.b_star.u23 == gemmis.b_star.u23);
CHECK(ours.calculate_r_factor() == gemmis.calculate_r_factor());
}
}