Skip to content

Commit fa4d09d

Browse files
authored
Merge pull request #390 from QCMM/gro_update
Update for load_one info in gro files
2 parents 08593d3 + 63658b7 commit fa4d09d

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

iodata/formats/gromacs.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def _helper_read_frame(lit: LineIterator) -> tuple:
9090
attypes = []
9191
pos = np.zeros((natoms, 3), np.float32)
9292
vel = np.zeros((natoms, 3), np.float32)
93+
has_velocities = True
9394
for i in range(natoms):
9495
line = next(lit)
9596
resnums.append(int(line[:5]))
@@ -99,11 +100,18 @@ def _helper_read_frame(lit: LineIterator) -> tuple:
99100
pos[i, 0] = float(words[0])
100101
pos[i, 1] = float(words[1])
101102
pos[i, 2] = float(words[2])
102-
vel[i, 0] = float(words[3])
103-
vel[i, 1] = float(words[4])
104-
vel[i, 2] = float(words[5])
103+
# velocities are optional in gro files; only parse if present
104+
if len(words) >= 6:
105+
vel[i, 0] = float(words[3])
106+
vel[i, 1] = float(words[4])
107+
vel[i, 2] = float(words[5])
108+
else:
109+
has_velocities = False
105110
pos *= nanometer # atom coordinates are in nanometers
106-
vel *= nanometer / picosecond
111+
if has_velocities:
112+
vel *= nanometer / picosecond
113+
else:
114+
vel = None
107115
# Read the cell line
108116
cell = np.zeros((3, 3), np.float32)
109117
words = next(lit).split()

iodata/periodic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@
144144

145145
sym2num: dict[str, int] = {value: key for key, value in num2sym.items()}
146146

147-
148147
# Labels used for bond types.
149148

150149
num2bond = {

iodata/test/data/water_no_vel.gro

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MD of 2 waters, t= 0.0
2+
6
3+
1WATER OW1 1 0.126 1.624 1.679
4+
1WATER HW2 2 0.190 1.661 1.747
5+
1WATER HW3 3 0.177 1.568 1.613
6+
2WATER OW1 4 1.275 0.053 0.622
7+
2WATER HW2 5 1.337 0.002 0.680
8+
2WATER HW3 6 1.326 0.120 0.568
9+
1.82060 1.82060 1.82060

iodata/test/test_gromacs.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828

2929
def test_load_water():
30-
# test gro file of one water
30+
"""Test gro file of one water."""
3131
with as_file(files("iodata.test.data").joinpath("water.gro")) as fn_gro:
3232
mol = load_one(str(fn_gro))
3333
check_water(mol)
@@ -48,10 +48,28 @@ def check_water(mol):
4848

4949

5050
def test_load_many():
51+
"""Test water gro file with two frames."""
5152
with as_file(files("iodata.test.data").joinpath("water2.gro")) as fn_gro:
5253
mols = list(load_many(str(fn_gro)))
5354
assert len(mols) == 2
5455
assert mols[0].extra["time"] == 0.0 * picosecond
5556
assert mols[1].extra["time"] == 1.0 * picosecond
5657
for mol in mols:
5758
check_water(mol)
59+
60+
61+
def test_load_without_velocities():
62+
"""A GRO file without velocity columns should set velocities to None."""
63+
with as_file(files("iodata.test.data").joinpath("water_no_vel.gro")) as fn_gro:
64+
mol = load_one(str(fn_gro))
65+
66+
# basic checks
67+
assert mol.title == "MD of 2 waters"
68+
assert mol.atcoords.shape == (6, 3)
69+
70+
# coordinates should match the ones in the file (compare in nm)
71+
assert_allclose(mol.atcoords[-1] / nanometer, [1.326, 0.120, 0.568])
72+
73+
# velocities should be present in extra but None
74+
assert "velocities" in mol.extra
75+
assert mol.extra["velocities"] is None

0 commit comments

Comments
 (0)