13 #include <Compadre_Config.h>
17 #ifdef COMPADRE_USE_MPI
21 template<
typename T1,
typename T2>
22 std::pair<T2,T1>
swap_pair(
const std::pair<T1,T2> &item)
24 return std::pair<T2,T1>(item.second, item.first);
27 template<
typename T1,
typename T2>
28 std::multimap<T2,T1>
invert_map(
const std::map<T1,T2> &original_map)
30 std::multimap<T2,T1> new_map;
31 std::transform(original_map.begin(), original_map.end(), std::inserter(new_map, new_map.begin()), swap_pair<T1,T2>);
35 using namespace Compadre;
37 int main (
int argc,
char* args[]) {
40 #ifdef COMPADRE_USE_MPI
41 MPI_Init(&argc, &args);
45 Kokkos::initialize(argc, args);
48 bool all_passed =
true;
57 int arg5toi = atoi(args[4]);
59 search_type = arg5toi;
62 bool do_radius_search = (search_type==0);
63 bool do_knn_search = (search_type==1);
64 printf(
"do_radius_search: %d\n", do_radius_search);
65 printf(
"do_knn_search: %d\n", do_knn_search);
69 double h_multiplier = 3.0;
71 double arg4tof = atof(args[3]);
73 h_multiplier = arg4tof;
79 int number_target_coords = 200;
81 int arg3toi = atoi(args[2]);
83 number_target_coords = arg3toi;
91 int arg2toi = atoi(args[1]);
101 double h_spacing = 1./std::pow(number_target_coords, 0.50 + 0.2*(dimension==2));
102 int n_neg1_to_1 = 2*(1/h_spacing) + 1;
105 int number_source_coords = (n_neg1_to_1+1)*(n_neg1_to_1+1);
106 if (dimension==3) number_source_coords *= (n_neg1_to_1+1);
107 printf(
"target coords: %d\n", number_target_coords);
108 printf(
"source coords: %d\n", number_source_coords);
111 Kokkos::View<double**, Kokkos::DefaultHostExecutionSpace> source_coords(
"source coordinates",
112 number_source_coords, 3);
115 Kokkos::View<double**, Kokkos::DefaultHostExecutionSpace> target_coords(
"target coordinates", number_target_coords, 3);
118 int source_index = 0;
119 double this_coord[3] = {0,0,0};
120 for (
int i=-n_neg1_to_1/2; i<n_neg1_to_1/2+1; ++i) {
121 this_coord[0] = i*h_spacing;
122 for (
int j=-n_neg1_to_1/2; j<n_neg1_to_1/2+1; ++j) {
123 this_coord[1] = j*h_spacing;
124 for (
int k=-n_neg1_to_1/2; k<n_neg1_to_1/2+1; ++k) {
125 this_coord[2] = k*h_spacing;
127 source_coords(source_index,0) = this_coord[0];
128 source_coords(source_index,1) = this_coord[1];
129 source_coords(source_index,2) = this_coord[2];
134 source_coords(source_index,0) = this_coord[0];
135 source_coords(source_index,1) = this_coord[1];
136 source_coords(source_index,2) = 0;
141 source_coords(source_index,0) = this_coord[0];
142 source_coords(source_index,1) = 0;
143 source_coords(source_index,2) = 0;
149 for(
int i=0; i<number_target_coords; i++){
152 double rand_dir[3] = {0,0,0};
154 for (
int j=0; j<dimension; ++j) {
156 rand_dir[j] = ((double)rand() / (double) RAND_MAX) - 0.5;
160 for (
int j=0; j<dimension; ++j) {
161 target_coords(i,j) = rand_dir[j];
166 if (do_radius_search) {
169 printf(
"\n1D compressed row neighbor lists:\n");
174 Kokkos::View<int*, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
175 number_target_coords);
176 Kokkos::View<int*, Kokkos::DefaultHostExecutionSpace> number_neighbors_list(
"number of neighbors list",
177 number_target_coords);
180 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
181 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> random_vec(
"random vector", number_target_coords);
183 Kokkos::parallel_for(
"random perturbation of search size", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
184 (0,number_target_coords), [&](
const int i) {
186 random_vec(i) = 0.5 * ((double)rand() / (double) RAND_MAX) - 0.5;
187 epsilon(i) = (h_multiplier + random_vec(i)) * h_spacing;
194 size_t total_num_neighbors = point_cloud_search.generateCRNeighborListsFromRadiusSearch(
true ,
195 target_coords, neighbor_lists, number_neighbors_list, epsilon);
196 printf(
"total num neighbors: %lu\n", total_num_neighbors);
199 Kokkos::resize(neighbor_lists, total_num_neighbors);
202 point_cloud_search.generateCRNeighborListsFromRadiusSearch(
false ,
203 target_coords, neighbor_lists, number_neighbors_list, epsilon);
207 double radius_search_time = timer.seconds();
208 printf(
"nanoflann search time: %f s\n", radius_search_time);
212 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
213 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
214 (0,number_target_coords), [&](
const int i) {
215 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
216 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
217 point_cloud_neighbor_list_i[nla.getNeighborHost(i,j)] = 1.0;
220 double point_cloud_convert_time = timer.seconds();
221 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
226 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
227 Kokkos::parallel_for(
"brute force radius search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
228 (0,number_target_coords), [&](
const int i) {
229 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
230 for (
int j=0; j<number_source_coords; ++j) {
232 for (
int k=0; k<dimension; ++k) {
233 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
235 dist = std::sqrt(dist);
237 if (dist<(h_multiplier + random_vec(i))*h_spacing) {
238 brute_force_neighbor_list_i[j]=dist;
242 double brute_force_search_time = timer.seconds();
243 printf(
"brute force search time: %f s\n", brute_force_search_time);
249 Kokkos::parallel_reduce(
"check brute force search in point cloud search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
250 (0,number_target_coords), [&](
const int i,
int& t_num_passed) {
251 bool all_found =
true;
252 for (
auto it=brute_force_neighbor_list[i].begin(); it!=brute_force_neighbor_list[i].end(); ++it) {
253 if (point_cloud_neighbor_list[i].count(it->first)!=1) {
257 if (all_found) t_num_passed++;
258 }, Kokkos::Sum<int>(num_passed));
260 if (num_passed != number_target_coords) {
261 printf(
"Brute force neighbor not found in point cloud list\n");
267 Kokkos::parallel_reduce(
"original in brute force search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
268 (0,number_target_coords), KOKKOS_LAMBDA(
const int i,
int& t_num_passed) {
269 bool all_found =
true;
270 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
271 if (brute_force_neighbor_list[i].count(nla.getNeighborHost(i,j))!=1) {
275 if (all_found) t_num_passed++;
276 }, Kokkos::Sum<int>(num_passed));
278 if (num_passed != number_target_coords) {
279 printf(
"Point cloud neighbor not found in brute force list\n");
283 double check_time = timer.seconds();
284 printf(
"check time: %f s\n", check_time);
288 printf(
"\n2D neighbor lists:\n");
293 Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
294 number_target_coords, 2);
297 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
298 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> random_vec(
"random vector", number_target_coords);
300 Kokkos::parallel_for(
"random perturbation of search size", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
301 (0,number_target_coords), [&](
const int i) {
303 random_vec(i) = 0.5 * ((double)rand() / (double) RAND_MAX) - 0.5;
304 epsilon(i) = (h_multiplier + random_vec(i)) * h_spacing;
311 auto max_num_neighbors = point_cloud_search.generate2DNeighborListsFromRadiusSearch(
true ,
312 target_coords, neighbor_lists, epsilon);
313 printf(
"max num neighbors: %lu\n", max_num_neighbors);
316 neighbor_lists = Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace>(
"neighbor lists",
317 number_target_coords, 1+max_num_neighbors);
320 max_num_neighbors = point_cloud_search.generate2DNeighborListsFromRadiusSearch(
false ,
321 target_coords, neighbor_lists, epsilon);
323 double radius_search_time = timer.seconds();
324 printf(
"nanoflann search time: %f s\n", radius_search_time);
328 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
329 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
330 (0,number_target_coords), [&](
const int i) {
331 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
332 for (
int j=1; j<=neighbor_lists(i,0); ++j) {
333 point_cloud_neighbor_list_i[neighbor_lists(i,j)] = 1.0;
336 double point_cloud_convert_time = timer.seconds();
337 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
342 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
343 Kokkos::parallel_for(
"brute force radius search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
344 (0,number_target_coords), [&](
const int i) {
345 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
346 for (
int j=0; j<number_source_coords; ++j) {
348 for (
int k=0; k<dimension; ++k) {
349 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
351 dist = std::sqrt(dist);
353 if (dist<(h_multiplier + random_vec(i))*h_spacing) {
354 brute_force_neighbor_list_i[j]=dist;
358 double brute_force_search_time = timer.seconds();
359 printf(
"brute force search time: %f s\n", brute_force_search_time);
365 Kokkos::parallel_reduce(
"check brute force search in point cloud search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
366 (0,number_target_coords), [&](
const int i,
int& t_num_passed) {
367 bool all_found =
true;
368 for (
auto it=brute_force_neighbor_list[i].begin(); it!=brute_force_neighbor_list[i].end(); ++it) {
369 if (point_cloud_neighbor_list[i].count(it->first)!=1) {
373 if (all_found) t_num_passed++;
374 }, Kokkos::Sum<int>(num_passed));
376 if (num_passed != number_target_coords) {
377 printf(
"Brute force neighbor not found in point cloud list\n");
383 Kokkos::parallel_reduce(
"original in brute force search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
384 (0,number_target_coords), KOKKOS_LAMBDA(
const int i,
int& t_num_passed) {
385 bool all_found =
true;
386 for (
int j=1; j<=neighbor_lists(i,0); ++j) {
387 if (brute_force_neighbor_list[i].count(neighbor_lists(i,j))!=1) {
391 if (all_found) t_num_passed++;
392 }, Kokkos::Sum<int>(num_passed));
394 if (num_passed != number_target_coords) {
395 printf(
"Point cloud neighbor not found in brute force list\n");
399 double check_time = timer.seconds();
400 printf(
"check time: %f s\n", check_time);
404 printf(
"\n2D neighbor lists converted to 1D compressed row neighbor lists:\n");
409 Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
410 number_target_coords, 2);
413 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
414 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> random_vec(
"random vector", number_target_coords);
416 Kokkos::parallel_for(
"random perturbation of search size", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
417 (0,number_target_coords), [&](
const int i) {
419 random_vec(i) = 0.5 * ((double)rand() / (double) RAND_MAX) - 0.5;
420 epsilon(i) = (h_multiplier + random_vec(i)) * h_spacing;
427 auto max_num_neighbors = point_cloud_search.generate2DNeighborListsFromRadiusSearch(
true ,
428 target_coords, neighbor_lists, epsilon);
429 printf(
"max num neighbors: %lu\n", max_num_neighbors);
432 neighbor_lists = Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace>(
"neighbor lists",
433 number_target_coords, 1+max_num_neighbors);
436 max_num_neighbors = point_cloud_search.generate2DNeighborListsFromRadiusSearch(
false ,
437 target_coords, neighbor_lists, epsilon);
441 double radius_search_time = timer.seconds();
442 printf(
"nanoflann search time: %f s\n", radius_search_time);
446 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
447 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
448 (0,number_target_coords), [&](
const int i) {
449 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
450 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
451 point_cloud_neighbor_list_i[nla.getNeighborHost(i,j)] = 1.0;
454 double point_cloud_convert_time = timer.seconds();
455 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
460 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
461 Kokkos::parallel_for(
"brute force radius search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
462 (0,number_target_coords), [&](
const int i) {
463 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
464 for (
int j=0; j<number_source_coords; ++j) {
466 for (
int k=0; k<dimension; ++k) {
467 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
469 dist = std::sqrt(dist);
471 if (dist<(h_multiplier + random_vec(i))*h_spacing) {
472 brute_force_neighbor_list_i[j]=dist;
476 double brute_force_search_time = timer.seconds();
477 printf(
"brute force search time: %f s\n", brute_force_search_time);
483 Kokkos::parallel_reduce(
"check brute force search in point cloud search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
484 (0,number_target_coords), [&](
const int i,
int& t_num_passed) {
485 bool all_found =
true;
486 for (
auto it=brute_force_neighbor_list[i].begin(); it!=brute_force_neighbor_list[i].end(); ++it) {
487 if (point_cloud_neighbor_list[i].count(it->first)!=1) {
491 if (all_found) t_num_passed++;
492 }, Kokkos::Sum<int>(num_passed));
494 if (num_passed != number_target_coords) {
495 printf(
"Brute force neighbor not found in point cloud list\n");
501 Kokkos::parallel_reduce(
"original in brute force search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
502 (0,number_target_coords), KOKKOS_LAMBDA(
const int i,
int& t_num_passed) {
503 bool all_found =
true;
504 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
505 if (brute_force_neighbor_list[i].count(nla.getNeighborHost(i,j))!=1) {
509 if (all_found) t_num_passed++;
510 }, Kokkos::Sum<int>(num_passed));
512 if (num_passed != number_target_coords) {
513 printf(
"Point cloud neighbor not found in brute force list\n");
517 double check_time = timer.seconds();
518 printf(
"check time: %f s\n", check_time);
521 else if (do_knn_search) {
526 printf(
"\n1D compressed row neighbor lists:\n");
532 Kokkos::View<int*, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
533 number_target_coords);
534 Kokkos::View<int*, Kokkos::DefaultHostExecutionSpace> number_neighbors_list(
"number of neighbors list",
535 number_target_coords);
538 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
543 auto total_num_neighbors = point_cloud_search.generateCRNeighborListsFromKNNSearch(
true ,
544 target_coords, neighbor_lists, number_neighbors_list, epsilon, min_neighbors, 1.5 );
545 printf(
"total num neighbors: %lu\n", total_num_neighbors);
548 Kokkos::resize(neighbor_lists, total_num_neighbors);
551 point_cloud_search.generateCRNeighborListsFromKNNSearch(
false ,
552 target_coords, neighbor_lists, number_neighbors_list, epsilon, min_neighbors, 1.5 );
558 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
559 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
560 (0,number_target_coords), [&](
const int i) {
561 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
562 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
564 for (
int k=0; k<dimension; ++k) {
565 dist += (target_coords(i,k)-source_coords(nla.getNeighborHost(i,j),k))*(target_coords(i,k)-source_coords(nla.getNeighborHost(i,j),k));
567 dist = std::sqrt(dist);
568 point_cloud_neighbor_list_i[nla.getNeighborHost(i,j)] = dist;
571 double point_cloud_convert_time = timer.seconds();
572 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
577 int epsilon_diff_from_knn_dist_multiplied = 0;
578 Kokkos::parallel_reduce(
"check kth neighbor in point cloud", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
579 (0,number_target_coords), [&](
const int i,
int& t_epsilon_diff_from_knn_dist_multiplied) {
580 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
581 std::multimap<double, int> inverted_map =
invert_map(point_cloud_neighbor_list_i);
583 for (
auto it=inverted_map.begin(); it!=inverted_map.end(); ++it) {
584 if (j==min_neighbors-1) {
585 if (std::abs(epsilon(i)/1.5 - it->first) > eps) {
586 t_epsilon_diff_from_knn_dist_multiplied++;
592 }, Kokkos::Sum<int>(epsilon_diff_from_knn_dist_multiplied));
593 if (epsilon_diff_from_knn_dist_multiplied > 0) {
594 printf(
"1.5*kth_neighbor_distance differs from calculation in epsilon vector.\n");
603 int total_insufficient_neighbors = 0;
604 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
605 Kokkos::parallel_reduce(
"brute force knn search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
606 (0,number_target_coords), [&](
const int i,
int& t_total_insufficient_neighbors) {
607 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
608 for (
int j=0; j<number_source_coords; ++j) {
610 for (
int k=0; k<dimension; ++k) {
611 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
613 dist = std::sqrt(dist);
616 if (dist<(epsilon(i)/1.5 + eps)) {
617 brute_force_neighbor_list_i[j]=dist;
621 if (brute_force_neighbor_list_i.size() < (size_t)min_neighbors) t_total_insufficient_neighbors++;
622 }, Kokkos::Sum<int>(total_insufficient_neighbors));
623 if (total_insufficient_neighbors > 0) {
624 printf(
"less neighbors found using kth_neighbor_distance+eps than using knn search.\n");
627 double brute_force_search_time = timer.seconds();
628 printf(
"brute force search time: %f s\n", brute_force_search_time);
633 printf(
"\n2D neighbor lists:\n");
639 Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
640 number_target_coords, 1+min_neighbors);
643 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
647 auto max_num_neighbors = point_cloud_search.generate2DNeighborListsFromKNNSearch(
true ,
648 target_coords, neighbor_lists, epsilon, min_neighbors, 1.5 );
649 printf(
"max num neighbors: %lu\n", max_num_neighbors);
652 Kokkos::resize(neighbor_lists, neighbor_lists.extent(0), 1+max_num_neighbors);
655 max_num_neighbors = point_cloud_search.generate2DNeighborListsFromKNNSearch(
false ,
656 target_coords, neighbor_lists, epsilon, min_neighbors, 1.5 );
660 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
661 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
662 (0,number_target_coords), [&](
const int i) {
663 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
664 for (
int j=1; j<=neighbor_lists(i,0); ++j) {
666 for (
int k=0; k<dimension; ++k) {
667 dist += (target_coords(i,k)-source_coords(neighbor_lists(i,j),k))*(target_coords(i,k)-source_coords(neighbor_lists(i,j),k));
669 dist = std::sqrt(dist);
670 point_cloud_neighbor_list_i[neighbor_lists(i,j)] = dist;
673 double point_cloud_convert_time = timer.seconds();
674 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
679 int epsilon_diff_from_knn_dist_multiplied = 0;
680 Kokkos::parallel_reduce(
"check kth neighbor in point cloud", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
681 (0,number_target_coords), [&](
const int i,
int& t_epsilon_diff_from_knn_dist_multiplied) {
682 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
683 std::multimap<double, int> inverted_map =
invert_map(point_cloud_neighbor_list_i);
685 for (
auto it=inverted_map.begin(); it!=inverted_map.end(); ++it) {
686 if (j==min_neighbors-1) {
687 if (std::abs(epsilon(i)/1.5 - it->first) > eps) {
688 t_epsilon_diff_from_knn_dist_multiplied++;
694 }, Kokkos::Sum<int>(epsilon_diff_from_knn_dist_multiplied));
695 if (epsilon_diff_from_knn_dist_multiplied > 0) {
696 printf(
"1.5*kth_neighbor_distance differs from calculation in epsilon vector.\n");
705 int total_insufficient_neighbors = 0;
706 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
707 Kokkos::parallel_reduce(
"brute force knn search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
708 (0,number_target_coords), [&](
const int i,
int& t_total_insufficient_neighbors) {
709 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
710 for (
int j=0; j<number_source_coords; ++j) {
712 for (
int k=0; k<dimension; ++k) {
713 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
715 dist = std::sqrt(dist);
718 if (dist<(epsilon(i)/1.5 + eps)) {
719 brute_force_neighbor_list_i[j]=dist;
723 if (brute_force_neighbor_list_i.size() < (size_t)min_neighbors) t_total_insufficient_neighbors++;
724 }, Kokkos::Sum<int>(total_insufficient_neighbors));
725 if (total_insufficient_neighbors > 0) {
726 printf(
"less neighbors found using kth_neighbor_distance+eps than using knn search.\n");
729 double brute_force_search_time = timer.seconds();
730 printf(
"brute force search time: %f s\n", brute_force_search_time);
735 printf(
"\n2D neighbor lists converted to 1D compressed row neighbor lists:\n");
741 Kokkos::View<int**, Kokkos::DefaultHostExecutionSpace> neighbor_lists(
"neighbor lists",
742 number_target_coords, 1+min_neighbors);
745 Kokkos::View<double*, Kokkos::DefaultHostExecutionSpace> epsilon(
"h supports", number_target_coords);
749 auto max_num_neighbors = point_cloud_search.generate2DNeighborListsFromKNNSearch(
true ,
750 target_coords, neighbor_lists, epsilon, min_neighbors, 1.5 );
751 printf(
"max num neighbors: %lu\n", max_num_neighbors);
754 Kokkos::resize(neighbor_lists, neighbor_lists.extent(0), 1+max_num_neighbors);
757 max_num_neighbors = point_cloud_search.generate2DNeighborListsFromKNNSearch(
false ,
758 target_coords, neighbor_lists, epsilon, min_neighbors, 1.5 );
764 std::vector<std::map<int, double> > point_cloud_neighbor_list(number_target_coords, std::map<int, double>());
765 Kokkos::parallel_for(
"point search conversion", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
766 (0,number_target_coords), [&](
const int i) {
767 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
768 for (
int j=0; j<nla.getNumberOfNeighborsHost(i); ++j) {
770 for (
int k=0; k<dimension; ++k) {
771 dist += (target_coords(i,k)-source_coords(nla.getNeighborHost(i,j),k))*(target_coords(i,k)-source_coords(nla.getNeighborHost(i,j),k));
773 dist = std::sqrt(dist);
774 point_cloud_neighbor_list_i[nla.getNeighborHost(i,j)] = dist;
777 double point_cloud_convert_time = timer.seconds();
778 printf(
"point cloud convert time: %f s\n", point_cloud_convert_time);
783 int epsilon_diff_from_knn_dist_multiplied = 0;
784 Kokkos::parallel_reduce(
"check kth neighbor in point cloud", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
785 (0,number_target_coords), [&](
const int i,
int& t_epsilon_diff_from_knn_dist_multiplied) {
786 auto &point_cloud_neighbor_list_i =
const_cast<std::map<int, double>&
>(point_cloud_neighbor_list[i]);
787 std::multimap<double, int> inverted_map =
invert_map(point_cloud_neighbor_list_i);
789 for (
auto it=inverted_map.begin(); it!=inverted_map.end(); ++it) {
790 if (j==min_neighbors-1) {
791 if (std::abs(epsilon(i)/1.5 - it->first) > eps) {
792 t_epsilon_diff_from_knn_dist_multiplied++;
798 }, Kokkos::Sum<int>(epsilon_diff_from_knn_dist_multiplied));
799 if (epsilon_diff_from_knn_dist_multiplied > 0) {
800 printf(
"1.5*kth_neighbor_distance differs from calculation in epsilon vector.\n");
809 int total_insufficient_neighbors = 0;
810 std::vector<std::map<int, double> > brute_force_neighbor_list(number_target_coords, std::map<int, double>());
811 Kokkos::parallel_reduce(
"brute force knn search", Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>
812 (0,number_target_coords), [&](
const int i,
int& t_total_insufficient_neighbors) {
813 auto &brute_force_neighbor_list_i =
const_cast<std::map<int, double>&
>(brute_force_neighbor_list[i]);
814 for (
int j=0; j<number_source_coords; ++j) {
816 for (
int k=0; k<dimension; ++k) {
817 dist += (target_coords(i,k)-source_coords(j,k))*(target_coords(i,k)-source_coords(j,k));
819 dist = std::sqrt(dist);
822 if (dist<(epsilon(i)/1.5 + eps)) {
823 brute_force_neighbor_list_i[j]=dist;
827 if (brute_force_neighbor_list_i.size() < (size_t)min_neighbors) t_total_insufficient_neighbors++;
828 }, Kokkos::Sum<int>(total_insufficient_neighbors));
829 if (total_insufficient_neighbors > 0) {
830 printf(
"less neighbors found using kth_neighbor_distance+eps than using knn search.\n");
833 double brute_force_search_time = timer.seconds();
834 printf(
"brute force search time: %f s\n", brute_force_search_time);
839 printf(
"do_radius_search and do_knn_search both false. Invalid combination.\n");
845 #ifdef COMPADRE_USE_MPI
851 fprintf(stdout,
"Passed test \n");
854 fprintf(stdout,
"Failed test \n");
PointCloudSearch< view_type > CreatePointCloudSearch(view_type src_view, const local_index_type dimensions=-1, const local_index_type max_leaf=-1)
CreatePointCloudSearch allows for the construction of an object of type PointCloudSearch with templat...
int main(int argc, char **argv)
NeighborLists< view_type > CreateNeighborLists(view_type number_of_neighbors_list)
CreateNeighborLists allows for the construction of an object of type NeighborLists with template dedu...
std::pair< T2, T1 > swap_pair(const std::pair< T1, T2 > &item)
std::multimap< T2, T1 > invert_map(const std::map< T1, T2 > &original_map)
static KOKKOS_INLINE_FUNCTION int getNP(const int m, const int dimension=3, const ReconstructionSpace r_space=ReconstructionSpace::ScalarTaylorPolynomial)
Returns size of the basis for a given polynomial order and dimension General to dimension 1...
NeighborLists< view_type_1d > Convert2DToCompressedRowNeighborLists(view_type_2d neighbor_lists)
Converts 2D neighbor lists to compressed row neighbor lists.