Skip to content

Commit

Permalink
Import of AICD valve definition from text file
Browse files Browse the repository at this point in the history
* Add readKeywordContentFromFile
* Add import of AICD values from Completor and Eclipse
  • Loading branch information
magnesj authored Sep 12, 2023
1 parent 2e94bd8 commit 13b4c19
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicWellPathImportPerforationIntervalsFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicNewStimPlanModelPlotFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicImportEnsembleFractureStatisticsFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicImportValveTemplatesFeature.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand All @@ -28,6 +29,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicWellPathImportPerforationIntervalsFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicNewStimPlanModelPlotFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicImportEnsembleFractureStatisticsFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicImportValveTemplatesFeature.cpp
)

list(APPEND COMMAND_CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RicImportValveTemplatesFeature.h"

#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RiaOpmParserTools.h"

#include "RimProject.h"
#include "RimValveTemplate.h"
#include "RimValveTemplateCollection.h"

#include "Riu3DMainWindowTools.h"
#include "RiuFileDialogTools.h"

#include <QAction>

CAF_CMD_SOURCE_INIT( RicImportValveTemplatesFeature, "RicImportValveTemplatesFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicImportValveTemplatesFeature::isCommandEnabled() const
{
return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicImportValveTemplatesFeature::onActionTriggered( bool isChecked )
{
RiaApplication* app = RiaApplication::instance();
QString defaultDir = app->lastUsedDialogDirectory( "BINARY_GRID" );

QStringList fileNames = RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(),
"Import Valve Templates",
defaultDir,
"Valve Files (*.sch *.case);;All Files (*.*)" );

if ( RimProject::current()->allValveTemplateCollections().empty() )
{
RiaLogging::error( "No valve template collection found, failed to import valves." );
return;
}

RimValveTemplateCollection* templateColl = RimProject::current()->allValveTemplateCollections().front();
for ( const auto& fileName : fileNames )
{
std::vector<RiaOpmParserTools::AicdTemplateValues> aicdTemplates;

if ( fileName.contains( ".case" ) )
{
aicdTemplates = RiaOpmParserTools::extractWsegAicdCompletor( fileName.toStdString() );
}
else
{
aicdTemplates = RiaOpmParserTools::extractWsegAicd( fileName.toStdString() );
}

// There can be multiple items of the same template, make sure we have unique templates
std::sort( aicdTemplates.begin(), aicdTemplates.end() );
auto it = std::unique( aicdTemplates.begin(), aicdTemplates.end() );
aicdTemplates.resize( std::distance( aicdTemplates.begin(), it ) );

int number = 1;
for ( const auto& aicdValue : aicdTemplates )
{
auto newTemplate = RimValveTemplate::createAicdTemplate( aicdValue, number++ );
templateColl->addValveTemplate( newTemplate );
}
}

templateColl->updateConnectedEditors();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicImportValveTemplatesFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setIcon( QIcon( ":/ICDValve16x16.png" ) );
actionToSetup->setText( "Import Valve Templates" );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicImportValveTemplatesFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

protected:
bool isCommandEnabled() const override;
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
75 changes: 45 additions & 30 deletions ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,16 +1181,15 @@ bool RifEclipseInputFileTools::readFaultsAndParseIncludeStatementsRecursively( Q
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(
const QString& keyword,
const QString& keywordToStopParsing,
QFile& file,
qint64 startPos,
const std::vector<std::pair<QString, QString>>& pathAliasDefinitions,
QStringList* keywordDataContent,
std::vector<QString>* filenamesContainingKeyword,
bool* isStopParsingKeywordDetected,
const QString& faultIncludeFileAbsolutePathPrefix /* rename to includeStatementAbsolutePathPrefix */ )
bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively( const QString& keyword,
const QString& keywordToStopParsing,
const std::vector<std::pair<QString, QString>>& pathAliasDefinitions,
const QString& includeStatementAbsolutePathPrefix,
QFile& file,
qint64 startPos,
QStringList& keywordDataContent,
std::vector<QString>& filenamesContainingKeyword,
bool& isStopParsingKeywordDetected )
{
QString line;

Expand All @@ -1213,10 +1212,7 @@ bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(

if ( !keywordToStopParsing.isEmpty() && line.startsWith( keywordToStopParsing, Qt::CaseInsensitive ) )
{
if ( isStopParsingKeywordDetected )
{
*isStopParsingKeywordDetected = true;
}
isStopParsingKeywordDetected = true;

return false;
}
Expand Down Expand Up @@ -1256,7 +1252,7 @@ bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(
if ( includeFilename.startsWith( '/' ) )
{
// Absolute UNIX path, prefix on Windows
includeFilename = faultIncludeFileAbsolutePathPrefix + includeFilename;
includeFilename = includeStatementAbsolutePathPrefix + includeFilename;
}
#endif

Expand All @@ -1267,17 +1263,15 @@ bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(
QFile includeFile( absoluteFilename );
if ( includeFile.open( QFile::ReadOnly ) )
{
// qDebug() << "Found include statement, and start parsing of\n " << absoluteFilename;

if ( !readKeywordAndParseIncludeStatementsRecursively( keyword,
keywordToStopParsing,
pathAliasDefinitions,
includeStatementAbsolutePathPrefix,
includeFile,
0,
pathAliasDefinitions,
keywordDataContent,
filenamesContainingKeyword,
isStopParsingKeywordDetected,
faultIncludeFileAbsolutePathPrefix ) )
isStopParsingKeywordDetected ) )
{
qDebug() << "Error when parsing include file : " << absoluteFilename;
}
Expand All @@ -1290,11 +1284,11 @@ bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(
if ( !line.contains( "/" ) )
{
readKeywordDataContent( file, file.pos(), keywordDataContent, isStopParsingKeywordDetected );
filenamesContainingKeyword->push_back( file.fileName() );
filenamesContainingKeyword.push_back( file.fileName() );
}
}

if ( isStopParsingKeywordDetected && *isStopParsingKeywordDetected )
if ( isStopParsingKeywordDetected )
{
continueParsing = false;
}
Expand All @@ -1312,7 +1306,7 @@ bool RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively(
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseInputFileTools::readKeywordDataContent( QFile& data, qint64 filePos, QStringList* textContent, bool* isEditKeywordDetected )
void RifEclipseInputFileTools::readKeywordDataContent( QFile& data, qint64 filePos, QStringList& textContent, bool& isStopParsingKeywordDetected )
{
if ( !data.seek( filePos ) )
{
Expand Down Expand Up @@ -1340,11 +1334,7 @@ void RifEclipseInputFileTools::readKeywordDataContent( QFile& data, qint64 fileP
else if ( line.startsWith( editKeyword, Qt::CaseInsensitive ) )
{
// End parsing when edit keyword is detected

if ( isEditKeywordDetected )
{
*isEditKeywordDetected = true;
}
isStopParsingKeywordDetected = true;

return;
}
Expand All @@ -1356,7 +1346,7 @@ void RifEclipseInputFileTools::readKeywordDataContent( QFile& data, qint64 fileP

if ( !line.isEmpty() )
{
textContent->push_back( line );
textContent.push_back( line );
}

} while ( !data.atEnd() );
Expand All @@ -1369,7 +1359,7 @@ RiaDefines::EclipseUnitSystem RifEclipseInputFileTools::readUnitSystem( QFile& f
{
bool stopParsing = false;
QStringList unitText;
readKeywordDataContent( file, gridunitPos, &unitText, &stopParsing );
readKeywordDataContent( file, gridunitPos, unitText, stopParsing );
for ( QString unitString : unitText )
{
if ( unitString.contains( "FEET", Qt::CaseInsensitive ) )
Expand Down Expand Up @@ -1437,6 +1427,31 @@ bool RifEclipseInputFileTools::hasGridData( const QString& fileName )
return coordKeywordItr != keywordAndValues.end();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RifEclipseInputFileTools::readKeywordContentFromFile( const QString& keyword, const QString& keywordToStopParsing, QFile& file )
{
std::vector<std::pair<QString, QString>> pathAliasDefinitions;
const QString includeStatementAbsolutePathPrefix;
const qint64 startPositionInFile = 0;
QStringList keywordContent;
std::vector<QString> fileNamesContainingKeyword;
bool isStopParsingKeywordDetected = false;

RifEclipseInputFileTools::readKeywordAndParseIncludeStatementsRecursively( keyword,
keywordToStopParsing,
pathAliasDefinitions,
includeStatementAbsolutePathPrefix,
file,
startPositionInFile,
keywordContent,
fileNamesContainingKeyword,
isStopParsingKeywordDetected );

return keywordContent;
}

//--------------------------------------------------------------------------------------------------
/// The file pointer is pointing at the line following the FAULTS keyword.
/// Parse content of this keyword until end of file or
Expand Down
19 changes: 10 additions & 9 deletions ApplicationLibCode/FileInterface/RifEclipseInputFileTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,18 @@ class RifEclipseInputFileTools : public cvf::Object

static void parseAndReadPathAliasKeyword( const QString& fileName, std::vector<std::pair<QString, QString>>* pathAliasDefinitions );

static QStringList readKeywordContentFromFile( const QString& keyword, const QString& keywordToStopParsing, QFile& file );

// Public for unit testing, use readKeywordContentFromFile()
static bool readKeywordAndParseIncludeStatementsRecursively( const QString& keyword,
const QString& keywordToStopParsing,
QFile& file,
qint64 startPos,
const std::vector<std::pair<QString, QString>>& pathAliasDefinitions,
QStringList* keywordDataContent,
std::vector<QString>* filenamesContainingKeyword,
bool* isEditKeywordDetected,
const QString& faultIncludeFileAbsolutePathPrefix // rename to
// includeStatementAbsolutePathPrefix
);
const QString& includeStatementAbsolutePathPrefix,
QFile& file,
qint64 startPos,
QStringList& keywordDataContent,
std::vector<QString>& filenamesContainingKeyword,
bool& isStopParsingKeywordDetected );

static RiaDefines::EclipseUnitSystem readUnitSystem( QFile& file, qint64 gridunitPos );

Expand All @@ -136,7 +137,7 @@ class RifEclipseInputFileTools : public cvf::Object
bool* isEditKeywordDetected,
const QString& faultIncludeFileAbsolutePathPrefix );

static void readKeywordDataContent( QFile& data, qint64 filePos, QStringList* textContent, bool* isEditKeywordDetected );
static void readKeywordDataContent( QFile& data, qint64 filePos, QStringList& textContent, bool& isStopParsingKeywordDetected );

static void findGridKeywordPositions( const std::vector<RifKeywordAndFilePos>& keywords,
qint64* coordPos,
Expand Down
Loading

0 comments on commit 13b4c19

Please sign in to comment.