PACMAN 0.1.0
Portable Algorithms for Coupling, Mapping, and Adaptive iNterpolation
Loading...
Searching...
No Matches
clusters_radius.hpp
Go to the documentation of this file.
1//
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4//
5
6#pragma once
7
8#include <Kokkos_Core.hpp>
9
10#include "callbacks.hpp"
11#include "common/types.hpp"
12#include "interpolator.hxx"
13
14namespace PACMAN {
15namespace RbfPum {
16
22FULL_TEMPLATE
23void TEMPLATED_CLASSNAME::FindRadius(void) {
24 assert(this->mNodesPerCluster > 0);
25 const std::string _region_name = "RbfPumInterpolator::find_radius";
26 Kokkos::Profiling::ScopedRegion region(_region_name);
27 const ExecSpace execspace{};
28
29 VectorView<Point> samples(Kokkos::view_alloc(execspace,
30 Kokkos::WithoutInitializing,
31 _region_name + "::samples"),
32 2 * Dim + 1);
33 auto samples_host = Kokkos::create_mirror_view(Kokkos::WithoutInitializing,
34 Kokkos::HostSpace{}, samples);
35
36 const Point lower = this->mSourceBvh.bounds().minCorner();
37 const Point upper = this->mSourceBvh.bounds().maxCorner();
38 Point center{};
39 for (int_t axis = 0; axis < Dim; ++axis) {
40 center[axis] = (lower[axis] + upper[axis]) / 2.;
41 }
42
43 samples_host(0) = Point{center};
44 for (int_t axis = 0; axis < Dim; ++axis) {
45 Point sample = Point{center};
46 coordinates_t current_axis_length = std::abs(upper[axis] - lower[axis]);
47 sample[axis] -= current_axis_length * 0.25;
48 samples_host(axis + 1) = Point{sample};
49 sample[axis] += current_axis_length * 0.5;
50 samples_host(Dim + axis + 1) = Point{sample};
51 }
52
53 Kokkos::deep_copy(samples, samples_host);
54
55 Projection project_samples_to_input_predicate{samples};
56 ProjectionCallback project_samples_to_input_callback{};
57
58 /* The returned values are pairs containing the point and its projection on
59 * the source mesh.
60 * Kokkos::pair<point, projection>
61 */
62 using ProjectionRet = Kokkos::pair<Point, Point>;
63 VectorView<ProjectionRet> projected_samples_values(
64 Kokkos::view_alloc(execspace, Kokkos::WithoutInitializing,
65 _region_name + "::projected_samples_values"),
66 0);
67 VectorView<offset_t> projected_samples_offsets(
68 Kokkos::view_alloc(execspace, Kokkos::WithoutInitializing,
69 _region_name + "::projected_samples_offsets"),
70 0);
71
72 this->mSourceBvh.query(execspace, project_samples_to_input_predicate,
73 project_samples_to_input_callback,
74 projected_samples_values, projected_samples_offsets);
75
76 // Each sample matches exactly one projected point
77 Kokkos::parallel_for(
78 _region_name + "::p_for project samples on the source "
79 "mesh",
80 Kokkos::RangePolicy(execspace, 0, 2 * Dim + 1),
81 KOKKOS_LAMBDA(const offset_t &i) {
82 samples(i) =
83 projected_samples_values(projected_samples_offsets(i)).second;
84 });
85 Kokkos::fence();
86
87 DistanceToKNearest vertices_per_sample_predicate{this->mNodesPerCluster,
88 samples};
89 DistanceToKNearestCallback vertices_per_sample_callback{};
90
91 VectorView<fp_t> squared_distances_values(
92 Kokkos::view_alloc(execspace, Kokkos::WithoutInitializing,
93 _region_name + "::squared_distances_values"),
94 0);
95 VectorView<offset_t> squared_distances_offsets(
96 Kokkos::view_alloc(execspace, Kokkos::WithoutInitializing,
97 _region_name + "::squared_distances_offsets"),
98 0);
99 this->mSourceBvh.query(execspace, vertices_per_sample_predicate,
100 vertices_per_sample_callback, squared_distances_values,
101 squared_distances_offsets);
102
103 VectorView<fp_t> max_radii(Kokkos::view_alloc(execspace,
104 Kokkos::WithoutInitializing,
105 _region_name + "::max_radii"),
106 2 * Dim + 1);
107 Kokkos::parallel_for(
108 _region_name + "::p_for sum max radius",
109 Kokkos::RangePolicy(execspace, 0, 2 * Dim + 1),
110 KOKKOS_LAMBDA(const int_t &i) {
111 fp_t n_max = fp_consts::zero();
112 for (offset_t ii = squared_distances_offsets(i);
113 ii < squared_distances_offsets(i + 1); ++ii) {
114 n_max = (n_max > squared_distances_values(ii))
115 ? n_max
116 : squared_distances_values(ii);
117 }
118 max_radii(i) = n_max;
119 });
120 Kokkos::fence();
121 Kokkos::sort(max_radii);
122 auto max_radii_h =
123 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, max_radii);
124 this->mRadius = std::sqrt(max_radii_h(Dim));
125}
126} // namespace RbfPum
127
128} // namespace PACMAN
KOKKOS_INLINE_FUNCTION consteval fp_t zero(void)
Definition types.hpp:92
double fp_t
Definition types.hpp:15
double coordinates_t
Definition types.hpp:16
int32_t offset_t
Definition types.hpp:21
int32_t int_t
Definition types.hpp:18