Debian Codegraph @

docs/writing_mesh_checkpoint.py

raw
1 # # Writing a mesh checkpoint
2 #
3 # In this example, we will demonstrate how to write a mesh checkpoint to disk.
4 #
5 # We start by creating a simple {py:func}`unit-square mesh<dolfinx.mesh.create_unit_square>`.
6
7 # +
8 import logging
9 from pathlib import Path
10
11 from mpi4py import MPI
12
13 import dolfinx
14 import ipyparallel as ipp
15
16 mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
17 # -
18
19 # Note that when a mesh is created in DOLFINx, we send in a
20 # {py:class}`MPI communicator<mpi4py.MPI.Comm>`.
21 # The communicator is used to partition (distribute) the mesh across the available processes.
22 # This means that each process only have access to a sub-set of cells and nodes of the mesh.
23 # We can inspect these with the following commands:
24
25
26 def print_mesh_info(mesh: dolfinx.mesh.Mesh):
27 cell_map = mesh.topology.index_map(mesh.topology.dim)
28 node_map = mesh.geometry.index_map()
29 print(
30 f"Rank {mesh.comm.rank}: number of owned cells {cell_map.size_local}",
31 f", number of ghosted cells {cell_map.num_ghosts}\n",
32 f"Number of owned nodes {node_map.size_local}",
33 f", number of ghosted nodes {node_map.num_ghosts}",
34 )
35
36
37 print_mesh_info(mesh)
38
39 # ## Create a distributed mesh
40 # Next, we can use {py:mod}`IPython parallel<ipyparallel>` to inspect a partitioned
41 # {py:class}`mesh<dolfinx.mesh.Mesh>`.
42 # We create a convenience function for creating a mesh that
43 # {py:attr}`shares cells<dolfinx.mesh.GhostMode.shared_facet>` on the boundary
44 # between two processes if `ghosted=True`.
45
46
47 def create_distributed_mesh(ghosted: bool, N: int = 10):
48 """
49 Create a distributed mesh with N x N cells. Share cells on process boundaries
50 if ghosted is set to True
51 """
52 from mpi4py import MPI
53
54 import dolfinx
55
56 ghost_mode = dolfinx.mesh.GhostMode.shared_facet if ghosted else dolfinx.mesh.GhostMode.none
57 mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, N, N, ghost_mode=ghost_mode)
58 print(f"{ghost_mode=}")
59 print_mesh_info(mesh)
60
61
62 # Next we start up a new cluster with three engines.
63 # As we defined `print_mesh_info` locally on this process, we need to push it to all engines.
64
65 with ipp.Cluster(engines="mpi", n=3, log_level=logging.ERROR) as cluster:
66 # Push print_mesh_info to all engines
67 cluster[:].push({"print_mesh_info": print_mesh_info})
68
69 # Create mesh with ghosted cells
70 query_true = cluster[:].apply_async(create_distributed_mesh, True)
71 query_true.wait()
72 assert query_true.successful(), query_true.error
73 print("".join(query_true.stdout))
74 # Create mesh without ghosted cells
75 query_false = cluster[:].apply_async(create_distributed_mesh, False)
76 query_false.wait()
77 assert query_false.successful(), query_false.error
78 print("".join(query_false.stdout))
79
80 # ## Writing a mesh checkpoint
81 # The input data to a mesh is:
82 # - A geometry: the set of points in R^D that are part of each cell
83 # - A two-dimensional connectivity array: A list that indicates which nodes of the geometry
84 # is part of each cell
85 # - A {py:func}`reference element<basix.ufl.element>`: Used for push data back and
86 # forth from the reference element and computing Jacobians
87 # We now use {py:mod}`adios4dolfinx` to write a mesh to file.
88
89
90 def write_mesh(filename: Path):
91 import subprocess
92
93 from mpi4py import MPI
94
95 import dolfinx
96
97 import adios4dolfinx
98
99 # Create a simple unit square mesh
100 mesh = dolfinx.mesh.create_unit_square(
101 MPI.COMM_WORLD, 10, 10, cell_type=dolfinx.mesh.CellType.quadrilateral
102 )
103
104 # Write mesh checkpoint
105 adios4dolfinx.write_mesh(filename, mesh, engine="BP4")
106
107 # Inspect checkpoint on rank 0 with `bpls`
108 if mesh.comm.rank == 0:
109 output = subprocess.run(["bpls", "-a", "-l", str(filename.absolute())], capture_output=True)
110 print(output.stdout.decode("utf-8"))
111
112
113 # +
114 mesh_file = Path("mesh.bp")
115
116 with ipp.Cluster(engines="mpi", n=2, log_level=logging.ERROR) as cluster:
117 # Write mesh to file
118 query = cluster[:].apply_async(write_mesh, mesh_file)
119 query.wait()
120 assert query.successful(), query.error
121 print("".join(query.stdout))
122 # -
123
124 # We observe that we have stored all the data needed to re-create the mesh in the file `mesh.bp`.
125 # We can therefore read it (to any number of processes) with {py:func}`adios4dolfinx.read_mesh`
126
127
128 def read_mesh(filename: Path):
129 from mpi4py import MPI
130
131 import dolfinx
132
133 import adios4dolfinx
134
135 mesh = adios4dolfinx.read_mesh(
136 filename, comm=MPI.COMM_WORLD, engine="BP4", ghost_mode=dolfinx.mesh.GhostMode.none
137 )
138 print_mesh_info(mesh)
139
140
141 # ## Reading mesh checkpoints (N-to-M)
142 # We can now read the checkpoint on a different number of processes than we wrote it on.
143
144 with ipp.Cluster(engines="mpi", n=4, log_level=logging.ERROR) as cluster:
145 # Write mesh to file
146 cluster[:].push({"print_mesh_info": print_mesh_info})
147 query = cluster[:].apply_async(read_mesh, mesh_file)
148 query.wait()
149 assert query.successful(), query.error
150 print("".join(query.stdout))