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

Fix charge ordering in .prmtop files #1045

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Dates are given in YYYY-MM-DD format.

Please note that all releases prior to a version 1.0.0 are considered pre-releases and many API changes will come before a stable release.

## 0.3.x

* #1045 Fixes a bug in which charges were sometimes misordered in `.prmtop` files.

## 0.3.30 - 2024-08

* #1039 Updates support of "cutoff" electrostatics in `.to_openmm` to better reflect what OpenMM supports. Set `"reaction-field"` to force the use of `CutoffPeriodic`, provided the vdW and electrostatic cutoff distances match. The potential/method `"cutoff"` is no longer supported but may be re-added in the future.
Expand Down
20 changes: 20 additions & 0 deletions openff/interchange/_tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,23 @@ def test_issue_1031(monkeypatch):
# check a few atom names to ensure these didn't end up being empty sets
for atom_name in ("NE2", "H3", "HA", "CH3", "CA", "CB", "CE1"):
assert atom_name in openff_atom_names


def test_issue_1033(water, sage):
"""Test issue/PR 1033, in which charges were not ordered topologically."""
pytest.importorskip("parmed")

interchange = sage.create_interchange(
Topology.from_molecules(3 * [water]),
)

interchange.to_prmtop("test.prmtop")

structure = parmed.load_file("test.prmtop")

# atoms should be ordered OHH OHH OHH, charges should be as well
assert 3 * [
(8, -0.834),
(1, 0.417),
(1, 0.417),
] == [(atom.atomic_number, atom.charge) for atom in structure.atoms]
12 changes: 9 additions & 3 deletions openff/interchange/interop/amber/export/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
UnsupportedExportError,
UnsupportedMixingRuleError,
)
from openff.interchange.models import PotentialKey
from openff.interchange.models import PotentialKey, TopologyKey


def _write_text_blob(file, blob):
Expand Down Expand Up @@ -514,9 +514,15 @@ def to_prmtop(interchange: "Interchange", file_path: Path | str):
_write_text_blob(prmtop, text_blob)

prmtop.write("%FLAG CHARGE\n" "%FORMAT(5E16.8)\n")
electro = interchange["Electrostatics"]
charges = [
charge.m_as(unit.e) * AMBER_COULOMBS_CONSTANT
for charge in interchange["Electrostatics"].charges.values()
charge * AMBER_COULOMBS_CONSTANT
for charge in [
electro.charges[TopologyKey(atom_indices=(index,))].m_as(
unit.elementary_charge,
)
for index in range(interchange.topology.n_atoms)
]
]
text_blob = "".join([f"{val:16.8E}" for val in charges])
_write_text_blob(prmtop, text_blob)
Expand Down
Loading