Loading...
Searching...
No Matches
interpolation.hpp
Go to the documentation of this file.
1
2#pragma once
3#include "Kokkos_Macros.hpp"
4
5namespace terra::util {
6
7/// @brief Computes the linearly interpolated value at a specified point using two surrounding data points.
8///
9/// @param pos_a coordinate of the first data point
10/// @param pos_b coordinate of the first data point
11/// @param val_a value at pos_a
12/// @param val_b value at pos_b
13/// @param pos where to evaluate
14/// @param clamp if true, clamps to [val_a, val_b]
15/// @return The interpolated value at the specified x-coordinate.
16KOKKOS_INLINE_FUNCTION
18 const double pos_a,
19 const double pos_b,
20 const double val_a,
21 const double val_b,
22 const double pos,
23 const bool clamp )
24{
25 const double dx = pos_b - pos_a;
26
27 // Degenerate interval
28 if ( dx == 0.0 )
29 {
30 return val_a;
31 }
32
33 const double t = ( pos - pos_a ) / dx;
34
35 if ( clamp )
36 {
37 if ( t <= 0.0 )
38 {
39 return val_a;
40 }
41
42 if ( t >= 1.0 )
43 {
44 return val_b;
45 }
46 }
47
48 return val_a + t * ( val_b - val_a );
49}
50
51} // namespace terra::util
Definition solver.hpp:9
double interpolate_linear_1D(const double pos_a, const double pos_b, const double val_a, const double val_b, const double pos, const bool clamp)
Computes the linearly interpolated value at a specified point using two surrounding data points.
Definition interpolation.hpp:17