Skip to content

Commit

Permalink
fix PBC bug
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Jun 21, 2024
1 parent 0fee63f commit a3904ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
24 changes: 24 additions & 0 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def test_datasets_h5py(tmp_path, dataset, request):
assert "observables/atoms/mlip_energy_2/value" in f
assert "observables/atoms/mlip_stress/value" in f

npt.assert_array_equal(
f["particles/atoms/box"].attrs["boundary"], ["none", "none", "none"]
)


def test_two_datasets(tmp_path, s22_all_properties, s22_mixed_pbc_cell):
io_a = znh5md.IO(tmp_path / "test.h5", particle_group="a")
Expand Down Expand Up @@ -110,3 +114,23 @@ def test_two_datasets_external(tmp_path, s22_all_properties, s22_mixed_pbc_cell)

assert len(io_a) == len(s22_all_properties)
assert len(io_b) == len(s22_mixed_pbc_cell)


def test_pbc(tmp_path, s22_mixed_pbc_cell):
io = znh5md.IO(tmp_path / "test.h5")
io.extend(s22_mixed_pbc_cell)

pbc = ["periodic" if x else "none" for x in io[0].pbc]

with h5py.File(tmp_path / "test.h5", "r") as f:
assert "/particles/atoms/position/value" in f
assert "/particles/atoms/box" in f

npt.assert_array_equal(f["particles/atoms/box"].attrs["boundary"], pbc)
for idx, atom in enumerate(s22_mixed_pbc_cell):
npt.assert_array_equal(
f["particles/atoms/box/edges/value"][idx], atom.get_cell()
)
# Do we want to rename "pbc" to "boundary" and make if "none" or "periodic" as well?
# This should only exist if requested
npt.assert_array_equal(f["particles/atoms/box/pbc/value"][idx], atom.pbc)
14 changes: 14 additions & 0 deletions tests/test_read_write.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ase
import numpy as np
import numpy.testing as npt
import pytest

Expand Down Expand Up @@ -33,3 +35,15 @@ def test_append_false(tmp_path, s22_all_properties):
znh5md.write(file, s22_all_properties)
with pytest.raises(FileExistsError):
znh5md.write(file, s22_all_properties, append=False)


def test_write_H(tmp_path):
n_atoms = 10
images = [
ase.Atoms("H" * n_atoms, positions=np.random.rand(n_atoms, 3))
for _ in range(10000)
]
file = tmp_path / "test.h5"
znh5md.write(file, images)
atoms = znh5md.read(file)
assert atoms == images[-1]
5 changes: 3 additions & 2 deletions znh5md/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ def _create_particle_group(self, f, data):
self._create_group(g_particle_grp, "position", data.positions, "Angstrom")
self._create_group(g_particle_grp, "box/edges", data.cell)
g_particle_grp["box"].attrs["dimension"] = 3
boundary = [["periodic" if y else "none" for y in x] for x in data.pbc]
g_particle_grp["box"].attrs["boundary"] = boundary
g_particle_grp["box"].attrs["boundary"] = [
"periodic" if y else "none" for y in data.pbc[0]
]

if self.pbc_group and data.pbc is not None:
self._create_group(g_particle_grp, "box/pbc", data.pbc)
Expand Down

0 comments on commit a3904ab

Please sign in to comment.