Refinement on the CPU: B-only evaluations leave the position derivatives out

The B factors' minimiser reads only the B derivatives; the gradient pass skips the x, y, z sums for
those evaluations. The B derivatives are summed exactly as before.

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-17 11:24:14 +01:00
1 parent 8ef877dc14
commit 74b6b568d2
3 files changed
+34 -16

No files matched your search

@@ -803,8 +803,10 @@ public:
double xray = 0, restraints = 0;
{
const RefinementStage s1("eval.engine");
ctx_.engine->SetXyzGradient(mode_ == Mode::Xyz); // B alone reads only the B derivatives
xray = ctx_.engine->EvaluateWithRestraints(ctx_.Model(), restraint_set_, gradient != nullptr ? &gx : nullptr,
gradient != nullptr ? &gr : nullptr, restraints);
ctx_.engine->SetXyzGradient(true);
}
*cost = weight_ * xray + restraints;
if (gradient == nullptr)
@@ -143,6 +143,7 @@ public:
void SetRowModel(const XrayRowModel &rows) override { rows_ = rows; }
bool OnGPU() const override { return false; }
void SetThreads(size_t nthreads) override { nthreads_ = nthreads; }
void SetXyzGradient(bool xyz) override { xyz_gradient_ = xyz; }
void Fcalc(const gemmi::Model &model, std::vector<std::complex<float>> &f) override {
std::vector<std::complex<double>> fd;
@@ -237,7 +238,7 @@ public:
map_.data[i] *= norm;
});
const bool aniso = AnyAniso();
AtomGradientsFromMap(map_, model, blur_, nthreads_, *grad, aniso ? &grad_u_ : nullptr, true);
AtomGradientsFromMap(map_, model, blur_, nthreads_, *grad, aniso ? &grad_u_ : nullptr, true, xyz_gradient_);
if (!aniso)
grad_u_.clear();
return sum;
@@ -265,6 +266,7 @@ private:
double d_min_;
const RefinementReflections &refl_;
size_t nthreads_;
bool xyz_gradient_ = true; // SetXyzGradient()
gemmi::Grid<float> grid_;
SymmetryComposition composition_;
std::vector<size_t> orbit_leaders_;
@@ -1016,7 +1018,7 @@ bool AtomGradientsFromMapMetal(const gemmi::Grid<float> &map, const gemmi::Densi
void AtomGradientsFromMap(const gemmi::Grid<float> &map, const gemmi::Model &model, double blur, size_t nthreads,
std::vector<std::array<double, 4>> &grad, std::vector<std::array<double, 6>> *grad_u,
[[maybe_unused]] bool gpu) {
[[maybe_unused]] bool gpu, bool xyz) {
const gemmi::DensityCalculator<Table, float> dc = Calculator(map.unit_cell, *map.spacegroup, 1.0, blur);
std::vector<const gemmi::Atom *> atoms;
for (const gemmi::Chain &ch : model.chains)
@@ -1071,20 +1073,30 @@ void AtomGradientsFromMap(const gemmi::Grid<float> &map, const gemmi::Model &mod
const float a = precal.a[q], bq = precal.b[q];
for (size_t k = 0; k < np; k++)
p.exponent[k] = std::max(bq * p.r2[k], -88.f);
for (size_t k = 0; k < np; k++) {
const double e = a * gemmi::unsafe_expapprox(p.exponent[k]);
sum_dr2[k] += e * bq;
sum_db[k] += e * bq * (1.5 + bq * p.r2[k]);
}
if (xyz)
for (size_t k = 0; k < np; k++) {
const double e = a * gemmi::unsafe_expapprox(p.exponent[k]);
sum_dr2[k] += e * bq;
sum_db[k] += e * bq * (1.5 + bq * p.r2[k]);
}
else
for (size_t k = 0; k < np; k++) {
const double e = a * gemmi::unsafe_expapprox(p.exponent[k]);
sum_db[k] += e * bq * (1.5 + bq * p.r2[k]);
}
}
double gx = 0, gy = 0, gz = 0, gb = 0;
for (size_t k = 0; k < np; k++) {
const double v = p.value[k];
gx += v * sum_dr2[k] * p.x[k];
gy += v * sum_dr2[k] * p.y[k];
gz += v * sum_dr2[k] * p.z[k];
gb += v * sum_db[k];
}
if (xyz)
for (size_t k = 0; k < np; k++) {
const double v = p.value[k];
gx += v * sum_dr2[k] * p.x[k];
gy += v * sum_dr2[k] * p.y[k];
gz += v * sum_dr2[k] * p.z[k];
gb += v * sum_db[k];
}
else
for (size_t k = 0; k < np; k++)
gb += p.value[k] * sum_db[k];
// rho = occ sum_q a_q exp(b_q r^2): d rho / d(centre) = occ 2 (sum a_q b_q e_q) (centre - point), and
// with b_q = -4 pi^2 / (B_q + B + blur), d rho / dB = occ sum a_q e_q b_q (1.5 + b_q r^2) / (4 pi^2).
grad[i] = {2.0 * atom.occ * gx, 2.0 * atom.occ * gy, 2.0 * atom.occ * gz, atom.occ * gb / FOUR_PI2};
@@ -208,6 +208,9 @@ public:
virtual bool OnGPU() const = 0;
// The threads the engine's evaluations are spread over from now on (the CPU's; a GPU engine keeps its own).
virtual void SetThreads(size_t) {}
// false: the evaluations from now on may leave the x, y, z derivatives out (where only B is refined); the B
// derivatives are the same. The CPU engine's; a GPU engine computes them all.
virtual void SetXyzGradient(bool) {}
// Which atoms are anisotropic and their shapes, in model order (AtomAniso), read at every Fcalc() and
// Evaluate() until it is set again; null or empty: every atom isotropic (the model's own aniso is not read).
@@ -290,7 +293,8 @@ std::unique_ptr<RefinementEngine> MakeRefinementEngineCPU(const gemmi::UnitCell
// PutModelDensityOnGrid() grids it: its B derivative is that along U + dB / (8 pi^2) I, and with `grad_u` (resized
// to the atoms, zero for the isotropic ones) its derivatives in U11 U22 U33 U12 U13 U23. With `gpu`, on the Apple GPU
// where there is one (AtomGradientMetal.h, macOS): the same points summed in single precision, the CPU's gradient to
// a few parts in 1e5 rather than bit for bit.
// a few parts in 1e5 rather than bit for bit. `xyz` false: an isotropic atom's x, y, z derivatives left at zero, its B
// derivative the same.
void AtomGradientsFromMap(const gemmi::Grid<float> &map, const gemmi::Model &model, double blur, size_t nthreads,
std::vector<std::array<double, 4>> &grad,
std::vector<std::array<double, 6>> *grad_u = nullptr, bool gpu = false);
std::vector<std::array<double, 6>> *grad_u = nullptr, bool gpu = false, bool xyz = true);