| 1 |
# # Storing mesh partition |
| 2 |
# This data is re-ordered when reading in a mesh, as the mesh is partitioned. |
| 3 |
# This means that when storing the mesh to disk from DOLFINx, the geometry and |
| 4 |
# connectivity arrays are re-ordered. |
| 5 |
# If we want to avoid to re-partition the mesh every time you run a simulation |
| 6 |
# (on a fixed number of processes), one can store the partitioning of the mesh |
| 7 |
# in the checkpoint. This is done by setting the flag `store_partition_info=True` |
| 8 |
# when calling {py:func}`adios4dolfinx.write_mesh`. |
| 9 |
|
| 10 |
# + |
| 11 |
import logging |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
import ipyparallel as ipp |
| 15 |
|
| 16 |
|
| 17 |
def write_partitioned_mesh(filename: Path): |
| 18 |
import subprocess |
| 19 |
|
| 20 |
from mpi4py import MPI |
| 21 |
|
| 22 |
import dolfinx |
| 23 |
|
| 24 |
import adios4dolfinx |
| 25 |
|
| 26 |
# Create a simple unit square mesh |
| 27 |
mesh = dolfinx.mesh.create_unit_square( |
| 28 |
MPI.COMM_WORLD, |
| 29 |
10, |
| 30 |
10, |
| 31 |
cell_type=dolfinx.mesh.CellType.quadrilateral, |
| 32 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 33 |
) |
| 34 |
|
| 35 |
# Write mesh checkpoint |
| 36 |
adios4dolfinx.write_mesh(filename, mesh, engine="BP4", store_partition_info=True) |
| 37 |
# Inspect checkpoint on rank 0 with `bpls` |
| 38 |
if mesh.comm.rank == 0: |
| 39 |
output = subprocess.run(["bpls", "-a", "-l", filename], capture_output=True) |
| 40 |
print(output.stdout.decode("utf-8")) |
| 41 |
|
| 42 |
|
| 43 |
# - |
| 44 |
|
| 45 |
# We inspect the partitioned mesh |
| 46 |
|
| 47 |
# + |
| 48 |
mesh_file = Path("partitioned_mesh.bp") |
| 49 |
n = 3 |
| 50 |
|
| 51 |
with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster: |
| 52 |
query = cluster[:].apply_async(write_partitioned_mesh, mesh_file) |
| 53 |
query.wait() |
| 54 |
assert query.successful(), query.error |
| 55 |
print("".join(query.stdout)) |
| 56 |
# - |
| 57 |
|
| 58 |
# # Reading a partitioned mesh |
| 59 |
|
| 60 |
# If we try to read the mesh in on a different number of processes, we will get an error. |
| 61 |
# We illustrate this below, by first trying to read the mesh using partitioning information, |
| 62 |
# which is done by setting the flag `read_from_partition=True` when calling |
| 63 |
# {py:func}`adios4dolfinx.read_mesh`. |
| 64 |
|
| 65 |
|
| 66 |
def read_partitioned_mesh(filename: Path, read_from_partition: bool = True): |
| 67 |
from mpi4py import MPI |
| 68 |
|
| 69 |
import adios4dolfinx |
| 70 |
|
| 71 |
prefix = f"{MPI.COMM_WORLD.rank + 1}/{MPI.COMM_WORLD.size}: " |
| 72 |
try: |
| 73 |
mesh = adios4dolfinx.read_mesh( |
| 74 |
filename, comm=MPI.COMM_WORLD, engine="BP4", read_from_partition=read_from_partition |
| 75 |
) |
| 76 |
print(f"{prefix} Mesh: {mesh.name} read successfully with {read_from_partition=}") |
| 77 |
except ValueError as e: |
| 78 |
print(f"{prefix} Caught exception: ", e) |
| 79 |
|
| 80 |
|
| 81 |
with ipp.Cluster(engines="mpi", n=n + 1, log_level=logging.ERROR) as cluster: |
| 82 |
# Read mesh from file with different number of processes |
| 83 |
query = cluster[:].apply_async(read_partitioned_mesh, mesh_file) |
| 84 |
query.wait() |
| 85 |
assert query.successful(), query.error |
| 86 |
print("".join(query.stdout)) |
| 87 |
|
| 88 |
# Read mesh from file with different number of processes (not using partitioning information). |
| 89 |
# If we instead turn of `read_from_partition`, we can read the mesh on a |
| 90 |
# different number of processes. |
| 91 |
|
| 92 |
with ipp.Cluster(engines="mpi", n=n + 1, log_level=logging.ERROR) as cluster: |
| 93 |
query = cluster[:].apply_async(read_partitioned_mesh, mesh_file, False) |
| 94 |
query.wait() |
| 95 |
assert query.successful(), query.error |
| 96 |
print("".join(query.stdout)) |
| 97 |
|
| 98 |
# Read mesh from file with same number of processes as was written, |
| 99 |
# re-using partitioning information. |
| 100 |
|
| 101 |
with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster: |
| 102 |
query = cluster[:].apply_async(read_partitioned_mesh, mesh_file, True) |
| 103 |
query.wait() |
| 104 |
assert query.successful(), query.error |
| 105 |
print("".join(query.stdout)) |