| 1 |
# # Checkpoint on input mesh |
| 2 |
# As we have discussed earlier, one can choose to store function data in a way that |
| 3 |
# is N-to-M compatible by using {py:func}`adios4dolfinx.write_checkpoint`. |
| 4 |
# This stores the distributed mesh in it's current (partitioned) ordering, and does |
| 5 |
# use the original input data ordering for the cells and connectivity. |
| 6 |
# This means that you cannot use your original mesh (from `.xdmf` files) or mesh tags |
| 7 |
# together with the checkpoint. The checkpoint has to store the mesh and associated |
| 8 |
# mesh-tags. |
| 9 |
|
| 10 |
# An optional way of store an N-to-M checkpoint is to store the function data in the same |
| 11 |
# ordering as the mesh. The write operation will be more expensive, as it requires data |
| 12 |
# communication to ensure contiguous data being written to the checkpoint. |
| 13 |
# The method is exposed as {py:func}`adios4dolfinx.write_function_on_input_mesh`. |
| 14 |
# Below we will demonstrate this method. |
| 15 |
|
| 16 |
# + |
| 17 |
import logging |
| 18 |
from pathlib import Path |
| 19 |
from typing import Tuple |
| 20 |
|
| 21 |
import ipyparallel as ipp |
| 22 |
|
| 23 |
|
| 24 |
def locate_facets(x, tol=1.0e-12): |
| 25 |
return abs(x[0]) < tol |
| 26 |
|
| 27 |
|
| 28 |
def create_xdmf_mesh(filename: Path): |
| 29 |
from mpi4py import MPI |
| 30 |
|
| 31 |
import dolfinx |
| 32 |
|
| 33 |
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10) |
| 34 |
facets = dolfinx.mesh.locate_entities_boundary(mesh, mesh.topology.dim - 1, locate_facets) |
| 35 |
facet_tag = dolfinx.mesh.meshtags(mesh, mesh.topology.dim - 1, facets, 1) |
| 36 |
facet_tag.name = "FacetTag" |
| 37 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, filename.with_suffix(".xdmf"), "w") as xdmf: |
| 38 |
xdmf.write_mesh(mesh) |
| 39 |
xdmf.write_meshtags(facet_tag, mesh.geometry) |
| 40 |
print(f"{mesh.comm.rank + 1}/{mesh.comm.size} Mesh written to {filename.with_suffix('.xdmf')}") |
| 41 |
|
| 42 |
|
| 43 |
mesh_file = Path("MyMesh.xdmf") |
| 44 |
with ipp.Cluster(engines="mpi", n=4, log_level=logging.ERROR) as cluster: |
| 45 |
# Create a mesh and write to XDMFFile |
| 46 |
cluster[:].push({"locate_facets": locate_facets}) |
| 47 |
query = cluster[:].apply_async(create_xdmf_mesh, mesh_file) |
| 48 |
query.wait() |
| 49 |
assert query.successful(), query.error |
| 50 |
print("".join(query.stdout)) |
| 51 |
# - |
| 52 |
|
| 53 |
# Next, we will create a function on the mesh and write it to a checkpoint. |
| 54 |
|
| 55 |
|
| 56 |
# + |
| 57 |
def f(x): |
| 58 |
return (x[0] + x[1]) * (x[0] < 0.5), x[1], x[2] - x[1] |
| 59 |
|
| 60 |
|
| 61 |
def write_function( |
| 62 |
mesh_filename: Path, function_filename: Path, element: Tuple[str, int, Tuple[int,]] |
| 63 |
): |
| 64 |
from mpi4py import MPI |
| 65 |
|
| 66 |
import dolfinx |
| 67 |
|
| 68 |
import adios4dolfinx |
| 69 |
|
| 70 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_filename, "r") as xdmf: |
| 71 |
mesh = xdmf.read_mesh() |
| 72 |
V = dolfinx.fem.functionspace(mesh, element) |
| 73 |
u = dolfinx.fem.Function(V) |
| 74 |
u.interpolate(f) |
| 75 |
|
| 76 |
adios4dolfinx.write_function_on_input_mesh( |
| 77 |
function_filename.with_suffix(".bp"), |
| 78 |
u, |
| 79 |
mode=adios4dolfinx.adios2_helpers.adios2.Mode.Write, |
| 80 |
time=0.0, |
| 81 |
name="Output", |
| 82 |
) |
| 83 |
print( |
| 84 |
f"{mesh.comm.rank + 1}/{mesh.comm.size} Function written to ", |
| 85 |
f"{function_filename.with_suffix('.bp')}", |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
# - |
| 90 |
|
| 91 |
# Read in mesh and write function to file |
| 92 |
|
| 93 |
element = ("DG", 4, (3,)) |
| 94 |
function_file = Path("MyFunction.bp") |
| 95 |
with ipp.Cluster(engines="mpi", n=2, log_level=logging.ERROR) as cluster: |
| 96 |
cluster[:].push({"f": f}) |
| 97 |
query = cluster[:].apply_async(write_function, mesh_file, function_file, element) |
| 98 |
query.wait() |
| 99 |
assert query.successful(), query.error |
| 100 |
print("".join(query.stdout)) |
| 101 |
|
| 102 |
|
| 103 |
# Finally, we will read in the mesh from file and the function from the checkpoint |
| 104 |
# and compare it with the analytical solution. |
| 105 |
|
| 106 |
|
| 107 |
def verify_checkpoint( |
| 108 |
mesh_filename: Path, function_filename: Path, element: Tuple[str, int, Tuple[int,]] |
| 109 |
): |
| 110 |
from mpi4py import MPI |
| 111 |
|
| 112 |
import dolfinx |
| 113 |
import numpy as np |
| 114 |
|
| 115 |
import adios4dolfinx |
| 116 |
|
| 117 |
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_filename, "r") as xdmf: |
| 118 |
in_mesh = xdmf.read_mesh() |
| 119 |
V = dolfinx.fem.functionspace(in_mesh, element) |
| 120 |
u_in = dolfinx.fem.Function(V) |
| 121 |
adios4dolfinx.read_function(function_filename.with_suffix(".bp"), u_in, time=0.0, name="Output") |
| 122 |
|
| 123 |
# Compute exact interpolation |
| 124 |
u_ex = dolfinx.fem.Function(V) |
| 125 |
u_ex.interpolate(f) |
| 126 |
|
| 127 |
np.testing.assert_allclose(u_in.x.array, u_ex.x.array) |
| 128 |
print( |
| 129 |
"Successfully read checkpoint onto mesh on rank ", |
| 130 |
f"{in_mesh.comm.rank + 1}/{in_mesh.comm.size}", |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
# Verify checkpoint by comparing to exact solution |
| 135 |
|
| 136 |
with ipp.Cluster(engines="mpi", n=5, log_level=logging.ERROR) as cluster: |
| 137 |
cluster[:].push({"f": f}) |
| 138 |
query = cluster[:].apply_async(verify_checkpoint, mesh_file, function_file, element) |
| 139 |
query.wait() |
| 140 |
assert query.successful(), query.error |
| 141 |
print("".join(query.stdout)) |