From 724430022f9d8d8be29a9f2c0851a64edd89ce0b Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 13 Nov 2026 23:26:42 +0100 Subject: [PATCH 1/3] tNCS analysis: phase classes from per-axis exp tables, not a libm cosine each The pseudo-translation searches (the gate's vector search and its 20 permutation nulls, the lattice-averaged search with up to 12 rotation images, and the full-resolution ladder) evaluate cos(2 pi h.u) thousands of times over up to a few hundred thousand reflections. On a rotation run with a hexagonal lattice and a pseudo-translation, the libm cosine under CosineCorrelation was ~40% of all CPU samples of the whole run, and the pre-promotion analysis on the P1 all-observation merge sat on the critical path of the space-group step for 4-7 s. cos(2 pi (h u_x + k u_y + l u_z)) is now the real part of the product of three table entries exp(2 pi i m u_axis), built once per evaluation (ClassTable); contrast_over shares it. Measured on a rotation set (1800 images, P3_221, three copies with tNCS), offline and --simulate-live --fast-live: pre-promotion wait 5.5-5.7 s -> 0.2-0.3 s, final tNCS analysis 1.3-1.9 s -> 0.3-0.4 s. Report, merged MTZ reflections, placed and refined models identical to the previous binary. [tncs] and twinning tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK --- .../scale_merge/TranslationalNCS.cpp | 56 +++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/image_analysis/scale_merge/TranslationalNCS.cpp b/image_analysis/scale_merge/TranslationalNCS.cpp index c72909778..f76e569d9 100644 --- a/image_analysis/scale_merge/TranslationalNCS.cpp +++ b/image_analysis/scale_merge/TranslationalNCS.cpp @@ -161,6 +161,52 @@ namespace { return best; } + // TncsClass - cos(2 pi h.v) averaged over `vectors` - for every h up to the order m_max, as the real + // part of exp(2 pi i h v_x) exp(2 pi i k v_y) exp(2 pi i l v_z), each factor read from a table over + // the orders: the searches below ask for it thousands of times on up to a few hundred thousand + // reflections, and a libm cosine per reflection per vector made it most of the CPU time of a whole + // run on a merge in P1. + class ClassTable { + public: + ClassTable(const std::vector> &vectors, int m_max) + : m_max_(m_max), width_(2 * m_max + 1), count_(vectors.size()), + re_(count_ * 3 * width_), im_(count_ * 3 * width_) { + for (size_t v = 0; v < count_; ++v) + for (int axis = 0; axis < 3; ++axis) + for (int m = -m_max; m <= m_max; ++m) { + const double phase = 2.0 * std::numbers::pi * m * vectors[v][axis]; + re_[(v * 3 + axis) * width_ + (m_max + m)] = std::cos(phase); + im_[(v * 3 + axis) * width_ + (m_max + m)] = std::sin(phase); + } + } + + double operator()(const std::array &h) const { + double c = 0.0; + for (size_t v = 0; v < count_; ++v) { + const size_t a = v * 3 * width_ + (m_max_ + h[0]); + const size_t b = (v * 3 + 1) * width_ + (m_max_ + h[1]); + const size_t d = (v * 3 + 2) * width_ + (m_max_ + h[2]); + const double ab_re = re_[a] * re_[b] - im_[a] * im_[b]; + const double ab_im = re_[a] * im_[b] + im_[a] * re_[b]; + c += ab_re * re_[d] - ab_im * im_[d]; + } + return c / static_cast(count_); + } + + private: + int m_max_; + size_t width_, count_; + std::vector re_, im_; + }; + + // The highest |index| among the first `n` reflections: the order a ClassTable for them reaches. + int MaxOrder(const std::vector> &hkl, size_t n) { + int m_max = 0; + for (size_t i = 0; i < n; ++i) + m_max = std::max({m_max, std::abs(hkl[i][0]), std::abs(hkl[i][1]), std::abs(hkl[i][2])}); + return m_max; + } + // How well a vector explains the intensities of the first `n` reflections under the physical // model = A (1 + f cos(2 pi h.u)): the correlation between E^2 and cos(2 pi h.u). // @@ -177,10 +223,10 @@ namespace { // With `rots` the cosine is the class averaged over them (see `modulation_averaged`). double CosineCorrelation(const std::vector> &hkl, const std::vector &e2, const std::array &u, size_t n, const std::vector &rots) { - const auto vectors = Images(u, rots); + const ClassTable cls(Images(u, rots), MaxOrder(hkl, n)); double sc = 0.0, scc = 0.0, sy = 0.0, syy = 0.0, scy = 0.0; for (size_t i = 0; i < n; ++i) { - const double c = TncsClass(hkl[i][0], hkl[i][1], hkl[i][2], vectors); + const double c = cls(hkl[i]); sc += c; scc += c * c; sy += e2[i]; @@ -574,9 +620,10 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector)/pi, which for the identity alone is the same phase folded // about 1/2 - the bins then centred on the strongest and the weakest class. + const int mod_max_order = MaxOrder(mod_hkl, mod_hkl.size()); auto contrast_over = [&](const std::vector &e2v, const std::array &u, std::vector *profile, const std::vector &rots) { - const auto vectors = Images(u, rots); + const ClassTable cls(Images(u, rots), mod_max_order); std::array sum{}, cnt{}; sum.fill(0.0); cnt.fill(0.0); @@ -586,8 +633,7 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector(f * kPhaseBins), kPhaseBins - 1); sum[b] += e2v[i]; cnt[b] += 1.0; From a22d8b9b56dfdb7d0c2e0b2c3c018d14277cb79f Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 00:03:24 +0100 Subject: [PATCH 2/3] Live MR: carry the pre-pass placement across the lattice's indexing ambiguity In a live run the molecular replacement of a prediction starts on the geometry pre-pass's merge and its placement is carried to the final merge where it holds there. The two passes index the sweep each on its own, so on a lattice with an indexing ambiguity the final merge can come out in the other hand, the carried placement then fits at R-free ~0.6 and the whole search is run again after the space-group step, on the critical path of the tail. CarryMolecularReplacement now picks the ambiguity operator (identity included) that takes the searched merge's reflections onto this merge's by the correlation of their intensities - a pass over the reflections, milliseconds - and turns the copies by it before the rigid body. Measured on a P3_221 rotation set with three copies and tNCS (--simulate-live 10 --fast-live, 1800 images): the pre-pass placement, turned by -h,-k,l, holds at R-free 0.417 (the re-search reached 0.413); MR step 0.91-1.40 s -> 0.18-0.26 s. Refined R-free 0.4175 against 0.4255 from the re-searched placement (same merge, same refinement). New test MolecularReplacement_CarryAcrossIndexingAmbiguity (fails on the old code at R-free 0.51). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK --- .../MolecularReplacement.cpp | 62 +++++++++++++++++-- tests/MolecularReplacementTest.cpp | 51 +++++++++++++++ 2 files changed, 107 insertions(+), 6 deletions(-) diff --git a/image_analysis/structure_refinement/MolecularReplacement.cpp b/image_analysis/structure_refinement/MolecularReplacement.cpp index 087b0e94c..8d9bf7a72 100644 --- a/image_analysis/structure_refinement/MolecularReplacement.cpp +++ b/image_analysis/structure_refinement/MolecularReplacement.cpp @@ -26,6 +26,8 @@ #include // DensityCalculator #include // IT92 x-ray form factors #include // Scaling (bulk solvent + anisotropic B) +#include // find_twin_laws +#include // ReciprocalAsu #include "../../common/JFJochMath.h" // PI #include "../../common/Logger.h" @@ -4092,6 +4094,46 @@ MolecularReplacementResult RunMolecularReplacement(const RugnuxModel &model, con return result; } +// The operator of the lattice's indexing ambiguity (the identity among them) that takes the reflections of `searched` +// onto those of `merge`, by the correlation of their intensities: the two passes of a run index the sweep each on +// its own, and a lattice with an ambiguity can come out in either hand of it. +static gemmi::Op AmbiguityBetween(const MRMerge &searched, const MRMerge &merge, const gemmi::SpaceGroup &sg) { + const gemmi::GroupOps gops = sg.operations(); + const gemmi::ReciprocalAsu asu(&sg); + std::unordered_map intensity; + const auto key = [&](const gemmi::Miller &h) { + const gemmi::Miller a = asu.to_asu(h, gops).first; + return (static_cast(a[0] + 512) << 20) | (static_cast(a[1] + 512) << 10) | (a[2] + 512); + }; + for (const MergedReflection &r : *merge.merged) + if (std::isfinite(r.I)) + intensity[key({{r.h, r.k, r.l}})] = r.I; + gemmi::Op best = gemmi::Op::identity(); + double best_cc = -2.0; + std::vector ops{gemmi::Op::identity()}; + for (const gemmi::Op &op : gemmi::find_twin_laws(gemmi::UnitCell(merge.cell), &sg, 3.0, false)) + ops.push_back(op); + for (const gemmi::Op &op : ops) { + double n = 0, sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0; + for (const MergedReflection &r : *searched.merged) { + if (!std::isfinite(r.I)) + continue; + const auto it = intensity.find(key(op.apply_to_hkl({{r.h, r.k, r.l}}))); + if (it == intensity.end()) + continue; + n += 1; + sx += r.I; sy += it->second; + sxx += r.I * r.I; syy += it->second * it->second; sxy += r.I * it->second; + } + const double cc = n > 1 ? (sxy - sx * sy / n) / std::sqrt((sxx - sx * sx / n) * (syy - sy * sy / n)) : -1.0; + if (cc > best_cc) { + best_cc = cc; + best = op; + } + } + return best; +} + std::optional CarryMolecularReplacement(const MolecularReplacementResult &early, const MRMerge &searched, const MRMerge &merge, Logger &logger, size_t nthreads, @@ -4117,12 +4159,18 @@ std::optional CarryMolecularReplacement(const Molecu if (r.d > 0) data_d_min = std::min(data_d_min, r.d); const double d_min = std::max(SCORE_D_MIN, data_d_min); - // Every copy at its fractional position in this cell. + // Every copy at its fractional position in this cell, turned by the indexing ambiguity between the two merges: + // a reflection h of `searched` is op(h) here, so a position x there is op^-1 x here. + const gemmi::Op op = AmbiguityBetween(searched, merge, sg); + const gemmi::Op to_here = op.inverse(); std::vector body = SplitCopies(*c.placed, c.copies); for (gemmi::Structure &b : body) { std::vector pos = ModelPositions(b.models[0]); - for (gemmi::Position &x : pos) - x = cell.orthogonalize(b.cell.fractionalize(x)); + for (gemmi::Position &x : pos) { + const gemmi::Fractional f = b.cell.fractionalize(x); + const std::array t = to_here.apply_to_xyz({f.x, f.y, f.z}); + x = cell.orthogonalize(gemmi::Fractional(t[0], t[1], t[2])); + } SetModelPositions(b.models[0], pos); b.cell = cell; b.spacegroup_hm = sg.xhm(); @@ -4165,9 +4213,11 @@ std::optional CarryMolecularReplacement(const Molecu auto placed = std::make_shared(AssembleCopies(body)); const PlacementScore score = score_placement(*placed, sg, *merge.merged, d_min, nthreads, &scorers); const bool holds = score.r_free <= c.r_free + R_FREE_MARGIN; - logger.Info("Molecular replacement: the search on the {} ({}): its placement in {} ({} copies, R-free {:.3f} there) refined against " - "this merge: R-work {:.3f} R-free {:.3f} ({:.2f} s) - {}", searched.name, - early.decision, sg.xhm(), c.copies, c.r_free, score.r_work, score.r_free, + logger.Info("Molecular replacement: the search on the {} ({}): its placement in {} ({} copies, R-free {:.3f} there){} " + "refined against this merge: R-work {:.3f} R-free {:.3f} ({:.2f} s) - {}", searched.name, + early.decision, sg.xhm(), c.copies, c.r_free, + op == gemmi::Op::identity() ? std::string() : ", indexed there as " + op.as_hkl().triplet('h') + " here,", + score.r_work, score.r_free, seconds_since(t0), holds ? "it holds, not searched again" : "it does not hold, searched again"); if (!holds) return std::nullopt; diff --git a/tests/MolecularReplacementTest.cpp b/tests/MolecularReplacementTest.cpp index e51635d5f..a2deea5e0 100644 --- a/tests/MolecularReplacementTest.cpp +++ b/tests/MolecularReplacementTest.cpp @@ -13,6 +13,7 @@ #include #include +#include #include "../image_analysis/structure_refinement/ModelFFT.h" #include "../image_analysis/structure_refinement/MolecularReplacement.h" @@ -817,6 +818,56 @@ TEST_CASE("MolecularReplacement_CarryToAnotherMerge", "[MolecularReplacement]") CHECK_FALSE(CarryMolecularReplacement(early, searched, {"MERGE", &merged, uc, other}, logger, 4)); } +// The two merges of one crystal in the two hands of its indexing ambiguity (P 41: k,h,-l), as two passes that index +// the sweep each on its own can give: the placement found on the one is turned onto the other (here up to a symmetry operator of the group) and +// holds there. +TEST_CASE("MolecularReplacement_CarryAcrossIndexingAmbiguity", "[MolecularReplacement]") { + const SearchModel model = synthetic_model(); + const gemmi::SpaceGroup &sg = gemmi::get_spacegroup_by_name("P 41"); + const gemmi::UnitCell cell(60, 60, 70, 90, 90, 90); + const gemmi::Structure truth = PlaceSearchModel(model, axis_angle({1, 2, 3}, 50), gemmi::Fractional(0.17, 0.31, 0.09), + cell, sg); + auto merged = observed(truth); + for (size_t i = 0; i < merged.size(); i++) + merged[i].rfree_flag = i % 10 == 0; + // The other hand: every reflection h of this merge is op(h) there, and the model placed there is op^-1 x. + const gemmi::Op op = gemmi::parse_triplet("y,x,-z"); + const gemmi::GroupOps gops = sg.operations(); + const gemmi::ReciprocalAsu asu(&sg); + std::vector other_hand = merged; + for (MergedReflection &r : other_hand) { + const gemmi::Miller h = asu.to_asu(op.apply_to_hkl({{r.h, r.k, r.l}}), gops).first; + r.h = h[0]; r.k = h[1]; r.l = h[2]; + } + gemmi::Structure there = truth; + std::vector pos = ModelPositions(there.models[0]); + for (gemmi::Position &x : pos) { + const gemmi::Fractional f = cell.fractionalize(x); + const std::array t = op.inverse().apply_to_xyz({f.x, f.y, f.z}); + x = cell.orthogonalize(gemmi::Fractional(t[0], t[1], t[2])); + } + SetModelPositions(there.models[0], pos); + + MolecularReplacementResult early; + early.ok = true; + early.chosen = 0; + early.decision = "AGREES"; + MRCandidate c; + c.space_group = sg; + c.copies = 1; + c.r_free = 0.30; + c.placed = std::make_shared(there); + early.candidates.push_back(c); + + const UnitCell uc{60, 60, 70, 90, 90, 90}; + Logger logger("MolecularReplacement_CarryAcrossIndexingAmbiguity"); + const auto carried = CarryMolecularReplacement(early, {"first merge", &other_hand, uc, sg}, + {"MERGE", &merged, uc, sg}, logger, 4); + // the placement as it came, untouched by the ambiguity, fits this merge at R-free 0.5 and more + REQUIRE(carried); + CHECK(carried->candidates[0].r_free < 0.2); +} + #ifdef JFJOCH_USE_CUDA TEST_CASE("MolecularReplacement_GPUMatchesCPU", "[MolecularReplacement]") { if (get_gpu_count() == 0) From fffc07b894c010f8514576fbf07aef8a8f5e4d89 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 01:00:04 +0100 Subject: [PATCH 3/3] tNCS analysis: sort keys read from arrays of their own The modulation band's sort of every acentric reflection, and the class ladder's two sorts, compared fields read from whole MergedReflections - a cache miss per comparison on a large merge, serial, and the largest single cost left in the pre-promotion analysis on a P1 merge of a large cell. The keys are now copied into arrays first; the comparisons and so the orders are unchanged. 4nwv (I222, offline, --no-refine): pre-promotion analyses wait 1.9-3.4 s -> 0.0-0.1 s, report identical. [tncs] tests pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK --- .../scale_merge/TranslationalNCS.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/image_analysis/scale_merge/TranslationalNCS.cpp b/image_analysis/scale_merge/TranslationalNCS.cpp index f76e569d9..2acb83aee 100644 --- a/image_analysis/scale_merge/TranslationalNCS.cpp +++ b/image_analysis/scale_merge/TranslationalNCS.cpp @@ -535,8 +535,12 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector(i)); } - std::sort(mod_idx.begin(), mod_idx.end(), - [&](int a, int b) { return merged[a].d != merged[b].d ? merged[a].d > merged[b].d : a < b; }); + // the comparisons read d from an array of its own rather than each from a whole MergedReflection: + // this sort, of every acentric reflection, was most of the analysis' serial time on a large merge + std::vector d(merged.size()); + for (int i : mod_idx) + d[i] = merged[i].d; + std::sort(mod_idx.begin(), mod_idx.end(), [&](int a, int b) { return d[a] != d[b] ? d[a] > d[b] : a < b; }); const int n_beyond = static_cast(std::count_if(mod_idx.begin(), mod_idx.end(), [&](int i) { return merged[i].d >= kModulationDMin; })); mod_idx.resize(std::min(kMaxModulationBand, std::max(n_beyond, std::min(kMinModulationBand, @@ -864,12 +868,16 @@ TranslationalNCSResult AnalyzeTranslationalNCS(const std::vector(i)); // Equal-count shells sorted by |h|, so that every stage of the ladder reads a population // that is already normalised and can be taken as a prefix. - std::sort(idx.begin(), idx.end(), [&](int a, int b) { - return std::max({std::abs(merged[a].h), std::abs(merged[a].k), std::abs(merged[a].l)}) - < std::max({std::abs(merged[b].h), std::abs(merged[b].k), std::abs(merged[b].l)}); - }); + // the keys in arrays of their own, as the modulation band's sort above + std::vector order(merged.size()); + std::vector d(merged.size()); + for (int i : idx) { + order[i] = std::max({std::abs(merged[i].h), std::abs(merged[i].k), std::abs(merged[i].l)}); + d[i] = merged[i].d; + } + std::sort(idx.begin(), idx.end(), [&](int a, int b) { return order[a] < order[b]; }); std::vector by_d = idx; - std::sort(by_d.begin(), by_d.end(), [&](int a, int b) { return merged[a].d > merged[b].d; }); + std::sort(by_d.begin(), by_d.end(), [&](int a, int b) { return d[a] > d[b]; }); std::vector e2_of(merged.size(), std::numeric_limits::quiet_NaN()); const size_t n_shells = std::max(1, by_d.size() / 500); for (size_t s = 0; s < n_shells; ++s) {