Merge branch 'twin-composite' into rc175: first pass recognises the coincidence lattice of two twin domains

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-10 00:52:02 +02:00
commit 482b302681
3 files changed
+116

No files matched your search

+9
View File
@@ -178,6 +178,15 @@ H = |I(h) − I(Th)|/(I(h) + I(Th)) of the pairs a law relates is uniform on [0,
gives the fraction. T. O. Yeates, "Detecting and overcoming crystal twinning" (1997), Methods Enzymol.
276, 344-358 [doi:10.1016/S0076-6879(97)76068-3](https://doi.org/10.1016/S0076-6879(97)76068-3).
**A first-pass cell that is the coincidence lattice of two twin domains** (twinning by reticular
merohedry of index 2 or 3, e.g. obverse/reverse rhombohedral domains or a cubic crystal twinned about
[111]) is recognised by the nodes that belong to neither domain being empty. The twin index is
Friedel's (G. Friedel, *Leçons de cristallographie*, 1926; no DOI); the split of the reflections into
those of one domain, of the other, of both and of neither is the one Herbst-Irmer and Sheldrick use
for obverse/reverse twins: R. Herbst-Irmer and G. M. Sheldrick, "Refinement of obverse/reverse twins"
(2002), Acta Cryst. B58, 477-481
[doi:10.1107/S0108768102001039](https://doi.org/10.1107/S0108768102001039).
**Translational pseudo-symmetry** is detected from the native Patterson computed from the merged
intensities, and the interpretation of an off-origin peak as a pseudo-translation between copies of
the contents of the asymmetric unit - together with the modulation it puts on the intensities, which
+106
View File
@@ -385,6 +385,7 @@ bool Rugnux::FirstPassRotationIndexing(PipelineLocals &p) {
run.RequireLattice(best);
best = run.ShortAxisHypothesis(std::move(best));
best = run.SubLatticeHypothesis(std::move(best));
best = run.TwinCompositeHypothesis(std::move(best));
if (!p.geometry_prepass && prepass_result_.has_value())
best = run.Pass1Fallback(std::move(best));
// The last resort, and ONLY on a run that has no usable lattice: the pass-1 fallback above
@@ -1414,3 +1415,108 @@ auto Rugnux::FirstPassRun::IndexSchemes() -> FirstPass {
StartShortAxisSpeculation();
});
}
// Twinning by reticular merohedry: two domains of one lattice whose nodes coincide on a sub-lattice
// of index n (obverse/reverse rhombohedral domains, a cubic crystal twinned about [111], n = 3). The
// cell that indexes BOTH domains is their coincidence lattice, n times the primitive volume of either
// domain's own cell, and it indexes more frames and more spots than a domain's own cell does - so
// neither the frame count nor the occupancy of the classes it adds tells it from a superstructure.
// What does is the class of nodes on neither domain. Two domains put every reflection on the union of
// their own index-n sub-lattices K1 and K2 and leave the (1 - 1/n)^2 of the nodes on neither empty but
// for chance coincidences. One crystal cannot: whatever sub-lattice its reflections favour (a
// pseudo-translation), the nodes off K1 that lie on K2 and those that lie on neither are the same
// cosets of it, and as dense in spots per node. The two sub-lattices are the same lattice in two
// orientations, so their cells must also agree. Where all of this holds the stronger domain's own
// cell is adopted, and the other domain is left to the leftover-lattice census, which names it.
// Following Friedel's twin index (Friedel 1926, Lecons de cristallographie) and the obverse/reverse
// reflection classes of Herbst-Irmer & Sheldrick (2002) Acta Cryst. B58, 477-481
auto Rugnux::FirstPassRun::TwinCompositeHypothesis(FirstPass best) -> FirstPass {
if (rx.cancelled_ || !best.result.has_value() || best.score <= 0)
return best;
const CrystalLattice prim = best.result->lattice
.ToPrimitive(best.result->search_result.centering).NiggliReduce();
const auto hkl = IndexedMillerIndices(prim, ValidationCloud(*best.result), index_tol);
if (hkl.empty())
return best;
for (const int n : {2, 3}) {
// Each index-n sub-lattice once: the form (u,v,w) mod n with its first non-zero entry 1.
std::vector<std::array<int, 3>> forms;
for (int u = 0; u < n; u++)
for (int v = 0; v < n; v++)
for (int w = 0; w < n; w++)
if ((u ? u : v ? v : w) == 1)
forms.push_back({u, v, w});
const auto on = [n](const std::array<int, 3> &f, const std::array<int, 3> &m) {
return ((f[0] * m[0] + f[1] * m[1] + f[2] * m[2]) % n + n) % n == 0;
};
struct Classes { int both = 0, only1 = 0, only2 = 0, neither = 0; };
const auto classes = [&](size_t i, size_t j) {
Classes c;
for (const auto &m : hkl) {
const bool o1 = on(forms[i], m), o2 = on(forms[j], m);
(o1 && o2 ? c.both : o1 ? c.only1 : o2 ? c.only2 : c.neither)++;
}
return c;
};
// The pair whose union holds the most spots, the stronger domain first.
size_t bi = 0, bj = 1;
Classes c = classes(0, 1);
for (size_t i = 0; i < forms.size(); i++)
for (size_t j = i + 1; j < forms.size(); j++) {
const Classes t = classes(i, j);
if (t.neither < c.neither) {
c = t; bi = i; bj = j;
}
}
if (c.only2 > c.only1) {
std::swap(bi, bj);
std::swap(c.only1, c.only2);
}
const auto sub_of = [&](size_t f) {
AddedClassEvidence ev;
ev.u = forms[f][0]; ev.v = forms[f][1]; ev.w = forms[f][2];
return SubLattice(prim, ev, n).NiggliReduce();
};
const CrystalLattice sub = sub_of(bi);
const UnitCell u1 = sub.GetUnitCell(), u2 = sub_of(bj).GetUnitCell();
const bool congruent = std::abs(u1.a - u2.a) < 0.02 * u1.a && std::abs(u1.b - u2.b) < 0.02 * u1.b
&& std::abs(u1.c - u2.c) < 0.02 * u1.c && std::abs(u1.alpha - u2.alpha) < 2.0
&& std::abs(u1.beta - u2.beta) < 2.0 && std::abs(u1.gamma - u2.gamma) < 2.0;
// Spots per node: each domain alone holds 1/n - 1/n^2 of the nodes, neither (1 - 1/n)^2. One
// crystal reads the two densities alike, up to the scatter of the pair picked as the emptiest
// out of 7 or 13 forms (up to 1.6 where the run has a hundred spots); two domains read them
// tens of times apart.
const double weak_density = c.only2 / (1.0 / n - 1.0 / (n * n));
const double neither_density = c.neither / ((1.0 - 1.0 / n) * (1.0 - 1.0 / n));
const bool composite = congruent && c.only2 >= 20 && weak_density > 4.0 * neither_density;
logger.Info("Twin-composite hypothesis, index {}: of {} validation spots on the standing cell "
"({:.0f} A^3), the sub-lattices ({},{},{}) and ({},{},{}) hold {} and {} alone, {} "
"together and {} on neither - per node {:.1f} on the weaker one's own class against "
"{:.1f} on neither{} => {}", n, hkl.size(), best.vol,
forms[bi][0], forms[bi][1], forms[bi][2], forms[bj][0], forms[bj][1], forms[bj][2],
c.only1, c.only2, c.both, c.neither, weak_density, neither_density,
congruent ? "" : ", cells not congruent",
composite ? "two domains on their coincidence lattice" : "one lattice");
if (!composite)
continue;
RotationIndexerResult cand = *best.result;
cand.search_result = LatticeSearch(sub);
cand.lattice = cand.search_result.conventional;
cand.unconstrained.reset();
cand.extra_lattices.clear();
const int score = CountIndexed(*indexer, cand);
const auto uc = cand.lattice.GetUnitCell();
const bool adopt = score > static_cast<int>(validation.size()) / 2;
logger.Info("Twin-composite hypothesis: the stronger domain's cell, {}-centred {} {:.2f} {:.2f} "
"{:.2f} {:.1f} {:.1f} {:.1f}, indexes {}/{} validation frames - {}",
cand.search_result.centering, gemmi::crystal_system_str(cand.search_result.system),
uc.a, uc.b, uc.c, uc.alpha, uc.beta, uc.gamma, score,
static_cast<int>(validation.size()), adopt ? "adopting it" : "keeping the standing cell");
if (adopt) {
best = FirstPass{std::move(cand), score, fmt::format("{} (twin domain)", best.name),
std::abs(sub.CalcVolume())};
break;
}
}
return best;
}
+1
View File
@@ -60,6 +60,7 @@ struct Rugnux::FirstPassRun {
void RequireLattice(const FirstPass &best) const;
FirstPass ShortAxisHypothesis(FirstPass best);
FirstPass SubLatticeHypothesis(FirstPass best);
FirstPass TwinCompositeHypothesis(FirstPass best);
FirstPass Pass1Fallback(FirstPass best);
std::optional<FirstPass> FirstPassLadder(int score_before, int min_gain, int min_score);