Debian Codegraph @

tests/test_vtkhdf.py

raw
1 from mpi4py import MPI
2
3 import numpy as np
4 import pytest
5 import ufl
6 from dolfinx.fem import Function, assemble_scalar, form, functionspace
7 from dolfinx.io.vtkhdf import write_cell_data, write_mesh, write_point_data
8 from dolfinx.mesh import CellType, compute_midpoints, create_unit_cube, create_unit_square, meshtags
9
10 import io4dolfinx
11
12
13 def f(x, t):
14 return x[0] - 2 * x[1] + x[2] * t
15
16
17 def g(x, t):
18 return x[0], 2 * x[1], -x[2] * t
19
20
21 @pytest.mark.parametrize(
22 "cell_type", [CellType.tetrahedron, CellType.hexahedron, CellType.tetrahedron]
23 )
24 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
25 def test_read_write_timedep_mesh(dtype, tmp_path, cell_type):
26 comm = MPI.COMM_WORLD
27 tmp_path = comm.bcast(tmp_path, root=0)
28 comm.barrier()
29
30 # Write temporal data
31 mesh = create_unit_cube(comm, 5, 5, 5, dtype=dtype, cell_type=cell_type)
32 ref_vol = mesh.comm.allreduce(
33 assemble_scalar(form(1 * ufl.dx(domain=mesh), dtype=dtype)), op=MPI.SUM
34 )
35 ref_surf = mesh.comm.allreduce(
36 assemble_scalar(form(1 * ufl.ds(domain=mesh), dtype=dtype)), op=MPI.SUM
37 )
38
39 # Write temporal data
40 filename = tmp_path / f"timedep_mesh_{cell_type.name}_{np.dtype(dtype).name}.vtkhdf"
41 io4dolfinx.write_mesh(filename, mesh, time=0.3, backend="vtkhdf")
42 mesh.geometry.x[:, 0] += 0.05 * mesh.geometry.x[:, 0]
43 mesh.geometry.x[:, 1] *= 1.1 + np.sin(mesh.geometry.x[:, 0])
44
45 ref_pert_vol = mesh.comm.allreduce(
46 assemble_scalar(form(1 * ufl.dx(domain=mesh), dtype=dtype)), op=MPI.SUM
47 )
48 ref_pert_surf = mesh.comm.allreduce(
49 assemble_scalar(form(1 * ufl.ds(domain=mesh), dtype=dtype)), op=MPI.SUM
50 )
51
52 io4dolfinx.write_mesh(
53 filename,
54 mesh,
55 time=0.5,
56 backend="vtkhdf",
57 mode=io4dolfinx.FileMode.append,
58 )
59
60 in_mesh = io4dolfinx.read_mesh(filename, comm, time=0.5, backend="vtkhdf")
61 pert_vol = mesh.comm.allreduce(
62 assemble_scalar(form(1 * ufl.dx(domain=in_mesh), dtype=dtype)), op=MPI.SUM
63 )
64 pert_surf = mesh.comm.allreduce(
65 assemble_scalar(form(1 * ufl.ds(domain=in_mesh), dtype=dtype)), op=MPI.SUM
66 )
67 assert np.isclose(pert_vol, ref_pert_vol)
68 assert np.isclose(pert_surf, ref_pert_surf)
69
70 in_mesh = io4dolfinx.read_mesh(filename, comm, time=0.3, backend="vtkhdf")
71 vol = mesh.comm.allreduce(
72 assemble_scalar(form(1 * ufl.dx(domain=in_mesh), dtype=dtype)), op=MPI.SUM
73 )
74 surf = mesh.comm.allreduce(
75 assemble_scalar(form(1 * ufl.ds(domain=in_mesh), dtype=dtype)), op=MPI.SUM
76 )
77 assert np.isclose(vol, ref_vol)
78 assert np.isclose(surf, ref_surf)
79
80
81 @pytest.mark.parametrize("cell_type", [CellType.hexahedron, CellType.tetrahedron])
82 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
83 def test_write_point_data(dtype, tmp_path, cell_type):
84 comm = MPI.COMM_WORLD
85 tmp_path = comm.bcast(tmp_path, root=0)
86 comm.barrier()
87
88 # Write temporal data
89 mesh = create_unit_cube(comm, 5, 5, 5, dtype=dtype, cell_type=cell_type)
90 filename = tmp_path / f"point_data_{cell_type.name}_{np.dtype(dtype).name}.vtkhdf"
91 write_mesh(str(filename), mesh)
92 t = np.linspace(0.1, 1.2, 25)
93 num_nodes_local = mesh.geometry.index_map().size_local
94 for ti in t:
95 point_data = f(mesh.geometry.x.T[:, :num_nodes_local], ti)
96 write_point_data(str(filename), mesh, point_data, float(ti))
97 comm.barrier()
98
99 grid = io4dolfinx.read_mesh(filename=filename, comm=comm, time=None, backend="vtkhdf")
100 # Since we shuffle time we need to shuffle in the same way on each process
101 np.random.shuffle(t)
102 t = comm.bcast(t, root=0)
103 for tj in t:
104 u = io4dolfinx.read_point_data(
105 filename=filename, name="u", mesh=grid, time=tj, backend="vtkhdf"
106 )
107 v_ref = Function(u.function_space, dtype=u.x.array.dtype)
108 atol = 15 * np.finfo(u.x.array.dtype).eps
109 v_ref.interpolate(lambda x: f(x, tj))
110 np.testing.assert_allclose(u.x.array, v_ref.x.array, atol=atol)
111
112 # Test blocked data as well (with shuffled input timestep)
113 blocked_file = filename.with_stem(filename.stem + "_blocked")
114 write_mesh(str(blocked_file), mesh)
115 for tj in t:
116 point_data = np.asarray(g(mesh.geometry.x.T[:, :num_nodes_local], tj)).T.flatten()
117 write_point_data(str(blocked_file), mesh, point_data, float(tj))
118 comm.barrier()
119
120 np.random.shuffle(t)
121 t = comm.bcast(t, root=0)
122 for tk in t:
123 u = io4dolfinx.read_point_data(
124 filename=blocked_file, name="u", mesh=grid, time=tk, backend="vtkhdf"
125 )
126 v_ref = Function(u.function_space, dtype=u.x.array.dtype)
127 atol = 15 * np.finfo(u.x.array.dtype).eps
128 v_ref.interpolate(lambda x: g(x, tk))
129 np.testing.assert_allclose(u.x.array, v_ref.x.array, atol=atol)
130
131
132 @pytest.mark.parametrize("cell_type", [CellType.hexahedron, CellType.tetrahedron])
133 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
134 def test_write_cell_data(dtype, tmp_path, cell_type):
135 comm = MPI.COMM_WORLD
136 tmp_path = comm.bcast(tmp_path, root=0)
137 comm.barrier()
138
139 # Write temporal data
140 mesh = create_unit_cube(comm, 5, 5, 5, dtype=dtype, cell_type=cell_type)
141 filename = tmp_path / f"cell_data_{cell_type.name}_{np.dtype(dtype).name}.vtkhdf"
142 write_mesh(str(filename), mesh)
143
144 t = np.linspace(0.1, 1.2, 25)
145 num_cells_local = mesh.topology.index_map(mesh.topology.dim).size_local
146 midpoints = compute_midpoints(
147 mesh, mesh.topology.dim, np.arange(num_cells_local, dtype=np.int32)
148 )
149 for ti in t:
150 cell_data = f(midpoints.T, ti)
151 write_cell_data(str(filename), mesh, cell_data, float(ti))
152 comm.barrier()
153
154 grid = io4dolfinx.read_mesh(filename=filename, comm=comm, time=None, backend="vtkhdf")
155 # Since we shuffle time we need to shuffle in the same way on each process
156 np.random.shuffle(t)
157 t = comm.bcast(t, root=0)
158 for tj in t:
159 u = io4dolfinx.read_cell_data(
160 filename=filename, name="u", mesh=grid, time=tj, backend="vtkhdf"
161 )
162 v_ref = Function(u.function_space, dtype=u.x.array.dtype)
163 atol = 15 * np.finfo(u.x.array.dtype).eps
164 v_ref.interpolate(lambda x: f(x, tj))
165 np.testing.assert_allclose(u.x.array, v_ref.x.array, atol=atol)
166
167 # Test blocked data as well (with shuffled input timestep)
168 blocked_file = filename.with_stem(filename.stem + "_blocked")
169 write_mesh(str(blocked_file), mesh)
170 for tj in t:
171 cell_data = np.asarray(g(midpoints.T, tj)).T.flatten()
172 write_cell_data(str(blocked_file), mesh, cell_data, float(tj))
173 comm.barrier()
174
175 np.random.shuffle(t)
176 t = comm.bcast(t, root=0)
177 for tk in t:
178 u = io4dolfinx.read_cell_data(
179 filename=blocked_file, name="u", mesh=grid, time=tk, backend="vtkhdf"
180 )
181 v_ref = Function(u.function_space, dtype=u.x.array.dtype)
182 atol = 15 * np.finfo(u.x.array.dtype).eps
183 v_ref.interpolate(lambda x: g(x, tk))
184 np.testing.assert_allclose(u.x.array, v_ref.x.array, atol=atol)
185
186
187 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
188 def test_write_meshtags(dtype, tmp_path, generate_reference_map):
189 comm = MPI.COMM_WORLD
190 tmp_path = comm.bcast(tmp_path, root=0)
191 comm.barrier()
192
193 filename = tmp_path / f"meshtags_{np.dtype(dtype).name}.vtkhdf"
194
195 mesh = create_unit_cube(comm, 3, 3, 3, dtype=dtype, cell_type=CellType.hexahedron)
196 io4dolfinx.write_mesh(
197 filename,
198 mesh,
199 mode=io4dolfinx.FileMode.write,
200 time=1.0,
201 backend_args={"name": "hex"},
202 backend="vtkhdf",
203 )
204 dim = mesh.topology.dim
205 mesh.topology.create_connectivity(dim, mesh.topology.dim)
206 cmap = mesh.topology.index_map(dim)
207 cells = np.arange(cmap.size_local, dtype=np.int32)
208 ct = meshtags(mesh, dim, cells, cells + cmap.local_range[0])
209 io4dolfinx.write_meshtags(
210 filename, mesh, ct, "CellTags", backend_args={"name": "hex"}, backend="vtkhdf"
211 )
212 root = 0
213
214 org_map = generate_reference_map(mesh, ct, comm, root)
215 # Move mesh
216 mesh.geometry.x[:, 0] *= 1 + 0.2 * np.sin(mesh.geometry.x[:, 1])
217
218 io4dolfinx.write_mesh(
219 filename,
220 mesh,
221 mode=io4dolfinx.FileMode.append,
222 time=2.5,
223 backend_args={"name": "hex"},
224 backend="vtkhdf",
225 )
226
227 # Add stationary meshtags (after time loop)
228 mesh = create_unit_cube(comm, 7, 3, 5, dtype=dtype, cell_type=CellType.tetrahedron)
229 io4dolfinx.write_mesh(
230 filename,
231 mesh,
232 mode=io4dolfinx.FileMode.append,
233 time=1.0,
234 backend_args={"name": "tet"},
235 backend="vtkhdf",
236 )
237 dim = mesh.topology.dim
238 org_maps = {}
239 mesh.geometry.x[:, 0] *= 2.0 + mesh.geometry.x[:, 1]
240 io4dolfinx.write_mesh(
241 filename,
242 mesh,
243 mode=io4dolfinx.FileMode.append,
244 time=2.5,
245 backend_args={"name": "tet"},
246 backend="vtkhdf",
247 )
248 for dim in range(mesh.topology.dim + 1):
249 mesh.topology.create_connectivity(dim, mesh.topology.dim)
250 entities = np.arange(mesh.topology.index_map(dim).size_local, dtype=np.int32)
251 et = meshtags(mesh, dim, entities, entities + mesh.topology.index_map(dim).local_range[0])
252 io4dolfinx.write_meshtags(
253 filename, mesh, et, f"{dim}tags", backend_args={"name": "tet"}, backend="vtkhdf"
254 )
255 org_maps[dim] = generate_reference_map(mesh, et, comm, root)
256
257 tol = 15 * np.finfo(dtype).eps
258 # Read in hex grid from second time step
259 hex_mesh = io4dolfinx.read_mesh(
260 filename, comm, time=1.0, backend_args={"name": "hex"}, backend="vtkhdf"
261 )
262 hex_tag = io4dolfinx.read_meshtags(
263 filename, hex_mesh, "CellTags", backend_args={"name": "hex"}, backend="vtkhdf"
264 )
265 read_map = generate_reference_map(hex_mesh, hex_tag, comm, root)
266 # On root process, check that midpoints are the same for each value in the meshtag
267 if MPI.COMM_WORLD.rank == root:
268 assert len(org_map) == len(read_map)
269 for value, (_, midpoint) in org_map.items():
270 _, read_midpoint = read_map[value]
271 np.testing.assert_allclose(read_midpoint, midpoint, atol=tol)
272
273 # Read tet grid from second time step
274 tet_mesh = io4dolfinx.read_mesh(
275 filename, comm, time=2.5, backend_args={"name": "tet"}, backend="vtkhdf"
276 )
277 for dim in range(mesh.topology.dim + 1):
278 tet_tag = io4dolfinx.read_meshtags(
279 filename, tet_mesh, f"{dim}tags", backend_args={"name": "tet"}, backend="vtkhdf"
280 )
281 read_map = generate_reference_map(tet_mesh, tet_tag, comm, root)
282 if MPI.COMM_WORLD.rank == root:
283 assert len(org_maps[dim]) == len(read_map)
284 for value, (_, midpoint) in org_maps[dim].items():
285 _, read_midpoint = read_map[value]
286 np.testing.assert_allclose(read_midpoint, midpoint, atol=tol)
287
288
289 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
290 def test_read_write_pointdata(dtype, tmp_path):
291 tol = 15 * np.finfo(dtype).eps
292
293 comm = MPI.COMM_WORLD
294 tmp_path = comm.bcast(tmp_path, root=0)
295 comm.barrier()
296
297 filename = tmp_path / "point_data.vtkhdf"
298
299 mesh = create_unit_cube(comm, 3, 3, 3, dtype=dtype, cell_type=CellType.hexahedron)
300
301 def f(x, t):
302 return (x[0] + np.sin(x[1]) + np.cos(x[0] * t), x[2] + x[1] - t)
303
304 io4dolfinx.write_mesh(
305 filename,
306 mesh,
307 mode=io4dolfinx.FileMode.write,
308 time=1.0,
309 backend_args={"name": "hex"},
310 backend="vtkhdf",
311 )
312
313 f_name = "point_data"
314 V = functionspace(mesh, ("Lagrange", 2, (2,)))
315 u = Function(V, dtype=dtype, name=f_name)
316 u.interpolate(lambda x: f(x, 1.0))
317 io4dolfinx.write_point_data(
318 filename,
319 u,
320 mode=io4dolfinx.FileMode.append,
321 time=1.0,
322 backend_args={"name": "hex"},
323 backend="vtkhdf",
324 )
325
326 io4dolfinx.write_mesh(
327 filename,
328 mesh,
329 mode=io4dolfinx.FileMode.append,
330 time=2.0,
331 backend_args={"name": "hex"},
332 backend="vtkhdf",
333 )
334 u.interpolate(lambda x: f(x, 2.0))
335 io4dolfinx.write_point_data(
336 filename,
337 u,
338 mode=io4dolfinx.FileMode.append,
339 time=2.0,
340 backend_args={"name": "hex"},
341 backend="vtkhdf",
342 )
343
344 # Read in hex grid from second time step
345 hex_mesh = io4dolfinx.read_mesh(
346 filename, comm, time=2.0, backend_args={"name": "hex"}, backend="vtkhdf"
347 )
348 u_end = io4dolfinx.read_point_data(
349 filename,
350 mesh=hex_mesh,
351 name=f_name,
352 time=2.0,
353 backend_args={"name": "hex"},
354 backend="vtkhdf",
355 )
356 u_ref = Function(u_end.function_space, dtype=dtype)
357 u_ref.interpolate(lambda x: f(x, 2.0))
358 np.testing.assert_allclose(u_end.x.array, u_ref.x.array, atol=tol)
359
360
361 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
362 def test_read_write_celldata(dtype, tmp_path):
363 tol = 15 * np.finfo(dtype).eps
364
365 comm = MPI.COMM_WORLD
366 tmp_path = comm.bcast(tmp_path, root=0)
367 comm.barrier()
368
369 filename = tmp_path / "cell_data.vtkhdf"
370
371 mesh = create_unit_cube(comm, 3, 3, 3, dtype=dtype, cell_type=CellType.tetrahedron)
372
373 def f(x, t):
374 return (x[0] + np.sin(x[1]) + np.cos(x[0] * t), x[1] + t, x[0] - t)
375
376 t_0 = 2.2
377 t_1 = 3.0
378
379 backend_args = {"name": "Grid"}
380 io4dolfinx.write_mesh(
381 filename,
382 mesh,
383 mode=io4dolfinx.FileMode.write,
384 time=t_0,
385 backend_args=backend_args,
386 backend="vtkhdf",
387 )
388
389 f_name = "Data"
390 V = functionspace(mesh, ("DG", 0, (3,)))
391 u = Function(V, dtype=dtype, name=f_name)
392 u.interpolate(lambda x: f(x, t_0))
393 io4dolfinx.write_cell_data(
394 filename,
395 u,
396 mode=io4dolfinx.FileMode.append,
397 time=t_0,
398 backend_args=backend_args,
399 backend="vtkhdf",
400 )
401
402 io4dolfinx.write_mesh(
403 filename,
404 mesh,
405 mode=io4dolfinx.FileMode.append,
406 time=t_1,
407 backend_args=backend_args,
408 backend="vtkhdf",
409 )
410 u.interpolate(lambda x: f(x, t_1))
411 io4dolfinx.write_cell_data(
412 filename,
413 u,
414 mode=io4dolfinx.FileMode.append,
415 time=t_1,
416 backend_args=backend_args,
417 backend="vtkhdf",
418 )
419
420 for t in [t_1, t_0]:
421 grid = io4dolfinx.read_mesh(
422 filename, comm, time=t, backend_args=backend_args, backend="vtkhdf"
423 )
424 u_end = io4dolfinx.read_cell_data(
425 filename,
426 mesh=grid,
427 name=f_name,
428 time=t,
429 backend_args=backend_args,
430 backend="vtkhdf",
431 )
432 u_ref = Function(u_end.function_space, dtype=dtype)
433 u_ref.interpolate(lambda x: f(x, t))
434 np.testing.assert_allclose(u_end.x.array, u_ref.x.array, atol=tol)
435
436
437 @pytest.mark.parametrize("dtype", [np.float32, np.float64])
438 def test_read_write_mix_data(dtype, tmp_path):
439 tol = 15 * np.finfo(dtype).eps
440
441 mesh = create_unit_square(MPI.COMM_WORLD, 5, 7, dtype=dtype)
442
443 def f(x, t):
444 return x[0] + t * x[1]
445
446 def g(x, t):
447 return x[1] - x[0] * t**2
448
449 ts = [0.1, 0.3, 0.4, 0.5]
450
451 V = functionspace(mesh, ("Lagrange", 1))
452 u = Function(V, name="points", dtype=dtype)
453 z = Function(V, name="some_other_array", dtype=dtype)
454 Q = functionspace(mesh, ("DG", 0))
455 q = Function(Q, name="cells", dtype=dtype)
456 tmp_path = mesh.comm.bcast(tmp_path, root=0)
457 filename = tmp_path / "mixed_data.vtkhdf"
458
459 backend_args = {"name": "MyGrid"}
460 for i, t in enumerate(ts):
461 if np.isclose(t, 0.1):
462 mode = io4dolfinx.FileMode.write
463 else:
464 mode = io4dolfinx.FileMode.append
465 mesh.geometry.x[:] *= 1 + 0.1 * t
466 io4dolfinx.write_mesh(
467 filename, mesh, mode=mode, time=t, backend="vtkhdf", backend_args=backend_args
468 )
469 u.interpolate(lambda x: f(x, t))
470 q.interpolate(lambda x: g(x, t))
471 io4dolfinx.write_point_data(
472 filename,
473 u,
474 time=t,
475 mode=io4dolfinx.FileMode.append,
476 backend_args=backend_args,
477 backend="vtkhdf",
478 )
479 io4dolfinx.write_point_data(
480 filename,
481 z,
482 time=t,
483 mode=io4dolfinx.FileMode.append,
484 backend_args=backend_args,
485 backend="vtkhdf",
486 )
487
488 mesh_in = io4dolfinx.read_mesh(
489 filename, MPI.COMM_WORLD, time=t, backend_args=backend_args, backend="vtkhdf"
490 )
491 u_in = io4dolfinx.read_point_data(
492 filename, name=u.name, mesh=mesh_in, time=t, backend_args=backend_args, backend="vtkhdf"
493 )
494 u_ref = Function(u_in.function_space, dtype=dtype)
495 u_ref.interpolate(lambda x: f(x, t))
496 np.testing.assert_allclose(u_ref.x.array, u_in.x.array, atol=tol)
497 c_step = i
498 if not np.isclose(t, 0.3):
499 io4dolfinx.write_cell_data(
500 filename,
501 q,
502 time=t,
503 backend_args=backend_args,
504 mode=io4dolfinx.FileMode.append,
505 backend="vtkhdf",
506 )
507 else:
508 # Read in mesh from previous step as geometry adapts,
509 # while reading data from current step.
510 c_step = i - 1
511 mesh_in = io4dolfinx.read_mesh(
512 filename,
513 MPI.COMM_WORLD,
514 time=ts[c_step],
515 backend_args=backend_args,
516 backend="vtkhdf",
517 )
518 q_in = io4dolfinx.read_cell_data(
519 filename, name=q.name, mesh=mesh_in, time=t, backend_args=backend_args, backend="vtkhdf"
520 )
521 q_ref = Function(q_in.function_space, dtype=dtype)
522 q_ref.interpolate(lambda x: g(x, ts[c_step]))
523 np.testing.assert_allclose(q_ref.x.array, q_in.x.array, atol=tol)