| 1 |
from mpi4py import MPI |
| 2 |
|
| 3 |
import dolfinx |
| 4 |
import numpy as np |
| 5 |
import ufl |
| 6 |
|
| 7 |
import io4dolfinx |
| 8 |
|
| 9 |
|
| 10 |
def test_xdmf_mesh(tmp_path): |
| 11 |
tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 12 |
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10) |
| 13 |
tmp_file = tmp_path / "mesh.xdmf" |
| 14 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, tmp_file, "w") as xdmf: |
| 15 |
xdmf.write_mesh(mesh) |
| 16 |
|
| 17 |
MPI.COMM_WORLD.barrier() |
| 18 |
in_grid = io4dolfinx.read_mesh(tmp_file, MPI.COMM_WORLD, backend="xdmf") |
| 19 |
|
| 20 |
assert mesh.topology.dim == in_grid.topology.dim |
| 21 |
assert mesh.geometry.dim == in_grid.geometry.dim |
| 22 |
for i in range(mesh.topology.dim): |
| 23 |
mesh.topology.create_entities(i) |
| 24 |
o_map = mesh.topology.index_map(i) |
| 25 |
in_grid.topology.create_entities(i) |
| 26 |
map = in_grid.topology.index_map(i) |
| 27 |
assert o_map.size_global == map.size_global |
| 28 |
|
| 29 |
assert mesh.geometry.index_map().size_global == in_grid.geometry.index_map().size_global |
| 30 |
org_area = mesh.comm.allreduce( |
| 31 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=mesh))), op=MPI.SUM |
| 32 |
) |
| 33 |
area = mesh.comm.allreduce( |
| 34 |
dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * ufl.dx(domain=in_grid))), op=MPI.SUM |
| 35 |
) |
| 36 |
assert np.isclose(area, org_area) |
| 37 |
|
| 38 |
|
| 39 |
def test_xdmf_function(tmp_path): |
| 40 |
tmp_path = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 41 |
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 8, 10) |
| 42 |
|
| 43 |
def f(x): |
| 44 |
return (x[0], x[1], -2 * x[1], 3 * x[0]) |
| 45 |
|
| 46 |
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (4,))) |
| 47 |
u = dolfinx.fem.Function(V, name="u") |
| 48 |
u.interpolate(f) |
| 49 |
|
| 50 |
tmp_file = tmp_path / "function.xdmf" |
| 51 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, tmp_file, "w") as xdmf: |
| 52 |
xdmf.write_mesh(mesh) |
| 53 |
xdmf.write_function(u) |
| 54 |
|
| 55 |
MPI.COMM_WORLD.barrier() |
| 56 |
|
| 57 |
in_grid = io4dolfinx.read_mesh(tmp_file, MPI.COMM_WORLD, backend="xdmf") |
| 58 |
names = io4dolfinx.read_function_names( |
| 59 |
tmp_file, MPI.COMM_WORLD, backend_args={}, backend="xdmf" |
| 60 |
) |
| 61 |
assert len(names) == 1 |
| 62 |
assert names[0] == "u" |
| 63 |
|
| 64 |
u = io4dolfinx.read_point_data(tmp_file, "u", in_grid, backend="xdmf") |
| 65 |
u_ref = dolfinx.fem.Function(u.function_space) |
| 66 |
u_ref.interpolate(f) |
| 67 |
eps = np.finfo(mesh.geometry.x.dtype).eps |
| 68 |
np.testing.assert_allclose(u.x.array, u_ref.x.array, atol=eps) |