Skip to content

Commit d502e54

Browse files
committed
Improve python exposure determine_point_ownership
* Python wrapper around nanobind exposed function * Extra optional arguments `cells`
1 parent 06e90b8 commit d502e54

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

cpp/dolfinx/fem/interpolate.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,12 @@ geometry::PointOwnershipData<T> create_interpolation_data(
10701070
x[3 * i + j] = coords[i + j * num_points];
10711071

10721072
// Determine ownership of each point
1073-
return geometry::determine_point_ownership<T>(mesh1, x, padding);
1073+
const int tdim = mesh1.topology()->dim();
1074+
auto cell_map1 = mesh1.topology()->index_map(tdim);
1075+
const std::int32_t num_cells1 = cell_map1->size_local();
1076+
std::vector<std::int32_t> cells1(num_cells1, 0);
1077+
std::iota(cells1.begin(), cells1.end(), 0);
1078+
return geometry::determine_point_ownership<T>(mesh1, x, cells1, padding);
10741079
}
10751080

10761081
/// @brief Interpolate a finite element Function defined on a mesh to a

cpp/dolfinx/geometry/utils.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ graph::AdjacencyList<std::int32_t> compute_colliding_cells(
663663
/// @param[in] mesh The mesh
664664
/// @param[in] points Points to check for collision (`shape=(num_points,
665665
/// 3)`). Storage is row-major.
666+
/// @param[in] cells Cells to check for ownership
666667
/// @param[in] padding Amount of absolute padding of bounding boxes of the mesh.
667668
/// Each bounding box of the mesh is padded with this amount, to increase
668669
/// the number of candidates, avoiding rounding errors in determining the owner
@@ -685,18 +686,14 @@ graph::AdjacencyList<std::int32_t> compute_colliding_cells(
685686
template <std::floating_point T>
686687
PointOwnershipData<T> determine_point_ownership(const mesh::Mesh<T>& mesh,
687688
std::span<const T> points,
689+
std::span<const std::int32_t> cells,
688690
T padding)
689691
{
690692
MPI_Comm comm = mesh.comm();
691693

692694
// Create a global bounding-box tree to find candidate processes with
693695
// cells that could collide with the points
694696
const int tdim = mesh.topology()->dim();
695-
auto cell_map = mesh.topology()->index_map(tdim);
696-
const std::int32_t num_cells = cell_map->size_local();
697-
// NOTE: Should we send the cells in as input?
698-
std::vector<std::int32_t> cells(num_cells, 0);
699-
std::iota(cells.begin(), cells.end(), 0);
700697
BoundingBoxTree bb(mesh, tdim, cells, padding);
701698
BoundingBoxTree global_bbtree = bb.create_global_tree(comm);
702699

python/dolfinx/geometry.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,32 @@ def compute_distance_gjk(
270270
271271
"""
272272
return _cpp.geometry.compute_distance_gjk(p, q)
273+
274+
def determine_point_ownership(
275+
mesh: Mesh,
276+
points: npt.NDArray[np.floating],
277+
cells: typing.Optional[np.ndarray] = None,
278+
padding: float = 0.0
279+
) -> PointOwnershipData:
280+
"""Build PointOwnershipData for a mesh-points pair.
281+
First, potential collisions are found
282+
by building BoundingBoxTrees for the points
283+
and the mesh cells and finding which leaves collide.
284+
Then, actual cell-point containment pairs are determined
285+
using the GJK algorithm.
286+
287+
Args:
288+
mesh: The mesh
289+
points: The points
290+
cells: Cells in mesh who potentially contain the points.
291+
If ``None`` then all cells are considered.
292+
padding: Padding added to the bounding boxes.
293+
294+
Returns:
295+
PointOwnershipData
296+
297+
"""
298+
if cells is None:
299+
map = mesh.topology.index_map(mesh.topology.dim)
300+
cells = np.arange(map.size_local, dtype=np.int32)
301+
return PointOwnershipData(_cpp.geometry.determine_point_ownership(mesh._cpp_object, points, cells, padding))

python/dolfinx/wrappers/geometry.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,18 @@ void declare_bbtree(nb::module_& m, std::string type)
180180
nb::arg("mesh"), nb::arg("dim"), nb::arg("indices"), nb::arg("points"));
181181
m.def("determine_point_ownership",
182182
[](const dolfinx::mesh::Mesh<T>& mesh,
183-
nb::ndarray<const T, nb::c_contig> points, const T padding)
183+
nb::ndarray<const T, nb::c_contig> points,
184+
nb::ndarray<const std::int32_t, nb::ndim<1>, nb::c_contig> cells,
185+
const T padding)
184186
{
185187
const std::size_t p_s0 = points.ndim() == 1 ? 1 : points.shape(0);
186188
std::span<const T> _p(points.data(), 3 * p_s0);
187189
return dolfinx::geometry::determine_point_ownership<T>(mesh, _p,
190+
std::span(cells.data(), cells.size()),
188191
padding);
189-
});
192+
},
193+
nb::arg("mesh"), nb::arg("points"), nb::arg("cells"), nb::arg("padding"),
194+
"Compute PointOwnershipData for mesh-points pair.");
190195

191196
std::string pod_pyclass_name = "PointOwnershipData_" + type;
192197
nb::class_<dolfinx::geometry::PointOwnershipData<T>>(m,

0 commit comments

Comments
 (0)