diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index b7ae83199..04734031d 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -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) diff --git a/image_analysis/structure_refinement/RefinementXray.cpp b/image_analysis/structure_refinement/RefinementXray.cpp index 956d7c9ae..8bf4daa4d 100644 --- a/image_analysis/structure_refinement/RefinementXray.cpp +++ b/image_analysis/structure_refinement/RefinementXray.cpp @@ -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> &f) override { std::vector> 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 grid_; SymmetryComposition composition_; std::vector orbit_leaders_; @@ -1016,7 +1018,7 @@ bool AtomGradientsFromMapMetal(const gemmi::Grid &map, const gemmi::Densi void AtomGradientsFromMap(const gemmi::Grid &map, const gemmi::Model &model, double blur, size_t nthreads, std::vector> &grad, std::vector> *grad_u, - [[maybe_unused]] bool gpu) { + [[maybe_unused]] bool gpu, bool xyz) { const gemmi::DensityCalculator dc = Calculator(map.unit_cell, *map.spacegroup, 1.0, blur); std::vector atoms; for (const gemmi::Chain &ch : model.chains) @@ -1071,20 +1073,30 @@ void AtomGradientsFromMap(const gemmi::Grid &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}; diff --git a/image_analysis/structure_refinement/RefinementXray.h b/image_analysis/structure_refinement/RefinementXray.h index 3294fcc19..991dc7c8f 100644 --- a/image_analysis/structure_refinement/RefinementXray.h +++ b/image_analysis/structure_refinement/RefinementXray.h @@ -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 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 &map, const gemmi::Model &model, double blur, size_t nthreads, std::vector> &grad, - std::vector> *grad_u = nullptr, bool gpu = false); + std::vector> *grad_u = nullptr, bool gpu = false, bool xyz = true);