Files
ResInsight/ApplicationLibCode/UnitTests/RifActiveCellsReader-Test.cpp
T
Magne Sjaastad ab96c520bd #14895: Improve stability when importing malformed input files
* #14895: Fix null grid dereference when importing mismatching GRDECL
* #14895: Fix out-of-bounds read in perforation interval import
* #14895: Validate connectivity indices in VTK import
* #14895: Fix heap overflow for odd PORV count in dual porosity cases
* #14895: Update roffcpp to validate array lengths when parsing ROFF files
2026-10-12 09:40:23 +02:00

164 lines
6.0 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- 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 "gtest/gtest.h"
#include "RiaStringEncodingTools.h"
#include "RiaTestDataDirectory.h"
#include "RifActiveCellsReader.h"
#include "ert/ecl/ecl_endian_flip.hpp"
#include "ert/ecl/ecl_file.hpp"
#include "ert/ecl/ecl_grid.hpp"
#include <QDir>
#include <QTemporaryDir>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifActiveCellsReaderTest, BasicTest10k )
{
QDir baseFolder( TEST_MODEL_DIR );
bool subFolderExists = baseFolder.cd( "TEST10K_FLT_LGR_NNC" );
EXPECT_TRUE( subFolderExists );
ecl_grid_type* mainEclGrid = nullptr;
{
QString filename( "TEST10K_FLT_LGR_NNC.EGRID" );
QString filePath = baseFolder.absoluteFilePath( filename );
mainEclGrid = ecl_grid_alloc( RiaStringEncodingTools::toNativeEncoded( filePath ).data() );
}
std::vector<std::vector<int>> activeCellsFromActnum;
std::vector<std::vector<int>> activeCellsFromPorv;
{
QString filename( "TEST10K_FLT_LGR_NNC.EGRID" );
QString filePath = baseFolder.absoluteFilePath( filename );
ecl_file_type* gridFile = ecl_file_open( RiaStringEncodingTools::toNativeEncoded( filePath ).data(), ECL_FILE_CLOSE_STREAM );
activeCellsFromActnum = RifActiveCellsReader::activeCellsFromActnumKeyword( gridFile );
EXPECT_EQ( (size_t)2, activeCellsFromActnum.size() );
ecl_file_close( gridFile );
}
{
QString filename( "TEST10K_FLT_LGR_NNC.INIT" );
QString filePath = baseFolder.absoluteFilePath( filename );
ecl_file_type* initFile = ecl_file_open( RiaStringEncodingTools::toNativeEncoded( filePath ).data(), ECL_FILE_CLOSE_STREAM );
int cellCountMainGrid = 0;
activeCellsFromPorv = RifActiveCellsReader::activeCellsFromPorvKeyword( initFile, false, cellCountMainGrid );
EXPECT_EQ( 2, (int)activeCellsFromPorv.size() );
ecl_file_close( initFile );
}
for ( size_t gridIndex = 0; gridIndex < activeCellsFromActnum.size(); gridIndex++ )
{
for ( size_t valueIndex = 0; valueIndex < activeCellsFromActnum[gridIndex].size(); valueIndex++ )
{
auto actnumValue = activeCellsFromActnum[gridIndex][valueIndex];
auto porvValue = activeCellsFromPorv[gridIndex][valueIndex];
if ( actnumValue > 0 )
{
EXPECT_TRUE( porvValue > 0 );
}
else
{
EXPECT_EQ( 0, porvValue );
}
}
}
std::vector<int> expectedActiveCellCountPerGrid;
expectedActiveCellCountPerGrid.push_back( 8517 );
expectedActiveCellCountPerGrid.push_back( 2608 );
for ( int gridIndex = 0; gridIndex < static_cast<int>( activeCellsFromActnum.size() ); gridIndex++ )
{
ecl_grid_type* currentGrid = nullptr;
if ( gridIndex == 0 )
{
currentGrid = mainEclGrid;
}
else
{
currentGrid = ecl_grid_iget_lgr( mainEclGrid, gridIndex - 1 );
}
auto activeCellsForGrid = activeCellsFromActnum[gridIndex];
if ( ecl_grid_get_global_size( currentGrid ) == static_cast<int>( activeCellsForGrid.size() ) )
{
int expectedCellCount = expectedActiveCellCountPerGrid[gridIndex];
EXPECT_EQ( expectedCellCount, ecl_grid_get_nactive( currentGrid ) );
int* actnum_values = activeCellsForGrid.data();
ecl_grid_reset_actnum( currentGrid, actnum_values );
EXPECT_EQ( expectedCellCount, ecl_grid_get_nactive( currentGrid ) );
}
}
ecl_grid_free( mainEclGrid );
}
//--------------------------------------------------------------------------------------------------
/// Odd number of PORV values for a dual porosity case. Used to write one int past the end of the
/// active cell vector.
//--------------------------------------------------------------------------------------------------
TEST( RifActiveCellsReaderTest, DualPorosityWithOddPorvCount )
{
QTemporaryDir tempDir;
ASSERT_TRUE( tempDir.isValid() );
const int porvCount = 11;
const QString filePath = tempDir.path() + "/ODD_PORV.INIT";
{
ecl_kw_type* kw = ecl_kw_alloc( "PORV", porvCount, ecl_type_create_from_type( ECL_FLOAT_TYPE ) );
float* data = static_cast<float*>( ecl_kw_get_ptr( kw ) );
for ( int i = 0; i < porvCount; i++ )
{
data[i] = 1.0f + static_cast<float>( i );
}
fortio_type* fortio = fortio_open_writer( RiaStringEncodingTools::toNativeEncoded( filePath ).data(), false, ECL_ENDIAN_FLIP );
ASSERT_NE( nullptr, fortio );
ecl_kw_fwrite( kw, fortio );
fortio_fclose( fortio );
ecl_kw_free( kw );
}
ecl_file_type* initFile = ecl_file_open( RiaStringEncodingTools::toNativeEncoded( filePath ).data(), ECL_FILE_CLOSE_STREAM );
ASSERT_NE( nullptr, initFile );
auto activeCells = RifActiveCellsReader::activeCellsFromPorvKeyword( initFile, true, 0 );
ecl_file_close( initFile );
ASSERT_EQ( size_t( 1 ), activeCells.size() );
ASSERT_EQ( size_t( porvCount / 2 ), activeCells[0].size() );
for ( int value : activeCells[0] )
{
EXPECT_GT( value, 0 );
}
}