| 1 |
from mpi4py import MPI |
| 2 |
|
| 3 |
import dolfinx |
| 4 |
import numpy as np |
| 5 |
import pytest |
| 6 |
import ufl |
| 7 |
|
| 8 |
from adios4dolfinx import read_mesh, write_mesh |
| 9 |
from adios4dolfinx.adios2_helpers import adios2 |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.parametrize("encoder, suffix", [("BP4", ".bp"), ("HDF5", ".h5"), ("BP5", ".bp")]) |
| 13 |
@pytest.mark.parametrize( |
| 14 |
"ghost_mode", [dolfinx.mesh.GhostMode.shared_facet, dolfinx.mesh.GhostMode.none] |
| 15 |
) |
| 16 |
@pytest.mark.parametrize("store_partition", [True, False]) |
| 17 |
def test_mesh_read_writer(encoder, suffix, ghost_mode, tmp_path, store_partition): |
| 18 |
N = 7 |
| 19 |
# Consistent tmp dir across processes |
| 20 |
fname = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 21 |
file = fname / f"adios_mesh_{encoder}_{store_partition}" |
| 22 |
xdmf_file = fname / "xdmf_mesh_{encode}_{ghost_mode}_{store_partition}" |
| 23 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, N, N, N, ghost_mode=ghost_mode) |
| 24 |
|
| 25 |
write_mesh(file.with_suffix(suffix), mesh, encoder, store_partition_info=store_partition) |
| 26 |
mesh.comm.Barrier() |
| 27 |
with dolfinx.io.XDMFFile(mesh.comm, xdmf_file.with_suffix(".xdmf"), "w") as xdmf: |
| 28 |
xdmf.write_mesh(mesh) |
| 29 |
mesh.comm.Barrier() |
| 30 |
|
| 31 |
mesh_adios = read_mesh( |
| 32 |
file.with_suffix(suffix), |
| 33 |
MPI.COMM_WORLD, |
| 34 |
engine=encoder, |
| 35 |
ghost_mode=ghost_mode, |
| 36 |
read_from_partition=store_partition, |
| 37 |
) |
| 38 |
mesh_adios.comm.Barrier() |
| 39 |
if store_partition: |
| 40 |
|
| 41 |
def compute_distance_matrix(points_A, points_B, tol=1e-12): |
| 42 |
points_A_e = np.expand_dims(points_A, 1) |
| 43 |
points_B_e = np.expand_dims(points_B, 0) |
| 44 |
distances = np.sum(np.square(points_A_e - points_B_e), axis=2) |
| 45 |
return distances < tol |
| 46 |
|
| 47 |
cell_map = mesh.topology.index_map(mesh.topology.dim) |
| 48 |
new_cell_map = mesh_adios.topology.index_map(mesh_adios.topology.dim) |
| 49 |
assert cell_map.size_local == new_cell_map.size_local |
| 50 |
assert cell_map.num_ghosts == new_cell_map.num_ghosts |
| 51 |
mesh.topology.create_connectivity(mesh.topology.dim, mesh.topology.dim) |
| 52 |
midpoints = dolfinx.mesh.compute_midpoints( |
| 53 |
mesh, |
| 54 |
mesh.topology.dim, |
| 55 |
np.arange(cell_map.size_local + cell_map.num_ghosts, dtype=np.int32), |
| 56 |
) |
| 57 |
mesh_adios.topology.create_connectivity(mesh_adios.topology.dim, mesh_adios.topology.dim) |
| 58 |
new_midpoints = dolfinx.mesh.compute_midpoints( |
| 59 |
mesh_adios, |
| 60 |
mesh_adios.topology.dim, |
| 61 |
np.arange(new_cell_map.size_local + new_cell_map.num_ghosts, dtype=np.int32), |
| 62 |
) |
| 63 |
# Check that all points in owned by initial mesh is owned by the new mesh |
| 64 |
# (might be locally reordered) |
| 65 |
owned_distances = compute_distance_matrix( |
| 66 |
midpoints[: cell_map.size_local], new_midpoints[: new_cell_map.size_local] |
| 67 |
) |
| 68 |
np.testing.assert_allclose(np.sum(owned_distances, axis=1), 1) |
| 69 |
# Check that all points that are ghosted in original mesh is ghosted on the |
| 70 |
# same process in the new mesh |
| 71 |
ghost_distances = compute_distance_matrix( |
| 72 |
midpoints[cell_map.size_local :], new_midpoints[new_cell_map.size_local :] |
| 73 |
) |
| 74 |
np.testing.assert_allclose(np.sum(ghost_distances, axis=1), 1) |
| 75 |
|
| 76 |
mesh.comm.Barrier() |
| 77 |
|
| 78 |
with dolfinx.io.XDMFFile(mesh.comm, xdmf_file.with_suffix(".xdmf"), "r") as xdmf: |
| 79 |
mesh_xdmf = xdmf.read_mesh(ghost_mode=ghost_mode) |
| 80 |
|
| 81 |
for i in range(mesh.topology.dim + 1): |
| 82 |
mesh.topology.create_entities(i) |
| 83 |
mesh_xdmf.topology.create_entities(i) |
| 84 |
mesh_adios.topology.create_entities(i) |
| 85 |
assert ( |
| 86 |
mesh_xdmf.topology.index_map(i).size_global |
| 87 |
== mesh_adios.topology.index_map(i).size_global |
| 88 |
) |
| 89 |
|
| 90 |
# Check that integration over different entities are consistent |
| 91 |
measures = ( |
| 92 |
[ufl.ds, ufl.dx] if ghost_mode is dolfinx.mesh.GhostMode.none else [ufl.ds, ufl.dS, ufl.dx] |
| 93 |
) |
| 94 |
for measure in measures: |
| 95 |
c_adios = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_adios))) |
| 96 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 97 |
c_xdmf = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_xdmf))) |
| 98 |
assert np.isclose( |
| 99 |
mesh_adios.comm.allreduce(c_adios, MPI.SUM), |
| 100 |
mesh.comm.allreduce(c_xdmf, MPI.SUM), |
| 101 |
) |
| 102 |
assert np.isclose( |
| 103 |
mesh_adios.comm.allreduce(c_adios, MPI.SUM), |
| 104 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize("encoder, suffix", [("BP4", ".bp"), ("BP5", ".bp")]) |
| 109 |
@pytest.mark.parametrize( |
| 110 |
"ghost_mode", [dolfinx.mesh.GhostMode.shared_facet, dolfinx.mesh.GhostMode.none] |
| 111 |
) |
| 112 |
@pytest.mark.parametrize("store_partition", [True, False]) |
| 113 |
def test_timedep_mesh(encoder, suffix, ghost_mode, tmp_path, store_partition): |
| 114 |
# Currently unsupported, unclear why ("HDF5", ".h5"), |
| 115 |
N = 13 |
| 116 |
# Consistent tmp dir across processes |
| 117 |
fname = MPI.COMM_WORLD.bcast(tmp_path, root=0) |
| 118 |
file = fname / f"adios_time_dep_mesh_{encoder}" |
| 119 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, N, N, N, ghost_mode=ghost_mode) |
| 120 |
|
| 121 |
def u(x): |
| 122 |
return np.asarray([x[0] + 0.1 * np.sin(x[1]), 0.2 * np.cos(x[1]), x[2]]) |
| 123 |
|
| 124 |
write_mesh( |
| 125 |
file.with_suffix(suffix), |
| 126 |
mesh, |
| 127 |
encoder, |
| 128 |
mode=adios2.Mode.Write, |
| 129 |
time=0.0, |
| 130 |
store_partition_info=store_partition, |
| 131 |
) |
| 132 |
delta_x = u(mesh.geometry.x.T).T |
| 133 |
mesh.geometry.x[:] += delta_x |
| 134 |
write_mesh(file.with_suffix(suffix), mesh, encoder, mode=adios2.Mode.Append, time=3.0) |
| 135 |
mesh.geometry.x[:] -= delta_x |
| 136 |
|
| 137 |
mesh_first = read_mesh( |
| 138 |
file.with_suffix(suffix), |
| 139 |
MPI.COMM_WORLD, |
| 140 |
encoder, |
| 141 |
ghost_mode, |
| 142 |
time=0.0, |
| 143 |
read_from_partition=store_partition, |
| 144 |
) |
| 145 |
mesh_first.comm.Barrier() |
| 146 |
|
| 147 |
# Check that integration over different entities are consistent |
| 148 |
measures = [ufl.ds, ufl.dx] |
| 149 |
if ghost_mode == dolfinx.mesh.GhostMode.shared_facet: |
| 150 |
measures.append(ufl.dx) |
| 151 |
for measure in measures: |
| 152 |
c_adios = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_first))) |
| 153 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 154 |
assert np.isclose( |
| 155 |
mesh_first.comm.allreduce(c_adios, MPI.SUM), |
| 156 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 157 |
) |
| 158 |
|
| 159 |
mesh.geometry.x[:] += delta_x |
| 160 |
mesh_second = read_mesh( |
| 161 |
file.with_suffix(suffix), |
| 162 |
MPI.COMM_WORLD, |
| 163 |
encoder, |
| 164 |
ghost_mode, |
| 165 |
time=3.0, |
| 166 |
read_from_partition=store_partition, |
| 167 |
) |
| 168 |
mesh_second.comm.Barrier() |
| 169 |
measures = [ufl.ds, ufl.dx] |
| 170 |
if ghost_mode == dolfinx.mesh.GhostMode.shared_facet: |
| 171 |
measures.append(ufl.dx) |
| 172 |
for measure in measures: |
| 173 |
c_adios = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh_second))) |
| 174 |
c_ref = dolfinx.fem.assemble_scalar(dolfinx.fem.form(1 * measure(domain=mesh))) |
| 175 |
assert np.isclose( |
| 176 |
mesh_second.comm.allreduce(c_adios, MPI.SUM), |
| 177 |
mesh.comm.allreduce(c_ref, MPI.SUM), |
| 178 |
) |