Refinement Newton (prototype): X-ray coupling between neighbouring atoms, quasi-Newton on GN, per-part switches, step scale

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-16 10:59:04 +01:00
1 parent 4dc4a47d2f
commit 03f8ff2d29
2 files changed
+287 -24

No files matched your search

@@ -664,6 +664,144 @@ struct Context {
return shape;
}
// The X-ray normal matrix between the positions of neighbouring atoms of `params` (riders left out), in the units of
// XrayXyzCurvatureShape(): for atoms k and l a vector r apart, the sum over the shells of
// w f_k f_l exp(-(B_k + B_l) s^2 / 4) (2 pi s)^2 [(j1(x) / x) I - j2(x) r r^T / r^2], x = 2 pi s r - the overlap of
// their density derivatives, the reflections spread evenly over directions; at r = 0 the shape's own (2 pi)^2 s^2 / 3.
// Out to where it has fallen below a hundredth of its value at 0 for two carbons at the median B, and stays there.
// Per parameter index its neighbours and blocks (xx yy zz xy xz yz), both ways round.
struct XrayCoupling {
std::vector<int> start, other;
std::vector<std::array<double, 6>> block;
};
XrayCoupling XrayXyzCoupling(const XrayRowModel &rows, const std::vector<int> &params) const {
constexpr int SHELLS = 32;
double s2_max = 0;
for (double s2 : refl.inv_d2)
s2_max = std::max(s2_max, s2);
std::vector<double> w(SHELLS, 0), s2_sum(SHELLS, 0);
std::vector<int> n(SHELLS, 0);
for (size_t m = 0; m < refl.Size(); m++) {
if (refl.free[m])
continue;
const int k = std::min(SHELLS - 1, static_cast<int>(refl.inv_d2[m] / s2_max * SHELLS));
const double kd = rows.k_total[m] * rows.d[m];
w[k] += 2.0 * kd * kd / rows.var[m];
s2_sum[k] += refl.inv_d2[m];
n[k]++;
}
std::vector<double> s(SHELLS, 0);
for (int k = 0; k < SHELLS; k++)
s[k] = n[k] > 0 ? std::sqrt(s2_sum[k] / n[k]) : 0.0;
std::map<std::pair<int, signed char>, std::vector<double>> fs; // per element and charge, f per shell
auto form = [&](const gemmi::Atom &a) -> const std::vector<double> & {
std::vector<double> &f = fs[{static_cast<int>(a.element.elem), a.charge}];
if (f.empty()) {
const auto &coef = IonFormFactor<double>(a.element.elem, a.charge);
f.resize(SHELLS, 0);
for (int k = 0; k < SHELLS; k++)
f[k] = coef.calculate_sf(s[k] * s[k] / 4.0);
}
return f;
};
// a = j1(x) / x and b = j2(x) per shell at distance r, for f_k f_l exp(-(B_k + B_l) s^2 / 4) given per shell
auto kernel = [&](const std::vector<double> &ff, double r, double &a_sum, double &b_sum) {
a_sum = b_sum = 0;
for (int k = 0; k < SHELLS; k++) {
if (n[k] == 0)
continue;
const double x = 2 * M_PI * s[k] * r, c = w[k] * ff[k] * 4 * M_PI * M_PI * s[k] * s[k];
double a, b;
if (x < 1e-3) {
a = 1.0 / 3.0 - x * x / 30.0;
b = x * x / 15.0;
} else {
const double sx = std::sin(x), cx = std::cos(x);
a = (sx / (x * x) - cx / x) / x;
b = (3.0 / (x * x) - 1.0) * sx / x - 3.0 * cx / (x * x);
}
a_sum += c * a;
b_sum += c * b;
}
};
// the cutoff, for two carbons at the median B
std::vector<double> bs;
for (int i : params)
bs.push_back(atoms[i]->b_iso);
XrayCoupling out;
out.start.assign(params.size() + 1, 0);
if (bs.empty())
return out;
std::nth_element(bs.begin(), bs.begin() + bs.size() / 2, bs.end());
const double b_med = bs[bs.size() / 2];
gemmi::Atom carbon;
carbon.element = gemmi::El::C;
std::vector<double> ff(SHELLS);
const std::vector<double> &fc = form(carbon);
for (int k = 0; k < SHELLS; k++)
ff[k] = fc[k] * fc[k] * std::exp(-b_med * s[k] * s[k] / 2.0);
double a0, b0;
kernel(ff, 0.0, a0, b0);
double cutoff = 1.0;
for (double r = 1.0; r <= 8.0; r += 0.1) {
double a, b;
kernel(ff, r, a, b);
if (std::max(std::fabs(a), std::fabs(a - b)) > 0.01 * a0)
cutoff = r + 0.1;
}
// the neighbours by a grid of cells one cutoff wide
std::map<std::array<int, 3>, std::vector<int>> cells;
auto cell_of = [&](const gemmi::Position &p) {
return std::array<int, 3>{static_cast<int>(std::floor(p.x / cutoff)), static_cast<int>(std::floor(p.y / cutoff)),
static_cast<int>(std::floor(p.z / cutoff))};
};
auto is_param = [&](int i) { return rider_parent.empty() || rider_parent[i] < 0; };
for (size_t k = 0; k < params.size(); k++)
if (is_param(params[k]))
cells[cell_of(atoms[params[k]]->pos)].push_back(static_cast<int>(k));
std::vector<std::vector<std::pair<int, std::array<double, 6>>>> rows_of(params.size());
for (size_t k = 0; k < params.size(); k++) {
const gemmi::Atom &ak = *atoms[params[k]];
if (!is_param(params[k]))
continue;
const std::array<int, 3> c = cell_of(ak.pos);
const std::vector<double> &fk = form(ak);
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
for (int dz = -1; dz <= 1; dz++) {
const auto it = cells.find({c[0] + dx, c[1] + dy, c[2] + dz});
if (it == cells.end())
continue;
for (int l : it->second) {
if (l == static_cast<int>(k))
continue;
const gemmi::Atom &al = *atoms[params[l]];
const gemmi::Position d = al.pos - ak.pos;
const double r = d.length();
if (r >= cutoff || r <= 0)
continue;
const std::vector<double> &fl = form(al);
for (int q = 0; q < SHELLS; q++)
ff[q] = fk[q] * fl[q] * ak.occ * al.occ * std::exp(-(ak.b_iso + al.b_iso) * s[q] * s[q] / 4.0);
double a, b;
kernel(ff, r, a, b);
const double u[3] = {d.x / r, d.y / r, d.z / r};
rows_of[k].push_back({l, {a - b * u[0] * u[0], a - b * u[1] * u[1], a - b * u[2] * u[2],
-b * u[0] * u[1], -b * u[0] * u[2], -b * u[1] * u[2]}});
}
}
std::sort(rows_of[k].begin(), rows_of[k].end(), [](const auto &x, const auto &y) { return x.first < y.first; });
}
for (size_t k = 0; k < params.size(); k++) {
out.start[k + 1] = out.start[k] + static_cast<int>(rows_of[k].size());
for (const auto &[l, blk] : rows_of[k]) {
out.other.push_back(l);
out.block.push_back(blk);
}
}
return out;
}
// The factor that turns XrayBCurvatureShape() into the X-ray target's curvature: its second derivative along a
// random +-1 direction of the B factors of `params`, by central differences of the gradient, over what the shape
// predicts for it.
@@ -1107,6 +1245,7 @@ void Minimise(Context &ctx, const Target &target, int iterations, RefinementMini
struct NewtonSettings {
int xyz_iterations = 30, b_iterations = 20; // the most per macro-cycle, as the L-BFGS's
double relative_stop = 0.0;
double step_scale = 1.0; // the step bound, times the L-BFGS's
};
const NewtonSettings &Newton() {
static const NewtonSettings s = [] {
@@ -1117,11 +1256,14 @@ const NewtonSettings &Newton() {
n.b_iterations = std::atoi(e);
if (const char *e = getenv("RUGNUX_NEWTON_RELSTOP"))
n.relative_stop = std::atof(e);
if (const char *e = getenv("RUGNUX_NEWTON_STEPSCALE"))
n.step_scale = std::atof(e);
return n;
}();
return s;
}
bool NewtonQuasi();
// `target` by `newton` from where it is, with H v = product and D = diagonal (each element > 0).
template <typename Target>
void MinimiseNewton(Context &ctx, const Target &target, RefinementNewton &newton, const RefinementNewton::Product &product,
@@ -1130,6 +1272,27 @@ void MinimiseNewton(Context &ctx, const Target &target, RefinementNewton &newton
std::vector<double> p = target.Get();
if (p.empty())
return;
if (NewtonQuasi() && newton.lbfgs != nullptr) {
// RUGNUX_NEWTON=2: the L-BFGS, its initial inverse Hessian the Gauss-Newton matrix solved by CG
RefinementMinimiser &m = *newton.lbfgs;
if (const char *e = getenv("RUGNUX_NEWTON_CGTOL"))
newton.cg_tolerance = std::atof(e);
const int cg_before = newton.cg_iterations;
m.SetPreconditioner([&](const std::vector<double> &x, std::vector<double> &q) {
std::vector<double> z;
newton.SolveUndamped(product, diagonal, precondition, x, q, z);
q.swap(z);
});
const RefinementMinimiser::Outcome o = m.Minimise(
[&](const std::vector<double> &x, double &f, std::vector<double> &g) { return target.Evaluate(x.data(), &f, g.data()); },
p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); });
m.SetPreconditioner(nullptr);
target.Set(p.data());
if (RefineStats())
fprintf(stderr, "MINSTAT qn.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations,
iterations, newton.cg_iterations - cg_before, o.f_start, o.f_end);
return;
}
newton.relative_stop = Newton().relative_stop;
newton.trace = getenv("RUGNUX_NEWTON_TRACE") != nullptr;
if (const char *e = getenv("RUGNUX_NEWTON_CGMAX"))
@@ -1139,8 +1302,8 @@ void MinimiseNewton(Context &ctx, const Target &target, RefinementNewton &newton
const int cg_before = newton.cg_iterations;
const RefinementMinimiser::Outcome o = newton.Minimise(
[&](const std::vector<double> &x, double &f, std::vector<double> &g) { return target.Evaluate(x.data(), &f, g.data()); },
product, diagonal, p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep(), [&]() { return ctx.Cancelled(); },
precondition);
product, diagonal, p, iterations, MINIMISER_F_TOLERANCE, target.MaxStep() * Newton().step_scale,
[&]() { return ctx.Cancelled(); }, precondition);
target.Set(p.data());
if (RefineStats())
fprintf(stderr, "MINSTAT newton.%s it=%d ev=%d lim=%d cg=%d f0=%.6g f1=%.6g\n", tag, o.iterations, o.evaluations,
@@ -1256,6 +1419,27 @@ private:
std::vector<double> t_;
};
// hv[offset + 3k ..] += scale sum over the neighbours l of k of the X-ray block (k, l) times v[offset + 3l ..].
void AddXrayCoupling(const Context::XrayCoupling &c, double scale, const std::vector<double> &v, size_t offset,
std::vector<double> &hv, size_t nthreads) {
const int n = static_cast<int>(c.start.size()) - 1;
ParallelChunks(n, ThreadsForWork(n, nthreads, 4096), [&](int lo, int hi) {
for (int k = lo; k < hi; k++) {
double s0 = 0, s1 = 0, s2 = 0;
for (int q = c.start[k]; q < c.start[k + 1]; q++) {
const std::array<double, 6> &m = c.block[q];
const double *x = &v[offset + 3 * c.other[q]];
s0 += m[0] * x[0] + m[3] * x[1] + m[4] * x[2];
s1 += m[3] * x[0] + m[1] * x[1] + m[5] * x[2];
s2 += m[4] * x[0] + m[5] * x[1] + m[2] * x[2];
}
hv[offset + 3 * k] += scale * s0;
hv[offset + 3 * k + 1] += scale * s1;
hv[offset + 3 * k + 2] += scale * s2;
}
});
}
// z = M^-1 r for M symmetric positive definite (xx yy zz xy xz yz), by its adjugate.
std::array<double, 3> Solve3(const std::array<double, 6> &m, double r0, double r1, double r2) {
const double a = m[0], b = m[1], c = m[2], d = m[3], e = m[4], f = m[5];
@@ -1287,7 +1471,8 @@ void PreconditionBlocks(const std::vector<std::array<double, 6>> &blocks, double
// The positions or the B factors: H = diag(xray) (per atom, already times the weight) + the restraints' J^T J of `set`.
void MinimiseAtomsNewton(Context &ctx, const AtomTarget &target, RefinementNewton &newton, const std::vector<double> &xray,
const RestraintSet &set, int iterations) {
const RestraintSet &set, int iterations, const Context::XrayCoupling *coupling = nullptr,
double coupling_scale = 0) {
const bool xyz = target.GetMode() == AtomTarget::Mode::Xyz;
const int per = xyz ? 3 : 1;
std::vector<double> p = target.Get();
@@ -1311,6 +1496,8 @@ void MinimiseAtomsNewton(Context &ctx, const AtomTarget &target, RefinementNewto
normal.Multiply(v, hv);
for (size_t k = 0; k < n; k++)
hv[k] += h[k] * v[k];
if (coupling != nullptr)
AddXrayCoupling(*coupling, coupling_scale, v, 0, hv, ctx.nthreads);
};
diagonal = FlooredDiagonal(diagonal);
if (!xyz) {
@@ -1329,7 +1516,8 @@ void MinimiseAtomsNewton(Context &ctx, const AtomTarget &target, RefinementNewto
// (`xray_xyz`, `xray_b`: per atom, the X-ray curvature alone), no coupling between them.
void MinimiseJointNewton(Context &ctx, const XyzBTarget &target, const std::vector<int> &atoms, RefinementNewton &newton,
const std::vector<double> &xray_xyz, const std::vector<double> &xray_b, const RestraintSet &set_xyz,
const RestraintSet &set_b, double w, double adp_w, int iterations) {
const RestraintSet &set_b, double w, double adp_w, int iterations,
const Context::XrayCoupling *coupling = nullptr, double coupling_scale = 0) {
const size_t n = atoms.size();
const double u2 = XyzBTarget::B_UNIT * XyzBTarget::B_UNIT;
RestraintNormalMatrix normal_xyz(atoms, ctx.atoms.size(), true, ctx.nthreads),
@@ -1367,6 +1555,8 @@ void MinimiseJointNewton(Context &ctx, const XyzBTarget &target, const std::vect
hv[3 * k + c] = hx[3 * k + c] / w + xray_xyz[atoms[k]] * v[3 * k + c];
hv[3 * n + k] = u2 * (hb[k] / adp_w + xray_b[atoms[k]] * v[3 * n + k]);
}
if (coupling != nullptr)
AddXrayCoupling(*coupling, coupling_scale, v, 0, hv, ctx.nthreads);
};
diagonal = FlooredDiagonal(diagonal);
// the positions' 3 x 3 blocks of H at the start, fixed for the call; the B factors' diagonal
@@ -2287,6 +2477,21 @@ bool NewtonOn() {
static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) != "0";
return on;
}
// RUGNUX_NEWTON_PARTS (default "xyz,b,tls,u"): which of the minimisations take the Newton steps.
bool NewtonOn(const char *part) {
static const std::string parts = getenv("RUGNUX_NEWTON_PARTS") != nullptr ? getenv("RUGNUX_NEWTON_PARTS") : "xyz,b,tls,u";
return NewtonOn() && parts.find(part) != std::string::npos;
}
// RUGNUX_NEWTON=2: the L-BFGS on top of the Gauss-Newton matrix (MinimiseNewton).
bool NewtonQuasi() {
static const bool on = getenv("RUGNUX_NEWTON") != nullptr && std::string(getenv("RUGNUX_NEWTON")) == "2";
return on;
}
// RUGNUX_NEWTON_COUPLING=1: the X-ray normal matrix between neighbouring atoms' positions in H (XrayXyzCoupling).
bool NewtonCoupling() {
static const bool on = getenv("RUGNUX_NEWTON_COUPLING") != nullptr;
return on;
}
// RUGNUX_NEWTON_JOINT=1: where the B factors are refined isotropically, they and the positions at once (XyzBTarget).
bool NewtonJoint() {
static const bool on = getenv("RUGNUX_NEWTON_JOINT") != nullptr;
@@ -2356,7 +2561,11 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &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);
RefinementNewton xyz_newton, b_newton, tls_newton;
RefinementNewton xyz_newton, b_newton, tls_newton, u_newton;
xyz_newton.lbfgs = &xyz_minimiser;
b_newton.lbfgs = &b_minimiser;
tls_newton.lbfgs = &tls_minimiser;
u_newton.lbfgs = &b_minimiser;
double xray_scale = 0, xyz_scale = -1, tls_scale = -1;
for (int cycle = 0; cycle < cycles && !ctx.Cancelled(); cycle++) {
PlaceRiders(ctx, topo);
@@ -2393,10 +2602,13 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &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);
if (NewtonOn())
MinimiseAtomsNewton(ctx, xyz_h, xyz_newton,
NewtonXyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale), xyz_set,
Newton().xyz_iterations);
if (NewtonOn("xyz")) {
const std::vector<double> h = NewtonXyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale);
const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(blurred, refinable)
: Context::XrayCoupling();
MinimiseAtomsNewton(ctx, xyz_h, xyz_newton, h, xyz_set, Newton().xyz_iterations,
NewtonCoupling() ? &c : nullptr, xyz_h.Weight() * xyz_scale);
}
else
Minimise(ctx, xyz_h, settings.xyz_iterations, xyz_minimiser, {}, "xyz_blur");
ctx.engine->SetRowModel(ctx.rows);
@@ -2409,20 +2621,26 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &refinable, const s
for (double &x : xray_b)
x *= xray_scale;
const XyzBTarget xyz_b(ctx, refinable, w, adp_w, topo);
MinimiseJointNewton(ctx, xyz_b, refinable, xyz_newton,
NewtonXyzCurvature(ctx, refinable, ctx.rows, 1.0, xyz_scale), xray_b, xyz_set, b_set, w,
adp_w, Newton().xyz_iterations);
const std::vector<double> h = NewtonXyzCurvature(ctx, refinable, ctx.rows, 1.0, xyz_scale);
const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(ctx.rows, refinable)
: Context::XrayCoupling();
MinimiseJointNewton(ctx, xyz_b, refinable, xyz_newton, h, xray_b, xyz_set, b_set, w, adp_w,
Newton().xyz_iterations, NewtonCoupling() ? &c : nullptr, xyz_scale);
joint_done = true;
} else if (NewtonOn())
MinimiseAtomsNewton(ctx, xyz, xyz_newton, NewtonXyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale),
xyz_set, Newton().xyz_iterations);
} else if (NewtonOn("xyz")) {
const std::vector<double> h = NewtonXyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale);
const Context::XrayCoupling c = NewtonCoupling() ? ctx.XrayXyzCoupling(ctx.rows, refinable)
: Context::XrayCoupling();
MinimiseAtomsNewton(ctx, xyz, xyz_newton, h, xyz_set, Newton().xyz_iterations,
NewtonCoupling() ? &c : nullptr, w * xyz_scale);
}
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");
if (NewtonOn()) {
if (NewtonOn("tls")) {
std::vector<double> xray = ctx.XrayBCurvatureShape();
if (tls_scale < 0)
tls_scale = ctx.CalibrateBCurvature(refinable, xray);
@@ -2455,11 +2673,11 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &refinable, const s
for (double h : BDiagonal(xray, restraint, u_atoms, adp_w))
diagonal.insert(diagonal.end(), 6, h);
const RefinementStage s3("minimise.u");
if (NewtonOn()) {
if (NewtonOn("u")) {
std::vector<double> h;
for (int i : u_atoms)
h.insert(h.end(), 6, adp_w * xray[i]);
MinimiseUNewton(ctx, u_target, b_newton, h, diagonal, Newton().b_iterations);
MinimiseUNewton(ctx, u_target, u_newton, h, diagonal, Newton().b_iterations);
} else
Minimise(ctx, u_target, settings.b_iterations, b_minimiser, diagonal, "u");
} else if (adp_w > 0 && !joint_done) {
@@ -2471,7 +2689,7 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &refinable, const s
for (double &x : xray)
x *= xray_scale;
const RefinementStage s3("minimise.b");
if (NewtonOn()) {
if (NewtonOn("b")) {
for (double &x : xray)
x *= adp_w;
MinimiseAtomsNewton(ctx, b, b_newton, xray, b_set, Newton().b_iterations);
@@ -4798,11 +5016,14 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
}
// PROTOTYPE (lane k): RUGNUX_REFINE_PROTOCOL=<population>,<rounds>[,<macro-cycles>] on the CPU and the GPU alike
if (const char *e = getenv("RUGNUX_REFINE_PROTOCOL")) {
int v[3] = {settings.population, settings.rounds, settings.macro_cycles};
sscanf(e, "%d,%d,%d", &v[0], &v[1], &v[2]);
int v[5] = {settings.population, settings.rounds, settings.macro_cycles, settings.xyz_iterations,
settings.b_iterations};
sscanf(e, "%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4]);
settings.population = v[0];
settings.rounds = v[1];
settings.macro_cycles = v[2];
settings.xyz_iterations = v[3];
settings.b_iterations = v[4];
logger.Info("Refinement EXPERIMENT protocol: population {}, {} rounds, {} macro-cycles", settings.population,
settings.rounds, settings.macro_cycles);
}
@@ -35,6 +35,10 @@ public:
}
// The estimated diagonal of the Hessian; every element > 0.
void SetDiagonal(std::vector<double> h) { h_ = std::move(h); }
// The initial inverse Hessian as an operator instead of the diagonal: q = H0 q at x (null: the diagonal). It is
// taken as it is, without the Oren-Shanno factor.
using Preconditioner = std::function<void(const std::vector<double> &x, std::vector<double> &q)>;
void SetPreconditioner(Preconditioner p) { precondition_ = std::move(p); }
struct Outcome {
int iterations = 0;
@@ -60,11 +64,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;
@@ -146,7 +150,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<double> &g, std::vector<double> &d) const {
void Direction(const std::vector<double> &x, const std::vector<double> &g, std::vector<double> &d) const {
if (precondition_) {
DirectionPreconditioned(x, g, d);
return;
}
const size_t n = g.size(), m = s_.size();
std::vector<double> &q = q_; // kept from call to call: made anew, its pages fault in every time
q.assign(g.begin(), g.end());
@@ -203,6 +211,27 @@ private:
d[k] = -q[k];
}
// The two-loop recursion with H0 the preconditioner.
void DirectionPreconditioned(const std::vector<double> &x, const std::vector<double> &g, std::vector<double> &d) const {
const size_t m = s_.size(), n = g.size();
std::vector<double> 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 < n; 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 < n; k++)
q[k] += s_[i][k] * (a[i] - b);
}
d.resize(n);
for (size_t k = 0; k < n; k++)
d[k] = -q[k];
}
Preconditioner precondition_;
size_t memory_, nthreads_;
std::deque<std::vector<double>> s_, y_;
std::deque<double> sy_; // s.y of each pair, as taken when it was kept
@@ -226,6 +255,7 @@ public:
using Outcome = RefinementMinimiser::Outcome;
int cg_iterations = 0; // summed over the calls, for the log
RefinementMinimiser *lbfgs = nullptr; // PROTOTYPE (RUGNUX_NEWTON=2): the L-BFGS the GN matrix preconditions
int cg_max = 50;
double cg_tolerance = 0.1;
double relative_stop = 0.0;
@@ -293,6 +323,18 @@ public:
return out;
}
// z = H^-1 q (lambda 0), by the same conjugate gradients: H0 for the L-BFGS (RefinementMinimiser::SetPreconditioner).
void SolveUndamped(const Product &product, const std::vector<double> &diagonal, const Preconditioner &precondition,
const std::vector<double> &x, const std::vector<double> &q, std::vector<double> &z) {
const double l = lambda_;
lambda_ = 0;
std::vector<double> minus(q.size());
for (size_t k = 0; k < q.size(); k++)
minus[k] = -q[k];
Solve(product, diagonal, precondition, x, minus, z);
lambda_ = l;
}
private:
static double Dot(const std::vector<double> &a, const std::vector<double> &b) {
double s = 0;