diff --git a/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.cpp b/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.cpp index 6d4764996f..b7467f2bd1 100644 --- a/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.cpp +++ b/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.cpp @@ -1061,7 +1061,7 @@ bool RifOpmFlowDeckFile::stopAtTimeStep( int timeStep ) //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RifOpmFlowDeckFile::replaceKeywordData( const std::string& keyword, const std::vector& data ) +bool RifOpmFlowDeckFile::replaceKeyword( const std::string& keyword, const std::vector& data, bool dataKeyword ) { try { @@ -1073,10 +1073,13 @@ bool RifOpmFlowDeckFile::replaceKeywordData( const std::string& keyword, const s // Get the existing keyword to preserve location const auto& existingKw = m_fileDeck->operator[]( keywordIdx.value() ); + if ( dataKeyword != existingKw.isDataKeyword() ) + { + return false; // Mismatch in data keyword flag + } - // Create a new keyword with the same name and location - Opm::DeckKeyword newKw( existingKw.location(), keyword ); - newKw.setDataKeyword( true ); + // Create a new keyword with the same settings + Opm::DeckKeyword newKw = existingKw.emptyStructuralCopy(); // Keywords ending with "NUM" should be integers (e.g., SATNUM, PVTNUM, EQLNUM) bool isIntType = keyword.size() >= 3 && keyword.substr( keyword.size() - 3 ) == "NUM"; @@ -1123,7 +1126,7 @@ bool RifOpmFlowDeckFile::replaceKeywordData( const std::string& keyword, const s //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RifOpmFlowDeckFile::replaceKeywordData( const std::string& keyword, const std::vector& data ) +bool RifOpmFlowDeckFile::replaceKeyword( const std::string& keyword, const std::vector& data, bool dataKeyword ) { try { @@ -1135,10 +1138,13 @@ bool RifOpmFlowDeckFile::replaceKeywordData( const std::string& keyword, const s // Get the existing keyword to preserve location const auto& existingKw = m_fileDeck->operator[]( keywordIdx.value() ); + if ( dataKeyword != existingKw.isDataKeyword() ) + { + return false; // Mismatch in data keyword flag + } - // Create a new keyword with the same name and location - Opm::DeckKeyword newKw( existingKw.location(), keyword ); - newKw.setDataKeyword( true ); + // Create a new keyword with the same settings + Opm::DeckKeyword newKw = existingKw.emptyStructuralCopy(); // Create a record with the new data Opm::DeckRecord record; diff --git a/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.h b/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.h index 3e238cecdf..287cd93145 100644 --- a/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.h +++ b/ApplicationLibCode/FileInterface/RifOpmFlowDeckFile.h @@ -90,10 +90,10 @@ public: bool addKeyword( const std::string& section, const Opm::DeckKeyword& keyword ); - bool replaceKeywordData( const std::string& keyword, const std::vector& data ); - bool replaceKeywordData( const std::string& keyword, const std::vector& data ); - bool replaceKeyword( const std::string& section, const Opm::DeckKeyword& keyword ); + bool replaceKeyword( const std::string& keyword, const std::vector& data, bool dataKeyword = false ); + bool replaceKeyword( const std::string& keyword, const std::vector& data, bool dataKeyword = false ); + bool replaceAllKeywords( const std::string& keywordName, const std::vector& keywords ); bool replaceKeywordAtIndex( const Opm::FileDeck::Index& index, const Opm::DeckKeyword& keyword ); diff --git a/ApplicationLibCode/ReservoirDataModel/SimulationFile/RigSimulationInputTool.cpp b/ApplicationLibCode/ReservoirDataModel/SimulationFile/RigSimulationInputTool.cpp index 1a7f568fda..b53f2c7e52 100644 --- a/ApplicationLibCode/ReservoirDataModel/SimulationFile/RigSimulationInputTool.cpp +++ b/ApplicationLibCode/ReservoirDataModel/SimulationFile/RigSimulationInputTool.cpp @@ -195,7 +195,7 @@ std::expected RigSimulationInputTool::updateCornerPointGridInDeck static_cast( gridAdapter.cellCountJ() ), static_cast( gridAdapter.cellCountK() ) }; - if ( !deckFile.replaceKeywordData( "DIMENS", dimens ) ) + if ( !deckFile.replaceKeyword( "DIMENS", dimens ) ) { return std::unexpected( "Failed to replace DIMENS keyword in deck file" ); } @@ -204,7 +204,7 @@ std::expected RigSimulationInputTool::updateCornerPointGridInDeck // Format: NX NY NZ (use defaults for NUMRES (1) and COORD_TYPE: 'F'=Cartesian) if ( std::find( keywords.begin(), keywords.end(), "SPECGRID" ) == keywords.end() ) { - Opm::DeckKeyword newKw( Opm::ParserKeyword( "SPECGRID" ) ); + Opm::DeckKeyword newKw( ( Opm::ParserKeywords::SPECGRID() ) ); deckFile.addKeyword( "GRID", newKw ); } @@ -212,7 +212,7 @@ std::expected RigSimulationInputTool::updateCornerPointGridInDeck static_cast( gridAdapter.cellCountJ() ), static_cast( gridAdapter.cellCountK() ) }; - if ( !deckFile.replaceKeywordData( "SPECGRID", specgrid ) ) + if ( !deckFile.replaceKeyword( "SPECGRID", specgrid ) ) { return std::unexpected( "Failed to replace SPECGRID keyword in deck file" ); } @@ -230,21 +230,22 @@ std::expected RigSimulationInputTool::updateCornerPointGridInDeck if ( std::find( keywords.begin(), keywords.end(), "ACTNUM" ) == keywords.end() ) { - Opm::DeckKeyword newKw( Opm::ParserKeyword( "ACTNUM" ) ); + Opm::DeckKeyword newKw( ( Opm::ParserKeywords::ACTNUM() ) ); + newKw.setDataKeyword( true ); // needed due to bug(?) in OPM constructor deckFile.addKeyword( "GRID", newKw ); } - if ( !deckFile.replaceKeywordData( "COORD", coords ) ) + if ( !deckFile.replaceKeyword( "COORD", coords, true /*data kw*/ ) ) { return std::unexpected( "Failed to replace COORD keyword in deck file" ); } - if ( !deckFile.replaceKeywordData( "ZCORN", zcorn ) ) + if ( !deckFile.replaceKeyword( "ZCORN", zcorn, true /*data kw*/ ) ) { return std::unexpected( "Failed to replace ZCORN keyword in deck file" ); } - if ( !deckFile.replaceKeywordData( "ACTNUM", actnumArray ) ) + if ( !deckFile.replaceKeyword( "ACTNUM", actnumArray, true /*data kw*/ ) ) { return std::unexpected( "Failed to replace ACTNUM keyword in deck file" ); } @@ -284,14 +285,14 @@ std::expected RigSimulationInputTool::replaceKeywordValuesInDeckF if ( result ) { // Replace keyword values in deck with extracted data - if ( deckFile.replaceKeywordData( keywordStdStr, result.value() ) ) + if ( deckFile.replaceKeyword( keywordStdStr, result.value(), true /* data kw */ ) ) { RiaLogging::info( QString( "Successfully replaced data for keyword '%1' (%2 values)" ).arg( keyword ).arg( result.value().size() ) ); } else { - RiaLogging::warning( QString( "Failed to replace keyword '%1' in deck" ).arg( keyword ) ); + RiaLogging::debug( QString( "'%1' is not a data keyword, skipping" ).arg( keyword ) ); } } } @@ -1499,12 +1500,13 @@ std::expected RigSimulationInputTool::addOperNumRegionAndOperater if ( std::find( keywords.begin(), keywords.end(), "OPERNUM" ) == keywords.end() ) { Opm::DeckKeyword newKw( ( Opm::ParserKeywords::OPERNUM() ) ); + newKw.setDataKeyword( true ); deckFile.addKeyword( "GRID", newKw ); } } // Replace keyword values in deck with refined exported data - if ( deckFile.replaceKeywordData( "OPERNUM", operNumResult ) ) + if ( deckFile.replaceKeyword( "OPERNUM", operNumResult, true /*data kw*/ ) ) { RiaLogging::info( QString( "Replaced OPERNUM values for refined grid with dimensions %1x%2x%3" ) .arg( gridAdapter.cellCountI() )