| 1 |
import importlib |
| 2 |
|
| 3 |
from mpi4py import MPI |
| 4 |
|
| 5 |
import dolfinx |
| 6 |
import numpy as np |
| 7 |
import pytest |
| 8 |
import ufl |
| 9 |
|
| 10 |
from io4dolfinx import FileMode, read_mesh, write_mesh |
| 11 |
|
| 12 |
|
| 13 |
def get_hdf5_version(): |
| 14 |
"""Get the HDF5 library version found on the system. |
| 15 |
|
| 16 |
Note: This function first tries to get the version via h5py. |
| 17 |
If h5py is not installed, it attempts to load the HDF5 shared library |
| 18 |
directly using ctypes to query the version. |
| 19 |
""" |
| 20 |
import ctypes |
| 21 |
from ctypes import byref, c_uint |
| 22 |
|
| 23 |
from packaging.version import parse as _v |
| 24 |
|
| 25 |
try: |
| 26 |
import h5py |
| 27 |
|
| 28 |
return _v(h5py.version.hdf5_version) |
| 29 |
except ImportError: |
| 30 |
try: |
| 31 |
# Try to load default HDF5 library names |
| 32 |
# If adios2 is already imported, the symbols might be globally available |
| 33 |
try: |
| 34 |
libhdf5 = ctypes.CDLL("libhdf5.so") |
| 35 |
except OSError: |
| 36 |
try: |
| 37 |
libhdf5 = ctypes.CDLL("libhdf5.dylib") # MacOS |
| 38 |
except OSError: |
| 39 |
print("\n[Method 2] Could not load libhdf5 shared library directly.") |
| 40 |
libhdf5 = None |
| 41 |
|
| 42 |
if libhdf5: |
| 43 |
major = c_uint() |
| 44 |
minor = c_uint() |
| 45 |
rel = c_uint() |
| 46 |
|
| 47 |
# Call the C function |
| 48 |
libhdf5.H5get_libversion(byref(major), byref(minor), byref(rel)) |
| 49 |
return _v(f"{major.value}.{minor.value}.{rel.value}") |
| 50 |
except Exception: |
| 51 |
pass |
| 52 |
|
| 53 |
except Exception: |
| 54 |
pass |
| 55 |
|
| 56 |
raise RuntimeError("Failed to get HDF5 version") |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
"backend, encoder, suffix", |
| 61 |
[ |
| 62 |
("adios2", "BP4", ".bp"), |
| 63 |
("adios2", "HDF5", ".h5"), |
| 64 |
("adios2", "BP5", ".bp"), |
| 65 |
("h5py", "HDF5", ".h5"), |
| 66 |
], |
| 67 |
) |
| 68 |
@pytest.mark.parametrize( |
| 69 |
"ghost_mode", [dolfinx.mesh.GhostMode.shared_facet, dolfinx.mesh.GhostMode.none] |
| 70 |
) |
| 71 |
@pytest.mark.parametrize("store_partition", [True, False]) |
| 72 |
def test_mesh_read_writer(backend, encoder, suffix, ghost_mode, tmp_path, store_partition): |
| 73 |
N = 7 |
| 74 |
|
| 75 |
if backend == "adios2" and encoder == "HDF5" and get_hdf5_version().major >= 2: |
| 76 |
pytest.skip("HDF5 version >= 2 is not supported due to ADIOS2 limitations.") |
| 77 |
try: |
| 78 |
importlib.import_module(backend) |
| 79 |
except ModuleNotFoundError: |
| 80 |
pytest.skip(f"{backend} not installed") |
| 81 |
|
| 82 |
# Consistent tmp dir across processes |
| 83 |
fname = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 84 |
file = fname / f"{backend}_mesh_{encoder}_{store_partition}" |
| 85 |
xdmf_file = fname / f"xdmf_mesh_{encoder}_{ghost_mode}_{store_partition}" |
| 86 |
|
| 87 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, N, N, N, ghost_mode=ghost_mode) |
| 88 |
|
| 89 |
backend_args = None |
| 90 |
if backend == "adios2": |
| 91 |
backend_args = {"engine": encoder} |
| 92 |
write_mesh( |
| 93 |
file.with_suffix(suffix), |
| 94 |
mesh, |
| 95 |
store_partition_info=store_partition, |
| 96 |
backend_args=backend_args, |
| 97 |
backend=backend, |
| 98 |
) |
| 99 |
|
| 100 |
mesh.comm.Barrier() |
| 101 |
with dolfinx.io.XDMFFile(mesh.comm, xdmf_file.with_suffix(".xdmf"), "w") as xdmf: |
| 102 |
xdmf.write_mesh(mesh) |
| 103 |
mesh.comm.Barrier() |
| 104 |
|
| 105 |
in_mesh = read_mesh( |
| 106 |
file.with_suffix(suffix), |
| 107 |
MPI.COMM_WORLD, |
| 108 |
ghost_mode=ghost_mode, |
| 109 |
read_from_partition=store_partition, |
| 110 |
backend_args=backend_args, |
| 111 |
backend=backend, |
| 112 |
) |
| 113 |
in_mesh.comm.Barrier() |
| 114 |
if store_partition: |
| 115 |
|
| 116 |
def compute_distance_matrix(points_A, points_B, tol=1e-12): |
| 117 |
points_A_e = np.expand_dims(points_A, 1) |
| 118 |
points_B_e = np.expand_dims(points_B, 0) |
| 119 |
distances = np.sum(np.square(points_A_e - points_B_e), axis=2) |
| 120 |
return distances < tol |
| 121 |
|
| 122 |
cell_map = mesh.topology.index_map(mesh.topology.dim) |
| 123 |
new_cell_map = in_mesh.topology.index_map(in_mesh.topology.dim) |
| 124 |
assert cell_map.size_local == new_cell_map.size_local |
| 125 |
assert cell_map.num_ghosts == new_cell_map.num_ghosts |
| 126 |
mesh.topology.create_connectivity(mesh.topology.dim, mesh.topology.dim) |
| 127 |
midpoints = dolfinx.mesh.compute_midpoints( |
| 128 |
mesh, |
| 129 |
mesh.topology.dim, |
| 130 |
np.arange(cell_map.size_local + cell_map.num_ghosts, dtype=np.int32), |
| 131 |
) |
| 132 |
in_mesh.topology.create_connectivity(in_mesh.topology.dim, in_mesh.topology.dim) |
| 133 |
new_midpoints = dolfinx.mesh.compute_midpoints( |
| 134 |
in_mesh, |
| 135 |
in_mesh.topology.dim, |
| 136 |
np.arange(new_cell_map.size_local + new_cell_map.num_ghosts, dtype=np.int32), |
| 137 |
) |
| 138 |
# Check that all points in owned by initial mesh is owned by the new mesh |
| 139 |
# (might be locally reordered) |
| 140 |
owned_distances = compute_distance_matrix( |
| 141 |
midpoints[: cell_map.size_local], new_midpoints[: new_cell_map.size_local] |
| 142 |
) |
| 143 |
np.testing.assert_allclose(np.sum(owned_distances, axis=1), 1) |
| 144 |
# Check that all points that are ghosted in original mesh is ghosted on the |
| 145 |
# same process in the new mesh |
| 146 |
ghost_distances = compute_distance_matrix( |
| 147 |
midpoints[cell_map.size_local :], new_midpoints[new_cell_map.size_local :] |
| 148 |
) |
| 149 |
np.testing.assert_allclose(np.sum(ghost_distances, axis=1), 1) |
| 150 |
|
| 151 |
mesh.comm.Barrier() |
| 152 |
|
| 153 |
with dolfinx.io.XDMFFile(mesh.comm, xdmf_file.with_suffix(".xdmf"), "r") as xdmf: |
| 154 |
mesh_xdmf = xdmf.read_mesh(ghost_mode=ghost_mode) |
| 155 |
|
| 156 |
for i in range(mesh.topology.dim + 1): |
| 157 |
mesh.topology.create_entities(i) |
| 158 |
mesh_xdmf.topology.create_entities(i) |
| 159 |
in_mesh.topology.create_entities(i) |
| 160 |
assert ( |
| 161 |
mesh_xdmf.topology.index_map(i).size_global == in_mesh.topology.index_map(i).size_global |
| 162 |
) |
| 163 |
|
| 164 |
# Check that integration over different entities are consistent |
| 165 |
measures = ( |
| 166 |
[ufl.ds, ufl.dx] if ghost_mode is dolfinx.mesh.GhostMode.none else [ufl.ds, ufl.dS, ufl.dx] |
| 167 |
) |
| 168 |
for measure in measures: |
| 169 |
form = dolfinx.fem.form(1 * measure(domain=in_mesh)) |
| 170 |
c_adios = dolfinx.fem.assemble_scalar(form) |
| 171 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 172 |
c_xdmf = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_xdmf))) |
| 173 |
assert np.isclose( |
| 174 |
in_mesh.comm.allreduce(c_adios, MPI.SUM), |
| 175 |
mesh.comm.allreduce(c_xdmf, MPI.SUM), |
| 176 |
) |
| 177 |
assert np.isclose( |
| 178 |
in_mesh.comm.allreduce(c_adios, MPI.SUM), |
| 179 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
@pytest.mark.parametrize( |
| 184 |
"backend, encoder, suffix", |
| 185 |
[("adios2", "BP4", ".bp"), ("adios2", "BP5", ".bp"), ("h5py", None, ".h5")], |
| 186 |
) |
| 187 |
@pytest.mark.parametrize( |
| 188 |
"ghost_mode", [dolfinx.mesh.GhostMode.shared_facet, dolfinx.mesh.GhostMode.none] |
| 189 |
) |
| 190 |
@pytest.mark.parametrize("store_partition", [True, False]) |
| 191 |
def test_timedep_mesh(encoder, backend, suffix, ghost_mode, tmp_path, store_partition): |
| 192 |
try: |
| 193 |
importlib.import_module(backend) |
| 194 |
except ModuleNotFoundError: |
| 195 |
pytest.skip(f"{backend} not installed") |
| 196 |
|
| 197 |
# Currently unsupported, unclear why ("HDF5", ".h5"), |
| 198 |
N = 13 |
| 199 |
# Consistent tmp dir across processes |
| 200 |
fname = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 201 |
file = fname / f"adios_time_dep_mesh_{encoder}" |
| 202 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, N, N, N, ghost_mode=ghost_mode) |
| 203 |
|
| 204 |
def u(x): |
| 205 |
return np.asarray([x[0] + 0.1 * np.sin(x[1]), 0.2 * np.cos(x[1]), x[2]]) |
| 206 |
|
| 207 |
backend_args = None |
| 208 |
if backend == "adios2": |
| 209 |
backend_args = {"engine": encoder} |
| 210 |
write_mesh( |
| 211 |
file.with_suffix(suffix), |
| 212 |
mesh, |
| 213 |
mode=FileMode.write, |
| 214 |
time=0.0, |
| 215 |
store_partition_info=store_partition, |
| 216 |
backend_args=backend_args, |
| 217 |
backend=backend, |
| 218 |
) |
| 219 |
delta_x = u(mesh.geometry.x.T).T |
| 220 |
mesh.geometry.x[:] += delta_x |
| 221 |
write_mesh( |
| 222 |
file.with_suffix(suffix), |
| 223 |
mesh, |
| 224 |
mode=FileMode.append, |
| 225 |
time=3.0, |
| 226 |
backend_args=backend_args, |
| 227 |
backend=backend, |
| 228 |
) |
| 229 |
mesh.geometry.x[:] -= delta_x |
| 230 |
|
| 231 |
mesh_first = read_mesh( |
| 232 |
file.with_suffix(suffix), |
| 233 |
MPI.COMM_WORLD, |
| 234 |
ghost_mode, |
| 235 |
time=0.0, |
| 236 |
read_from_partition=store_partition, |
| 237 |
backend_args=backend_args, |
| 238 |
backend=backend, |
| 239 |
) |
| 240 |
mesh_first.comm.Barrier() |
| 241 |
|
| 242 |
# Check that integration over different entities are consistent |
| 243 |
measures = [ufl.ds, ufl.dx] |
| 244 |
if ghost_mode == dolfinx.mesh.GhostMode.shared_facet: |
| 245 |
measures.append(ufl.dx) |
| 246 |
for measure in measures: |
| 247 |
# try: |
| 248 |
c_adios = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_first))) |
| 249 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 250 |
assert np.isclose( |
| 251 |
mesh_first.comm.allreduce(c_adios, MPI.SUM), |
| 252 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 253 |
) |
| 254 |
|
| 255 |
mesh.geometry.x[:] += delta_x |
| 256 |
mesh_second = read_mesh( |
| 257 |
file.with_suffix(suffix), |
| 258 |
MPI.COMM_WORLD, |
| 259 |
ghost_mode, |
| 260 |
time=3.0, |
| 261 |
read_from_partition=store_partition, |
| 262 |
backend_args=backend_args, |
| 263 |
backend=backend, |
| 264 |
) |
| 265 |
mesh_second.comm.Barrier() |
| 266 |
measures = [ufl.ds, ufl.dx] |
| 267 |
if ghost_mode == dolfinx.mesh.GhostMode.shared_facet: |
| 268 |
measures.append(ufl.dx) |
| 269 |
for measure in measures: |
| 270 |
c_adios = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_second))) |
| 271 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 272 |
assert np.isclose( |
| 273 |
mesh_second.comm.allreduce(c_adios, MPI.SUM), |
| 274 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 275 |
) |