| 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, |
| 56 |
family, |
| 57 |
degree, |
| 58 |
is_complex, |
| 59 |
simplex_mesh_2D, |
| 60 |
get_dtype, |
| 61 |
write_function, |
| 62 |
read_function, |
| 63 |
backend, |
| 64 |
): |
| 65 |
mesh = simplex_mesh_2D |
| 66 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 67 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 68 |
|
| 69 |
def f(x): |
| 70 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 71 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 72 |
values[1] = x[1] |
| 73 |
if is_complex: |
| 74 |
values[0] += 2j * x[1] |
| 75 |
values[1] += 2j * x[0] |
| 76 |
return values |
| 77 |
|
| 78 |
fname = write_function(mesh, el, f, f_dtype, backend=backend) |
| 79 |
MPI.COMM_WORLD.Barrier() |
| 80 |
read_function(read_comm, el, f, fname, f_dtype, backend=backend) |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 84 |
@pytest.mark.parametrize("family", ["N1curl", "RT"]) |
| 85 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 86 |
@pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD]) |
| 87 |
def test_read_write_3D( |
| 88 |
read_comm, |
| 89 |
family, |
| 90 |
degree, |
| 91 |
is_complex, |
| 92 |
simplex_mesh_3D, |
| 93 |
get_dtype, |
| 94 |
write_function, |
| 95 |
read_function, |
| 96 |
backend, |
| 97 |
): |
| 98 |
mesh = simplex_mesh_3D |
| 99 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 100 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 101 |
|
| 102 |
def f(x): |
| 103 |
values = np.empty((3, x.shape[1]), dtype=f_dtype) |
| 104 |
values[0] = np.full(x.shape[1], np.pi) |
| 105 |
values[1] = x[1] + 2 * x[0] |
| 106 |
values[2] = np.cos(x[2]) |
| 107 |
if is_complex: |
| 108 |
values[0] += 2j * x[2] |
| 109 |
values[1] += 2j * np.cos(x[2]) |
| 110 |
return values |
| 111 |
|
| 112 |
fname = write_function(mesh, el, f, dtype=f_dtype, backend=backend) |
| 113 |
MPI.COMM_WORLD.Barrier() |
| 114 |
read_function(read_comm, el, f, fname, dtype=f_dtype, backend=backend) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 118 |
@pytest.mark.parametrize("family", ["RTCF"]) |
| 119 |
@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 120 |
@pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD]) |
| 121 |
def test_read_write_2D_quad( |
| 122 |
read_comm, |
| 123 |
family, |
| 124 |
degree, |
| 125 |
is_complex, |
| 126 |
non_simplex_mesh_2D, |
| 127 |
get_dtype, |
| 128 |
write_function, |
| 129 |
read_function, |
| 130 |
backend, |
| 131 |
): |
| 132 |
mesh = non_simplex_mesh_2D |
| 133 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 134 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 135 |
|
| 136 |
def f(x): |
| 137 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 138 |
values[0] = np.full(x.shape[1], np.pi) |
| 139 |
values[1] = x[1] + 2 * x[0] |
| 140 |
if is_complex: |
| 141 |
values[0] += 2j * x[2] |
| 142 |
values[1] += 2j * np.cos(x[2]) |
| 143 |
return values |
| 144 |
|
| 145 |
hash = write_function(mesh, el, f, f_dtype, backend=backend) |
| 146 |
MPI.COMM_WORLD.Barrier() |
| 147 |
read_function(read_comm, el, f, hash, f_dtype, backend=backend) |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 151 |
@pytest.mark.parametrize("family", ["NCF"]) |
| 152 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 153 |
@pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD]) |
| 154 |
def test_read_write_hex( |
| 155 |
read_comm, |
| 156 |
family, |
| 157 |
degree, |
| 158 |
is_complex, |
| 159 |
non_simplex_mesh_3D, |
| 160 |
get_dtype, |
| 161 |
write_function, |
| 162 |
read_function, |
| 163 |
backend, |
| 164 |
): |
| 165 |
mesh = non_simplex_mesh_3D |
| 166 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 167 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 168 |
|
| 169 |
def f(x): |
| 170 |
values = np.empty((3, x.shape[1]), dtype=f_dtype) |
| 171 |
values[0] = np.full(x.shape[1], np.pi) + x[0] |
| 172 |
values[1] = np.cos(x[2]) |
| 173 |
values[2] = x[0] |
| 174 |
if is_complex: |
| 175 |
values[0] += 2j * x[2] |
| 176 |
values[2] -= 1j * x[1] |
| 177 |
return values |
| 178 |
|
| 179 |
hash = write_function(mesh, el, f, dtype=f_dtype, backend=backend) |
| 180 |
MPI.COMM_WORLD.Barrier() |
| 181 |
read_function(read_comm, el, f, hash, dtype=f_dtype, backend=backend) |
| 182 |
|
| 183 |
|
| 184 |
@pytest.mark.parametrize("is_complex", [True, False]) |
| 185 |
@pytest.mark.parametrize("family", ["RTCF"]) |
| 186 |
@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 187 |
@pytest.mark.parametrize("read_comm", [MPI.COMM_SELF, MPI.COMM_WORLD]) |
| 188 |
def test_read_write_multiple( |
| 189 |
read_comm, |
| 190 |
family, |
| 191 |
degree, |
| 192 |
is_complex, |
| 193 |
non_simplex_mesh_2D, |
| 194 |
get_dtype, |
| 195 |
write_function, |
| 196 |
read_function, |
| 197 |
backend, |
| 198 |
): |
| 199 |
mesh = non_simplex_mesh_2D |
| 200 |
f_dtype = get_dtype(mesh.geometry.x.dtype, is_complex) |
| 201 |
el = basix.ufl.element(family, mesh.basix_cell(), degree, dtype=mesh.geometry.x.dtype) |
| 202 |
|
| 203 |
def f(x): |
| 204 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 205 |
values[0] = np.full(x.shape[1], np.pi) |
| 206 |
values[1] = x[1] + 2 * x[0] |
| 207 |
if is_complex: |
| 208 |
values[0] -= 2j * x[2] |
| 209 |
values[1] += 2j * np.cos(x[2]) |
| 210 |
return values |
| 211 |
|
| 212 |
def g(x): |
| 213 |
values = np.empty((2, x.shape[1]), dtype=f_dtype) |
| 214 |
values[0] = 2 * x[1] |
| 215 |
values[1] = 3 * x[0] |
| 216 |
if is_complex: |
| 217 |
values[0] += 3j * x[0] |
| 218 |
values[1] += 2j * x[0] * x[1] |
| 219 |
return values |
| 220 |
|
| 221 |
hash_f = write_function(mesh, el, f, dtype=f_dtype, name="f", append=False, backend=backend) |
| 222 |
hash_g = write_function(mesh, el, g, dtype=f_dtype, name="g", append=True, backend=backend) |
| 223 |
assert hash_f == hash_g |
| 224 |
|
| 225 |
MPI.COMM_WORLD.Barrier() |
| 226 |
read_function(read_comm, el, f, hash_f, dtype=f_dtype, name="f", backend=backend) |
| 227 |
read_function(read_comm, el, g, hash_g, dtype=f_dtype, name="g", backend=backend) |