|
| 1 | +import sire as sr |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +@pytest.mark.skipif( |
| 6 | + "openmm" not in sr.convert.supported_formats(), |
| 7 | + reason="openmm support is not available", |
| 8 | +) |
| 9 | +def test_openmm_cmap_energy(tmpdir, multichain_cmap, openmm_platform): |
| 10 | + """ |
| 11 | + Verify that Sire correctly adds CMAPTorsionForce to the OpenMM context by |
| 12 | + comparing the total potential energy against a context built directly via |
| 13 | + the OpenMM Python API from the same input files. |
| 14 | +
|
| 15 | + The multichain_cmap fixture is a periodic solvated system with three protein |
| 16 | + chains, each carrying CMAP backbone correction terms. Using it exercises the |
| 17 | + multi-molecule CMAP code path in the conversion layer. |
| 18 | + """ |
| 19 | + import openmm |
| 20 | + import openmm.app |
| 21 | + import openmm.unit |
| 22 | + |
| 23 | + mols = sr.system.System() |
| 24 | + mols.add(multichain_cmap[0]) |
| 25 | + mols.add(multichain_cmap[1]) |
| 26 | + mols.add(multichain_cmap[2]) |
| 27 | + |
| 28 | + # Sanity-check: at least two molecules must carry CMAP so that the |
| 29 | + # multi-chain code path is exercised. |
| 30 | + cmap_mol_count = sum(1 for mol in mols.molecules() if mol.has_property("cmap")) |
| 31 | + assert ( |
| 32 | + cmap_mol_count >= 2 |
| 33 | + ), "Expected at least two molecules with CMAP terms in multichain_cmap" |
| 34 | + |
| 35 | + # Save the Sire system to AMBER files so the direct OpenMM path reads the |
| 36 | + # same topology and coordinates that Sire uses internally. |
| 37 | + dir_path = str(tmpdir.mkdir("cmap_omm")) |
| 38 | + prm7 = str(sr.save(mols, f"{dir_path}/system.prm7")[0]) |
| 39 | + rst7 = str(sr.save(mols, f"{dir_path}/system.rst7")[0]) |
| 40 | + |
| 41 | + platform_name = openmm_platform or "CPU" |
| 42 | + |
| 43 | + # Create an OpenMM context via Sire's conversion layer, then get the |
| 44 | + # potential energy. |
| 45 | + sire_map = { |
| 46 | + "constraint": "none", |
| 47 | + "cutoff": "none", |
| 48 | + "cutoff_type": "none", |
| 49 | + "platform": platform_name, |
| 50 | + } |
| 51 | + omm_sire = sr.convert.to(mols, "openmm", map=sire_map) |
| 52 | + sire_energy = ( |
| 53 | + omm_sire.getState(getEnergy=True) |
| 54 | + .getPotentialEnergy() |
| 55 | + .value_in_unit(openmm.unit.kilojoules_per_mole) |
| 56 | + ) |
| 57 | + |
| 58 | + # Create an OpenMM context directly from the AMBER files and get the |
| 59 | + # potential energy. |
| 60 | + prmtop = openmm.app.AmberPrmtopFile(prm7) |
| 61 | + inpcrd = openmm.app.AmberInpcrdFile(rst7) |
| 62 | + |
| 63 | + omm_system = prmtop.createSystem( |
| 64 | + nonbondedMethod=openmm.app.NoCutoff, |
| 65 | + constraints=None, |
| 66 | + ) |
| 67 | + |
| 68 | + integrator = openmm.VerletIntegrator(0.001) |
| 69 | + platform = openmm.Platform.getPlatformByName(platform_name) |
| 70 | + omm_context = openmm.Context(omm_system, integrator, platform) |
| 71 | + omm_context.setPositions(inpcrd.positions) |
| 72 | + if inpcrd.boxVectors is not None: |
| 73 | + omm_context.setPeriodicBoxVectors(*inpcrd.boxVectors) |
| 74 | + |
| 75 | + direct_energy = ( |
| 76 | + omm_context.getState(getEnergy=True) |
| 77 | + .getPotentialEnergy() |
| 78 | + .value_in_unit(openmm.unit.kilojoules_per_mole) |
| 79 | + ) |
| 80 | + |
| 81 | + # Energies should agree to within 1 kJ/mol. |
| 82 | + assert sire_energy == pytest.approx(direct_energy, abs=1.0) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.skipif( |
| 86 | + "openmm" not in sr.convert.supported_formats(), |
| 87 | + reason="openmm support is not available", |
| 88 | +) |
| 89 | +def test_openmm_cmap_perturbable(multichain_cmap, openmm_platform): |
| 90 | + """ |
| 91 | + Verify that CMAPTorsionForce grids are correctly handled for a perturbable |
| 92 | + molecule. The pre-merged stream file merged_molecule_cmap.s3 contains a |
| 93 | + perturbable molecule whose two end states are identical (an identity |
| 94 | + perturbation of a CHARMM protein chain), so both end states carry the same |
| 95 | + CMAP backbone correction terms. The test checks that the perturbable code |
| 96 | + path correctly applies the same grids at all lambda values and that |
| 97 | + set_lambda does not corrupt the force parameters. |
| 98 | + """ |
| 99 | + import openmm |
| 100 | + |
| 101 | + platform_name = openmm_platform or "CPU" |
| 102 | + |
| 103 | + mol0 = multichain_cmap[0] |
| 104 | + |
| 105 | + mols_pert = sr.load_test_files("merged_molecule_cmap.s3") |
| 106 | + mols_pert = sr.morph.link_to_reference(mols_pert) |
| 107 | + |
| 108 | + omm_map = { |
| 109 | + "constraint": "none", |
| 110 | + "cutoff": "none", |
| 111 | + "cutoff_type": "none", |
| 112 | + "platform": platform_name, |
| 113 | + } |
| 114 | + |
| 115 | + def get_cmap_torsion_grids(context): |
| 116 | + """ |
| 117 | + Return list of (size, grid) for each CMAP torsion, dereferencing the |
| 118 | + map index. Grid values are returned as plain floats (kJ/mol) so that |
| 119 | + pytest.approx can compare them. This is map-count-agnostic: the |
| 120 | + non-perturbable path deduplicates maps while the perturbable path |
| 121 | + allocates one map per torsion, but the per-torsion grid values must |
| 122 | + agree. |
| 123 | + """ |
| 124 | + system = context.getSystem() |
| 125 | + for force in system.getForces(): |
| 126 | + if isinstance(force, openmm.CMAPTorsionForce): |
| 127 | + maps = [] |
| 128 | + for i in range(force.getNumMaps()): |
| 129 | + size, grid = force.getMapParameters(i) |
| 130 | + grid_floats = [ |
| 131 | + v.value_in_unit(openmm.unit.kilojoules_per_mole) for v in grid |
| 132 | + ] |
| 133 | + maps.append((size, grid_floats)) |
| 134 | + result = [] |
| 135 | + for t in range(force.getNumTorsions()): |
| 136 | + map_idx = force.getTorsionParameters(t)[0] |
| 137 | + result.append(maps[map_idx]) |
| 138 | + return result |
| 139 | + return [] |
| 140 | + |
| 141 | + def unique_grids(torsion_grids, decimals=3): |
| 142 | + """ |
| 143 | + Return the sorted set of unique (size, rounded-grid) tuples. |
| 144 | +
|
| 145 | + Torsion ordering can differ between the perturbable and non-perturbable |
| 146 | + code paths, so we compare the sets of unique grid shapes rather than |
| 147 | + comparing torsion-by-torsion. |
| 148 | + """ |
| 149 | + seen = set() |
| 150 | + result = [] |
| 151 | + for size, grid in torsion_grids: |
| 152 | + key = (size, tuple(round(v, decimals) for v in grid)) |
| 153 | + if key not in seen: |
| 154 | + seen.add(key) |
| 155 | + result.append(key) |
| 156 | + return sorted(result) |
| 157 | + |
| 158 | + # Reference: non-perturbable molecule. |
| 159 | + mols_ref = sr.system.System() |
| 160 | + mols_ref.add(mol0) |
| 161 | + omm_ref = sr.convert.to(mols_ref, "openmm", map=omm_map) |
| 162 | + ref_torsion_grids = get_cmap_torsion_grids(omm_ref) |
| 163 | + |
| 164 | + assert len(ref_torsion_grids) > 0, "Reference context has no CMAP torsions" |
| 165 | + ref_unique = unique_grids(ref_torsion_grids) |
| 166 | + |
| 167 | + # Perturbable context — one context, lambda changed in place. |
| 168 | + omm_pert = sr.convert.to(mols_pert, "openmm", map=omm_map) |
| 169 | + |
| 170 | + # At both lambda=0 and lambda=1 the set of unique CMAP grids must match the |
| 171 | + # non-perturbable reference (cmap0 == cmap1 for an identity perturbation). |
| 172 | + # We compare sets of unique grids because the perturbable and non-perturbable |
| 173 | + # code paths may order torsions differently. |
| 174 | + for lam in (0.0, 1.0): |
| 175 | + omm_pert.set_lambda(lam) |
| 176 | + pert_torsion_grids = get_cmap_torsion_grids(omm_pert) |
| 177 | + |
| 178 | + assert len(pert_torsion_grids) == len(ref_torsion_grids), ( |
| 179 | + f"Wrong number of CMAP torsions at lambda={lam}: " |
| 180 | + f"{len(pert_torsion_grids)} != {len(ref_torsion_grids)}" |
| 181 | + ) |
| 182 | + |
| 183 | + pert_unique = unique_grids(pert_torsion_grids) |
| 184 | + assert pert_unique == ref_unique, ( |
| 185 | + f"Set of unique CMAP grids differs from reference at lambda={lam}: " |
| 186 | + f"{len(pert_unique)} unique grids vs {len(ref_unique)} in reference" |
| 187 | + ) |
0 commit comments