mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Interpolation: Fix "trend" extrapolation for value outside of range on high end.
This commit is contained in:
@@ -103,7 +103,22 @@ double RiaInterpolationTools::linear( const std::vector<double>& x, const std::v
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaInterpolationTools::extrapolate( const std::vector<double>& x, const std::vector<double>& y, double value )
|
||||
{
|
||||
return y[0] + ( value - x[0] ) / ( x[1] - x[0] ) * ( y[1] - y[0] );
|
||||
if ( value <= x[0] )
|
||||
return extrapolate( x[0], y[0], x[1], y[1], value );
|
||||
else
|
||||
{
|
||||
size_t lastIndex = x.size() - 1;
|
||||
size_t secondLastIndex = x.size() - 2;
|
||||
return extrapolate( x[secondLastIndex], y[secondLastIndex], x[lastIndex], y[lastIndex], value );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaInterpolationTools::extrapolate( double x0, double y0, double x1, double y1, double value )
|
||||
{
|
||||
return y0 + ( value - x0 ) / ( x1 - x0 ) * ( y1 - y0 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -47,5 +47,7 @@ private:
|
||||
static int findNextDataPoint( const std::vector<double>& values, int index );
|
||||
static int findPreviousDataPoint( const std::vector<double>& values, int index );
|
||||
static double extrapolate( const std::vector<double>& x, const std::vector<double>& y, double value );
|
||||
static double extrapolate( double x0, double y0, double x1, double y1, double value );
|
||||
|
||||
static double extrapolateClosestValue( const std::vector<double>& x, const std::vector<double>& y, double value );
|
||||
};
|
||||
|
||||
@@ -117,6 +117,19 @@ TEST( RiaInterpolationToolsTest, ValidIntervalValueTooLowExtrapolationTrend )
|
||||
EXPECT_DOUBLE_EQ( -2.0, res );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RiaInterpolationToolsTest, ValidIntervalValueTooHighExtrapolationTrend )
|
||||
{
|
||||
std::vector<double> x = { 0.0, 1.0, 2.0, 3.0 };
|
||||
std::vector<double> y = { 0.0, 1.0, 10.0, 20.0 };
|
||||
|
||||
// Outside interval on low side
|
||||
double res = RiaInterpolationTools::linear( x, y, 4.0, RiaInterpolationTools::ExtrapolationMode::TREND );
|
||||
EXPECT_DOUBLE_EQ( 30.0, res );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user