| 1 |
# Copyright (C) 2023-2026 Jørgen Schartum Dokken |
| 2 |
# |
| 3 |
# This file is part of io4dolfinx |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
|
| 8 |
import inspect |
| 9 |
import pathlib |
| 10 |
|
| 11 |
from mpi4py import MPI |
| 12 |
|
| 13 |
import dolfinx |
| 14 |
import numpy as np |
| 15 |
import pytest |
| 16 |
import ufl |
| 17 |
|
| 18 |
from io4dolfinx import ( |
| 19 |
read_cell_data, |
| 20 |
read_function, |
| 21 |
read_function_from_legacy_h5, |
| 22 |
read_function_names, |
| 23 |
read_mesh, |
| 24 |
read_mesh_from_legacy_h5, |
| 25 |
read_meshtags, |
| 26 |
read_point_data, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_legacy_mesh(backend): |
| 31 |
comm = MPI.COMM_WORLD |
| 32 |
path = (pathlib.Path("legacy") / "mesh.h5").absolute() |
| 33 |
if not path.exists(): |
| 34 |
pytest.skip(f"{path} does not exist") |
| 35 |
mesh = read_mesh_from_legacy_h5(filename=path, comm=comm, group="/mesh", backend=backend) |
| 36 |
assert mesh.topology.dim == 3 |
| 37 |
volume = mesh.comm.allreduce( |
| 38 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))), |
| 39 |
op=MPI.SUM, |
| 40 |
) |
| 41 |
surface = mesh.comm.allreduce( |
| 42 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.ds(domain=mesh))), |
| 43 |
op=MPI.SUM, |
| 44 |
) |
| 45 |
assert np.isclose(volume, 1) |
| 46 |
assert np.isclose(surface, 6) |
| 47 |
|
| 48 |
mesh.topology.create_entities(mesh.topology.dim - 1) |
| 49 |
num_facets = mesh.topology.index_map(mesh.topology.dim - 1).size_global |
| 50 |
assert num_facets == 18 |
| 51 |
|
| 52 |
|
| 53 |
def test_read_legacy_mesh_from_checkpoint(backend): |
| 54 |
comm = MPI.COMM_WORLD |
| 55 |
filename = (pathlib.Path("legacy") / "mesh_checkpoint.h5").absolute() |
| 56 |
if not filename.exists(): |
| 57 |
pytest.skip(f"{filename} does not exist") |
| 58 |
mesh = read_mesh_from_legacy_h5( |
| 59 |
filename=filename, comm=comm, group="/Mesh/mesh", backend=backend |
| 60 |
) |
| 61 |
assert mesh.topology.dim == 3 |
| 62 |
volume = mesh.comm.allreduce( |
| 63 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))), |
| 64 |
op=MPI.SUM, |
| 65 |
) |
| 66 |
surface = mesh.comm.allreduce( |
| 67 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.ds(domain=mesh))), |
| 68 |
op=MPI.SUM, |
| 69 |
) |
| 70 |
assert np.isclose(volume, 1) |
| 71 |
assert np.isclose(surface, 6) |
| 72 |
|
| 73 |
mesh.topology.create_entities(mesh.topology.dim - 1) |
| 74 |
num_facets = mesh.topology.index_map(mesh.topology.dim - 1).size_global |
| 75 |
assert num_facets == 18 |
| 76 |
|
| 77 |
|
| 78 |
def test_legacy_function(backend): |
| 79 |
comm = MPI.COMM_WORLD |
| 80 |
path = (pathlib.Path("legacy") / "mesh.h5").absolute() |
| 81 |
if not path.exists(): |
| 82 |
pytest.skip(f"{path} does not exist") |
| 83 |
mesh = read_mesh_from_legacy_h5(path, comm, "/mesh", backend=backend) |
| 84 |
ff = read_meshtags( |
| 85 |
path, mesh, "facet_tags", backend_args={"legacy": True, "engine": "HDF5"}, backend=backend |
| 86 |
) |
| 87 |
fdim = mesh.topology.dim - 1 |
| 88 |
assert ff.dim == fdim |
| 89 |
boundary_facets = ff.find(2) |
| 90 |
interior_facets = ff.find(1) |
| 91 |
mesh.topology.create_connectivity(fdim, fdim + 1) |
| 92 |
num_facets_local = ( |
| 93 |
mesh.topology.index_map(fdim).size_local + mesh.topology.index_map(fdim).num_ghosts |
| 94 |
) |
| 95 |
true_exterior_facets = dolfinx.mesh.exterior_facet_indices(mesh.topology) |
| 96 |
assert len(boundary_facets) == len(true_exterior_facets) |
| 97 |
assert np.isin(boundary_facets, true_exterior_facets).all() |
| 98 |
true_interior = np.ones(num_facets_local, dtype=np.int8) |
| 99 |
true_interior[true_exterior_facets] = 0 |
| 100 |
interior_marker = np.flatnonzero(true_interior) |
| 101 |
assert len(interior_marker) == len(interior_facets) |
| 102 |
assert np.isin(interior_facets, interior_marker).all() |
| 103 |
|
| 104 |
V = dolfinx.fem.functionspace(mesh, ("DG", 2)) |
| 105 |
u = ufl.TrialFunction(V) |
| 106 |
v = ufl.TestFunction(V) |
| 107 |
a = ufl.inner(u, v) * ufl.dx |
| 108 |
x = ufl.SpatialCoordinate(mesh) |
| 109 |
# NOTE: must stay exactly representable in the DG2 space (see tests/create_legacy_data.py) |
| 110 |
# so the projection is quadrature-scheme independent when compared to legacy dolfin's. |
| 111 |
f = x[0] ** 2 + 2 * x[1] * x[2] - x[1] |
| 112 |
L = ufl.inner(f, v) * ufl.dx |
| 113 |
|
| 114 |
if not dolfinx.has_petsc4py: |
| 115 |
pytest.skip("dolfinx not configured with PETSc4py") |
| 116 |
|
| 117 |
from dolfinx.fem.petsc import LinearProblem |
| 118 |
|
| 119 |
uh = dolfinx.fem.Function(V) |
| 120 |
if "petsc_options_prefix" in inspect.signature(LinearProblem.__init__).parameters.keys(): |
| 121 |
extra_options = {"petsc_options_prefix": "legacy_test"} |
| 122 |
else: |
| 123 |
extra_options = {} |
| 124 |
problem = LinearProblem( |
| 125 |
a, L, bcs=[], u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"}, **extra_options |
| 126 |
) |
| 127 |
problem.solve() |
| 128 |
|
| 129 |
u_in = dolfinx.fem.Function(V) |
| 130 |
read_function_from_legacy_h5(path, mesh.comm, u_in, group="v", backend=backend) |
| 131 |
np.testing.assert_allclose(uh.x.array, u_in.x.array, atol=1e-14) |
| 132 |
|
| 133 |
W = dolfinx.fem.functionspace(mesh, ("DG", 2, (mesh.geometry.dim,))) |
| 134 |
wh = dolfinx.fem.Function(W) |
| 135 |
wh.interpolate(lambda x: (x[0], 3 * x[2], 7 * x[1])) |
| 136 |
w_in = dolfinx.fem.Function(W) |
| 137 |
|
| 138 |
read_function_from_legacy_h5(path, mesh.comm, w_in, group="w", backend=backend) |
| 139 |
np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14) |
| 140 |
|
| 141 |
|
| 142 |
def test_read_legacy_function_from_checkpoint(backend): |
| 143 |
comm = MPI.COMM_WORLD |
| 144 |
path = (pathlib.Path("legacy") / "mesh_checkpoint.h5").absolute() |
| 145 |
if not path.exists(): |
| 146 |
pytest.skip(f"{path} does not exist") |
| 147 |
|
| 148 |
from dolfinx.fem.petsc import LinearProblem |
| 149 |
|
| 150 |
mesh = read_mesh_from_legacy_h5(path, comm, "/Mesh/mesh", backend=backend) |
| 151 |
|
| 152 |
V = dolfinx.fem.functionspace(mesh, ("DG", 2)) |
| 153 |
u = ufl.TrialFunction(V) |
| 154 |
v = ufl.TestFunction(V) |
| 155 |
a = ufl.inner(u, v) * ufl.dx |
| 156 |
x = ufl.SpatialCoordinate(mesh) |
| 157 |
# NOTE: must stay exactly representable in the DG2 space (see tests/create_legacy_data.py) |
| 158 |
# so the projection is quadrature-scheme independent when compared to legacy dolfin's. |
| 159 |
f = x[0] ** 2 + 2 * x[1] * x[2] - x[1] |
| 160 |
L = ufl.inner(f, v) * ufl.dx |
| 161 |
|
| 162 |
if not dolfinx.has_petsc4py: |
| 163 |
pytest.skip("dolfinx not configured with PETSc4py") |
| 164 |
|
| 165 |
uh = dolfinx.fem.Function(V) |
| 166 |
if "petsc_options_prefix" in inspect.signature(LinearProblem.__init__).parameters.keys(): |
| 167 |
extra_options = {"petsc_options_prefix": "legacy_checkpoint_test"} |
| 168 |
else: |
| 169 |
extra_options = {} |
| 170 |
problem = LinearProblem( |
| 171 |
a, L, bcs=[], u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"}, **extra_options |
| 172 |
) |
| 173 |
problem.solve() |
| 174 |
|
| 175 |
u_in = dolfinx.fem.Function(V) |
| 176 |
|
| 177 |
read_function_from_legacy_h5(path, mesh.comm, u_in, group="v", step=0, backend=backend) |
| 178 |
assert np.allclose(uh.x.array, u_in.x.array) |
| 179 |
|
| 180 |
# Check second step |
| 181 |
uh.interpolate(lambda x: x[0]) |
| 182 |
read_function_from_legacy_h5(path, mesh.comm, u_in, group="v", step=1, backend=backend) |
| 183 |
assert np.allclose(uh.x.array, u_in.x.array) |
| 184 |
|
| 185 |
W = dolfinx.fem.functionspace(mesh, ("DG", 2, (mesh.geometry.dim,))) |
| 186 |
wh = dolfinx.fem.Function(W) |
| 187 |
wh.interpolate(lambda x: (x[0], 3 * x[2], 7 * x[1])) |
| 188 |
w_in = dolfinx.fem.Function(W) |
| 189 |
|
| 190 |
read_function_from_legacy_h5(path, mesh.comm, w_in, group="w", step=0, backend=backend) |
| 191 |
np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14) |
| 192 |
|
| 193 |
wh.interpolate(lambda x: np.vstack((x[0], 0 * x[0], x[1]))) |
| 194 |
read_function_from_legacy_h5(path, mesh.comm, w_in, group="w", step=1, backend=backend) |
| 195 |
np.testing.assert_allclose(wh.x.array, w_in.x.array, atol=1e-14) |
| 196 |
|
| 197 |
if backend == "h5py": |
| 198 |
qh = read_cell_data( |
| 199 |
path.with_suffix(".xdmf"), "vDG_checkpoint", mesh, 0.5, backend_args={}, backend="xdmf" |
| 200 |
) |
| 201 |
cell_map = mesh.topology.index_map(mesh.topology.dim) |
| 202 |
num_cells_local = cell_map.size_local + cell_map.num_ghosts |
| 203 |
midpoints = dolfinx.mesh.compute_midpoints( |
| 204 |
mesh, mesh.topology.dim, np.arange(num_cells_local, dtype=np.int32) |
| 205 |
) |
| 206 |
ref_val = midpoints[:, 0] + midpoints[:, 1] |
| 207 |
np.testing.assert_allclose(qh.x.array, ref_val) |
| 208 |
|
| 209 |
|
| 210 |
def test_io4dolfinx_legacy(): |
| 211 |
comm = MPI.COMM_WORLD |
| 212 |
path = (pathlib.Path("legacy_checkpoint") / "io4dolfinx_checkpoint.bp").absolute() |
| 213 |
if not path.exists(): |
| 214 |
pytest.skip(f"{path} does not exist") |
| 215 |
|
| 216 |
el = ("N1curl", 3) |
| 217 |
backend_args = {"engine": "BP4", "legacy": True} |
| 218 |
mesh = read_mesh( |
| 219 |
path, comm, dolfinx.mesh.GhostMode.shared_facet, backend_args=backend_args, backend="adios2" |
| 220 |
) |
| 221 |
|
| 222 |
def f(x): |
| 223 |
values = np.zeros((2, x.shape[1]), dtype=np.float64) |
| 224 |
values[0] = x[0] |
| 225 |
values[1] = -x[1] |
| 226 |
return values |
| 227 |
|
| 228 |
V = dolfinx.fem.functionspace(mesh, el) |
| 229 |
u = dolfinx.fem.Function(V) |
| 230 |
read_function(path, u, backend_args=backend_args, backend="adios2") |
| 231 |
|
| 232 |
u_ex = dolfinx.fem.Function(V) |
| 233 |
u_ex.interpolate(f) |
| 234 |
|
| 235 |
np.testing.assert_allclose(u.x.array, u_ex.x.array, atol=1e-14) |
| 236 |
|
| 237 |
|
| 238 |
def test_legacy_vtu_mesh(): |
| 239 |
comm = MPI.COMM_WORLD |
| 240 |
path = (pathlib.Path("legacy") / "mesh_P1000000.vtu").absolute() |
| 241 |
if not path.exists(): |
| 242 |
pytest.skip(f"{path} does not exist") |
| 243 |
|
| 244 |
pytest.importorskip("pyvista") |
| 245 |
mesh = read_mesh(path, comm, backend="pyvista") |
| 246 |
|
| 247 |
num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global |
| 248 |
assert num_cells_global == 12 * 13 * 2 |
| 249 |
|
| 250 |
area = mesh.comm.allreduce( |
| 251 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))), op=MPI.SUM |
| 252 |
) |
| 253 |
assert np.isclose(area, 1.9 * 2.8) |
| 254 |
|
| 255 |
|
| 256 |
def test_legacy_pvd(): |
| 257 |
comm = MPI.COMM_WORLD |
| 258 |
path = (pathlib.Path("legacy") / "mesh_P1_func.pvd").absolute() |
| 259 |
if not path.exists(): |
| 260 |
pytest.skip(f"{path} does not exist") |
| 261 |
|
| 262 |
pytest.importorskip("pyvista") |
| 263 |
mesh = read_mesh(path, comm, backend="pyvista") |
| 264 |
|
| 265 |
num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global |
| 266 |
assert num_cells_global == 12 * 13 * 2 |
| 267 |
|
| 268 |
area = mesh.comm.allreduce( |
| 269 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))), op=MPI.SUM |
| 270 |
) |
| 271 |
assert np.isclose(area, 1.9 * 2.8) |
| 272 |
|
| 273 |
u = read_point_data(path, "w", mesh, backend="pyvista") |
| 274 |
names = read_function_names(path, mesh.comm, {}, backend="pyvista") |
| 275 |
assert len(names) == 1 |
| 276 |
assert names[0] == "w" |
| 277 |
|
| 278 |
u_ref = dolfinx.fem.Function(u.function_space) |
| 279 |
u_ref.interpolate(lambda x: (x[0], x[1] - x[0], np.zeros_like(x[0]))) |
| 280 |
|
| 281 |
np.testing.assert_allclose(u.x.array, u_ref.x.array) |