diff --git a/rugnux/RugnuxPasses.cpp b/rugnux/RugnuxPasses.cpp index 99b15b948..7981ffd1f 100644 --- a/rugnux/RugnuxPasses.cpp +++ b/rugnux/RugnuxPasses.cpp @@ -138,24 +138,31 @@ namespace { // are whole-number combinations of the other's, to the 0.1 ScaleMergeAndSymmetry reindexes to. The // cell edges alone do not say it: a pre-pass at a beam 15 px off was measured settling on a cell of // edges within 2 % of the right one's, in another orientation, with 8 of 60 validation frames. - bool IndexesSameLattice(const ProcessResult &a, const ProcessResult &b) { + // Returned is the largest distance from a whole number. A pre-pass at another distance indexes the + // same crystal on a cell scaled with the distance, so that scale - `scale`, a's cell over b's: the + // ratio of the distances they were integrated at - is taken out first. Not the ratio of their + // volumes: a supercell twice the cell along all three axes would then read as the same lattice. + double LatticeMismatch(const ProcessResult &a, const ProcessResult &b, double scale) { + if (!a.end_message.rotation_lattice || !b.end_message.rotation_lattice) + return std::numeric_limits::infinity(); const auto primitive = [](const ProcessResult &r) { const auto &type = r.end_message.rotation_lattice_type; return r.end_message.rotation_lattice->ToPrimitive(type ? type->centering : 'P'); }; - const auto integral = [](const CrystalLattice &x, const CrystalLattice &y) { + const auto mismatch = [](const CrystalLattice &x, const CrystalLattice &y, double scale) { const Coord v[3] = {x.Vec0(), x.Vec1(), x.Vec2()}; const Coord rs[3] = {y.Astar(), y.Bstar(), y.Cstar()}; + double worst = 0.0; for (int i = 0; i < 3; ++i) - for (int j = 0; j < 3; ++j) - if (!(std::fabs(v[i] * rs[j] - std::round(v[i] * rs[j])) < 0.1)) - return false; - return true; + for (int j = 0; j < 3; ++j) { + const double n = v[i] * rs[j] / scale; + worst = std::max(worst, std::fabs(n - std::round(n))); + } + return worst; }; - if (!a.end_message.rotation_lattice || !b.end_message.rotation_lattice) - return false; const CrystalLattice pa = primitive(a), pb = primitive(b); - return integral(pa, pb) && integral(pb, pa); + const double m = std::max(mismatch(pa, pb, scale), mismatch(pb, pa, 1.0 / scale)); + return std::isfinite(m) ? m : std::numeric_limits::infinity(); } } @@ -893,6 +900,11 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o // changes sign. The first is the fixed point found and realised in one pass: on most crystals the // data integrated at the free fit's distance ask for that distance again, and the passes that walked // past it to bracket it only confirmed the sign change and realised the same residual again. A + // distance that holds its own while the move it asks for still points on, the way every distance + // before it asked, is not that: along the direction the data do not determine the ask stays the same + // size from wherever it is integrated, and only its significance wanders (measured: four distances + // each asking for 0.8-1.0 mm more, the fourth held by its own test). It is one more distance tried, + // and the walk goes on to the bracket test below. A // pre-pass that did not index pass 1's lattice is not used, and ends the search: its ask and its // residual are another lattice's, and no residual of it can be compared with pass 1's. std::vector> points{{d0, held.distance_free_mm}}; @@ -902,7 +914,8 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o const double d = d0 + k * move; const ProcessResult r = prepass_at(d); const double a = asked(r); - const bool same_lattice = IndexesSameLattice(pass1, r); + const double mismatch = LatticeMismatch(pass1, r, d0 / d); + const bool same_lattice = mismatch < 0.1; text += fmt::format(", {:.2f} -> {:.2f}{}", d, a, same_lattice ? "" : " (another lattice)"); if (!same_lattice) { const auto cell = [](const ProcessResult &x) { @@ -912,18 +925,18 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o return fmt::format("{:.3f} {:.3f} {:.3f} {:.2f} {:.2f} {:.2f}", c.a, c.b, c.c, c.alpha, c.beta, c.gamma); }; logger.Info("Distance check: the pre-pass at {:.3f} mm indexed another lattice than pass 1's - not used " - "(primitive {} against pass 1's {})", d, cell(r), cell(pass1)); + "(primitive {} against pass 1's {}, indices {:.3f} from whole, {:.3f} before the cell scale " + "is taken out)", d, cell(r), cell(pass1), mismatch, LatticeMismatch(pass1, r, 1.0)); } if (!std::isfinite(a) || !same_lattice) break; realised.push_back({d, r.post_refine}); points.emplace_back(d, a); - if (r.post_refine->rocking_holds_distance) { - fixed = d; + if ((a - d) * move <= 0.0) { + if (r.post_refine->rocking_holds_distance) + fixed = d; break; } - if ((a - d) * move <= 0.0) - break; } if (cancelled_) return std::nullopt; @@ -958,7 +971,7 @@ std::optional> Rugnux::FixedPointDistance(RugnuxObserver *o std::optional star; if (tried != realised.end()) { star = *tried; - } else if (const ProcessResult r = prepass_at(d_star); IndexesSameLattice(pass1, r)) { + } else if (const ProcessResult r = prepass_at(d_star); LatticeMismatch(pass1, r, d0 / d_star) < 0.1) { star = Realised{d_star, r.post_refine}; } else { logger.Info("Distance check: the pre-pass at {:.3f} mm indexed another lattice than pass 1's - not used",