| 1 |
# # Writing MeshTags data to a checkpoint file |
| 2 |
# In many scenarios, the mesh used in a checkpoint is not trivial, and subdomains and sub-entities |
| 3 |
# have been tagged with appropriate markers. |
| 4 |
# As the mesh gets redistributed when read |
| 5 |
# (see [Writing Mesh Checkpoint](./writing_mesh_checkpoint)), |
| 6 |
# we need to store any tags together with this new mesh. |
| 7 |
|
| 8 |
# As an example we will use a unit-cube, where each entity has been tagged with a unique index. |
| 9 |
|
| 10 |
# + |
| 11 |
import logging |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
from mpi4py import MPI |
| 15 |
|
| 16 |
import dolfinx |
| 17 |
import ipyparallel as ipp |
| 18 |
import numpy as np |
| 19 |
|
| 20 |
import io4dolfinx |
| 21 |
|
| 22 |
assert MPI.COMM_WORLD.size == 1, "This example should only be run with 1 MPI process" |
| 23 |
|
| 24 |
mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, nx=3, ny=4, nz=5) |
| 25 |
# - |
| 26 |
|
| 27 |
# We start by computing the unique global index of each (owned) entity in the mesh |
| 28 |
# as well as its corresponding midpoint |
| 29 |
|
| 30 |
entity_midpoints = {} |
| 31 |
meshtags = {} |
| 32 |
for i in range(mesh.topology.dim + 1): |
| 33 |
mesh.topology.create_entities(i) |
| 34 |
e_map = mesh.topology.index_map(i) |
| 35 |
|
| 36 |
# Compute midpoints of entities |
| 37 |
entities = np.arange(e_map.size_local, dtype=np.int32) |
| 38 |
mesh.topology.create_connectivity(i, mesh.topology.dim) |
| 39 |
entity_midpoints[i] = dolfinx.mesh.compute_midpoints(mesh, i, entities) |
| 40 |
# Associate each local index with its global index |
| 41 |
values = np.arange(e_map.size_local, dtype=np.int32) + e_map.local_range[0] |
| 42 |
meshtags[i] = dolfinx.mesh.meshtags(mesh, i, entities, values) |
| 43 |
|
| 44 |
# We use {py:func}`io4dolfinx.write_mesh` and `io4dolfinx.write_meshtags` to write the |
| 45 |
# {py:class}`dolfinx.mesh.Mesh` and {py:class}`dolfinx.mesh.MeshTags` to file. |
| 46 |
# We associate each meshtag with a name |
| 47 |
|
| 48 |
filename = Path("mesh_with_meshtags.bp") |
| 49 |
io4dolfinx.write_mesh(filename, mesh) |
| 50 |
for i, tag in meshtags.items(): |
| 51 |
io4dolfinx.write_meshtags(filename, mesh, tag, meshtag_name=f"meshtags_{i}") |
| 52 |
|
| 53 |
|
| 54 |
# Next we want to read the meshtags in on a different number of processes, |
| 55 |
# and check that the midpoints of each entity is still correct. |
| 56 |
# We do this with {py:func}`io4dolfinx.read_meshtags`. |
| 57 |
|
| 58 |
|
| 59 |
def verify_meshtags(filename: Path): |
| 60 |
# We assume that entity_midpoints have been sent to the engine |
| 61 |
from mpi4py import MPI |
| 62 |
|
| 63 |
import dolfinx |
| 64 |
import numpy as np |
| 65 |
|
| 66 |
import io4dolfinx |
| 67 |
|
| 68 |
read_mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD) |
| 69 |
prefix = f"{read_mesh.comm.rank + 1}/{read_mesh.comm.size}: " |
| 70 |
for i in range(read_mesh.topology.dim + 1): |
| 71 |
# Read mesh from file |
| 72 |
meshtags = io4dolfinx.read_meshtags(filename, read_mesh, meshtag_name=f"meshtags_{i}") |
| 73 |
|
| 74 |
# Compute midpoints for all local entities on process |
| 75 |
read_mesh.topology.create_connectivity(i, read_mesh.topology.dim) |
| 76 |
midpoints = dolfinx.mesh.compute_midpoints(read_mesh, i, meshtags.indices) |
| 77 |
# Compare locally computed midpoint with reference data |
| 78 |
for global_pos, midpoint in zip(meshtags.values, midpoints): |
| 79 |
np.testing.assert_allclose( |
| 80 |
entity_midpoints[i][global_pos], |
| 81 |
midpoint, |
| 82 |
err_msg=f"{prefix}: Midpoint ({i, global_pos}) do not match", |
| 83 |
) |
| 84 |
print(f"{prefix} Matching of all entities of dimension {i} successful") |
| 85 |
|
| 86 |
|
| 87 |
with ipp.Cluster(engines="mpi", n=3, log_level=logging.ERROR) as cluster: |
| 88 |
cluster[:].push({"entity_midpoints": entity_midpoints}) |
| 89 |
query = cluster[:].apply_async(verify_meshtags, filename) |
| 90 |
query.wait() |
| 91 |
assert query.successful(), query.error |
| 92 |
print("".join(query.stdout)) |