diff --git a/rugnux/ModelScaling.cpp b/rugnux/ModelScaling.cpp index 9a35b929d..e0c64e626 100644 --- a/rugnux/ModelScaling.cpp +++ b/rugnux/ModelScaling.cpp @@ -5,6 +5,9 @@ #include #include +#include + +#include "../common/ParallelFor.h" namespace { @@ -34,7 +37,15 @@ double RFactor(const gemmi::Scaling &scaling) { // pair is held fixed at each grid point and only the overall scale and the symmetry-constrained // anisotropic B are refined, which is the well-conditioned half of the problem and is left to // gemmi's solver rather than reimplemented. -ModelScaleReport FitModelScale(gemmi::Scaling &scaling, ModelScaleBox box) { +// +// Every grid point is its own fit: fit_isotropic_b_approximately() sets k_overall and b_star from the +// data and the point's solvent pair alone, so a point does not depend on the one fitted before it, and +// the points run in parallel, each chunk on its own copy of `scaling`. The winner is then read off in +// grid order with the serial rule (lowest finite R, the first on a tie), so the answer is the serial +// loop's bit for bit. The one exception is fit_isotropic_b_approximately() finding five or fewer +// reflections to fit on - it then returns without setting anything and a point WOULD start from where +// the previous one ended - so there the grid is walked in order, on `scaling` itself, as it always was. +ModelScaleReport FitModelScale(gemmi::Scaling &scaling, ModelScaleBox box, size_t nthreads) { ModelScaleReport report; report.n_points = static_cast(scaling.points.size()); if (scaling.points.size() < 20) @@ -44,36 +55,63 @@ ModelScaleReport FitModelScale(gemmi::Scaling &scaling, ModelScaleBox box scaling.fix_k_sol = true; // the grid owns the solvent pair; the solver never sees it scaling.fix_b_sol = true; + // The reflections fit_isotropic_b_approximately() fits on (its own filter). + int n_isotropic = 0; + for (const auto &p : scaling.points) + if (!(p.fobs < 1 || p.fobs < p.sigma)) + ++n_isotropic; + const bool independent = n_isotropic > 5; + double best_r = -1, best_k_sol = 0.35, best_b_sol = 46.0, best_k_overall = 1.0; gemmi::SMat33 best_b_star{0, 0, 0, 0, 0, 0}; - auto try_point = [&](double k_sol, double b_sol) { - scaling.k_sol = k_sol; - scaling.b_sol = b_sol; - scaling.fit_isotropic_b_approximately(); // a fresh starting point for this solvent pair - scaling.fit_parameters(); // k_overall + anisotropic B only - ++report.n_grid; - const double r = RFactor(scaling); - // A diverged fit gives r = NaN; latched as best_r it wins every later r < best_r. - if (std::isfinite(r) && (best_r < 0 || r < best_r)) { - best_r = r; - best_k_sol = k_sol; - best_b_sol = b_sol; - best_k_overall = scaling.k_overall; - best_b_star = scaling.b_star; + struct PointFit { double k_sol, b_sol, r = NAN, k_overall = 1.0; gemmi::SMat33 b_star{0, 0, 0, 0, 0, 0}; }; + auto fit_point = [](gemmi::Scaling &s, PointFit &pf) { + s.k_sol = pf.k_sol; + s.b_sol = pf.b_sol; + s.fit_isotropic_b_approximately(); // a fresh starting point for this solvent pair + s.fit_parameters(); // k_overall + anisotropic B only + pf.r = RFactor(s); + pf.k_overall = s.k_overall; + pf.b_star = s.b_star; + }; + auto try_points = [&](std::vector &pts) { + if (independent) + ParallelChunks(static_cast(pts.size()), nthreads, [&](int lo, int hi) { + gemmi::Scaling local = scaling; + for (int i = lo; i < hi; ++i) + fit_point(local, pts[i]); + }); + else + for (auto &pf : pts) + fit_point(scaling, pf); + for (const auto &pf : pts) { + ++report.n_grid; + // A diverged fit gives r = NaN; latched as best_r it wins every later r < best_r. + if (std::isfinite(pf.r) && (best_r < 0 || pf.r < best_r)) { + best_r = pf.r; + best_k_sol = pf.k_sol; + best_b_sol = pf.b_sol; + best_k_overall = pf.k_overall; + best_b_star = pf.b_star; + } } }; // Coarse pass over the whole box, then one refinement pass around the winner. + std::vector coarse; for (double ks = box.k_lo; ks <= box.k_hi + 1e-9; ks += 0.05) for (double bs = box.b_lo; bs <= box.b_hi + 1e-9; bs += 10.0) - try_point(ks, bs); + coarse.push_back(PointFit{ks, bs}); + try_points(coarse); const double k0 = best_k_sol, b0 = best_b_sol; const double k_hi2 = std::min(box.k_hi, k0 + 0.05); const double b_hi2 = std::min(box.b_hi, b0 + 10.0); + std::vector fine; for (double ks = std::max(box.k_lo, k0 - 0.05); ks <= k_hi2 + 1e-9; ks += 0.025) for (double bs = std::max(box.b_lo, b0 - 10.0); bs <= b_hi2 + 1e-9; bs += 5.0) - try_point(ks, bs); + fine.push_back(PointFit{ks, bs}); + try_points(fine); scaling.k_sol = best_k_sol; scaling.b_sol = best_b_sol; diff --git a/rugnux/ModelScaling.h b/rugnux/ModelScaling.h index 0600097d1..9217d6cfc 100644 --- a/rugnux/ModelScaling.h +++ b/rugnux/ModelScaling.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "gemmi/scaling.hpp" // Fit the overall scale, the anisotropic B and the flat bulk solvent of a model to observed @@ -31,4 +33,6 @@ struct ModelScaleReport { double r_work_fit = 1.0; // R on the fitted reflections, at the chosen solution }; -ModelScaleReport FitModelScale(gemmi::Scaling &scaling, ModelScaleBox box = {}); +// nthreads: the grid points are fitted in parallel on up to this many threads; the answer does not +// depend on it. +ModelScaleReport FitModelScale(gemmi::Scaling &scaling, ModelScaleBox box = {}, size_t nthreads = 1); diff --git a/rugnux/ModelValidation.cpp b/rugnux/ModelValidation.cpp index d85b0430b..b3120e309 100644 --- a/rugnux/ModelValidation.cpp +++ b/rugnux/ModelValidation.cpp @@ -545,7 +545,7 @@ ModelValidationResult ValidateAgainstModel(const std::vector & gemmi::Scaling scaling(ucell, sg); scaling.use_solvent = true; scaling.prepare_points(out.fmodel, fobs_work, &ms.fmask); - FitModelScale(scaling); + FitModelScale(scaling, {}, nthreads); scaling.scale_data(out.fmodel, &ms.fmask); // out.fmodel now holds the scaled, solvent-corrected Fmodel out.k_sol = scaling.k_sol; out.b_sol = scaling.b_sol; diff --git a/tests/ModelScalingTest.cpp b/tests/ModelScalingTest.cpp index 155ece16a..28aa20bbc 100644 --- a/tests/ModelScalingTest.cpp +++ b/tests/ModelScalingTest.cpp @@ -141,3 +141,26 @@ TEST_CASE("ModelScaling adds nothing when there is no solvent to model") { CHECK(scaling.b_sol >= 10.0); CHECK(scaling.b_sol <= 80.0); } + +// The grid points are fitted in parallel; the answer must be the serial one, bit for bit. +TEST_CASE("ModelScaling is the same on any number of threads") { + const gemmi::UnitCell cell = Cell(); + const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 21 21 21"); + REQUIRE(sg != nullptr); + gemmi::Scaling serial(cell, sg); + serial.use_solvent = true; + MakePoints(serial, cell, /*k_overall=*/0.5, /*b_iso=*/20.0, + /*k_sol=*/0.35, /*b_sol=*/45.0, /*d_min=*/2.0, /*d_max=*/50.0); + gemmi::Scaling parallel = serial; + + const ModelScaleReport r1 = FitModelScale(serial, {}, 1); + const ModelScaleReport r8 = FitModelScale(parallel, {}, 8); + CHECK(r1.n_grid == r8.n_grid); + CHECK(r1.r_work_fit == r8.r_work_fit); + CHECK(serial.k_sol == parallel.k_sol); + CHECK(serial.b_sol == parallel.b_sol); + CHECK(serial.k_overall == parallel.k_overall); + CHECK(serial.b_star.u11 == parallel.b_star.u11); + CHECK(serial.b_star.u22 == parallel.b_star.u22); + CHECK(serial.b_star.u33 == parallel.b_star.u33); +}