Skip to content

Commit 2076e56

Browse files
author
SimonBoothroyd
authored
Merge pull request #192 from openforcefield/openmm_warnings
Fix Packmol Unique Atom Names
2 parents 52489e9 + eb63029 commit 2076e56

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

evaluator/utils/packmol.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import string
1414
import subprocess
1515
import tempfile
16+
from collections import defaultdict
1617
from distutils.spawn import find_executable
1718
from functools import reduce
1819

@@ -113,7 +114,6 @@ def pack_box(
113114
raise ValueError("Either a `box_size` or `mass_density` must be specified.")
114115

115116
if box_size is not None and len(box_size) != 3:
116-
117117
raise ValueError("`box_size` must be a pint.Quantity wrapped list of length 3")
118118

119119
if mass_density is not None and box_aspect_ratio is None:
@@ -122,10 +122,11 @@ def pack_box(
122122
if box_aspect_ratio is not None:
123123

124124
assert len(box_aspect_ratio) == 3
125-
assert box_aspect_ratio[0] * box_aspect_ratio[1] * box_aspect_ratio[2] > 0
125+
assert all(x > 0.0 for x in box_aspect_ratio)
126126

127127
# noinspection PyTypeChecker
128128
if len(molecules) != len(number_of_copies):
129+
129130
raise ValueError(
130131
"Length of `molecules` and `number_of_copies` must be identical."
131132
)
@@ -488,6 +489,7 @@ def _create_pdb_and_topology(molecule, file_path):
488489
topology = Topology.from_molecules([molecule])
489490

490491
with tempfile.NamedTemporaryFile(mode="r+", suffix=".pdb") as pdb_file:
492+
491493
PDBFile.writeFile(topology.to_openmm(), molecule.conformers[0], pdb_file)
492494
pdb_file.flush()
493495
mdtraj_molecule = mdtraj.load_pdb(pdb_file.name)
@@ -534,6 +536,18 @@ def _create_pdb_and_topology(molecule, file_path):
534536
for atom in residue.atoms:
535537
atom.name = "Na+"
536538

539+
else:
540+
541+
# Assign unique atom names as the OpenFF toolkit does not.
542+
element_counter = defaultdict(int)
543+
544+
for atom in residue.atoms:
545+
546+
atom.name = (
547+
f"{atom.element.symbol}{element_counter[atom.element.symbol] + 1}"
548+
)
549+
element_counter[atom.element.symbol] += 1
550+
537551
for original_residue_name in residue_map:
538552

539553
if residue_map[original_residue_name] is not None:

0 commit comments

Comments
 (0)