| 1 |
# # Time-dependent mesh checkpoints |
| 2 |
# As we have seen in the previous examples, we store information about the connectivity, |
| 3 |
# the coordinates of the mesh nodes, |
| 4 |
# as well as a reference element. Note that the only thing that can change for a mesh |
| 5 |
# during a simulation are the coordinate of the mesh nodes. |
| 6 |
# In the following example, we will demonstrate how to write a time-dependent mesh |
| 7 |
# checkpoint to disk. |
| 8 |
|
| 9 |
# First, we create a simple function to compute the volume of a mesh |
| 10 |
|
| 11 |
# + |
| 12 |
import logging |
| 13 |
from pathlib import Path |
| 14 |
|
| 15 |
from mpi4py import MPI |
| 16 |
|
| 17 |
import ipyparallel as ipp |
| 18 |
|
| 19 |
import adios4dolfinx |
| 20 |
|
| 21 |
|
| 22 |
def compute_volume(mesh, time_stamp): |
| 23 |
from mpi4py import MPI |
| 24 |
|
| 25 |
import dolfinx |
| 26 |
import ufl |
| 27 |
|
| 28 |
# Compute the volume of the mesh |
| 29 |
vol_form = dolfinx.fem.form(1 * ufl.dx(domain=mesh)) |
| 30 |
vol_local = dolfinx.fem.assemble_scalar(vol_form) |
| 31 |
vol_glob = mesh.comm.allreduce(vol_local, op=MPI.SUM) |
| 32 |
if mesh.comm.rank == 0: |
| 33 |
print(f"{mesh.comm.rank + 1}/{mesh.comm.size} Time: {time_stamp} Mesh Volume: {vol_glob}") |
| 34 |
|
| 35 |
|
| 36 |
def write_meshes(filename: Path): |
| 37 |
from mpi4py import MPI |
| 38 |
|
| 39 |
import dolfinx |
| 40 |
import numpy as np |
| 41 |
|
| 42 |
import adios4dolfinx |
| 43 |
|
| 44 |
# Create a unit cube |
| 45 |
mesh = dolfinx.mesh.create_unit_cube( |
| 46 |
MPI.COMM_WORLD, |
| 47 |
3, |
| 48 |
6, |
| 49 |
5, |
| 50 |
cell_type=dolfinx.mesh.CellType.hexahedron, |
| 51 |
ghost_mode=dolfinx.mesh.GhostMode.shared_facet, |
| 52 |
) |
| 53 |
|
| 54 |
# Write mesh to file, associated with time stamp 1.5 |
| 55 |
adios4dolfinx.write_mesh(filename, mesh, engine="BP4", time=1.5) |
| 56 |
compute_volume(mesh, 1.5) |
| 57 |
mesh.geometry.x[:, 0] += 0.1 * mesh.geometry.x[:, 0] |
| 58 |
mesh.geometry.x[:, 1] += 0.3 * mesh.geometry.x[:, 1] * np.sin(mesh.geometry.x[:, 2]) |
| 59 |
compute_volume(mesh, 3.3) |
| 60 |
# Write mesh to file, associated with time stamp 3.3 |
| 61 |
# Note that we set the mode to append, as we have already created the file |
| 62 |
# and we do not want to overwrite the existing data |
| 63 |
adios4dolfinx.write_mesh( |
| 64 |
filename, mesh, engine="BP4", time=3.3, mode=adios4dolfinx.adios2_helpers.adios2.Mode.Append |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
# - |
| 69 |
|
| 70 |
# We write the sequence of meshes to file |
| 71 |
|
| 72 |
# + |
| 73 |
mesh_file = Path("timedep_mesh.bp") |
| 74 |
n = 3 |
| 75 |
|
| 76 |
with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster: |
| 77 |
# Write mesh to file |
| 78 |
cluster[:].push({"compute_volume": compute_volume}) |
| 79 |
query = cluster[:].apply_async(write_meshes, mesh_file) |
| 80 |
query.wait() |
| 81 |
assert query.successful(), query.error |
| 82 |
print("".join(query.stdout)) |
| 83 |
# - |
| 84 |
|
| 85 |
# # Reading a time dependent mesh |
| 86 |
# The only thing we need to do to read the mesh is to send in the associated time stamp, |
| 87 |
# which we do by adding `time=time_stamp` when calling {py:func}`adios4dolfinx.read_mesh`. |
| 88 |
|
| 89 |
second_mesh = adios4dolfinx.read_mesh(mesh_file, comm=MPI.COMM_WORLD, engine="BP4", time=3.3) |
| 90 |
compute_volume(second_mesh, 3.3) |
| 91 |
|
| 92 |
first_mesh = adios4dolfinx.read_mesh(mesh_file, comm=MPI.COMM_WORLD, engine="BP4", time=1.5) |
| 93 |
compute_volume(first_mesh, 1.5) |
| 94 |
|
| 95 |
# We observe that the volume of the mesh has changed, as we have perturbed the mesh |
| 96 |
# between the two time stamps. |
| 97 |
# We also note that we can read the meshes in on a different number of processes than |
| 98 |
# we wrote them with and in a different order (as long as the time stamps are correct). |