| 1 |
import typing |
| 2 |
from collections import ChainMap |
| 3 |
|
| 4 |
from mpi4py import MPI |
| 5 |
|
| 6 |
import dolfinx |
| 7 |
import ipyparallel as ipp |
| 8 |
import numpy as np |
| 9 |
import numpy.typing |
| 10 |
import numpy.typing as npt |
| 11 |
import pytest |
| 12 |
|
| 13 |
import io4dolfinx |
| 14 |
|
| 15 |
|
| 16 |
def find_backends(): |
| 17 |
backends = [] |
| 18 |
try: |
| 19 |
import adios2 |
| 20 |
|
| 21 |
if adios2.is_built_with_mpi: |
| 22 |
backends.append("adios2") |
| 23 |
except ModuleNotFoundError: |
| 24 |
pass |
| 25 |
try: |
| 26 |
import h5py |
| 27 |
|
| 28 |
if h5py.get_config().mpi: |
| 29 |
backends.append("h5py") |
| 30 |
except ModuleNotFoundError: |
| 31 |
pass |
| 32 |
return backends |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture(params=find_backends(), scope="function") |
| 36 |
def backend(request): |
| 37 |
yield request.param |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture(scope="module") |
| 41 |
def cluster(): |
| 42 |
cluster = ipp.Cluster(engines="mpi", n=2) |
| 43 |
rc = cluster.start_and_connect_sync() |
| 44 |
rc.wait_for_engines(n=2) |
| 45 |
yield rc |
| 46 |
cluster.stop_cluster_sync() |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(scope="function") |
| 50 |
def write_function(tmp_path): |
| 51 |
def _write_function( |
| 52 |
mesh, |
| 53 |
el, |
| 54 |
f, |
| 55 |
dtype, |
| 56 |
backend: typing.Literal["adios2", "h5py"], |
| 57 |
name="uh", |
| 58 |
append: bool = False, |
| 59 |
) -> str: |
| 60 |
V = dolfinx.fem.functionspace(mesh, el) |
| 61 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 62 |
uh.interpolate(f) |
| 63 |
uh.name = name |
| 64 |
el_hash = ( |
| 65 |
io4dolfinx.utils.element_signature(V) |
| 66 |
.replace(" ", "") |
| 67 |
.replace(",", "") |
| 68 |
.replace("(", "") |
| 69 |
.replace(")", "") |
| 70 |
.replace("[", "") |
| 71 |
.replace("]", "") |
| 72 |
) |
| 73 |
# Consistent tmp dir across processes |
| 74 |
f_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 75 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 76 |
if backend == "adios2": |
| 77 |
suffix = ".bp" |
| 78 |
else: |
| 79 |
suffix = ".h5" |
| 80 |
|
| 81 |
filename = (f_path / f"mesh_{file_hash}").with_suffix(suffix) |
| 82 |
if mesh.comm.size != 1: |
| 83 |
if not append: |
| 84 |
io4dolfinx.write_mesh(filename, mesh, backend=backend) |
| 85 |
io4dolfinx.write_function(filename, uh, time=0.0, backend=backend) |
| 86 |
else: |
| 87 |
if MPI.COMM_WORLD.rank == 0: |
| 88 |
if not append: |
| 89 |
io4dolfinx.write_mesh(filename, mesh, backend=backend) |
| 90 |
io4dolfinx.write_function(filename, uh, time=0.0, backend=backend) |
| 91 |
|
| 92 |
return filename |
| 93 |
|
| 94 |
return _write_function |
| 95 |
|
| 96 |
|
| 97 |
@pytest.fixture(scope="function") |
| 98 |
def read_function(): |
| 99 |
def _read_function( |
| 100 |
comm, el, f, path, dtype, backend: typing.Literal["adios2", "h5py"], name="uh" |
| 101 |
): |
| 102 |
mesh = io4dolfinx.read_mesh( |
| 103 |
path, |
| 104 |
comm, |
| 105 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 106 |
backend=backend, |
| 107 |
) |
| 108 |
V = dolfinx.fem.functionspace(mesh, el) |
| 109 |
v = dolfinx.fem.Function(V, dtype=dtype) |
| 110 |
v.name = name |
| 111 |
io4dolfinx.read_function(path, v, backend=backend) |
| 112 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 113 |
v_ex.interpolate(f) |
| 114 |
|
| 115 |
res = np.finfo(dtype).resolution |
| 116 |
np.testing.assert_allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 117 |
|
| 118 |
return _read_function |
| 119 |
|
| 120 |
|
| 121 |
@pytest.fixture(scope="function") |
| 122 |
def get_dtype(): |
| 123 |
def _get_dtype(in_dtype: np.dtype, is_complex: bool): |
| 124 |
dtype: numpy.typing.DTypeLike |
| 125 |
if in_dtype == np.float32: |
| 126 |
if is_complex: |
| 127 |
dtype = np.complex64 |
| 128 |
else: |
| 129 |
dtype = np.float32 |
| 130 |
elif in_dtype == np.float64: |
| 131 |
if is_complex: |
| 132 |
dtype = np.complex128 |
| 133 |
else: |
| 134 |
dtype = np.float64 |
| 135 |
else: |
| 136 |
raise ValueError("Unsuported dtype") |
| 137 |
return dtype |
| 138 |
|
| 139 |
return _get_dtype |
| 140 |
|
| 141 |
|
| 142 |
@pytest.fixture(scope="function") |
| 143 |
def write_function_time_dep(tmp_path): |
| 144 |
def _write_function_time_dep( |
| 145 |
mesh, el, f0, f1, t0, t1, dtype, backend: typing.Literal["adios2", "h5py"] |
| 146 |
) -> str: |
| 147 |
V = dolfinx.fem.functionspace(mesh, el) |
| 148 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 149 |
uh.interpolate(f0) |
| 150 |
el_hash = ( |
| 151 |
io4dolfinx.utils.element_signature(V) |
| 152 |
.replace(" ", "") |
| 153 |
.replace(",", "") |
| 154 |
.replace("(", "") |
| 155 |
.replace(")", "") |
| 156 |
.replace("[", "") |
| 157 |
.replace("]", "") |
| 158 |
) |
| 159 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 160 |
# Consistent tmp dir across processes |
| 161 |
f_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 162 |
if backend == "adios2": |
| 163 |
suffix = ".bp" |
| 164 |
else: |
| 165 |
suffix = ".h5" |
| 166 |
filename = (f_path / f"mesh_{file_hash}").with_suffix(suffix) |
| 167 |
if mesh.comm.size != 1: |
| 168 |
io4dolfinx.write_mesh(filename, mesh, backend=backend) |
| 169 |
io4dolfinx.write_function(filename, uh, time=t0, backend=backend) |
| 170 |
uh.interpolate(f1) |
| 171 |
io4dolfinx.write_function(filename, uh, time=t1, backend=backend) |
| 172 |
|
| 173 |
else: |
| 174 |
if MPI.COMM_WORLD.rank == 0: |
| 175 |
io4dolfinx.write_mesh(filename, mesh, backend=backend) |
| 176 |
io4dolfinx.write_function(filename, uh, time=t0, backend=backend) |
| 177 |
uh.interpolate(f1) |
| 178 |
io4dolfinx.write_function(filename, uh, time=t1, backend=backend) |
| 179 |
|
| 180 |
return filename |
| 181 |
|
| 182 |
return _write_function_time_dep |
| 183 |
|
| 184 |
|
| 185 |
@pytest.fixture(scope="function") |
| 186 |
def read_function_time_dep(): |
| 187 |
def _read_function_time_dep( |
| 188 |
comm, el, f0, f1, t0, t1, path, dtype, backend: typing.Literal["adios2", "h5py"] |
| 189 |
): |
| 190 |
mesh = io4dolfinx.read_mesh( |
| 191 |
path, |
| 192 |
comm, |
| 193 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 194 |
backend=backend, |
| 195 |
) |
| 196 |
V = dolfinx.fem.functionspace(mesh, el) |
| 197 |
v = dolfinx.fem.Function(V, dtype=dtype) |
| 198 |
|
| 199 |
io4dolfinx.read_function(path, v, time=t1, backend=backend) |
| 200 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 201 |
v_ex.interpolate(f1) |
| 202 |
|
| 203 |
res = np.finfo(dtype).resolution |
| 204 |
assert np.allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 205 |
|
| 206 |
io4dolfinx.read_function(path, v, time=t0, backend=backend) |
| 207 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 208 |
v_ex.interpolate(f0) |
| 209 |
|
| 210 |
res = np.finfo(dtype).resolution |
| 211 |
assert np.allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 212 |
|
| 213 |
return _read_function_time_dep |
| 214 |
|
| 215 |
|
| 216 |
def _generate_reference_map( |
| 217 |
mesh: dolfinx.mesh.Mesh, |
| 218 |
meshtag: dolfinx.mesh.MeshTags, |
| 219 |
comm: MPI.Comm, |
| 220 |
root: int, |
| 221 |
) -> typing.Optional[dict[str, tuple[int, npt.NDArray]]]: |
| 222 |
""" |
| 223 |
Helper function to generate map from meshtag value to its corresponding index and midpoint. |
| 224 |
|
| 225 |
Args: |
| 226 |
mesh: The mesh |
| 227 |
meshtag: The associated meshtag |
| 228 |
comm: MPI communicator to gather the map from all processes with |
| 229 |
root (int): Rank to store data on |
| 230 |
Returns: |
| 231 |
Root rank returns the map, all other ranks return None |
| 232 |
""" |
| 233 |
mesh.topology.create_connectivity(meshtag.dim, mesh.topology.dim) |
| 234 |
midpoints = dolfinx.mesh.compute_midpoints(mesh, meshtag.dim, meshtag.indices) |
| 235 |
e_map = mesh.topology.index_map(meshtag.dim) |
| 236 |
value_to_midpoint = {} |
| 237 |
for index, value in zip(meshtag.indices, meshtag.values): |
| 238 |
value_to_midpoint[value] = ( |
| 239 |
int(e_map.local_range[0] + index), |
| 240 |
midpoints[index], |
| 241 |
) |
| 242 |
global_map = comm.gather(value_to_midpoint, root=root) |
| 243 |
if comm.rank == root: |
| 244 |
return dict(ChainMap(*global_map)) # type: ignore |
| 245 |
return None |
| 246 |
|
| 247 |
|
| 248 |
@pytest.fixture |
| 249 |
def generate_reference_map(): |
| 250 |
return _generate_reference_map |