/home/runner/work/amr-wind/amr-wind/amr-wind/utilities/linear_interpolation.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/utilities/linear_interpolation.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
linear_interpolation.H
Go to the documentation of this file.
1#ifndef LINEAR_INTERPOLATION_H
2#define LINEAR_INTERPOLATION_H
3
4#include "AMReX_Gpu.H"
5#include "AMReX_REAL.H"
6#include "AMReX_Extension.H"
7
8using namespace amrex::literals;
9
11
12enum class Limits : int {
13 LOWLIM = -2,
14 UPLIM = -1,
15 VALID = 0,
16};
17
18struct Index
19{
20 int idx;
22};
23
24template <typename It, typename T>
25AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
26check_bounds(const It begin, const It end, const T& x)
27{
28 const int sz = static_cast<int>(end - begin);
29
30 if ((sz < 2) || (x < *begin)) {
31 return Index{0, Limits::LOWLIM};
32 }
33 if (x > *(begin + (sz - 1))) {
34 return Index{sz - 1, Limits::UPLIM};
35 }
36
37 return Index{0, Limits::VALID};
38}
39
40template <typename It, typename T>
41AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
42bisection_search(const It begin, const It end, const T& x)
43{
44 auto idx = check_bounds(begin, end, x);
45 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
46 return idx;
47 }
48
49 int il = 0;
50 int ir = end - begin;
51 const T xl = *begin;
52
53 while ((ir - il) > 1) {
54 int mid = (il + ir) >> 1;
55 const T xmid = *(begin + mid);
56
57 if ((x - xmid) * (x - xl) <= 0.0_rt) {
58 ir = mid;
59 } else {
60 il = mid;
61 }
62 }
63 idx.idx = il;
64 return idx;
65}
66
67template <typename It, typename T>
68AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
69nearest_search(const It begin, const It end, const T& x)
70{
71
72 auto idx = bisection_search(begin, end, x);
73 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
74 return idx;
75 }
76
77 if ((x - begin[idx.idx]) > (begin[idx.idx + 1] - x)) {
78 idx.idx += 1;
79 }
80
81 return idx;
82}
83
84template <typename It, typename T>
85AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
86find_index(const It begin, const It end, const T& x, const int hint = 1)
87{
88 auto idx = check_bounds(begin, end, x);
89 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
90 return idx;
91 }
92
93 for (It it = (begin + hint); it < end; ++it) {
94 if (x <= *it) {
95 idx.idx = it - begin - 1;
96 break;
97 }
98 }
99 return idx;
100}
101
102template <typename C1, typename C2>
103AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
104 typename std::iterator_traits<C2>::value_type
106 const C1 xbegin,
107 const C2 yinp,
108 const typename std::iterator_traits<C1>::value_type& xout,
109 const Index& idx,
110 const int ncomp = 1,
111 const int comp = 0)
112{
113 using DType1 = typename std::iterator_traits<C1>::value_type;
114
115 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
116 return yinp[ncomp * idx.idx + comp];
117 }
118 static constexpr DType1 eps = std::numeric_limits<float>::epsilon();
119 const int j = idx.idx;
120 const auto denom = (xbegin[j + 1] - xbegin[j]);
121 const auto facR = (denom > eps) ? ((xout - xbegin[j]) / denom) : 1.0_rt;
122 const auto facL = static_cast<DType1>(1.0_rt) - facR;
123 return facL * yinp[ncomp * j + comp] + facR * yinp[ncomp * (j + 1) + comp];
124}
125
126template <typename C1, typename C2>
127AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
128 typename std::iterator_traits<C2>::value_type
130 const C1 xbegin,
131 const C1 xend,
132 const C2 yinp,
133 const typename std::iterator_traits<C1>::value_type& xout,
134 const int ncomp = 1,
135 const int comp = 0)
136{
137 const auto idx = bisection_search(xbegin, xend, xout);
138 return linear_impl(xbegin, yinp, xout, idx, ncomp, comp);
139}
140
141template <typename C1, typename C2>
142inline typename C2::value_type linear(
143 const C1& xinp,
144 const C2& yinp,
145 const typename C1::value_type& xout,
146 const int ncomp = 1,
147 const int comp = 0)
148{
149 return linear(
150 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout, ncomp,
151 comp);
152}
153
154template <typename C1, typename C2>
156 const C1& xinp,
157 const C2& yinp,
158 const C1& xout,
159 C2& yout,
160 const int ncomp = 1,
161 const int comp = 0)
162{
163 static constexpr typename C1::value_type eps =
164 std::numeric_limits<float>::epsilon();
165 AMREX_ASSERT(xinp.size() == yinp.size());
166 AMREX_ASSERT(xout.size() == yout.size());
167
168 int hint = 1;
169 int npts = xout.size();
170 for (int i = 0; i < npts; ++i) {
171 const auto& x = xout[i];
172 const auto idx =
173 find_index(xinp.data(), (xinp.data() + xinp.size()), x, hint);
174
175 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
176 yout[i] = yinp[ncomp * idx.idx + comp];
177 } else if (idx.lim == Limits::VALID) {
178 int j = idx.idx;
179 const auto denom = (xinp[j + 1] - xinp[j]);
180 const auto facR = (denom > eps) ? ((x - xinp[j]) / denom) : 1.0_rt;
181 const auto facL =
182 static_cast<typename C1::value_type>(1.0_rt) - facR;
183 yout[i] = facL * yinp[ncomp * j + comp] +
184 facR * yinp[ncomp * (j + 1) + comp];
185 ;
186 }
187 hint = idx.idx + 1;
188 }
189}
190
191template <typename C1, typename C2>
192inline void linear(
193 const C1& xinp,
194 const C2& yinp,
195 const C1& xout,
196 C2& yout,
197 const int ncomp = 1,
198 const int comp = 0)
199{
200 AMREX_ASSERT(
201 static_cast<amrex::Long>(xinp.size()) ==
202 static_cast<amrex::Long>(yinp.size()));
203 AMREX_ASSERT(
204 static_cast<amrex::Long>(xout.size()) ==
205 static_cast<amrex::Long>(yout.size()));
206
207 int npts = xout.size();
208 for (int i = 0; i < npts; ++i) {
209 yout[i] = linear(xinp, yinp, xout[i], ncomp, comp);
210 }
211}
212
213// Angles in yinp should be defined between [-upper_bound, +upper_bound]
214// Use cases would be degrees (upper_bound =360) and radians (upper_bound =2pi)
215template <typename C1, typename C2>
216AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
217 typename std::iterator_traits<C2>::value_type
219 const C1 xbegin,
220 const C1 xend,
221 const C2 yinp,
222 const typename std::iterator_traits<C1>::value_type& xout,
223 const typename std::iterator_traits<C1>::value_type& upper_bound)
224{
225 using DType1 = typename std::iterator_traits<C1>::value_type;
226 const auto idx = bisection_search(xbegin, xend, xout);
227
228 if ((idx.lim == Limits::LOWLIM) || (idx.lim == Limits::UPLIM)) {
229 return yinp[idx.idx];
230 }
231 static constexpr DType1 eps = std::numeric_limits<float>::epsilon();
232 const int j = idx.idx;
233 const auto denom = (xbegin[j + 1] - xbegin[j]);
234 /* https://math.stackexchange.com/questions/2144234/interpolating-between-2-angles
235 */
236 const auto R = (denom > eps) ? ((xout - xbegin[j]) / denom) : 0.0_rt;
237 const auto ub = upper_bound;
238 const auto hb = 0.5_rt * upper_bound;
239 const auto ohb = 1.5_rt * upper_bound;
240 return yinp[j] +
241 (std::fmod((std::fmod(yinp[j + 1] - yinp[j], ub) + ohb), ub) - hb) *
242 R;
243}
244
245template <typename C1, typename C2>
246inline typename C2::value_type linear_angle(
247 const C1& xinp,
248 const C2& yinp,
249 const typename C1::value_type& xout,
250 const typename C1::value_type& upper_bound)
251{
252 return linear_angle(
253 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout,
254 upper_bound);
255}
256
257template <typename C1, typename C2>
258inline void linear_angle(
259 const C1& xinp,
260 const C2& yinp,
261 const C1& xout,
262 C2& yout,
263 const typename C1::value_type& upper_bound)
264{
265 AMREX_ASSERT(
266 static_cast<amrex::Long>(xinp.size()) ==
267 static_cast<amrex::Long>(yinp.size()));
268 AMREX_ASSERT(
269 static_cast<amrex::Long>(xout.size()) ==
270 static_cast<amrex::Long>(yout.size()));
271
272 int npts = xout.size();
273 for (int i = 0; i < npts; ++i) {
274 yout[i] = linear_angle(xinp, yinp, xout[i], upper_bound);
275 }
276}
277
278template <typename C1, typename C2>
279AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
280 typename std::iterator_traits<C2>::value_type
282 const C1 xbegin,
283 const C1 ybegin,
284 const int ny,
285 const C2 zinp,
286 const typename std::iterator_traits<C1>::value_type& xout,
287 const typename std::iterator_traits<C1>::value_type& yout,
288 const Index& xidx,
289 const Index& yidx)
290{
291 using DType1 = typename std::iterator_traits<C1>::value_type;
292
293 if ((xidx.lim == Limits::LOWLIM) || (xidx.lim == Limits::UPLIM) ||
294 (yidx.lim == Limits::LOWLIM) || (yidx.lim == Limits::UPLIM)) {
295 return zinp[xidx.idx * ny + yidx.idx];
296 }
297
298 const int i = xidx.idx;
299 const int j = yidx.idx;
300
301 static constexpr DType1 eps = std::numeric_limits<float>::epsilon();
302 const auto xdenom = (xbegin[i + 1] - xbegin[i]);
303 const auto xfacR = (xdenom > eps) ? ((xout - xbegin[i]) / xdenom) : 1.0_rt;
304 const auto xfacL = static_cast<DType1>(1.0_rt) - xfacR;
305 const auto ydenom = (ybegin[j + 1] - ybegin[j]);
306 const auto yfacR = (ydenom > eps) ? ((yout - ybegin[j]) / ydenom) : 1.0_rt;
307 const auto yfacL = static_cast<DType1>(1.0_rt) - yfacR;
308
309 const auto z00 = zinp[i * ny + j];
310 const auto z10 = zinp[(i + 1) * ny + j];
311 const auto z01 = zinp[i * ny + (j + 1)];
312 const auto z11 = zinp[(i + 1) * ny + (j + 1)];
313
314 return z00 * xfacL * yfacL + z10 * xfacR * yfacL + z01 * xfacL * yfacR +
315 z11 * xfacR * yfacR;
316}
317
318template <typename C1, typename C2>
319AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
320 typename std::iterator_traits<C2>::value_type
322 const C1 xbegin,
323 const C1 xend,
324 const C1 ybegin,
325 const C1 yend,
326 const C2 zinp,
327 const typename std::iterator_traits<C1>::value_type& xout,
328 const typename std::iterator_traits<C1>::value_type& yout)
329{
330 const auto xidx = bisection_search(xbegin, xend, xout);
331 const auto yidx = bisection_search(ybegin, yend, yout);
332 return bilinear_impl(
333 xbegin, ybegin, static_cast<int>(yend - ybegin), zinp, xout, yout, xidx,
334 yidx);
335}
336
337template <typename C1, typename C2>
338inline typename C2::value_type bilinear(
339 const C1& xinp,
340 const C1& yinp,
341 const C2& zinp,
342 const typename C1::value_type& xout,
343 const typename C1::value_type& yout)
344{
345 AMREX_ALWAYS_ASSERT((xinp.size() * yinp.size()) == zinp.size());
346 return bilinear(
347 xinp.data(), (xinp.data() + xinp.size()), yinp.data(),
348 (yinp.data() + yinp.size()), zinp.data(), xout, yout);
349}
350
351} // namespace amr_wind::interp
352
353#endif /* LINEAR_INTERPOLATION_H */
Definition linear_interpolation.H:10
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index find_index(const It begin, const It end, const T &x, const int hint=1)
Definition linear_interpolation.H:86
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index check_bounds(const It begin, const It end, const T &x)
Definition linear_interpolation.H:26
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index bisection_search(const It begin, const It end, const T &x)
Definition linear_interpolation.H:42
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::iterator_traits< C2 >::value_type linear_angle(const C1 xbegin, const C1 xend, const C2 yinp, const typename std::iterator_traits< C1 >::value_type &xout, const typename std::iterator_traits< C1 >::value_type &upper_bound)
Definition linear_interpolation.H:218
Limits
Definition linear_interpolation.H:12
@ UPLIM
Definition linear_interpolation.H:14
@ VALID
Definition linear_interpolation.H:15
@ LOWLIM
Definition linear_interpolation.H:13
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::iterator_traits< C2 >::value_type bilinear(const C1 xbegin, const C1 xend, const C1 ybegin, const C1 yend, const C2 zinp, const typename std::iterator_traits< C1 >::value_type &xout, const typename std::iterator_traits< C1 >::value_type &yout)
Definition linear_interpolation.H:321
void linear_monotonic(const C1 &xinp, const C2 &yinp, const C1 &xout, C2 &yout, const int ncomp=1, const int comp=0)
Definition linear_interpolation.H:155
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index nearest_search(const It begin, const It end, const T &x)
Definition linear_interpolation.H:69
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::iterator_traits< C2 >::value_type linear(const C1 xbegin, const C1 xend, const C2 yinp, const typename std::iterator_traits< C1 >::value_type &xout, const int ncomp=1, const int comp=0)
Definition linear_interpolation.H:129
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::iterator_traits< C2 >::value_type linear_impl(const C1 xbegin, const C2 yinp, const typename std::iterator_traits< C1 >::value_type &xout, const Index &idx, const int ncomp=1, const int comp=0)
Definition linear_interpolation.H:105
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE std::iterator_traits< C2 >::value_type bilinear_impl(const C1 xbegin, const C1 ybegin, const int ny, const C2 zinp, const typename std::iterator_traits< C1 >::value_type &xout, const typename std::iterator_traits< C1 >::value_type &yout, const Index &xidx, const Index &yidx)
Definition linear_interpolation.H:281
Definition linear_interpolation.H:19
Limits lim
Definition linear_interpolation.H:21
int idx
Definition linear_interpolation.H:20