Merge branch 'rc188-r' into rc188-int
Build Packages / Create release (push) Successful in 8s
Build Packages / Build documentation (push) Successful in 46s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m13s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m27s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m33s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 21m41s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 25m33s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 27m36s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 30m19s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 30m48s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 31m55s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 31m17s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 26m29s
Build Packages / Generate python client (push) Successful in 56s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 28m57s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 30m44s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 28m57s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 24m31s
Build Packages / Unit tests (push) Successful in 56m18s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 26m56s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 30m52s
Build Packages / Validation Battery (push) Successful in 32m22s

This commit is contained in:
leonarski_f committed 2026-11-17 19:39:14 +01:00
commit fdeb43af65
4 files changed
+152 -41

No files matched your search

@@ -6,6 +6,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <functional>
#include <set>
#include <future>
#include <limits>
@@ -16,6 +17,7 @@
#include <numeric>
#include <random>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <Eigen/Dense>
@@ -808,8 +810,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)
@@ -1905,6 +1909,11 @@ constexpr size_t TWIN_NULL_SHIFT = 2;
// evidence (AdoptTwinLaw).
constexpr double TWIN_NULL_Z = 3.0;
constexpr double TWIN_NULL_Z_DATA = 2.0;
// A twinned refinement stops after its first ladder where the law is ahead of its null by less than this many
// standard errors there (RefineOnce, 1a.): 1.5 under the lowest bar a law is taken at (TWIN_NULL_Z_DATA). From the
// ladder's end to the refinement's, the law's lead rose by 1.35 at most (on 19 refinements with a law, 3 twinned: those
// 8.6-17.7 at the ladder's end, the others under 2.4).
constexpr double TWIN_STOP_Z = 0.5;
// `first`: the first law whose mates are replaced - the laws before it keep theirs (the null of the domains added to
// them). With several laws a twin group holds up to Laws() + 1 reflections, kept next to each other, and law first + k
// is paired (Laws() + 1) (k + 1) apart: past the reflection's own group, and no two laws drawing the same partner.
@@ -2249,18 +2258,51 @@ double XrayGradientNorm(Context &ctx, const std::vector<int> &refinable) {
}
// 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<double> XyzCurvature(Context &ctx, const std::vector<int> &refinable, const XrayRowModel &rows, double w,
double &scale) {
// calibrated by `calibrate` (from the shape) at the first call of the trial (negative: not yet).
std::vector<double> XyzCurvature(Context &ctx, const XrayRowModel &rows, double w, double &scale,
const std::function<double(const std::vector<double> &)> &calibrate) {
std::vector<double> shape = ctx.XrayXyzCurvatureShape(rows);
if (scale < 0)
scale = ctx.CalibrateXyzCurvature(refinable, shape);
scale = calibrate(shape);
for (double &x : shape)
x *= w * scale;
return shape;
}
TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &refinable, const std::vector<gemmi::Position> &pos0,
// The calibrations of the X-ray curvature in x, y, z (CalibrateXyzCurvature) of the trials RunTrials() runs together:
// trials that start from the same model with the same B and displacement model calibrate the same, so the first to
// need one makes it and the others wait for it.
class XyzCalibrations {
public:
// whether the B factors are refined (MoveOverallB), the displacement hypothesis, the kick, its seed and the first
// macro-cycle's blur
using Key = std::tuple<bool, const void *, double, unsigned, double>;
double Get(const Key &key, const std::function<double()> &calibrate) {
std::unique_lock<std::mutex> lock(m_);
if (auto it = made_.find(key); it != made_.end()) {
const std::shared_future<double> f = it->second;
lock.unlock();
return f.get();
}
std::promise<double> p;
made_.emplace(key, p.get_future().share());
lock.unlock();
try {
const double scale = calibrate();
p.set_value(scale);
return scale;
} catch (...) {
p.set_exception(std::current_exception()); // to those waiting for it
throw;
}
}
private:
std::mutex m_;
std::map<Key, std::shared_future<double>> made_;
};
TrialOutcome RunTrial(TrialSlot slot, XyzCalibrations &calibrations, const std::vector<int> &refinable, const std::vector<gemmi::Position> &pos0,
const std::vector<double> &b0, const std::vector<AtomAniso> &aniso0, const Context::Fit *fit,
const TrialWeights &tw, int cycles, const RefinementSettings &settings, const NcsCopies *ncs,
const AdpHypothesis *hyp = nullptr, bool hold_positions = false) {
@@ -2294,6 +2336,8 @@ TrialOutcome RunTrial(TrialSlot slot, const std::vector<int> &refinable, const s
PlaceRiders(ctx, topo);
ctx.tls = hyp->bodies;
ctx.tls_b = hyp->tls_b;
// the bodies' U bounded by the start's floor, not by the one the slot's last trial left
ctx.b_floor = fit != nullptr ? fit->b_floor : B_FLOOR;
ctx.ApplyTls();
} else if (kind == AdpHypothesis::Kind::Anisotropic) {
if (ctx.aniso.size() != ctx.atoms.size())
@@ -2314,8 +2358,15 @@ 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);
// the X-ray curvature's calibrations (CalibrateXyzCurvature, CalibrateBCurvature), once per trial
// the X-ray curvature's calibrations (CalibrateXyzCurvature, CalibrateBCurvature), once per trial - that in x, y,
// z shared with the trials that start alike
double xyz_scale = -1, b_scale = -1;
auto calibrate_xyz = [&](const std::vector<double> &shape) {
const XyzCalibrations::Key key{adp_w > 0, hyp, tw.kick, tw.seed, tw.blur.empty() ? 0.0 : tw.blur[0]};
const double scale = calibrations.Get(key, [&]() { return ctx.CalibrateXyzCurvature(refinable, shape); });
ctx.MoveRiders(); // the riders where the calibration leaves them, wherever it ran
return scale;
};
for (int cycle = 0; cycle < cycles && !ctx.Cancelled(); cycle++) {
PlaceRiders(ctx, topo);
if (cycle == 0 && fit != nullptr)
@@ -2347,11 +2398,11 @@ 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);
MinimiseAtoms(ctx, xyz_h, xyz_minimiser, XyzCurvature(ctx, refinable, blurred, xyz_h.Weight(), xyz_scale),
MinimiseAtoms(ctx, xyz_h, xyz_minimiser, XyzCurvature(ctx, blurred, xyz_h.Weight(), xyz_scale, calibrate_xyz),
set, settings.xyz_iterations);
ctx.engine->SetRowModel(ctx.rows);
} else
MinimiseAtoms(ctx, xyz, xyz_minimiser, XyzCurvature(ctx, refinable, ctx.rows, w, xyz_scale), set,
MinimiseAtoms(ctx, xyz, xyz_minimiser, XyzCurvature(ctx, ctx.rows, w, xyz_scale, calibrate_xyz), set,
settings.xyz_iterations);
}
// the X-ray curvature in B for the atoms as they are, which TLS, U and B take a share of
@@ -2462,9 +2513,11 @@ std::vector<TrialOutcome> RunTrials(const std::vector<TrialSlot> &trial_slots,
// every thread is dealt out, and a slot that has no trial left gives its threads to those still running.
const size_t running = std::min(trial_slots.size(), trials.size());
const bool on_cpu = !trial_slots[0].ctx->engine->OnGPU();
const size_t nthreads =
settings.nthreads + (settings.spare_arm_threads != nullptr ? settings.spare_arm_threads->load() : 0);
std::atomic<int> spare_threads{0};
for (size_t s = 0; s < running; s++) {
const size_t share = settings.nthreads / running + (on_cpu && s < settings.nthreads % running ? 1 : 0);
const size_t share = nthreads / running + (on_cpu && s < nthreads % running ? 1 : 0);
trial_slots[s].ctx->SetThreads(std::max<size_t>(1, share));
if (on_cpu)
trial_slots[s].ctx->spare_threads = &spare_threads;
@@ -2480,6 +2533,7 @@ std::vector<TrialOutcome> RunTrials(const std::vector<TrialSlot> &trial_slots,
std::iota(order.begin(), order.end(), 0);
std::stable_sort(order.begin(), order.end(), [&](size_t a, size_t b) { return length(a) > length(b); });
std::atomic<size_t> next{trial_slots.size()};
XyzCalibrations calibrations;
auto run_slot = [&](size_t s) {
try {
#ifdef JFJOCH_USE_CUDA
@@ -2487,8 +2541,8 @@ std::vector<TrialOutcome> RunTrials(const std::vector<TrialSlot> &trial_slots,
#endif
for (size_t j = s; j < trials.size(); j = next++)
if (const size_t i = order[j]; !trial_slots[s].ctx->Cancelled())
out[i] = RunTrial(trial_slots[s], refinable, pos0, b0, aniso0, fit, trials[i], cycles, settings, ncs,
hyps.empty() ? nullptr : hyps[i], hold_positions);
out[i] = RunTrial(trial_slots[s], calibrations, refinable, pos0, b0, aniso0, fit, trials[i], cycles,
settings, ncs, hyps.empty() ? nullptr : hyps[i], hold_positions);
} catch (...) {
errors[s] = std::current_exception();
}
@@ -3357,13 +3411,15 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
logger.Debug("Refinement: {} weight trials at a time", trial_slots.size());
// Trials whose need is decided by others are run with them where the slots take them all at once - but not
// where the slots share one card, whose time they would take from the trials that are needed (a model of 43k
// atoms: the ladder 10 s ahead, 5 s not) - and left out where it is decided they were not needed, with their
// evaluations, which do not count: the outcome and the count are the same whatever the slots.
// atoms: the ladder 10 s ahead, 5 s not), nor on the CPU, where the slots share the threads and a trial run
// ahead takes them from those that are needed - and left out where it is decided they were not needed, with
// their evaluations, which do not count: the outcome and the count are the same whatever the slots.
std::set<int> cards;
for (const TrialSlot &s : trial_slots)
cards.insert(s.ctx->device);
const bool one_card = cards.size() == 1 && *cards.begin() >= 0;
auto ahead = [&](size_t trials) { return !one_card && trial_slots.size() >= trials; };
const bool on_gpu = ctx.engine->OnGPU();
auto ahead = [&](size_t trials) { return on_gpu && !one_card && trial_slots.size() >= trials; };
int ahead_evaluations = 0;
auto drop_ahead = [&](TrialOutcome &o) {
ahead_evaluations += o.evaluations;
@@ -3473,6 +3529,22 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
return k >= 0 ? std::move(outcomes[k]) : TrialOutcome();
};
TrialOutcome state = ladder_choice(pos0, b0, aniso0, nullptr);
// 1a. A twinned refinement (DecideTwin) stops where the ladder leaves its law far from being taken, hardly
// ahead of its null (TwinNull, TWIN_STOP_Z) - its threads go to the untwinned refinement beside it, and
// the law is decided where it stopped.
bool stopped = false;
if (settings.twin_arm_stops && state.ran && !ctx.Cancelled()) {
ctx.SetPositions(state.pos);
ctx.SetB(state.b);
ctx.SetAniso(state.aniso);
ctx.Restore(state.fit);
ctx.FModel();
RefinementResult null;
TwinNull(ctx, null, null_first);
stopped = null.twin_null_z < TWIN_STOP_Z;
logger.Info("Refinement, twin law: after the ladder fraction {:.3f}, ahead of its null by {:.1f} standard "
"errors{}", FractionSum(ctx.rows), null.twin_null_z, stopped ? " - not taken further" : "");
}
// 2. The trajectory: steps of a few macro-cycles from where the last one ended, each trying the weights as they
// are and, in turn, the positions' X-ray weight halved and doubled (halved only in the first step, from the
@@ -3538,7 +3610,8 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
}
return best;
};
state = trajectory(std::move(state), false, from_best, "step");
if (!stopped)
state = trajectory(std::move(state), false, from_best, "step");
// 2a. The population: from where the trajectory ended, members that refine the positions on the data blurred
// along a schedule of their own (TrialWeights::blur: at 60 A^2 the data reach 4 A, at 20 A^2 2.2 A, then
@@ -3553,7 +3626,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// The members are at most as many as the trial slots, one wave: on a large model, where the card holds
// few, more members would cost a wave each.
const size_t population = std::min<size_t>(std::max(settings.population, 0), trial_slots.size());
if (population > 1 && state.ran && !ctx.Cancelled()) {
if (population > 1 && !stopped && state.ran && !ctx.Cancelled()) {
const RefinementStage pop_stage("population");
const double w = state.trial.weight, adp = state.trial.adp_weight, ncs_s = state.trial.ncs_sigma;
// the members, the most different first: the positions as they are and kicked, on the data as they are
@@ -3633,7 +3706,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// ladder (the trajectory run again from there gained nothing on twelve sets). Each round thus costs a map,
// an audit and a refinement of the solvent, where a refinement of its own cost the whole weight search
// (the build was 2-4 times the refinement).
if (build && state.ran && !ctx.Cancelled()) {
if (build && !stopped && state.ran && !ctx.Cancelled()) {
const auto build_start = std::chrono::steady_clock::now();
const RefinementStage build_stage("solvent_build");
SolventBuildResult &b = result.solvent_build;
@@ -3893,7 +3966,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// never sees the free reflections. A model whose side chains the data already hold moves nothing and
// costs the sweep alone. The atoms it prunes are left at occupancy 0, as the solvent build's placeholders
// are, and held.
if (settings.side_chain_field && state.ran && !ctx.Cancelled() && !ctx.refl.Twinned() &&
if (settings.side_chain_field && !stopped && state.ran && !ctx.Cancelled() && !ctx.refl.Twinned() &&
std::none_of(state.aniso.begin(), state.aniso.end(), [](const AtomAniso &a) { return a.on; })) {
const RefinementStage field_stage("side_chain_field");
const auto field_start = std::chrono::steady_clock::now();
@@ -4061,7 +4134,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// 3. The final weights: a short ladder from where the trajectory ended, tighter geometry first, its choice
// kept where it displaces that. A rung the step from there tried already is taken from that step.
if (state.ran && !ctx.Cancelled()) {
if (!stopped && state.ran && !ctx.Cancelled()) {
const double w = state.trial.weight, adp = state.trial.adp_weight, ncs_s = state.trial.ncs_sigma;
std::vector<TrialWeights> trials;
for (double f : settings.final_ladder)
@@ -4253,7 +4326,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
}
logger.Info("Refinement: displacement model {}", result.adp_model);
};
if (settings.adp_hypotheses && state.ran && !ctx.Cancelled())
if (settings.adp_hypotheses && !stopped && state.ran && !ctx.Cancelled())
displacements(input_tls);
// 4b. The weights and the displacement model decided again, from the model as it now is: the balanced
// weights on its B factors, the ladder (1.) from it with its displacements as they are (the U of TLS or
@@ -4268,8 +4341,8 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
// would run the same trials from the same model again, so the round ends there: from a model refined
// before, a round is its ladder and one step. No round where the refinement does not displace the model
// as given (5.).
for (int round = 2; round <= settings.rounds && settings.adp_hypotheses && state.ran && !ctx.Cancelled() &&
(!settings.as_given_competes || displaces(state.trial, given.trial));
for (int round = 2; round <= settings.rounds && settings.adp_hypotheses && !stopped && state.ran &&
!ctx.Cancelled() && (!settings.as_given_competes || displaces(state.trial, given.trial));
round++) {
const TrialOutcome before = state;
const std::vector<TlsBody> own_tls = state.tls;
@@ -4427,7 +4500,8 @@ double FinalRWork(const RefinementResult &r) {
// standard errors, each added domain at a fraction of TWIN_DOMAIN_MIN at least.
void DecideTwin(const gemmi::Structure &placed, const std::vector<MergedReflection> &merged,
const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg, double wavelength_A, Logger &logger,
const RefinementSettings &settings, RefinementResult &result, const std::shared_future<void> &untwinned) {
const RefinementSettings &settings, RefinementResult &result, const std::shared_future<void> &untwinned,
const std::function<void()> &arm_done) {
const RefinementStage stage("twin");
const std::vector<gemmi::Op> laws = RefinementTwinLaws(cell, sg);
const std::vector<gemmi::Op> domains = RefinementTwinDomains(cell, sg);
@@ -4489,7 +4563,10 @@ void DecideTwin(const gemmi::Structure &placed, const std::vector<MergedReflecti
}
}
}
RefinementResult twinned = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, settings, {law});
RefinementSettings arm = settings;
arm.twin_arm_stops = true;
RefinementResult twinned = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, arm, {law});
arm_done();
if (!take_untwinned())
return;
result.twin_split_pairs = 0;
@@ -4767,10 +4844,18 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
const bool halves = on_cpu && settings.population <= 1 && settings.nthreads > 1 &&
!RefinementTwinLaws(cell, sg).empty();
RefinementSettings untwinned_settings = settings, twin_settings = settings;
// and each arm, done, gives its threads to the other
std::atomic<size_t> spare_for_untwinned{0}, spare_for_twin{0};
if (halves) {
twin_settings.nthreads = settings.nthreads / 2;
untwinned_settings.nthreads = settings.nthreads - twin_settings.nthreads;
untwinned_settings.spare_arm_threads = &spare_for_untwinned;
twin_settings.spare_arm_threads = &spare_for_twin;
}
auto twin_done = [&]() {
if (halves)
spare_for_untwinned = twin_settings.nthreads;
};
Logger twin_logger = Logger::Buffered();
CardHeld held;
std::future<size_t> held_bytes = held.bytes.get_future();
@@ -4795,10 +4880,12 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
untwinned.wait();
// A failure here (the card's memory, shared with the untwinned arm) leaves the untwinned result standing.
try {
DecideTwin(placed, data, cell, sg, wavelength_A, twin_logger, twin_settings, result, untwinned);
DecideTwin(placed, data, cell, sg, wavelength_A, twin_logger, twin_settings, result, untwinned,
twin_done);
} catch (const std::exception &e) {
twin_logger.Warning("Refinement: twin hypothesis not tested - {}", e.what());
}
twin_done(); // where it ended before its refinement
});
try {
result = RefineOnce(placed, data, cell, sg, wavelength_A, logger, untwinned_settings, {}, 0, &held);
@@ -4810,6 +4897,8 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector<M
throw;
}
held.Say(0); // where it stopped before placing its slots
if (halves)
spare_for_twin = untwinned_settings.nthreads;
untwinned_done.set_value();
twin_arm.get();
twin_logger.ReplayInto(logger);
@@ -109,6 +109,12 @@ struct RefinementSettings {
// iterations and before every weight trial. The trials that ran are chosen among as usual, those that did not
// are left out (RefinementResult::cancelled); cut before the first trial, the model is the placement's.
const std::atomic<bool> *cancel = nullptr;
// Where two refinements run side by side on the CPU (RefineModel's arms): the threads the other one has finished
// with, which this one's weight trials take up from then on (the result does not depend on it). Null: none.
const std::atomic<size_t> *spare_arm_threads = nullptr;
// A twinned refinement beside the untwinned one (DecideTwin): it stops after its first ladder where that leaves
// the law far from being taken.
bool twin_arm_stops = false;
// The user's restraint dictionary, over the embedded one (BuildRefinementTopology)
RestraintDictionary dictionary;
};
@@ -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);