Refinement: the trials that start alike share their curvature calibration in x, y, z
The calibration of the X-ray curvature in the positions (two evaluations, fixed seed) depends only on where a trial starts: the model, whether its B factors are refined, the displacement hypothesis, the kick and the first blur - not on the weights. The trials of one RunTrials() call that agree in all of these now share it: the first to need it makes it, the others wait for it. The scale and the riders are bit for bit what each trial would have made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
This commit is contained in:
1 parent
74b6b568d2
commit
91ebb7626f
1 file changed
+53
-10
@@ -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>
|
||||
@@ -2190,18 +2192,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) {
|
||||
@@ -2253,8 +2288,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)
|
||||
@@ -2286,11 +2328,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
|
||||
@@ -2419,6 +2461,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
|
||||
@@ -2426,8 +2469,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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user