mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#8955 Grid Property Calculator : Add filtering support.
This commit is contained in:
@@ -19,16 +19,20 @@
|
||||
#include "RimGridCalculation.h"
|
||||
|
||||
#include "RiaDefines.h"
|
||||
#include "RimEclipseCase.h"
|
||||
#include "RimEclipseCellColors.h"
|
||||
#include "RimEclipseView.h"
|
||||
#include "RimReloadCaseTools.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
#include "RiaPorosityModel.h"
|
||||
|
||||
#include "RimEclipseCase.h"
|
||||
#include "RimEclipseCellColors.h"
|
||||
#include "RimEclipseView.h"
|
||||
#include "RimGridCalculationVariable.h"
|
||||
#include "RimReloadCaseTools.h"
|
||||
|
||||
#include "RigCaseCellResultsData.h"
|
||||
#include "RigEclipseResultAddress.h"
|
||||
#include "RigMainGrid.h"
|
||||
#include "RigResultAccessor.h"
|
||||
#include "RigResultAccessorFactory.h"
|
||||
|
||||
#include "expressionparser/ExpressionParser.h"
|
||||
|
||||
@@ -57,78 +61,24 @@ bool RimGridCalculation::calculate()
|
||||
{
|
||||
QString leftHandSideVariableName = RimGridCalculation::findLeftHandSide( m_expression );
|
||||
|
||||
auto porosityModel = RiaDefines::PorosityModelType::MATRIX_MODEL;
|
||||
|
||||
RimEclipseCase* eclipseCase = findEclipseCaseFromVariables();
|
||||
if ( !eclipseCase )
|
||||
{
|
||||
RiaLogging::errorInMessageBox( nullptr,
|
||||
"Expression Parser",
|
||||
"Grid Property Calculator",
|
||||
QString( "No case found for calculation : %1" ).arg( leftHandSideVariableName ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t timeStepCount = eclipseCase->results( porosityModel )->maxTimeStepCount();
|
||||
|
||||
std::vector<std::vector<std::vector<double>>> values;
|
||||
for ( size_t i = 0; i < m_variables.size(); i++ )
|
||||
auto [isOk, errorMessage] = validateVariables();
|
||||
if ( !isOk )
|
||||
{
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( m_variables[i] );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
|
||||
if ( !v->eclipseCase() )
|
||||
{
|
||||
RiaLogging::errorInMessageBox( nullptr,
|
||||
"Expression Parser",
|
||||
QString( "No case defined for variable : %1" ).arg( v->name() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( v->resultVariable().isEmpty() )
|
||||
{
|
||||
RiaLogging::errorInMessageBox( nullptr,
|
||||
"Expression Parser",
|
||||
QString( "No result variable defined for variable : %1" ).arg( v->name() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
auto resultCategoryType = v->resultCategoryType();
|
||||
RigEclipseResultAddress resAddr( resultCategoryType, v->resultVariable() );
|
||||
if ( !eclipseCase->results( porosityModel )->ensureKnownResultLoaded( resAddr ) )
|
||||
{
|
||||
RiaLogging::errorInMessageBox( nullptr,
|
||||
"Expression Parser",
|
||||
QString( "Unable to load result for variable : %1" ).arg( v->name() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
int timeStep = v->timeStep();
|
||||
|
||||
std::vector<std::vector<double>> inputValues = eclipseCase->results( porosityModel )->cellScalarResults( resAddr );
|
||||
if ( resultCategoryType == RiaDefines::ResultCatType::STATIC_NATIVE )
|
||||
{
|
||||
// Use static data for all time steps
|
||||
inputValues.resize( timeStepCount );
|
||||
for ( size_t tsId = 1; tsId < timeStepCount; tsId++ )
|
||||
{
|
||||
inputValues[tsId] = inputValues[0];
|
||||
}
|
||||
}
|
||||
else if ( timeStep != RimGridCalculationVariable::allTimeStepsValue() )
|
||||
{
|
||||
// Use data from a specific time step for this variable for all result time steps
|
||||
for ( size_t tsId = 0; tsId < timeStepCount; tsId++ )
|
||||
{
|
||||
if ( static_cast<int>( tsId ) != timeStep )
|
||||
{
|
||||
inputValues[tsId] = inputValues[timeStep];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
values.push_back( inputValues );
|
||||
RiaLogging::errorInMessageBox( nullptr, "Grid Property Calculator", errorMessage );
|
||||
return false;
|
||||
}
|
||||
|
||||
auto porosityModel = RiaDefines::PorosityModelType::MATRIX_MODEL;
|
||||
|
||||
RigEclipseResultAddress resAddr( RiaDefines::ResultCatType::GENERATED, leftHandSideVariableName );
|
||||
|
||||
if ( !eclipseCase->results( porosityModel )->ensureKnownResultLoaded( resAddr ) )
|
||||
@@ -138,22 +88,32 @@ bool RimGridCalculation::calculate()
|
||||
|
||||
eclipseCase->results( porosityModel )->clearScalarResult( resAddr );
|
||||
|
||||
const size_t timeStepCount = eclipseCase->results( porosityModel )->maxTimeStepCount();
|
||||
|
||||
std::vector<std::vector<double>>* scalarResultFrames =
|
||||
eclipseCase->results( porosityModel )->modifiableCellScalarResultTimesteps( resAddr );
|
||||
scalarResultFrames->resize( timeStepCount );
|
||||
|
||||
for ( size_t tsId = 0; tsId < timeStepCount; tsId++ )
|
||||
{
|
||||
std::vector<std::vector<double>> values;
|
||||
for ( size_t i = 0; i < m_variables.size(); i++ )
|
||||
{
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( m_variables[i] );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
values.push_back( getInputVectorForVariable( v, tsId, porosityModel ) );
|
||||
}
|
||||
|
||||
ExpressionParser parser;
|
||||
for ( size_t i = 0; i < m_variables.size(); i++ )
|
||||
{
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( m_variables[i] );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
parser.assignVector( v->name(), values[i][tsId] );
|
||||
parser.assignVector( v->name(), values[i] );
|
||||
}
|
||||
|
||||
std::vector<double> resultValues;
|
||||
resultValues.resize( values[0][tsId].size() );
|
||||
resultValues.resize( values[0].size() );
|
||||
parser.assignVector( leftHandSideVariableName, resultValues );
|
||||
|
||||
QString errorText;
|
||||
@@ -161,6 +121,14 @@ bool RimGridCalculation::calculate()
|
||||
|
||||
if ( evaluatedOk )
|
||||
{
|
||||
auto [cellFilterView, defaultValueConfig] = findFilterValuesFromVariables();
|
||||
|
||||
if ( cellFilterView )
|
||||
{
|
||||
auto [defaultValueType, defaultValue] = defaultValueConfig;
|
||||
filterResults( cellFilterView, values, defaultValueType, defaultValue, resultValues );
|
||||
}
|
||||
|
||||
scalarResultFrames->at( tsId ) = resultValues;
|
||||
|
||||
m_isDirty = false;
|
||||
@@ -170,7 +138,7 @@ bool RimGridCalculation::calculate()
|
||||
QString s = "The following error message was received from the parser library : \n\n";
|
||||
s += errorText;
|
||||
|
||||
RiaLogging::errorInMessageBox( nullptr, "Expression Parser", s );
|
||||
RiaLogging::errorInMessageBox( nullptr, "Grid Property Calculator", s );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -181,7 +149,7 @@ bool RimGridCalculation::calculate()
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RimEclipseCase* RimGridCalculation::findEclipseCaseFromVariables()
|
||||
RimEclipseCase* RimGridCalculation::findEclipseCaseFromVariables() const
|
||||
{
|
||||
for ( auto variable : m_variables )
|
||||
{
|
||||
@@ -194,6 +162,150 @@ RimEclipseCase* RimGridCalculation::findEclipseCaseFromVariables()
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::pair<RimGridView*, RimGridCalculationVariable::DefaultValueConfig>
|
||||
RimGridCalculation::findFilterValuesFromVariables() const
|
||||
{
|
||||
for ( auto variable : m_variables )
|
||||
{
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( variable.p() );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
|
||||
if ( v->cellFilterView() ) return std::make_pair( v->cellFilterView(), v->defaultValueConfiguration() );
|
||||
}
|
||||
|
||||
return std::pair( nullptr, std::make_pair( RimGridCalculationVariable::DefaultValueType::POSITIVE_INFINITY, HUGE_VAL ) );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int RimGridCalculation::findFilterVariableIndex() const
|
||||
{
|
||||
for ( size_t i = 0; i < m_variables.size(); i++ )
|
||||
{
|
||||
auto variable = m_variables[i];
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( variable );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
|
||||
if ( v->cellFilterView() ) return static_cast<int>( i );
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::vector<double> RimGridCalculation::getInputVectorForVariable( RimGridCalculationVariable* v,
|
||||
size_t tsId,
|
||||
RiaDefines::PorosityModelType porosityModel ) const
|
||||
{
|
||||
int timeStep = v->timeStep();
|
||||
|
||||
auto resultCategoryType = v->resultCategoryType();
|
||||
|
||||
// General case is to use the data from the given time step
|
||||
size_t timeStepToUse = tsId;
|
||||
|
||||
if ( resultCategoryType == RiaDefines::ResultCatType::STATIC_NATIVE )
|
||||
{
|
||||
// Use the first time step for static data for all time steps
|
||||
timeStepToUse = 0;
|
||||
}
|
||||
else if ( timeStep != RimGridCalculationVariable::allTimeStepsValue() )
|
||||
{
|
||||
// Use data from a specific time step for this variable for all result time steps
|
||||
timeStepToUse = timeStep;
|
||||
}
|
||||
|
||||
RigEclipseResultAddress resAddr( resultCategoryType, v->resultVariable() );
|
||||
|
||||
auto mainGrid = v->eclipseCase()->mainGrid();
|
||||
size_t maxGridCount = mainGrid->gridCount();
|
||||
|
||||
size_t cellCount = mainGrid->globalCellArray().size();
|
||||
std::vector<double> inputValues( cellCount );
|
||||
for ( size_t gridIdx = 0; gridIdx < maxGridCount; ++gridIdx )
|
||||
{
|
||||
auto grid = mainGrid->gridByIndex( gridIdx );
|
||||
|
||||
cvf::ref<RigResultAccessor> sourceResultAccessor =
|
||||
RigResultAccessorFactory::createFromResultAddress( v->eclipseCase()->eclipseCaseData(),
|
||||
gridIdx,
|
||||
porosityModel,
|
||||
timeStepToUse,
|
||||
resAddr );
|
||||
|
||||
#pragma omp parallel for
|
||||
for ( int localGridCellIdx = 0; localGridCellIdx < static_cast<int>( grid->cellCount() ); localGridCellIdx++ )
|
||||
{
|
||||
const size_t reservoirCellIndex = grid->reservoirCellIndex( localGridCellIdx );
|
||||
inputValues[reservoirCellIndex] = sourceResultAccessor->cellScalar( localGridCellIdx );
|
||||
}
|
||||
}
|
||||
|
||||
return inputValues;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimGridCalculation::replaceFilteredValuesWithVector( const std::vector<double>& inputValues,
|
||||
cvf::ref<cvf::UByteArray> visibility,
|
||||
std::vector<double>& resultValues )
|
||||
{
|
||||
#pragma omp parallel for
|
||||
for ( int i = 0; i < static_cast<int>( resultValues.size() ); i++ )
|
||||
{
|
||||
if ( !visibility->val( i ) )
|
||||
{
|
||||
resultValues[i] = inputValues[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimGridCalculation::replaceFilteredValuesWithDefaultValue( double defaultValue,
|
||||
cvf::ref<cvf::UByteArray> visibility,
|
||||
std::vector<double>& resultValues )
|
||||
{
|
||||
#pragma omp parallel for
|
||||
for ( int i = 0; i < static_cast<int>( resultValues.size() ); i++ )
|
||||
{
|
||||
if ( !visibility->val( i ) )
|
||||
{
|
||||
resultValues[i] = defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimGridCalculation::filterResults( RimGridView* cellFilterView,
|
||||
const std::vector<std::vector<double>>& values,
|
||||
RimGridCalculationVariable::DefaultValueType defaultValueType,
|
||||
double defaultValue,
|
||||
std::vector<double>& resultValues ) const
|
||||
{
|
||||
auto visibility = cellFilterView->currentTotalCellVisibility();
|
||||
|
||||
if ( defaultValueType == RimGridCalculationVariable::DefaultValueType::FROM_PROPERTY )
|
||||
{
|
||||
int filterVariableIndex = findFilterVariableIndex();
|
||||
replaceFilteredValuesWithVector( values[filterVariableIndex], visibility, resultValues );
|
||||
}
|
||||
else
|
||||
{
|
||||
replaceFilteredValuesWithDefaultValue( defaultValue, visibility, resultValues );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -237,3 +349,39 @@ void RimGridCalculation::removeDependentObjects()
|
||||
RimReloadCaseTools::updateAll3dViews( eclipseCase );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::pair<bool, QString> RimGridCalculation::validateVariables()
|
||||
{
|
||||
auto porosityModel = RiaDefines::PorosityModelType::MATRIX_MODEL;
|
||||
|
||||
for ( size_t i = 0; i < m_variables.size(); i++ )
|
||||
{
|
||||
RimGridCalculationVariable* v = dynamic_cast<RimGridCalculationVariable*>( m_variables[i] );
|
||||
CAF_ASSERT( v != nullptr );
|
||||
|
||||
if ( !v->eclipseCase() )
|
||||
{
|
||||
QString errorMessage = QString( "No case defined for variable : %1" ).arg( v->name() );
|
||||
return std::make_pair( false, errorMessage );
|
||||
}
|
||||
|
||||
if ( v->resultVariable().isEmpty() )
|
||||
{
|
||||
QString errorMessage = QString( "No result variable defined for variable : %1" ).arg( v->name() );
|
||||
return std::make_pair( false, errorMessage );
|
||||
}
|
||||
|
||||
auto resultCategoryType = v->resultCategoryType();
|
||||
RigEclipseResultAddress resAddr( resultCategoryType, v->resultVariable() );
|
||||
if ( !v->eclipseCase()->results( porosityModel )->ensureKnownResultLoaded( resAddr ) )
|
||||
{
|
||||
QString errorMessage = QString( "Unable to load result for variable : %1" ).arg( v->name() );
|
||||
return std::make_pair( false, errorMessage );
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_pair( true, "" );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user