9#include "BoundingBoxTree.h"
16#include <dolfinx/common/log.h>
17#include <dolfinx/fem/CoordinateElement.h>
18#include <dolfinx/fem/ElementDofLayout.h>
19#include <dolfinx/graph/AdjacencyList.h>
20#include <dolfinx/mesh/Mesh.h>
34template <std::
floating_po
int T>
42 std::vector<std::int32_t>
57template <std::
floating_po
int T>
59 std::span<const std::int32_t> entities,
60 std::span<const T> points)
62 const int tdim =
mesh.topology()->dim();
65 std::span<const T> geom_dofs =
geometry.x();
66 auto x_dofmap =
geometry.dofmaps().front();
67 std::vector<T> shortest_vectors;
68 shortest_vectors.reserve(3 * entities.size());
71 for (std::size_t e = 0; e < entities.size(); e++)
75 assert(entities[e] >= 0);
76 auto dofs = md::submdspan(x_dofmap, entities[e], md::full_extent);
77 std::vector<T> nodes(3 * dofs.size());
78 for (std::size_t i = 0; i < dofs.size(); ++i)
80 const std::int32_t pos = 3 * dofs[i];
81 for (std::size_t j = 0; j < 3; ++j)
82 nodes[3 * i + j] = geom_dofs[pos + j];
87 shortest_vectors.insert(shortest_vectors.end(), d.begin(), d.end());
92 mesh.topology_mutable()->create_connectivity(dim, tdim);
93 mesh.topology_mutable()->create_connectivity(tdim, dim);
94 auto e_to_c =
mesh.topology()->connectivity(dim, tdim);
96 auto c_to_e =
mesh.topology_mutable()->connectivity(tdim, dim);
98 for (std::size_t e = 0; e < entities.size(); e++)
100 const std::int32_t index = entities[e];
103 assert(e_to_c->num_links(index) > 0);
104 const std::int32_t c = e_to_c->links(index)[0];
107 auto cell_entities = c_to_e->links(c);
108 auto it0 = std::find(cell_entities.begin(), cell_entities.end(), index);
109 assert(it0 != cell_entities.end());
110 const int local_cell_entity
111 = std::ranges::distance(cell_entities.begin(), it0);
114 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
115 const std::vector<int> entity_dofs
116 =
geometry.cmaps().front().create_dof_layout().entity_closure_dofs(
117 dim, local_cell_entity);
118 std::vector<T> nodes(3 * entity_dofs.size());
119 for (std::size_t i = 0; i < entity_dofs.size(); i++)
121 const std::int32_t pos = 3 * dofs[entity_dofs[i]];
122 for (std::size_t j = 0; j < 3; ++j)
123 nodes[3 * i + j] = geom_dofs[pos + j];
128 shortest_vectors.insert(shortest_vectors.end(), d.begin(), d.end());
132 return shortest_vectors;
141template <std::
floating_po
int T>
143 std::span<const T, 3> x)
145 auto b0 = b.template subspan<0, 3>();
146 auto b1 = b.template subspan<3, 3>();
147 return std::transform_reduce(x.begin(), x.end(), b0.begin(), 0.0,
152 return dx > 0 ? 0 : dx * dx;
154 + std::transform_reduce(x.begin(), x.end(), b1.begin(), 0.0,
159 return dx < 0 ? 0 : dx * dx;
178template <std::
floating_po
int T>
180 std::span<const std::int32_t> entities,
181 std::span<const T> points)
184 std::vector<T> d(v.size() / 3, 0);
185 for (std::size_t i = 0; i < d.size(); ++i)
186 for (std::size_t j = 0; j < 3; ++j)
187 d[i] += v[3 * i + j] * v[3 * i + j];
194constexpr bool is_leaf(std::array<std::int32_t, 2> bbox)
197 return bbox[0] == bbox[1];
204template <std::
floating_po
int T>
205constexpr bool point_in_bbox(std::span<const T, 6> b, std::span<const T, 3> x)
207 constexpr T rtol = 1e-14;
209 for (std::size_t i = 0; i < 3; i++)
211 T eps = rtol * (b[i + 3] - b[i]);
212 in &= (x[i] >= (b[i] - eps)) && (x[i] <= (b[i + 3] + eps));
223template <std::
floating_po
int T>
224constexpr bool bbox_in_bbox(std::span<const T, 6> a, std::span<const T, 6> b)
226 constexpr T rtol = 1e-14;
227 auto a0 = a.template subspan<0, 3>();
228 auto a1 = a.template subspan<3, 3>();
229 auto b0 = b.template subspan<0, 3>();
230 auto b1 = b.template subspan<3, 3>();
233 for (std::size_t i = 0; i < 3; i++)
235 T eps = rtol * (b1[i] - b0[i]);
236 in &= a1[i] >= (b0[i] - eps);
237 in &= a0[i] <= (b1[i] + eps);
245template <std::
floating_po
int T>
265template <std::
floating_po
int T>
267 std::int32_t entity, std::span<const T, 3> point,
270 const int tdim =
mesh.topology()->dim();
272 std::span<const T> geom_dofs =
geometry.x();
273 auto x_dofmap =
geometry.dofmaps().front();
279 auto dofs = md::submdspan(x_dofmap, entity, md::full_extent);
280 assert(3 * dofs.size() <= scratch.
nodes.size());
281 for (std::size_t i = 0; i < dofs.size(); ++i)
283 const std::int32_t pos = 3 * dofs[i];
284 for (std::size_t j = 0; j < 3; ++j)
285 scratch.
nodes[3 * i + j] = geom_dofs[pos + j];
289 point, std::span<const T>(scratch.
nodes.data(), 3 * dofs.size()));
293 mesh.topology_mutable()->create_connectivity(dim, tdim);
294 mesh.topology_mutable()->create_connectivity(tdim, dim);
295 auto e_to_c =
mesh.topology()->connectivity(dim, tdim);
297 auto c_to_e =
mesh.topology_mutable()->connectivity(tdim, dim);
301 assert(e_to_c->num_links(entity) > 0);
302 const std::int32_t c = e_to_c->links(entity)[0];
305 auto cell_entities = c_to_e->links(c);
306 auto it0 = std::find(cell_entities.begin(), cell_entities.end(), entity);
307 assert(it0 != cell_entities.end());
308 const int local_cell_entity
309 = std::ranges::distance(cell_entities.begin(), it0);
312 auto dofs = md::submdspan(x_dofmap, c, md::full_extent);
313 const std::vector<int>& entity_dofs
315 assert(3 * entity_dofs.size() <= scratch.
nodes.size());
316 for (std::size_t i = 0; i < entity_dofs.size(); i++)
318 const std::int32_t pos = 3 * dofs[entity_dofs[i]];
319 for (std::size_t j = 0; j < 3; ++j)
320 scratch.
nodes[3 * i + j] = geom_dofs[pos + j];
325 std::span<const T>(scratch.
nodes.data(), 3 * entity_dofs.size()));
328 return d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
332template <std::
floating_po
int T>
333std::pair<std::int32_t, T>
334_compute_closest_entity(
const geometry::BoundingBoxTree<T>& tree,
335 std::span<const T, 3> point, std::int32_t node,
336 const mesh::Mesh<T>& mesh, std::int32_t closest_entity,
341 const std::array<std::int32_t, 2> bbox = tree.bbox(node);
346 if (tree.tdim() == 0)
348 std::array<T, 6> diff = tree.get_bbox(node);
349 for (std::size_t k = 0; k < 3; ++k)
351 r2 = diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
361 r2 = squared_distance_entity(mesh, tree.tdim(), bbox.back(), point,
369 closest_entity = bbox.back();
373 return {closest_entity, R2};
380 return {closest_entity, R2};
384 std::pair<std::int32_t, T> p0 = _compute_closest_entity(
385 tree, point, bbox.front(), mesh, closest_entity, R2, scratch);
386 std::pair<std::int32_t, T> p1 = _compute_closest_entity(
387 tree, point, bbox.back(), mesh, p0.first, p0.second, scratch);
398template <std::
floating_po
int T>
399void _compute_collisions_point(
const geometry::BoundingBoxTree<T>& tree,
400 std::span<const T, 3> p,
401 std::vector<std::int32_t>& entities,
402 std::vector<std::int32_t>& stack)
404 assert(stack.empty());
405 std::int32_t next = tree.num_bboxes() - 1;
406 std::span<const T> coords = tree.bbox_coordinates();
407 auto view_bbox = [&coords](std::int32_t node)
408 {
return std::span<const T, 6>(coords.data() + 6 * node, 6); };
411 if (std::array bbox = tree.bbox(next);
412 is_leaf(bbox) and point_in_bbox(view_bbox(next), p))
416 entities.push_back(bbox[1]);
423 bool left = point_in_bbox(view_bbox(bbox[0]), p);
424 bool right = point_in_bbox(view_bbox(bbox[1]), p);
430 stack.push_back(bbox[1]);
449 if (next == -1 and !stack.empty())
458template <std::
floating_po
int T>
459void _compute_collisions_tree(
const geometry::BoundingBoxTree<T>& A,
460 const geometry::BoundingBoxTree<T>& B,
461 std::int32_t node_A, std::int32_t node_B,
462 std::vector<std::int32_t>& entities)
465 if (!bbox_in_bbox<T>(A.get_bbox(node_A), B.get_bbox(node_B)))
469 const std::array<std::int32_t, 2> bbox_A = A.bbox(node_A);
470 const std::array<std::int32_t, 2> bbox_B = B.bbox(node_B);
473 const bool is_leaf_A = is_leaf(bbox_A);
474 const bool is_leaf_B = is_leaf(bbox_B);
475 if (is_leaf_A and is_leaf_B)
479 entities.push_back(bbox_A[1]);
480 entities.push_back(bbox_B[1]);
485 _compute_collisions_tree(A, B, node_A, bbox_B[0], entities);
486 _compute_collisions_tree(A, B, node_A, bbox_B[1], entities);
491 _compute_collisions_tree(A, B, bbox_A[0], node_B, entities);
492 _compute_collisions_tree(A, B, bbox_A[1], node_B, entities);
494 else if (node_A > node_B)
500 _compute_collisions_tree(A, B, bbox_A[0], node_B, entities);
501 _compute_collisions_tree(A, B, bbox_A[1], node_B, entities);
505 _compute_collisions_tree(A, B, node_A, bbox_B[0], entities);
506 _compute_collisions_tree(A, B, node_A, bbox_B[1], entities);
521template <std::
floating_po
int T>
523 std::span<const std::int32_t> entities)
525 logger().info(
"Building point search tree to accelerate distance queries for "
526 "a given topological dimension and subset of entities.");
528 const std::vector<T> midpoints
530 std::vector<std::pair<std::array<T, 3>, std::int32_t>> points(
532 for (std::size_t i = 0; i < points.size(); ++i)
534 for (std::size_t j = 0; j < 3; ++j)
535 points[i].first[j] = midpoints[3 * i + j];
536 points[i].second = entities[i];
548template <std::
floating_po
int T>
553 std::vector<std::int32_t> entities;
556 impl::_compute_collisions_tree(tree0, tree1, tree0.
num_bboxes() - 1,
573template <std::
floating_po
int T>
579 std::vector<std::int32_t> entities, offsets(points.size() / 3 + 1, 0);
580 entities.reserve(points.size() / 3);
583 std::vector<std::int32_t> stack;
584 for (std::size_t p = 0; p < points.size() / 3; ++p)
586 impl::_compute_collisions_point(
587 tree, std::span<const T, 3>(points.data() + 3 * p, 3), entities,
589 offsets[p + 1] = entities.size();
597 std::vector<std::int32_t>(),
598 std::vector<std::int32_t>(points.size() / 3 + 1, 0));
627template <std::
floating_po
int T>
629 std::span<const std::int32_t> cells,
630 std::array<T, 3> point, T tol,
631 std::span<T> coordinate_dofs)
637 std::span<const T> geom_dofs =
geometry.x();
638 auto x_dofmap =
geometry.dofmaps().front();
639 const std::size_t num_nodes = x_dofmap.extent(1);
640 assert(coordinate_dofs.size() >= num_nodes * 3);
641 std::span<T> cell_dofs = coordinate_dofs.first(num_nodes * 3);
642 for (
auto cell : cells)
644 auto dofs = md::submdspan(x_dofmap, cell, md::full_extent);
645 for (std::size_t i = 0; i < num_nodes; ++i)
647 std::copy_n(std::next(geom_dofs.begin(), 3 * dofs[i]), 3,
648 std::next(cell_dofs.begin(), 3 * i));
654 [](
auto d,
auto e) {
return d + e * e; });
674template <std::
floating_po
int T>
675std::vector<std::int32_t>
681 return std::vector<std::int32_t>(points.size() / 3, -1);
683 std::vector<std::int32_t> entities;
684 entities.reserve(points.size() / 3);
687 =
mesh.geometry().cmaps().front().create_dof_layout();
692 std::size_t max_nodes = 0;
694 max_nodes = std::max(max_nodes,
static_cast<std::size_t
>(cmap.dim()));
695 impl::ClosestEntityScratch<T> scratch{std::vector<T>(3 * max_nodes),
697 for (std::size_t i = 0; i < points.size() / 3; ++i)
702 std::array<std::int32_t, 2> leaf0 = midpoint_tree.
bbox(0);
703 assert(impl::is_leaf(leaf0));
704 std::array<T, 6> diff = midpoint_tree.
get_bbox(0);
705 for (std::size_t k = 0; k < 3; ++k)
706 diff[k] -= points[3 * i + k];
707 T R2 = diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
713 const auto [m_index, m_distance2] = impl::_compute_closest_entity(
714 midpoint_tree, std::span<const T, 3>(points.data() + 3 * i, 3),
722 const auto [index, distance2] = impl::_compute_closest_entity(
723 tree, std::span<const T, 3>(points.data() + 3 * i, 3),
726 entities.push_back(index);
747template <std::
floating_po
int T>
751 std::span<const T> points)
753 std::vector<std::int32_t> offsets = {0};
754 offsets.reserve(candidate_cells.
num_nodes() + 1);
755 std::vector<std::int32_t> colliding_cells;
756 constexpr T eps2 = 1e-12;
757 const int tdim =
mesh.topology()->dim();
758 for (std::int32_t i = 0; i < candidate_cells.
num_nodes(); i++)
760 auto cells = candidate_cells.
links(i);
761 std::vector<T> _point(3 * cells.size());
762 for (std::size_t j = 0; j < cells.size(); ++j)
763 for (std::size_t k = 0; k < 3; ++k)
764 _point[3 * j + k] = points[3 * i + k];
767 for (std::size_t j = 0; j < cells.size(); j++)
768 if (distances_sq[j] < eps2)
769 colliding_cells.push_back(cells[j]);
771 offsets.push_back(colliding_cells.size());
818template <std::
floating_po
int T>
822 std::optional<std::span<const std::int32_t>> cells,
823 bool find_closest_cell =
true)
825 MPI_Comm comm =
mesh.comm();
827 const int tdim =
mesh.topology()->dim();
829 std::vector<std::int32_t> local_cells;
830 if (not(cells.has_value()))
832 auto cell_map =
mesh.topology()->index_map(tdim);
833 local_cells.resize(cell_map->size_local());
834 std::iota(local_cells.begin(), local_cells.end(), 0);
836 = std::span<const std::int32_t>(local_cells.data(), local_cells.size());
848 std::vector<std::int32_t> out_ranks = collisions.
array();
849 std::ranges::sort(out_ranks);
850 auto [unique_end, range_end] = std::ranges::unique(out_ranks);
851 out_ranks.erase(unique_end, range_end);
855 std::ranges::sort(in_ranks);
858 MPI_Comm forward_comm;
859 MPI_Dist_graph_create_adjacent(
860 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
861 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &forward_comm);
865 std::map<std::int32_t, std::int32_t> rank_to_neighbor;
866 for (std::size_t i = 0; i < out_ranks.size(); i++)
867 rank_to_neighbor[out_ranks[i]] = i;
870 std::vector<std::int32_t> send_sizes(out_ranks.size());
871 for (std::size_t i = 0; i < points.size() / 3; ++i)
872 for (std::int32_t p : collisions.
links(i))
873 send_sizes[rank_to_neighbor[p]] += 3;
876 std::vector<std::int32_t> recv_sizes(in_ranks.size());
877 send_sizes.reserve(1);
878 recv_sizes.reserve(1);
879 MPI_Request sizes_request;
880 MPI_Ineighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
881 MPI_INT, forward_comm, &sizes_request);
884 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
885 std::partial_sum(send_sizes.begin(), send_sizes.end(),
886 std::next(send_offsets.begin(), 1));
889 std::vector<T> send_data(send_offsets.back());
890 std::vector<std::int32_t> counter(send_sizes.size(), 0);
892 std::vector<std::int32_t> unpack_map(send_offsets.back() / 3);
893 for (std::size_t i = 0; i < points.size(); i += 3)
895 for (std::int32_t p : collisions.
links(i / 3))
897 std::int32_t neighbor = rank_to_neighbor[p];
898 std::int32_t pos = send_offsets[neighbor] + counter[neighbor];
899 auto it = std::next(send_data.begin(), pos);
900 std::copy_n(std::next(points.begin(), i), 3, it);
901 unpack_map[pos / 3] = i / 3;
902 counter[neighbor] += 3;
906 MPI_Wait(&sizes_request, MPI_STATUS_IGNORE);
907 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
908 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
909 std::next(recv_offsets.begin(), 1));
911 std::vector<T> received_points((std::size_t)recv_offsets.back());
912 MPI_Neighbor_alltoallv(
913 send_data.data(), send_sizes.data(), send_offsets.data(),
919 std::span<const T> geom_dofs =
geometry.x();
920 auto x_dofmap =
geometry.dofmaps().front();
925 received_points.size()));
930 std::vector<T> coordinate_dofs(3 * x_dofmap.extent(1));
934 std::vector<std::int32_t> cell_indicator(received_points.size() / 3);
935 std::vector<std::int32_t> closest_cells(received_points.size() / 3);
936 for (std::size_t p = 0; p < received_points.size(); p += 3)
938 std::array<T, 3> point;
939 std::copy_n(std::next(received_points.begin(), p), 3, point.begin());
942 mesh, candidate_collisions.
links(p / 3), point,
943 10 * std::numeric_limits<T>::epsilon(), std::span<T>(coordinate_dofs));
946 cell_indicator[p / 3] = (colliding_cell >= 0) ? rank : -1;
949 closest_cells[p / 3] = colliding_cell;
954 MPI_Comm reverse_comm;
955 MPI_Dist_graph_create_adjacent(
956 comm, out_ranks.size(), out_ranks.data(), MPI_UNWEIGHTED, in_ranks.size(),
957 in_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &reverse_comm);
962 auto rescale = [](
auto& x)
963 { std::ranges::transform(x, x.begin(), [](
auto e) { return (e / 3); }); };
965 rescale(recv_offsets);
967 rescale(send_offsets);
970 std::swap(recv_sizes, send_sizes);
971 std::swap(recv_offsets, send_offsets);
974 std::vector<std::int32_t> recv_ranks(recv_offsets.back());
975 MPI_Neighbor_alltoallv(cell_indicator.data(), send_sizes.data(),
976 send_offsets.data(), MPI_INT32_T, recv_ranks.data(),
977 recv_sizes.data(), recv_offsets.data(), MPI_INT32_T,
980 std::vector<int> point_owners(points.size() / 3, -1);
981 for (std::size_t i = 0; i < unpack_map.size(); i++)
983 const std::int32_t pos = unpack_map[i];
985 if (recv_ranks[i] >= 0 && point_owners[pos] == -1)
986 point_owners[pos] = recv_ranks[i];
989 if (find_closest_cell)
993 std::vector<std::uint8_t> send_needs_closest_cell(recv_offsets.back());
994 for (std::int32_t i = 0; i < recv_offsets.back(); i++)
996 const std::int32_t pos = unpack_map[i];
997 send_needs_closest_cell[i] = point_owners[pos] == -1;
1001 std::swap(send_sizes, recv_sizes);
1002 std::swap(send_offsets, recv_offsets);
1003 std::vector<std::uint8_t> dest_needs_closest_cell(recv_offsets.back());
1004 MPI_Neighbor_alltoallv(send_needs_closest_cell.data(), send_sizes.data(),
1005 send_offsets.data(), MPI_UINT8_T,
1006 dest_needs_closest_cell.data(), recv_sizes.data(),
1007 recv_offsets.data(), MPI_UINT8_T, forward_comm);
1009 std::vector<T> squared_distances(received_points.size() / 3, -1);
1011 for (std::size_t i = 0; i < dest_needs_closest_cell.size(); i++)
1013 if (dest_needs_closest_cell[i] == 1)
1015 assert(closest_cells[i] == -1);
1016 std::array<T, 3> point;
1017 std::copy_n(std::next(received_points.begin(), 3 * i), 3,
1021 T shortest_distance = std::numeric_limits<T>::max();
1022 std::int32_t closest_cell = -1;
1023 for (
auto cell : candidate_collisions.
links(i))
1025 auto dofs = md::submdspan(x_dofmap, cell, md::full_extent);
1026 for (std::size_t j = 0; j < dofs.size(); ++j)
1028 const int pos = 3 * dofs[j];
1029 for (std::size_t k = 0; k < 3; ++k)
1030 coordinate_dofs[3 * j + k] = geom_dofs[pos + k];
1033 std::span<const T>(point.data(), point.size()),
1034 std::span<const T>(coordinate_dofs.data(), 3 * dofs.size()));
1035 if (T current_distance = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
1036 current_distance < shortest_distance)
1038 shortest_distance = current_distance;
1039 closest_cell = cell;
1042 closest_cells[i] = closest_cell;
1043 squared_distances[i] = shortest_distance;
1047 std::swap(recv_sizes, send_sizes);
1048 std::swap(recv_offsets, send_offsets);
1052 std::vector<T> recv_distances(recv_offsets.back());
1053 MPI_Neighbor_alltoallv(
1054 squared_distances.data(), send_sizes.data(), send_offsets.data(),
1059 std::vector<T> closest_distance(point_owners.size(),
1060 std::numeric_limits<T>::max());
1061 for (std::size_t i = 0; i < out_ranks.size(); i++)
1063 for (std::int32_t j = recv_offsets[i]; j < recv_offsets[i + 1]; j++)
1065 const std::int32_t pos = unpack_map[j];
1066 T current_dist = recv_distances[j];
1070 if (T d = closest_distance[pos];
1071 (current_dist >= 0) and (current_dist < d))
1073 point_owners[pos] = out_ranks[i];
1074 closest_distance[pos] = current_dist;
1081 std::swap(send_sizes, recv_sizes);
1082 std::swap(send_offsets, recv_offsets);
1085 std::vector<std::int32_t> send_owners(send_offsets.back());
1086 std::ranges::fill(counter, 0);
1087 for (std::size_t i = 0; i < points.size() / 3; ++i)
1089 for (std::int32_t p : collisions.
links(i))
1091 std::int32_t neighbor = rank_to_neighbor[p];
1092 send_owners[send_offsets[neighbor] + counter[neighbor]++]
1098 std::vector<std::int32_t> dest_ranks(recv_offsets.back());
1099 MPI_Neighbor_alltoallv(send_owners.data(), send_sizes.data(),
1100 send_offsets.data(), MPI_INT32_T, dest_ranks.data(),
1101 recv_sizes.data(), recv_offsets.data(), MPI_INT32_T,
1105 std::vector<int> owned_recv_ranks;
1106 owned_recv_ranks.reserve(recv_offsets.back());
1107 std::vector<T> owned_recv_points;
1108 std::vector<std::int32_t> owned_recv_cells;
1109 for (std::size_t i = 0; i < in_ranks.size(); i++)
1111 for (std::int32_t j = recv_offsets[i]; j < recv_offsets[i + 1]; j++)
1113 if (rank == dest_ranks[j])
1115 owned_recv_ranks.push_back(in_ranks[i]);
1116 owned_recv_points.insert(
1117 owned_recv_points.end(), std::next(received_points.cbegin(), 3 * j),
1118 std::next(received_points.cbegin(), 3 * (j + 1)));
1119 owned_recv_cells.push_back(closest_cells[j]);
1124 MPI_Comm_free(&forward_comm);
1125 MPI_Comm_free(&reverse_comm);
1127 .dest_owners = std::move(owned_recv_ranks),
1128 .dest_points = std::move(owned_recv_points),
1129 .dest_cells = std::move(owned_recv_cells)};
Definition CoordinateElement.h:39
Definition ElementDofLayout.h:31
const std::vector< int > & entity_closure_dofs(int dim, int entity_index) const
Definition ElementDofLayout.cpp:65
Definition BoundingBoxTree.h:217
BoundingBoxTree create_global_tree(MPI_Comm comm) const
Definition BoundingBoxTree.h:354
std::int32_t num_bboxes() const
Return number of bounding boxes.
Definition BoundingBoxTree.h:390
std::array< T, 6 > get_bbox(std::size_t node) const
Return bounding box coordinates for a given node in the tree,.
Definition BoundingBoxTree.h:342
std::array< std::int32_t, 2 > bbox(std::size_t node) const
Definition BoundingBoxTree.h:424
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:188
std::span< LinkData > links(std::size_t node)
Get the links (edges) for given node.
Definition AdjacencyList.h:169
std::int32_t num_nodes() const
Get the number of nodes.
Definition AdjacencyList.h:154
Geometry stores the geometry imposed on a mesh.
Definition Geometry.h:39
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:25
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:326
std::vector< int > compute_graph_edges_nbx(MPI_Comm comm, std::span< const int > edges, int tag=static_cast< int >(tag::consensus_nbx))
Determine incoming graph edges using the NBX consensus algorithm.
Definition MPI.cpp:294
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:73
Geometry data structures and algorithms.
Definition BoundingBoxTree.h:26
std::vector< T > squared_distance(const mesh::Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities, std::span< const T > points)
Compute the squared distance between a point and a mesh entity.
Definition utils.h:179
std::vector< std::int32_t > compute_collisions(const BoundingBoxTree< T > &tree0, const BoundingBoxTree< T > &tree1)
Compute all collisions between two bounding box trees.
Definition utils.h:549
T compute_squared_distance_bbox(std::span< const T, 6 > b, std::span< const T, 3 > x)
Compute squared distance between point and bounding box.
Definition utils.h:142
graph::AdjacencyList< std::int32_t > compute_colliding_cells(const mesh::Mesh< T > &mesh, const graph::AdjacencyList< std::int32_t > &candidate_cells, std::span< const T > points)
Compute which cells collide with a point.
Definition utils.h:748
std::vector< T > shortest_vector(const mesh::Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities, std::span< const T > points)
Compute the shortest vector from a mesh entity to a point.
Definition utils.h:58
BoundingBoxTree< T > create_midpoint_tree(const mesh::Mesh< T > &mesh, int tdim, std::span< const std::int32_t > entities)
Create a bounding box tree for the midpoints of a subset of entities.
Definition utils.h:522
PointOwnershipData< T > determine_point_ownership(const mesh::Mesh< T > &mesh, std::span< const T > points, T padding, std::optional< std::span< const std::int32_t > > cells, bool find_closest_cell=true)
Determine, for a set of points, the owning process of the cell (if any) that contains each point.
Definition utils.h:820
std::vector< std::int32_t > compute_closest_entity(const BoundingBoxTree< T > &tree, const BoundingBoxTree< T > &midpoint_tree, const mesh::Mesh< T > &mesh, std::span< const T > points)
Compute closest mesh entity to a point.
Definition utils.h:676
std::array< T, 3 > compute_distance_gjk(std::span< const T > p0, std::span< const T > q0)
Compute the distance between two convex bodies p0 and q0, each defined by a set of points.
Definition gjk.h:372
std::int32_t compute_first_colliding_cell(const mesh::Mesh< T > &mesh, std::span< const std::int32_t > cells, std::array< T, 3 > point, T tol, std::span< T > coordinate_dofs)
Given a set of cells, find the first one that collides with a point.
Definition utils.h:628
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
std::vector< T > compute_midpoints(const Mesh< T > &mesh, int dim, std::span< const std::int32_t > entities)
Compute the midpoints for mesh entities of a given dimension.
Definition utils.h:475
auto logger() -> spdlog::logger &
Dolfinx's logger.
Definition log.cpp:24
Information on the ownership of points distributed across processes.
Definition utils.h:36
std::vector< T > dest_points
Points that are owned by current process.
Definition utils.h:41
std::vector< std::int32_t > dest_cells
Definition utils.h:43
std::vector< int > dest_owners
Ranks that sent dest_points to current process.
Definition utils.h:40
std::vector< int > src_owner
Definition utils.h:37
std::vector< T > nodes
Definition utils.h:248
const fem::ElementDofLayout & cmap_dof_layout
Coordinate-element dof layout.
Definition utils.h:253