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)); }