diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index b3bd6beb0..b062da167 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -1171,11 +1171,20 @@ bool MeasuredCentreClearlyLoses(const SearchMergeSignal &file_arm, const SearchM // What a cell refined UNDER a group's own constraint leaves of that group's metric violation: float // rounding on the lattice vectors, and nothing else (the constraint holds the angles at 90 exactly, -// so the residue is 1e-6 and below). A cell scoring above this was refined under something else - -// which is what has happened wherever the group was adopted AFTER integration, on a metric no -// constrained fit ever produced. Two orders below MAX_METRIC_VIOLATION, which bounds a cell that is -// WRONG for its group rather than merely unconstrained: the first says re-refine, the second refuse. +// so the residue is 1e-6 and below: CELL_FLOAT_NOISE). A cell scoring above that was refined under +// something else - which is what has happened wherever the group was adopted AFTER integration, on a +// metric no constrained fit ever produced. Up to this bound such a cell is the usual drift of a free +// fit (alpha 90.03 on a monoclinic cell) and is only projected onto the group's metric, without a +// word (CellConstrainedToGroup); above it the deviation is worth a re-refinement and a warning. Two +// orders below MAX_METRIC_VIOLATION, which bounds a cell that is WRONG for its group rather than +// merely unconstrained: the first says re-refine, the second refuse. constexpr double MAX_CONSTRAINED_METRIC_NOISE = 1e-3; +constexpr double CELL_FLOAT_NOISE = 1e-5; + +// The nearest cell the metric of `sg` fixes (SymmetrizeMetric), where `uc`'s violation of it lies +// between CELL_FLOAT_NOISE and MAX_METRIC_VIOLATION; empty where there is nothing to change. What is +// written - report, mmCIF, .ins, MTZ, _process.h5 - has to be a cell its group can describe. +std::optional CellConstrainedToGroup(const UnitCell &uc, const gemmi::SpaceGroup &sg); // Callbacks for progress and live results. Methods may be called from worker threads, so an // implementation must be thread-safe. The default no-ops suit the CLIs. diff --git a/rugnux/RugnuxPasses.cpp b/rugnux/RugnuxPasses.cpp index e1d15770f..383a2b5b2 100644 --- a/rugnux/RugnuxPasses.cpp +++ b/rugnux/RugnuxPasses.cpp @@ -328,6 +328,13 @@ SearchMergeSignal SearchSignal(const ProcessResult &r) { r.search_merge_inner_cc_half, r.search_merge_inner_reflections}; } +std::optional CellConstrainedToGroup(const UnitCell &uc, const gemmi::SpaceGroup &sg) { + const double violation = MetricViolation(uc, sg); + if (violation <= CELL_FLOAT_NOISE || violation > MAX_METRIC_VIOLATION) + return {}; + return SymmetrizeMetric(CrystalLattice(uc), sg).GetUnitCell(); +} + bool MeasuredCentreClearlyLoses(const SearchMergeSignal &file_arm, const SearchMergeSignal &measured_arm, bool same_lattice, double sigmas) { // A count is Poisson, as the first pass's near ties read counts of reflections; a CC1/2 over n diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index ab7b9e997..cff894305 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -4081,6 +4081,27 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { : target != nullptr && !target->is_reference_setting() ? "SPACE_GROUP" : "STANDARD"); } + // The group and the axes are final here, and everything written from now on reads this cell. A + // group adopted after integration (a de-novo search on a lattice indexed in a lower class, -S, a + // reference) can leave the free fit's metric under it - a C 1 2/c 1 with alpha 90.03 - and below + // MAX_CONSTRAINED_METRIC_NOISE nothing above has touched it. The reflections stay as integrated. + if (result.consensus_cell && experiment_.GetGemmiSpaceGroup().has_value()) { + const gemmi::SpaceGroup &sg = *experiment_.GetGemmiSpaceGroup(); + if (const auto constrained = CellConstrainedToGroup(*result.consensus_cell, sg)) { + const UnitCell &uc = *result.consensus_cell; + logger.Info("Cell constrained to {}: a={:.4f} b={:.4f} c={:.4f} alpha={:.3f} beta={:.3f} " + "gamma={:.3f}, from the freely refined a={:.4f} b={:.4f} c={:.4f} alpha={:.3f} " + "beta={:.3f} gamma={:.3f}", sg.xhm(), constrained->a, constrained->b, + constrained->c, constrained->alpha, constrained->beta, constrained->gamma, + uc.a, uc.b, uc.c, uc.alpha, uc.beta, uc.gamma); + result.consensus_cell = constrained; + end_msg.unit_cell = result.consensus_cell; + } + if (end_msg.rotation_lattice.has_value() + && CellConstrainedToGroup(end_msg.rotation_lattice->GetUnitCell(), sg).has_value()) + end_msg.rotation_lattice = SymmetrizeMetric(*end_msg.rotation_lattice, sg); + } + // A pass RunAllPasses is about to throw away and run again. Its report is never printed and // its files are overwritten seconds later, so everything below that only fills those in would // be spent on an answer nobody reads. Every trigger of a re-run is settled by here - the diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index 38de2e1e5..93ce1abcb 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -2373,6 +2373,17 @@ int RugnuxInputRun::PrepareRugnuxInput() { experiment.SetSpaceGroup(*space_group); if (fixed_reference_unit_cell.has_value()) experiment.SetUnitCell(fixed_reference_unit_cell); + // ...on the metric that group fixes, as the full pipeline writes it: a file from before it did, + // or a -S the stored cell was not refined under, can hold a cell the group cannot describe. + if (experiment.GetUnitCell() && experiment.GetGemmiSpaceGroup()) + if (const auto constrained = CellConstrainedToGroup(*experiment.GetUnitCell(), + *experiment.GetGemmiSpaceGroup())) { + logger.Info("Cell constrained to {}: a={:.4f} b={:.4f} c={:.4f} alpha={:.3f} beta={:.3f} " + "gamma={:.3f}", experiment.GetGemmiSpaceGroup()->xhm(), constrained->a, + constrained->b, constrained->c, constrained->alpha, constrained->beta, + constrained->gamma); + experiment.SetUnitCell(constrained); + } // A rotation (goniometer) dataset uses RotationScaleMerge unless --force-still asks for stills scaling. IndexingSettings indexing_settings; indexing_settings.RotationIndexing(experiment.GetGoniometer().has_value() && !force_still); diff --git a/tests/RugnuxTest.cpp b/tests/RugnuxTest.cpp index e168fa846..2b812e40b 100644 --- a/tests/RugnuxTest.cpp +++ b/tests/RugnuxTest.cpp @@ -784,6 +784,37 @@ TEST_CASE("MeasuredCentreWins", "[process]") { CHECK(MeasuredCentreWins(file, arm(monoclinic, mono, 0.88, 20000)).empty()); } +// A C 1 2/c 1 crystal indexed on its primitive (triclinic) cell and refined freely there: put back into +// the C-centred setting, its alpha and gamma come out a few hundredths of a degree off 90 - too +// little for the re-refinement above MAX_CONSTRAINED_METRIC_NOISE, and not a cell the group describes. +TEST_CASE("CellConstrainedToGroup puts a triclinic-refined C2/c cell onto the monoclinic metric", "[process]") { + const auto &c2c = *gemmi::find_spacegroup_by_name("C 1 2/c 1"); + const CrystalLattice conv(UnitCell{.a = 18.0, .b = 9.0, .c = 10.0, .alpha = 90.0, .beta = 105.0, .gamma = 90.0}); + // The primitive cell of the C lattice, with the drift a free fit leaves on one of its vectors. + const Coord p0 = (conv.Vec0() + conv.Vec1()) * 0.5f + Coord(0.0f, 0.004f, 0.003f); + const Coord p1 = (conv.Vec0() - conv.Vec1()) * 0.5f; + const CrystalLattice refined(p0 + p1, p0 - p1, conv.Vec2()); + const UnitCell free_cell = refined.GetUnitCell(); + REQUIRE(MetricViolation(free_cell, c2c) > CELL_FLOAT_NOISE); + REQUIRE(MetricViolation(free_cell, c2c) < MAX_CONSTRAINED_METRIC_NOISE); + REQUIRE(std::fabs(free_cell.alpha - 90.0) + std::fabs(free_cell.gamma - 90.0) > 0.01); + + const auto fixed = CellConstrainedToGroup(free_cell, c2c); + REQUIRE(fixed.has_value()); + CHECK(fixed->alpha == Catch::Approx(90.0).margin(1e-4)); + CHECK(fixed->gamma == Catch::Approx(90.0).margin(1e-4)); + CHECK(MetricViolation(*fixed, c2c) <= CELL_FLOAT_NOISE); + CHECK(fixed->beta == Catch::Approx(free_cell.beta).margin(0.05)); + CHECK(fixed->a == Catch::Approx(free_cell.a).epsilon(1e-3)); + CHECK(fixed->b == Catch::Approx(free_cell.b).epsilon(1e-3)); + CHECK(fixed->c == Catch::Approx(free_cell.c).epsilon(1e-3)); + + // A cell already on the metric is left as it is, and so is one the group cannot describe at all. + CHECK_FALSE(CellConstrainedToGroup(*fixed, c2c).has_value()); + CHECK_FALSE(CellConstrainedToGroup(UnitCell{.a = 18.0, .b = 9.0, .c = 10.0, .alpha = 60.0, .beta = 105.0, + .gamma = 90.0}, c2c).has_value()); +} + TEST_CASE("MeasuredCentreClearlyLoses", "[process]") { // Same lattice: the measured centre loses only where it is behind on the signal by more than the // Poisson noise of the two counts AND its inner shell does not agree better by the margin.