111 size_t NumPart_boundary,
112 std::vector<FoFCell> & PartCells,
113 std::vector<size_t> & particlegriddata) {
116 const double dx_cells = 1.0 / Ngrid;
119 const double dx_box_domain = xmax_box - xmin_box;
122 Local_nx = std::ceil(dx_box_domain / dx_cells);
125 const size_t NgridTot = size_t(Local_nx) *
FML::power(Ngrid,
NDIM - 1);
128 PartCells.resize(NgridTot);
131 auto get_cell_index = [&](
size_t ipart, std::vector<int> & coord,
int type) {
132 decltype(part[0].get_pos()) pos;
138 throw std::runtime_error(
"FoF::The particle is somehow marked as not being left/regular/right! "
139 "Should not happen ever!\n");
143 size_t index_cell = 0;
144 for (
int idim = 0; idim <
NDIM; idim++) {
145 auto dx = (pos[idim] - xmin_box * (idim == 0 ? 1 : 0));
146 coord[idim] = std::clamp(
int(dx * Ngrid), 0, (idim == 0 ? Local_nx : Ngrid) - 1);
147 index_cell = index_cell * Ngrid + coord[idim];
153 std::vector<int> coord(
NDIM);
154 for (
size_t i = 0; i < NumPart; i++) {
156 PartCells[index_cell].np++;
158 for (
size_t i = 0; i < NumPart_boundary; i++) {
160 PartCells[index_cell].np_boundary++;
164 particlegriddata.assign(NumPart_boundary + NumPart, 0);
165 size_t * ptr = particlegriddata.data();
169 [[maybe_unused]]
size_t np_total = 0;
170 [[maybe_unused]]
size_t np_boundary_total = 0;
171 for (
auto &
cell : PartCells) {
172 cell.ParticleIndexBoundary = ptr;
173 ptr +=
cell.np_boundary;
174 cell.ParticleIndex = ptr;
177 np_max = std::max(np_max,
cell.np);
179 np_boundary_total +=
cell.np_boundary;
181 cell.np_boundary = 0;
185 for (
size_t i = 0; i < NumPart; i++) {
186 const size_t index_cell = get_cell_index(i, coord, 0);
187 auto & np = PartCells[index_cell].np;
188 PartCells[index_cell].ParticleIndex[np] = i;
191 for (
size_t i = 0; i < NumPart_boundary; i++) {
192 const size_t index_cell = get_cell_index(i, coord, -1);
193 auto & np_boundary = PartCells[index_cell].np_boundary;
194 PartCells[index_cell].ParticleIndexBoundary[np_boundary] = i;
205 double Buffersize_over_Boxsize,
206 std::vector<FoFHaloClass> & LocalFoFGroups,
209 LocalFoFGroups.clear();
212 if (std::all_of(local_counts.begin(), local_counts.end(), [](
auto n) { return n == 0; }))
215 std::all_of(local_counts.begin(), local_counts.end(), [](
auto n) { return n > 0; }),
216 "[FoF] Nonempty global catalogues require particles on every rank");
217 [[maybe_unused]]
const bool debug =
false;
220 assert_mpi(FML::PARTICLE::GetNDIM(T()) ==
NDIM,
221 "FriendsOfFriends::The dimensions of particle and method do not match!\n");
224 size_t NumPart_tot = NumPart;
226 const double mean_particle_separation = 1.0 / std::pow(
double(NumPart_tot), 1.0 /
NDIM);
227 const double fof_distance =
linking_length * mean_particle_separation;
228 const double fof_distance2 = fof_distance * fof_distance;
234 Ngrid = std::min(
int(0.5 / fof_distance), Ngrid);
235 Ngrid = std::max(1, std::min(Ngrid,
int(1.0 / mean_particle_separation)));
241 assert_mpi(1.0 / Ngrid > 2 * fof_distance,
242 "FriendsOfFriends::The gridsize is too large (larger than the 2x linking distance)");
247 std::sort(part, part + NumPart, [](
const T & a,
const T & b) {
249 constexpr int axis = 1;
250 T *
f =
const_cast<T *
>(&a);
251 T * g =
const_cast<T *
>(&b);
254 return pos_b[axis] > pos_a[axis];
261 const double dx_boundary =
FML::NTasks == 1 ? 0.0 : Buffersize_over_Boxsize;
264 if (dx_boundary > xmax - xmin) {
265 throw std::runtime_error(
"FoF::The boundary asked for is larger than the neighbor task. Reduce "
266 "boundary (or the number of CPUs)\n");
273 std::vector<T> boundary_particles{};
274 const auto record_size = FML::PARTICLE::GetSize(part[0]);
275 const size_t bytes_per_particle = record_size > 0 ? size_t(record_size) : 0;
278 size_t n_send_left = 0;
279 size_t n_send_right = 0;
280 size_t n_recv_left = 0;
281 size_t n_recv_right = 0;
290 record_bytes > 0 && std::all_of(record_sizes.begin(),
292 [&](
auto n) { return n == record_bytes; }),
293 "[FoF] Boundary exchange requires a uniform positive serialized record size");
294 bool valid_sizes =
true;
297 for (
size_t i = 0; i < NumPart; i++) {
298 const auto size = FML::PARTICLE::GetSize(part[i]);
299 valid_sizes &= size > 0 && size_t(size) == bytes_per_particle;
301 if (pos[0] < xmin + dx_boundary)
303 if (pos[0] > xmax - dx_boundary)
305 if (pos[0] < xmin or pos[0] > xmax)
309 noutside == 0 && valid_sizes,
310 "[FoF] Boundary particles must be in-domain and have a uniform serialized size");
317 MPI_Sendrecv(&n_send_left,
323 sizeof(n_recv_right),
330 MPI_Sendrecv(&n_send_right,
331 sizeof(n_send_right),
346 n_recv_left = n_send_left = 0;
348 n_recv_right = n_send_right = 0;
353 std::cout <<
"Task " <<
FML::ThisTask <<
" will send " << n_send_right <<
" to " << RightTask
355 std::cout <<
"Task " <<
FML::ThisTask <<
" will send " << n_send_left <<
" to " << LeftTask
357 std::cout <<
"Task " <<
FML::ThisTask <<
" will recv " << n_recv_right <<
" from " << RightTask
359 std::cout <<
"Task " <<
FML::ThisTask <<
" will recv " << n_recv_left <<
" from " << LeftTask
361 std::cout <<
"Send fraction " << (n_send_left + n_send_right) /
double(NumPart) <<
"\n";
365 const size_t bytes_send_left =
367 const size_t bytes_send_right =
369 const size_t bytes_recv_left =
371 const size_t bytes_recv_right =
375 std::vector<char> CommBufferSend;
376 std::vector<char> CommBufferRecv;
377 CommBufferSend.resize(std::max(bytes_send_left, bytes_send_right));
378 CommBufferRecv.resize(std::max(bytes_recv_left, bytes_recv_right));
383 boundary_particles.max_size());
384 boundary_particles.resize(incoming.total);
387 char * buffer = CommBufferSend.data();
389 for (
size_t i = 0; i < NumPart; i++) {
391 if (pos[0] < xmin + dx_boundary) {
393 buffer += bytes_per_particle;
398 MPI_Sendrecv(CommBufferSend.data(),
403 CommBufferRecv.data(),
412 buffer = CommBufferRecv.data();
413 for (
size_t i = 0; i < n_recv_right; i++) {
415 buffer += bytes_per_particle;
419 buffer = CommBufferSend.data();
420 if (n_send_right > 0)
421 for (
size_t i = 0; i < NumPart; i++) {
423 if (pos[0] > xmax - dx_boundary) {
425 buffer += bytes_per_particle;
430 MPI_Sendrecv(CommBufferSend.data(),
435 CommBufferRecv.data(),
444 buffer = CommBufferRecv.data();
445 for (
size_t i = 0; i < n_recv_left; i++) {
447 buffer += bytes_per_particle;
451 CommBufferSend = std::vector<char>();
452 CommBufferRecv = std::vector<char>();
458 for (
size_t i = 0; i < n_recv_left; i++) {
466 for (
size_t i = 0; i < n_recv_right; i++) {
475 const auto NumPart_boundary = boundary_particles.size();
476 const auto NumPart_total = NumPart + NumPart_boundary;
481 std::vector<FoFCell> PartCells;
482 std::vector<size_t> particlegriddata;
490 boundary_particles.data(),
491 boundary_particles.size(),
501 std::cout <<
"#=====================================================\n";
503 std::cout <<
"# ___________________ ___________ \n";
504 std::cout <<
"# \\_ _____/\\_____ \\ \\_ _____/ \n";
505 std::cout <<
"# | __) / | \\ | __) \n";
506 std::cout <<
"# | \\ / | \\| \\ \n";
507 std::cout <<
"# \\___ / \\_______ /\\___ / \n";
508 std::cout <<
"# \\/ \\/ \\/ \n";
510 std::cout <<
"# FriendsOfFriends linking\n";
512 std::cout <<
"# FoF Linking Distance: " << fof_distance <<
"\n";
513 std::cout <<
"# dx_boundary / Boxsize: " << Buffersize_over_Boxsize <<
"\n";
514 std::cout <<
"# Periodic box: " << std::boolalpha <<
periodic <<
"\n";
515 std::cout <<
"# FoF linking Gridsize = " << Ngrid <<
" Local_nx: " << Local_nx <<
"\n";
516 std::cout <<
"# Maximum possible FoF gridsize = " << int(0.5 / fof_distance) <<
"\n";
517 std::cout <<
"# Npart_boundary : " << NumPart_boundary <<
" ( "
518 << NumPart_boundary / std::pow(1024.0, 2) * bytes_per_particle <<
" MB )\n";
519 std::cout <<
"# Grid memory per task: "
521 NumPart_total *
sizeof(
size_t)) /
524 std::cout <<
"# ...compared to particle memory: " << NumPart * bytes_per_particle / std::pow(1024.0, 2)
527 std::cout <<
"#=====================================================\n";
539 auto particle_id_FoF = std::vector<size_t>(NumPart + NumPart_boundary,
no_FoF_ID);
542 auto type_from_globalindex = [&](
size_t globalindex) {
543 if (globalindex < NumPart_boundary)
547 auto localindex_from_globalindex = [&](
size_t globalindex) {
548 if (globalindex < NumPart_boundary)
550 return globalindex - NumPart_boundary;
552 auto globalindex_from_localindex_type = [&](
size_t localindex,
int type) {
555 return localindex + NumPart_boundary;
557 auto particle_from_localindex_and_type = [&](
size_t localindex,
int type) {
559 return &boundary_particles[localindex];
560 return &part[localindex];
564 std::vector<size_t> friend_local_index_list;
565 std::vector<signed char> friend_type_list;
566 friend_local_index_list.reserve(1000);
567 friend_type_list.reserve(1000);
571 auto FindAllFriends = [&](T * curpart,
572 [[maybe_unused]]
int type,
573 [[maybe_unused]]
size_t localindex,
576 std::vector<size_t> & friend_local_index_list,
577 std::vector<signed char> & friend_type_list) {
582 std::array<int, NDIM> coord;
583 std::array<int, NDIM> di;
584 for (
int idim = 0; idim <
NDIM; idim++) {
585 double ix = (pos1[idim] - (xmin - dx_boundary) * (idim == 0 ? 1 : 0)) * Ngrid;
586 coord[idim] = std::clamp(
int(ix), 0, (idim == 0 ? Local_nx : Ngrid) - 1);
587 di[idim] = (ix - coord[idim] > 0.5) ? +1 : -1;
594 std::array<int, NDIM> icoord;
595 for (
int nbcell = 0; nbcell < twotondim; nbcell++) {
596 for (
int idim = 0, n = 1; idim <
NDIM; idim++, n *= 2) {
597 int go_left_right_or_stay = (nbcell / n % 2);
598 icoord[idim] = coord[idim] + go_left_right_or_stay * di[idim];
604 if (icoord[0] < 0 or icoord[0] >= Local_nx)
609 if (icoord[0] >= Ngrid)
615 size_t index_nbor_cell = icoord[0];
616 for (
int idim = 1; idim <
NDIM; idim++) {
619 if (icoord[idim] < 0)
620 icoord[idim] += Ngrid;
621 if (icoord[idim] >= Ngrid)
622 icoord[idim] -= Ngrid;
624 if (icoord[idim] < 0 or icoord[idim] >= Ngrid) {
629 index_nbor_cell = index_nbor_cell * Ngrid + icoord[idim];
637 const size_t np_boundary = PartCells[index_nbor_cell].np_boundary;
638 const size_t np = PartCells[index_nbor_cell].np;
639 const size_t np_total = np + np_boundary;
642 for (
size_t ii = 0;
ii < np_total;
ii++) {
645 size_t nborglobalindex{};
646 size_t nborlocalindex{};
649 if (
ii < np_boundary) {
650 nborlocalindex = PartCells[index_nbor_cell].ParticleIndexBoundary[
ii];
651 nborglobalindex = nborlocalindex;
652 nborpart = &boundary_particles[nborlocalindex];
655 nborlocalindex = PartCells[index_nbor_cell].ParticleIndex[
ii - np_boundary];
656 nborglobalindex = nborlocalindex + NumPart_boundary;
657 nborpart = &part[nborlocalindex];
662 if (particle_id_FoF[nborglobalindex] !=
no_FoF_ID)
664 if (nborglobalindex == globalindex)
670 std::array<double, NDIM> dx2;
671 for (
int idim = 0; idim <
NDIM; idim++) {
672 dx2[idim] = std::abs(pos2[idim] - pos1[idim]);
676 for (
int idim = 0; idim <
NDIM; idim++)
677 dist2 += dx2[idim] * dx2[idim];
680 if (dist2 < fof_distance2) {
681 particle_id_FoF[nborglobalindex] = FoFID;
683 if (particle_id_FoF[globalindex] ==
no_FoF_ID)
684 particle_id_FoF[globalindex] = FoFID;
686 friend_local_index_list.push_back(nborlocalindex);
687 friend_type_list.push_back(nbortype);
696 auto reaches_edge = [&](T & p) {
700 return x < xmin - dx_boundary + fof_distance or x > xmax + dx_boundary - fof_distance;
708 for (
size_t i = 0; i < NumPart_total; i++) {
712 if ((10 * i) / NumPart_total != (10 * i + 10) / NumPart_total or i == 0) {
713 std::cout << (100 * i + 100) / NumPart_total <<
"% " << std::flush;
714 if (i == NumPart_total - 1) {
715 std::cout << std::endl;
720 size_t globalindex = i;
721 size_t localindex = localindex_from_globalindex(globalindex);
722 int type = type_from_globalindex(globalindex);
723 T * curpart = particle_from_localindex_and_type(localindex, type);
728 friend_local_index_list.clear();
729 friend_type_list.clear();
732 FindAllFriends(curpart,
737 friend_local_index_list,
740 if (friend_local_index_list.size() == 0)
746 FoFHaloClass newhalo(FoFID);
748 bool reaches_buffer_edge = reaches_edge(*curpart);
750 while (friend_local_index_list.size() > 0) {
752 auto localindex = friend_local_index_list.back();
753 type = int(friend_type_list.back());
754 globalindex = globalindex_from_localindex_type(localindex, type);
755 curpart = particle_from_localindex_and_type(localindex, type);
759 reaches_buffer_edge = reaches_buffer_edge or reaches_edge(*curpart);
762 friend_local_index_list.pop_back();
763 friend_type_list.pop_back();
766 FindAllFriends(curpart,
771 friend_local_index_list,
776 if (
int(newhalo.np) >= nmin_FoF_group) {
777 double xhalo = newhalo.get_pos()[0];
779 xhalo += (xhalo < 0.0) ? 1.0 : ((xhalo >= 1.0) ? -1.0 : 0.0);
781 LocalFoFGroups.push_back(newhalo);
782 if (reaches_buffer_edge)
797 particlegriddata = std::vector<size_t>();
798 PartCells = std::vector<FoFCell>();
805 auto nhalos = LocalFoFGroups.size();
808 std::cout <<
"# We found a total of " << nhalos <<
" (" << LocalFoFGroups.size()
816 <<
" groups reach the edge of the buffer of particles copied from the neighbor tasks ("
817 << Buffersize_over_Boxsize <<
" of the box). They might be cut off and then also found as "
818 <<
"a second, partial group on the neighbor task. Increase the buffer (to more than the size "
819 <<
"of the largest groups) to be sure the result does not depend on the number of tasks\n";
822 std::vector<char> kept_group(FoFID, 0);
823 for (
auto &
h : LocalFoFGroups)
824 kept_group[
h.id] = 1;
829 std::vector<long long int> addfofid(
FML::NTasks, 0);
831 addfofid[i] = (
long long int)FoFID;
838 for (
auto &
h : LocalFoFGroups) {
847 if constexpr (FML::PARTICLE::has_set_fofid<T>()) {
848 auto global_id = [&](
size_t localid) {
851 for (
size_t i = 0; i < NumPart; i++)
856 std::vector<size_t> to_right(n_recv_right), to_left(n_recv_left);
857 for (
size_t i = 0; i < n_recv_right; i++)
858 to_right[i] = global_id(particle_id_FoF[i]);
859 for (
size_t i = 0; i < n_recv_left; i++)
860 to_left[i] = global_id(particle_id_FoF[n_recv_right + i]);
861 std::vector<size_t> from_left(n_send_left), from_right(n_send_right);
862 const int id_bytes_n_recv_right =
864 const int id_bytes_n_send_left =
866 const int id_bytes_n_recv_left =
868 const int id_bytes_n_send_right =
871 MPI_Sendrecv(to_right.data(),
872 id_bytes_n_recv_right,
877 id_bytes_n_send_left,
883 MPI_Sendrecv(to_left.data(),
884 id_bytes_n_recv_left,
889 id_bytes_n_send_right,
895 size_t il = 0, ir = 0;
896 for (
size_t i = 0; i < NumPart; i++) {
898 if (n_send_left > 0 and pos[0] < xmin + dx_boundary) {
899 const auto id = from_left[il++];
903 if (n_send_right > 0 and pos[0] > xmax - dx_boundary) {
904 const auto id = from_right[ir++];