Skip to content

Commit

Permalink
#12017 Add caching of well target result data
Browse files Browse the repository at this point in the history
Write data to welltargetdata.GRDECL in a subfolder of the project. Import this data when project is load. The data in this cache is owerwritten when the project is saved.
  • Loading branch information
magnesj committed Dec 23, 2024
1 parent 1cdbffb commit edd6110
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 87 deletions.
122 changes: 68 additions & 54 deletions ApplicationLibCode/FileInterface/RifReaderRegularGridModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,91 @@

#include "RifReaderRegularGridModel.h"

#include "RiaDefines.h"
#include "RiaLogging.h"

#include "RifEclipseInputFileTools.h"
#include "RifEclipseInputPropertyLoader.h"
#include "RifEclipseKeywordContent.h"
#include "RifEclipseTextFileReader.h"

#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigEclipseResultAddress.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifReaderRegularGridModel::RifReaderRegularGridModel()
: m_reservoir( nullptr )
{
}
#include "RimEclipseCase.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifReaderRegularGridModel::~RifReaderRegularGridModel()
{
}
#include <QDir>
#include <QFileInfo>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifReaderRegularGridModel::open( const QString& fileName, RigEclipseCaseData* eclipseCase )
void RifReaderRegularGridModel::writeCache( const QString& fileName, RimEclipseCase* eclipseCase )
{
m_reservoirBuilder.createGridsAndCells( eclipseCase );
m_reservoir = eclipseCase;
return true;
}
if ( !eclipseCase ) return;

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifReaderRegularGridModel::staticResult( const QString& result, RiaDefines::PorosityModelType matrixOrFracture, std::vector<double>* values )
{
CAF_ASSERT( false );
return true;
}
QFileInfo storageFileInfo( fileName );
if ( storageFileInfo.exists() )
{
QDir storageDir = storageFileInfo.dir();
storageDir.remove( storageFileInfo.fileName() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifReaderRegularGridModel::dynamicResult( const QString& result,
RiaDefines::PorosityModelType matrixOrFracture,
size_t stepIndex,
std::vector<double>* values )
{
CAF_ASSERT( false );
return true;
}
QDir::root().mkpath( storageFileInfo.absolutePath() );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifReaderRegularGridModel::setWorldCoordinates( cvf::Vec3d minWorldCoordinate, cvf::Vec3d maxWorldCoordinate )
{
m_reservoirBuilder.setWorldCoordinates( minWorldCoordinate, maxWorldCoordinate );
}
QFile cacheFile( fileName );
if ( !cacheFile.open( QIODevice::WriteOnly ) )
{
RiaLogging::error( "Saving project: Can't open the cache file : " + fileName );
return;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifReaderRegularGridModel::setGridPointDimensions( const cvf::Vec3st& gridPointDimensions )
{
m_reservoirBuilder.setIJKCount( gridPointDimensions );
auto rigCellResults = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
if ( !rigCellResults ) return;

auto resultNames = rigCellResults->resultNames( RiaDefines::ResultCatType::GENERATED );

std::vector<QString> keywords;
for ( const auto& resultName : resultNames )
{
keywords.push_back( resultName );
}

const bool writeEchoKeywords = false;
if ( !RifEclipseInputFileTools::exportKeywords( fileName, eclipseCase->eclipseCaseData(), keywords, writeEchoKeywords ) )
{
RiaLogging::error( "Error detected when writing the cache file : " + fileName );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifReaderRegularGridModel::addLocalGridRefinement( const cvf::Vec3st& minCellPosition,
const cvf::Vec3st& maxCellPosition,
const cvf::Vec3st& singleCellRefinementFactors )
void RifReaderRegularGridModel::ensureDataIsReadFromCache( const QString& fileName, RimEclipseCase* eclipseCase )
{
m_reservoirBuilder.addLocalGridRefinement( minCellPosition, maxCellPosition, singleCellRefinementFactors );
if ( !eclipseCase ) return;

auto rigCellResults = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
if ( !rigCellResults ) return;

// Early return if we have already read the data from the cache
auto existingResultNames = rigCellResults->resultNames( RiaDefines::ResultCatType::GENERATED );
if ( !existingResultNames.empty() ) return;

auto keywordsAndData = RifEclipseTextFileReader::readKeywordAndValues( fileName.toStdString() );
for ( const auto& content : keywordsAndData )
{
const auto resultName = QString::fromStdString( content.keyword );

RigEclipseResultAddress resAddr( RiaDefines::ResultCatType::GENERATED, RiaDefines::ResultDataType::FLOAT, resultName );
rigCellResults->createResultEntry( resAddr, false );

auto newPropertyData = rigCellResults->modifiableCellScalarResultTimesteps( resAddr );

std::vector<double> doubleVals;
doubleVals.insert( doubleVals.begin(), content.values.begin(), content.values.end() );

newPropertyData->push_back( doubleVals );
}
}
29 changes: 7 additions & 22 deletions ApplicationLibCode/FileInterface/RifReaderRegularGridModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,13 @@

#pragma once

#include "RifReaderInterface.h"
#include "RigReservoirBuilder.h"
#include <QString>

class RifReaderRegularGridModel : public RifReaderInterface
{
public:
RifReaderRegularGridModel();
~RifReaderRegularGridModel() override;

void setWorldCoordinates( cvf::Vec3d minWorldCoordinate, cvf::Vec3d maxWorldCoordinate );
void setGridPointDimensions( const cvf::Vec3st& gridPointDimensions );

void addLocalGridRefinement( const cvf::Vec3st& minCellPosition,
const cvf::Vec3st& maxCellPosition,
const cvf::Vec3st& singleCellRefinementFactors );
class RimEclipseCase;

bool open( const QString& fileName, RigEclipseCaseData* eclipseCase ) override;

bool staticResult( const QString& result, RiaDefines::PorosityModelType matrixOrFracture, std::vector<double>* values ) override;
bool dynamicResult( const QString& result, RiaDefines::PorosityModelType matrixOrFracture, size_t stepIndex, std::vector<double>* values ) override;
namespace RifReaderRegularGridModel
{
void writeCache( const QString& fileName, RimEclipseCase* eclipseCase );
void ensureDataIsReadFromCache( const QString& fileName, RimEclipseCase* eclipseCase );

private:
RigReservoirBuilder m_reservoirBuilder;
RigEclipseCaseData* m_reservoir;
};
}; // namespace RifReaderRegularGridModel
42 changes: 33 additions & 9 deletions ApplicationLibCode/ProjectDataModel/RimRegularGridCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigReservoirBuilder.h"

#include "RimTools.h"

CAF_PDM_SOURCE_INIT( RimRegularGridCase, "EclipseBoundingBoxCase" );
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -69,32 +72,53 @@ void RimRegularGridCase::setCellCount( const cvf::Vec3st& cellCount )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<RifReaderInterface> RimRegularGridCase::createModel( QString modelName )
void RimRegularGridCase::createModel()
{
cvf::ref<RifReaderRegularGridModel> reader = new RifReaderRegularGridModel;
cvf::ref<RigEclipseCaseData> reservoir = new RigEclipseCaseData( this );
cvf::ref<RigEclipseCaseData> reservoir = new RigEclipseCaseData( this );

reader->setWorldCoordinates( m_minimum, m_maximum );
RigReservoirBuilder reservoirBuilder;
reservoirBuilder.setWorldCoordinates( m_minimum, m_maximum );

cvf::Vec3st gridPointDimensions( m_cellCountI, m_cellCountJ, m_cellCountK );
reader->setGridPointDimensions( gridPointDimensions );
reservoirBuilder.setIJKCount( gridPointDimensions );

reader->open( "", reservoir.p() );
reservoirBuilder.createGridsAndCells( reservoir.p() );

setReservoirData( reservoir.p() );
computeCachedData();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimRegularGridCase::setupBeforeSave()
{
auto fileName = cacheFileName();
RifReaderRegularGridModel::writeCache( fileName, this );
}

return reader.p();
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimRegularGridCase::cacheFileName() const
{
auto cacheDirPath = RimTools::getCacheRootDirectoryPathFromProject();
cacheDirPath += "_welltarget/welltargetdata.GRDECL";
return cacheDirPath;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimRegularGridCase::openEclipseGridFile()
{
if ( eclipseCaseData() ) return true;
if ( !eclipseCaseData() )
{
createModel();
}

createModel( "" );
auto fileName = cacheFileName();
RifReaderRegularGridModel::ensureDataIsReadFromCache( fileName, this );

return true;
}
6 changes: 5 additions & 1 deletion ApplicationLibCode/ProjectDataModel/RimRegularGridCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class RimRegularGridCase : public RimEclipseResultCase

bool openEclipseGridFile() override;

cvf::ref<RifReaderInterface> createModel( QString modelName );
void createModel();

private:
void setupBeforeSave() override;
QString cacheFileName() const;

private:
caf::PdmField<cvf::Vec3d> m_minimum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ RimRegularGridCase* RigWellTargetCandidatesGenerator::generateEnsembleCandidates
RimRegularGridCase* targetCase = new RimRegularGridCase;
targetCase->setBoundingBox( boundingBox );
targetCase->setCellCount( resultGridCellCount );
targetCase->createModel( "" );
targetCase->createModel();

std::vector<int> occurrence;

Expand Down

0 comments on commit edd6110

Please sign in to comment.