Debian Codegraph @

docs/time_dependent_mesh.py

raw
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 io4dolfinx
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 io4dolfinx
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 io4dolfinx.write_mesh(filename, mesh, 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 io4dolfinx.write_mesh(filename, mesh, time=3.3, mode=io4dolfinx.FileMode.append)
64
65
66 # -
67
68 # We write the sequence of meshes to file
69
70 # +
71 mesh_file = Path("timedep_mesh.bp")
72 n = 3
73
74 with ipp.Cluster(engines="mpi", n=n, log_level=logging.ERROR) as cluster:
75 # Write mesh to file
76 cluster[:].push({"compute_volume": compute_volume})
77 query = cluster[:].apply_async(write_meshes, mesh_file)
78 query.wait()
79 assert query.successful(), query.error
80 print("".join(query.stdout))
81 # -
82
83 # # Reading a time dependent mesh
84 # The only thing we need to do to read the mesh is to send in the associated time stamp,
85 # which we do by adding `time=time_stamp` when calling {py:func}`io4dolfinx.read_mesh`.
86
87 second_mesh = io4dolfinx.read_mesh(
88 mesh_file, comm=MPI.COMM_WORLD, backend="adios2", backend_args={"engine": "BP4"}, time=3.3
89 )
90 compute_volume(second_mesh, 3.3)
91
92 first_mesh = io4dolfinx.read_mesh(
93 mesh_file, comm=MPI.COMM_WORLD, backend="adios2", backend_args={"engine": "BP4"}, time=1.5
94 )
95 compute_volume(first_mesh, 1.5)
96
97 # We observe that the volume of the mesh has changed, as we have perturbed the mesh
98 # between the two time stamps.
99 # We also note that we can read the meshes in on a different number of processes than
100 # we wrote them with and in a different order (as long as the time stamps are correct).