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 parsing of box vector in H5MD reader #4076

Merged
merged 9 commits into from
Mar 27, 2023
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
1 change: 1 addition & 0 deletions package/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ Chronological list of authors
- Xu Hong Chen
- Domenico Marson
- Ahmed Salah Ghoneim
- Alexander Schlaich

External code
-------------
Expand Down
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ The rules for this file:

??/??/?? IAlibay, pgbarletta, mglagolev, hmacdope, manuel.nuno.melo, chrispfae,
ooprathamm, MeetB7, BFedder, v-parmar, MoSchaeffler, jbarnoud, jandom,
xhgchen, jaclark5, DrDomenicoMarson, AHMED-salah00
xhgchen, jaclark5, DrDomenicoMarson, AHMED-salah00, schlaicha
* 2.5.0

Fixes
* Fix parsing of box vector in H5MD reader (Issue #4076)
* Fix the misleading 'AtomGroup.guess_bonds()' docs and passed missing
arguments (PR #4059)
* Fix for NetCDF trajectories without `time` variable (Issue #4073)
Expand Down
15 changes: 13 additions & 2 deletions package/MDAnalysis/coordinates/H5MD.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ class H5MDReader(base.ReaderBase):
Adds :meth:`parse_n_atoms` method to obtain the number of atoms directly
from the trajectory by evaluating the shape of the ``position``,
``velocity``, or ``force`` groups.
.. versionchanged:: 2.5.0
Add correct handling of simple cuboid boxes

"""

Expand Down Expand Up @@ -640,8 +642,17 @@ def _read_frame(self, frame):

# Sets frame box dimensions
# Note: H5MD files must contain 'box' group in each 'particles' group
if 'edges' in particle_group['box']:
ts.dimensions = core.triclinic_box(*particle_group['box/edges/value'][frame, :])
if "edges" in particle_group["box"]:
edges = particle_group["box/edges/value"][frame, :]
# A D-dimensional vector or a D × D matrix, depending on the
# geometry of the box, of Float or Integer type. If edges is a
# vector, it specifies the space diagonal of a cuboid-shaped box.
# If edges is a matrix, the box is of triclinic shape with the edge
# vectors given by the rows of the matrix.
if edges.shape == (3,):
ts.dimensions = [*edges, 90, 90, 90]
else:
ts.dimensions = core.triclinic_box(*edges)
else:
ts.dimensions = None

Expand Down
12 changes: 12 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_h5md.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ def test_2D_box(self, h5md_file, outfile):
match="MDAnalysis only supports 3-dimensional"):
u = mda.Universe(TPR_xvf, outfile)

def test_box_vector(self, h5md_file, outfile):
with h5md_file as f:
with h5py.File(outfile, 'w') as g:
f.copy(source='particles', dest=g)
f.copy(source='h5md', dest=g)
vector = [1, 2, 3]
del g['particles/trajectory/box/edges']
g['particles/trajectory/box/edges/value'] = [vector, vector, vector]
u = mda.Universe(TPR_xvf, outfile)
# values in vector are conveted from nm -> Angstrom
assert_equal(u.trajectory.ts.dimensions, [10, 20, 30, 90, 90, 90])

def test_no_box(self, h5md_file, outfile):
with h5md_file as f:
with h5py.File(outfile, 'w') as g:
Expand Down