Debian Codegraph @

docs/original_checkpoint.py

raw
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}`io4dolfinx.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}`io4dolfinx.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 with dolfinx.io.XDMFFile(MPI.COMM_WORLD, filename.with_suffix(".xdmf"), "w") as xdmf:
37 xdmf.write_mesh(mesh)
38 xdmf.write_meshtags(facet_tag, mesh.geometry)
39 print(f"{mesh.comm.rank + 1}/{mesh.comm.size} Mesh written to {filename.with_suffix('.xdmf')}")
40
41
42 mesh_file = Path("MyMesh.xdmf")
43 with ipp.Cluster(engines="mpi", n=4, log_level=logging.ERROR) as cluster:
44 # Create a mesh and write to XDMFFile
45 cluster[:].push({"locate_facets": locate_facets})
46 query = cluster[:].apply_async(create_xdmf_mesh, mesh_file)
47 query.wait()
48 assert query.successful(), query.error
49 print("".join(query.stdout))
50 # -
51
52 # Next, we will create a function on the mesh and write it to a checkpoint.
53
54
55 # +
56 def f(x):
57 return (x[0] + x[1]) * (x[0] < 0.5), x[1], x[2] - x[1]
58
59
60 def write_function(
61 mesh_filename: Path, function_filename: Path, element: Tuple[str, int, Tuple[int,]]
62 ):
63 from mpi4py import MPI
64
65 import dolfinx
66
67 import io4dolfinx
68
69 with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_filename, "r") as xdmf:
70 mesh = xdmf.read_mesh()
71 V = dolfinx.fem.functionspace(mesh, element)
72 u = dolfinx.fem.Function(V)
73 u.interpolate(f)
74
75 io4dolfinx.write_function_on_input_mesh(
76 function_filename.with_suffix(".bp"),
77 u,
78 mode=io4dolfinx.FileMode.write,
79 time=0.0,
80 name="Output",
81 )
82 print(
83 f"{mesh.comm.rank + 1}/{mesh.comm.size} Function written to ",
84 f"{function_filename.with_suffix('.bp')}",
85 )
86
87
88 # -
89
90 # Read in mesh and write function to file
91
92 element = ("DG", 4, (3,))
93 function_file = Path("MyFunction.bp")
94 with ipp.Cluster(engines="mpi", n=2, log_level=logging.ERROR) as cluster:
95 cluster[:].push({"f": f})
96 query = cluster[:].apply_async(write_function, mesh_file, function_file, element)
97 query.wait()
98 assert query.successful(), query.error
99 print("".join(query.stdout))
100
101
102 # Finally, we will read in the mesh from file and the function from the checkpoint
103 # and compare it with the analytical solution.
104
105
106 def verify_checkpoint(
107 mesh_filename: Path, function_filename: Path, element: Tuple[str, int, Tuple[int,]]
108 ):
109 from mpi4py import MPI
110
111 import dolfinx
112 import numpy as np
113
114 import io4dolfinx
115
116 with dolfinx.io.XDMFFile(MPI.COMM_WORLD, mesh_filename, "r") as xdmf:
117 in_mesh = xdmf.read_mesh()
118 V = dolfinx.fem.functionspace(in_mesh, element)
119 u_in = dolfinx.fem.Function(V)
120 io4dolfinx.read_function(function_filename.with_suffix(".bp"), u_in, time=0.0, name="Output")
121
122 # Compute exact interpolation
123 u_ex = dolfinx.fem.Function(V)
124 u_ex.interpolate(f)
125
126 np.testing.assert_allclose(u_in.x.array, u_ex.x.array)
127 print(
128 "Successfully read checkpoint onto mesh on rank ",
129 f"{in_mesh.comm.rank + 1}/{in_mesh.comm.size}",
130 )
131
132
133 # Verify checkpoint by comparing to exact solution
134
135 with ipp.Cluster(engines="mpi", n=5, log_level=logging.ERROR) as cluster:
136 cluster[:].push({"f": f})
137 query = cluster[:].apply_async(verify_checkpoint, mesh_file, function_file, element)
138 query.wait()
139 assert query.successful(), query.error
140 print("".join(query.stdout))