Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Add atom type merging for Interchange.to_gromacs() #962

Closed
wants to merge 29 commits into from
Closed
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
20aadef
Add atom type merging for Interchange.to_gromacs()
pbuslaev Apr 9, 2024
36fcd52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2024
c98e14e
Update openff/interchange/interop/gromacs/export/_export.py
pbuslaev Apr 15, 2024
6954a9a
Update openff/interchange/interop/gromacs/export/_export.py
pbuslaev Apr 15, 2024
4ddc1e0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
e38de61
Update openff/interchange/interop/gromacs/export/_export.py
pbuslaev Apr 15, 2024
f8907ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
b85edc7
Update _export.py
pbuslaev Apr 15, 2024
817141c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
2070603
Added tests to merging atomtypes. Added flag for merging atomtypes to…
pbuslaev Apr 15, 2024
185d88d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
85bff31
Fixed typo
pbuslaev Apr 15, 2024
3d071f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
2b8fec9
Fixed typo
pbuslaev Apr 15, 2024
09c44bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2024
ff52df3
Update openff/interchange/_tests/unit_tests/interop/gromacs/export/te…
pbuslaev Apr 16, 2024
66acca8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
9092e3f
Fixed imports
pbuslaev Apr 16, 2024
0f72fe5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
28171c8
Fixed smiles for testing
pbuslaev Apr 16, 2024
50b98d8
Fixed to_gromacs documentation
pbuslaev Apr 16, 2024
712ab1f
Added blank line to docs
pbuslaev Apr 16, 2024
daf225c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
363802e
Added merge_atom_types flag to get_gromac_energies
pbuslaev Apr 16, 2024
cd650ee
Fixed typo in tolerance
pbuslaev Apr 16, 2024
5948f36
Fixed quantity comparison
pbuslaev Apr 16, 2024
fe3cd93
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
51c9465
Added underscore to all public methods with merge_atom_types flag
pbuslaev Apr 22, 2024
4531a75
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions openff/interchange/interop/gromacs/export/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def to_top(self):

with open(self.top_file, "w") as top:
self._write_defaults(top)
self._write_atomtypes(top)
to_reduced_atom_types = self._write_atomtypes(top)
pbuslaev marked this conversation as resolved.
Show resolved Hide resolved

self._write_moleculetypes(top)
self._write_moleculetypes(top, to_reduced_atom_types)

self._write_system(top)
self._write_molecules(top)
Expand All @@ -60,31 +60,71 @@ def _write_defaults(self, top):
f"{self.system.coul_14:8.6f}\n\n",
)

def _write_atomtypes(self, top):
def _write_atomtypes(self, top) -> dict:
pbuslaev marked this conversation as resolved.
Show resolved Hide resolved
top.write("[ atomtypes ]\n")
top.write(
";type, bondingtype, atomic_number, mass, charge, ptype, sigma, epsilon\n",
)

reduced_atom_types = []
to_reduced_atom_types = {}

def _is_atom_type_in_list(
atom_type, atom_type_list, diff: float = 1e-5
) -> bool | str:
"""
Checks if the atom type is already in list.
"""
for _at_name, _atom_type in atom_type_list:
if (
atom_type.atomic_number == _atom_type.atomic_number
and numpy.abs(atom_type.mass.m - _atom_type.mass.m) < diff
and numpy.abs(atom_type.sigma.m - _atom_type.sigma.m) < diff
and numpy.abs(atom_type.epsilon.m - _atom_type.epsilon.m) < diff
pbuslaev marked this conversation as resolved.
Show resolved Hide resolved
):
return _at_name
return False

def _get_new_entry_name(atom_type_list) -> str:
"""
Entry name for atom type to be added.
"""
if len(reduced_atom_types) > 0:
_previous_name = reduced_atom_types[-1][0]
_previous_idx = int(_previous_name.split("_")[1])
return f"AT_{_previous_idx+1}"
else:
return "AT_0"

for atom_type in self.system.atom_types.values():
if not isinstance(atom_type, LennardJonesAtomType):
raise NotImplementedError(
"Only Lennard-Jones atom types are currently supported.",
)

if _is_atom_type_in_list(atom_type, reduced_atom_types):
to_reduced_atom_types[atom_type.name] = _is_atom_type_in_list(
atom_type, reduced_atom_types
)
else:
_at_name = _get_new_entry_name(reduced_atom_types)
reduced_atom_types.append((_at_name, atom_type))
to_reduced_atom_types[atom_type.name] = _at_name

for atom_type_name, atom_type in reduced_atom_types:
top.write(
f"{atom_type.name :<11s}\t"
f"{atom_type_name :<11s}\t"
f"{atom_type.atomic_number :6d}\t"
f"{atom_type.mass.m :.16g}\t"
f"{atom_type.charge.m :.16f}\t"
f"{atom_type.particle_type :5s}\t"
f"{atom_type.sigma.m :.16g}\t"
f"{atom_type.epsilon.m :.16g}\n",
)

top.write("\n")
return to_reduced_atom_types

def _write_moleculetypes(self, top):
def _write_moleculetypes(self, top, to_reduced_atom_types):
for molecule_name, molecule_type in self.system.molecule_types.items():
top.write("[ moleculetype ]\n")

Expand All @@ -93,7 +133,7 @@ def _write_moleculetypes(self, top):
f"{molecule_type.nrexcl:10d}\n\n",
)

self._write_atoms(top, molecule_type)
self._write_atoms(top, molecule_type, to_reduced_atom_types)
self._write_pairs(top, molecule_type)
self._write_bonds(top, molecule_type)
self._write_angles(top, molecule_type)
Expand All @@ -104,14 +144,14 @@ def _write_moleculetypes(self, top):

top.write("\n")

def _write_atoms(self, top, molecule_type):
def _write_atoms(self, top, molecule_type, to_reduced_atom_types):
top.write("[ atoms ]\n")
top.write(";index, atom type, resnum, resname, name, cgnr, charge, mass\n")

for atom in molecule_type.atoms:
top.write(
f"{atom.index :6d} "
f"{atom.atom_type :6s}"
f"{to_reduced_atom_types[atom.atom_type] :6s}"
f"{atom.residue_index :8d} "
f"{atom.residue_name :8s} "
f"{atom.name :6s}"
Expand Down
Loading