diff --git a/corelib/src/libs/SireBase/sparsematrix.hpp b/corelib/src/libs/SireBase/sparsematrix.hpp index 211d028b2..550294854 100644 --- a/corelib/src/libs/SireBase/sparsematrix.hpp +++ b/corelib/src/libs/SireBase/sparsematrix.hpp @@ -33,8 +33,8 @@ #include "sireglobal.h" #include "SireStream/datastream.h" -#include "SireStream/shareddatastream.h" #include "SireStream/magic_error.h" +#include "SireStream/shareddatastream.h" #include "SireError/errors.h" @@ -109,7 +109,7 @@ namespace SireBase inline uint qHash(const Index &idx) { - return (idx.i << 16) & (idx.j | 0x0000FFFF); + return ::qHash((quint64(idx.i) << 32) | idx.j); } } // end of namespace detail diff --git a/corelib/src/libs/SireIO/biosimspace.cpp b/corelib/src/libs/SireIO/biosimspace.cpp index 02be18b9c..9c1331e33 100644 --- a/corelib/src/libs/SireIO/biosimspace.cpp +++ b/corelib/src/libs/SireIO/biosimspace.cpp @@ -29,6 +29,8 @@ #include +#include + #include "SireBase/getinstalldir.h" #include "SireError/errors.h" @@ -1781,34 +1783,107 @@ namespace SireIO // this is a no-op. For force fields with non-default per-pair values // (e.g. GLYCAM funct=2 (1,1) instead of global sf14 for 1-4 pairs) // it replaces the base value with the correct per-pair value. - auto patch = [&](const CLJNBPairs &nb, CLJNBPairs &nb_merged, - const QHash &mapping) + const CLJScaleFactor unscaled(1, 1); + + // Visit each atom pair that isn't (1,1), since only these can differ. + auto visit_scaled = [&](const CLJNBPairs &pairs, const std::function &visit) { - // Iterate only over the mapped atoms rather than all atoms in nb. - // This is O(k²) in the number of mapped atoms k, which is always - // ≤ nb.nAtoms() and can be much smaller. - const QList keys = mapping.keys(); - const int k = keys.size(); + const auto &info = pairs.info(); + const int ncg = info.nCutGroups(); + + auto atom = [&](int cg, int i) + { return info.atomIdx(CGAtomIdx(CGIdx(cg), SireID::Index(i))).value(); }; - for (int i = 0; i < k; ++i) + for (int icg = 0; icg < ncg; ++icg) { - const AtomIdx ai = keys.at(i); - const AtomIdx merged_ai = mapping.value(ai); + const int nats0 = info.nAtoms(CGIdx(icg)); - for (int j = i; j < k; ++j) + for (int jcg = icg; jcg < ncg; ++jcg) { - const AtomIdx aj = keys.at(j); - const AtomIdx merged_aj = mapping.value(aj); + const auto &block = pairs.get(CGIdx(icg), CGIdx(jcg)); - const CLJScaleFactor nb_sf = nb.get(ai, aj); - const CLJScaleFactor base_sf = nb_merged.get(merged_ai, merged_aj); + if (block.defaultValue() == unscaled) + { + for (const auto &element : block.nonDefaultElements()) + { + const int i = std::get<0>(element); + const int j = std::get<1>(element); - if (nb_sf.coulomb() != base_sf.coulomb() or nb_sf.lj() != base_sf.lj()) - nb_merged.set(merged_ai, merged_aj, nb_sf); + if ((icg != jcg or j >= i) and std::get<2>(element) != unscaled) + visit(atom(icg, i), atom(jcg, j)); + } + + continue; + } + + const int nats1 = info.nAtoms(CGIdx(jcg)); + + for (int i = 0; i < nats0; ++i) + { + for (int j = (icg == jcg ? i : 0); j < nats1; ++j) + { + if (block.get(i, j) != unscaled) + visit(atom(icg, i), atom(jcg, j)); + } + } } } }; + auto patch = [&](const CLJNBPairs &nb, CLJNBPairs &nb_merged, + const QHash &mapping) + { + const auto &info = nb.info(); + const auto &merged_info = nb_merged.info(); + + const int nats = info.nAtoms(); + const int merged_nats = merged_info.nAtoms(); + + QVector to_merged(nats, -1); + QVector from_merged(merged_nats, -1); + + for (auto it = mapping.constBegin(); it != mapping.constEnd(); ++it) + { + const int atom = it.key().map(nats); + const int merged_atom = it.value().map(merged_nats); + + to_merged[atom] = merged_atom; + from_merged[merged_atom] = atom; + } + + auto check_pair = [&](int atom0, int atom1) + { + const int merged0 = to_merged[atom0]; + const int merged1 = to_merged[atom1]; + + if (merged0 < 0 or merged1 < 0) + return; + + const auto &cgatom0 = info.cgAtomIdx(AtomIdx(atom0)); + const auto &cgatom1 = info.cgAtomIdx(AtomIdx(atom1)); + const auto &merged_cgatom0 = merged_info.cgAtomIdx(AtomIdx(merged0)); + const auto &merged_cgatom1 = merged_info.cgAtomIdx(AtomIdx(merged1)); + + const CLJScaleFactor nb_sf = nb.get(cgatom0, cgatom1); + + if (nb_sf != nb_merged.get(merged_cgatom0, merged_cgatom1)) + nb_merged.set(merged_cgatom0, merged_cgatom1, nb_sf); + }; + + // Take a copy so the scan isn't affected by the patching. + const CLJNBPairs base = nb_merged; + + visit_scaled(nb, check_pair); + + visit_scaled(base, [&](int merged0, int merged1) + { + const int atom0 = from_merged[merged0]; + const int atom1 = from_merged[merged1]; + + if (atom0 >= 0 and atom1 >= 0) + check_pair(atom0, atom1); }); + }; + patch(nb0, intra0, mol0_merged_mapping); patch(nb1, intra1, mol1_merged_mapping); diff --git a/corelib/src/libs/SireIO/biosimspace.h b/corelib/src/libs/SireIO/biosimspace.h index de7dc83e5..28ded3d6a 100644 --- a/corelib/src/libs/SireIO/biosimspace.h +++ b/corelib/src/libs/SireIO/biosimspace.h @@ -376,13 +376,11 @@ namespace SireIO Vector cross(const Vector &v0, const Vector &v1); - //! Merge the CLJNBPairs (intrascale) of two molecules into a perturbable - /*! merged molecule's end-state intrascales. + //! Patch the connectivity-derived end-state intrascales of a merged + /*! molecule with the per-pair scale factors of the original molecules. - Expands nb0 from molecule0's atom index space into the merged - molecule's space (preserving actual per-pair scale factors, including - force-field-specific overrides such as GLYCAM funct=2 pairs), then - calls CLJNBPairs::merge to produce intrascale0 and intrascale1. + Pairs in intra0 (intra1) that differ from nb0 (nb1) take the original + value. Only pairs that aren't (1,1) in at least one matrix are checked. \param nb0 The CLJNBPairs for molecule0 in its original atom index space. diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index d2a29466d..50cd54980 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -231,6 +231,14 @@ organisation on `GitHub `__. anchor, keeping a hard Coulomb interaction at intermediate lambda. Overlapping vanishing and appearing groups could collapse onto each other and crash. +* Sped up ``SireIO::patchIntrascale`` by only checking atom pairs with non-default scale + factors, rather than every pair of atoms. This makes it practical for merges of large + molecules such as proteins. + +* Fixed the ``SparseMatrix`` hash function returning 0 for almost every key, which put + all entries in a single bucket and made lookups and inserts linear in the number of + entries. This slowed ``CLJNBPairs`` operations on molecules with large CutGroups. + `2026.2.0 `__ - September 2026 ---------------------------------------------------------------------------------------------- diff --git a/tests/io/test_patch_intrascale.py b/tests/io/test_patch_intrascale.py new file mode 100644 index 000000000..1794b596b --- /dev/null +++ b/tests/io/test_patch_intrascale.py @@ -0,0 +1,53 @@ +from sire.legacy.IO import patchIntrascale +from sire.legacy.MM import CLJNBPairs, CLJScaleFactor +from sire.legacy.Mol import AtomIdx + + +def _same(a, b): + return a.coulomb() == b.coulomb() and a.lj() == b.lj() + + +def test_patch_intrascale(ala_mols): + mol = ala_mols[0] + n = mol.num_atoms() + + # Pairs scaled in only one of nb and base, so that both scans are needed. + nb = mol.property("intrascale") + pair14 = next( + (AtomIdx(i), AtomIdx(j)) + for i in range(n) + for j in range(i + 1, n) + if _same(nb.get(AtomIdx(i), AtomIdx(j)), CLJScaleFactor(0.5 / 0.6, 0.5)) + ) + nb.set(*pair14, CLJScaleFactor(1, 1)) + + base = CLJNBPairs(mol.property("connectivity"), CLJScaleFactor(0.5 / 0.6, 0.5)) + far = (AtomIdx(0), AtomIdx(n - 1)) + assert _same(base.get(*far), CLJScaleFactor(1, 1)) + base.set(*far, CLJScaleFactor(0.3, 0.7)) + + # Leave the last few atoms unmapped. + nmapped = n - 3 + + far_mapped = (AtomIdx(0), AtomIdx(nmapped - 1)) + assert _same(nb.get(*far_mapped), CLJScaleFactor(1, 1)) + assert _same(base.get(*far_mapped), CLJScaleFactor(1, 1)) + nb.set(*far_mapped, CLJScaleFactor(0.4, 0.6)) + mapping = {AtomIdx(i): AtomIdx(i) for i in range(nmapped)} + + full = {AtomIdx(i): AtomIdx(i) for i in range(n)} + patched, patched_full = patchIntrascale(nb, nb, base, base, mapping, full) + + for i in range(n): + for j in range(i, n): + ai, aj = AtomIdx(i), AtomIdx(j) + + assert _same(patched_full.get(ai, aj), nb.get(ai, aj)) + + if i < nmapped and j < nmapped: + assert _same(patched.get(ai, aj), nb.get(ai, aj)) + else: + assert _same(patched.get(ai, aj), base.get(ai, aj)) + + assert _same(patched.get(*pair14), CLJScaleFactor(1, 1)) + assert _same(patched.get(*far_mapped), CLJScaleFactor(0.4, 0.6))