Skip to content

Commit

Permalink
Fix calculation for active cell values
Browse files Browse the repository at this point in the history
Always allocate result data for active cells in destination case. Make sure that active cells is used for both population of expression variables and filtering of data. Improved naming to make it clear that we always work with active cell data.
  • Loading branch information
magnesj committed Apr 12, 2024
1 parent eeecc41 commit cd4227e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 71 deletions.
123 changes: 74 additions & 49 deletions ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ bool RimGridCalculation::calculate()
{
if ( !calculationCase ) continue;

if ( !calculationCase->eclipseCaseData() )
{
QString msg = QString( "No data available for case %1, aborting calculation" ).arg( calculationCase->caseUserDescription() );
RiaLogging::errorInMessageBox( nullptr, "Grid Property Calculator", msg );
return false;
}

if ( !calculationCase->eclipseCaseData()->activeCellInfo( RiaDefines::PorosityModelType::MATRIX_MODEL ) )
{
QString msg =
QString( "No active cell data available for case %1, aborting calculation" ).arg( calculationCase->caseUserDescription() );
RiaLogging::errorInMessageBox( nullptr, "Grid Property Calculator", msg );
return false;
}

for ( auto inputCase : inputCases() )
{
if ( !calculationCase->isGridSizeEqualTo( inputCase ) )
Expand Down Expand Up @@ -537,11 +552,11 @@ RigEclipseResultAddress RimGridCalculation::outputAddress() const
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<double> RimGridCalculation::getDataForVariable( RimGridCalculationVariable* variable,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const
std::vector<double> RimGridCalculation::getActiveCellValuesForVariable( RimGridCalculationVariable* variable,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const
{
if ( !sourceCase || !destinationCase ) return {};

Expand All @@ -562,18 +577,19 @@ std::vector<double> RimGridCalculation::getDataForVariable( RimGridCalculationVa
timeStepToUse = timeStep;
}

return getDataForResult( variable->resultVariable(), resultCategoryType, timeStepToUse, porosityModel, sourceCase, destinationCase );
return getActiveCellValues( variable->resultVariable(), resultCategoryType, timeStepToUse, porosityModel, sourceCase, destinationCase );
}

//--------------------------------------------------------------------------------------------------
///
/// Return values for active cells, both for dynamic and static results. Use the active cell info from the destination case, and read data
/// from the source case.
//--------------------------------------------------------------------------------------------------
std::vector<double> RimGridCalculation::getDataForResult( const QString& resultName,
const RiaDefines::ResultCatType resultCategoryType,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const
std::vector<double> RimGridCalculation::getActiveCellValues( const QString& resultName,
const RiaDefines::ResultCatType resultCategoryType,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const
{
if ( !sourceCase || !destinationCase ) return {};

Expand All @@ -593,7 +609,10 @@ std::vector<double> RimGridCalculation::getDataForResult( const QString&
// Active cell info must always be retrieved from the destination case, as the returned vector must be of the same size as
// number of active cells in the destination case. Active cells can be different between source and destination case.
auto activeCellInfoDestination = destinationCase->eclipseCaseData()->activeCellInfo( porosityModel );
auto activeReservoirCells = activeCellInfoDestination->activeReservoirCellIndices();
if ( !activeCellInfoDestination ) return {};

auto activeReservoirCells = activeCellInfoDestination->activeReservoirCellIndices();
if ( activeReservoirCells.empty() ) return {};

std::vector<double> values( activeCellInfoDestination->activeReservoirCellIndices().size() );

Expand Down Expand Up @@ -621,47 +640,50 @@ std::vector<double> RimGridCalculation::getDataForResult( const QString&
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimGridCalculation::replaceFilteredValuesWithVector( const std::vector<double>& inputValues,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* outputEclipseCase )
void RimGridCalculation::replaceFilteredValuesWithVector( const std::vector<double>& inputValues,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RigActiveCellInfo* activeCellInfo )

{
auto activeCellInfo = outputEclipseCase->eclipseCaseData()->activeCellInfo( porosityModel );
int numCells = static_cast<int>( visibility->size() );
auto activeReservoirCellIndices = activeCellInfo->activeReservoirCellIndices();
int numActiveCells = static_cast<int>( activeReservoirCellIndices.size() );

CAF_ASSERT( numActiveCells == (int)resultValues.size() );
CAF_ASSERT( numActiveCells == (int)inputValues.size() );

#pragma omp parallel for
for ( int i = 0; i < numCells; i++ )
for ( int i = 0; i < numActiveCells; i++ )
{
if ( !visibility->val( i ) && activeCellInfo->isActive( i ) )
const auto reservoirCellIndex = activeReservoirCellIndices[i];
if ( !visibility->val( reservoirCellIndex ) )
{
size_t cellResultIndex = activeCellInfo->cellResultIndex( i );
resultValues[cellResultIndex] = inputValues[cellResultIndex];
resultValues[i] = inputValues[i];
}
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimGridCalculation::replaceFilteredValuesWithDefaultValue( double defaultValue,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* outputEclipseCase )
void RimGridCalculation::replaceFilteredValuesWithDefaultValue( double defaultValue,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RigActiveCellInfo* activeCellInfo )

{
auto activeCellInfo = outputEclipseCase->eclipseCaseData()->activeCellInfo( porosityModel );
int numCells = static_cast<int>( visibility->size() );
auto activeReservoirCellIndices = activeCellInfo->activeReservoirCellIndices();
int numActiveCells = static_cast<int>( activeReservoirCellIndices.size() );

CAF_ASSERT( numActiveCells == (int)resultValues.size() );

#pragma omp parallel for
for ( int i = 0; i < numCells; i++ )
for ( int i = 0; i < numActiveCells; i++ )
{
if ( !visibility->val( i ) && activeCellInfo->isActive( i ) )
const auto reservoirCellIndex = activeReservoirCellIndices[i];
if ( !visibility->val( reservoirCellIndex ) )
{
size_t cellResultIndex = activeCellInfo->cellResultIndex( i );
resultValues[cellResultIndex] = defaultValue;
resultValues[i] = defaultValue;
}
}
}
Expand All @@ -681,18 +703,20 @@ void RimGridCalculation::filterResults( RimGridView*
{
auto visibility = cellFilterView->currentTotalCellVisibility();

auto activeCellInfo = outputEclipseCase->eclipseCaseData()->activeCellInfo( porosityModel );

if ( defaultValueType == RimGridCalculation::DefaultValueType::FROM_PROPERTY )
{
auto nonVisibleValues = getDataForResult( m_nonVisibleResultAddress->resultName(),
m_nonVisibleResultAddress->resultType(),
timeStep,
porosityModel,
m_nonVisibleResultAddress->eclipseCase(),
outputEclipseCase );
auto nonVisibleValues = getActiveCellValues( m_nonVisibleResultAddress->resultName(),
m_nonVisibleResultAddress->resultType(),
timeStep,
porosityModel,
outputEclipseCase,
outputEclipseCase );

if ( !nonVisibleValues.empty() )
{
replaceFilteredValuesWithVector( nonVisibleValues, visibility, resultValues, porosityModel, outputEclipseCase );
replaceFilteredValuesWithVector( nonVisibleValues, visibility, resultValues, activeCellInfo );
}
else
{
Expand All @@ -706,7 +730,7 @@ void RimGridCalculation::filterResults( RimGridView*
double valueToUse = defaultValue;
if ( defaultValueType == RimGridCalculation::DefaultValueType::POSITIVE_INFINITY ) valueToUse = HUGE_VAL;

replaceFilteredValuesWithDefaultValue( valueToUse, visibility, resultValues, porosityModel, outputEclipseCase );
replaceFilteredValuesWithDefaultValue( valueToUse, visibility, resultValues, activeCellInfo );
}
}

Expand Down Expand Up @@ -873,15 +897,16 @@ bool RimGridCalculation::calculateForCases( const std::vector<RimEclipseCase*>&
bool useDataFromSourceCase = ( v->eclipseCase() == m_destinationCase );
auto sourceCase = useDataFromSourceCase ? calculationCase : v->eclipseCase();

auto dataForVariable = getDataForVariable( v, tsId, porosityModel, sourceCase, calculationCase );
auto dataForVariable = getActiveCellValuesForVariable( v, tsId, porosityModel, sourceCase, calculationCase );
if ( dataForVariable.empty() )
{
RiaLogging::error( QString( " No data found for variable '%1'." ).arg( v->name() ) );
}
else if ( inputValueVisibilityFilter && hasAggregationExpression )
{
const double defaultValue = 0.0;
replaceFilteredValuesWithDefaultValue( defaultValue, inputValueVisibilityFilter, dataForVariable, porosityModel, calculationCase );
const double defaultValue = 0.0;
auto activeCellInfo = calculationCase->eclipseCaseData()->activeCellInfo( porosityModel );
replaceFilteredValuesWithDefaultValue( defaultValue, inputValueVisibilityFilter, dataForVariable, activeCellInfo );
}

dataForAllVariables.push_back( dataForVariable );
Expand All @@ -896,10 +921,10 @@ bool RimGridCalculation::calculateForCases( const std::vector<RimEclipseCase*>&
}

std::vector<double> resultValues;
if ( m_destinationCase && m_destinationCase->eclipseCaseData() )
if ( calculationCase && calculationCase->eclipseCaseData() )
{
// Find number of active cells in the destination case.
auto activeCellInfoDestination = m_destinationCase->eclipseCaseData()->activeCellInfo( porosityModel );
auto activeCellInfoDestination = calculationCase->eclipseCaseData()->activeCellInfo( porosityModel );
if ( activeCellInfoDestination )
{
resultValues.resize( activeCellInfoDestination->reservoirActiveCellCount() );
Expand Down
43 changes: 21 additions & 22 deletions ApplicationLibCode/ProjectDataModel/RimGridCalculation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RimGridView;
class RigEclipseResultAddress;
class RimEclipseResultAddress;
class RimIdenticalGridCaseGroup;
class RigActiveCellInfo;

//==================================================================================================
///
Expand Down Expand Up @@ -88,18 +89,18 @@ class RimGridCalculation : public RimUserDefinedCalculation

std::pair<bool, QString> validateVariables();

std::vector<double> getDataForVariable( RimGridCalculationVariable* variable,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const;
std::vector<double> getActiveCellValuesForVariable( RimGridCalculationVariable* variable,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const;

std::vector<double> getDataForResult( const QString& resultName,
const RiaDefines::ResultCatType resultCategoryType,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const;
std::vector<double> getActiveCellValues( const QString& resultName,
const RiaDefines::ResultCatType resultCategoryType,
size_t tsId,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* sourceCase,
RimEclipseCase* destinationCase ) const;

void filterResults( RimGridView* cellFilterView,
const std::vector<std::vector<double>>& values,
Expand All @@ -110,17 +111,15 @@ class RimGridCalculation : public RimUserDefinedCalculation
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* outputEclipseCase ) const;

static void replaceFilteredValuesWithVector( const std::vector<double>& inputValues,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* outputEclipseCase );

static void replaceFilteredValuesWithDefaultValue( double defaultValue,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RiaDefines::PorosityModelType porosityModel,
RimEclipseCase* outputEclipseCase );
static void replaceFilteredValuesWithVector( const std::vector<double>& inputValues,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RigActiveCellInfo* activeCellInfo );

static void replaceFilteredValuesWithDefaultValue( double defaultValue,
cvf::ref<cvf::UByteArray> visibility,
std::vector<double>& resultValues,
RigActiveCellInfo* activeCellInfo );

using DefaultValueConfig = std::pair<RimGridCalculation::DefaultValueType, double>;
DefaultValueConfig defaultValueConfiguration() const;
Expand Down

0 comments on commit cd4227e

Please sign in to comment.