Debian Codegraph @

tests/test_checkpointing.py

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