Debian Codegraph @

tests/test_checkpointing_vector.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 dtypes = [np.float64, np.float32] # Mesh geometry dtypes
12 write_comm = [MPI.COMM_SELF, MPI.COMM_WORLD] # Communicators for creating mesh
13
14
15 simplex_two_dim = itertools.product(dtypes, [dolfinx.mesh.CellType.triangle], write_comm)
16 simplex_three_dim = itertools.product(dtypes, [dolfinx.mesh.CellType.tetrahedron], write_comm)
17
18 non_simplex_two_dim = itertools.product(dtypes, [dolfinx.mesh.CellType.quadrilateral], write_comm)
19 non_simplex_three_dim = itertools.product(dtypes, [dolfinx.mesh.CellType.hexahedron], write_comm)
20
21
22 @pytest.fixture(params=simplex_two_dim, scope="module")
23 def simplex_mesh_2D(request):
24 dtype, cell_type, write_comm = request.param
25 mesh = dolfinx.mesh.create_unit_square(write_comm, 10, 10, cell_type=cell_type, dtype=dtype)
26 return mesh
27
28
29 @pytest.fixture(params=simplex_three_dim, scope="module")
30 def simplex_mesh_3D(request):
31 dtype, cell_type, write_comm = request.param
32 mesh = dolfinx.mesh.create_unit_cube(write_comm, 5, 5, 5, cell_type=cell_type, dtype=dtype)
33 return mesh
34
35
36 @pytest.fixture(params=non_simplex_two_dim, scope="module")
37 def non_simplex_mesh_2D(request):
38 dtype, cell_type, write_comm = request.param
39 mesh = dolfinx.mesh.create_unit_square(write_comm, 10, 10, cell_type=cell_type, dtype=dtype)
40 return mesh
41
42
43 @pytest.fixture(params=non_simplex_three_dim, scope="module")
44 def non_simplex_mesh_3D(request):
45 dtype, cell_type, write_comm = request.param
46 mesh = dolfinx.mesh.create_unit_cube(write_comm, 5, 5, 5, cell_type=cell_type, dtype=dtype)
47 return mesh
48
49
50 @pytest.mark.parametrize("is_complex", [True, False])
51 @pytest.mark.parametrize("family", ["N1curl", "RT"])
52 @pytest.mark.parametrize("degree", [1, 4])
53 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
54 def test_read_write_2D(
55 read_comm, family, degree, is_complex, simplex_mesh_2D, get_dtype, write_function, read_function
56 ):
57 mesh = simplex_mesh_2D
58 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
59 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, dtype=mesh.geometry.x.dtype)
60
61 def f(x):
62 values = np.empty((2, x.shape[1]), dtype=f_dtype)
63 values[0] = np.full(x.shape[1], np.pi) + x[0]
64 values[1] = x[1]
65 if is_complex:
66 values[0] += 2j * x[1]
67 values[1] += 2j * x[0]
68 return values
69
70 fname = write_function(mesh, el, f, f_dtype)
71 MPI.COMM_WORLD.Barrier()
72 read_function(read_comm, el, f, fname, f_dtype)
73
74
75 @pytest.mark.parametrize("is_complex", [True, False])
76 @pytest.mark.parametrize("family", ["N1curl", "RT"])
77 @pytest.mark.parametrize("degree", [1, 4])
78 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
79 def test_read_write_3D(
80 read_comm, family, degree, is_complex, simplex_mesh_3D, get_dtype, write_function, read_function
81 ):
82 mesh = simplex_mesh_3D
83 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
84 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, dtype=mesh.geometry.x.dtype)
85
86 def f(x):
87 values = np.empty((3, x.shape[1]), dtype=f_dtype)
88 values[0] = np.full(x.shape[1], np.pi)
89 values[1] = x[1] + 2 * x[0]
90 values[2] = np.cos(x[2])
91 if is_complex:
92 values[0] += 2j * x[2]
93 values[1] += 2j * np.cos(x[2])
94 return values
95
96 fname = write_function(mesh, el, f, dtype=f_dtype)
97 MPI.COMM_WORLD.Barrier()
98 read_function(read_comm, el, f, fname, dtype=f_dtype)
99
100
101 @pytest.mark.parametrize("is_complex", [True, False])
102 @pytest.mark.parametrize("family", ["RTCF"])
103 @pytest.mark.parametrize("degree", [1, 2, 3])
104 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
105 def test_read_write_2D_quad(
106 read_comm,
107 family,
108 degree,
109 is_complex,
110 non_simplex_mesh_2D,
111 get_dtype,
112 write_function,
113 read_function,
114 ):
115 mesh = non_simplex_mesh_2D
116 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
117 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, dtype=mesh.geometry.x.dtype)
118
119 def f(x):
120 values = np.empty((2, x.shape[1]), dtype=f_dtype)
121 values[0] = np.full(x.shape[1], np.pi)
122 values[1] = x[1] + 2 * x[0]
123 if is_complex:
124 values[0] += 2j * x[2]
125 values[1] += 2j * np.cos(x[2])
126 return values
127
128 hash = write_function(mesh, el, f, f_dtype)
129 MPI.COMM_WORLD.Barrier()
130 read_function(read_comm, el, f, hash, f_dtype)
131
132
133 @pytest.mark.parametrize("is_complex", [True, False])
134 @pytest.mark.parametrize("family", ["NCF"])
135 @pytest.mark.parametrize("degree", [1, 4])
136 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
137 def test_read_write_hex(
138 read_comm,
139 family,
140 degree,
141 is_complex,
142 non_simplex_mesh_3D,
143 get_dtype,
144 write_function,
145 read_function,
146 ):
147 mesh = non_simplex_mesh_3D
148 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
149 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, dtype=mesh.geometry.x.dtype)
150
151 def f(x):
152 values = np.empty((3, x.shape[1]), dtype=f_dtype)
153 values[0] = np.full(x.shape[1], np.pi) + x[0]
154 values[1] = np.cos(x[2])
155 values[2] = x[0]
156 if is_complex:
157 values[0] += 2j * x[2]
158 values[2] -= 1j * x[1]
159 return values
160
161 hash = write_function(mesh, el, f, dtype=f_dtype)
162 MPI.COMM_WORLD.Barrier()
163 read_function(read_comm, el, f, hash, dtype=f_dtype)
164
165
166 @pytest.mark.parametrize("is_complex", [True, False])
167 @pytest.mark.parametrize("family", ["RTCF"])
168 @pytest.mark.parametrize("degree", [1, 2, 3])
169 @pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD])
170 def test_read_write_multiple(
171 read_comm,
172 family,
173 degree,
174 is_complex,
175 non_simplex_mesh_2D,
176 get_dtype,
177 write_function,
178 read_function,
179 ):
180 mesh = non_simplex_mesh_2D
181 f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex)
182 el = basix.ufl.element(family, mesh.ufl_cell().cellname(), degree, dtype=mesh.geometry.x.dtype)
183
184 def f(x):
185 values = np.empty((2, x.shape[1]), dtype=f_dtype)
186 values[0] = np.full(x.shape[1], np.pi)
187 values[1] = x[1] + 2 * x[0]
188 if is_complex:
189 values[0] -= 2j * x[2]
190 values[1] += 2j * np.cos(x[2])
191 return values
192
193 def g(x):
194 values = np.empty((2, x.shape[1]), dtype=f_dtype)
195 values[0] = 2 * x[1]
196 values[1] = 3 * x[0]
197 if is_complex:
198 values[0] += 3j * x[0]
199 values[1] += 2j * x[0] * x[1]
200 return values
201
202 hash_f = write_function(mesh, el, f, dtype=f_dtype, name="f", append=False)
203 hash_g = write_function(mesh, el, g, dtype=f_dtype, name="g", append=True)
204 assert hash_f == hash_g
205
206 MPI.COMM_WORLD.Barrier()
207 read_function(read_comm, el, f, hash_f, dtype=f_dtype, name="f")
208 read_function(read_comm, el, g, hash_g, dtype=f_dtype, name="g")