Loading web-font TeX/Main/Regular
OGS
MathLib::PiecewiseLinearInterpolation Class Reference

Detailed Description

This class implements a one dimensional piecewise linear interpolation algorithm.

Definition at line 25 of file PiecewiseLinearInterpolation.h.

#include <PiecewiseLinearInterpolation.h>

Inheritance diagram for MathLib::PiecewiseLinearInterpolation:
[legend]

Public Member Functions

 PiecewiseLinearInterpolation (std::vector< double > supporting_points, std::vector< double > values_at_supp_pnts, bool supp_pnts_sorted=false)
 
 PiecewiseLinearInterpolation (std::vector< int > const &supporting_points, std::vector< double > const &values_at_supp_pnts, bool supp_pnts_sorted=false)
 
double getValue (double pnt_to_interpolate) const
 Calculates the interpolation value.
 
double getDerivative (double const pnt_to_interpolate) const
 Calculates derivative using quadratic interpolation and central difference quotient.
 
double getSupportMax () const
 
double getSupportMin () const
 

Protected Attributes

std::vector< double > supp_pnts_
 
std::vector< double > values_at_supp_pnts_
 

Constructor & Destructor Documentation

◆ PiecewiseLinearInterpolation() [1/2]

MathLib::PiecewiseLinearInterpolation::PiecewiseLinearInterpolation ( std::vector< double > supporting_points,
std::vector< double > values_at_supp_pnts,
bool supp_pnts_sorted = false )

The constructor copies the entries of the vector of supporting points (x_0, x_1, \dots, x_n) and the entries of the vector of values at the supporting points (y_0, y_1, \dots, y_n) where n is the number of entries of the vector. The number of supporting points must be the same like the number of values at the supporting points. It is assumed that x_j corresponds to y_j for all j \in [0, n].

It is not assumed that the supporting points are sorted, i.e. x_0 < x_1 < \dots < x_n. It is assumed, that the supporting points are pairwise different. The user can set the flag supp_pnts_sorted to true, if the supporting points are sorted. This will save some setup time.

Parameters
supporting_pointsvector of supporting points
values_at_supp_pntsvector of values at the supporting points
supp_pnts_sortedfalse (default), if it is sure the supporting points are sorted one can set the switch to true

Definition at line 27 of file PiecewiseLinearInterpolation.cpp.

32 : supp_pnts_(std::move(supporting_points)),
33 values_at_supp_pnts_(std::move(values_at_supp_pnts))
34{
35 if (!supp_pnts_sorted)
36 {
38 supp_pnts_, static_cast<std::size_t>(0), supp_pnts_.size(),
40 }
41
42 const auto it = std::adjacent_find(supp_pnts_.begin(), supp_pnts_.end());
43 if (it != supp_pnts_.end())
44 {
45 const std::size_t i = std::distance(supp_pnts_.begin(), it);
47 "Variable {:d} and variable {:d} are the same. Piecewise linear "
48 "interpolation is not possible\n",
49 i, i + 1);
50 }
51}
#define OGS_FATAL(...)
Definition Error.h:26
void quicksort(It1 first1, It1 last1, It2 first2, Comparator compare)
Definition quicksort.h:26

References OGS_FATAL, BaseLib::quicksort(), supp_pnts_, and values_at_supp_pnts_.

◆ PiecewiseLinearInterpolation() [2/2]

MathLib::PiecewiseLinearInterpolation::PiecewiseLinearInterpolation ( std::vector< int > const & supporting_points,
std::vector< double > const & values_at_supp_pnts,
bool supp_pnts_sorted = false )

Definition at line 53 of file PiecewiseLinearInterpolation.cpp.

57 : supp_pnts_(supporting_points | ranges::to<std::vector<double>>()),
58 values_at_supp_pnts_(values_at_supp_pnts)
59{
60 if (!supp_pnts_sorted)
61 {
63 supp_pnts_, static_cast<std::size_t>(0), supp_pnts_.size(),
65 }
66
67 const auto it = std::adjacent_find(supp_pnts_.begin(), supp_pnts_.end());
68 if (it != supp_pnts_.end())
69 {
70 const std::size_t i = std::distance(supp_pnts_.begin(), it);
72 "Variable {:d} and variable {:d} are the same. Piecewise linear "
73 "interpolation is not possible\n",
74 i, i + 1);
75 }
76}

References OGS_FATAL, BaseLib::quicksort(), supp_pnts_, and values_at_supp_pnts_.

Member Function Documentation

◆ getDerivative()

double MathLib::PiecewiseLinearInterpolation::getDerivative ( double const pnt_to_interpolate) const

Calculates derivative using quadratic interpolation and central difference quotient.

Parameters
pnt_to_interpolateThe point should be located within the range [x_{\min}, x_{\max}], where x_{\min} = \min_{1 \le j \le n} x_j and x_{\max} = \max_{1 \le j \le n} x_j. when points are located outside of this interval, the derivative is set to 0.
Attention
if a point is located between the first and second points (or last and second to last point), the derivative is calculated using linear interpolation.

Definition at line 108 of file PiecewiseLinearInterpolation.cpp.

110{
111 if (pnt_to_interpolate < supp_pnts_.front() ||
112 supp_pnts_.back() < pnt_to_interpolate)
113 {
114 return 0;
115 }
116
117 auto const& it(std::lower_bound(supp_pnts_.begin(), supp_pnts_.end(),
118 pnt_to_interpolate));
119 std::size_t interval_idx = std::distance(supp_pnts_.begin(), it);
120
121 if (pnt_to_interpolate == supp_pnts_.front())
122 {
123 interval_idx = 1;
124 }
125
126 if (interval_idx > 1 && interval_idx < supp_pnts_.size() - 2)
127 {
128 // left and right support points.
129 double const x_ll = supp_pnts_[interval_idx - 2];
130 double const x_l = supp_pnts_[interval_idx - 1];
131 double const x = supp_pnts_[interval_idx];
132 double const x_r = supp_pnts_[interval_idx + 1];
133
134 // left and right values.
135 double const f_ll = values_at_supp_pnts_[interval_idx - 2];
136 double const f_l = values_at_supp_pnts_[interval_idx - 1];
137 double const f = values_at_supp_pnts_[interval_idx];
138 double const f_r = values_at_supp_pnts_[interval_idx + 1];
139
140 double const tangent_right = (f_l - f_r) / (x_l - x_r);
141 double const tangent_left = (f_ll - f) / (x_ll - x);
142 double const w = (pnt_to_interpolate - x) / (x_l - x);
143 return (1. - w) * tangent_right + w * tangent_left;
144 }
145
146 return (values_at_supp_pnts_[interval_idx] -
147 values_at_supp_pnts_[interval_idx - 1]) /
148 (supp_pnts_[interval_idx] - supp_pnts_[interval_idx - 1]);
149}

References supp_pnts_, and values_at_supp_pnts_.

Referenced by MaterialPropertyLib::Curve::dValue(), and MathLib::PiecewiseLinearMonotonicCurve::isStrongMonotonic().

◆ getSupportMax()

double MathLib::PiecewiseLinearInterpolation::getSupportMax ( ) const

◆ getSupportMin()

double MathLib::PiecewiseLinearInterpolation::getSupportMin ( ) const

◆ getValue()

double MathLib::PiecewiseLinearInterpolation::getValue ( double pnt_to_interpolate) const

Calculates the interpolation value.

Parameters
pnt_to_interpolateThe point should be located within the range [x_{\min}, x_{\max}], where x_{\min} = \min_{1 \le j \le n} x_j and x_{\max} = \max_{1 \le j \le n} x_j. Points outside of this interval are set to x_{\min} or x_{\max}.
Returns
The interpolated value.

Definition at line 78 of file PiecewiseLinearInterpolation.cpp.

79{
80 // search interval that has the point inside
81 if (pnt_to_interpolate <= supp_pnts_.front())
82 {
83 return values_at_supp_pnts_[0];
84 }
85
86 if (supp_pnts_.back() <= pnt_to_interpolate)
87 {
88 return values_at_supp_pnts_[supp_pnts_.size() - 1];
89 }
90
91 auto const& it(std::lower_bound(supp_pnts_.begin(), supp_pnts_.end(),
92 pnt_to_interpolate));
93 std::size_t const interval_idx = std::distance(supp_pnts_.begin(), it) - 1;
94
95 // support points.
96 double const x = supp_pnts_[interval_idx];
97 double const x_r = supp_pnts_[interval_idx + 1];
98
99 // values.
100 double const f = values_at_supp_pnts_[interval_idx];
101 double const f_r = values_at_supp_pnts_[interval_idx + 1];
102
103 // compute linear interpolation polynom: y = m * (x - support[i]) + value[i]
104 const double t = (pnt_to_interpolate - x) / (x_r - x);
105 return std::lerp(f, f_r, t);
106}
static const double t

References supp_pnts_, MathLib::t, and values_at_supp_pnts_.

Referenced by ProcessLib::DeactivatedSubdomain::isDeactivated(), MaterialPropertyLib::CurveWrapper::operator()(), ParameterLib::CurveScaledParameter< T >::operator()(), ParameterLib::FunctionParameter< T >::CurveWrapper::operator()(), ProcessLib::HeatTransportBHE::BHE::BuildingPowerCurveConstantFlow::operator()(), ProcessLib::HeatTransportBHE::BHE::FixedPowerFlowCurve::operator()(), ProcessLib::HeatTransportBHE::BHE::PowerCurveConstantFlow::operator()(), ProcessLib::HeatTransportBHE::BHE::PowerCurveFlowCurve::operator()(), ProcessLib::HeatTransportBHE::BHE::TemperatureCurveConstantFlow::operator()(), ProcessLib::HeatTransportBHE::BHE::TemperatureCurveFlowCurve::operator()(), and MaterialPropertyLib::Curve::value().

Member Data Documentation

◆ supp_pnts_

◆ values_at_supp_pnts_

std::vector<double> MathLib::PiecewiseLinearInterpolation::values_at_supp_pnts_
protected

The documentation for this class was generated from the following files: