// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include #include #include #include #include "gemmi/it92.hpp" #include "gemmi/sfcalc.hpp" #include "gemmi/symmetry.hpp" #include "../image_analysis/structure_refinement/SideChainField.h" #ifdef JFJOCH_USE_CUDA #include #endif namespace { constexpr double EDGE = 24.0; // A, a P 21 21 21 box: the sum runs over four operators constexpr double D_MIN = 2.0; constexpr double DEG = 3.14159265358979323846 / 180.0; // The atom bonded to c at `bond`, at `angle` b-c-x and the torsion a-b-c-x (degrees). gemmi::Position place(const gemmi::Position &a, const gemmi::Position &b, const gemmi::Position &c, double bond, double angle, double torsion) { const gemmi::Vec3 bc = (c - b).normalized(); const gemmi::Vec3 n = (b - a).cross(bc).normalized(); const gemmi::Vec3 m = n.cross(bc); const double x = -bond * std::cos(angle * DEG); const double y = bond * std::sin(angle * DEG) * std::cos(torsion * DEG); const double z = bond * std::sin(angle * DEG) * std::sin(torsion * DEG); return gemmi::Position(gemmi::Vec3(c) + bc * x + m * y + n * z); } // A lysine at these chi angles, its main chain near the middle of the box. gemmi::Residue lysine(double chi1, double chi2, double chi3, double chi4) { gemmi::Residue r; r.name = "LYS"; r.seqid = gemmi::SeqId(1, ' '); const gemmi::Position n(10, 10, 10), ca(11.46, 10, 10); const gemmi::Position c = place(gemmi::Position(10, 11, 10), n, ca, 1.52, 111, 180); const gemmi::Position o = place(n, ca, c, 1.23, 121, 0); const gemmi::Position cb = place(c, n, ca, 1.53, 110.5, 122.5); const gemmi::Position cg = place(n, ca, cb, 1.52, 114, chi1); const gemmi::Position cd = place(ca, cb, cg, 1.52, 111, chi2); const gemmi::Position ce = place(cb, cg, cd, 1.52, 111, chi3); const gemmi::Position nz = place(cg, cd, ce, 1.49, 112, chi4); const std::pair atoms[] = {{"N", n}, {"CA", ca}, {"C", c}, {"O", o}, {"CB", cb}, {"CG", cg}, {"CD", cd}, {"CE", ce}, {"NZ", nz}}; for (const auto &[name, pos] : atoms) { gemmi::Atom a; a.name = name; a.element = gemmi::Element(name[0] == 'N' ? gemmi::El::N : name[0] == 'O' ? gemmi::El::O : gemmi::El::C); a.pos = pos; a.occ = 1.0f; a.b_iso = 20.0f; r.atoms.push_back(a); } return r; } gemmi::Structure box(const gemmi::Residue &residue) { gemmi::Structure st; st.cell = gemmi::UnitCell(EDGE, EDGE, EDGE, 90, 90, 90); st.spacegroup_hm = "P 21 21 21"; st.setup_cell_images(); st.models.emplace_back(1); st.models[0].chains.emplace_back("A"); st.models[0].chains[0].residues.push_back(residue); return st; } // The reflections of the box to D_MIN, one per orbit, every one a working reflection, at D = 1 and a small variance. FieldReflections reflections(const gemmi::Structure &st) { const gemmi::SpaceGroup &sg = *st.find_spacegroup(); const gemmi::GroupOps gops = sg.operations(); const gemmi::ReciprocalAsu asu(&sg); FieldReflections fr; const int hmax = static_cast(EDGE / D_MIN); for (int h = -hmax; h <= hmax; h++) for (int k = -hmax; k <= hmax; k++) for (int l = -hmax; l <= hmax; l++) { const gemmi::Miller hkl{h, k, l}; const double inv_d2 = st.cell.calculate_1_d2(hkl); if ((h == 0 && k == 0 && l == 0) || inv_d2 > 1.0 / (D_MIN * D_MIN) || !asu.is_in(hkl)) continue; fr.hkl.push_back({static_cast(h), static_cast(k), static_cast(l)}); fr.stol2.push_back(static_cast(inv_d2 / 4.0)); fr.fo.push_back(0); fr.d.push_back(1.0f); fr.var.push_back(4.0f); fr.centric.push_back(gops.is_reflection_centric(hkl)); fr.work.push_back(1); fr.k_total.push_back(1.0f); fr.fmask.push_back(0); } return fr; } std::vector elements() { return {gemmi::IT92::get(gemmi::El::C, 0).coefs, gemmi::IT92::get(gemmi::El::N, 0).coefs, gemmi::IT92::get(gemmi::El::O, 0).coefs}; } int element_index(gemmi::El el) { return el == gemmi::El::C ? 0 : el == gemmi::El::N ? 1 : 2; } // Every atom of the residue as a state. std::vector all_atoms(const gemmi::Structure &st) { std::vector out; for (const gemmi::Atom &a : st.models[0].chains[0].residues[0].atoms) { const gemmi::Fractional f = st.cell.fractionalize(a.pos); out.push_back({static_cast(f.x), static_cast(f.y), static_cast(f.z), a.b_iso, a.occ, element_index(a.element.elem)}); } return out; } bool gpu_present() { #ifdef JFJOCH_USE_CUDA int n = 0; return cudaGetDeviceCount(&n) == cudaSuccess && n > 0; #else return false; #endif } } // namespace TEST_CASE("SideChainField_direct_sum_matches_gemmi", "[Refinement]") { const gemmi::Structure st = box(lysine(-60, 180, 180, 60)); const FieldReflections fr = reflections(st); REQUIRE(fr.Size() > 500); FieldStates states; states.Add(all_atoms(st)); auto cpu = MakeSideChainFieldEvaluatorCPU(fr, FieldOperators(*st.find_spacegroup()), elements(), 2); const std::vector> f = cpu->Contribution(states, 0); gemmi::StructureFactorCalculator> calc(st.cell); double num = 0, den = 0; for (size_t m = 0; m < fr.Size(); m++) { const gemmi::Miller hkl{static_cast(fr.hkl[m][0]), static_cast(fr.hkl[m][1]), static_cast(fr.hkl[m][2])}; const std::complex ref = calc.calculate_sf_from_model(st.models[0], hkl); num += std::norm(std::complex(f[m]) - ref); den += std::norm(ref); } CHECK(std::sqrt(num / den) < 1e-4); if (gpu_present()) { #ifdef JFJOCH_USE_CUDA auto gpu = MakeSideChainFieldEvaluatorGPU(0, fr, FieldOperators(*st.find_spacegroup()), elements()); const std::vector> g = gpu->Contribution(states, 0); double gnum = 0; for (size_t m = 0; m < fr.Size(); m++) gnum += std::norm(std::complex(f[m]) - std::complex(g[m])); CHECK(std::sqrt(gnum / den) < 1e-4); #endif } } TEST_CASE("SideChainField_recovers_the_rotamer_the_data_hold", "[Refinement]") { // the data: |F| of the lysine at one rotamer; the model starts at another const gemmi::Structure truth = box(lysine(180, 180, 180, 180)); FieldReflections fr = reflections(truth); FieldStates truth_state; truth_state.Add(all_atoms(truth)); const std::vector ff = elements(); const std::vector> ops = FieldOperators(*truth.find_spacegroup()); { auto e = MakeSideChainFieldEvaluatorCPU(fr, ops, ff, 2); const std::vector> f = e->Contribution(truth_state, 0); for (size_t m = 0; m < fr.Size(); m++) fr.fo[m] = std::abs(f[m]); } const gemmi::Structure start = box(lysine(-60, 180, 60, 180)); const std::vector element_of = {1, 0, 0, 2, 0, 0, 0, 0, 1}; const std::vector refinable(9, 1); auto run = [&](SideChainFieldEvaluator &e) { FieldStates s; s.Add(all_atoms(start)); e.SetTotal(e.Contribution(s, 0)); return OptimiseSideChainField(start, element_of, refinable, {}, 1.0, fr.Size(), e); }; auto cpu = MakeSideChainFieldEvaluatorCPU(fr, ops, ff, 2); const SideChainFieldResult r = run(*cpu); REQUIRE(r.candidates == 1); REQUIRE(r.moves.size() == 1); REQUIRE(!r.moves[0].absent); REQUIRE(r.moves[0].chi_after.size() == 4); for (double chi : r.moves[0].chi_after) CHECK(std::abs(std::remainder(chi - 180.0, 360.0)) < 1.0); CHECK(r.energy_after < r.energy_before); if (gpu_present()) { #ifdef JFJOCH_USE_CUDA auto gpu = MakeSideChainFieldEvaluatorGPU(0, fr, ops, ff); const SideChainFieldResult g = run(*gpu); REQUIRE(g.moves.size() == 1); CHECK(g.moves[0].chi_after == r.moves[0].chi_after); CHECK(g.energy_after == Catch::Approx(r.energy_after).epsilon(1e-4)); // on every card, the reflections split among them: the same move, the energy summed in another order int n = 0; cudaGetDeviceCount(&n); std::vector cards; for (int k = 0; k < n; k++) cards.push_back(k); auto split = MakeSideChainFieldEvaluatorGPUs(cards, fr, ops, ff); const SideChainFieldResult s = run(*split); REQUIRE(s.moves.size() == 1); CHECK(s.moves[0].chi_after == g.moves[0].chi_after); CHECK(s.energy_after == Catch::Approx(g.energy_after).epsilon(1e-9)); #endif } }