From 4fc709dcf064796d03c5c02aeaaac82cd672f88c Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 28 Nov 2017 10:09:41 +0100 Subject: [PATCH] Added optional shift attribute in keyword size configuration. --- lib/eclipse/Parser/Parser.cpp | 16 +++---- lib/eclipse/Parser/ParserKeyword.cpp | 26 ++++++----- .../parser/eclipse/Parser/ParserKeyword.hpp | 35 +++++++++++++-- .../share/keywords/000_Eclipse100/A/AQUTAB | 11 +++-- lib/eclipse/tests/ParserTests.cpp | 44 ++++++++++++++----- 5 files changed, 95 insertions(+), 37 deletions(-) diff --git a/lib/eclipse/Parser/Parser.cpp b/lib/eclipse/Parser/Parser.cpp index d8c8b99c5..783c9ae3d 100644 --- a/lib/eclipse/Parser/Parser.cpp +++ b/lib/eclipse/Parser/Parser.cpp @@ -433,13 +433,13 @@ std::shared_ptr< RawKeyword > createRawKeyword( const string_view& kw, ParserSta parserKeyword->isTableCollection() ); } - const auto& sizeKeyword = parserKeyword->getSizeDefinitionPair(); + const auto& keyword_size = parserKeyword->getKeywordSize(); const auto& deck = parserState.deck; - if( deck.hasKeyword(sizeKeyword.first ) ) { - const auto& sizeDefinitionKeyword = deck.getKeyword(sizeKeyword.first); + if( deck.hasKeyword(keyword_size.keyword ) ) { + const auto& sizeDefinitionKeyword = deck.getKeyword(keyword_size.keyword); const auto& record = sizeDefinitionKeyword.getRecord(0); - const auto targetSize = record.getItem( sizeKeyword.second ).get< int >( 0 ); + const auto targetSize = record.getItem( keyword_size.item ).get< int >( 0 ) + keyword_size.shift; return std::make_shared< RawKeyword >( keywordString, parserState.current_path().string(), parserState.line(), @@ -447,16 +447,16 @@ std::shared_ptr< RawKeyword > createRawKeyword( const string_view& kw, ParserSta parserKeyword->isTableCollection() ); } - std::string msg = "Expected the kewyord: " + sizeKeyword.first + std::string msg = "Expected the kewyord: " +keyword_size.keyword + " to infer the number of records in: " + keywordString; auto& msgContainer = parserState.deck.getMessageContainer(); parserState.parseContext.handleError(ParseContext::PARSE_MISSING_DIMS_KEYWORD , msgContainer, msg ); - const auto* keyword = parser.getKeyword( sizeKeyword.first ); + const auto* keyword = parser.getKeyword( keyword_size.keyword ); const auto& record = keyword->getRecord(0); - const auto& int_item = record.get( sizeKeyword.second ); + const auto& int_item = record.get( keyword_size.item); - const auto targetSize = int_item.getDefault< int >( ); + const auto targetSize = int_item.getDefault< int >( ) + keyword_size.shift; return std::make_shared< RawKeyword >( keywordString, parserState.current_path().string(), parserState.line(), diff --git a/lib/eclipse/Parser/ParserKeyword.cpp b/lib/eclipse/Parser/ParserKeyword.cpp index b57435fae..fa69d7c85 100644 --- a/lib/eclipse/Parser/ParserKeyword.cpp +++ b/lib/eclipse/Parser/ParserKeyword.cpp @@ -66,11 +66,11 @@ namespace Opm { } - ParserKeyword::ParserKeyword(const std::string& name, const std::string& sizeKeyword, const std::string& sizeItem, bool _isTableCollection) + ParserKeyword::ParserKeyword(const std::string& name, const std::string& sizeKeyword, const std::string& sizeItem, int size_shift, bool _isTableCollection) { commonInit( name , OTHER_KEYWORD_IN_DECK); m_isTableCollection = _isTableCollection; - initSizeKeyword(sizeKeyword, sizeItem); + initSizeKeyword(sizeKeyword, sizeItem, size_shift); } void ParserKeyword::clearDeckNames() { @@ -187,8 +187,8 @@ namespace Opm { - void ParserKeyword::initSizeKeyword(const std::string& sizeKeyword, const std::string& sizeItem) { - m_sizeDefinitionPair = std::pair(sizeKeyword, sizeItem); + void ParserKeyword::initSizeKeyword(const std::string& sizeKeyword, const std::string& sizeItem, int size_shift) { + keyword_size = KeywordSize(sizeKeyword, sizeItem, size_shift); m_keywordSizeType = OTHER_KEYWORD_IN_DECK; } @@ -196,7 +196,11 @@ namespace Opm { if (sizeObject.is_object()) { std::string sizeKeyword = sizeObject.get_string("keyword"); std::string sizeItem = sizeObject.get_string("item"); - initSizeKeyword(sizeKeyword, sizeItem); + int size_shift = 0; + if (sizeObject.has_item("shift")) + size_shift = sizeObject.get_int("shift"); + + initSizeKeyword(sizeKeyword, sizeItem, size_shift); } else { m_keywordSizeType = ParserKeywordSizeEnumFromString( sizeObject.as_string() ); } @@ -518,10 +522,10 @@ void set_dimensions( ParserItem& item, return m_keywordSizeType; } - const std::pair& ParserKeyword::getSizeDefinitionPair() const { - return m_sizeDefinitionPair; - } + const KeywordSize& ParserKeyword::getKeywordSize() const { + return keyword_size; + } bool ParserKeyword::isDataKeyword() const { if( this->m_records.empty() ) return false; @@ -610,7 +614,7 @@ void set_dimensions( ParserItem& item, break; case OTHER_KEYWORD_IN_DECK: ss << "setSizeType(" << sizeString << ");" << std::endl; - ss << indent << "initSizeKeyword(\"" << m_sizeDefinitionPair.first << "\",\"" << m_sizeDefinitionPair.second << "\");" << std::endl; + ss << indent << "initSizeKeyword(\"" << keyword_size.keyword << "\",\"" << keyword_size.item << "\"," << keyword_size.shift << ");" << std::endl; if (m_isTableCollection) ss << "setTableCollection( true );" << std::endl; break; @@ -714,11 +718,9 @@ void set_dimensions( ParserItem& item, break; case OTHER_KEYWORD_IN_DECK: - if( m_sizeDefinitionPair.first != rhs.m_sizeDefinitionPair.first - || m_sizeDefinitionPair.second != rhs.m_sizeDefinitionPair.second ) + if (this->keyword_size != rhs.keyword_size) return false; break; - default: break; } diff --git a/lib/eclipse/include/opm/parser/eclipse/Parser/ParserKeyword.hpp b/lib/eclipse/include/opm/parser/eclipse/Parser/ParserKeyword.hpp index 053a1e5cd..e3aefa897 100644 --- a/lib/eclipse/include/opm/parser/eclipse/Parser/ParserKeyword.hpp +++ b/lib/eclipse/include/opm/parser/eclipse/Parser/ParserKeyword.hpp @@ -42,11 +42,40 @@ namespace Opm { class string_view; class MessageContainer; + /* + Small helper struct to assemble the information needed to infer the size + of a keyword based on another keyword in the deck. + */ + struct KeywordSize { + KeywordSize(const std::string& in_keyword, const std::string& in_item, int in_shift) : + keyword(in_keyword), + item(in_item), + shift(in_shift) + {} + + KeywordSize() {} + + bool operator==(const KeywordSize& other) const { + return ((this->keyword == other.keyword) && + (this->item == other.item) && + (this->shift == other.shift)); + } + + bool operator!=(const KeywordSize& other) const { + return !(*this == other); + } + + std::string keyword; + std::string item; + int shift; + }; + class ParserKeyword { public: ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem, + int size_shift, bool _isTableCollection = false); explicit ParserKeyword(const std::string& name); explicit ParserKeyword(const Json::JsonObject& jsonConfig); @@ -54,7 +83,7 @@ namespace Opm { void setFixedSize( size_t keywordSize); void setSizeType( ParserKeywordSizeEnum sizeType ); void setTableCollection(bool _isTableCollection); - void initSizeKeyword( const std::string& sizeKeyword, const std::string& sizeItem); + void initSizeKeyword( const std::string& sizeKeyword, const std::string& sizeItem, int size_shift); typedef std::set DeckNameSet; @@ -96,7 +125,7 @@ namespace Opm { DeckKeyword parse(const ParseContext& parseContext , MessageContainer& msgContainer, std::shared_ptr< RawKeyword > rawKeyword) const; enum ParserKeywordSizeEnum getSizeType() const; - const std::pair& getSizeDefinitionPair() const; + const KeywordSize& getKeywordSize() const; bool isDataKeyword() const; std::string createDeclaration(const std::string& indent) const; @@ -108,7 +137,7 @@ namespace Opm { bool operator!=( const ParserKeyword& ) const; private: - std::pair m_sizeDefinitionPair; + KeywordSize keyword_size; std::string m_name; DeckNameSet m_deckNames; DeckNameSet m_validSectionNames; diff --git a/lib/eclipse/share/keywords/000_Eclipse100/A/AQUTAB b/lib/eclipse/share/keywords/000_Eclipse100/A/AQUTAB index 1fa821b96..43db7698b 100755 --- a/lib/eclipse/share/keywords/000_Eclipse100/A/AQUTAB +++ b/lib/eclipse/share/keywords/000_Eclipse100/A/AQUTAB @@ -1,4 +1,7 @@ -{"name" : "AQUTAB" , "sections" : ["PROPS"], - "items" : [ - {"name" : "TD" , "value_type" : "DOUBLE"}, - {"name" : "PD" , "value_type" : "DOUBLE"}]} \ No newline at end of file +{"name" : "AQUTAB" , + "sections" : ["PROPS"], + "size" : {"keyword" : "AQUDIMS" , "item" : "NIFTBL", "shift" : -1}, +"items" : [{"name" : "table" , + "value_type" : "DOUBLE" , + "size_type" : "ALL", + "dimension" : ["1" , "1"]}]} diff --git a/lib/eclipse/tests/ParserTests.cpp b/lib/eclipse/tests/ParserTests.cpp index c35dff9c9..8802853bf 100644 --- a/lib/eclipse/tests/ParserTests.cpp +++ b/lib/eclipse/tests/ParserTests.cpp @@ -57,7 +57,7 @@ std::shared_ptr createTable(const std::string& name, const std::string& sizeItem, bool isTableCollection) { std::shared_ptr pkw = std::make_shared(name); - pkw->initSizeKeyword(sizeKeyword , sizeItem); + pkw->initSizeKeyword(sizeKeyword , sizeItem, 0); pkw->setTableCollection(isTableCollection); return pkw; } @@ -1317,10 +1317,10 @@ BOOST_AUTO_TEST_CASE(ParserKeyword_withSize_SizeTypeFIXED) { BOOST_AUTO_TEST_CASE(ParserKeyword_withOtherSize_SizeTypeOTHER) { std::string keyword("KEYWORD"); const auto& parserKeyword = createTable(keyword, "EQUILDIMS" , "NTEQUIL" , false); - const std::pair& sizeKW = parserKeyword->getSizeDefinitionPair(); + const auto& keyword_size = parserKeyword->getKeywordSize(); BOOST_CHECK_EQUAL(OTHER_KEYWORD_IN_DECK , parserKeyword->getSizeType() ); - BOOST_CHECK_EQUAL("EQUILDIMS", sizeKW.first ); - BOOST_CHECK_EQUAL("NTEQUIL" , sizeKW.second ); + BOOST_CHECK_EQUAL("EQUILDIMS", keyword_size.keyword ); + BOOST_CHECK_EQUAL("NTEQUIL" , keyword_size.item ); } BOOST_AUTO_TEST_CASE(ParserKeyword_validDeckName) { @@ -1452,12 +1452,12 @@ BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_nosize_notItems_OK) { BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_withSizeOther) { Json::JsonObject jsonObject("{\"name\": \"BPR\", \"sections\":[\"SUMMARY\"], \"size\" : {\"keyword\" : \"Bjarne\" , \"item\": \"BjarneIgjen\"}, \"items\" :[{\"name\":\"ItemX\" , \"value_type\" : \"DOUBLE\"}]}"); const auto& parserKeyword = std::make_shared(jsonObject); - const std::pair& sizeKW = parserKeyword->getSizeDefinitionPair(); + const auto& keyword_size = parserKeyword->getKeywordSize(); BOOST_CHECK_EQUAL("BPR" , parserKeyword->getName()); BOOST_CHECK_EQUAL( false , parserKeyword->hasFixedSize() ); BOOST_CHECK_EQUAL(OTHER_KEYWORD_IN_DECK , parserKeyword->getSizeType()); - BOOST_CHECK_EQUAL("Bjarne", sizeKW.first ); - BOOST_CHECK_EQUAL("BjarneIgjen" , sizeKW.second ); + BOOST_CHECK_EQUAL("Bjarne", keyword_size.keyword); + BOOST_CHECK_EQUAL("BjarneIgjen" , keyword_size.item ); } BOOST_AUTO_TEST_CASE(ConstructFromJsonObject_missingName_throws) { @@ -1607,13 +1607,13 @@ BOOST_AUTO_TEST_CASE(DefaultIsNot_TableKeyword) { BOOST_AUTO_TEST_CASE(ConstructorIsTableCollection) { const auto& parserKeyword = createTable("JA" , "TABDIMS" , "NTPVT" , true); - const std::pair& sizeKW = parserKeyword->getSizeDefinitionPair(); BOOST_CHECK(parserKeyword->isTableCollection()); BOOST_CHECK(!parserKeyword->hasFixedSize()); + const auto& keyword_size = parserKeyword->getKeywordSize(); BOOST_CHECK_EQUAL( parserKeyword->getSizeType() , OTHER_KEYWORD_IN_DECK); - BOOST_CHECK_EQUAL("TABDIMS", sizeKW.first ); - BOOST_CHECK_EQUAL("NTPVT" , sizeKW.second ); + BOOST_CHECK_EQUAL("TABDIMS", keyword_size.keyword ); + BOOST_CHECK_EQUAL("NTPVT" , keyword_size.item); } BOOST_AUTO_TEST_CASE(ParseEmptyRecord) { @@ -1849,3 +1849,27 @@ PVT-M BOOST_CHECK( deck.hasKeyword( "LAB" ) ); BOOST_CHECK( deck.hasKeyword( "PVT-M" ) ); } + + + +BOOST_AUTO_TEST_CASE(ParseAQUTAB) { + const auto * deck_string = R"( +RUNSPEC + +AQUDIMS + * * 2 / + +PROPS + +AQUTAB + 0 1 + 0.10 1.1 + 0.20 1.2 / +)"; + + Parser parser; + const auto deck = parser.parseString( deck_string, ParseContext()); + const auto& aqutab = deck.getKeyword("AQUTAB"); + BOOST_CHECK_EQUAL( 1, aqutab.size()); +} +