Skip to content

Commit

Permalink
Add writing and reading from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Dec 23, 2024
1 parent 6238d10 commit d6e617c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 21 deletions.
81 changes: 80 additions & 1 deletion ApplicationLibCode/FileInterface/RifReaderRegularGridModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +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"

#include "RimEclipseCase.h"

#include <QDir>
#include <QFileInfo>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifReaderRegularGridModel::writeCache( const QString& filePath, RimEclipseCase* eclipseCase )
{
if ( !eclipseCase ) return;

QFileInfo storageFileInfo( filePath );
if ( storageFileInfo.exists() )
{
QDir storageDir = storageFileInfo.dir();
storageDir.remove( storageFileInfo.fileName() );
}

QDir::root().mkpath( storageFileInfo.absolutePath() );

QFile cacheFile( filePath );
if ( !cacheFile.open( QIODevice::WriteOnly ) )
{
RiaLogging::error( "Saving project: Can't open the cache file : " + filePath );
return;
}

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( filePath, eclipseCase->eclipseCaseData(), keywords, writeEchoKeywords ) )
{
RiaLogging::error( "Error detected when writing the cache file : " + filePath );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifReaderRegularGridModel::RifReaderRegularGridModel()
void RifReaderRegularGridModel::ensureDataIsReadFromCache( const QString& filePath, RimEclipseCase* eclipseCase )
{
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( filePath.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 );
}
}
13 changes: 8 additions & 5 deletions ApplicationLibCode/FileInterface/RifReaderRegularGridModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

#pragma once

class RifReaderRegularGridModel
#include <QString>

class RimEclipseCase;

namespace RifReaderRegularGridModel
{
public:
RifReaderRegularGridModel();
void writeCache( const QString& filePath, RimEclipseCase* eclipseCase );
void ensureDataIsReadFromCache( const QString& filePath, RimEclipseCase* eclipseCase );

private:
};
}; // namespace RifReaderRegularGridModel
31 changes: 17 additions & 14 deletions ApplicationLibCode/ProjectDataModel/RimRegularGridCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,33 @@ void RimRegularGridCase::createModel()
///
//--------------------------------------------------------------------------------------------------
void RimRegularGridCase::setupBeforeSave()
{
auto cacheFilePath = getValidCacheFileName();
RifReaderRegularGridModel::writeCache( cacheFilePath, this );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimRegularGridCase::getValidCacheFileName() const
{
auto cacheDirPath = RimTools::getCacheRootDirectoryPathFromProject();
cacheDirPath += "_welltarget";

/*
// Delete the storage file
QFileInfo storageFileInfo( newValidCacheFileName );
if ( storageFileInfo.exists() )
{
QDir storageDir = storageFileInfo.dir();
storageDir.remove( storageFileInfo.fileName() );
}
*/
cacheDirPath += "_welltarget/welltargetdata.GRDECL";
return cacheDirPath;
}

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

createModel();
auto cacheFilePath = getValidCacheFileName();
RifReaderRegularGridModel::ensureDataIsReadFromCache( cacheFilePath, this );

return true;
}
3 changes: 2 additions & 1 deletion ApplicationLibCode/ProjectDataModel/RimRegularGridCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class RimRegularGridCase : public RimEclipseResultCase
void createModel();

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

private:
caf::PdmField<cvf::Vec3d> m_minimum;
Expand Down

0 comments on commit d6e617c

Please sign in to comment.