diff --git a/rugnux/ModelValidation.cpp b/rugnux/ModelValidation.cpp index b3120e309..06b3e4790 100644 --- a/rugnux/ModelValidation.cpp +++ b/rugnux/ModelValidation.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -580,14 +581,24 @@ ModelValidationResult ValidateAgainstModel(const std::vector & // 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 &obs_in) -> Placement { + auto place_and_fit = [&](ModelState &ms, const std::vector &obs_in, + std::optional 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 & 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 & 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 identity_fit; // fit_model(ms, obs), for place_and_fit to reuse }; auto probe_indexing = [&](ModelState &ms) { IndexingProbe out; - std::vector r_free{fit_model(ms, obs).r_free}; // identity first, then the twin laws + out.identity_fit = fit_model(ms, obs); + std::vector 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 & 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 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 & 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 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 & // 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 & 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;