From 28bf8e3aeb6fbd5a5fbebdca9a551229077e7c86 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 16 Nov 2026 08:08:58 +0100 Subject: [PATCH] PROTOTYPE: damped Newton steps for the positions and the B factors in refinement (RUGNUX_NEWTON) Behind environment switches, off by default; nothing changes without them. - RefinementNewton (RefinementMinimiser.h): Levenberg-Marquardt steps, (H + lambda D) d = -g by preconditioned CG, accepted on the target. - H = the X-ray curvature per atom as a multiple of I (XrayXyzCurvatureShape, the B shape's counterpart, calibrated once per trial by central differences: 2 counted evaluations) plus the restraints' Hessian: by differences of their gradient (RUGNUX_NEWTON=1) or Gauss-Newton (RUGNUX_NEWTON_GN=1, RestraintGaussNewton: J = g / sqrt(2E) per record, planes per atom). - RUGNUX_NEWTON_XYZ / _B iterations per macro-cycle (10 / 6), _CGMAX, _CGTOL, _CLIP, _RELSTOP, _TRACE; RUGNUX_NEWTON=2 an L-BFGS with the Newton H as H0 (worse, kept for the record). - RUGNUX_REFINE_STATS: MINSTAT/TRIALSTAT/TRIALSSET/WEIGHTSTAT lines on stderr (evaluation budget). - EvaluateRestraints: the per-record terms moved into RecordTerm (same operations, same order). Measured (refine-only, CPU one round, 6 sets): evaluations -40..-63% at equal R-free on average; on the GPU protocol R-free worse on 4 of 6 sets. Not for production as is. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/Refinement.cpp | 294 +++++++++++++++++- .../RefinementMinimiser.h | 176 ++++++++++- .../RefinementRestraints.cpp | 136 ++++++-- .../RefinementRestraints.h | 9 + 4 files changed, 571 insertions(+), 44 deletions(-) diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 924ed0803..9581d46da 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -97,6 +99,12 @@ constexpr double MAX_STEP_XYZ = 0.3, MAX_STEP_B = 10.0; // The loosest NCS restraint a trajectory step tries (A); looser than that, the copies are refined independently. constexpr double NCS_SIGMA_MAX = 0.4; +// PROTOTYPE instrumentation (lane k): MINSTAT/TRIALSTAT/... lines on stderr where RUGNUX_REFINE_STATS is set. +bool RefineStats() { + static const bool on = getenv("RUGNUX_REFINE_STATS") != nullptr; + return on; +} + // The figure of merit of the likelihood, , at X = 2 Fo D Fc / var: I1(X)/I0(X) for an acentric // reflection, tanh(X / 2) for a centric one. // Following Murshudov, Vagin & Dodson (1997) Acta Cryst. D53, 240-255 @@ -607,7 +615,13 @@ struct Context { // The X-ray target's curvature in each atom's B, up to one factor (CalibrateBCurvature): the likelihood's // curvature in |F_model| of the cycle, 2 (k_total D)^2 / var, summed in resolution shells, against the atom's own // part of it, (s^2 / 4)^2 f(s)^2 exp(-B s^2 / 2). A hydrogen's is its parent's. - std::vector XrayBCurvatureShape() const { + std::vector XrayBCurvatureShape() const { return XrayCurvatureShape(rows, false); } + // The same for each of an atom's x, y and z: its own part of the likelihood's curvature in |F_model| is then + // (2 pi)^2 s^2 / 3 f(s)^2 exp(-B s^2 / 2), the reflections taken as spread evenly over directions - the X-ray + // block of the normal matrix as a multiple of I per atom (Murshudov, Vagin & Dodson 1997 keep the X-ray blocks + // of atoms on the diagonal). `r`: the row model the engine has (blurred or not). + std::vector XrayXyzCurvatureShape(const XrayRowModel &r) const { return XrayCurvatureShape(r, true); } + std::vector XrayCurvatureShape(const XrayRowModel &rows, bool xyz) const { constexpr int SHELLS = 32; double s2_max = 0; for (double s2 : refl.inv_d2) @@ -638,7 +652,10 @@ struct Context { for (int k = 0; k < SHELLS; k++) if (n[k] > 0) { const double s2 = s2_sum[k] / n[k]; - shape[i] += w[k] * f[k] * a.occ * a.occ * std::exp(-a.b_iso * s2 / 2.0) * s2 * s2 / 16.0; + if (xyz) + shape[i] += w[k] * f[k] * a.occ * a.occ * std::exp(-a.b_iso * s2 / 2.0) * s2 * (4.0 * M_PI * M_PI / 3.0); + else + shape[i] += w[k] * f[k] * a.occ * a.occ * std::exp(-a.b_iso * s2 / 2.0) * s2 * s2 / 16.0; } } for (size_t i = 0; i < rider_parent.size(); i++) @@ -675,6 +692,37 @@ struct Context { return den > 0 ? std::max(num / den, 0.0) : 0.0; } + // The factor that turns XrayXyzCurvatureShape() into the X-ray target's curvature, as CalibrateBCurvature() does + // for B: along a random +-1 direction of the positions of `params`, by central differences of the gradient. + // Two evaluations, counted. + double CalibrateXyzCurvature(const std::vector ¶ms, const std::vector &shape) { + constexpr double EPS = 0.01; // A + const std::vector p0 = Positions(); + std::mt19937 rng(7); + std::vector v(params.size()); + for (gemmi::Vec3 &x : v) + x = gemmi::Vec3(rng() % 2 == 0 ? 1.0 : -1.0, rng() % 2 == 0 ? 1.0 : -1.0, rng() % 2 == 0 ? 1.0 : -1.0); + std::array>, 2> g; + for (int side = 0; side < 2; side++) { + for (size_t k = 0; k < params.size(); k++) + atoms[params[k]]->pos = p0[params[k]] + gemmi::Position(v[k] * (side == 0 ? EPS : -EPS)); + MoveRiders(); + engine->Evaluate(Model(), &g[side]); + evaluations++; + FoldRiders(g[side]); + } + SetPositions(p0); + MoveRiders(); + double num = 0, den = 0; + for (size_t k = 0; k < params.size(); k++) { + const int i = params[k]; + num += (v[k].x * (g[0][i][0] - g[1][i][0]) + v[k].y * (g[0][i][1] - g[1][i][1]) + + v[k].z * (g[0][i][2] - g[1][i][2])) / (2 * EPS); + den += 3 * shape[i]; + } + return den > 0 ? std::max(num / den, 0.0) : 0.0; + } + // F_model with the cycle's scale and solvent, for the atoms as they are now. std::vector> FModel() { engine->Fcalc(Model(), fcalc); @@ -792,6 +840,29 @@ public: return std::isfinite(*cost); } + // The restraints' gradient alone at p, as Evaluate() makes it: for products with their Hessian by differences. + void RestraintGradient(const double *p, double *gradient) const { + Set(p); + std::vector> &gr = gr_; + ctx_.engine->EvaluateRestraints(restraint_set_, ctx_.XyzB(), &gr); + const int n = static_cast(param_atoms_.size()); + for (int k = 0; k < n; k++) { + const int i = param_atoms_[k]; + if (mode_ == Mode::Xyz) { + gemmi::Vec3 g(gr[i][0], gr[i][1], gr[i][2]); + if (const SpecialPosition *sp = Special(i)) + g = sp->projector.multiply(g); + gradient[3 * k] = g.x; + gradient[3 * k + 1] = g.y; + gradient[3 * k + 2] = g.z; + } else + gradient[k] = gr[i][3]; + } + } + Mode GetMode() const { return mode_; } + double Weight() const { return weight_; } + const std::vector &Atoms() const { return param_atoms_; } + private: const SpecialPosition *Special(int atom) const { const int k = topo_.special_index.empty() ? -1 : topo_.special_index[atom]; @@ -956,16 +1027,151 @@ std::vector BDiagonal(const std::vector &xray, const std::vector // `minimiser` carries its memory from the call before; `diagonal` is the curvature it scales the steps by (none: 1). template void Minimise(Context &ctx, const Target &target, int iterations, RefinementMinimiser &minimiser, - std::vector diagonal = {}) { + std::vector diagonal = {}, const char *tag = "?") { std::vector p = target.Get(); if (p.empty()) return; minimiser.SetDiagonal(diagonal.empty() ? std::vector(p.size(), 1.0) : std::move(diagonal)); - minimiser.Minimise([&](const std::vector &x, double &f, std::vector &g) { + const RefinementMinimiser::Outcome o = minimiser.Minimise([&](const std::vector &x, double &f, std::vector &g) { return target.Evaluate(x.data(), &f, g.data()); }, p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); target.Set(p.data()); // where the minimiser ended, not where its last line search probed + if (RefineStats()) fprintf(stderr, "MINSTAT lbfgs.%s it=%d ev=%d lim=%d n=%zu f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations, + iterations, p.size(), o.f_start, o.f_end); +} + +bool NewtonHybrid(); +// PROTOTYPE (RUGNUX_NEWTON=1): the positions or the B factors by damped Newton steps (RefinementNewton). H is the +// X-ray target's curvature per parameter (`xray`: per atom, already times the weight) plus the restraints' exact +// Hessian, its products by forward differences of their gradient; D is H's diagonal, the restraints' part of it +// probed by differences on atoms far apart in the list at a time (the atoms of one restraint are near in it). +void MinimiseNewton(Context &ctx, const AtomTarget &target, int iterations, RefinementNewton &newton, + const std::vector &xray, RefinementMinimiser &lbfgs, const RestraintSet &set) { + std::vector p = target.Get(); + if (p.empty()) + return; + const bool xyz = target.GetMode() == AtomTarget::Mode::Xyz; + const int per = xyz ? 3 : 1; + const size_t n = p.size(); + const double eps = xyz ? 1e-4 : 0.1; // the largest change of a parameter in a difference (A, A^2) + std::vector h(n); + for (size_t k = 0; k < n; k++) + h[k] = xray[target.Atoms()[k / per]]; + std::vector x_at, g_at(n), gp(n), xp(n); + auto restraint_hv = [&](const std::vector &x, const std::vector &v, std::vector &hv) { + if (x_at != x) { + x_at = x; + target.RestraintGradient(x.data(), g_at.data()); + } + double v_max = 0; + for (double e : v) + v_max = std::max(v_max, std::fabs(e)); + hv.assign(n, 0.0); + if (v_max == 0) + return; + const double t = eps / v_max; + for (size_t k = 0; k < n; k++) + xp[k] = x[k] + t * v[k]; + target.RestraintGradient(xp.data(), gp.data()); + for (size_t k = 0; k < n; k++) + hv[k] = (gp[k] - g_at[k]) / t; + }; + // RUGNUX_NEWTON_GN=1: the restraints' Gauss-Newton Hessian (RestraintGaussNewton) instead of the exact one by + // differences - positive semi-definite, and its diagonal exact + static const bool gn = getenv("RUGNUX_NEWTON_GN") != nullptr; + std::vector param_of(ctx.atoms.size(), -1); + for (size_t k = 0; k < target.Atoms().size(); k++) + param_of[target.Atoms()[k]] = static_cast(k); + const int c0 = xyz ? 0 : 3; + RestraintJacobian jac; + std::vector x_jac; + auto gn_hv = [&](const std::vector &x, const std::vector &v, std::vector &hv) { + if (x_jac != x) { + x_jac = x; + target.Set(x.data()); + jac = RestraintGaussNewton(set, ctx.XyzB()); + } + hv.assign(n, 0.0); + for (size_t r = 0; r + 1 < jac.start.size(); r++) { + double dot = 0; + for (int e = jac.start[r]; e < jac.start[r + 1]; e++) + if (const int k = param_of[jac.atom[e]]; k >= 0) + for (int c = 0; c < per; c++) + dot += jac.j[e][c0 + c] * v[per * k + c]; + for (int e = jac.start[r]; e < jac.start[r + 1]; e++) + if (const int k = param_of[jac.atom[e]]; k >= 0) + for (int c = 0; c < per; c++) + hv[per * k + c] += dot * jac.j[e][c0 + c]; + } + }; + auto product = [&](const std::vector &x, const std::vector &v, std::vector &hv) { + if (gn) + gn_hv(x, v, hv); + else + restraint_hv(x, v, hv); + for (size_t k = 0; k < n; k++) + hv[k] += h[k] * v[k]; + }; + // the restraints' diagonal: every COLOURS-th atom at a time, one direction at a time + constexpr int COLOURS = 24; + std::vector diagonal(n, 0.0), v(n), hv; + if (gn) { + target.Set(p.data()); + x_jac = p; + jac = RestraintGaussNewton(set, ctx.XyzB()); + for (size_t e = 0; e < jac.atom.size(); e++) + if (const int k = param_of[jac.atom[e]]; k >= 0) + for (int c = 0; c < per; c++) + diagonal[per * k + c] += jac.j[e][c0 + c] * jac.j[e][c0 + c]; + } + for (int c = 0; c < (gn ? 0 : COLOURS); c++) + for (int dir = 0; dir < per; dir++) { + std::fill(v.begin(), v.end(), 0.0); + for (size_t k = static_cast(per * c + dir); k < n; k += static_cast(per * COLOURS)) + v[k] = 1.0; + restraint_hv(p, v, hv); + for (size_t k = static_cast(per * c + dir); k < n; k += static_cast(per * COLOURS)) + diagonal[k] = hv[k]; + } + std::vector sorted; + for (size_t k = 0; k < n; k++) + sorted.push_back(h[k] + std::max(diagonal[k], 0.0)); + std::nth_element(sorted.begin(), sorted.begin() + n / 2, sorted.end()); + const double floor = std::max(1e-3 * sorted[n / 2], 1e-12); + for (size_t k = 0; k < n; k++) + diagonal[k] = std::max(h[k] + std::max(diagonal[k], 0.0), floor); + if (const char *e = getenv("RUGNUX_NEWTON_CGMAX")) + newton.cg_max = std::atoi(e); + newton.clip_step = getenv("RUGNUX_NEWTON_CLIP") != nullptr; + newton.trace = getenv("RUGNUX_NEWTON_TRACE") != nullptr; + if (const char *e = getenv("RUGNUX_NEWTON_RELSTOP")) + newton.relative_stop = std::atof(e); + if (const char *e = getenv("RUGNUX_NEWTON_CGTOL")) + newton.cg_tolerance = std::atof(e); + const int cg_before = newton.cg_iterations; + if (NewtonHybrid()) { + lbfgs.SetPreconditioner([&](const std::vector &x, std::vector &q) { + std::vector z; + RefinementNewton::Solve(product, diagonal, 0.0, x, q, z, newton.cg_max, newton.cg_tolerance, + newton.cg_iterations); + q.swap(z); + }); + const RefinementMinimiser::Outcome o = lbfgs.Minimise( + [&](const std::vector &x, double &f, std::vector &g) { return target.Evaluate(x.data(), &f, g.data()); }, + p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); + lbfgs.SetPreconditioner(nullptr); + target.Set(p.data()); + if (RefineStats()) fprintf(stderr, "MINSTAT hybrid.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", xyz ? "xyz" : "b", o.iterations, + o.evaluations, iterations, newton.cg_iterations - cg_before, o.f_start, o.f_end); + return; + } + const RefinementMinimiser::Outcome o = newton.Minimise( + [&](const std::vector &x, double &f, std::vector &g) { return target.Evaluate(x.data(), &f, g.data()); }, + product, diagonal, p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); }); + target.Set(p.data()); + if (RefineStats()) fprintf(stderr, "MINSTAT newton.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", xyz ? "xyz" : "b", o.iterations, + o.evaluations, iterations, newton.cg_iterations - cg_before, o.f_start, o.f_end); } // Groups of atoms that move as rigid bodies (one per chain) against the X-ray target: per body an angle-axis @@ -1144,6 +1350,18 @@ double BalancedWeight(Context &ctx, const std::vector &refinable) { std::vector> gx, gr; ctx.engine->Evaluate(ctx.Model(), &gx); std::vector> xyzb = ctx.XyzB(); + { // MEASUREMENT (lane k): the ratio of the gradients' norms at the model as given, unshaken + std::vector> g0; + ctx.engine->EvaluateRestraints(0, xyzb, &g0); + double a = 0, b = 0; + for (int i : refinable) + for (int c = 0; c < 3; c++) { + a += gx[i][c] * gx[i][c]; + b += g0[i][c] * g0[i][c]; + } + if (RefineStats()) fprintf(stderr, "WEIGHTSTAT unshaken_ratio=%.4g |gx|=%.4g |gr0|=%.4g\n", a > 0 ? std::sqrt(b / a) : 0.0, + std::sqrt(a), std::sqrt(b)); + } std::mt19937 rng(1); std::normal_distribution shake(0.0, 0.1); for (std::array &a : xyzb) @@ -1788,6 +2006,35 @@ double XrayGradientNorm(Context &ctx, const std::vector &refinable) { return n; } +// PROTOTYPE switches: RUGNUX_NEWTON=1 the positions and the B factors by MinimiseNewton(), with +// RUGNUX_NEWTON_XYZ / RUGNUX_NEWTON_B iterations per macro-cycle. +bool NewtonOn() { + static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) != "0"; + return on; +} +// RUGNUX_NEWTON=2: L-BFGS with the Newton H as its initial matrix instead of the damped Newton steps. +bool NewtonHybrid() { + static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) == "2"; + return on; +} +int NewtonIterations(bool xyz) { + const char *e = getenv(xyz ? "RUGNUX_NEWTON_XYZ" : "RUGNUX_NEWTON_B"); + return e != nullptr ? std::atoi(e) : (xyz ? 10 : 6); +} +// The X-ray curvature per atom in each of x, y, z at weight w, for the row model `rows` the engine has; `scale` +// calibrated at the first call of the trial (negative: not yet). +std::vector NewtonXyzCurvature(Context &ctx, const std::vector &refinable, const XrayRowModel &rows, + double w, double &scale) { + std::vector shape = ctx.XrayXyzCurvatureShape(rows); + if (scale < 0) { + scale = ctx.CalibrateXyzCurvature(refinable, shape); + if (RefineStats()) fprintf(stderr, "NEWTONCAL xyz_scale=%.4g\n", scale); + } + for (double &x : shape) + x *= w * scale; + return shape; +} + TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const std::vector &pos0, const std::vector &b0, const std::vector &aniso0, const Context::Fit *fit, const TrialWeights &tw, int cycles, const RefinementSettings &settings, const NcsCopies *ncs, @@ -1838,7 +2085,8 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s const AdpTarget tls_target(ctx, AdpTarget::Mode::Tls, refinable, 1.0, nullptr); const AdpTarget u_target(ctx, AdpTarget::Mode::U, u_atoms, adp_w, &u_restraints); RefinementMinimiser xyz_minimiser(20, ctx.nthreads), b_minimiser(20, ctx.nthreads), tls_minimiser(20, ctx.nthreads); - double xray_scale = 0; + RefinementNewton xyz_newton, b_newton; + double xray_scale = 0, xyz_scale = -1; for (int cycle = 0; cycle < cycles && !ctx.Cancelled(); cycle++) { PlaceRiders(ctx, topo); if (cycle == 0 && fit != nullptr) @@ -1852,10 +2100,13 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s if (adp_w > 0) ctx.MoveOverallB(); if (!hold_positions) { + RestraintSet xyz_set; { const RestraintSet set = RestraintsNow(ctx, topo, ncs, ncs_sigma); const RefinementStage s1("set_restraints"); ctx.engine->SetRestraints(0, set); + if (NewtonOn()) + xyz_set = set; } const RefinementStage s2("minimise.xyz"); const double b_h = cycle < static_cast(tw.blur.size()) ? tw.blur[cycle] : 0.0; @@ -1870,15 +2121,22 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s ctx.engine->SetRowModel(blurred); const double nh = XrayGradientNorm(ctx, refinable); const AtomTarget xyz_h(ctx, AtomTarget::Mode::Xyz, refinable, nh > 0 ? w * std::sqrt(n0 / nh) : w, 0, topo); - Minimise(ctx, xyz_h, settings.xyz_iterations, xyz_minimiser); + if (NewtonOn()) + MinimiseNewton(ctx, xyz_h, NewtonIterations(true), xyz_newton, + NewtonXyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale), xyz_minimiser, xyz_set); + else + Minimise(ctx, xyz_h, settings.xyz_iterations, xyz_minimiser, {}, "xyz_blur"); ctx.engine->SetRowModel(ctx.rows); - } else - Minimise(ctx, xyz, settings.xyz_iterations, xyz_minimiser); + } else if (NewtonOn()) + MinimiseNewton(ctx, xyz, NewtonIterations(true), xyz_newton, + NewtonXyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale), xyz_minimiser, xyz_set); + else + Minimise(ctx, xyz, settings.xyz_iterations, xyz_minimiser, {}, "xyz"); } if (kind == AdpHypothesis::Kind::Tls) { ctx.ApplyTls(); // the bodies' U at the atoms as they are const RefinementStage s4("minimise.tls"); - Minimise(ctx, tls_target, settings.b_iterations, tls_minimiser); + Minimise(ctx, tls_target, settings.b_iterations, tls_minimiser, {}, "tls"); } if (adp_w > 0 && kind == AdpHypothesis::Kind::Anisotropic) { // each U's six elements take a third of the curvature of its B @@ -1903,7 +2161,7 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s for (double h : BDiagonal(xray, restraint, u_atoms, adp_w)) diagonal.insert(diagonal.end(), 6, h); const RefinementStage s3("minimise.u"); - Minimise(ctx, u_target, settings.b_iterations, b_minimiser, diagonal); + Minimise(ctx, u_target, settings.b_iterations, b_minimiser, diagonal, "u"); } else if (adp_w > 0) { const RestraintSet b_set = BRestraintsNow(ctx, topo); ctx.engine->SetRestraints(1, b_set); @@ -1913,8 +2171,13 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s for (double &x : xray) x *= xray_scale; const RefinementStage s3("minimise.b"); - Minimise(ctx, b, settings.b_iterations, b_minimiser, - BDiagonal(xray, BRestraintCurvature(b_set, ctx.XyzB()), refinable, adp_w)); + if (NewtonOn()) { + for (double &x : xray) + x *= adp_w; + MinimiseNewton(ctx, b, NewtonIterations(false), b_newton, xray, b_minimiser, b_set); + } else + Minimise(ctx, b, settings.b_iterations, b_minimiser, + BDiagonal(xray, BRestraintCurvature(b_set, ctx.XyzB()), refinable, adp_w), "b"); } } PlaceRiders(ctx, topo); @@ -1959,6 +2222,8 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector &refinable, const s out.fit = ctx.Fitted(); out.ran = true; out.evaluations = ctx.evaluations - evaluations_before; + if (RefineStats()) fprintf(stderr, "TRIALSTAT kind=%d adp_w=%.3g w=%.3g cycles=%d ev=%d rfree=%.4f\n", static_cast(kind), adp_w, w, cycles, + out.evaluations, t.r_free); return out; } @@ -2024,6 +2289,11 @@ std::vector RunTrials(const std::vector &trial_slots, for (const std::exception_ptr &e : errors) if (e) std::rethrow_exception(e); + int ev = 0; + for (const TrialOutcome &o : out) + ev += o.evaluations; + if (RefineStats()) fprintf(stderr, "TRIALSSET t=%.3f n=%zu cycles=%d ev=%d\n", + std::chrono::duration(std::chrono::system_clock::now().time_since_epoch()).count(), out.size(), cycles, ev); return out; } diff --git a/image_analysis/structure_refinement/RefinementMinimiser.h b/image_analysis/structure_refinement/RefinementMinimiser.h index 1a19b2677..521a3a6fc 100644 --- a/image_analysis/structure_refinement/RefinementMinimiser.h +++ b/image_analysis/structure_refinement/RefinementMinimiser.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include #include @@ -34,6 +35,9 @@ public: } // The estimated diagonal of the Hessian; every element > 0. void SetDiagonal(std::vector h) { h_ = std::move(h); } + // PROTOTYPE: H0 as an operator instead of the diagonal, q = H0 q at x (null: the diagonal). + using Preconditioner = std::function &x, std::vector &q)>; + void SetPreconditioner(Preconditioner p) { precondition_ = std::move(p); } struct Outcome { int iterations = 0; @@ -59,11 +63,11 @@ public: for (int it = 0; it < iterations; it++) { if (stop && stop()) break; - Direction(g, d); + Direction(x, g, d); double gd = Dot(g, d); if (!(gd < 0)) { // not a descent direction: the memory no longer fits, start again from the diagonal Reset(); - Direction(g, d); + Direction(x, g, d); gd = Dot(g, d); if (!(gd < 0)) break; @@ -145,7 +149,11 @@ private: // additions, so each sweep over the parameters takes the next pair's dot product while it applies the // previous pair's update to q - the same operations on each element in the same order as a sweep for // each would do, with the update's memory traffic hidden under the chain. - void Direction(const std::vector &g, std::vector &d) const { + void Direction(const std::vector &x, const std::vector &g, std::vector &d) const { + if (precondition_) { + DirectionPreconditioned(x, g, d); + return; + } const size_t n = g.size(), m = s_.size(); std::vector &q = q_; // kept from call to call: made anew, its pages fault in every time q.assign(g.begin(), g.end()); @@ -202,6 +210,27 @@ private: d[k] = -q[k]; } + // The two-loop recursion plainly, with H0 the preconditioner (its own scale: no Oren-Shanno factor). + void DirectionPreconditioned(const std::vector &x, const std::vector &g, std::vector &d) const { + const size_t m = s_.size(); + std::vector q = g, a(m); + for (size_t i = m; i-- > 0;) { + a[i] = Dot(s_[i], q) / sy_[i]; + for (size_t k = 0; k < q.size(); k++) + q[k] -= a[i] * y_[i][k]; + } + precondition_(x, q); + for (size_t i = 0; i < m; i++) { + const double b = Dot(y_[i], q) / sy_[i]; + for (size_t k = 0; k < q.size(); k++) + q[k] += s_[i][k] * (a[i] - b); + } + d.resize(q.size()); + for (size_t k = 0; k < q.size(); k++) + d[k] = -q[k]; + } + + Preconditioner precondition_; size_t memory_, nthreads_; std::deque> s_, y_; std::deque sy_; // s.y of each pair, as taken when it was kept @@ -209,3 +238,144 @@ private: mutable std::vector q_; // Direction()'s working vector std::vector h_; }; + +// Damped Newton steps (Levenberg-Marquardt): each iteration solves (H + lambda D) d = -g by conjugate gradients +// preconditioned with (1 + lambda) D, where H v is the caller's product with an approximation of the Hessian at the +// point of the last accepted evaluation and D an estimate of its diagonal, and takes d where it lowers f. lambda falls +// after a step the quadratic model predicted well and rises after one it did not; a step that does not lower f is +// solved again with the larger lambda, without a new gradient. Every iteration is one evaluation of the target where +// its step is taken. +// Following Levenberg (1944) Q. Appl. Math. 2, 164-168 and Marquardt (1963) J. SIAM 11, 431-441 +class RefinementNewton { +public: + using Target = RefinementMinimiser::Target; + // hv = H v at x, the point of the last accepted evaluation. + using Product = std::function &x, const std::vector &v, std::vector &hv)>; + using Outcome = RefinementMinimiser::Outcome; + + int cg_iterations = 0; // summed over the calls, for the log + int cg_max = 50; + double cg_tolerance = 0.1; + bool clip_step = false; + bool trace = false; + double relative_stop = 0; // PROTOTYPE: stop where an iteration lowers f by less than this of all the calls' decrease so far // PROTOTYPE: each parameter's step clipped to max_step, not the whole step scaled + + Outcome Minimise(const Target &target, const Product &product, const std::vector &diagonal, + std::vector &x, int iterations, double f_tolerance, double max_step, + const std::function &stop = nullptr) { + const size_t n = x.size(); + Outcome out; + double f = 0; + std::vector g(n), d(n), x_new(n), g_new(n), hd(n), rhs(n); + if (!target(x, f, g)) + return out; + out.evaluations++; + out.f_start = out.f_end = f; + for (int it = 0; it < iterations; it++) { + if (stop && stop()) + break; + bool accepted = false; + double f_new = 0; + for (int attempt = 0; attempt < 6 && !accepted; attempt++) { + for (size_t k = 0; k < n; k++) + rhs[k] = -g[k]; + Solve(product, diagonal, lambda_, x, rhs, d, cg_max, cg_tolerance, cg_iterations); + double d_max = 0; + for (double v : d) + d_max = std::max(d_max, std::fabs(v)); + if (clip_step) + for (double &v : d) + v = std::clamp(v, -max_step, max_step); + else if (d_max > max_step) + for (double &v : d) + v *= max_step / d_max; + product(x, d, hd); + const double gd = Dot(g, d), predicted = -(gd + 0.5 * Dot(d, hd)); + if (!(gd < 0)) + break; + for (size_t k = 0; k < n; k++) + x_new[k] = x[k] + d[k]; + const bool ok = target(x_new, f_new, g_new); + out.evaluations++; + const double rho = ok ? (f - f_new) / predicted : -1.0; + if (trace) + fprintf(stderr, "NTRACE it=%d att=%d dmax=%.3g lambda=%.3g pred=%.4g act=%.4g rho=%.3f\n", it, attempt, + d_max, lambda_, predicted, f - f_new, rho); + if (ok && f_new <= f + 1e-4 * gd) { + accepted = true; + if (rho > 0.75) + lambda_ = std::max(lambda_ / 3.0, 1e-4); + else if (rho < 0.25) + lambda_ *= 2.0; + } else + lambda_ = std::max(4.0 * lambda_, 0.1); + } + if (!accepted) + break; + const double decrease = f - f_new; + x.swap(x_new); + g.swap(g_new); + f = f_new; + out.iterations++; + out.f_end = f; + if (decrease <= f_tolerance * std::max(std::fabs(f), 1.0)) + break; + if (relative_stop > 0 && decrease < relative_stop * (out.f_start - f)) + break; + } + return out; + } + + // d with (H + lambda D) d = rhs to `tolerance` of |rhs|, by conjugate gradients preconditioned with + // (1 + lambda) D; where a direction of negative curvature turns up, d as far as it got (the preconditioned rhs at + // first). + static void Solve(const Product &product, const std::vector &diagonal, double lambda_, + const std::vector &x, const std::vector &rhs, std::vector &d, int CG_MAX, + double tolerance, int &cg_iterations) { + const size_t n = rhs.size(); + d.assign(n, 0.0); + std::vector r(n), z(n), p(n), hp(n); + for (size_t k = 0; k < n; k++) { + r[k] = rhs[k]; + z[k] = r[k] / ((1 + lambda_) * diagonal[k]); + } + p = z; + double rz = Dot(r, z); + const double r0 = std::sqrt(Dot(r, r)); + for (int k = 0; k < CG_MAX; k++) { + cg_iterations++; + product(x, p, hp); + for (size_t j = 0; j < n; j++) + hp[j] += lambda_ * diagonal[j] * p[j]; + const double php = Dot(p, hp); + if (!(php > 0)) { + if (k == 0) + d = z; + break; + } + const double a = rz / php; + for (size_t j = 0; j < n; j++) { + d[j] += a * p[j]; + r[j] -= a * hp[j]; + } + if (std::sqrt(Dot(r, r)) < tolerance * r0) + break; + for (size_t j = 0; j < n; j++) + z[j] = r[j] / ((1 + lambda_) * diagonal[j]); + const double rz_new = Dot(r, z); + for (size_t j = 0; j < n; j++) + p[j] = z[j] + rz_new / rz * p[j]; + rz = rz_new; + } + } + +private: + static double Dot(const std::vector &a, const std::vector &b) { + double s = 0; + for (size_t k = 0; k < a.size(); k++) + s += a[k] * b[k]; + return s; + } + + double lambda_ = 0.01; // kept from call to call, as the L-BFGS memory is +}; diff --git a/image_analysis/structure_refinement/RefinementRestraints.cpp b/image_analysis/structure_refinement/RefinementRestraints.cpp index d35c89910..7d0813183 100644 --- a/image_analysis/structure_refinement/RefinementRestraints.cpp +++ b/image_analysis/structure_refinement/RefinementRestraints.cpp @@ -397,6 +397,48 @@ double RestraintValue(const RestraintSet &set, const RestraintRecord &r, const s return NAN; } +namespace { +// E of a restraint of the kinds over up to four atoms (not a plane, not a B similarity), its gradient in g[0..n). +double RecordTerm(const RestraintRecord &r, const std::vector> &xyzb, V3 g[4], int &n) { + const double ideal = r.ideal, sigma = r.sigma; + double e = 0; + n = 0; + switch (static_cast(r.kind)) { + case RestraintKind::Bond: + e = refine_math::BondTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), ideal, sigma, g); + n = 2; + break; + case RestraintKind::Angle: + e = refine_math::AngleTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), ideal, sigma, g); + n = 3; + break; + case RestraintKind::Torsion: + e = refine_math::TorsionTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), + At(xyzb, r.atom[3]), ideal, sigma, r.period, g); + n = 4; + break; + case RestraintKind::Chiral: + e = refine_math::ChiralTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), + At(xyzb, r.atom[3]), ideal, sigma, g); + n = 4; + break; + case RestraintKind::NcsDistance: + e = refine_math::NcsDistanceTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), + At(xyzb, r.atom[3]), sigma, ideal, g); + n = 4; + break; + case RestraintKind::Repulsion: + e = refine_math::RepulsionTerm(At(xyzb, r.atom[0]), Image(r, At(xyzb, r.atom[1])), ideal, sigma, g); + g[1] = FromImage(r, g[1]); + n = 2; + break; + default: + break; + } + return e; +} +} // namespace + double EvaluateRestraints(const RestraintSet &set, const std::vector> &xyzb, std::vector> *grad) { double sum = 0; @@ -405,36 +447,8 @@ double EvaluateRestraints(const RestraintSet &set, const std::vector g[4] = {}; int n = 0; double e = 0; - const double ideal = r.ideal, sigma = r.sigma; + const double sigma = r.sigma; switch (static_cast(r.kind)) { - case RestraintKind::Bond: - e = refine_math::BondTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), ideal, sigma, g); - n = 2; - break; - case RestraintKind::Angle: - e = refine_math::AngleTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), ideal, sigma, g); - n = 3; - break; - case RestraintKind::Torsion: - e = refine_math::TorsionTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), - At(xyzb, r.atom[3]), ideal, sigma, r.period, g); - n = 4; - break; - case RestraintKind::Chiral: - e = refine_math::ChiralTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), - At(xyzb, r.atom[3]), ideal, sigma, g); - n = 4; - break; - case RestraintKind::NcsDistance: - e = refine_math::NcsDistanceTerm(At(xyzb, r.atom[0]), At(xyzb, r.atom[1]), At(xyzb, r.atom[2]), - At(xyzb, r.atom[3]), sigma, ideal, g); - n = 4; - break; - case RestraintKind::Repulsion: - e = refine_math::RepulsionTerm(At(xyzb, r.atom[0]), Image(r, At(xyzb, r.atom[1])), ideal, sigma, g); - g[1] = FromImage(r, g[1]); - n = 2; - break; case RestraintKind::Plane: { const int m = r.atom[1]; px.resize(m); @@ -460,6 +474,8 @@ double EvaluateRestraints(const RestraintSet &set, const std::vector> &xyzb) { + RestraintJacobian jac; + jac.start.push_back(0); + auto row = [&](int atom, double x, double y, double z, double b) { + jac.atom.push_back(atom); + jac.j.push_back({x, y, z, b}); + }; + std::vector> px, pg; + for (const RestraintRecord &r : set.records) { + const RestraintKind kind = static_cast(r.kind); + if (kind == RestraintKind::Plane) { + // each atom's distance from the plane a residual of its own, d r_i / d x_j = (delta_ij - 1/n) normal / sigma + // (the normal held), so one row per atom of the plane over all of them + const int m = r.atom[1]; + px.resize(m); + pg.resize(m); + for (int k = 0; k < m; k++) + px[k] = At(xyzb, set.plane_atoms[r.atom[0] + k]); + refine_math::PlaneTerm(px.data(), m, static_cast(r.sigma), pg.data()); + const double c = std::sqrt(2.0) / r.sigma; + V3 nv{0, 0, 0}; + for (int k = 0; k < m; k++) // the normal from the largest of the gradients (each is 2 r_k / sigma n) + if (refine_math::Norm(pg[k]) > refine_math::Norm(nv)) + nv = pg[k]; + if (refine_math::Norm(nv) == 0) + continue; + nv = (1.0 / refine_math::Norm(nv)) * nv; + for (int i = 0; i < m; i++) { + for (int k = 0; k < m; k++) { + const double f = c * ((i == k ? 1.0 : 0.0) - 1.0 / m); + row(set.plane_atoms[r.atom[0] + k], f * nv.x, f * nv.y, f * nv.z, 0); + } + jac.start.push_back(static_cast(jac.atom.size())); + } + continue; + } + // E = residual^2 (or close to it): its gradient is 2 residual grad(residual), so J = g / sqrt(2 E) gives the + // Gauss-Newton term 2 grad(residual) grad(residual)^T = J J^T; nothing where E is 0 + if (kind == RestraintKind::BSimilarity) { + double gi = 0, gj = 0; + const double e = refine_math::BSimilarityTerm(xyzb[r.atom[0]][3], xyzb[r.atom[1]][3], + static_cast(r.sigma), gi, gj); + if (!(e > 0)) + continue; + const double f = 1.0 / std::sqrt(2.0 * e); + row(r.atom[0], 0, 0, 0, f * gi); + row(r.atom[1], 0, 0, 0, f * gj); + } else { + V3 g[4] = {}; + int n = 0; + const double e = RecordTerm(r, xyzb, g, n); + if (!(e > 0)) + continue; + const double f = 1.0 / std::sqrt(2.0 * e); + for (int k = 0; k < n; k++) + row(r.atom[k], f * g[k].x, f * g[k].y, f * g[k].z, 0); + } + jac.start.push_back(static_cast(jac.atom.size())); + } + return jac; +} + std::vector BRestraintCurvature(const RestraintSet &set, const std::vector> &xyzb) { std::vector h(xyzb.size(), 0.0); for (const RestraintRecord &r : set.records) diff --git a/image_analysis/structure_refinement/RefinementRestraints.h b/image_analysis/structure_refinement/RefinementRestraints.h index 0a887a7e7..23e8ff026 100644 --- a/image_analysis/structure_refinement/RefinementRestraints.h +++ b/image_analysis/structure_refinement/RefinementRestraints.h @@ -49,6 +49,15 @@ double RestraintValue(const RestraintSet &set, const RestraintRecord &r, const s double EvaluateRestraints(const RestraintSet &set, const std::vector> &xyzb, std::vector> *grad); +// PROTOTYPE: the Gauss-Newton approximation of the set's Hessian, sum over rows of J J^T: per row (a restraint's +// residual) its derivatives in (x, y, z, B) of the atoms atom[start[r] ... start[r + 1]). +struct RestraintJacobian { + std::vector start; + std::vector atom; + std::vector> j; +}; +RestraintJacobian RestraintGaussNewton(const RestraintSet &set, const std::vector> &xyzb); + // The diagonal of the Hessian of the set's B similarity restraints with respect to each atom's B, at `xyzb`. std::vector BRestraintCurvature(const RestraintSet &set, const std::vector> &xyzb);