1#ifndef LINEAR_INTERPOLATION_H
2#define LINEAR_INTERPOLATION_H
6#include "AMReX_Extension.H"
8using namespace amrex::literals;
24template <
typename It,
typename T>
25AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Index
28 const int sz =
static_cast<int>(end - begin);
30 if ((sz < 2) || (x < *begin)) {
33 if (x > *(begin + (sz - 1))) {
40template <
typename It,
typename T>
41AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
53 while ((ir - il) > 1) {
54 int mid = (il + ir) >> 1;
55 const T xmid = *(begin + mid);
57 if ((x - xmid) * (x - xl) <= 0.0_rt) {
67template <
typename It,
typename T>
68AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
77 if ((x - begin[idx.idx]) > (begin[idx.idx + 1] - x)) {
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)
93 for (It it = (begin + hint); it < end; ++it) {
95 idx.idx = it - begin - 1;
102template <
typename C1,
typename C2>
103AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
104 typename std::iterator_traits<C2>::value_type
108 const typename std::iterator_traits<C1>::value_type& xout,
113 using DType1 =
typename std::iterator_traits<C1>::value_type;
116 return yinp[(ncomp * idx.
idx) + comp];
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]) +
124 (facR * yinp[(ncomp * (j + 1)) + comp]);
127template <
typename C1,
typename C2>
128AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
129 typename std::iterator_traits<C2>::value_type
134 const typename std::iterator_traits<C1>::value_type& xout,
139 return linear_impl(xbegin, yinp, xout, idx, ncomp, comp);
142template <
typename C1,
typename C2>
146 const typename C1::value_type& xout,
151 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout, ncomp,
155template <
typename C1,
typename C2>
164 static constexpr typename C1::value_type eps =
165 std::numeric_limits<float>::epsilon();
166 AMREX_ASSERT(xinp.size() == yinp.size());
167 AMREX_ASSERT(xout.size() == yout.size());
170 int npts = xout.size();
171 for (
int i = 0; i < npts; ++i) {
172 const auto& x = xout[i];
174 find_index(xinp.data(), (xinp.data() + xinp.size()), x, hint);
177 yout[i] = yinp[(ncomp * idx.idx) + comp];
180 const auto denom = (xinp[j + 1] - xinp[j]);
181 const auto facR = (denom > eps) ? ((x - xinp[j]) / denom) : 1.0_rt;
183 static_cast<typename C1::value_type
>(1.0_rt) - facR;
184 yout[i] = (facL * yinp[(ncomp * j) + comp]) +
185 (facR * yinp[(ncomp * (j + 1)) + comp]);
192template <
typename C1,
typename C2>
202 static_cast<amrex::Long
>(xinp.size()) ==
203 static_cast<amrex::Long
>(yinp.size()));
205 static_cast<amrex::Long
>(xout.size()) ==
206 static_cast<amrex::Long
>(yout.size()));
208 int npts = xout.size();
209 for (
int i = 0; i < npts; ++i) {
210 yout[i] =
linear(xinp, yinp, xout[i], ncomp, comp);
216template <
typename C1,
typename C2>
217AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
218 typename std::iterator_traits<C2>::value_type
223 const typename std::iterator_traits<C1>::value_type& xout,
224 const typename std::iterator_traits<C1>::value_type& upper_bound)
226 using DType1 =
typename std::iterator_traits<C1>::value_type;
230 return yinp[idx.idx];
232 static constexpr DType1 eps = std::numeric_limits<float>::epsilon();
233 const int j = idx.idx;
234 const auto denom = (xbegin[j + 1] - xbegin[j]);
237 const auto R = (denom > eps) ? ((xout - xbegin[j]) / denom) : 0.0_rt;
238 const auto ub = upper_bound;
239 const auto hb = 0.5_rt * upper_bound;
240 const auto ohb = 1.5_rt * upper_bound;
242 ((std::fmod((std::fmod(yinp[j + 1] - yinp[j], ub) + ohb), ub) - hb) *
246template <
typename C1,
typename C2>
250 const typename C1::value_type& xout,
251 const typename C1::value_type& upper_bound)
254 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout,
258template <
typename C1,
typename C2>
264 const typename C1::value_type& upper_bound)
267 static_cast<amrex::Long
>(xinp.size()) ==
268 static_cast<amrex::Long
>(yinp.size()));
270 static_cast<amrex::Long
>(xout.size()) ==
271 static_cast<amrex::Long
>(yout.size()));
273 int npts = xout.size();
274 for (
int i = 0; i < npts; ++i) {
275 yout[i] =
linear_angle(xinp, yinp, xout[i], upper_bound);
279template <
typename C1,
typename C2>
280AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
281 typename std::iterator_traits<C2>::value_type
287 const typename std::iterator_traits<C1>::value_type& xout,
288 const typename std::iterator_traits<C1>::value_type& yout,
292 using DType1 =
typename std::iterator_traits<C1>::value_type;
296 return zinp[(xidx.
idx * ny) + yidx.
idx];
299 const int i = xidx.
idx;
300 const int j = yidx.
idx;
302 static constexpr DType1 eps = std::numeric_limits<float>::epsilon();
303 const auto xdenom = (xbegin[i + 1] - xbegin[i]);
304 const auto xfacR = (xdenom > eps) ? ((xout - xbegin[i]) / xdenom) : 1.0_rt;
305 const auto xfacL =
static_cast<DType1
>(1.0_rt) - xfacR;
306 const auto ydenom = (ybegin[j + 1] - ybegin[j]);
307 const auto yfacR = (ydenom > eps) ? ((yout - ybegin[j]) / ydenom) : 1.0_rt;
308 const auto yfacL =
static_cast<DType1
>(1.0_rt) - yfacR;
310 const auto z00 = zinp[(i * ny) + j];
311 const auto z10 = zinp[((i + 1) * ny) + j];
312 const auto z01 = zinp[(i * ny) + (j + 1)];
313 const auto z11 = zinp[((i + 1) * ny) + (j + 1)];
315 return (z00 * xfacL * yfacL) + (z10 * xfacR * yfacL) +
316 (z01 * xfacL * yfacR) + (z11 * xfacR * yfacR);
319template <
typename C1,
typename C2>
320AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
321 typename std::iterator_traits<C2>::value_type
328 const typename std::iterator_traits<C1>::value_type& xout,
329 const typename std::iterator_traits<C1>::value_type& yout)
334 xbegin, ybegin,
static_cast<int>(yend - ybegin), zinp, xout, yout, xidx,
338template <
typename C1,
typename C2>
343 const typename C1::value_type& xout,
344 const typename C1::value_type& yout)
346 AMREX_ALWAYS_ASSERT((xinp.size() * yinp.size()) == zinp.size());
348 xinp.data(), (xinp.data() + xinp.size()), yinp.data(),
349 (yinp.data() + yinp.size()), zinp.data(), xout, yout);
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:219
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:322
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:156
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:130
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:282
Definition linear_interpolation.H:19
Limits lim
Definition linear_interpolation.H:21
int idx
Definition linear_interpolation.H:20