diff --git a/openff/interchange/_tests/unit_tests/interop/gromacs/export/test_export.py b/openff/interchange/_tests/unit_tests/interop/gromacs/export/test_export.py index fbfcaca1..dea1935c 100644 --- a/openff/interchange/_tests/unit_tests/interop/gromacs/export/test_export.py +++ b/openff/interchange/_tests/unit_tests/interop/gromacs/export/test_export.py @@ -193,6 +193,29 @@ def test_atom_names_pdb(self): assert openmm_atom_names == pdb_atom_names + def test_residue_id_increment(self): + """Test that residue IDs increment properly for multiple molecules.""" + + mol1 = MoleculeWithConformer.from_smiles("CCO") + mol2 = MoleculeWithConformer.from_smiles("CCO") + + for atom in mol1.atoms: + atom.metadata["residue_name"] = "MOL1" + for atom in mol2.atoms: + atom.metadata["residue_name"] = "MOL2" + + interchange = Interchange.from_smirnoff( + ForceField("openff-2.0.0.offxml"), [mol1, mol2] + ) + interchange.to_gro("resid.gro") + + with open("resid.gro") as gro_file: + lines = gro_file.readlines() + residue_ids = {int(line[:5].strip()) for line in lines[2:-2]} + + # expecting residue IDs 1 and 2 for MOL1 and MOL2 respectively + assert residue_ids == {1, 2} + class TestGROMACS(_NeedsGROMACS): @pytest.mark.slow diff --git a/openff/interchange/interop/gromacs/export/_export.py b/openff/interchange/interop/gromacs/export/_export.py index e16f375b..f031d6ca 100644 --- a/openff/interchange/interop/gromacs/export/_export.py +++ b/openff/interchange/interop/gromacs/export/_export.py @@ -445,6 +445,7 @@ def _write_gro(self, gro, decimal: int): gro.write(f"{n_particles}\n") count = 0 + residue_id = 1 for molecule_name, molecule in self.system.molecule_types.items(): n_copies = self.system.molecules[molecule_name] @@ -456,7 +457,8 @@ def _write_gro(self, gro, decimal: int): f"%{decimal + 5}.{decimal}f" f"%{decimal + 5}.{decimal}f\n" % ( - atom.residue_index, # This needs to be looked up from a different data structure + residue_id = 1, + # (hidden: 1013) atom.residue_index, # This needs to be looked up from a different data structure atom.residue_name[:5], atom.name[:5], (count + 1) % 100000, @@ -467,6 +469,7 @@ def _write_gro(self, gro, decimal: int): ) count += 1 + residue_id += 1 if self.system.box is None: warnings.warn(