diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 04734031d..3ba9c018d 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include #include @@ -2190,18 +2192,51 @@ double XrayGradientNorm(Context &ctx, const std::vector &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 XyzCurvature(Context &ctx, const std::vector &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 XyzCurvature(Context &ctx, const XrayRowModel &rows, double w, double &scale, + const std::function &)> &calibrate) { std::vector 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 &refinable, const std::vector &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; + double Get(const Key &key, const std::function &calibrate) { + std::unique_lock lock(m_); + if (auto it = made_.find(key); it != made_.end()) { + const std::shared_future f = it->second; + lock.unlock(); + return f.get(); + } + std::promise 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> made_; +}; + +TrialOutcome RunTrial(TrialSlot slot, XyzCalibrations &calibrations, const std::vector &refinable, const std::vector &pos0, const std::vector &b0, const std::vector &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 &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 &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 &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 RunTrials(const std::vector &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 next{trial_slots.size()}; + XyzCalibrations calibrations; auto run_slot = [&](size_t s) { try { #ifdef JFJOCH_USE_CUDA @@ -2426,8 +2469,8 @@ std::vector RunTrials(const std::vector &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(); }