Files
jungfrauandClaude Opus 5.5 f07493c9bf Side-chain field: the reflections split across every card
The field's search is iterated conditional modes over the residues, one after another, and
each step evaluates -ln L over every working reflection for each combination of states. On a
large model it ran on the main engine's card alone while the others idled: a 242k-atom model
with 45 NCS copies and 663k reflections spent 1743 s of a 2392 s refinement in it on one of
four T4s.

-ln L is a sum over the reflections, so MakeSideChainFieldEvaluatorGPUs gives each card a
contiguous part of them and adds the parts' sums in card order. The search, its order and its
decisions are unchanged; only the last bits of each sum move, where the parts meet. One card
keeps the evaluator as it was.

On that model, 4 x T4: the field 1743 -> 456 s, the refinement 2392 -> 1119 s, the refined
model bit-identical (same R-free, same md5). Two small models refine bit-identically, in the
same time.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJz9sA2BQBEgKkuVuL
2026-11-17 11:27:24 +01:00

663 lines
32 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "SideChainField.h"
#include <algorithm>
#include <cmath>
#include <future>
#include <map>
#include <optional>
#include <set>
#include <spdlog/fmt/fmt.h>
#include <gemmi/neighbor.hpp>
#include "../../common/ParallelFor.h"
#include "../bragg_integration/BraggExp.h"
#include "RefinementMath.h"
#include "SideChainRotamers.h"
#ifdef __APPLE__
#include "../../common/JFJochException.h"
#include "../../common/Logger.h"
#include "SideChainFieldMetal.h"
#endif
namespace {
constexpr double REPULSION_SIGMA = 0.2; // A, the refinement's (RefinementRestraints.cpp)
constexpr double REACH = 4.0; // A: the farthest pair that can overlap (OverlapOf)
constexpr int MAX_SWEEPS = 6;
// the likelihood evaluations (states times reflections) a sweep may take before the copies of a residue are searched
// together only: about 3 s on one card, which a model of a few hundred residues, or a few copies of one, stays within
constexpr double SWEEP_BUDGET = 4e8;
// the most states one variable puts on the card: the copies together in every position of an arginine (3^3
// staggered chis x 12 settings of the guanidinium, as it is, absent)
constexpr size_t MAX_STATES = 3 * 3 * 3 * 12 + 2;
// The amide of Asn and Gln either way round fits the density alike at the resolutions of protein crystals: the
// hydrogen bonds its O and N can make decide, as PDB-REDO's flips do (Joosten et al. (2011) Bioinformatics 27,
// 3392-3398). Per partner within HBOND_REACH: a donor to the O or an acceptor to the N counts for the state, two
// donors or two acceptors against it, HBOND_WEIGHT each in the units of -ln L.
constexpr double HBOND_REACH = 3.3; // A
constexpr double HBOND_WEIGHT = 2.0;
enum class HBond { None, Donor, Acceptor, Both };
HBond HBondType(const gemmi::Residue &res, const gemmi::Atom &a) {
if (res.is_water())
return HBond::Both;
const std::string &n = a.name;
if (n == "N")
return res.name == "PRO" ? HBond::None : HBond::Donor;
if (n == "O" || n == "OXT")
return HBond::Acceptor;
if (res.name == "ASP" || res.name == "GLU" || res.name == "ASN" || res.name == "GLN")
return a.element == gemmi::El::O ? HBond::Acceptor : a.element == gemmi::El::N ? HBond::Donor : HBond::None;
if (res.name == "LYS" || res.name == "ARG" || res.name == "TRP")
return a.element == gemmi::El::N ? HBond::Donor : HBond::None;
if (res.name == "SER" || res.name == "THR" || res.name == "TYR" || res.name == "HIS" || res.name == "CYS")
return a.element == gemmi::El::O || a.element == gemmi::El::N || a.element == gemmi::El::S ? HBond::Both : HBond::None;
if (res.name == "MET")
return a.element == gemmi::El::S ? HBond::Acceptor : HBond::None;
return a.element == gemmi::El::O || a.element == gemmi::El::N ? HBond::Both : HBond::None;
}
// The hydrogen bonds the amide of an Asn or Gln at positions `p` can make, less those it cannot: partners of its O
// and N in the rest of the model within HBOND_REACH, by type. 0 for any other residue.
int AmideHBonds(gemmi::NeighborSearch &ns, const gemmi::Model &model, const gemmi::UnitCell &cell,
const gemmi::Residue &res, int chain_idx, int residue_idx, const std::vector<gemmi::Position> &p) {
if (res.name != "ASN" && res.name != "GLN")
return 0;
int score = 0;
for (size_t i = 0; i < p.size(); i++) {
const HBond own = HBondType(res, res.atoms[i]);
if (SideChainDepth(res.atoms[i].name) < 3 || (own != HBond::Donor && own != HBond::Acceptor))
continue;
ns.for_each(p[i], '\0', HBOND_REACH, [&](const gemmi::NeighborSearch::Mark &m, double) {
if (m.image_idx == 0 && m.chain_idx == chain_idx && m.residue_idx == residue_idx)
return;
const gemmi::Residue &other = model.chains[m.chain_idx].residues[m.residue_idx];
const gemmi::Atom &b = other.atoms[m.atom_idx];
if (!(b.occ > 0))
return;
const HBond type = HBondType(other, b);
if (type == HBond::None || cell.find_nearest_pbc_image(p[i], b.pos, m.image_idx).dist() > HBOND_REACH)
return;
if (type == HBond::Both || type != own)
score++;
else
score--;
});
}
return score;
}
// sin and cos of 2 pi v in single precision, written so that a loop of them vectorises: v taken to [-1/2, 1/2] by
// its nearest integer, the half angle pi v (within +-pi/2) by its Taylor series to the 11th and 12th power (an error
// below 1e-7), then the double-angle formulas.
inline void SinCos2Pi(float v, float &sn, float &cs) {
const float x = 3.14159265f * (v - std::nearbyint(v));
const float x2 = x * x;
const float s = x * (1 + x2 * (-1.f / 6 + x2 * (1.f / 120 + x2 * (-1.f / 5040 + x2 * (1.f / 362880 + x2 * (-1.f / 39916800))))));
const float c = 1 + x2 * (-1.f / 2 + x2 * (1.f / 24 + x2 * (-1.f / 720 + x2 * (1.f / 40320 + x2 * (-1.f / 3628800 + x2 * (1.f / 479001600))))));
sn = 2 * s * c;
cs = c * c - s * s;
}
class EvaluatorCPU : public SideChainFieldEvaluator {
public:
EvaluatorCPU(const FieldReflections &refl, const std::vector<std::array<float, 12>> &ops,
const std::vector<FormFactor> &elements, size_t nthreads, [[maybe_unused]] bool gpu)
: refl_(refl), elements_(elements), nthreads_(nthreads), n_ops_(ops.size()) {
// per operator and row (k * rows + m): R^T h and h . t, so that the phase of an atom at x is
// 2 pi (R^T h . x + h . t)
const size_t rows = refl.Size();
for (std::vector<float> *v : {&hx_, &hy_, &hz_, &ht_})
v->resize(ops.size() * rows);
for (size_t k = 0; k < ops.size(); k++)
for (size_t m = 0; m < rows; m++) {
const std::array<float, 3> &h = refl.hkl[m];
const std::array<float, 12> &op = ops[k];
hx_[k * rows + m] = op[0] * h[0] + op[3] * h[1] + op[6] * h[2];
hy_[k * rows + m] = op[1] * h[0] + op[4] * h[1] + op[7] * h[2];
hz_[k * rows + m] = op[2] * h[0] + op[5] * h[1] + op[8] * h[2];
ht_[k * rows + m] = op[9] * h[0] + op[10] * h[1] + op[11] * h[2];
}
// per element and row (e * rows + m) its form factor, c + sum a_i exp(-b_i stol2)
f0_.resize(elements.size() * rows);
for (size_t e = 0; e < elements.size(); e++)
for (size_t m = 0; m < rows; m++) {
const FormFactor &ff = elements[e];
float f = ff[8];
for (int i = 0; i < 4; i++)
f += ff[i] * std::exp(-ff[4 + i] * refl.stol2[m]);
f0_[e * rows + m] = f;
}
#ifdef __APPLE__
if (gpu)
try {
metal_ = SideChainFieldMetal::Make(rows, n_ops_, hx_, hy_, hz_, ht_, f0_, refl.stol2);
} catch (const JFJochException &e) {
MetalFailed(e);
}
#endif
}
void SetTotal(const std::vector<std::complex<float>> &f) override { total_ = f; }
// In single precision, as the GPU's contribution_kernel: per atom of the state its form factor at every row of
// a block, then per operator the phase's sine and cosine (SinCos2Pi) - loops over the rows that the compiler
// vectorises. The B factor's exp is BraggExp, which an Apple GPU computes to the same bits.
std::vector<std::complex<float>> Contribution(const FieldStates &states, int s) override {
#ifdef __APPLE__
if (metal_) {
std::vector<std::vector<std::complex<float>>> one(1);
if (ContributionsMetal(states, s, one))
return one[0];
}
#endif
const size_t rows = refl_.Size();
std::vector<std::complex<float>> f(rows);
constexpr size_t BLOCK = 256;
const int blocks = static_cast<int>((rows + BLOCK - 1) / BLOCK);
ParallelFor(blocks, nthreads_, [&](int b) {
const size_t lo = b * BLOCK, n = std::min(BLOCK, rows - lo);
float re[BLOCK] = {}, im[BLOCK] = {}, fa[BLOCK], v[BLOCK];
for (int a = states.start[s]; a < states.start[s + 1]; a++) {
const FieldAtom &at = states.atoms[a];
const float *f0 = &f0_[at.element * rows + lo];
for (size_t m = 0; m < n; m++)
fa[m] = f0[m] * at.occ * BraggExp(-at.b * refl_.stol2[lo + m]);
for (size_t k = 0; k < n_ops_; k++) {
const size_t o = k * rows + lo;
for (size_t m = 0; m < n; m++)
v[m] = hx_[o + m] * at.x + hy_[o + m] * at.y + hz_[o + m] * at.z + ht_[o + m];
for (size_t m = 0; m < n; m++) {
float sn, cs;
SinCos2Pi(v[m], sn, cs);
re[m] += fa[m] * cs;
im[m] += fa[m] * sn;
}
}
}
for (size_t m = 0; m < n; m++)
f[lo + m] = {re[m], im[m]};
});
return f;
}
std::vector<double> Evaluate(const FieldStates &states) override {
const std::vector<std::vector<std::complex<float>>> df = All(states);
std::vector<double> nll(states.Combos(), 0.0);
// the combinations side by side, each summed over the rows in order
ParallelFor(static_cast<int>(states.Combos()), nthreads_, [&](int c) {
double sum = 0;
for (size_t m = 0; m < refl_.Size(); m++) {
if (!refl_.work[m])
continue;
std::complex<float> f = total_[m];
for (int s : states.current)
f -= df[s][m];
for (int k = states.combo_start[c]; k < states.combo_start[c + 1]; k++)
f += df[states.combo_states[k]][m];
sum += Term(m, f);
}
nll[c] = sum;
});
return nll;
}
void Move(const FieldStates &states, int to) override {
// the contributions of the states the move takes out and puts in, only
std::vector<std::vector<std::complex<float>>> df(states.Count());
for (int s : states.current)
df[s] = Contribution(states, s);
for (int k = states.combo_start[to]; k < states.combo_start[to + 1]; k++)
if (const int s = states.combo_states[k]; df[s].empty())
df[s] = Contribution(states, s);
for (size_t m = 0; m < refl_.Size(); m++) {
for (int s : states.current)
total_[m] -= df[s][m];
for (int k = states.combo_start[to]; k < states.combo_start[to + 1]; k++)
total_[m] += df[states.combo_states[k]][m];
}
}
private:
std::vector<std::vector<std::complex<float>>> All(const FieldStates &states) {
std::vector<std::vector<std::complex<float>>> df;
#ifdef __APPLE__
// on the GPU in batches of states, so that its buffer stays small beside the states' own
constexpr size_t BATCH = 32;
for (size_t s = 0; metal_ && s < states.Count(); s += BATCH) {
std::vector<std::vector<std::complex<float>>> batch(std::min(BATCH, states.Count() - s));
if (!ContributionsMetal(states, static_cast<int>(s), batch))
break;
for (auto &f : batch)
df.push_back(std::move(f));
}
#endif
for (size_t s = df.size(); s < states.Count(); s++)
df.push_back(Contribution(states, static_cast<int>(s)));
return df;
}
#ifdef __APPLE__
// Contribution() of the states from `first` on, one per element of `out`, on the Apple GPU (SideChainFieldMetal.h):
// the same values, bit for bit. False where it failed: the CPU from then on, with one warning.
bool ContributionsMetal(const FieldStates &states, int first, std::vector<std::vector<std::complex<float>>> &out) {
try {
metal_->Contributions(states, first, out);
return true;
} catch (const JFJochException &e) {
MetalFailed(e);
return false;
}
}
void MetalFailed(const JFJochException &e) {
Logger("Rugnux").Warning("Side-chain field: the Apple GPU failed, on the CPU from here on: {}", e.what());
metal_.reset();
}
std::unique_ptr<SideChainFieldMetal> metal_;
#endif
double Term(size_t m, std::complex<float> f_atoms) const {
const double fc = std::abs(refl_.k_total[m] * (f_atoms + refl_.fmask[m]));
double dfc = 0;
return refine_math::AmplitudeNegLogLikelihood<double>(refl_.fo[m], fc, refl_.d[m], refl_.var[m],
refl_.centric[m], dfc);
}
FieldReflections refl_;
std::vector<FormFactor> elements_;
size_t nthreads_, n_ops_;
std::vector<float> hx_, hy_, hz_, ht_;
std::vector<float> f0_;
std::vector<std::complex<float>> total_;
};
// The residues the field searches.
struct Candidate {
int chain, residue; // indices in the model
int ordinal; // the residue's number over the model in order
int first_atom; // index of the residue's first atom in the model's atom list
SideChain sc;
std::string name;
bool absent = false; // pruned to CB by an earlier sweep
};
std::vector<Candidate> Candidates(const gemmi::Structure &st, const std::vector<char> &refinable) {
const gemmi::Model &model = st.models[0];
std::vector<std::pair<const gemmi::Atom *, const gemmi::Atom *>> links;
for (const gemmi::Connection &conn : st.connections) {
const gemmi::const_CRA a = model.find_cra(conn.partner1), b = model.find_cra(conn.partner2);
if (a.atom != nullptr && b.atom != nullptr)
links.emplace_back(a.atom, b.atom);
}
std::vector<Candidate> out;
int n = 0, ordinal = 0;
for (size_t ci = 0; ci < model.chains.size(); ci++)
for (size_t ri = 0; ri < model.chains[ci].residues.size(); ri++, ordinal++) {
const gemmi::Residue &res = model.chains[ci].residues[ri];
const int first = n;
n += static_cast<int>(res.atoms.size());
std::optional<SideChain> sc = MakeSideChain(res);
if (!sc)
continue;
bool ok = true;
for (size_t i = 0; i < res.atoms.size(); i++)
if (!res.atoms[i].is_hydrogen() && (!refinable[first + i] || !(res.atoms[i].occ >= 1.0f)))
ok = false;
for (const auto &[a, b] : links)
for (const gemmi::Atom &x : res.atoms)
if (&x == a || &x == b)
ok = false; // a side chain a LINK binds stays
if (!ok)
continue;
out.push_back({static_cast<int>(ci), static_cast<int>(ri), ordinal, first, std::move(*sc),
res.name + " " + model.chains[ci].name + res.seqid.str()});
}
return out;
}
// The field's variables: a residue, or the copies of one the NCS relates (ncs_group: per residue ordinal, -1 for
// none) - which move together or one at a time; small side chains first, arginine last.
std::vector<std::vector<int>> Variables(const std::vector<Candidate> &candidates, const gemmi::Model &model,
const std::vector<int> &ncs_group) {
std::vector<std::vector<int>> out;
std::map<int, size_t> of_group;
for (size_t i = 0; i < candidates.size(); i++) {
const int g = ncs_group.empty() ? -1 : ncs_group[candidates[i].ordinal];
const gemmi::Residue &res = model.chains[candidates[i].chain].residues[candidates[i].residue];
if (g >= 0 && of_group.count(g) &&
model.chains[candidates[out[of_group[g]][0]].chain].residues[candidates[out[of_group[g]][0]].residue].name ==
res.name) {
out[of_group[g]].push_back(static_cast<int>(i));
continue;
}
if (g >= 0)
of_group[g] = out.size();
out.push_back({static_cast<int>(i)});
}
std::stable_sort(out.begin(), out.end(), [&](const std::vector<int> &a, const std::vector<int> &b) {
auto key = [&](const Candidate &c) {
return model.chains[c.chain].residues[c.residue].name == "ARG" ? 100 : static_cast<int>(c.sc.chi.size());
};
return key(candidates[a[0]]) < key(candidates[b[0]]);
});
return out;
}
} // namespace
std::vector<std::array<float, 12>> FieldOperators(const gemmi::SpaceGroup &sg) {
std::vector<std::array<float, 12>> out;
for (const gemmi::Op &op : sg.operations()) {
std::array<float, 12> o{};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
o[3 * i + j] = static_cast<float>(op.rot[i][j]) / static_cast<float>(gemmi::Op::DEN);
o[9 + i] = static_cast<float>(op.tran[i]) / static_cast<float>(gemmi::Op::DEN);
}
out.push_back(o);
}
return out;
}
#ifdef JFJOCH_USE_CUDA
size_t SideChainFieldDeviceBytes(size_t rows, size_t n_ops) {
// the operators applied to each reflection (16 B each), the reflection arrays (about 48 B a row), and dF for
// the states of one variable: within the sweep budget where the copies move alone, MAX_STATES otherwise
return rows * (16 * n_ops + 48) + 8 * std::max(static_cast<size_t>(SWEEP_BUDGET), MAX_STATES * rows);
}
namespace {
// The GPU evaluator over several cards, each holding a contiguous part of the reflections. -ln L is a sum over the
// reflections, so the cards evaluate their parts side by side and each combination's value is the sum of theirs, added
// in card order (the last bits differ from one card's, whose sum is not cut where the parts meet).
class EvaluatorCards : public SideChainFieldEvaluator {
public:
EvaluatorCards(const std::vector<int> &devices, const FieldReflections &refl,
const std::vector<std::array<float, 12>> &ops, const std::vector<FormFactor> &elements) {
const size_t n = devices.size(), rows = refl.Size();
for (size_t k = 0; k <= n; k++)
begin_.push_back(rows * k / n);
for (size_t k = 0; k < n; k++)
parts_.push_back(MakeSideChainFieldEvaluatorGPU(devices[k], Slice(refl, begin_[k], begin_[k + 1]), ops,
elements));
}
void SetTotal(const std::vector<std::complex<float>> &f) override {
for (size_t k = 0; k < parts_.size(); k++)
parts_[k]->SetTotal(std::vector<std::complex<float>>(f.begin() + begin_[k], f.begin() + begin_[k + 1]));
}
std::vector<double> Evaluate(const FieldStates &states) override {
std::vector<std::future<std::vector<double>>> on;
for (auto &p : parts_)
on.push_back(std::async(std::launch::async, [&p, &states] { return p->Evaluate(states); }));
std::vector<double> nll = on[0].get();
for (size_t k = 1; k < on.size(); k++) {
const std::vector<double> part = on[k].get();
for (size_t c = 0; c < nll.size(); c++)
nll[c] += part[c];
}
return nll;
}
void Move(const FieldStates &states, int to) override {
std::vector<std::future<void>> on;
for (auto &p : parts_)
on.push_back(std::async(std::launch::async, [&p, &states, to] { p->Move(states, to); }));
for (auto &f : on)
f.get();
}
std::vector<std::complex<float>> Contribution(const FieldStates &states, int s) override {
std::vector<std::complex<float>> f;
for (auto &p : parts_) {
const std::vector<std::complex<float>> part = p->Contribution(states, s);
f.insert(f.end(), part.begin(), part.end());
}
return f;
}
private:
static FieldReflections Slice(const FieldReflections &r, size_t lo, size_t hi) {
auto part = [lo, hi](const auto &v) { return std::decay_t<decltype(v)>(v.begin() + lo, v.begin() + hi); };
return {part(r.hkl), part(r.stol2), part(r.fo), part(r.d), part(r.var), part(r.centric), part(r.work),
part(r.k_total), part(r.fmask)};
}
std::vector<size_t> begin_; // the first row of each card's part, and the end
std::vector<std::unique_ptr<SideChainFieldEvaluator>> parts_;
};
} // namespace
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorGPUs(const std::vector<int> &devices,
const FieldReflections &refl,
const std::vector<std::array<float, 12>> &ops,
const std::vector<FormFactor> &elements) {
if (devices.size() == 1)
return MakeSideChainFieldEvaluatorGPU(devices[0], refl, ops, elements);
return std::make_unique<EvaluatorCards>(devices, refl, ops, elements);
}
#endif
std::unique_ptr<SideChainFieldEvaluator> MakeSideChainFieldEvaluatorCPU(const FieldReflections &refl,
const std::vector<std::array<float, 12>> &ops,
const std::vector<FormFactor> &elements,
size_t nthreads, bool gpu) {
return std::make_unique<EvaluatorCPU>(refl, ops, elements, nthreads, gpu);
}
std::vector<FieldAtom> FieldAtomsOf(const gemmi::Residue &res, const SideChain &sc, const std::vector<gemmi::Position> &p,
const gemmi::UnitCell &cell, const std::vector<int> &element_of, int first_atom) {
std::vector<FieldAtom> out;
for (size_t i = 0; i < res.atoms.size(); i++)
if (sc.depth[i] >= 2) {
const gemmi::Fractional f = cell.fractionalize(p[i]);
out.push_back({static_cast<float>(f.x), static_cast<float>(f.y), static_cast<float>(f.z), res.atoms[i].b_iso,
1.0f, element_of[first_atom + i]});
}
return out;
}
SideChainFieldResult OptimiseSideChainField(const gemmi::Structure &given, const std::vector<int> &element_of,
const std::vector<char> &refinable, const std::vector<int> &ncs_group,
double weight, size_t rows, SideChainFieldEvaluator &evaluator) {
SideChainFieldResult result;
gemmi::Structure st = given;
gemmi::Model &model = st.models[0];
gemmi::UnitCell cell = st.cell;
cell.set_cell_images_from_spacegroup(st.find_spacegroup());
std::vector<Candidate> candidates = Candidates(st, refinable);
result.candidates = static_cast<int>(candidates.size());
if (candidates.empty())
return result;
const std::vector<std::vector<int>> variables = Variables(candidates, model, ncs_group);
// each copy alone in each rotamer is what makes a sweep over many copies costly: dropped where it would take a
// sweep over the budget
double sweep_cost = 0;
for (const std::vector<int> &var : variables) {
const Candidate &cand = candidates[var[0]];
const double n_pos = SideChainRotamers(model.chains[cand.chain].residues[cand.residue].name,
cand.sc.chi.size()).size() + 1;
sweep_cost += (1 + n_pos * (var.size() > 1 ? var.size() + 1 : 1)) * static_cast<double>(rows);
}
const bool copies_alone = sweep_cost <= SWEEP_BUDGET;
result.copies_together = !copies_alone;
// the repulsion term of the refinement's target, in the units of -ln L: ((d0 - d) / sigma)^2 summed, over the
// X-ray weight
const double repulsion = 1.0 / (REPULSION_SIGMA * REPULSION_SIGMA * weight);
gemmi::NeighborSearch ns(model, cell, REACH + 1);
ns.populate(/*include_h=*/false);
std::map<int, size_t> moved; // candidate -> entry in result.moves
// the residues near a move of the last sweep: what the next sweep visits again (the first visits every residue)
std::set<std::pair<int, int>> near, near_last;
auto note_near = [&](const gemmi::Position &pos) {
ns.for_each(pos, '\0', REACH + 2, [&](const gemmi::NeighborSearch::Mark &m, double) {
near.insert({m.chain_idx, m.residue_idx});
});
};
for (int sweep = 1; sweep <= MAX_SWEEPS; sweep++) {
bool any = false;
near_last.swap(near);
near.clear();
for (const std::vector<int> &var : variables) {
const size_t k = var.size();
if (sweep > 1 && std::none_of(var.begin(), var.end(), [&](int i) {
return near_last.count({candidates[i].chain, candidates[i].residue}) > 0;
}))
continue;
// per copy: its positions as it is and in each rotamer (the same list for every copy: one residue
// type), the chi angles of each, and the geometry's share of each one's energy: its overlap with the
// rest of the model, and the hydrogen bonds of an amide
std::vector<std::vector<std::vector<gemmi::Position>>> positions(k);
std::vector<std::vector<double>> geometry(k);
std::vector<std::vector<double>> chis;
for (size_t c = 0; c < k; c++) {
Candidate &cand = candidates[var[c]];
const gemmi::Residue &res = model.chains[cand.chain].residues[cand.residue];
cand.sc.pos.clear();
for (const gemmi::Atom &a : res.atoms)
cand.sc.pos.push_back(a.pos);
positions[c] = {cand.sc.pos};
if (c == 0)
chis = {cand.sc.Chis()};
for (const std::vector<double> &chi : SideChainRotamers(res.name, cand.sc.chi.size())) {
positions[c].push_back(cand.sc.Build(chi));
if (c == 0)
chis.push_back(chi);
}
for (const std::vector<gemmi::Position> &p : positions[c])
geometry[c].push_back(repulsion * OverlapOf(ns, model, cell, res, cand.chain, cand.residue, cand.sc, p, {}).sum -
HBOND_WEIGHT * AmideHBonds(ns, model, cell, res, cand.chain, cand.residue, p));
geometry[c].push_back(0); // absent
if (cand.absent)
geometry[c][0] = 0;
}
const int n_pos = static_cast<int>(positions[0].size()), absent = n_pos;
auto atoms_of = [&](size_t c, int p) {
const Candidate &cand = candidates[var[c]];
if (p == 0 && cand.absent) // as it is, pruned: no atoms
return std::vector<FieldAtom>();
return FieldAtomsOf(model.chains[cand.chain].residues[cand.residue], cand.sc, positions[c][p], cell,
element_of, cand.first_atom);
};
// the combinations as assignments, per copy the index of its positions (absent beyond them): as it is,
// the copies together in each rotamer and absent, then each copy alone in each
std::vector<std::vector<int>> assign = {std::vector<int>(k, 0)};
for (int p = 1; p <= absent; p++)
assign.emplace_back(k, p);
FieldStates states;
if (copies_alone) {
// the states: per copy its positions (c * n_pos + p)
for (size_t c = 0; c < k; c++) {
for (int p = 0; p < n_pos; p++)
states.Add(atoms_of(c, p));
states.current.push_back(static_cast<int>(c) * n_pos);
}
if (k > 1)
for (size_t c = 0; c < k; c++)
for (int p = 1; p <= absent; p++) {
assign.emplace_back(k, 0);
assign.back()[c] = p;
}
for (const std::vector<int> &a : assign) {
std::vector<int> combo;
for (size_t c = 0; c < k; c++)
if (a[c] != absent)
combo.push_back(static_cast<int>(c) * n_pos + a[c]);
states.AddCombo(combo);
}
} else {
// the copies together only: one state per position, every copy's atoms in it (p), a k-th of the
// states and of the memory they take on the card
for (int p = 0; p < n_pos; p++) {
std::vector<FieldAtom> atoms;
for (size_t c = 0; c < k; c++) {
const std::vector<FieldAtom> a = atoms_of(c, p);
atoms.insert(atoms.end(), a.begin(), a.end());
}
states.Add(atoms);
}
states.current.push_back(0);
for (const std::vector<int> &a : assign)
states.AddCombo(a[0] == absent ? std::vector<int>() : std::vector<int>{a[0]});
}
std::vector<double> energy = evaluator.Evaluate(states);
// the alternatives are many and the best of them is chosen: a move must beat the state as given by the
// log of their number, what the best of as many noise draws would gain
const double occam = std::log(static_cast<double>(assign.size() - 1));
for (size_t s = 0; s < assign.size(); s++) {
for (size_t c = 0; c < k; c++)
energy[s] += geometry[c][assign[s][c]];
if (s > 0)
energy[s] += occam;
}
const int best = static_cast<int>(std::min_element(energy.begin(), energy.end()) - energy.begin());
if (sweep == 1 && &var == &variables.front())
result.energy_before = energy[0];
if (sweep == 1) {
const int alt = static_cast<int>(std::min_element(energy.begin() + 1, energy.end()) - energy.begin());
result.alternatives.push_back({candidates[var[0]].name + (k > 1 ? fmt::format(" x{}", k) : ""),
energy[0] - energy[alt] + occam, chis[0],
assign[alt][0] == absent ? std::vector<double>() : chis[assign[alt][0]]});
}
if (best == 0 || !(energy[best] < energy[0]))
continue;
// moved: the field, the model and the contacts the others see
evaluator.Move(states, best);
int changed = 0;
for (size_t c = 0; c < k; c++)
changed += assign[best][c] != 0;
for (size_t c = 0; c < k; c++) {
const int p = assign[best][c];
if (p == 0)
continue;
Candidate &cand = candidates[var[c]];
gemmi::Residue &res = model.chains[cand.chain].residues[cand.residue];
SideChainFieldMove &m = moved.count(var[c]) ? result.moves[moved[var[c]]] : result.moves.emplace_back();
if (m.name.empty()) {
moved[var[c]] = result.moves.size() - 1;
m.name = cand.name;
m.chi_before = cand.sc.Chis();
}
m.energy_gain += (energy[0] - energy[best]) / changed;
m.absent = cand.absent = p == absent;
m.chi_after = m.absent ? std::vector<double>() : chis[p];
m.atoms.clear();
m.pruned.clear();
for (size_t i = 0; i < res.atoms.size(); i++)
if (cand.sc.depth[i] >= 2) {
if (!res.atoms[i].is_hydrogen()) {
note_near(res.atoms[i].pos);
note_near(positions[c][p == absent ? 0 : p][i]);
}
if (m.absent) {
res.atoms[i].occ = 0;
m.pruned.push_back(cand.first_atom + static_cast<int>(i));
} else {
res.atoms[i].occ = 1;
res.atoms[i].pos = positions[c][p][i];
m.atoms.emplace_back(cand.first_atom + static_cast<int>(i), positions[c][p][i]);
}
}
}
ns = gemmi::NeighborSearch(model, cell, REACH + 1);
ns.populate(/*include_h=*/false);
any = true;
}
result.sweeps = sweep;
if (!any)
break;
}
result.energy_after = result.energy_before;
for (const SideChainFieldMove &m : result.moves)
result.energy_after -= m.energy_gain;
return result;
}