| 1 |
from mpi4py import MPI |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
import pytest |
| 5 |
from packaging.version import parse as _v |
| 6 |
|
| 7 |
import io4dolfinx |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize("comm", [MPI.COMM_SELF, MPI.COMM_WORLD]) |
| 11 |
def test_read_write_attributes(comm, backend, tmp_path): |
| 12 |
if backend == "adios2": |
| 13 |
import adios2 |
| 14 |
|
| 15 |
if _v(np.__version__) >= _v("2.0.0") and _v(adios2.__version__) < _v("2.10.2"): |
| 16 |
pytest.skip(reason="Cannot use numpy>=2.0.0 and adios2<2.10.2") |
| 17 |
|
| 18 |
attributes1 = { |
| 19 |
"a": np.array([1, 2, 3], dtype=np.uint8), |
| 20 |
"b": np.array([4, 5], dtype=np.uint8), |
| 21 |
} |
| 22 |
attributes2 = { |
| 23 |
"c": np.array([6], dtype=np.uint8), |
| 24 |
"d": np.array([7, 8, 9, 10], dtype=np.float64), |
| 25 |
} |
| 26 |
fname = comm.bcast(tmp_path, root=0) |
| 27 |
fname = fname / "attributes" |
| 28 |
suffix = ".bp" if backend == "adios2" else ".h5" |
| 29 |
file = fname.with_suffix(suffix) |
| 30 |
# print(comm.size) |
| 31 |
|
| 32 |
io4dolfinx.write_attributes( |
| 33 |
comm=comm, filename=file, name="group1", attributes=attributes1, backend=backend |
| 34 |
) |
| 35 |
io4dolfinx.write_attributes( |
| 36 |
comm=comm, filename=file, name="group2", attributes=attributes2, backend=backend |
| 37 |
) |
| 38 |
loaded_attributes1 = io4dolfinx.read_attributes( |
| 39 |
comm=comm, filename=file, name="group1", backend=backend |
| 40 |
) |
| 41 |
loaded_attributes2 = io4dolfinx.read_attributes( |
| 42 |
comm=comm, filename=file, name="group2", backend=backend |
| 43 |
) |
| 44 |
|
| 45 |
for k, v in loaded_attributes1.items(): |
| 46 |
assert np.allclose(v, attributes1[k]) |
| 47 |
for k, v in attributes1.items(): |
| 48 |
assert np.allclose(v, loaded_attributes1[k]) |
| 49 |
|
| 50 |
for k, v in loaded_attributes2.items(): |
| 51 |
assert np.allclose(v, attributes2[k]) |
| 52 |
for k, v in attributes2.items(): |
| 53 |
assert np.allclose(v, loaded_attributes2[k]) |