1#ifndef LINEAR_INTERPOLATION_H
2#define LINEAR_INTERPOLATION_H
5#include <AMReX_Extension.H>
21template <
typename It,
typename T>
22AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Index
25 const int sz =
static_cast<int>(end - begin);
27 if ((sz < 2) || (x < *begin)) {
30 if (x > *(begin + (sz - 1))) {
37template <
typename It,
typename T>
38AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
50 while ((ir - il) > 1) {
51 int mid = (il + ir) >> 1;
52 const T xmid = *(begin + mid);
54 if ((x - xmid) * (x - xl) <= 0.0) {
64template <
typename It,
typename T>
65AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
74 if ((x - begin[idx.idx]) > (begin[idx.idx + 1] - x)) {
81template <
typename It,
typename T>
82AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index
83find_index(
const It begin,
const It end,
const T& x,
const int hint = 1)
90 for (It it = (begin + hint); it < end; ++it) {
92 idx.idx = it - begin - 1;
99template <
typename C1,
typename C2>
100AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
101 typename std::iterator_traits<C2>::value_type
105 const typename std::iterator_traits<C1>::value_type& xout,
110 using DType1 =
typename std::iterator_traits<C1>::value_type;
113 return yinp[ncomp * idx.
idx + comp];
115 static constexpr DType1 eps = 1.0e-8;
116 const int j = idx.
idx;
117 const auto denom = (xbegin[j + 1] - xbegin[j]);
118 const auto facR = (denom > eps) ? ((xout - xbegin[j]) / denom) : 1.0;
119 const auto facL =
static_cast<DType1
>(1.0) - facR;
120 return facL * yinp[ncomp * j + comp] + facR * yinp[ncomp * (j + 1) + comp];
123template <
typename C1,
typename C2>
124AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
125 typename std::iterator_traits<C2>::value_type
130 const typename std::iterator_traits<C1>::value_type& xout,
135 return linear_impl(xbegin, yinp, xout, idx, ncomp, comp);
138template <
typename C1,
typename C2>
142 const typename C1::value_type& xout,
147 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout, ncomp,
151template <
typename C1,
typename C2>
160 static constexpr typename C1::value_type eps = 1.0e-8;
161 AMREX_ASSERT(xinp.size() == yinp.size());
162 AMREX_ASSERT(xout.size() == yout.size());
165 int npts = xout.size();
166 for (
int i = 0; i < npts; ++i) {
167 const auto& x = xout[i];
169 find_index(xinp.data(), (xinp.data() + xinp.size()), x, hint);
172 yout[i] = yinp[ncomp * idx.idx + comp];
175 const auto denom = (xinp[j + 1] - xinp[j]);
176 const auto facR = (denom > eps) ? ((x - xinp[j]) / denom) : 1.0;
177 const auto facL =
static_cast<typename C1::value_type
>(1.0) - facR;
178 yout[i] = facL * yinp[ncomp * j + comp] +
179 facR * yinp[ncomp * (j + 1) + comp];
186template <
typename C1,
typename C2>
196 static_cast<amrex::Long
>(xinp.size()) ==
197 static_cast<amrex::Long
>(yinp.size()));
199 static_cast<amrex::Long
>(xout.size()) ==
200 static_cast<amrex::Long
>(yout.size()));
202 int npts = xout.size();
203 for (
int i = 0; i < npts; ++i) {
204 yout[i] =
linear(xinp, yinp, xout[i], ncomp, comp);
210template <
typename C1,
typename C2>
211AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
212 typename std::iterator_traits<C2>::value_type
217 const typename std::iterator_traits<C1>::value_type& xout,
218 const typename std::iterator_traits<C1>::value_type& upper_bound)
220 using DType1 =
typename std::iterator_traits<C1>::value_type;
224 return yinp[idx.idx];
226 static constexpr DType1 eps = 1.0e-8;
227 const int j = idx.idx;
228 const auto denom = (xbegin[j + 1] - xbegin[j]);
231 const auto R = (denom > eps) ? ((xout - xbegin[j]) / denom) : 0.0;
232 const auto ub = upper_bound;
233 const auto hb = 0.5 * upper_bound;
234 const auto ohb = 1.5 * upper_bound;
236 (std::fmod((std::fmod(yinp[j + 1] - yinp[j], ub) + ohb), ub) - hb) *
240template <
typename C1,
typename C2>
244 const typename C1::value_type& xout,
245 const typename C1::value_type& upper_bound)
248 xinp.data(), (xinp.data() + xinp.size()), yinp.data(), xout,
252template <
typename C1,
typename C2>
258 const typename C1::value_type& upper_bound)
261 static_cast<amrex::Long
>(xinp.size()) ==
262 static_cast<amrex::Long
>(yinp.size()));
264 static_cast<amrex::Long
>(xout.size()) ==
265 static_cast<amrex::Long
>(yout.size()));
267 int npts = xout.size();
268 for (
int i = 0; i < npts; ++i) {
269 yout[i] =
linear_angle(xinp, yinp, xout[i], upper_bound);
273template <
typename C1,
typename C2>
274AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
275 typename std::iterator_traits<C2>::value_type
281 const typename std::iterator_traits<C1>::value_type& xout,
282 const typename std::iterator_traits<C1>::value_type& yout,
286 using DType1 =
typename std::iterator_traits<C1>::value_type;
290 return zinp[xidx.
idx * ny + yidx.
idx];
293 const int i = xidx.
idx;
294 const int j = yidx.
idx;
296 static constexpr DType1 eps = 1.0e-8;
297 const auto xdenom = (xbegin[i + 1] - xbegin[i]);
298 const auto xfacR = (xdenom > eps) ? ((xout - xbegin[i]) / xdenom) : 1.0;
299 const auto xfacL =
static_cast<DType1
>(1.0) - xfacR;
300 const auto ydenom = (ybegin[j + 1] - ybegin[j]);
301 const auto yfacR = (ydenom > eps) ? ((yout - ybegin[j]) / ydenom) : 1.0;
302 const auto yfacL =
static_cast<DType1
>(1.0) - yfacR;
304 const auto z00 = zinp[i * ny + j];
305 const auto z10 = zinp[(i + 1) * ny + j];
306 const auto z01 = zinp[i * ny + (j + 1)];
307 const auto z11 = zinp[(i + 1) * ny + (j + 1)];
309 return z00 * xfacL * yfacL + z10 * xfacR * yfacL + z01 * xfacL * yfacR +
313template <
typename C1,
typename C2>
314AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
315 typename std::iterator_traits<C2>::value_type
322 const typename std::iterator_traits<C1>::value_type& xout,
323 const typename std::iterator_traits<C1>::value_type& yout)
328 xbegin, ybegin,
static_cast<int>(yend - ybegin), zinp, xout, yout, xidx,
332template <
typename C1,
typename C2>
337 const typename C1::value_type& xout,
338 const typename C1::value_type& yout)
340 AMREX_ALWAYS_ASSERT((xinp.size() * yinp.size()) == zinp.size());
342 xinp.data(), (xinp.data() + xinp.size()), yinp.data(),
343 (yinp.data() + yinp.size()), zinp.data(), xout, yout);
Definition linear_interpolation.H:7
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:83
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index check_bounds(const It begin, const It end, const T &x)
Definition linear_interpolation.H:23
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index bisection_search(const It begin, const It end, const T &x)
Definition linear_interpolation.H:39
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:213
Limits
Definition linear_interpolation.H:9
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:316
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:152
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Index nearest_search(const It begin, const It end, const T &x)
Definition linear_interpolation.H:66
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:126
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:102
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:276
Definition linear_interpolation.H:16
Limits lim
Definition linear_interpolation.H:18
int idx
Definition linear_interpolation.H:17