Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the curve merger algorithm #11955

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ApplicationLibCode/Application/Tools/RiaCurveMerger.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class RiaCurveMerger
interpolatedYValue( const XValueType& xValue, const std::vector<XValueType>& curveXValues, const std::vector<double>& curveYValues );

private:
void computeUnionOfXValues( bool includeValuesFromPartialCurves );
void computeUnionOfXValues( bool includeValuesFromPartialCurves );
static bool isMonotonicallyIncreasing( const std::vector<XValueType>& curveXValues );

private:
std::vector<std::pair<std::vector<XValueType>, std::vector<double>>> m_originalValues;
Expand All @@ -76,6 +77,9 @@ class RiaCurveMerger

std::vector<XValueType> m_allXValues;
std::vector<std::vector<double>> m_interpolatedValuesForAllCurves;

bool m_isXValuesSharedBetweenCurves;
bool m_isXValuesMonotonicallyIncreasing;
};

using RiaTimeHistoryCurveMerger = RiaCurveMerger<time_t>;
Expand Down
85 changes: 73 additions & 12 deletions ApplicationLibCode/Application/Tools/RiaCurveMerger.inl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ bool XValueComparator<XValueType>::equals( const XValueType& lhs, const XValueTy
//--------------------------------------------------------------------------------------------------
template <typename XValueType>
RiaCurveMerger<XValueType>::RiaCurveMerger()
: m_isXValuesSharedBetweenCurves( false )
, m_isXValuesMonotonicallyIncreasing( true )
{
}

Expand All @@ -68,6 +70,24 @@ void RiaCurveMerger<XValueType>::addCurveData( const std::vector<XValueType>& xV

if ( !xValues.empty() )
{
if ( m_originalValues.empty() )
{
m_isXValuesSharedBetweenCurves = true;
m_isXValuesMonotonicallyIncreasing = isMonotonicallyIncreasing( xValues );
}
else
{
if ( m_isXValuesSharedBetweenCurves )
{
const auto& firstXValues = m_originalValues.front().first;
m_isXValuesSharedBetweenCurves = std::equal( firstXValues.begin(), firstXValues.end(), xValues.begin() );
}

if ( m_isXValuesMonotonicallyIncreasing )
{
m_isXValuesMonotonicallyIncreasing = isMonotonicallyIncreasing( xValues );
}
}
m_originalValues.push_back( std::make_pair( xValues, yValues ) );
}
}
Expand Down Expand Up @@ -131,14 +151,14 @@ void RiaCurveMerger<XValueType>::computeInterpolatedValues( bool includeValuesFr
m_allXValues.clear();
m_interpolatedValuesForAllCurves.clear();

computeUnionOfXValues( includeValuesFromPartialCurves );

const size_t curveCount = m_originalValues.size();
if ( curveCount == 0 )
{
return;
}

computeUnionOfXValues( includeValuesFromPartialCurves );

const size_t dataValueCount = m_allXValues.size();
if ( dataValueCount == 0 )
{
Expand All @@ -157,8 +177,18 @@ void RiaCurveMerger<XValueType>::computeInterpolatedValues( bool includeValuesFr
#pragma omp parallel for
for ( int valueIndex = 0; valueIndex < static_cast<int>( dataValueCount ); valueIndex++ )
{
double interpolValue =
interpolatedYValue( m_allXValues[valueIndex], m_originalValues[curveIdx].first, m_originalValues[curveIdx].second );
double interpolValue = 0.0;

if ( m_isXValuesSharedBetweenCurves )
{
interpolValue = m_originalValues[curveIdx].second[valueIndex];
}
else
{
interpolValue =
interpolatedYValue( m_allXValues[valueIndex], m_originalValues[curveIdx].first, m_originalValues[curveIdx].second );
}

if ( !RiaCurveDataTools::isValidValue( interpolValue, false ) )
{
#pragma omp critical
Expand All @@ -180,6 +210,13 @@ void RiaCurveMerger<XValueType>::computeUnionOfXValues( bool includeValuesForPar
{
m_allXValues.clear();

if ( m_isXValuesSharedBetweenCurves && !m_originalValues.empty() )
{
// If all curves have the same X values, use the X values from the first curve.
m_allXValues = m_originalValues.front().first;
return;
}

std::set<XValueType, XComparator> unionOfXValues;

std::vector<std::pair<XValueType, XValueType>> originalXBounds;
Expand Down Expand Up @@ -240,16 +277,26 @@ double RiaCurveMerger<XValueType>::interpolatedYValue( const XValueType&
if ( xValues.empty() ) return HUGE_VAL;
if ( yValues.size() != xValues.size() ) return HUGE_VAL;

const bool removeInterpolatedValues = false;
size_t startIndex = 0;

// Use lower_bound to find the first element that is not less than the interpolation value using a threshold that is larger than the
// threshold used in XComparator::equals
XValueType threshold = 1.0e-6 * xValues.back();
auto it = std::lower_bound( xValues.begin(), xValues.end(), interpolationXValue - threshold );
if ( it == xValues.end() ) return HUGE_VAL;
if ( isMonotonicallyIncreasing( xValues ) )
{
// Use lower_bound to find the first element that is not less than the interpolation value using a threshold that is larger than
// the threshold used in XComparator::equals
//
// Using this method will improve the performance significantly for large datasets, as std::lower_bound is much faster than the
// search in the loop below. One relevant use case is computation of delta summary values for large datasets. Here the time
// steps are specified in increasing order
//
XValueType threshold = 1.0e-6 * xValues.back();
auto it = std::lower_bound( xValues.begin(), xValues.end(), interpolationXValue - threshold );
if ( it == xValues.end() ) return HUGE_VAL;

startIndex = it - xValues.begin();
if ( startIndex > 0 ) startIndex--;
}

size_t startIndex = it - xValues.begin();
if ( startIndex > 0 ) startIndex--;
const bool removeInterpolatedValues = false;

for ( size_t firstI = startIndex; firstI < xValues.size(); firstI++ )
{
Expand Down Expand Up @@ -307,3 +354,17 @@ double RiaCurveMerger<XValueType>::interpolatedYValue( const XValueType&

return HUGE_VAL;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename XValueType>
bool RiaCurveMerger<XValueType>::isMonotonicallyIncreasing( const std::vector<XValueType>& container )
{
return std::adjacent_find( container.begin(),
container.end(),
[]( const auto& a, const auto& b )
{
return b < a; // Returns true if not monotonically increasing
} ) == container.end();
}
24 changes: 24 additions & 0 deletions ApplicationLibCode/UnitTests/RigTimeCurveHistoryMerger-Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ TEST( RiaTimeHistoryCurveMergerTest, NoTimeStepOverlap )
EXPECT_EQ( 0, static_cast<int>( curveMerger.validIntervalsForAllXValues().size() ) );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaTimeHistoryCurveMergerTest, SharedXValues )
{
std::vector<double> valuesA{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
std::vector<double> valuesB{ 10, 20, 30, 40, 50, 60, 70 };
std::vector<time_t> timeSteps{ 1, 2, 3, 4, 5, 6, 7 };

RiaTimeHistoryCurveMerger interpolate;
interpolate.addCurveData( timeSteps, valuesA );
interpolate.addCurveData( timeSteps, valuesB );
interpolate.computeInterpolatedValues( true );

auto interpolatedTimeSteps = interpolate.allXValues();
EXPECT_TRUE( std::equal( timeSteps.begin(), timeSteps.end(), interpolatedTimeSteps.begin() ) );

auto generatedYValuesA = interpolate.interpolatedYValuesForAllXValues( 0 );
EXPECT_TRUE( std::equal( valuesA.begin(), valuesA.end(), generatedYValuesA.begin() ) );

auto generatedYValuesB = interpolate.interpolatedYValuesForAllXValues( 1 );
EXPECT_TRUE( std::equal( valuesB.begin(), valuesB.end(), generatedYValuesB.begin() ) );
}
Loading