Debian Codegraph @

docs/reading_legacy_data.md

raw
1 # Reading Legacy DOLFIN Data
2
3 `io4dolfinx` provides functionality to read meshes and functions created with the
4 legacy version of DOLFIN (often referred to as "Old FEniCS"). This allows users to
5 migrate data from older simulations to DOLFINx.
6
7 ## Supported Formats
8
9 The library supports reading data stored in the **HDF5** format used by legacy DOLFIN.
10 This includes:
11 * Meshes stored using `dolfin.HDF5File`.
12 * Functions stored using `dolfin.HDF5File`.
13 * Checkpoints stored using `dolfin.XDMFFile` (which produces an `.h5` file alongside the `.xdmf` file).
14
15 **Note:** The `.xml` or `.xml.gz` formats are not supported. You must convert these to
16 HDF5 using legacy DOLFIN before reading them with `io4dolfinx`.
17
18 ## Reading Meshes
19
20 To read a mesh, you must know the **group name** where the mesh is stored within the HDF5 file.
21 In legacy DOLFIN, this was often `/mesh` or the name you provided to the write function.
22
23 ```python
24 from mpi4py import MPI
25 import io4dolfinx
26 ```
27
28 ```python
29 comm = MPI.COMM_WORLD
30 filename = "legacy_mesh.h5"
31 ```
32
33 ### Read the mesh
34 Here 'group' is the path to the mesh inside the HDF5 file (e.g., "/mesh")
35 ```python
36 mesh = io4dolfinx.read_mesh_from_legacy_h5(
37 filename=filename,
38 comm=comm,
39 group="/mesh",
40 )
41 ```
42
43 ```python
44 print(f"Read mesh with topology dimension: {mesh.topology.dim}")
45 ```
46
47 ## Reading Functions
48
49 Reading a function requires you to first create a compatible **FunctionSpace** and **Function** in DOLFINx.
50 The data is then read from the file and loaded into this function.
51
52 ### Simple Function Read
53
54 If the function was saved directly using `HDF5File.write(u, "name")`:
55
56 ```python
57 import dolfinx
58 import io4dolfinx
59 from mpi4py import MPI
60 ```
61
62 #### Read the mesh first
63 ```python
64 comm = MPI.COMM_WORLD
65 filename = "legacy_data.h5"
66 mesh = io4dolfinx.read_mesh_from_legacy_h5(filename, comm, group="/mesh")
67 ```
68
69 #### Create the appropriate FunctionSpace
70 You must know the element family and degree used in the legacy simulation
71 ```python
72 V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))
73 ```
74
75
76 #### Create the function to hold the data
77 ```python
78 u = dolfinx.fem.Function(V)
79 ```
80
81 #### Read the data
82
83 Here 'group' corresponds to the name given when writing in legacy DOLFIN
84 ```python
85 io4dolfinx.read_function_from_legacy_h5(filename=filename, comm=comm, u=u, group="v")
86 ```
87
88 ### Reading from XDMF Checkpoints
89
90 If the data was saved using `XDMFFile.write_checkpoint`, the HDF5 structure is slightly different (often containing time steps). You can specify the `step` argument to read a specific snapshot.
91
92
93 Assuming mesh and function space V are already created as above
94
95 ```python
96 u_checkpoint = dolfinx.fem.Function(V)
97 ```
98
99 Read the first time step (step=0). Note that the filename should be the .h5 file generated by the XDMFFile
100
101 ```python
102 io4dolfinx.read_function_from_legacy_h5(
103 filename="checkpoint.h5",
104 comm=comm,
105 u=u_checkpoint,
106 group="v", # The name of the function in the checkpoint
107 step=0,
108 )
109 ```
110
111 ## Reading Vector Functions
112
113 Vector functions are read in the same way, provided the `FunctionSpace` is initialized correctly with the vector shape.
114
115 ```python
116 W = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
117 w = dolfinx.fem.Function(W)
118 ```
119
120 ```python
121 io4dolfinx.read_function_from_legacy_h5(
122 filename="legacy_vector.h5", comm=comm, u=w, group="velocity_function"
123 )
124 ```
125
126 ## Limitations
127
128 * **Function Types:** Only `Lagrange` (Continuous Galerkin) and `DG` (Discontinuous Galerkin)
129 functions are supported.
130 * **One Checkpoint per File:** The legacy reader generally expects one checkpoint series per file
131 structure for XDMF checkpoints.
132 * **Backends:** The default backend is `adios2`. Ensure you have an MPI-enabled build of HDF5
133 if using the `h5py` backend explicitly.