Debian Codegraph @

tests/test_checkpointing.py

raw
1 import itertools
2 from pathlib import Path
3 from unittest.mock import Mock
4
5 from mpi4py import MPI
6
7 import basix
8 import basix.ufl
9 import dolfinx
10 import numpy as np
11 import pytest
12
13 import io4dolfinx
14
15 dtypes = [np.float64, np.float32] # Mesh geometry dtypes
16 write_comm = [MPI.COMM_SELF, MPI.COMM_WORLD] # Communicators for creating mesh
17
18 two_dimensional_cell_types = [
19 dolfinx.mesh.CellType.triangle,
20 dolfinx.mesh.CellType.quadrilateral,
21 ]
22 three_dimensional_cell_types = [
23 dolfinx.mesh.CellType.tetrahedron,
24 dolfinx.mesh.CellType.hexahedron,
25 ]
26
27 two_dim_combinations = itertools.product(dtypes, two_dimensional_cell_types, write_comm)
28 three_dim_combinations = itertools.product(dtypes, three_dimensional_cell_types, write_comm)
29
30
31 @pytest.fixture(params=two_dim_combinations, scope="module")
32 def mesh_2D(request):
33 dtype, cell_type, write_comm = request.param
34 mesh = dolfinx.mesh.create_unit_square(write_comm, 10, 10, cell_type=cell_type, dtype=dtype)
35 return mesh
36
37
38 @pytest.fixture(params=three_dim_combinations, scope="module")
39 def mesh_3D(request):
40 dtype, cell_type, write_comm = request.param
41 M = 5
42 mesh = dolfinx.mesh.create_unit_cube(write_comm, M, M, M, cell_type=cell_type, dtype=dtype)
43 return mesh
44
45
46 @pytest.mark.parametrize("is_complex", [True, False])
47 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
48 @pytest.mark.parametrize("degree", [1, 4])
49 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
50 def test_read_write_P_2D(
51 read_comm,
52 family,
53 degree,
54 is_complex,
55 mesh_2D,
56 get_dtype,
57 write_function,
58 read_function,
59 backend,
60 ):
61 mesh = mesh_2D
62 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
63
64 el = basix.ufl.element(
65 family,
66 mesh.basix_cell(),
67 degree,
68 basix.LagrangeVariant.gll_warped,
69 shape=(mesh.geometry.dim,),
70 dtype=mesh.geometry.x.dtype,
71 )
72
73 def f(x):
74 values = np.empty((2, x.shape[1]), dtype=f_dtype)
75 values[0] = np.full(x.shape[1], np.pi) + x[0]
76 values[1] = x[0]
77 if is_complex:
78 values[0] += 1j * x[1]
79 values[1] -= 3j * x[1]
80 return values
81
82 hash = write_function(mesh, el, f, f_dtype, backend=backend)
83 MPI.COMM_WORLD.Barrier()
84 read_function(read_comm, el, f, hash, f_dtype, backend=backend)
85
86
87 @pytest.mark.parametrize("is_complex", [True, False])
88 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
89 @pytest.mark.parametrize("degree", [1, 4])
90 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
91 def test_read_write_P_3D(
92 read_comm,
93 family,
94 degree,
95 is_complex,
96 mesh_3D,
97 get_dtype,
98 write_function,
99 read_function,
100 backend,
101 ):
102 mesh = mesh_3D
103 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
104 el = basix.ufl.element(
105 family,
106 mesh.basix_cell(),
107 degree,
108 basix.LagrangeVariant.gll_warped,
109 shape=(mesh.geometry.dim,),
110 dtype=mesh.geometry.x.dtype,
111 )
112
113 def f(x):
114 values = np.empty((3, x.shape[1]), dtype=f_dtype)
115 values[0] = np.pi + x[0]
116 values[1] = x[1] + 2 * x[0]
117 values[2] = np.cos(x[2])
118 if is_complex:
119 values[0] -= 2j * x[2]
120 values[2] += 1j * x[1]
121 return values
122
123 hash = write_function(mesh, el, f, f_dtype, backend=backend)
124
125 MPI.COMM_WORLD.Barrier()
126 read_function(read_comm, el, f, hash, f_dtype, backend=backend)
127
128
129 @pytest.mark.parametrize("is_complex", [True, False])
130 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
131 @pytest.mark.parametrize("degree", [1, 4])
132 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
133 def test_read_write_P_2D_time(
134 read_comm,
135 family,
136 degree,
137 is_complex,
138 mesh_2D,
139 get_dtype,
140 write_function_time_dep,
141 read_function_time_dep,
142 backend,
143 ):
144 mesh = mesh_2D
145 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
146
147 el = basix.ufl.element(
148 family,
149 mesh.basix_cell(),
150 degree,
151 basix.LagrangeVariant.gll_warped,
152 shape=(mesh.geometry.dim,),
153 dtype=mesh.geometry.x.dtype,
154 )
155
156 def f0(x):
157 values = np.empty((2, x.shape[1]), dtype=f_dtype)
158 values[0] = np.full(x.shape[1], np.pi) + x[0]
159 values[1] = x[0]
160 if is_complex:
161 values[0] += x[1] * 1j
162 values[1] -= 3j * x[1]
163 return values
164
165 def f1(x):
166 values = np.empty((2, x.shape[1]), dtype=f_dtype)
167 values[0] = 2 * np.full(x.shape[1], np.pi) + x[0]
168 values[1] = -x[0] + 2 * x[1]
169 if is_complex:
170 values[0] += x[1] * 1j
171 values[1] += 3j * x[1]
172 return values
173
174 t0 = 0.8
175 t1 = 0.6
176 hash = write_function_time_dep(mesh, el, f0, f1, t0, t1, f_dtype, backend)
177 MPI.COMM_WORLD.Barrier()
178 read_function_time_dep(read_comm, el, f0, f1, t0, t1, hash, f_dtype, backend)
179
180
181 @pytest.mark.parametrize("is_complex", [True, False])
182 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
183 @pytest.mark.parametrize("degree", [1, 4])
184 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
185 def test_read_write_P_3D_time(
186 read_comm,
187 family,
188 degree,
189 is_complex,
190 mesh_3D,
191 get_dtype,
192 write_function_time_dep,
193 read_function_time_dep,
194 backend,
195 ):
196 mesh = mesh_3D
197 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
198 el = basix.ufl.element(
199 family,
200 mesh.basix_cell(),
201 degree,
202 basix.LagrangeVariant.gll_warped,
203 shape=(mesh.geometry.dim,),
204 dtype=mesh.geometry.x.dtype,
205 )
206
207 def f(x):
208 values = np.empty((3, x.shape[1]), dtype=f_dtype)
209 values[0] = np.pi + x[0]
210 values[1] = x[1] + 2 * x[0]
211 values[2] = np.cos(x[2])
212 if is_complex:
213 values[0] += 2j * x[2]
214 values[2] += 5j * x[1]
215 return values
216
217 def g(x):
218 values = np.empty((3, x.shape[1]), dtype=f_dtype)
219 values[0] = x[0]
220 values[1] = 2 * x[0]
221 values[2] = x[0]
222 if is_complex:
223 values[0] += np.pi * 2j * x[2]
224 values[1] += 1j * x[2]
225 values[2] += 1j * np.cos(x[1])
226 return values
227
228 t0 = 0.1
229 t1 = 1.3
230 hash = write_function_time_dep(mesh, el, g, f, t0, t1, f_dtype, backend)
231 MPI.COMM_WORLD.Barrier()
232 read_function_time_dep(read_comm, el, g, f, t0, t1, hash, f_dtype, backend)
233
234
235 @pytest.mark.parametrize(
236 "func, args",
237 [
238 (io4dolfinx.read_attributes, ("nonexisting_file", MPI.COMM_WORLD, "")),
239 (io4dolfinx.read_timestamps, ("nonexisting_file", MPI.COMM_WORLD, "")),
240 (io4dolfinx.read_meshtags, ("nonexisting_file", MPI.COMM_WORLD, None, "")),
241 (io4dolfinx.read_function, ("nonexisting_file", Mock())),
242 (io4dolfinx.read_mesh, ("nonexisting_file", MPI.COMM_WORLD)),
243 ],
244 )
245 def test_read_nonexisting_file_raises_FileNotFoundError(func, args, backend):
246 if backend == "adios":
247 suffix = ".bp"
248 else:
249 suffix = ".h5"
250 file = Path(args[0]).with_suffix(suffix)
251 with pytest.raises(FileNotFoundError):
252 func(file, *args[1:], backend=backend)
253
254
255 def test_read_function_with_invalid_name_raises_KeyError(tmp_path, backend):
256 if backend == "adios":
257 suffix = ".bp"
258 else:
259 suffix = ".h5"
260 comm = MPI.COMM_WORLD
261 f_path = comm.bcast(tmp_path, root=0)
262 filename = (f_path / "func").with_suffix(suffix)
263 mesh = dolfinx.mesh.create_unit_square(comm, 10, 10, cell_type=dolfinx.mesh.CellType.triangle)
264 V = dolfinx.fem.functionspace(mesh, ("P", 1))
265 u = dolfinx.fem.Function(V)
266 io4dolfinx.write_function(filename, u, time=0, name="some_name", backend=backend)
267 io4dolfinx.write_function(filename, u, time=0, name="some_other_name", backend=backend)
268 # variables = set(sorted(["some_name", "some_other_name"]))
269 with pytest.raises(KeyError):
270 io4dolfinx.read_function(filename, u, time=0, name="nonexisting_name", backend=backend)
271 # assert e.value.args[0] == (
272 # f"nonexisting_name not found in {filename}. Did you mean one of {variables}?"
273 # )
274
275
276 def test_read_timestamps(get_dtype, mesh_2D, tmp_path, backend):
277 if backend == "adios":
278 suffix = ".bp"
279 else:
280 suffix = ".h5"
281
282 mesh = mesh_2D
283 dtype = get_dtype(mesh.geometry.x.dtype, False)
284
285 el = basix.ufl.element(
286 "Lagrange",
287 mesh.basix_cell(),
288 1,
289 shape=(mesh.geometry.dim,),
290 dtype=mesh.geometry.x.dtype,
291 )
292 V = dolfinx.fem.functionspace(mesh, el)
293
294 u = dolfinx.fem.Function(V, dtype=dtype, name="u")
295 v = dolfinx.fem.Function(V, dtype=dtype, name="v")
296
297 f_path = mesh.comm.bcast(tmp_path, root=0)
298 filename = (f_path / "read_time_stamps").with_suffix(suffix)
299
300 t_u = [0.1, 1.4]
301 t_v = [0.45, 1.2]
302
303 io4dolfinx.write_mesh(filename, mesh, backend=backend)
304 io4dolfinx.write_function(filename, u, time=t_u[0], backend=backend)
305 io4dolfinx.write_function(filename, v, time=t_v[0], backend=backend)
306 io4dolfinx.write_function(filename, u, time=t_u[1], backend=backend)
307 io4dolfinx.write_function(filename, v, time=t_v[1], backend=backend)
308
309 timestamps_u = io4dolfinx.read_timestamps(
310 comm=mesh.comm, filename=filename, function_name="u", backend=backend
311 )
312 timestamps_v = io4dolfinx.read_timestamps(
313 comm=mesh.comm, filename=filename, function_name="v", backend=backend
314 )
315 assert np.allclose(timestamps_u, t_u)
316 assert np.allclose(timestamps_v, t_v)