|
| 1 | +import numpy as np |
| 2 | +from scipy.spatial import cKDTree |
| 3 | + |
| 4 | + |
| 5 | +def tag_boundary_patch( |
| 6 | + dolfinx_mesh, |
| 7 | + patch_dataset, |
| 8 | + patch_id, |
| 9 | + tol=1e-6, |
| 10 | + *, |
| 11 | + tree: cKDTree, |
| 12 | + facet_indices: np.ndarray, |
| 13 | + facet_vertices: np.ndarray, |
| 14 | +): |
| 15 | + """Tags the facets of a dolfinx mesh that belong to a given OpenFOAM boundary patch. |
| 16 | +
|
| 17 | + Args: |
| 18 | + dolfinx_mesh: the dolfinx mesh |
| 19 | + patch_dataset: the pyvista dataset for the boundary patch |
| 20 | + patch_id: integer tag to assign to matched facets |
| 21 | + tol: spatial tolerance for matching patch points to mesh vertices |
| 22 | + tree: pre-built cKDTree on mesh geometry points |
| 23 | + facet_indices: pre-computed exterior facet indices |
| 24 | + facet_vertices: pre-computed 2D array (n_facets, n_verts_per_facet) |
| 25 | + of vertex indices for each exterior facet |
| 26 | +
|
| 27 | + Returns: |
| 28 | + tuple of (matched_facet_indices, tags) as int32 arrays |
| 29 | + """ |
| 30 | + matched = tree.query_ball_point(patch_dataset.points, tol) |
| 31 | + matched_idx = np.unique( |
| 32 | + np.fromiter((i for sub in matched for i in sub), dtype=np.intp) |
| 33 | + ) |
| 34 | + |
| 35 | + vertex_matched = np.zeros(len(dolfinx_mesh.geometry.x), dtype=bool) |
| 36 | + if len(matched_idx): |
| 37 | + vertex_matched[matched_idx] = True |
| 38 | + |
| 39 | + facet_mask = vertex_matched[facet_vertices].all(axis=1) |
| 40 | + matched_facets = facet_indices[facet_mask] |
| 41 | + |
| 42 | + return matched_facets, np.full(len(matched_facets), patch_id, dtype=np.int32) |
0 commit comments