Coverage for /dolfinx-env/lib/python3.14/site-packages/io4dolfinx/backends/h5py_serial/backend.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-11-30 12:02 +0000

1"""The h5py backend for h5py built without MPI. 

2 

3Files are read by every process on its own and written through rank 0, see 

4:mod:`io4dolfinx.backends.serial`. They are the files of the h5py backend. 

5 

6SPDX License identifier: MIT 

7 

8Copyright: Jørgen S. Dokken, Simula Research Laboratory 

9""" 

10 

11from pathlib import Path 

12from typing import Any 

13 

14from mpi4py import MPI 

15 

16import dolfinx 

17import numpy as np 

18 

19from ...structures import FunctionData, MeshData, MeshTagsData 

20from .. import FileMode 

21from ..h5py import backend as parallel 

22from ..h5py import storage as h5_storage 

23from ..serial import ( 

24 gather_function, 

25 gather_mesh, 

26 gather_meshtags, 

27 gather_rows, 

28 serial, 

29 write_on_root, 

30) 

31 

32read_mode = parallel.read_mode 

33get_default_backend_args = parallel.get_default_backend_args 

34convert_file_mode = parallel.convert_file_mode 

35 

36read_metadata = serial(parallel.read_metadata) 

37read_attributes = serial(parallel.read_attributes) 

38read_timestamps = serial(parallel.read_timestamps) 

39read_mesh_data = serial(parallel.read_mesh_data) 

40read_meshtags_data = serial(parallel.read_meshtags_data) 

41read_dofmap = serial(parallel.read_dofmap) 

42read_dofs = serial(parallel.read_dofs) 

43read_cell_perms = serial(parallel.read_cell_perms) 

44read_legacy_mesh = serial(parallel.read_legacy_mesh) 

45read_hdf5_array = serial(parallel.read_hdf5_array) 

46read_function_names = serial(parallel.read_function_names) 

47read_point_data = parallel.read_point_data 

48read_cell_data = parallel.read_cell_data 

49write_data = parallel.write_data 

50 

51 

52def write_attributes( 

53 filename: Path | str, 

54 comm: MPI.Comm, 

55 name: str, 

56 attributes: dict[str, np.ndarray], 

57 backend_args: dict[str, Any] | None = None, 

58): 

59 """Write attributes to file, see :func:`io4dolfinx.backends.h5py.backend.write_attributes`.""" 

60 write_on_root( 

61 comm, 

62 attributes, 

63 lambda d: parallel.write_attributes(filename, MPI.COMM_SELF, name, d, backend_args), 

64 ) 

65 

66 

67def write_mesh( 

68 filename: Path | str, 

69 comm: MPI.Comm, 

70 mesh: MeshData, 

71 backend_args: dict[str, Any] | None = None, 

72 mode: FileMode = FileMode.write, 

73 time: float = 0.0, 

74): 

75 """Write a mesh to file, see :func:`io4dolfinx.backends.h5py.backend.write_mesh`.""" 

76 h5_storage.storage_options(comm, backend_args or {}) 

77 data = gather_mesh(comm, mesh) 

78 write_on_root( 

79 comm, 

80 data, 

81 lambda d: parallel.write_mesh(filename, MPI.COMM_SELF, d, backend_args, mode, time), 

82 ) 

83 

84 

85def write_meshtags( 

86 filename: str | Path, 

87 comm: MPI.Comm, 

88 data: MeshTagsData, 

89 backend_args: dict[str, Any] | None = None, 

90): 

91 """Write mesh tags to file, see :func:`io4dolfinx.backends.h5py.backend.write_meshtags`.""" 

92 h5_storage.storage_options(comm, backend_args or {}) 

93 tags = gather_meshtags(comm, data) 

94 write_on_root( 

95 comm, tags, lambda d: parallel.write_meshtags(filename, MPI.COMM_SELF, d, backend_args) 

96 ) 

97 

98 

99def write_function( 

100 filename: Path, 

101 comm: MPI.Comm, 

102 u: FunctionData, 

103 time: float, 

104 mode: FileMode, 

105 backend_args: dict[str, Any] | None, 

106): 

107 """Write a function to file, see :func:`io4dolfinx.backends.h5py.backend.write_function`.""" 

108 h5_storage.storage_options(comm, backend_args or {}) 

109 data = gather_function(comm, u) 

110 write_on_root( 

111 comm, 

112 data, 

113 lambda d: parallel.write_function(filename, MPI.COMM_SELF, d, time, mode, backend_args), 

114 ) 

115 

116 

117def snapshot_checkpoint( 

118 filename: Path | str, 

119 mode: FileMode, 

120 u: dolfinx.fem.Function, 

121 backend_args: dict[str, Any] | None, 

122): 

123 """Create or read a snapshot checkpoint, see 

124 :func:`io4dolfinx.backends.h5py.backend.snapshot_checkpoint`.""" 

125 if mode != FileMode.write: 

126 return serial(parallel.snapshot_checkpoint)(filename, mode, u, backend_args) 

127 comm = u.function_space.mesh.comm 

128 dofmap = u.function_space.dofmap 

129 start, stop = np.array(dofmap.index_map.local_range) * dofmap.index_map_bs 

130 options = h5_storage.storage_options(comm, backend_args or {}) 

131 values = gather_rows(comm, start, u.x.array[: stop - start]) 

132 

133 def write(dofs: np.ndarray): 

134 with parallel.h5pyfile(filename, filemode="w", comm=MPI.COMM_SELF) as h5file: 

135 dataset = h5_storage.create_dataset( 

136 h5file.create_group("snapshot"), "dofs", dofs.shape, dofs.dtype, options 

137 ) 

138 h5_storage.write_dataset(dataset, slice(None), dofs) 

139 

140 write_on_root(comm, values, write)