37 std::span<const std::int32_t> entities,
38 std::span<const T> points)
40 const int tdim = mesh.topology()->dim();
43 std::span<const T> geom_dofs = geometry.
x();
44 auto x_dofmap = geometry.
dofmap();
45 std::vector<T> shortest_vectors;
46 shortest_vectors.reserve(3 * entities.size());
49 for (std::size_t e = 0; e < entities.size(); e++)
53 assert(entities[e] >= 0);
54 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
55 x_dofmap, entities[e], MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
56 std::vector<T> nodes(3 * dofs.size());
57 for (std::size_t i = 0; i < dofs.size(); ++i)
59 const std::int32_t pos = 3 * dofs[i];
60 for (std::size_t j = 0; j < 3; ++j)
61 nodes[3 * i + j] = geom_dofs[pos + j];
65 = compute_distance_gjk<T>(points.subspan(3 * e, 3), nodes);
66 shortest_vectors.insert(shortest_vectors.end(), d.begin(), d.end());
71 mesh.topology_mutable()->create_connectivity(dim, tdim);
72 mesh.topology_mutable()->create_connectivity(tdim, dim);
73 auto e_to_c = mesh.topology()->connectivity(dim, tdim);
75 auto c_to_e = mesh.topology_mutable()->connectivity(tdim, dim);
77 for (std::size_t e = 0; e < entities.size(); e++)
79 const std::int32_t index = entities[e];
82 assert(e_to_c->num_links(index) > 0);
83 const std::int32_t c = e_to_c->links(index)[0];
86 auto cell_entities = c_to_e->links(c);
87 auto it0 = std::find(cell_entities.begin(), cell_entities.end(), index);
88 assert(it0 != cell_entities.end());
89 const int local_cell_entity = std::distance(cell_entities.begin(), it0);
92 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
93 x_dofmap, c, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
94 const std::vector<int> entity_dofs
95 = geometry.
cmap().create_dof_layout().entity_closure_dofs(
96 dim, local_cell_entity);
97 std::vector<T> nodes(3 * entity_dofs.size());
98 for (std::size_t i = 0; i < entity_dofs.size(); i++)
100 const std::int32_t pos = 3 * dofs[entity_dofs[i]];
101 for (std::size_t j = 0; j < 3; ++j)
102 nodes[3 * i + j] = geom_dofs[pos + j];
106 = compute_distance_gjk<T>(points.subspan(3 * e, 3), nodes);
107 shortest_vectors.insert(shortest_vectors.end(), d.begin(), d.end());
111 return shortest_vectors;
676 MPI_Comm comm = mesh.comm();
680 const int tdim = mesh.topology()->dim();
681 auto cell_map = mesh.topology()->index_map(tdim);
682 const std::int32_t num_cells = cell_map->size_local();
684 std::vector<std::int32_t> cells(num_cells, 0);
685 std::iota(cells.begin(), cells.end(), 0);
694 std::vector<std::int32_t> out_ranks = collisions.
array();
695 std::sort(out_ranks.begin(), out_ranks.end());
696 out_ranks.erase(std::unique(out_ranks.begin(), out_ranks.end()),
700 std::sort(in_ranks.begin(), in_ranks.end());
703 MPI_Comm forward_comm;
704 MPI_Dist_graph_create_adjacent(
705 comm, in_ranks.size(), in_ranks.data(), MPI_UNWEIGHTED, out_ranks.size(),
706 out_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &forward_comm);
710 std::map<std::int32_t, std::int32_t> rank_to_neighbor;
711 for (std::size_t i = 0; i < out_ranks.size(); i++)
712 rank_to_neighbor[out_ranks[i]] = i;
715 std::vector<std::int32_t> send_sizes(out_ranks.size());
716 for (std::size_t i = 0; i < points.size() / 3; ++i)
717 for (
auto p : collisions.
links(i))
718 send_sizes[rank_to_neighbor[p]] += 3;
721 std::vector<std::int32_t> recv_sizes(in_ranks.size());
722 send_sizes.reserve(1);
723 recv_sizes.reserve(1);
724 MPI_Request sizes_request;
725 MPI_Ineighbor_alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1,
726 MPI_INT, forward_comm, &sizes_request);
729 std::vector<std::int32_t> send_offsets(send_sizes.size() + 1, 0);
730 std::partial_sum(send_sizes.begin(), send_sizes.end(),
731 std::next(send_offsets.begin(), 1));
734 std::vector<T> send_data(send_offsets.back());
735 std::vector<std::int32_t> counter(send_sizes.size(), 0);
737 std::vector<std::int32_t> unpack_map(send_offsets.back() / 3);
738 for (std::size_t i = 0; i < points.size(); i += 3)
740 for (
auto p : collisions.
links(i / 3))
742 int neighbor = rank_to_neighbor[p];
743 int pos = send_offsets[neighbor] + counter[neighbor];
744 auto it = std::next(send_data.begin(), pos);
745 std::copy_n(std::next(points.begin(), i), 3, it);
746 unpack_map[pos / 3] = i / 3;
747 counter[neighbor] += 3;
751 MPI_Wait(&sizes_request, MPI_STATUS_IGNORE);
752 std::vector<std::int32_t> recv_offsets(in_ranks.size() + 1, 0);
753 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
754 std::next(recv_offsets.begin(), 1));
756 std::vector<T> received_points((std::size_t)recv_offsets.back());
757 MPI_Neighbor_alltoallv(
758 send_data.data(), send_sizes.data(), send_offsets.data(),
759 dolfinx::MPI::mpi_type<T>(), received_points.data(), recv_sizes.data(),
760 recv_offsets.data(), dolfinx::MPI::mpi_type<T>(), forward_comm);
764 std::span<const T> geom_dofs = geometry.
x();
765 auto x_dofmap = geometry.
dofmap();
770 received_points.size()));
774 std::vector<std::int32_t> cell_indicator(received_points.size() / 3);
775 std::vector<std::int32_t> closest_cells(received_points.size() / 3);
776 for (std::size_t p = 0; p < received_points.size(); p += 3)
778 std::array<T, 3> point;
779 std::copy_n(std::next(received_points.begin(), p), 3, point.begin());
782 mesh, candidate_collisions.
links(p / 3), point,
783 10 * std::numeric_limits<T>::epsilon());
786 cell_indicator[p / 3] = (colliding_cell >= 0) ? rank : -1;
789 closest_cells[p / 3] = colliding_cell;
794 MPI_Comm reverse_comm;
795 MPI_Dist_graph_create_adjacent(
796 comm, out_ranks.size(), out_ranks.data(), MPI_UNWEIGHTED, in_ranks.size(),
797 in_ranks.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &reverse_comm);
802 auto rescale = [](
auto& x)
804 std::transform(x.cbegin(), x.cend(), x.begin(),
805 [](
auto e) { return (e / 3); });
808 rescale(recv_offsets);
810 rescale(send_offsets);
813 std::swap(recv_sizes, send_sizes);
814 std::swap(recv_offsets, send_offsets);
817 std::vector<std::int32_t> recv_ranks(recv_offsets.back());
818 MPI_Neighbor_alltoallv(cell_indicator.data(), send_sizes.data(),
819 send_offsets.data(), MPI_INT32_T, recv_ranks.data(),
820 recv_sizes.data(), recv_offsets.data(), MPI_INT32_T,
823 std::vector<std::int32_t> point_owners(points.size() / 3, -1);
824 for (std::size_t i = 0; i < unpack_map.size(); i++)
826 const std::int32_t pos = unpack_map[i];
828 if (recv_ranks[i] >= 0 && point_owners[pos] == -1)
829 point_owners[pos] = recv_ranks[i];
834 std::vector<std::uint8_t> send_extrapolate(recv_offsets.back());
835 for (std::int32_t i = 0; i < recv_offsets.back(); i++)
837 const std::int32_t pos = unpack_map[i];
838 send_extrapolate[i] = point_owners[pos] == -1;
843 std::swap(send_sizes, recv_sizes);
844 std::swap(send_offsets, recv_offsets);
845 std::vector<std::uint8_t> dest_extrapolate(recv_offsets.back());
846 MPI_Neighbor_alltoallv(send_extrapolate.data(), send_sizes.data(),
847 send_offsets.data(), MPI_UINT8_T,
848 dest_extrapolate.data(), recv_sizes.data(),
849 recv_offsets.data(), MPI_UINT8_T, forward_comm);
851 std::vector<T> squared_distances(received_points.size() / 3, -1);
853 for (std::size_t i = 0; i < dest_extrapolate.size(); i++)
855 if (dest_extrapolate[i] == 1)
857 assert(closest_cells[i] == -1);
858 std::array<T, 3> point;
859 std::copy_n(std::next(received_points.begin(), 3 * i), 3, point.begin());
862 T shortest_distance = std::numeric_limits<T>::max();
863 std::int32_t closest_cell = -1;
864 for (
auto cell : candidate_collisions.
links(i))
866 auto dofs = MDSPAN_IMPL_STANDARD_NAMESPACE::submdspan(
867 x_dofmap, cell, MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent);
868 std::vector<T> nodes(3 * dofs.size());
869 for (std::size_t j = 0; j < dofs.size(); ++j)
871 const int pos = 3 * dofs[j];
872 for (std::size_t k = 0; k < 3; ++k)
873 nodes[3 * j + k] = geom_dofs[pos + k];
875 const std::array<T, 3> d = compute_distance_gjk<T>(
876 std::span<const T>(point.data(), point.size()), nodes);
877 if (T current_distance = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
878 current_distance < shortest_distance)
880 shortest_distance = current_distance;
884 closest_cells[i] = closest_cell;
885 squared_distances[i] = shortest_distance;
889 std::swap(recv_sizes, send_sizes);
890 std::swap(recv_offsets, send_offsets);
893 std::vector<T> recv_distances(recv_offsets.back());
894 MPI_Neighbor_alltoallv(
895 squared_distances.data(), send_sizes.data(), send_offsets.data(),
896 dolfinx::MPI::mpi_type<T>(), recv_distances.data(), recv_sizes.data(),
897 recv_offsets.data(), dolfinx::MPI::mpi_type<T>(), reverse_comm);
900 std::vector<T> closest_distance(point_owners.size(),
901 std::numeric_limits<T>::max());
902 for (std::size_t i = 0; i < out_ranks.size(); i++)
904 for (std::int32_t j = recv_offsets[i]; j < recv_offsets[i + 1]; j++)
906 const std::int32_t pos = unpack_map[j];
907 auto current_dist = recv_distances[j];
909 if (
auto d = closest_distance[pos];
910 (current_dist > 0) and (current_dist < d))
912 point_owners[pos] = out_ranks[i];
913 closest_distance[pos] = current_dist;
919 std::swap(send_sizes, recv_sizes);
920 std::swap(send_offsets, recv_offsets);
923 std::vector<std::int32_t> send_owners(send_offsets.back());
924 std::fill(counter.begin(), counter.end(), 0);
925 for (std::size_t i = 0; i < points.size() / 3; ++i)
927 for (
auto p : collisions.
links(i))
929 int neighbor = rank_to_neighbor[p];
930 send_owners[send_offsets[neighbor] + counter[neighbor]++]
936 std::vector<std::int32_t> dest_ranks(recv_offsets.back());
937 MPI_Neighbor_alltoallv(send_owners.data(), send_sizes.data(),
938 send_offsets.data(), MPI_INT32_T, dest_ranks.data(),
939 recv_sizes.data(), recv_offsets.data(), MPI_INT32_T,
943 std::vector<std::int32_t> owned_recv_ranks;
944 owned_recv_ranks.reserve(recv_offsets.back());
945 std::vector<T> owned_recv_points;
946 std::vector<std::int32_t> owned_recv_cells;
947 for (std::size_t i = 0; i < in_ranks.size(); i++)
949 for (std::int32_t j = recv_offsets[i]; j < recv_offsets[i + 1]; j++)
951 if (rank == dest_ranks[j])
953 owned_recv_ranks.push_back(in_ranks[i]);
954 owned_recv_points.insert(
955 owned_recv_points.end(), std::next(received_points.cbegin(), 3 * j),
956 std::next(received_points.cbegin(), 3 * (j + 1)));
957 owned_recv_cells.push_back(closest_cells[j]);
962 MPI_Comm_free(&forward_comm);
963 MPI_Comm_free(&reverse_comm);
965 return std::make_tuple(point_owners, owned_recv_ranks, owned_recv_points,