| 1 |
import itertools |
| 2 |
from typing import Tuple |
| 3 |
|
| 4 |
from mpi4py import MPI |
| 5 |
|
| 6 |
import basix.ufl |
| 7 |
import dolfinx |
| 8 |
import numpy as np |
| 9 |
import numpy.typing as npt |
| 10 |
import pytest |
| 11 |
|
| 12 |
from io4dolfinx.utils import compute_dofmap_pos, unroll_dofmap |
| 13 |
|
| 14 |
write_comm = [MPI.COMM_SELF, MPI.COMM_WORLD] # Communicators for creating mesh |
| 15 |
ghost_mode = [dolfinx.mesh.GhostMode.none, dolfinx.mesh.GhostMode.shared_facet] |
| 16 |
two_dimensional_cell_types = [ |
| 17 |
dolfinx.mesh.CellType.triangle, |
| 18 |
dolfinx.mesh.CellType.quadrilateral, |
| 19 |
] |
| 20 |
three_dimensional_cell_types = [dolfinx.mesh.CellType.hexahedron] |
| 21 |
|
| 22 |
two_dim_combinations = itertools.product(two_dimensional_cell_types, write_comm, ghost_mode) |
| 23 |
three_dim_combinations = itertools.product(three_dimensional_cell_types, write_comm, ghost_mode) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(params=two_dim_combinations, scope="module") |
| 27 |
def mesh_2D(request): |
| 28 |
cell_type, write_comm, ghost_mode = request.param |
| 29 |
mesh = dolfinx.mesh.create_unit_square( |
| 30 |
write_comm, 10, 10, cell_type=cell_type, ghost_mode=ghost_mode |
| 31 |
) |
| 32 |
return mesh |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture(params=three_dim_combinations, scope="module") |
| 36 |
def mesh_3D(request): |
| 37 |
cell_type, write_comm, ghost_mode = request.param |
| 38 |
M = 5 |
| 39 |
mesh = dolfinx.mesh.create_unit_cube( |
| 40 |
write_comm, M, M, M, cell_type=cell_type, ghost_mode=ghost_mode |
| 41 |
) |
| 42 |
return mesh |
| 43 |
|
| 44 |
|
| 45 |
def compute_positions( |
| 46 |
dofs: npt.NDArray[np.int32], |
| 47 |
dofmap_bs: int, |
| 48 |
num_owned_dofs: int, |
| 49 |
num_owned_cells: int, |
| 50 |
) -> Tuple[npt.NDArray[np.int32], npt.NDArray[np.int32]]: |
| 51 |
""" |
| 52 |
Support function for test. |
| 53 |
Given a dofmap, compute the local cell and position in the dofmap for each owned dof. |
| 54 |
The last cell (wrt) local index will be the one in the output map |
| 55 |
""" |
| 56 |
dof_to_cell_map = np.zeros(num_owned_dofs, dtype=np.int32) |
| 57 |
dof_to_pos_map = np.zeros(num_owned_dofs, dtype=np.int32) |
| 58 |
for c in range(num_owned_cells): |
| 59 |
for i, dof in enumerate(dofs[c]): |
| 60 |
for b in range(dofmap_bs): |
| 61 |
local_dof = dof * dofmap_bs + b |
| 62 |
if local_dof < num_owned_dofs: |
| 63 |
dof_to_cell_map[local_dof] = c |
| 64 |
dof_to_pos_map[local_dof] = i * dofmap_bs + b |
| 65 |
return dof_to_cell_map, dof_to_pos_map |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize("family", ["Lagrange", "DG"]) |
| 69 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 70 |
def test_unroll_P(family, degree, mesh_2D): |
| 71 |
V = dolfinx.fem.functionspace(mesh_2D, (family, degree)) |
| 72 |
dofmap = V.dofmap |
| 73 |
|
| 74 |
unrolled_map = unroll_dofmap(dofmap.list, dofmap.bs) |
| 75 |
|
| 76 |
normal_unroll = np.zeros( |
| 77 |
(dofmap.list.shape[0], dofmap.list.shape[1] * dofmap.bs), dtype=np.int32 |
| 78 |
) |
| 79 |
for i, dofs in enumerate(dofmap.list): |
| 80 |
for j, dof in enumerate(dofs): |
| 81 |
for k in range(dofmap.bs): |
| 82 |
normal_unroll[i, j * dofmap.bs + k] = dof * dofmap.bs + k |
| 83 |
|
| 84 |
np.testing.assert_allclose(unrolled_map, normal_unroll) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.parametrize("family", ["RTCF"]) |
| 88 |
@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 89 |
def test_unroll_RTCF(family, degree, mesh_3D): |
| 90 |
el = basix.ufl.element(family, mesh_3D.basix_cell(), degree) |
| 91 |
|
| 92 |
V = dolfinx.fem.functionspace(mesh_3D, el) |
| 93 |
dofmap = V.dofmap |
| 94 |
|
| 95 |
unrolled_map = unroll_dofmap(dofmap.list, dofmap.bs) |
| 96 |
|
| 97 |
normal_unroll = np.zeros( |
| 98 |
(dofmap.list.shape[0], dofmap.list.shape[1] * dofmap.bs), dtype=np.int32 |
| 99 |
) |
| 100 |
for i, dofs in enumerate(dofmap.list): |
| 101 |
for j, dof in enumerate(dofs): |
| 102 |
for k in range(dofmap.bs): |
| 103 |
normal_unroll[i, j * dofmap.bs + k] = dof * dofmap.bs + k |
| 104 |
|
| 105 |
np.testing.assert_allclose(unrolled_map, normal_unroll) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize("family", ["RTCF"]) |
| 109 |
@pytest.mark.parametrize("degree", [1, 2, 3]) |
| 110 |
def test_compute_dofmap_pos_RTCF(family, degree, mesh_3D): |
| 111 |
el = basix.ufl.element(family, mesh_3D.basix_cell(), degree) |
| 112 |
|
| 113 |
V = dolfinx.fem.functionspace(mesh_3D, el) |
| 114 |
local_cells, local_pos = compute_dofmap_pos(V) |
| 115 |
|
| 116 |
num_cells_local = mesh_3D.topology.index_map(mesh_3D.topology.dim).size_local |
| 117 |
num_dofs_local = V.dofmap.index_map.size_local * V.dofmap.index_map_bs |
| 118 |
reference_cells, reference_pos = compute_positions( |
| 119 |
V.dofmap.list, V.dofmap.bs, num_dofs_local, num_cells_local |
| 120 |
) |
| 121 |
np.testing.assert_allclose(reference_cells, local_cells) |
| 122 |
np.testing.assert_allclose(reference_pos, local_pos) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("family", ["Lagrange", "DG"]) |
| 126 |
@pytest.mark.parametrize("degree", [1, 4]) |
| 127 |
def test_compute_dofmap_pos_P(family, degree, mesh_2D): |
| 128 |
el = basix.ufl.element(family, mesh_2D.basix_cell(), degree) |
| 129 |
|
| 130 |
V = dolfinx.fem.functionspace(mesh_2D, el) |
| 131 |
local_cells, local_pos = compute_dofmap_pos(V) |
| 132 |
|
| 133 |
num_cells_local = mesh_2D.topology.index_map(mesh_2D.topology.dim).size_local |
| 134 |
num_dofs_local = V.dofmap.index_map.size_local * V.dofmap.index_map_bs |
| 135 |
reference_cells, reference_pos = compute_positions( |
| 136 |
V.dofmap.list, V.dofmap.bs, num_dofs_local, num_cells_local |
| 137 |
) |
| 138 |
np.testing.assert_allclose(reference_cells, local_cells) |
| 139 |
np.testing.assert_allclose(reference_pos, local_pos) |
| 140 |
|
| 141 |
|
| 142 |
def test_compute_send_sizes(): |
| 143 |
np.random.seed(42) |
| 144 |
N = 0 |
| 145 |
M = 10 |
| 146 |
num_data = 100 |
| 147 |
|
| 148 |
# Set of ranks to recieve data |
| 149 |
dest_ranks = np.arange(N, M, dtype=np.int32) |
| 150 |
|
| 151 |
# Random data owners |
| 152 |
data_owners = np.random.randint(N, M, num_data).astype(np.int32) |
| 153 |
|
| 154 |
# Compute the number of data to send to each rank with loops |
| 155 |
out_size = np.zeros(len(dest_ranks), dtype=np.int32) |
| 156 |
for owner in data_owners: |
| 157 |
for j, rank in enumerate(dest_ranks): |
| 158 |
if owner == rank: |
| 159 |
out_size[j] += 1 |
| 160 |
break |
| 161 |
|
| 162 |
process_pos_indicator = data_owners.reshape(-1, 1) == dest_ranks |
| 163 |
vectorized_out_size = np.count_nonzero(process_pos_indicator, axis=0) |
| 164 |
np.testing.assert_allclose(vectorized_out_size, out_size) |