Debian Codegraph @

docs/adding_backend.md

raw
1 # Adding a custom backend
2
3 {py:mod}`io4dolfinx` is designed to be backend-agnostic, meaning you can implement custom readers and writers for different file formats by adhering to a specific protocol.
4
5 ## The IOBackend Protocol
6
7 Any backend must implement the {py:class}`IOBackend<io4dolfinx.backends.IOBackend>` protocol defined in {py:mod}`io4dolfinx.backends`. This protocol ensures that the backend provides all necessary methods for reading and writing meshes, functions, and attributes.
8
9 To use a custom backend, you simply pass the python module name (as a string) to the `backend` argument of any {py:mod}`io4dolfinx` function.
10 The library will attempt to import the module and use it as the backend.
11
12 ## Required Data Structures
13
14 Your backend will interact with several data classes defined in {py:mod}`io4dolfinx.structures`.
15 You should import these to type-hint your implementation correctly:
16
17 * {py:class}`MeshData<io4dolfinx.structures.MeshData>`: Contains local geometry, topology, and partitioning information for writing {py:class}`meshes<dolfinx.mesh.Mesh>`.
18 * {py:class}`ReadMeshData<io4dolfinx.structures.ReadMeshData>`: A container for returning mesh data (cells, geometry, etc.) when reading.
19 * {py:class}`FunctionData<io4dolfinx.structures.FunctionData>`: Contains function values, dofmaps, and permutation info for writing functions.
20 * {py:class}`MeshTagsData<io4dolfinx.structures.MeshTagsData>`: Contains indices, values, and metadata for mesh tags.
21
22 ## Implementation Checklist
23
24 Your backend module must implement the functions listed below. Note that `comm` is always an {py:class}`MPI.Comm<mpi4py.MPI.Comm>` and `filename` is a
25 {py:class}`pathlib.Path` or {py:class}`str`.
26
27 ### General Configuration
28
29 * {py:func}`~io4dolfinx.backends.IOBackend.get_default_backend_args`
30 * Returns a dictionary of default arguments (e.g., engine type) for your backend.
31
32 ### Attribute IO
33
34 * {py:func}`~io4dolfinx.backends.IOBackend.write_attributes`
35 * Writes a dictionary of attributes (key-value pairs) to the file under the specified `name` (group).
36 * {py:func}`~io4dolfinx.backends.IOBackend.read_attributes`
37 * Reads and returns attributes associated with `name`.
38
39 ### Mesh IO
40
41 * {py:func}`~io4dolfinx.backends.IOBackend.write_mesh`
42 * Writes mesh geometry, topology, and optionally partitioning data.
43 * Must handle {py:class}`FileMode.write` (new file) and {py:class}`FileMode.append`.
44 * {py:func}`~io4dolfinx.backends.IOBackend.read_mesh_data`
45 * Reads mesh geometry and topology at a specific `time`.
46 * If `read_from_partition` is True, it should read pre-calculated partitioning data to avoid re-partitioning.
47
48 ### MeshTags IO
49
50 * {py:func}`~io4dolfinx.backends.IOBackend.write_meshtags`
51 * Writes mesh tag indices and values.
52 * {py:func}`~io4dolfinx.backends.IOBackend.read_meshtags_data`
53 * Reads mesh tags identified by `name`.
54
55 ### Function IO
56
57 * {py:func}`~io4dolfinx.backends.IOBackend.write_function`
58 * Writes function values, global dofmaps, and cell permutations.
59 * {py:func}`~io4dolfinx.backends.IOBackend.read_dofmap`
60 * Reads the dofmap (connectivity) for the function `name`.
61 * {py:func}`~io4dolfinx.backends.IOBackend.read_dofs`
62 * Reads the local chunk of function values for a specific `time`.
63 * Returns the array of values and the global starting index of that chunk.
64 * {py:func}`~io4dolfinx.backends.IOBackend.read_cell_perms`
65 * Reads cell permutation data used to map input cells to the current mesh.
66 * {py:func}`~io4dolfinx.backends.IOBackend.read_timestamps`
67 * Returns all available time-steps for a given function.
68
69 ### Legacy Support (Optional but defined in protocol)
70
71 * {py:func}`~io4dolfinx.backends.IOBackend.read_legacy_mesh`
72 * Reads mesh data from legacy DOLFIN HDF5/XDMF formats.
73 * {py:func}`~io4dolfinx.backends.IOBackend.read_hdf5_array`
74 * Reads a raw array from an HDF5-like structure (used for legacy vector reading).
75
76 ### Snapshots
77
78 * {py:func}`~io4dolfinx.backends.IOBackend.snapshot_checkpoint`
79 * Handles lightweight N-to-N checkpointing where data is saved exactly as distributed in memory without global reordering.
80
81 ## Example Skeleton
82
83 ```python
84 from typing import Any
85 from pathlib import Path
86 from mpi4py import MPI
87 import numpy as np
88 import dolfinx
89 from io4dolfinx.structures import MeshData, FunctionData, MeshTagsData, ReadMeshData
90 from io4dolfinx.backends import FileMode
91
92
93 def get_default_backend_args(arguments: dict[str, Any] | None) -> dict[str, Any]:
94 return arguments or {}
95
96
97 def write_mesh(
98 filename: Path | str,
99 comm: MPI.Comm,
100 mesh: MeshData,
101 backend_args: dict[str, Any] | None,
102 mode: FileMode,
103 time: float,
104 ):
105 # Implementation here
106 pass
107
108
109 # ... Implement all other methods defined in IOBackend ...
110 ```