From e25b74dd1c1bce523a2af466f0dc6748cdd01808 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 9 Oct 2026 23:51:58 +0200 Subject: [PATCH] rugnux first pass: recognise the coincidence lattice of two twin domains A crystal twinned by reticular merohedry of index n - obverse/reverse rhombohedral domains, or a cubic crystal twinned by a 60 deg rotation about [111] (the spinel law, index 3) - has its two domains' reflections on a common lattice of n times the primitive volume of either domain's own cell. That coincidence cell indexes more frames and more spots than the domain's cell, so the first pass adopted it (a hexagonal/orthohexagonal or R:R cell for an F- or I-cubic crystal), and nothing downstream could recover the true lattice or Laue class. A new step after the sub-lattice hypothesis (TwinCompositeHypothesis), for n = 2 and 3, counts the validation spots on the standing cell in the four classes of the pair of index-n sub-lattices whose union holds the most spots: on both, on each alone, and on neither. Two domains leave the (1 - 1/n)^2 of the nodes on neither empty but for chance coincidences; one crystal, whatever sub-lattice a pseudo-translation favours, populates the nodes off the stronger sub-lattice alike whether they lie on the second sub-lattice or on neither. The step adopts the stronger domain's cell when the two sub-lattice cells are congruent (2 % / 2 deg), the weaker domain's own class holds at least 20 spots at more than four times the per-node density of the class on neither, and the domain's cell indexes a majority of the validation frames. The other domain is left to the leftover-lattice census. Evaluated on the private, inhouse and a twin/tNCS/rhombohedral/cubic/ long-axis selection of the open arm: it fires on the two known index-3 twins (a spinel-twinned F-cubic crystal, now indexed F-cubic; the in-house I-cubic insulin set with a 60 deg [111] second domain, R3:R -> I23) with a weak/neither per-node density ratio of 60 and more (the class on neither holds 0-8 spots against 250-2850 on the weaker domain), and nowhere else. On every other set the ratio stays between 1.0 and 1.6, the top of that range only on runs with a hundred or so validation spots, where the pair picked as the emptiest of 7 or 13 forms carries the scatter; hence the factor 4. Both twins now merge in their cubic group, and the sets re-run against the release baseline otherwise did not change. Cost when it does not fire: tens of milliseconds per first pass. Merges two prototypes: the class census and n = 2 coverage of one, the sub-cell congruence requirement of the other; no environment switch. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- docs/ACKNOWLEDGEMENT.md | 9 ++++ rugnux/RugnuxFirstPass.cpp | 106 +++++++++++++++++++++++++++++++++++++ rugnux/RugnuxFirstPass.h | 1 + 3 files changed, 116 insertions(+) diff --git a/docs/ACKNOWLEDGEMENT.md b/docs/ACKNOWLEDGEMENT.md index 91ca925f4..17e6cd510 100644 --- a/docs/ACKNOWLEDGEMENT.md +++ b/docs/ACKNOWLEDGEMENT.md @@ -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 diff --git a/rugnux/RugnuxFirstPass.cpp b/rugnux/RugnuxFirstPass.cpp index 4d52b6613..08166d5a4 100644 --- a/rugnux/RugnuxFirstPass.cpp +++ b/rugnux/RugnuxFirstPass.cpp @@ -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> 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 &f, const std::array &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(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(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; +} diff --git a/rugnux/RugnuxFirstPass.h b/rugnux/RugnuxFirstPass.h index 6909e172d..f44732c72 100644 --- a/rugnux/RugnuxFirstPass.h +++ b/rugnux/RugnuxFirstPass.h @@ -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 FirstPassLadder(int score_before, int min_gain, int min_score);