From 13ddef63d344d113e4bb86660106ff42610350d1 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 12:19:12 +0100 Subject: [PATCH] Refinement: the B ceiling lifted for a model that arrives with B above it The ceiling of B was clamp(2 Bmax, 100, 300), and PrepareModel cut every B to 300 before it was set: a structure refined at 4-5 A (mean B 380, most atoms above 300) was flattened, its as-given R-free 0.05 above the validation's on the same model (0.359 against 0.306), and the refinement ended worse than the validation (0.339). The 300 bound stays for every model under it (it sizes the gather); a model above it gets a ceiling of 1.5 times its largest B (BCeiling), PrepareModel keeps only the floor, and an anisotropic U is scaled to the model's ceiling, not the constant. The case above: as given 0.325, refined 0.242. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/Refinement.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 8f94870b3..2dbc0ba8d 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -52,6 +52,12 @@ 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; +// The ceiling of B for a model whose highest B is `b_max`: twice it, within the bounds - the bounds lifted for a +// model that comes with B above them (a structure refined at 4-5 A, B 400-600 A^2), which the ceiling would +// otherwise flatten (the gather is reserved for it). +double BCeiling(double b_max) { + return std::clamp(2 * b_max, B_CEILING_MIN, std::max(B_CEILING_MAX, 1.5 * b_max)); +} // 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 @@ -141,7 +147,7 @@ gemmi::Structure PrepareModel(const gemmi::Structure &placed, const gemmi::UnitC a.charge = ScatteringCharge(r, a); if (a.aniso.nonzero()) a.b_iso = static_cast(gemmi::u_to_b() * a.aniso.trace() / 3.0); - a.b_iso = static_cast(std::clamp(a.b_iso, B_FLOOR, B_CEILING_MAX)); + a.b_iso = static_cast(std::max(a.b_iso, B_FLOOR)); } } gemmi::remove_empty_children(st); @@ -266,8 +272,8 @@ struct Context { continue; // its widest axis no wider than the refinement allows const double top = AnisoRadiusB(at.aniso.scaled(gemmi::u_to_b())); - if (top > B_CEILING_MAX) { - at.aniso = at.aniso.scaled(static_cast(B_CEILING_MAX / top)); + if (top > b_ceiling) { + at.aniso = at.aniso.scaled(static_cast(b_ceiling / top)); at.b_iso = static_cast(gemmi::u_to_b() * at.aniso.trace() / 3.0); } const double t = at.aniso.trace() / 3.0; @@ -2308,7 +2314,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vectorb_iso + gemmi::u_to_b() * body.U(ctx.atoms[i]->pos).trace() / 3.0); - ctx.b_ceiling = std::clamp(2 * b_max, B_CEILING_MIN, B_CEILING_MAX); + ctx.b_ceiling = BCeiling(b_max); if (ctx.atoms.empty()) { result.failure_reason = "the model has no atoms"; return result; @@ -3297,7 +3303,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector(b_max, ctx.atoms[i]->b_iso); - const double ceiling = std::clamp(2 * b_max, B_CEILING_MIN, B_CEILING_MAX); + const double ceiling = BCeiling(b_max); if (ceiling > ctx.b_ceiling) { const RefinementStage stage("engine"); for (std::unique_ptr &o : own)