| 1 |
# # Writing a function checkpoint |
| 2 |
# In the previous sections, we have gone in to quite some detail as to how |
| 3 |
# to store meshes with io4dolfinx. |
| 4 |
# This section will explain how to store {py:class}`functions<dolfinx.fem.Function>`, |
| 5 |
# and how to read them back in. |
| 6 |
|
| 7 |
# We start by creating a {py:class}`mesh<dolfinx.mesh.Mesh>` |
| 8 |
|
| 9 |
# + |
| 10 |
import logging |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
from mpi4py import MPI |
| 14 |
|
| 15 |
import dolfinx |
| 16 |
import ipyparallel as ipp |
| 17 |
|
| 18 |
import io4dolfinx |
| 19 |
|
| 20 |
assert MPI.COMM_WORLD.size == 1, "This example should only be run with 1 MPI process" |
| 21 |
|
| 22 |
mesh = dolfinx.mesh.create_unit_square( |
| 23 |
MPI.COMM_WORLD, nx=10, ny=10, cell_type=dolfinx.cpp.mesh.CellType.quadrilateral |
| 24 |
) |
| 25 |
# - |
| 26 |
|
| 27 |
# Next, we create a function, and interpolate a polynomial function into the function space |
| 28 |
|
| 29 |
# + |
| 30 |
el = "N1curl" |
| 31 |
degree = 3 |
| 32 |
V = dolfinx.fem.functionspace(mesh, (el, degree)) |
| 33 |
|
| 34 |
|
| 35 |
def f(x): |
| 36 |
return -(x[1] ** 2), x[0] - 2 * x[1] |
| 37 |
|
| 38 |
|
| 39 |
u = dolfinx.fem.Function(V) |
| 40 |
u.interpolate(f) |
| 41 |
# - |
| 42 |
|
| 43 |
# For the checkpointing, we start by storing the mesh to file |
| 44 |
|
| 45 |
filename = Path("function_checkpoint.bp") |
| 46 |
io4dolfinx.write_mesh(filename, mesh) |
| 47 |
|
| 48 |
# Next, we store the function to file, and associate it with a name. |
| 49 |
# Note that we can also associate a time stamp with it, as done for meshes in |
| 50 |
# [Writing time-dependent mesh checkpoint](./time_dependent_mesh). |
| 51 |
# We use {py:func}`io4dolfinx.write_function` for this. |
| 52 |
|
| 53 |
io4dolfinx.write_function(filename, u, time=0.3, name="my_curl_function") |
| 54 |
|
| 55 |
# Next, we want to read the function back in (using multiple MPI processes) |
| 56 |
# and check that the function is correct. |
| 57 |
# We use {py:func}`io4dolfinx.read_function` for this. |
| 58 |
# |
| 59 |
# ```{admonition} What mesh to use? |
| 60 |
# Note that we have read in the mesh with {py:func}`io4dolfinx.read_mesh` |
| 61 |
# before reading in the function. |
| 62 |
# We **cannot** use {py:func}`io4dolfinx.read_function` to read function data to the |
| 63 |
# original `mesh` object. |
| 64 |
# To do this, we need to use {py:func}`io4dolfinx.write_function_on_input_mesh`, see |
| 65 |
# [Writing function on input mesh checkpoint](./original_checkpoint). |
| 66 |
# for more details. |
| 67 |
# ``` |
| 68 |
|
| 69 |
|
| 70 |
def read_function(filename: Path, timestamp: float): |
| 71 |
from mpi4py import MPI |
| 72 |
|
| 73 |
import dolfinx |
| 74 |
import numpy as np |
| 75 |
|
| 76 |
import io4dolfinx |
| 77 |
|
| 78 |
in_mesh = io4dolfinx.read_mesh(filename, MPI.COMM_WORLD) |
| 79 |
W = dolfinx.fem.functionspace(in_mesh, (el, degree)) |
| 80 |
u_ref = dolfinx.fem.Function(W) |
| 81 |
u_ref.interpolate(f) |
| 82 |
u_in = dolfinx.fem.Function(W) |
| 83 |
io4dolfinx.read_function(filename, u_in, time=timestamp, name="my_curl_function") |
| 84 |
np.testing.assert_allclose(u_ref.x.array, u_in.x.array, atol=1e-14) |
| 85 |
print( |
| 86 |
f"{MPI.COMM_WORLD.rank + 1}/{MPI.COMM_WORLD.size}: ", |
| 87 |
f"Function read in correctly at time {timestamp}", |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
with ipp.Cluster(engines="mpi", n=3, log_level=logging.ERROR) as cluster: |
| 92 |
cluster[:].push({"f": f, "el": el, "degree": degree}) |
| 93 |
query = cluster[:].apply_async(read_function, filename, 0.3) |
| 94 |
query.wait() |
| 95 |
assert query.successful(), query.error |
| 96 |
print("".join(query.stdout)) |