rugnux --model: drop two fits nothing needed

A rejected rigid-body placement put the atoms back and then recomputed the full
Fcalc and solvent mask at the restored positions. Nothing reads them: the real
model's ModelState is only fitted again after its own compute_model_factors
(the re-placement on the merged indexing, the null-failure fallback), and a
null replicate's state is discarded. The atoms are still put back; the
structure factors are left as they are.

Where the lattice has twin laws, the indexing probe fits the identity labelling
first, and place_and_fit then made the identical fit again - same
ModelState (nothing moves the model between the two), same reflections. The
probe now hands that Fit over, for the real model when the identity wins and
always for a null replicate (placed against the merged indexing). fit_model is
deterministic, so the reused Fit is the one the second call produced.

Bit-identical by construction: no value that is read changes. To check: MODEL_*
keys and md5 of maps/.mtz/_model.cif identical with and without this commit on
the audit set, including sets with twin laws and a null.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013nW6FNRP1bBJJ8pfHiByAT
This commit is contained in:
leonarski_fandClaude Opus 5 committed 2026-09-19 13:03:22 +02:00
1 parent 2f727b1975
commit 47bc15c62c
1 file changed
+32 -10
+32 -10
View File
@@ -9,6 +9,7 @@
#include <complex>
#include <array>
#include <numeric>
#include <optional>
#include <random>
#include <string>
#include <utility>
@@ -580,14 +581,24 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
// The real model and every null replicate below go through this same lambda, so the two are
// comparable - a null that was not placed would be the null of a weaker procedure than the one it
// is there to judge. It leaves the model where it ends up, and the caller puts it back.
//
// `fitted`: fit_model(ms, obs_in) where the caller already has it - the indexing probe fits the
// identity labelling first, on the same structure factors and the same reflections - so the same
// fit is not made twice in a row.
//
// A rejected placement puts the atoms back but not ms.fcalc/ms.fmask, which are left at the
// rejected placement: nothing reads them afterwards - every later fit_model on this state is
// preceded by its own compute_model_factors - and recomputing them was a full Fcalc and mask
// for nothing.
struct Placement {
Fit fit;
bool rb_applied = false;
double rb_angle_deg = 0, rb_shift_A = 0, r_free_before_rb = 0;
};
auto place_and_fit = [&](ModelState &ms, const std::vector<MergedReflection> &obs_in) -> Placement {
auto place_and_fit = [&](ModelState &ms, const std::vector<MergedReflection> &obs_in,
std::optional<Fit> fitted) -> Placement {
Placement out;
out.fit = fit_model(ms, obs_in);
out.fit = fitted ? std::move(*fitted) : fit_model(ms, obs_in);
// --- rigid-body placement of the model in the data cell ---
// Re-fractionalizing the model above puts it in the right box but not in the right place: a
@@ -620,7 +631,6 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
out.fit = std::move(moved);
} else {
SetModelPositions(ms.st.models[0], before);
compute_model_factors(ms);
}
}
return out;
@@ -647,10 +657,12 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
struct IndexingProbe {
gemmi::Op op = gemmi::Op::identity(); // the lowest-R-free relabelling
double margin = 0; // by how much in R-free it leads the runner-up
std::optional<Fit> identity_fit; // fit_model(ms, obs), for place_and_fit to reuse
};
auto probe_indexing = [&](ModelState &ms) {
IndexingProbe out;
std::vector<double> r_free{fit_model(ms, obs).r_free}; // identity first, then the twin laws
out.identity_fit = fit_model(ms, obs);
std::vector<double> r_free{out.identity_fit->r_free}; // identity first, then the twin laws
double best_r_free = r_free.front();
for (const auto &op : reindex_ops) {
const double cand = fit_model(ms, ReindexReflections(obs, op)).r_free;
@@ -679,7 +691,13 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
obs_model = &reindexed;
}
}
Placement real = place_and_fit(mdl, *obs_model);
// The probe's identity fit is this fit exactly when the identity won: same structure factors
// (nothing has moved the model since), same reflections.
std::optional<Fit> identity_fit;
if (obs_model == &obs)
identity_fit = std::move(indexing.identity_fit);
indexing.identity_fit.reset();
Placement real = place_and_fit(mdl, *obs_model, std::move(identity_fit));
// --- is there anything for the model to decide? ---
// There are only two: the space-group label, where the model asserts the other enantiomorph, and
@@ -737,9 +755,13 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
SetModelPositions(rep.st.models[0], as_read);
reorient_about_centroid(rep.st.models[0], null_rotation[i]);
compute_model_factors(rep);
if (!reindex_ops.empty())
null_margin[i] = probe_indexing(rep).margin;
null_r_work[i] = place_and_fit(rep, obs).fit.r_work;
std::optional<Fit> identity_fit;
if (!reindex_ops.empty()) {
IndexingProbe probe = probe_indexing(rep);
null_margin[i] = probe.margin;
identity_fit = std::move(probe.identity_fit); // replicates are placed against obs
}
null_r_work[i] = place_and_fit(rep, obs, std::move(identity_fit)).fit.r_work;
});
const auto [null_mean, null_sd] = mean_sd(null_r_work);
result.fit_tested = true;
@@ -794,7 +816,7 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
// are now the ones the merge produced, so the fit is remade on them.
SetModelPositions(st.models[0], as_read);
compute_model_factors(mdl);
real = place_and_fit(mdl, obs);
real = place_and_fit(mdl, obs, std::nullopt);
record_fit(real);
}
}
@@ -824,7 +846,7 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
result.r_work_sigma = 0.0;
SetModelPositions(st.models[0], as_read);
compute_model_factors(mdl);
real = place_and_fit(mdl, obs);
real = place_and_fit(mdl, obs, std::nullopt);
}
Fit &best = real.fit;