|
| 1 | +import dolfinx.mesh |
| 2 | +import numpy as np |
| 3 | +import pyvista |
| 4 | +import pytest |
| 5 | +from mpi4py import MPI |
| 6 | +from scipy.spatial import cKDTree |
| 7 | +from dolfinx.mesh import exterior_facet_indices |
| 8 | + |
| 9 | +from foam2dolfinx.helpers import tag_boundary_patch |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def unit_cube_topology(): |
| 14 | + mesh = dolfinx.mesh.create_box( |
| 15 | + MPI.COMM_WORLD, [[0, 0, 0], [1, 1, 1]], [2, 2, 2], dolfinx.mesh.CellType.hexahedron |
| 16 | + ) |
| 17 | + fdim = mesh.topology.dim - 1 |
| 18 | + mesh.topology.create_connectivity(fdim, 0) |
| 19 | + mesh.topology.create_connectivity(0, fdim) |
| 20 | + mesh.topology.create_connectivity(fdim, mesh.topology.dim) |
| 21 | + facet_indices = exterior_facet_indices(mesh.topology) |
| 22 | + c_to_v = mesh.topology.connectivity(fdim, 0) |
| 23 | + facet_vertices = np.vstack([c_to_v.links(f) for f in facet_indices]) |
| 24 | + tree = cKDTree(mesh.geometry.x) |
| 25 | + return mesh, tree, facet_indices, facet_vertices |
| 26 | + |
| 27 | + |
| 28 | +def test_tag_boundary_patch_returns_empty_when_no_points_match(unit_cube_topology): |
| 29 | + mesh, tree, facet_indices, facet_vertices = unit_cube_topology |
| 30 | + patch = pyvista.PolyData(np.array([[100.0, 100.0, 100.0]])) |
| 31 | + matched, tags = tag_boundary_patch( |
| 32 | + mesh, patch, 1, tree=tree, facet_indices=facet_indices, facet_vertices=facet_vertices |
| 33 | + ) |
| 34 | + assert len(matched) == 0 |
| 35 | + assert len(tags) == 0 |
| 36 | + |
| 37 | + |
| 38 | +def test_tag_boundary_patch_tags_equal_patch_id(unit_cube_topology): |
| 39 | + mesh, tree, facet_indices, facet_vertices = unit_cube_topology |
| 40 | + x0_points = mesh.geometry.x[mesh.geometry.x[:, 0] < 1e-10] |
| 41 | + patch = pyvista.PolyData(x0_points) |
| 42 | + _, tags = tag_boundary_patch( |
| 43 | + mesh, patch, 42, tree=tree, facet_indices=facet_indices, facet_vertices=facet_vertices |
| 44 | + ) |
| 45 | + assert np.all(tags == 42) |
| 46 | + |
| 47 | + |
| 48 | +def test_tag_boundary_patch_matched_facets_are_subset_of_exterior(unit_cube_topology): |
| 49 | + mesh, tree, facet_indices, facet_vertices = unit_cube_topology |
| 50 | + x0_points = mesh.geometry.x[mesh.geometry.x[:, 0] < 1e-10] |
| 51 | + patch = pyvista.PolyData(x0_points) |
| 52 | + matched, _ = tag_boundary_patch( |
| 53 | + mesh, patch, 1, tree=tree, facet_indices=facet_indices, facet_vertices=facet_vertices |
| 54 | + ) |
| 55 | + assert np.all(np.isin(matched, facet_indices)) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("patch_points", [ |
| 59 | + np.array([[100.0, 100.0, 100.0]]), # no match |
| 60 | + None, # match (x=0 face, filled below) |
| 61 | +]) |
| 62 | +def test_tag_boundary_patch_output_arrays_are_int32(unit_cube_topology, patch_points): |
| 63 | + mesh, tree, facet_indices, facet_vertices = unit_cube_topology |
| 64 | + if patch_points is None: |
| 65 | + patch_points = mesh.geometry.x[mesh.geometry.x[:, 0] < 1e-10] |
| 66 | + patch = pyvista.PolyData(patch_points) |
| 67 | + matched, tags = tag_boundary_patch( |
| 68 | + mesh, patch, 1, tree=tree, facet_indices=facet_indices, facet_vertices=facet_vertices |
| 69 | + ) |
| 70 | + assert matched.dtype == np.int32 |
| 71 | + assert tags.dtype == np.int32 |
0 commit comments