Debian Codegraph @

tests/test_snapshot_checkpoint.py

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