Refinement: the twinned arm stops after its ladder where its law is far from being taken

Where the lattice admits a twin law a second, twinned refinement runs beside the untwinned one and is
decided at its end by how far the law is ahead of its null (TwinNull, taken at 3 standard errors, 2
where the data read twinned). That lead is now also measured after the twinned arm's first ladder;
under 0.5 standard errors the arm stops there, its threads go to the untwinned arm, and the law is
decided (not taken) where it stopped. On 19 refinements with a law the lead rose by at most 1.35 from
the ladder's end to the end; the 3 twinned crystals were 8.6-17.7 ahead at the ladder's end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-17 12:15:36 +01:00
1 parent 7276a1cf3b
commit d585be7b68
2 files changed
+36 -9

No files matched your search

@@ -1848,6 +1848,11 @@ constexpr size_t TWIN_NULL_SHIFT = 2;
// evidence (AdoptTwinLaw).
constexpr double TWIN_NULL_Z = 3.0;
constexpr double TWIN_NULL_Z_DATA = 2.0;
// A twinned refinement stops after its first ladder where the law is ahead of its null by less than this many
// standard errors there (RefineOnce, 1a.): 1.5 under the lowest bar a law is taken at (TWIN_NULL_Z_DATA). From the
// ladder's end to the refinement's, the law's lead rose by 1.35 at most (on 19 refinements with a law, 3 twinned: those
// 8.6-17.7 at the ladder's end, the others under 2.4).
constexpr double TWIN_STOP_Z = 0.5;
// `first`: the first law whose mates are replaced - the laws before it keep theirs (the null of the domains added to
// them). With several laws a twin group holds up to Laws() + 1 reflections, kept next to each other, and law first + k
// is paired (Laws() + 1) (k + 1) apart: past the reflection's own group, and no two laws drawing the same partner.
@@ -3440,6 +3445,22 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
return k >= 0 ? std::move(outcomes[k]) : TrialOutcome();
};
TrialOutcome state = ladder_choice(pos0, b0, aniso0, nullptr);
// 1a. A twinned refinement (DecideTwin) stops where the ladder leaves its law far from being taken, hardly
// ahead of its null (TwinNull, TWIN_STOP_Z) - its threads go to the untwinned refinement beside it, and
// the law is decided where it stopped.
bool stopped = false;
if (settings.twin_arm_stops && state.ran && !ctx.Cancelled()) {
ctx.SetPositions(state.pos);
ctx.SetB(state.b);
ctx.SetAniso(state.aniso);
ctx.Restore(state.fit);
ctx.FModel();
RefinementResult null;
TwinNull(ctx, null, null_first);
stopped = null.twin_null_z < TWIN_STOP_Z;
logger.Info("Refinement, twin law: after the ladder fraction {:.3f}, ahead of its null by {:.1f} standard "
"errors{}", FractionSum(ctx.rows), null.twin_null_z, stopped ? " - not taken further" : "");
}
// 2. The trajectory: steps of a few macro-cycles from where the last one ended, each trying the weights as they
// are and, in turn, the positions' X-ray weight halved and doubled (halved only in the first step, from the
@@ -3505,7 +3526,8 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
}
return best;
};
state = trajectory(std::move(state), false, from_best, "step");
if (!stopped)
state = trajectory(std::move(state), false, from_best, "step");
// 2a. The population: from where the trajectory ended, members that refine the positions on the data blurred
// along a schedule of their own (TrialWeights::blur: at 60 A^2 the data reach 4 A, at 20 A^2 2.2 A, then
@@ -3520,7 +3542,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// The members are at most as many as the trial slots, one wave: on a large model, where the card holds
// few, more members would cost a wave each.
const size_t population = std::min<size_t>(std::max(settings.population, 0), trial_slots.size());
if (population > 1 && state.ran && !ctx.Cancelled()) {
if (population > 1 && !stopped && state.ran && !ctx.Cancelled()) {
const RefinementStage pop_stage("population");
const double w = state.trial.weight, adp = state.trial.adp_weight, ncs_s = state.trial.ncs_sigma;
// the members, the most different first: the positions as they are and kicked, on the data as they are
@@ -3600,7 +3622,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// ladder (the trajectory run again from there gained nothing on twelve sets). Each round thus costs a map,
// an audit and a refinement of the solvent, where a refinement of its own cost the whole weight search
// (the build was 2-4 times the refinement).
if (build && state.ran && !ctx.Cancelled()) {
if (build && !stopped && state.ran && !ctx.Cancelled()) {
const auto build_start = std::chrono::steady_clock::now();
const RefinementStage build_stage("solvent_build");
SolventBuildResult &b = result.solvent_build;
@@ -3860,7 +3882,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// never sees the free reflections. A model whose side chains the data already hold moves nothing and
// costs the sweep alone. The atoms it prunes are left at occupancy 0, as the solvent build's placeholders
// are, and held.
if (settings.side_chain_field && state.ran && !ctx.Cancelled() && !ctx.refl.Twinned() &&
if (settings.side_chain_field && !stopped && state.ran && !ctx.Cancelled() && !ctx.refl.Twinned() &&
std::none_of(state.aniso.begin(), state.aniso.end(), [](const AtomAniso &a) { return a.on; })) {
const RefinementStage field_stage("side_chain_field");
const auto field_start = std::chrono::steady_clock::now();
@@ -4022,7 +4044,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// 3. The final weights: a short ladder from where the trajectory ended, tighter geometry first, its choice
// kept where it displaces that. A rung the step from there tried already is taken from that step.
if (state.ran && !ctx.Cancelled()) {
if (!stopped && state.ran && !ctx.Cancelled()) {
const double w = state.trial.weight, adp = state.trial.adp_weight, ncs_s = state.trial.ncs_sigma;
std::vector<TrialWeights> trials;
for (double f : settings.final_ladder)
@@ -4214,7 +4236,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
}
logger.Info("Refinement: displacement model {}", result.adp_model);
};
if (settings.adp_hypotheses && state.ran && !ctx.Cancelled())
if (settings.adp_hypotheses && !stopped && state.ran && !ctx.Cancelled())
displacements(input_tls);
// 4b. The weights and the displacement model decided again, from the model as it now is: the balanced
// weights on its B factors, the ladder (1.) from it with its displacements as they are (the U of TLS or
@@ -4229,8 +4251,8 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// would run the same trials from the same model again, so the round ends there: from a model refined
// before, a round is its ladder and one step. No round where the refinement does not displace the model
// as given (5.).
for (int round = 2; round <= settings.rounds && settings.adp_hypotheses && state.ran && !ctx.Cancelled() &&
(!settings.as_given_competes || displaces(state.trial, given.trial));
for (int round = 2; round <= settings.rounds && settings.adp_hypotheses && !stopped && state.ran &&
!ctx.Cancelled() && (!settings.as_given_competes || displaces(state.trial, given.trial));
round++) {
const TrialOutcome before = state;
const std::vector<TlsBody> own_tls = state.tls;
@@ -4451,7 +4473,9 @@ void DecideTwin(const gemmi::Structure &placed, const std::vector<MergedReflecti
}
}
}
RefinementResult twinned = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, settings, {law});
RefinementSettings arm = settings;
arm.twin_arm_stops = true;
RefinementResult twinned = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, arm, {law});
arm_done();
if (!take_untwinned())
return;
@@ -112,6 +112,9 @@ struct RefinementSettings {
// Where two refinements run side by side on the CPU (RefineModel's arms): the threads the other one has finished
// with, which this one's weight trials take up from then on (the result does not depend on it). Null: none.
const std::atomic<size_t> *spare_arm_threads = nullptr;
// A twinned refinement beside the untwinned one (DecideTwin): it stops after its first ladder where that leaves
// the law far from being taken.
bool twin_arm_stops = false;
// The user's restraint dictionary, over the embedded one (BuildRefinementTopology)
RestraintDictionary dictionary;
};