| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import itertools |
| 4 |
import os |
| 5 |
import typing |
| 6 |
from collections.abc import Callable |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
from mpi4py import MPI |
| 10 |
|
| 11 |
import basix |
| 12 |
import basix.ufl |
| 13 |
import dolfinx |
| 14 |
import numpy as np |
| 15 |
import pytest |
| 16 |
|
| 17 |
import io4dolfinx |
| 18 |
|
| 19 |
dtypes = [np.float64, np.float32] # Mesh geometry dtypes |
| 20 |
|
| 21 |
two_dimensional_cell_types = [ |
| 22 |
dolfinx.mesh.CellType.triangle, |
| 23 |
dolfinx.mesh.CellType.quadrilateral, |
| 24 |
] |
| 25 |
three_dimensional_cell_types = [ |
| 26 |
dolfinx.mesh.CellType.tetrahedron, |
| 27 |
dolfinx.mesh.CellType.hexahedron, |
| 28 |
] |
| 29 |
|
| 30 |
two_dim_combinations = itertools.product(dtypes, two_dimensional_cell_types) |
| 31 |
three_dim_combinations = itertools.product(dtypes, three_dimensional_cell_types) |
| 32 |
|
| 33 |
|
| 34 |
def create_locator(dtype): |
| 35 |
def locator(x, tol=np.finfo(dtype).resolution): |
| 36 |
return x[0] <= 0.5 + tol |
| 37 |
|
| 38 |
return locator |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture(scope="module") |
| 42 |
def create_simplex_mesh_2D(tmp_path_factory): |
| 43 |
mesh = dolfinx.mesh.create_unit_square( |
| 44 |
MPI.COMM_WORLD, |
| 45 |
10, |
| 46 |
10, |
| 47 |
cell_type=dolfinx.mesh.CellType.triangle, |
| 48 |
dtype=np.float64, |
| 49 |
) |
| 50 |
fname = tmp_path_factory.mktemp("output") / "original_mesh_2D_simplex.xdmf" |
| 51 |
fname = MPI.COMM_WORLD.bcast(fname, root=0) |
| 52 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 53 |
xdmf.write_mesh(mesh) |
| 54 |
return fname |
| 55 |
|
| 56 |
|
| 57 |
@pytest.fixture(scope="module") |
| 58 |
def create_simplex_mesh_3D(tmp_path_factory): |
| 59 |
# Make distributed to ensure original input indices are not identity |
| 60 |
fname = tmp_path_factory.mktemp("output") / "original_mesh_3D_simplex.xdmf" |
| 61 |
mesh = dolfinx.mesh.create_unit_cube( |
| 62 |
MPI.COMM_WORLD, |
| 63 |
5, |
| 64 |
5, |
| 65 |
5, |
| 66 |
cell_type=dolfinx.mesh.CellType.tetrahedron, |
| 67 |
dtype=np.float64, |
| 68 |
) |
| 69 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 70 |
xdmf.write_mesh(mesh) |
| 71 |
return fname |
| 72 |
|
| 73 |
|
| 74 |
@pytest.fixture(scope="module") |
| 75 |
def create_non_simplex_mesh_2D(tmp_path_factory): |
| 76 |
mesh = dolfinx.mesh.create_unit_square( |
| 77 |
MPI.COMM_WORLD, |
| 78 |
10, |
| 79 |
10, |
| 80 |
cell_type=dolfinx.mesh.CellType.quadrilateral, |
| 81 |
dtype=np.float64, |
| 82 |
) |
| 83 |
fname = tmp_path_factory.mktemp("output") / "original_mesh_2D_non_simplex.xdmf" |
| 84 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 85 |
xdmf.write_mesh(mesh) |
| 86 |
return fname |
| 87 |
|
| 88 |
|
| 89 |
@pytest.fixture(scope="module") |
| 90 |
def create_non_simplex_mesh_3D(tmp_path_factory): |
| 91 |
mesh = dolfinx.mesh.create_unit_cube( |
| 92 |
MPI.COMM_WORLD, |
| 93 |
5, |
| 94 |
5, |
| 95 |
5, |
| 96 |
cell_type=dolfinx.mesh.CellType.hexahedron, |
| 97 |
dtype=np.float64, |
| 98 |
) |
| 99 |
fname = tmp_path_factory.mktemp("output") / "original_mesh_3D_non_simplex.xdmf" |
| 100 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 101 |
xdmf.write_mesh(mesh) |
| 102 |
return fname |
| 103 |
|
| 104 |
|
| 105 |
@pytest.fixture(params=two_dim_combinations, scope="module") |
| 106 |
def create_2D_mesh(request, tmpdir_factory): |
| 107 |
dtype, cell_type = request.param |
| 108 |
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 5, 7, cell_type=cell_type, dtype=dtype) |
| 109 |
fname = Path(tmpdir_factory.mktemp("output")) / f"original_mesh_2D_{dtype}_{cell_type}.xdmf" |
| 110 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 111 |
xdmf.write_mesh(mesh) |
| 112 |
return fname |
| 113 |
|
| 114 |
|
| 115 |
@pytest.fixture(params=three_dim_combinations, scope="module") |
| 116 |
def create_3D_mesh(request, tmpdir_factory): |
| 117 |
dtype, cell_type = request.param |
| 118 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 5, 7, 3, cell_type=cell_type, dtype=dtype) |
| 119 |
fname = Path(tmpdir_factory.mktemp("output")) / f"original_mesh_3D_{dtype}_{cell_type}.xdmf" |
| 120 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "w") as xdmf: |
| 121 |
xdmf.write_mesh(mesh) |
| 122 |
return fname |
| 123 |
|
| 124 |
|
| 125 |
def write_function_original( |
| 126 |
write_mesh: bool, |
| 127 |
mesh: dolfinx.mesh.Mesh, |
| 128 |
el: basix.ufl._ElementBase, |
| 129 |
f: Callable[[np.ndarray], np.ndarray], |
| 130 |
dtype: np.dtype, |
| 131 |
name: str, |
| 132 |
path: Path, |
| 133 |
backend: typing.Literal["adios2", "h5py"], |
| 134 |
) -> Path: |
| 135 |
"""Convenience function for writing function to file on the original input mesh""" |
| 136 |
V = dolfinx.fem.functionspace(mesh, el) |
| 137 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 138 |
uh.interpolate(f) |
| 139 |
uh.name = name |
| 140 |
el_hash = ( |
| 141 |
io4dolfinx.utils.element_signature(V) |
| 142 |
.replace(" ", "") |
| 143 |
.replace(",", "") |
| 144 |
.replace("(", "") |
| 145 |
.replace(")", "") |
| 146 |
.replace("[", "") |
| 147 |
.replace("]", "") |
| 148 |
) |
| 149 |
|
| 150 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 151 |
if backend == "adios2": |
| 152 |
suffix = ".bp" |
| 153 |
elif backend == "h5py": |
| 154 |
suffix = ".h5" |
| 155 |
else: |
| 156 |
raise NotImplementedError(f"Unknown backend {backend}") |
| 157 |
|
| 158 |
locator = create_locator(dtype) |
| 159 |
|
| 160 |
tags = {} |
| 161 |
for i in range(mesh.topology.dim + 1): |
| 162 |
mesh.topology.create_entities(i) |
| 163 |
indices = dolfinx.mesh.locate_entities(mesh, i, locator) |
| 164 |
values = np.full_like(indices, i + 2, dtype=np.int32) |
| 165 |
tag = dolfinx.mesh.meshtags(mesh, i, indices, values) |
| 166 |
tag.name = f"tag_{i}" |
| 167 |
tags[i] = tag |
| 168 |
|
| 169 |
filename = (path / f"mesh_{file_hash}").with_suffix(suffix) |
| 170 |
if write_mesh: |
| 171 |
io4dolfinx.write_mesh_input_order(filename, mesh, backend=backend) |
| 172 |
io4dolfinx.write_function_on_input_mesh(filename, uh, time=0.0, backend=backend) |
| 173 |
for tag in tags.values(): |
| 174 |
io4dolfinx.write_meshtags(filename, mesh, tag, backend=backend, on_input_mesh=True) |
| 175 |
return filename |
| 176 |
|
| 177 |
|
| 178 |
def read_function_original( |
| 179 |
mesh_fname: Path, |
| 180 |
u_fname: Path, |
| 181 |
u_name: str, |
| 182 |
family: str, |
| 183 |
degree: int, |
| 184 |
f: Callable[[np.ndarray], np.ndarray], |
| 185 |
u_dtype: np.dtype, |
| 186 |
backend: typing.Literal["adios2", "h5py"], |
| 187 |
): |
| 188 |
""" |
| 189 |
Convenience function for reading mesh with IPython-parallel and compare to exact solution |
| 190 |
""" |
| 191 |
from mpi4py import MPI |
| 192 |
|
| 193 |
import dolfinx |
| 194 |
|
| 195 |
import io4dolfinx |
| 196 |
|
| 197 |
# assert MPI.COMM_WORLD.size > 1 |
| 198 |
if backend == "adios2": |
| 199 |
backend_args = {"engine": "BP4"} |
| 200 |
else: |
| 201 |
backend_args = None |
| 202 |
|
| 203 |
if mesh_fname.suffix == ".xdmf": |
| 204 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_fname, "r") as xdmf: |
| 205 |
mesh = xdmf.read_mesh() |
| 206 |
else: |
| 207 |
mesh = io4dolfinx.read_mesh( |
| 208 |
mesh_fname, |
| 209 |
MPI.COMM_WORLD, |
| 210 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 211 |
backend_args=backend_args, |
| 212 |
backend=backend, |
| 213 |
) |
| 214 |
|
| 215 |
el = basix.ufl.element( |
| 216 |
family, |
| 217 |
mesh.basix_cell(), |
| 218 |
degree, |
| 219 |
basix.LagrangeVariant.gll_warped, |
| 220 |
shape=(mesh.geometry.dim,), |
| 221 |
dtype=mesh.geometry.x.dtype, |
| 222 |
) |
| 223 |
|
| 224 |
V = dolfinx.fem.functionspace(mesh, el) |
| 225 |
u = dolfinx.fem.Function(V, name=u_name, dtype=u_dtype) |
| 226 |
|
| 227 |
io4dolfinx.read_function(u_fname, u, time=0.0, backend_args=backend_args, backend=backend) |
| 228 |
|
| 229 |
for i in range(mesh.topology.dim + 1): |
| 230 |
tag = io4dolfinx.read_meshtags(u_fname, mesh, meshtag_name=f"tag_{i}", backend=backend) |
| 231 |
assert np.all(tag.values == i + 2) |
| 232 |
|
| 233 |
locator = create_locator(mesh.geometry.x.dtype) |
| 234 |
|
| 235 |
correct_entities = dolfinx.mesh.locate_entities(mesh, i, locator) |
| 236 |
np.testing.assert_allclose(tag.indices, np.sort(correct_entities)) |
| 237 |
|
| 238 |
MPI.COMM_WORLD.Barrier() |
| 239 |
|
| 240 |
u_ex = dolfinx.fem.Function(V, name="exact", dtype=u_dtype) |
| 241 |
u_ex.interpolate(f) |
| 242 |
u_ex.x.scatter_forward() |
| 243 |
atol = 10 * np.finfo(u_dtype).resolution |
| 244 |
np.testing.assert_allclose(u.x.array, u_ex.x.array, atol=atol) # type: ignore |
| 245 |
|
| 246 |
|
| 247 |
def write_function_vector( |
| 248 |
write_mesh: bool, |
| 249 |
fname: Path, |
| 250 |
family: str, |
| 251 |
degree: int, |
| 252 |
f: Callable[[np.ndarray], np.ndarray], |
| 253 |
dtype: np.dtype, |
| 254 |
name: str, |
| 255 |
dir: Path, |
| 256 |
backend: typing.Literal["adios2", "h5py"], |
| 257 |
) -> Path: |
| 258 |
"""Convenience function for writing function to file on the original input mesh""" |
| 259 |
from mpi4py import MPI |
| 260 |
|
| 261 |
import basix.ufl |
| 262 |
import dolfinx |
| 263 |
|
| 264 |
import io4dolfinx |
| 265 |
|
| 266 |
assert MPI.COMM_WORLD.size > 1 |
| 267 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "r") as xdmf: |
| 268 |
mesh = xdmf.read_mesh() |
| 269 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 270 |
V = dolfinx.fem.functionspace(mesh, el) |
| 271 |
uh = dolfinx.fem.Function(V, dtype=dtype) |
| 272 |
uh.interpolate(f) |
| 273 |
uh.name = name |
| 274 |
el_hash = ( |
| 275 |
io4dolfinx.utils.element_signature(V) |
| 276 |
.replace(" ", "") |
| 277 |
.replace(",", "") |
| 278 |
.replace("(", "") |
| 279 |
.replace(")", "") |
| 280 |
.replace("[", "") |
| 281 |
.replace("]", "") |
| 282 |
) |
| 283 |
|
| 284 |
file_hash = f"{el_hash}_{np.dtype(dtype).name}" |
| 285 |
filename = dir / f"mesh_{file_hash}.bp" |
| 286 |
|
| 287 |
if write_mesh: |
| 288 |
io4dolfinx.write_mesh_input_order(filename, mesh, backend=backend) |
| 289 |
io4dolfinx.write_function_on_input_mesh(filename, uh, time=0.0, backend=backend) |
| 290 |
return filename |
| 291 |
|
| 292 |
|
| 293 |
def read_function_vector( |
| 294 |
mesh_fname: Path, |
| 295 |
u_fname: Path, |
| 296 |
u_name: str, |
| 297 |
family: str, |
| 298 |
degree: int, |
| 299 |
f: Callable[[np.ndarray], np.ndarray], |
| 300 |
u_dtype: np.dtype, |
| 301 |
backend: typing.Literal["adios2", "h5py"], |
| 302 |
): |
| 303 |
""" |
| 304 |
Convenience function for reading mesh with IPython-parallel and compare to exact solution |
| 305 |
""" |
| 306 |
|
| 307 |
if mesh_fname.suffix == ".xdmf": |
| 308 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_fname, "r") as xdmf: |
| 309 |
mesh = xdmf.read_mesh() |
| 310 |
elif mesh_fname.suffix == ".bp": |
| 311 |
mesh = io4dolfinx.read_mesh( |
| 312 |
mesh_fname, |
| 313 |
MPI.COMM_WORLD, |
| 314 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 315 |
backend=backend, |
| 316 |
) |
| 317 |
el = basix.ufl.element(family, mesh.basix_cell(), degree) |
| 318 |
|
| 319 |
V = dolfinx.fem.functionspace(mesh, el) |
| 320 |
u = dolfinx.fem.Function(V, name=u_name, dtype=u_dtype) |
| 321 |
io4dolfinx.read_function(u_fname, u, time=0.0, backend=backend) |
| 322 |
MPI.COMM_WORLD.Barrier() |
| 323 |
|
| 324 |
u_ex = dolfinx.fem.Function(V, name="exact", dtype=u_dtype) |
| 325 |
u_ex.interpolate(f) |
| 326 |
u_ex.x.scatter_forward() |
| 327 |
atol = 10 * np.finfo(u_dtype).resolution |
| 328 |
np.testing.assert_allclose(u.x.array, u_ex.x.array, atol=atol) # type: ignore |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.skipif( |
| 332 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 333 |
) |
| 334 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 335 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 336 |
@pytest.mark.parametrize("family", ["Lagrange", "DG"]) |
| 337 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 338 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 339 |
def test_read_write_P_2D( |
| 340 |
write_mesh, family, degree, is_complex, create_2D_mesh, cluster, get_dtype, tmp_path, backend |
| 341 |
): |
| 342 |
fname = create_2D_mesh |
| 343 |
|
| 344 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "r") as xdmf: |
| 345 |
mesh = xdmf.read_mesh() |
| 346 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 347 |
|
| 348 |
el = basix.ufl.element( |
| 349 |
family, |
| 350 |
mesh.basix_cell(), |
| 351 |
degree, |
| 352 |
basix.LagrangeVariant.gll_warped, |
| 353 |
shape=(mesh.geometry.dim,), |
| 354 |
dtype=mesh.geometry.x.dtype, |
| 355 |
) |
| 356 |
|
| 357 |
def f(x): |
| 358 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 359 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 360 |
values[1] = x[0] |
| 361 |
if is_complex: |
| 362 |
values[0] -= 3j * x[1] |
| 363 |
values[1] += 2j * x[0] |
| 364 |
return values |
| 365 |
|
| 366 |
hash = write_function_original( |
| 367 |
write_mesh, mesh, el, f, f_dtype, "u_original", tmp_path, backend |
| 368 |
) |
| 369 |
if write_mesh: |
| 370 |
mesh_fname = hash |
| 371 |
else: |
| 372 |
mesh_fname = fname |
| 373 |
query = cluster[:].apply_async( |
| 374 |
read_function_original, mesh_fname, hash, "u_original", family, degree, f, f_dtype, backend |
| 375 |
) |
| 376 |
query.wait() |
| 377 |
assert query.successful(), query.error |
| 378 |
|
| 379 |
|
| 380 |
@pytest.mark.skipif( |
| 381 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 382 |
) |
| 383 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 384 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 385 |
@pytest.mark.parametrize("family", ["Lagrange", "DG"]) |
| 386 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 387 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 388 |
def test_read_write_P_3D( |
| 389 |
write_mesh, family, degree, is_complex, create_3D_mesh, cluster, get_dtype, tmp_path, backend |
| 390 |
): |
| 391 |
fname = create_3D_mesh |
| 392 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "r") as xdmf: |
| 393 |
mesh = xdmf.read_mesh() |
| 394 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 395 |
el = basix.ufl.element( |
| 396 |
family, |
| 397 |
mesh.basix_cell(), |
| 398 |
degree, |
| 399 |
basix.LagrangeVariant.gll_warped, |
| 400 |
shape=(mesh.geometry.dim,), |
| 401 |
) |
| 402 |
|
| 403 |
def f(x): |
| 404 |
values = np.empty((3, x.shape[1]), dtype=f_dtype) |
| 405 |
values[0] = np.pi + x[0] |
| 406 |
values[1] = x[1] + 2 * x[0] |
| 407 |
values[2] = np.cos(x[2]) |
| 408 |
if is_complex: |
| 409 |
values[0] -= np.pi * x[1] |
| 410 |
values[1] += 3j * x[2] |
| 411 |
values[2] += 2j |
| 412 |
return values |
| 413 |
|
| 414 |
hash = write_function_original( |
| 415 |
write_mesh, mesh, el, f, f_dtype, "u_original", tmp_path, backend=backend |
| 416 |
) |
| 417 |
MPI.COMM_WORLD.Barrier() |
| 418 |
|
| 419 |
if write_mesh: |
| 420 |
mesh_fname = hash |
| 421 |
else: |
| 422 |
mesh_fname = fname |
| 423 |
|
| 424 |
query = cluster[:].apply_async( |
| 425 |
read_function_original, |
| 426 |
mesh_fname, |
| 427 |
hash, |
| 428 |
"u_original", |
| 429 |
family, |
| 430 |
degree, |
| 431 |
f, |
| 432 |
f_dtype, |
| 433 |
backend=backend, |
| 434 |
) |
| 435 |
query.wait() |
| 436 |
assert query.successful(), query.error |
| 437 |
|
| 438 |
|
| 439 |
@pytest.mark.skipif( |
| 440 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 441 |
) |
| 442 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 443 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 444 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 445 |
@pytest.mark.parametrize("family", ["N1curl", "RT"]) |
| 446 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 447 |
def test_read_write_2D_vector_simplex( |
| 448 |
write_mesh, |
| 449 |
family, |
| 450 |
degree, |
| 451 |
is_complex, |
| 452 |
create_simplex_mesh_2D, |
| 453 |
cluster, |
| 454 |
get_dtype, |
| 455 |
tmp_path, |
| 456 |
backend, |
| 457 |
): |
| 458 |
fname = create_simplex_mesh_2D |
| 459 |
|
| 460 |
f_dtype = get_dtype(np.float64, is_complex) |
| 461 |
|
| 462 |
def f(x): |
| 463 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 464 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 465 |
values[1] = x[1] |
| 466 |
if is_complex: |
| 467 |
values[0] -= np.sin(x[1]) * 2j |
| 468 |
values[1] += 3j |
| 469 |
return values |
| 470 |
|
| 471 |
query = cluster[:].apply_async( |
| 472 |
write_function_vector, |
| 473 |
write_mesh, |
| 474 |
fname, |
| 475 |
family, |
| 476 |
degree, |
| 477 |
f, |
| 478 |
f_dtype, |
| 479 |
"u_original", |
| 480 |
tmp_path, |
| 481 |
backend, |
| 482 |
) |
| 483 |
query.wait() |
| 484 |
assert query.successful(), query.error |
| 485 |
paths = query.result() |
| 486 |
file_path = paths[0] |
| 487 |
assert all([file_path == path for path in paths]) |
| 488 |
if write_mesh: |
| 489 |
mesh_fname = file_path |
| 490 |
else: |
| 491 |
mesh_fname = fname |
| 492 |
|
| 493 |
read_function_vector(mesh_fname, file_path, "u_original", family, degree, f, f_dtype, backend) |
| 494 |
|
| 495 |
|
| 496 |
@pytest.mark.skipif( |
| 497 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 498 |
) |
| 499 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 500 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 501 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 502 |
@pytest.mark.parametrize("family", ["N1curl", "RT"]) |
| 503 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 504 |
def test_read_write_3D_vector_simplex( |
| 505 |
write_mesh, |
| 506 |
family, |
| 507 |
degree, |
| 508 |
is_complex, |
| 509 |
create_simplex_mesh_3D, |
| 510 |
cluster, |
| 511 |
get_dtype, |
| 512 |
tmp_path, |
| 513 |
backend, |
| 514 |
): |
| 515 |
fname = create_simplex_mesh_3D |
| 516 |
|
| 517 |
f_dtype = get_dtype(np.float64, is_complex) |
| 518 |
|
| 519 |
def f(x): |
| 520 |
values = np.empty((3, x.shape[1]), dtype=f_dtype) |
| 521 |
values[0] = np.full(x.shape[1], np.pi) |
| 522 |
values[1] = x[1] + 2 * x[0] |
| 523 |
values[2] = np.cos(x[2]) |
| 524 |
if is_complex: |
| 525 |
values[0] += 2j * x[2] |
| 526 |
values[1] += 2j * np.cos(x[2]) |
| 527 |
return values |
| 528 |
|
| 529 |
query = cluster[:].apply_async( |
| 530 |
write_function_vector, |
| 531 |
write_mesh, |
| 532 |
fname, |
| 533 |
family, |
| 534 |
degree, |
| 535 |
f, |
| 536 |
f_dtype, |
| 537 |
"u_original", |
| 538 |
tmp_path, |
| 539 |
backend, |
| 540 |
) |
| 541 |
query.wait() |
| 542 |
assert query.successful(), query.error |
| 543 |
paths = query.result() |
| 544 |
file_path = paths[0] |
| 545 |
assert all([file_path == path for path in paths]) |
| 546 |
if write_mesh: |
| 547 |
mesh_fname = file_path |
| 548 |
else: |
| 549 |
mesh_fname = fname |
| 550 |
|
| 551 |
read_function_vector(mesh_fname, file_path, "u_original", family, degree, f, f_dtype, backend) |
| 552 |
|
| 553 |
|
| 554 |
@pytest.mark.skipif( |
| 555 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 556 |
) |
| 557 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 558 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 559 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 560 |
@pytest.mark.parametrize("family", ["RTCF"]) |
| 561 |
@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 562 |
def test_read_write_2D_vector_non_simplex( |
| 563 |
write_mesh, |
| 564 |
family, |
| 565 |
degree, |
| 566 |
is_complex, |
| 567 |
create_non_simplex_mesh_2D, |
| 568 |
cluster, |
| 569 |
get_dtype, |
| 570 |
tmp_path, |
| 571 |
backend, |
| 572 |
): |
| 573 |
fname = create_non_simplex_mesh_2D |
| 574 |
|
| 575 |
f_dtype = get_dtype(np.float64, is_complex) |
| 576 |
|
| 577 |
def f(x): |
| 578 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 579 |
values[0] = np.full(x.shape[1], np.pi) |
| 580 |
values[1] = x[1] + 2 * x[0] |
| 581 |
if is_complex: |
| 582 |
values[0] += 2j * x[1] |
| 583 |
values[1] -= np.sin(x[0]) * 9j |
| 584 |
return values |
| 585 |
|
| 586 |
query = cluster[:].apply_async( |
| 587 |
write_function_vector, |
| 588 |
write_mesh, |
| 589 |
fname, |
| 590 |
family, |
| 591 |
degree, |
| 592 |
f, |
| 593 |
f_dtype, |
| 594 |
"u_original", |
| 595 |
tmp_path, |
| 596 |
backend, |
| 597 |
) |
| 598 |
query.wait() |
| 599 |
assert query.successful(), query.error |
| 600 |
paths = query.result() |
| 601 |
file_path = paths[0] |
| 602 |
assert all([file_path == path for path in paths]) |
| 603 |
if write_mesh: |
| 604 |
mesh_fname = file_path |
| 605 |
else: |
| 606 |
mesh_fname = fname |
| 607 |
|
| 608 |
read_function_vector(mesh_fname, file_path, "u_original", family, degree, f, f_dtype, backend) |
| 609 |
|
| 610 |
|
| 611 |
@pytest.mark.skipif( |
| 612 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 613 |
) |
| 614 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 615 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 616 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 617 |
@pytest.mark.parametrize("family", ["NCF"]) |
| 618 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 619 |
def test_read_write_3D_vector_non_simplex( |
| 620 |
write_mesh, |
| 621 |
family, |
| 622 |
degree, |
| 623 |
is_complex, |
| 624 |
create_non_simplex_mesh_3D, |
| 625 |
cluster, |
| 626 |
get_dtype, |
| 627 |
tmp_path, |
| 628 |
backend, |
| 629 |
): |
| 630 |
fname = create_non_simplex_mesh_3D |
| 631 |
|
| 632 |
f_dtype = get_dtype(np.float64, is_complex) |
| 633 |
|
| 634 |
def f(x): |
| 635 |
values = np.empty((3, x.shape[1]), dtype=f_dtype) |
| 636 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 637 |
values[1] = np.cos(x[2]) |
| 638 |
values[2] = x[0] |
| 639 |
if is_complex: |
| 640 |
values[2] += x[0] * x[1] * 3j |
| 641 |
return values |
| 642 |
|
| 643 |
query = cluster[:].apply_async( |
| 644 |
write_function_vector, |
| 645 |
write_mesh, |
| 646 |
fname, |
| 647 |
family, |
| 648 |
degree, |
| 649 |
f, |
| 650 |
f_dtype, |
| 651 |
"u_original", |
| 652 |
tmp_path, |
| 653 |
backend, |
| 654 |
) |
| 655 |
query.wait() |
| 656 |
assert query.successful(), query.error |
| 657 |
paths = query.result() |
| 658 |
file_path = paths[0] |
| 659 |
assert all([file_path == path for path in paths]) |
| 660 |
if write_mesh: |
| 661 |
mesh_fname = file_path |
| 662 |
else: |
| 663 |
mesh_fname = fname |
| 664 |
|
| 665 |
read_function_vector(mesh_fname, file_path, "u_original", family, degree, f, f_dtype, backend) |
| 666 |
|
| 667 |
|
| 668 |
@pytest.mark.skipif( |
| 669 |
os.cpu_count() == 1, reason="Test requires that the system has more than one process" |
| 670 |
) |
| 671 |
@pytest.mark.skipif(MPI.COMM_WORLD.size > 1, reason="Test uses ipythonparallel for MPI") |
| 672 |
@pytest.mark.parametrize("write_mesh", [True, False]) |
| 673 |
def test_read_write_meshtags(write_mesh, create_2D_mesh, cluster, get_dtype, tmp_path, backend): |
| 674 |
# Special testcase for writing meshtags on input mesh, but to checkpoint file. |
| 675 |
family = "Lagrange" |
| 676 |
is_complex = False |
| 677 |
degree = 1 |
| 678 |
fname = create_2D_mesh |
| 679 |
|
| 680 |
def f(x, f_dtype): |
| 681 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 682 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 683 |
values[1] = x[0] |
| 684 |
return values |
| 685 |
|
| 686 |
def read_xdmf_and_write_distributed(fname): |
| 687 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, fname, "r") as xdmf: |
| 688 |
mesh = xdmf.read_mesh() |
| 689 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 690 |
|
| 691 |
el = basix.ufl.element( |
| 692 |
family, |
| 693 |
mesh.basix_cell(), |
| 694 |
degree, |
| 695 |
basix.LagrangeVariant.gll_warped, |
| 696 |
shape=(mesh.geometry.dim,), |
| 697 |
dtype=mesh.geometry.x.dtype, |
| 698 |
) |
| 699 |
|
| 700 |
hash = write_function_original( |
| 701 |
write_mesh, mesh, el, lambda x: f(x, f_dtype), f_dtype, "u_original", tmp_path, backend |
| 702 |
) |
| 703 |
return hash, f_dtype |
| 704 |
|
| 705 |
query = cluster[:].apply_async(read_xdmf_and_write_distributed, fname) |
| 706 |
query.wait() |
| 707 |
assert query.successful(), query.error |
| 708 |
hash, f_dtype = query.result()[0] |
| 709 |
if write_mesh: |
| 710 |
mesh_fname = hash |
| 711 |
else: |
| 712 |
mesh_fname = fname |
| 713 |
|
| 714 |
read_function_original( |
| 715 |
mesh_fname, hash, "u_original", family, degree, lambda x: f(x, f_dtype), f_dtype, backend |
| 716 |
) |
| 717 |
|
| 718 |
query.wait() |
| 719 |
assert query.successful(), query.error |