Debian Codegraph @

docs/quickstart.md

raw
1 # Quick Start Guide
2
3 This document provides a quick start overview of the functions available in
4 `io4dolfinx`. The library is designed to extend DOLFINx with advanced Input/Output
5 capabilities, focusing on flexible checkpointing and support for various data formats.
6
7 ## Core Checkpointing
8
9 The primary purpose of `io4dolfinx` is to support **N-to-M checkpointing**. This means
10 you can run a simulation on $N$ processes, save the state, and restart the simulation
11 on $M$ processes.
12
13 ### Meshes
14
15 Before storing any functions, the mesh must be written to the checkpoint file.
16 The mesh topology and geometry are saved in a distributed format.
17
18 ```python
19 from mpi4py import MPI
20 import dolfinx
21 import io4dolfinx
22 ```
23
24 ```python
25 comm = MPI.COMM_WORLD
26 mesh = dolfinx.mesh.create_unit_square(comm, 10, 10)
27 filename = "checkpoint.bp"
28 ```
29
30 Write mesh to file
31
32 ```python
33 #
34 io4dolfinx.write_mesh(filename, mesh)
35 ```
36
37 Read mesh from file. The mesh is redistributed according to the current communicator size.
38
39 ```python
40 mesh_new = io4dolfinx.read_mesh(filename, comm)
41 ```
42
43 ### Functions
44
45 Functions can be stored associated with a timestamp. They effectively store the
46 coefficients of the finite element function.
47
48 ```python
49 V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
50 u = dolfinx.fem.Function(V)
51 u.name = "my_solution"
52 ```
53
54 Write function
55
56 ```python
57 io4dolfinx.write_function(filename, u, time=0.5)
58 ```
59
60 Read function
61 ```{note}
62 You must read the mesh first (or have a compatible mesh ready, see [Checkpoint on input mesh](./original_checkpoint.py) for details).
63 ````
64
65 ```python
66 u_new = dolfinx.fem.Function(V)
67 io4dolfinx.read_function(filename, u_new, time=0.5, name="my_solution")
68 ```
69
70 ## Mesh Tags and Data
71
72 `io4dolfinx` supports storing auxiliary data associated with the mesh, such as subdomain
73 markers (`MeshTags`) or raw data arrays.
74
75 ### MeshTags
76
77 MeshTags (markers for cells, facets, etc.) can be written to the same checkpoint file
78 as the mesh. They are re-distributed correctly when reading back on a different number of processes.
79
80 Create some dummy tags
81 ```python
82 subdomains = dolfinx.mesh.meshtags(mesh, mesh.topology.dim, [0], [1])
83 ```
84
85 Write tags
86 ```python
87 io4dolfinx.write_meshtags(filename, mesh, subdomains, meshtag_name="subdomains")
88 ```
89
90 Read tags
91 ```python
92 tags = io4dolfinx.read_meshtags(filename, mesh, meshtag_name="subdomains")
93 ```
94
95 ## Advanced Checkpointing Strategies
96
97 Beyond standard N-to-M checkpointing, the library offers specialized strategies for
98 specific use cases.
99
100 ### Snapshot Checkpointing
101
102 A **snapshot** is a lightweight checkpoint intended for use within the *same* simulation
103 run (N-to-N). It is ideal for temporary storage (e.g., for an adjoint solver or
104 saving state before a risky operation) where you know the process count and mesh partitioning
105 will not change.
106
107 ```python
108 snapshot_file = "temp_snapshot.bp"
109 io4dolfinx.snapshot_checkpoint(u, snapshot_file, io4dolfinx.FileMode.write)
110 ```
111
112 Read back (must be on same mesh distribution)
113 ```python
114 io4dolfinx.snapshot_checkpoint(u, snapshot_file, io4dolfinx.FileMode.read)
115 ```
116
117 See the [Snapshot Checkpointing Guide](./snapshot_checkpoint.py) for more details and examples.
118
119 ### Original Mesh Checkpointing
120
121 Sometimes you want to save a solution that corresponds exactly to the input mesh file
122 (e.g., an `.xdmf` file you started with), rather than the current partitioned mesh.
123 This is useful for visualization or post-processing on the original geometry.
124
125 ```python
126 io4dolfinx.write_function_on_input_mesh("solution_on_input.bp", u)
127 ```
128
129 See the [Checkpoint on input mesh](./original_checkpoint.py) for more details and examples.
130
131 ## Legacy DOLFIN Support
132
133 The library provides readers for migrating data from legacy DOLFIN (FEniCS).
134
135 * **`read_mesh_from_legacy_h5`**: Reads a mesh from a legacy HDF5 file.
136 * **`read_function_from_legacy_h5`**: Reads a function from a legacy HDF5 file
137 (supports both `HDF5File` and `XDMFFile` archives).
138
139 See the [reading_legacy_data.md](./reading_legacy_data.md) guide for detailed examples.
140
141
142
143 ## Metadata and Utilities
144
145 Helper functions are available to query the contents of a checkpoint file.
146
147 * **`read_function_names`**: Returns a list of all functions stored in a file.
148
149 * **`read_timestamps`**: Returns the time steps available for a specific function.
150
151 * **`read_attributes` / `write_attributes`**: Allows storing arbitrary metadata dictionaries.
152
153
154 ## Backends
155
156 `io4dolfinx` is backend-agnostic. You can choose the storage engine by passing
157 the `backend` argument to most functions.
158
159 1. **`adios2` (Default)**: Uses the ADIOS2 library. Best for large-scale parallel IO.
160 Supports engines like "BP4", "BP5", and "HDF5".
161
162
163 2. **`h5py`**: Uses the standard HDF5 library via `h5py`. Requires an MPI-enabled HDF5 build.
164 Good for compatibility with other HDF5 tools.
165
166
167 3. **`vtkhdf`**: Supports reading and writing the VTKHDF format (scalable VTK).
168
169
170 4. **`pyvista`**: Primarily for reading unstructured grids (`.vtu`) via PyVista/meshio.
171
172
173 5. **`xdmf`**: Basic support for reading XDMF data.