diff --git a/hoomd/md/pytest/test_hdf5.py b/hoomd/md/pytest/test_hdf5.py index 978cba6469..1cd639e9ad 100644 --- a/hoomd/md/pytest/test_hdf5.py +++ b/hoomd/md/pytest/test_hdf5.py @@ -153,3 +153,31 @@ def test_mode(tmp_path, create_md_sim): if sim.device.communicator.rank == 0: with h5py.File(fn, "r") as fh: assert len(fh["hoomd-data/foo/bar"]) == 2 + + +def test_type_handling(tmp_path, create_md_sim): + logger = hoomd.logging.Logger(categories=['scalar']) + sim = create_md_sim + fn = tmp_path / "types.h5" + loggables = { + int: lambda: 42, + float: lambda: 0.0, + bool: lambda: True, + np.uint32: lambda: np.uint32(42), + np.float32: lambda: np.float32(3.1415), + np.bool_: lambda: np.bool_(True) + } + for key, value in loggables.items(): + logger[str(key)] = (value, "scalar") + hdf5_writer = hoomd.write.HDF5Log(1, fn, logger, mode="w") + sim.operations.writers.append(hdf5_writer) + sim.run(1) + + rank = sim.device.communicator.rank + del sim + + if rank == 0: + with h5py.File(fn, "r") as fh: + for key in loggables: + type_ = key if key not in (float, int, bool) else np.dtype(key) + assert fh[f"hoomd-data/{str(key)}"].dtype == type_ diff --git a/hoomd/write/hdf5.py b/hoomd/write/hdf5.py index 01015a437a..a5e5de864b 100644 --- a/hoomd/write/hdf5.py +++ b/hoomd/write/hdf5.py @@ -209,7 +209,12 @@ def _initialize_datasets(self, log_dict): chunk_size = None if category == "scalar": data_shape = (1,) - dtype = "f8" if isinstance(value, float) else "i8" + if isinstance(value, (np.number, np.bool_)): + dtype = value.dtype + elif isinstance(value, int): + dtype = np.dtype(bool) if isinstance(value, bool) else "i8" + else: + dtype = "f8" chunk_size = (self._SCALAR_CHUNK,) else: if not isinstance(value, np.ndarray):