SearchSpaceGroup: per-reflection work on all threads, every sum still serial in the old order

The space-group search was almost entirely single-threaded. The per-reflection work now runs in
parallel over fixed chunks, while every floating-point accumulation stays serial and in the same
order as before, so the output is bit-identical.

- AnalyzeLTestUnderMerge takes nthreads (default 1): the usable test, the orbit means and the
  partner search run in parallel; the 20 resolution-shell passes become one walk.
- Stage B ClassifyReflection: one byte per reflection, candidates scored in batches of nthreads.
- FindRowModulation: cone test and cosines in parallel.
- PrepareMerge: parallel flatten; the sorts use inline {d, index} keys with the same comparison.

Measured: SearchSpaceGroup 14.7 -> 4.8 s on 8tyy (instrumented build). Wall clock, median of 3
interleaved runs vs rc175, timed inside the GPU lock (-march=x86-64-v3, CUDA): 8tyy 136.9 -> 127.0 s,
8a1a 81.8 -> 80.4 s, cytochrome C 18.8 -> 18.9 s (unchanged). 8qaw max RSS 21.7 -> 21.9 GB.

p.mtz md5-identical to rc175 on myoglobin, cytochrome C, thaumatin, 8a1a, 8tyy, 9gdj and 8qaw
(GPU build), and on myoglobin and cytochrome C in a CUDA-off build.

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-07 12:11:21 +02:00
1 parent a7a60eb128
commit 1907f47368
3 files changed
+202 -97

No files matched your search

+115 -43
View File
@@ -131,6 +131,23 @@ namespace {
return -1;
}
// What a candidate group says about one reflection, packed in a byte for Stage B: the class it falls
// in, whether it lies on a rotation axis (present reflections only), and which mirror's zone holds it
// (absent and present ones), as zone + 1 in the top four bits - a point group has at most nine
// mirrors (m-3m), so that fits.
constexpr uint8_t REFL_UNUSED = 0, REFL_CENTRING_ABSENT = 1, REFL_ABSENT = 2, REFL_PRESENT = 3;
constexpr uint8_t REFL_CLASS_MASK = 3, REFL_ON_AXIS = 4;
uint8_t ClassifyReflection(const gemmi::GroupOps& gops, const std::vector<gemmi::Op::Rot>& mirrors,
const gemmi::Op::Miller& hkl) {
if (CenteringAbsent(gops, hkl))
return REFL_CENTRING_ABSENT;
const uint8_t zone = static_cast<uint8_t>((MirrorZoneOf(mirrors, hkl) + 1) << 4);
if (gops.is_systematically_absent(hkl))
return REFL_ABSENT | zone;
return REFL_PRESENT | zone | (gops.epsilon_factor_without_centering(hkl) > 1 ? REFL_ON_AXIS : 0);
}
// The zone a mirror fixes, named the way a crystallographer names it - "h0l" for the mirror
// normal to b, "hhl" for a diagonal one. Built by asking the operator which of a handful of
// representative reflections it fixes rather than by decoding the matrix, which keeps it correct
@@ -174,6 +191,20 @@ namespace {
static_assert(RowOf(0, 1, 0) == AxialRow{0, -1, 0});
// Median of an unordered set (reordered in place); 0 for an empty set.
// Sort reflection indices from low to high resolution. The same std::sort on the same comparison of
// d as always - so reflections at equal d come out in the order they always did - but over copies
// of d held beside the indices: comparing through the index fetched a whole MergedReflection, from
// anywhere in the merge, twice per comparison.
void SortLowToHighResolution(std::vector<size_t>& order, const std::vector<MergedReflection>& merged) {
struct Item { float d; size_t i; };
std::vector<Item> items(order.size());
for (size_t j = 0; j < order.size(); ++j)
items[j] = {merged[order[j]].d, order[j]};
std::sort(items.begin(), items.end(), [](const Item& a, const Item& b) { return a.d > b.d; });
for (size_t j = 0; j < order.size(); ++j)
order[j] = items[j].i;
}
double MedianOf(std::vector<double>& v) {
if (v.empty())
return 0.0;
@@ -321,7 +352,7 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array<in
const std::vector<char>& use, const std::vector<int>& H,
const std::vector<int>& K, const std::vector<int>& L,
const std::vector<double>& Esq, const std::vector<int>& shell,
int bins) {
int bins, size_t nthreads) {
RowModulation best;
if (bins <= 0)
return best;
@@ -333,26 +364,41 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array<in
if (t[0] * row[0] + t[1] * row[1] + t[2] * row[2] != 0)
candidates.push_back(t);
}
// The general reflections lying near the row's direction, once for all seven.
// The general reflections lying near the row's direction, once for all seven. The test is
// independent per reflection, so it is asked on all threads and the cone kept in index order.
std::vector<uint8_t> in_cone(use.size(), 0);
ParallelChunks(static_cast<int>(use.size()), nthreads, [&](int lo, int hi) {
for (int i = lo; i < hi; ++i)
in_cone[i] = use[i] && shell[i] >= 0
&& (H[i] == 0 ? 1 : 0) + (K[i] == 0 ? 1 : 0) + (L[i] == 0 ? 1 : 0) < 2
&& std::fabs(CosToRow(cell, H[i], K[i], L[i], row)) > PSEUDO_TRANSLATION_CONE_COS;
});
std::vector<size_t> cone;
for (size_t i = 0; i < use.size(); ++i) {
if (!use[i] || shell[i] < 0)
continue;
if ((H[i] == 0 ? 1 : 0) + (K[i] == 0 ? 1 : 0) + (L[i] == 0 ? 1 : 0) >= 2)
continue;
if (std::fabs(CosToRow(cell, H[i], K[i], L[i], row)) > PSEUDO_TRANSLATION_CONE_COS)
for (size_t i = 0; i < use.size(); ++i)
if (in_cone[i])
cone.push_back(i);
}
// cos(2 pi h.u) of every cone member, in cone order, on all threads: the cosine is what the two
// walks below cost, and each of them then sums over these in cone order, as it always did.
std::vector<double> phase(cone.size());
auto phases = [&](const std::array<double, 3>& u) {
ParallelChunks(static_cast<int>(cone.size()), nthreads, [&](int lo, int hi) {
for (int j = lo; j < hi; ++j) {
const size_t i = cone[j];
phase[j] = std::cos(2.0 * gemmi::pi() * (H[i] * u[0] + K[i] * u[1] + L[i] * u[2]));
}
});
};
// WHERE the translation is is found by a least-squares fit of E^2 ~ A + B cos(2 pi h.u) over
// the cone, shell by shell: the summed slope B peaks where the phases line up, which is at the
// crystal's own translation, and scrambles away within a few thousandths.
auto cos_fit = [&](const std::array<double, 3>& u) {
std::vector<double> n(bins, 0.0), sc(bins, 0.0), scc(bins, 0.0), se(bins, 0.0),
sec(bins, 0.0);
for (size_t i : cone) {
phases(u);
for (size_t j = 0; j < cone.size(); ++j) {
const size_t i = cone[j];
const int b = shell[i];
const double c =
std::cos(2.0 * gemmi::pi() * (H[i] * u[0] + K[i] * u[1] + L[i] * u[2]));
const double c = phase[j];
const double e = std::max(0.0, Esq[i]);
n[b] += 1.0; sc[b] += c; scc[b] += c * c; se[b] += e; sec[b] += e * c;
}
@@ -386,10 +432,11 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array<in
auto window_depth = [&](const std::array<double, 3>& u, std::vector<double>* per_shell) {
std::vector<double> lo(bins, 0.0), hi(bins, 0.0);
std::vector<size_t> nlo(bins, 0), nhi(bins, 0);
for (size_t i : cone) {
phases(u);
for (size_t j = 0; j < cone.size(); ++j) {
const size_t i = cone[j];
const int b = shell[i];
const double c =
std::cos(2.0 * gemmi::pi() * (H[i] * u[0] + K[i] * u[1] + L[i] * u[2]));
const double c = phase[j];
const double e = std::max(0.0, Esq[i]);
if (c < -PHASE_WINDOW_COS) { lo[b] += e; ++nlo[b]; }
else if (c > PHASE_WINDOW_COS) { hi[b] += e; ++nhi[b]; }
@@ -742,19 +789,21 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array<in
std::vector<HKLKey> key(n);
std::vector<char> pass_absence(n, 0), pass_cc(n, 0);
for (size_t i = 0; i < n; ++i) {
const auto& r = merged[i];
H[i] = r.h; K[i] = r.k; L[i] = r.l;
I[i] = r.I;
Sigma[i] = std::isfinite(r.sigma) && r.sigma > 0 ? r.sigma : 0.0;
key[i] = Canonicalize(r.h, r.k, r.l, opt.merge_friedel);
ParallelChunks(static_cast<int>(n), opt.nthreads, [&](int lo, int hi) {
for (int i = lo; i < hi; ++i) {
const auto& r = merged[i];
H[i] = r.h; K[i] = r.k; L[i] = r.l;
I[i] = r.I;
Sigma[i] = std::isfinite(r.sigma) && r.sigma > 0 ? r.sigma : 0.0;
key[i] = Canonicalize(r.h, r.k, r.l, opt.merge_friedel);
const bool finite = std::isfinite(r.I) && std::isfinite(r.sigma) && r.sigma > 0 &&
std::isfinite(r.d) && r.d > 0;
const bool in_range = finite && (opt.d_min_limit_A <= 0 || r.d >= opt.d_min_limit_A);
IoverSigma[i] = finite ? r.I / r.sigma : 0.0;
pass_absence[i] = in_range;
}
const bool finite = std::isfinite(r.I) && std::isfinite(r.sigma) && r.sigma > 0 &&
std::isfinite(r.d) && r.d > 0;
const bool in_range = finite && (opt.d_min_limit_A <= 0 || r.d >= opt.d_min_limit_A);
IoverSigma[i] = finite ? r.I / r.sigma : 0.0;
pass_absence[i] = in_range;
}
});
// present_i_over_sigma is a cut on the reflection's own significance, and the merged I/sigma is not
// that: it carries the error model's (b*I)^2 term, so it saturates at ISa = 1/b for a reflection
@@ -788,8 +837,7 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array<in
for (size_t i = 0; i < n; ++i)
if (subset[i])
order.push_back(i);
std::sort(order.begin(), order.end(),
[&](size_t a, size_t b) { return merged[a].d > merged[b].d; }); // low res -> high res
SortLowToHighResolution(order, merged);
// A hundred reflections a shell, at most twenty-five shells - and integer division, so
// below two hundred there is a single shell and E^2 is I over one global mean. That
// leaves the operator correlation exactly where it was, Pearson being invariant to a
@@ -1059,7 +1107,7 @@ SearchSpaceGroupResult SearchSpaceGroup(
// with is {0,0,-1} rather than {0,0,1} and a hand-written key silently never matches.
for (const AxialRow& row : {RowOf(1, 0, 0), RowOf(0, 1, 0), RowOf(0, 0, 1)}) {
auto m = FindRowModulation(*opt.cell, row, pass_absence, H, K, L, Esq, absence_shell,
absence_shell_count);
absence_shell_count, opt.nthreads);
if (m.found) {
result.pseudo_translations.push_back({row, m.halves, m.deepest});
row_modulation.emplace(row, std::move(m));
@@ -1149,8 +1197,7 @@ SearchSpaceGroupResult SearchSpaceGroup(
for (size_t i = 0; i < n; ++i)
if (pass_cc[i])
order.push_back(i);
std::sort(order.begin(), order.end(),
[&](size_t a, size_t b) { return merged[a].d > merged[b].d; });
SortLowToHighResolution(order, merged);
const size_t per = 100;
double r_num = 0.0, r_den = 0.0;
for (size_t lo = 0; lo < order.size(); lo += per) {
@@ -1827,7 +1874,8 @@ SearchSpaceGroupResult SearchSpaceGroup(
bool l_refused = false;
LTestUnderMerge l_test;
if (consistent && !c.pg->rotations.empty() && c.pg->representative && holds_lattice) {
l_test = AnalyzeLTestUnderMerge(merged, c.pg->rotations, c.pg->representative->centring_type());
l_test = AnalyzeLTestUnderMerge(merged, c.pg->rotations, c.pg->representative->centring_type(),
opt.nthreads);
constexpr double PERFECT_TWIN_L = 0.375;
constexpr double TWIN_LIKE_L = 0.44; // AnalyzeTwinning's own bar for a twin-like <|L|>
constexpr double MIN_GAP_CLOSED = 1.0 / 3.0;
@@ -2164,8 +2212,10 @@ SearchSpaceGroupResult SearchSpaceGroup(
}
}
std::vector<SpaceGroupCandidateScore> sg_scored(sg_cands.size());
ParallelFor(static_cast<int>(sg_cands.size()),
std::min<size_t>(opt.nthreads, sg_cands.size()), [&](int ci) {
// One candidate's score from its reflection classes (ClassifyReflection, one byte per reflection).
// Everything here is a walk over the merge in reflection order on one thread, so the sums are the
// ones a single pass always formed.
const auto score_candidate = [&](size_t ci, const std::vector<uint8_t>& refl_class) {
const gemmi::SpaceGroup& sg = *sg_cands[ci];
const gemmi::GroupOps gops = sg.operations();
SpaceGroupCandidateScore s{.space_group = sg};
@@ -2208,9 +2258,9 @@ SearchSpaceGroupResult SearchSpaceGroup(
std::vector<std::vector<double>> zone_present_esq(mirrors.size());
for (size_t i = 0; i < n; ++i) {
if (!pass_absence[i])
const uint8_t refl = refl_class[i];
if ((refl & REFL_CLASS_MASK) == REFL_UNUSED)
continue;
const gemmi::Op::Miller hkl{{H[i], K[i], L[i]}};
// Present := statistically significant AND intensity-significant. The E^2 gate keeps a
// weak axial reflection with an under-estimated sigma (fake high I/sigma) from faking a
// screw-axis violation; it only relaxes "present", so it cannot over-call a screw whose
@@ -2224,20 +2274,20 @@ SearchSpaceGroupResult SearchSpaceGroup(
const bool present = IoverSigma[i] > present_cut &&
(opt.present_e_squared <= 0.0 || Esq[i] > opt.present_e_squared);
if (CenteringAbsent(gops, hkl)) {
if ((refl & REFL_CLASS_MASK) == REFL_CENTRING_ABSENT) {
s.absent_observed += 1;
absent_sum += IoverSigma[i];
centering_absent += 1;
centering_absent_sum += IoverSigma[i];
centering_absent_esq += std::max(0.0, Esq[i]);
if (present) { s.absent_violations += 1; centering_violations += 1; }
} else if (gops.is_systematically_absent(hkl)) {
} else if ((refl & REFL_CLASS_MASK) == REFL_ABSENT) {
s.absent_observed += 1;
absent_sum += IoverSigma[i];
// A zonal absence belongs to the GLIDE that causes it and an axial one to the SCREW;
// MirrorZoneOf returns -1 for the axial rows, so neither test ever sees the other's
// reflections and no reflection is scored twice.
const int mz = MirrorZoneOf(mirrors, hkl);
const int mz = (refl >> 4) - 1;
if (mz >= 0)
glide_absent_refl.push_back({mz, Esq[i], Esq[i] / zone_tncs_expected[i],
IoverSigma[i]});
@@ -2251,13 +2301,13 @@ SearchSpaceGroupResult SearchSpaceGroup(
if (present) present_strong += 1;
// A non-identity rotation of the group maps this reflection to itself, i.e. it lies on
// a rotation axis - the control class for that axis's screw absences.
if (gops.epsilon_factor_without_centering(hkl) > 1)
if (refl & REFL_ON_AXIS)
row_present_esq[RowOf(H[i], K[i], L[i])].push_back(Esq[i]);
// ... and the control class of a GLIDE zone: the reflections of the same plane the
// group does NOT extinguish. Raw E^2, as the screw row's control is: the modulation
// correction divides the absent class by the scale it is judged against, and the
// control class IS that scale.
const int mz = MirrorZoneOf(mirrors, hkl);
const int mz = (refl >> 4) - 1;
if (mz >= 0)
zone_present_esq[mz].push_back(Esq[i]);
}
@@ -2601,7 +2651,29 @@ SearchSpaceGroupResult SearchSpaceGroup(
|| (s.screw_absence_evidence >= opt.min_screw_absence_evidence && screw_deferral_ok);
s.consistent = centering_ok && screw_ok;
sg_scored[ci] = std::move(s);
});
};
// Classifying the reflections is what scoring a candidate costs - a few dozen operators applied to
// every reflection - and it is independent per reflection, so each candidate's classes are formed
// on all threads, and the candidates of a batch are then scored side by side. A batch is as many
// candidates as there are threads, which bounds the class bytes held at once.
const size_t batch = std::max<size_t>(1, std::min(opt.nthreads, sg_cands.size()));
std::vector<std::vector<uint8_t>> refl_class(batch, std::vector<uint8_t>(n));
for (size_t c0 = 0; c0 < sg_cands.size(); c0 += batch) {
const size_t c1 = std::min(sg_cands.size(), c0 + batch);
for (size_t ci = c0; ci < c1; ++ci) {
const gemmi::GroupOps gops = sg_cands[ci]->operations();
const std::vector<gemmi::Op::Rot> mirrors = MirrorDirections(gops);
std::vector<uint8_t>& cls = refl_class[ci - c0];
ParallelChunks(static_cast<int>(n), opt.nthreads, [&](int lo, int hi) {
for (int i = lo; i < hi; ++i)
cls[i] = pass_absence[i] ? ClassifyReflection(gops, mirrors, {{H[i], K[i], L[i]}})
: REFL_UNUSED;
});
}
ParallelFor(static_cast<int>(c1 - c0), c1 - c0, [&](int b) {
score_candidate(c0 + b, refl_class[b]);
});
}
for (size_t ci = 0; ci < sg_scored.size(); ++ci) {
if (const auto it = same_absences.find(sg_cands[ci]); it != same_absences.end())
sg_scored[ci].same_absences = it->second;
+85 -53
View File
@@ -482,7 +482,8 @@ std::string TwinningVerdictLine(const TwinningAnalysisResult& result) {
}
LTestUnderMerge AnalyzeLTestUnderMerge(const std::vector<MergedReflection>& merged,
const std::vector<gemmi::Op>& rotations, char lattice_centring) {
const std::vector<gemmi::Op>& rotations, char lattice_centring,
size_t nthreads) {
LTestUnderMerge result;
gemmi::GroupOps centring;
centring.sym_ops = {gemmi::Op::identity()};
@@ -495,13 +496,22 @@ LTestUnderMerge AnalyzeLTestUnderMerge(const std::vector<MergedReflection>& merg
return rotate(op, r.h, r.k, r.l) == gemmi::Op::Miller{{-r.h, -r.k, -r.l}};
});
};
// Every per-reflection question below is answered on all threads into an array indexed by the
// reflection; the sums over them are then taken on one thread in the order the serial loop took
// them, so the result does not depend on the thread count.
const int n_merged = static_cast<int>(merged.size());
std::vector<uint8_t> usable(merged.size());
ParallelChunks(n_merged, nthreads, [&](int lo, int hi) {
for (int i = lo; i < hi; ++i) {
const auto& r = merged[i];
usable[i] = UsableIntensity(r) && std::isfinite(r.sigma) && r.sigma > 0.0 && !centric(r)
&& !centring.is_systematically_absent(gemmi::Op::Miller{{r.h, r.k, r.l}});
}
});
std::vector<size_t> used;
for (size_t i = 0; i < merged.size(); ++i) {
const auto& r = merged[i];
if (UsableIntensity(r) && std::isfinite(r.sigma) && r.sigma > 0.0 && !centric(r)
&& !centring.is_systematically_absent(gemmi::Op::Miller{{r.h, r.k, r.l}}))
for (size_t i = 0; i < merged.size(); ++i)
if (usable[i])
used.push_back(i);
}
if (used.empty())
return result;
@@ -527,30 +537,34 @@ LTestUnderMerge AnalyzeLTestUnderMerge(const std::vector<MergedReflection>& merg
isig[b] += merged[i].I / merged[i].sigma;
++n[b];
}
std::vector<double> shell_mean(n_shells, 0.0);
for (int b = 0; b < n_shells; ++b) {
if (n[b] == 0 || isig[b] / n[b] < 1.0 || sum[b] <= 0.0)
continue;
const double mean = sum[b] / n[b];
double kept = 0.0;
int n_kept = 0;
for (const size_t i : used)
if (shell_of(merged[i].d) == b && merged[i].I / mean <= 8.0) {
kept += merged[i].I;
++n_kept;
}
if (n_kept > 0 && kept > 0.0)
shell_mean[b] = kept / n_kept;
// The re-fit of every shell in one walk: each shell's sum still runs over its own reflections in
// the order of `used`.
std::vector<double> mean(n_shells, 0.0), kept(n_shells, 0.0);
std::vector<int> n_kept(n_shells, 0);
for (int b = 0; b < n_shells; ++b)
if (n[b] > 0 && isig[b] / n[b] >= 1.0 && sum[b] > 0.0)
mean[b] = sum[b] / n[b];
for (const size_t i : used) {
const int b = shell_of(merged[i].d);
if (mean[b] > 0.0 && merged[i].I / mean[b] <= 8.0) {
kept[b] += merged[i].I;
++n_kept[b];
}
}
std::vector<double> shell_mean(n_shells, 0.0);
for (int b = 0; b < n_shells; ++b)
if (n_kept[b] > 0 && kept[b] > 0.0)
shell_mean[b] = kept[b] / n_kept[b];
// E^2 per reflection, and where each sits by its indices.
std::vector<double> e2(merged.size(), 0.0);
std::unordered_map<int64_t, size_t> where;
where.reserve(used.size());
for (const size_t i : used) {
const double mean = shell_mean[shell_of(merged[i].d)];
if (mean <= 0.0)
const double mean_b = shell_mean[shell_of(merged[i].d)];
if (mean_b <= 0.0)
continue;
e2[i] = merged[i].I / mean;
e2[i] = merged[i].I / mean_b;
where.emplace(PackHKL(merged[i].h, merged[i].k, merged[i].l), i);
}
// Each reflection's orbit - the members present in the merge, itself included, named by the lowest
@@ -558,45 +572,63 @@ LTestUnderMerge AnalyzeLTestUnderMerge(const std::vector<MergedReflection>& merg
// pair, so the other hand is looked for where the rotated one is not. The mean is not inverse-variance
// weighted: with an intensity-proportional error term that would lean it towards the weaker members,
// and (I1 + I2)/2 is what a false operator's merge is predicted to read.
// Formed for each reflection `where` holds (the first one of its indices), independently.
std::vector<double> e2_merged(merged.size(), 0.0);
std::vector<size_t> orbit_label(merged.size(), 0);
for (const auto& [key, i] : where) {
std::vector<size_t> members{i};
for (const auto& op : rotations) {
const auto m = rotate(op, merged[i].h, merged[i].k, merged[i].l);
auto it = where.find(PackHKL(m[0], m[1], m[2]));
if (it == where.end())
it = where.find(PackHKL(-m[0], -m[1], -m[2]));
if (it != where.end() && std::find(members.begin(), members.end(), it->second) == members.end())
members.push_back(it->second);
ParallelChunks(static_cast<int>(used.size()), nthreads, [&](int lo, int hi) {
std::vector<size_t> members;
for (int u = lo; u < hi; ++u) {
const size_t i = used[u];
const auto self = where.find(PackHKL(merged[i].h, merged[i].k, merged[i].l));
if (self == where.end() || self->second != i)
continue;
members.assign(1, i);
for (const auto& op : rotations) {
const auto m = rotate(op, merged[i].h, merged[i].k, merged[i].l);
auto it = where.find(PackHKL(m[0], m[1], m[2]));
if (it == where.end())
it = where.find(PackHKL(-m[0], -m[1], -m[2]));
if (it != where.end() && std::find(members.begin(), members.end(), it->second) == members.end())
members.push_back(it->second);
}
double sum_e2 = 0.0;
for (const size_t j : members)
sum_e2 += e2[j];
e2_merged[i] = sum_e2 / static_cast<double>(members.size());
orbit_label[i] = *std::min_element(members.begin(), members.end());
}
double sum_e2 = 0.0;
for (const size_t j : members)
sum_e2 += e2[j];
e2_merged[i] = sum_e2 / static_cast<double>(members.size());
orbit_label[i] = *std::min_element(members.begin(), members.end());
}
});
// Following Padilla & Yeates (2003) Acta Cryst. D59, 1124-1130, partners as AnalyzeTwinning takes
// them: the first present reflection along these steps.
// them: the first present reflection along these steps. Each reflection's partner on all threads,
// the two sums over the pairs in order on one.
const std::array<std::array<int, 3>, 5> steps{{{2, 0, 0}, {0, 2, 0}, {0, 0, 2}, {1, 1, 0}, {1, 1, 3}}};
double sum_l = 0.0, sum_l_merged = 0.0;
for (const size_t i : used) {
if (!where.contains(PackHKL(merged[i].h, merged[i].k, merged[i].l)) || e2[i] <= 0.0
|| e2_merged[i] <= 0.0)
continue;
for (const auto& s : steps) {
const auto it = where.find(PackHKL(merged[i].h + s[0], merged[i].k + s[1], merged[i].l + s[2]));
if (it == where.end())
std::vector<int64_t> partner(used.size(), -1);
ParallelChunks(static_cast<int>(used.size()), nthreads, [&](int lo, int hi) {
for (int u = lo; u < hi; ++u) {
const size_t i = used[u];
if (!where.contains(PackHKL(merged[i].h, merged[i].k, merged[i].l)) || e2[i] <= 0.0
|| e2_merged[i] <= 0.0)
continue;
const size_t j = it->second;
if (orbit_label[j] != orbit_label[i] && e2[j] > 0.0 && e2_merged[j] > 0.0) {
sum_l += std::fabs(e2[i] - e2[j]) / (e2[i] + e2[j]);
sum_l_merged += std::fabs(e2_merged[i] - e2_merged[j]) / (e2_merged[i] + e2_merged[j]);
++result.pairs;
for (const auto& s : steps) {
const auto it = where.find(PackHKL(merged[i].h + s[0], merged[i].k + s[1], merged[i].l + s[2]));
if (it == where.end())
continue;
const size_t j = it->second;
if (orbit_label[j] != orbit_label[i] && e2[j] > 0.0 && e2_merged[j] > 0.0)
partner[u] = static_cast<int64_t>(j);
break; // one neighbour per reflection
}
break; // one neighbour per reflection
}
});
double sum_l = 0.0, sum_l_merged = 0.0;
for (size_t u = 0; u < used.size(); ++u) {
if (partner[u] < 0)
continue;
const size_t i = used[u], j = static_cast<size_t>(partner[u]);
sum_l += std::fabs(e2[i] - e2[j]) / (e2[i] + e2[j]);
sum_l_merged += std::fabs(e2_merged[i] - e2_merged[j]) / (e2_merged[i] + e2_merged[j]);
++result.pairs;
}
if (result.pairs > 0) {
result.mean_abs_l = sum_l / result.pairs;
@@ -115,7 +115,8 @@ struct LTestUnderMerge {
};
LTestUnderMerge AnalyzeLTestUnderMerge(const std::vector<MergedReflection>& merged,
const std::vector<gemmi::Op>& rotations, char lattice_centring);
const std::vector<gemmi::Op>& rotations, char lattice_centring,
size_t nthreads = 1);
// Twin-immune zone evidence for the operators a point group adds over each of its index-2 subgroups,
// read on a P1 merge (or on the adopted group's own): reported for every 2-fold the adopted group