From daba7e7dd466d88b35e7f87b7b1e084b88ae2743 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 10:58:09 +0100 Subject: [PATCH] Refinement: every atom's B tensor bounded the same way, the ceiling last The GPU's density gather reserves its pairs for every atom at the B ceiling at most. Two paths could leave an atom wider than that: the TLS bodies applied to the atoms bounded the ceiling first and then raised the smallest eigenvalue to the floor, which could lift the largest above the ceiling again; and the model's own TLS groups were read (as total B and as residual B) without any bound on the anisotropy. Either failed the refinement with "model density: gather pairs over the reserve" - seen on a model brought in by molecular replacement with its TLS groups, and whenever riding hydrogens were off (with them, the hydrogens' share of the reserve hid the overflow). One helper, BoundedB, now bounds all of these as the per-atom U already was: floor first, ceiling last. Battery refine arm identical to rc186; [Refinement] and [refine_only] pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/Refinement.cpp | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 07f0e089c..3bcb8e59a 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -52,6 +52,21 @@ namespace { constexpr double B_FLOOR = 2.0; // A^2: no atom's B goes below it // nor above twice the model's largest B, within these bounds (the GPU's gather is reserved for it) constexpr double B_CEILING_MIN = 100.0, B_CEILING_MAX = 300.0; + +// A B tensor (8 pi^2 U) inside the bounds: its smallest eigenvalue raised to `floor` by a multiple of I, then the +// whole tensor scaled down so that its largest is at most `ceiling`. The ceiling comes last because the GPU's +// gather is reserved for atoms no wider than it. +gemmi::SMat33 BoundedB(gemmi::SMat33 b, double floor, double ceiling) { + const std::array ev = b.calculate_eigenvalues(); + const double lowest = std::min({ev[0], ev[1], ev[2]}); + if (!(lowest >= floor)) + b = b.added_kI(floor - (std::isfinite(lowest) ? lowest : 0.0)); + const std::array ev2 = b.calculate_eigenvalues(); + const double top = std::max({ev2[0], ev2[1], ev2[2]}); + if (top > ceiling) + b = b.scaled(ceiling / top); + return b; +} // How far below the smallest B of the model a macro-cycle's B refinement may take an atom; the blur of the // cycle is chosen for that. constexpr double B_STEP_DOWN = 10.0; @@ -209,21 +224,10 @@ struct Context { for (const TlsBody &body : tls) for (int i : body.atoms) { const gemmi::SMat33 u = body.U(atoms[i]->pos); - double t = u.trace() / 3.0; const double residual = atoms[i]->b_iso - tls_b[i]; - // the total U inside the bounds: its largest eigenvalue (in B) at most the ceiling, its smallest at least - // the floor - the whole U scaled down, or raised by a multiple of I - gemmi::SMat33 b = u.scaled(gemmi::u_to_b()).added_kI(residual); - std::array ev = b.calculate_eigenvalues(); - const double top = std::max({ev[0], ev[1], ev[2]}); - if (top > b_ceiling) { - b = b.scaled(b_ceiling / top); - ev = b.calculate_eigenvalues(); - } - const double lowest = std::min({ev[0], ev[1], ev[2]}); - if (!(lowest >= b_floor)) - b = b.added_kI(b_floor - (std::isfinite(lowest) ? lowest : 0.0)); - t = b.trace() / 3.0; + // the total U inside the bounds + const gemmi::SMat33 b = BoundedB(u.scaled(gemmi::u_to_b()).added_kI(residual), b_floor, b_ceiling); + const double t = b.trace() / 3.0; atoms[i]->b_iso = static_cast(t); // what the bodies hold of B, the bounds' share included: the residual stays as it was tls_b[i] = atoms[i]->b_iso - residual; @@ -883,15 +887,7 @@ private: // Atom i's U from 8 pi^2 U = p[0..5]: its smallest eigenvalue raised to the cycle's floor and its largest brought // down to the ceiling where they are outside, then its B and its anisotropy. void SetU(int i, const double *p) const { - gemmi::SMat33 b{p[0], p[1], p[2], p[3], p[4], p[5]}; - const std::array ev = b.calculate_eigenvalues(); - const double lowest = std::min({ev[0], ev[1], ev[2]}); - if (!(lowest >= ctx_.b_floor)) - b = b.added_kI(ctx_.b_floor - (std::isfinite(lowest) ? lowest : 0.0)); - const std::array ev2 = b.calculate_eigenvalues(); - const double top = std::max({ev2[0], ev2[1], ev2[2]}); - if (top > ctx_.b_ceiling) - b = b.scaled(ctx_.b_ceiling / top); + const gemmi::SMat33 b = BoundedB({p[0], p[1], p[2], p[3], p[4], p[5]}, ctx_.b_floor, ctx_.b_ceiling); const double t = b.trace() / 3.0; ctx_.atoms[i]->b_iso = static_cast(t); AtomAniso &a = ctx_.aniso[i]; @@ -2189,13 +2185,17 @@ RFactors ReadInputDisplacements(Context &ctx, const std::vector &tls, L Reading read{residual ? "TLS, B residual" : "TLS, B total", b0, aniso0.empty() ? std::vector(ctx.atoms.size()) : aniso0}; for (const auto &[i, u] : tls_u) { - const double t = u.trace() / 3.0; + // the atom's B (with U_TLS's trace added for the residual reading) and U_TLS's anisotropy, inside the bounds + const double t_u = u.trace() / 3.0; + const double b_i = residual ? b0[i] + gemmi::u_to_b() * t_u : b0[i]; + const gemmi::SMat33 b = BoundedB(u.added_kI(-t_u).scaled(gemmi::u_to_b()).added_kI(b_i), + ctx.b_floor, ctx.b_ceiling); + const double t = b.trace() / 3.0; + read.b[i] = t; read.aniso[i].on = true; - read.aniso[i].dev = {static_cast(u.u11 - t), static_cast(u.u22 - t), - static_cast(u.u33 - t), static_cast(u.u12), static_cast(u.u13), - static_cast(u.u23)}; - if (residual) - read.b[i] = std::min(b0[i] + gemmi::u_to_b() * t, ctx.b_ceiling); + read.aniso[i].dev = {static_cast((b.u11 - t) / gemmi::u_to_b()), static_cast((b.u22 - t) / gemmi::u_to_b()), + static_cast((b.u33 - t) / gemmi::u_to_b()), static_cast(b.u12 / gemmi::u_to_b()), + static_cast(b.u13 / gemmi::u_to_b()), static_cast(b.u23 / gemmi::u_to_b())}; } readings.push_back(std::move(read)); }