| 1 |
from mpi4py import MPI |
| 2 |
|
| 3 |
import dolfinx |
| 4 |
import ipyparallel as ipp |
| 5 |
import numpy as np |
| 6 |
import numpy.typing |
| 7 |
import pytest |
| 8 |
|
| 9 |
import adios4dolfinx |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture(scope="module") |
| 13 |
def cluster(): |
| 14 |
cluster = ipp.Cluster(engines="mpi", n=2) |
| 15 |
rc = cluster.start_and_connect_sync() |
| 16 |
yield rc |
| 17 |
cluster.stop_cluster_sync() |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture(scope="function") |
| 21 |
def write_function(tmp_path): |
| 22 |
def _write_function(mesh, el, f, dtype, name="uh", append: bool = False) -> str: |
| 23 |
V = dolfinx.fem.functionspace(mesh, el) |
| 24 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 25 |
uh.interpolate(f) |
| 26 |
uh.name = name |
| 27 |
el_hash = ( |
| 28 |
adios4dolfinx.utils.element_signature(V) |
| 29 |
.replace(" ", "") |
| 30 |
.replace(",", "") |
| 31 |
.replace("(", "") |
| 32 |
.replace(")", "") |
| 33 |
.replace("[", "") |
| 34 |
.replace("]", "") |
| 35 |
) |
| 36 |
# Consistent tmp dir across processes |
| 37 |
f_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 38 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 39 |
filename = f_path / f"mesh_{file_hash}.bp" |
| 40 |
if mesh.comm.size != 1: |
| 41 |
if not append: |
| 42 |
adios4dolfinx.write_mesh(filename, mesh) |
| 43 |
adios4dolfinx.write_function(filename, uh, time=0.0) |
| 44 |
else: |
| 45 |
if MPI.COMM_WORLD.rank == 0: |
| 46 |
if not append: |
| 47 |
adios4dolfinx.write_mesh(filename, mesh) |
| 48 |
adios4dolfinx.write_function(filename, uh, time=0.0) |
| 49 |
|
| 50 |
return filename |
| 51 |
|
| 52 |
return _write_function |
| 53 |
|
| 54 |
|
| 55 |
@pytest.fixture(scope="function") |
| 56 |
def read_function(): |
| 57 |
def _read_function(comm, el, f, path, dtype, name="uh"): |
| 58 |
engine = "BP4" |
| 59 |
mesh = adios4dolfinx.read_mesh(path, comm, engine, dolfinx.mesh.GhostMode.shared_facet) |
| 60 |
V = dolfinx.fem.functionspace(mesh, el) |
| 61 |
v = dolfinx.fem.Function(V, dtype=dtype) |
| 62 |
v.name = name |
| 63 |
adios4dolfinx.read_function(path, v, engine) |
| 64 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 65 |
v_ex.interpolate(f) |
| 66 |
|
| 67 |
res = np.finfo(dtype).resolution |
| 68 |
np.testing.assert_allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 69 |
|
| 70 |
return _read_function |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture(scope="function") |
| 74 |
def get_dtype(): |
| 75 |
def _get_dtype(in_dtype: np.dtype, is_complex: bool): |
| 76 |
dtype: numpy.typing.DTypeLike |
| 77 |
if in_dtype == np.float32: |
| 78 |
if is_complex: |
| 79 |
dtype = np.complex64 |
| 80 |
else: |
| 81 |
dtype = np.float32 |
| 82 |
elif in_dtype == np.float64: |
| 83 |
if is_complex: |
| 84 |
dtype = np.complex128 |
| 85 |
else: |
| 86 |
dtype = np.float64 |
| 87 |
else: |
| 88 |
raise ValueError("Unsuported dtype") |
| 89 |
return dtype |
| 90 |
|
| 91 |
return _get_dtype |
| 92 |
|
| 93 |
|
| 94 |
@pytest.fixture(scope="function") |
| 95 |
def write_function_time_dep(tmp_path): |
| 96 |
def _write_function_time_dep(mesh, el, f0, f1, t0, t1, dtype) -> str: |
| 97 |
V = dolfinx.fem.functionspace(mesh, el) |
| 98 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 99 |
uh.interpolate(f0) |
| 100 |
el_hash = ( |
| 101 |
adios4dolfinx.utils.element_signature(V) |
| 102 |
.replace(" ", "") |
| 103 |
.replace(",", "") |
| 104 |
.replace("(", "") |
| 105 |
.replace(")", "") |
| 106 |
.replace("[", "") |
| 107 |
.replace("]", "") |
| 108 |
) |
| 109 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 110 |
# Consistent tmp dir across processes |
| 111 |
f_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 112 |
filename = f_path / f"mesh_{file_hash}.bp" |
| 113 |
if mesh.comm.size != 1: |
| 114 |
adios4dolfinx.write_mesh(filename, mesh) |
| 115 |
adios4dolfinx.write_function(filename, uh, time=t0) |
| 116 |
uh.interpolate(f1) |
| 117 |
adios4dolfinx.write_function(filename, uh, time=t1) |
| 118 |
|
| 119 |
else: |
| 120 |
if MPI.COMM_WORLD.rank == 0: |
| 121 |
adios4dolfinx.write_mesh(filename, mesh) |
| 122 |
adios4dolfinx.write_function(filename, uh, time=t0) |
| 123 |
uh.interpolate(f1) |
| 124 |
adios4dolfinx.write_function(filename, uh, time=t1) |
| 125 |
|
| 126 |
return filename |
| 127 |
|
| 128 |
return _write_function_time_dep |
| 129 |
|
| 130 |
|
| 131 |
@pytest.fixture(scope="function") |
| 132 |
def read_function_time_dep(): |
| 133 |
def _read_function_time_dep(comm, el, f0, f1, t0, t1, path, dtype): |
| 134 |
engine = "BP4" |
| 135 |
mesh = adios4dolfinx.read_mesh(path, comm, engine, dolfinx.mesh.GhostMode.shared_facet) |
| 136 |
V = dolfinx.fem.functionspace(mesh, el) |
| 137 |
v = dolfinx.fem.Function(V, dtype=dtype) |
| 138 |
|
| 139 |
adios4dolfinx.read_function(path, v, engine, time=t1) |
| 140 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 141 |
v_ex.interpolate(f1) |
| 142 |
|
| 143 |
res = np.finfo(dtype).resolution |
| 144 |
assert np.allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 145 |
|
| 146 |
adios4dolfinx.read_function(path, v, engine, time=t0) |
| 147 |
v_ex = dolfinx.fem.Function(V, dtype=dtype) |
| 148 |
v_ex.interpolate(f0) |
| 149 |
|
| 150 |
res = np.finfo(dtype).resolution |
| 151 |
assert np.allclose(v.x.array, v_ex.x.array, atol=10 * res, rtol=10 * res) |
| 152 |
|
| 153 |
return _read_function_time_dep |