Debian Codegraph @

tests/test_snapshot_checkpoint.py

raw
1 from pathlib import Path
2
3 from mpi4py import MPI
4
5 import adios2
6 import basix.ufl
7 import dolfinx
8 import numpy as np
9 import pytest
10
11 from adios4dolfinx import snapshot_checkpoint
12 from adios4dolfinx.adios2_helpers import resolve_adios_scope
13
14 adios2 = resolve_adios_scope(adios2)
15
16
17 triangle = dolfinx.mesh.CellType.triangle
18 quad = dolfinx.mesh.CellType.quadrilateral
19 tetra = dolfinx.mesh.CellType.tetrahedron
20 hex = dolfinx.mesh.CellType.hexahedron
21
22
23 @pytest.mark.parametrize(
24 "cell_type, family", [(triangle, "N1curl"), (triangle, "RT"), (quad, "RTCF")]
25 )
26 @pytest.mark.parametrize("degree", [1, 4])
27 def test_read_write_2D(family, degree, cell_type, tmp_path):
28 mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, cell_type=cell_type)
29 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree)
30
31 def f(x):
32 return (np.full(x.shape[1], np.pi) + x[0], x[1])
33
34 V = dolfinx.fem.functionspace(mesh, el)
35 u = dolfinx.fem.Function(V)
36 u.interpolate(f)
37
38 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
39 file = fname / Path("snapshot_2D_vs.bp")
40 snapshot_checkpoint(u, file, adios2.Mode.Write)
41
42 v = dolfinx.fem.Function(V)
43 snapshot_checkpoint(v, file, adios2.Mode.Read)
44 assert np.allclose(u.x.array, v.x.array)
45
46
47 @pytest.mark.parametrize("cell_type, family", [(tetra, "N1curl"), (tetra, "RT"), (hex, "NCF")])
48 @pytest.mark.parametrize("degree", [1, 4])
49 def test_read_write_3D(family, degree, cell_type, tmp_path):
50 mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 3, 3, 3, cell_type=cell_type)
51 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree)
52
53 def f(x):
54 return (np.full(x.shape[1], np.pi) + x[0], x[1], x[1] * x[2])
55
56 V = dolfinx.fem.functionspace(mesh, el)
57 u = dolfinx.fem.Function(V)
58 u.interpolate(f)
59
60 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
61 file = fname / Path("snapshot_3D_vs.bp")
62 snapshot_checkpoint(u, file, adios2.Mode.Write)
63
64 v = dolfinx.fem.Function(V)
65 snapshot_checkpoint(v, file, adios2.Mode.Read)
66 assert np.allclose(u.x.array, v.x.array)
67
68
69 @pytest.mark.parametrize(
70 "cell_type", [dolfinx.mesh.CellType.triangle, dolfinx.mesh.CellType.quadrilateral]
71 )
72 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
73 @pytest.mark.parametrize("degree", [1, 4])
74 def test_read_write_P_2D(family, degree, cell_type, tmp_path):
75 mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 5, 5, cell_type=cell_type)
76 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, shape=(mesh.geometry.dim,))
77
78 def f(x):
79 return (np.full(x.shape[1], np.pi) + x[0], x[1])
80
81 V = dolfinx.fem.functionspace(mesh, el)
82 u = dolfinx.fem.Function(V)
83 u.interpolate(f)
84
85 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
86 file = fname / Path("snapshot_2D_p.bp")
87 snapshot_checkpoint(u, file, adios2.Mode.Write)
88
89 v = dolfinx.fem.Function(V)
90 snapshot_checkpoint(v, file, adios2.Mode.Read)
91 assert np.allclose(u.x.array, v.x.array)
92
93
94 @pytest.mark.parametrize(
95 "cell_type", [dolfinx.mesh.CellType.tetrahedron, dolfinx.mesh.CellType.hexahedron]
96 )
97 @pytest.mark.parametrize("family", ["Lagrange", "DG"])
98 @pytest.mark.parametrize("degree", [1, 4])
99 def test_read_write_P_3D(family, degree, cell_type, tmp_path):
100 mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 5, 5, 5, cell_type=cell_type)
101 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, shape=(mesh.geometry.dim,))
102
103 def f(x):
104 return (np.full(x.shape[1], np.pi) + x[0], x[1] + 2 * x[0], np.cos(x[2]))
105
106 V = dolfinx.fem.functionspace(mesh, el)
107 u = dolfinx.fem.Function(V)
108 u.interpolate(f)
109
110 fname = MPI.COMM_WORLD.bcast(tmp_path, root=0)
111 file = fname / Path("snapshot_3D_p.bp")
112 snapshot_checkpoint(u, file, adios2.Mode.Write)
113
114 v = dolfinx.fem.Function(V)
115 snapshot_checkpoint(v, file, adios2.Mode.Read)
116 assert np.allclose(u.x.array, v.x.array)