Files
ResInsight/ApplicationLibCode/ReservoirDataModel/RigEclipseResultTools.cpp
T

237 lines
8.9 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2025 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RigEclipseResultTools.h"
#include "RiaDefines.h"
#include "RiaPorosityModel.h"
#include "RiaResultNames.h"
#include "RigActiveCellInfo.h"
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigEclipseResultAddress.h"
#include "RigMainGrid.h"
#include "RimEclipseCase.h"
#include "RimEclipseResultCase.h"
#include "RimEclipseView.h"
namespace RigEclipseResultTools
{
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void createResultVector( RimEclipseCase& eclipseCase, const QString& resultName, const std::vector<int>& intValues )
{
RigEclipseResultAddress resultAddress( RiaDefines::ResultCatType::GENERATED, RiaDefines::ResultDataType::INTEGER, resultName );
auto resultsData = eclipseCase.results( RiaDefines::PorosityModelType::MATRIX_MODEL );
resultsData->addStaticScalarResult( RiaDefines::ResultCatType::GENERATED, resultName, false, intValues.size() );
std::vector<double>* resultVector = resultsData->modifiableCellScalarResult( resultAddress, 0 );
resultVector->resize( intValues.size() );
for ( size_t idx = 0; idx < intValues.size(); idx++ )
{
resultVector->at( idx ) = 1.0 * intValues[idx];
}
resultsData->recalculateStatistics( resultAddress );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void generateBorderResult( RimEclipseCase* eclipseCase, cvf::ref<cvf::UByteArray> customVisibility, const QString& resultName )
{
if ( eclipseCase == nullptr || customVisibility.isNull() ) return;
auto activeReservoirCellIdxs =
eclipseCase->eclipseCaseData()->activeCellInfo( RiaDefines::PorosityModelType::MATRIX_MODEL )->activeReservoirCellIndices();
int numActiveCells = static_cast<int>( activeReservoirCellIdxs.size() );
std::vector<int> result;
result.resize( numActiveCells, BorderType::INVISIBLE_CELL );
auto grid = eclipseCase->eclipseCaseData()->mainGrid();
// go through all cells, only check those visible
#pragma omp parallel for
for ( int i = 0; i < numActiveCells; i++ )
{
auto cellIdx = activeReservoirCellIdxs[i];
if ( customVisibility->val( cellIdx ) )
{
auto neighbors = grid->neighborCells( cellIdx, true /*ignore invalid k layers*/ );
int nVisibleNeighbors = 0;
for ( auto nIdx : neighbors )
{
if ( customVisibility->val( nIdx ) ) nVisibleNeighbors++;
}
if ( nVisibleNeighbors == 6 )
{
result[i] = BorderType::INTERIOR_CELL;
}
else
{
result[i] = BorderType::BORDER_CELL;
}
}
}
RigEclipseResultTools::createResultVector( *eclipseCase, resultName, result );
eclipseCase->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void generateOperNumResult( RimEclipseCase* eclipseCase, int borderCellValue )
{
if ( eclipseCase == nullptr ) return;
// Auto-determine border cell value if not specified
if ( borderCellValue == -1 )
{
int maxOperNum = findMaxOperNumValue( eclipseCase );
borderCellValue = maxOperNum + 1;
}
auto activeReservoirCellIdxs =
eclipseCase->eclipseCaseData()->activeCellInfo( RiaDefines::PorosityModelType::MATRIX_MODEL )->activeReservoirCellIndices();
int numActiveCells = static_cast<int>( activeReservoirCellIdxs.size() );
std::vector<int> result;
result.resize( numActiveCells, 0 );
// Check if OPERNUM already exists - if so, keep existing values.
RigEclipseResultAddress operNumAddrNative( RiaDefines::ResultCatType::STATIC_NATIVE,
RiaDefines::ResultDataType::INTEGER,
RiaResultNames::opernum() );
RigEclipseResultAddress operNumAddrGenerated( RiaDefines::ResultCatType::GENERATED,
RiaDefines::ResultDataType::INTEGER,
RiaResultNames::opernum() );
auto resultsData = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
RigEclipseResultAddress operNumAddr;
bool hasExistingOperNum = false;
if ( resultsData->hasResultEntry( operNumAddrNative ) )
{
operNumAddr = operNumAddrNative;
hasExistingOperNum = true;
}
else if ( resultsData->hasResultEntry( operNumAddrGenerated ) )
{
operNumAddr = operNumAddrGenerated;
hasExistingOperNum = true;
}
if ( hasExistingOperNum )
{
resultsData->ensureKnownResultLoaded( operNumAddr );
auto existingValues = resultsData->cellScalarResults( operNumAddr );
if ( !existingValues.empty() )
{
for ( int i = 0; i < numActiveCells; i++ )
{
result[i] = static_cast<int>( existingValues[0][i] );
}
}
}
// Check if BORDNUM exists to modify border cells
RigEclipseResultAddress bordNumAddr( RiaDefines::ResultCatType::GENERATED, RiaDefines::ResultDataType::INTEGER, RiaResultNames::bordnum() );
if ( resultsData->hasResultEntry( bordNumAddr ) )
{
resultsData->ensureKnownResultLoaded( bordNumAddr );
auto bordNumValues = resultsData->cellScalarResults( bordNumAddr );
if ( !bordNumValues.empty() )
{
for ( int i = 0; i < numActiveCells; i++ )
{
// If BORDNUM = 1 (BORDER_CELL), assign the border cell value
if ( static_cast<int>( bordNumValues[0][i] ) == BorderType::BORDER_CELL )
{
result[i] = borderCellValue;
}
}
}
}
RigEclipseResultTools::createResultVector( *eclipseCase, RiaResultNames::opernum(), result );
eclipseCase->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int findMaxOperNumValue( RimEclipseCase* eclipseCase )
{
if ( eclipseCase == nullptr ) return 0;
auto resultsData = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
if ( !resultsData ) return 0;
// Try to find OPERNUM in both STATIC_NATIVE (from file) and GENERATED (created by us) categories
RigEclipseResultAddress operNumAddrNative( RiaDefines::ResultCatType::STATIC_NATIVE,
RiaDefines::ResultDataType::INTEGER,
RiaResultNames::opernum() );
RigEclipseResultAddress operNumAddrGenerated( RiaDefines::ResultCatType::GENERATED,
RiaDefines::ResultDataType::INTEGER,
RiaResultNames::opernum() );
RigEclipseResultAddress operNumAddr;
bool hasOperNum = false;
if ( resultsData->hasResultEntry( operNumAddrNative ) )
{
operNumAddr = operNumAddrNative;
hasOperNum = true;
}
else if ( resultsData->hasResultEntry( operNumAddrGenerated ) )
{
operNumAddr = operNumAddrGenerated;
hasOperNum = true;
}
if ( !hasOperNum ) return 0;
resultsData->ensureKnownResultLoaded( operNumAddr );
auto operNumValues = resultsData->cellScalarResults( operNumAddr );
if ( operNumValues.empty() ) return 0;
// Find maximum value
int maxValue = 0;
for ( double value : operNumValues[0] )
{
int intValue = static_cast<int>( value );
maxValue = std::max( maxValue, intValue );
}
return maxValue;
}
} // namespace RigEclipseResultTools