diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 04be707a1..f4b6e0eac 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -157,7 +157,7 @@ namespace Opm { } targetSize = sizeDefinitionItem->getInt(0); } - return RawKeywordPtr(new RawKeyword(keywordString, targetSize)); + return RawKeywordPtr(new RawKeyword(keywordString, targetSize , parserKeyword->isTableCollection())); } } else { if (strictParsing) { diff --git a/opm/parser/eclipse/Parser/ParserKeyword.cpp b/opm/parser/eclipse/Parser/ParserKeyword.cpp index b747a736a..72a6def71 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.cpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.cpp @@ -44,18 +44,22 @@ namespace Opm { } + ParserKeyword::ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem, bool isTableCollection) { + commonInit(name); + m_isTableCollection = isTableCollection; + initSizeKeyword(sizeKeyword , sizeItem); + } + ParserKeyword::ParserKeyword(const std::string& name, size_t fixedKeywordSize) { commonInit(name); m_keywordSizeType = FIXED; m_fixedSize = fixedKeywordSize; } - - ParserKeyword::ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem) { - commonInit(name); - initSizeKeyword( sizeKeyword , sizeItem ); - } + bool ParserKeyword::isTableCollection() const { + return m_isTableCollection; + } void ParserKeyword::initSize( const Json::JsonObject& jsonConfig ) { // The number of record has been set explicitly with the size: keyword @@ -140,6 +144,7 @@ namespace Opm { m_keywordSizeType = SLASH_TERMINATED; m_isDataKeyword = false; + m_isTableCollection = false; m_name = name; m_record = ParserRecordPtr(new ParserRecord); } @@ -306,7 +311,8 @@ namespace Opm { if ((m_name == other.m_name) && (m_record->equal( *(other.m_record) )) && (m_keywordSizeType == other.m_keywordSizeType) && - (m_isDataKeyword == other.m_isDataKeyword)) + (m_isDataKeyword == other.m_isDataKeyword) && + (m_isTableCollection == other.m_isTableCollection)) { bool equal = false; switch(m_keywordSizeType) { @@ -336,10 +342,13 @@ namespace Opm { os << lhs << " = new ParserKeyword(\"" << m_name << "\");" << std::endl; break; case FIXED: - os << lhs << " = new ParserKeyword(\"" << m_name << "\"," << m_fixedSize << ");" << std::endl; + os << lhs << " = new ParserKeyword(\"" << m_name << "\",(size_t)" << m_fixedSize << ");" << std::endl; break; case OTHER: - os << lhs << " = new ParserKeyword(\"" << m_name << "\",\"" << m_sizeDefinitionPair.first << "\",\"" << m_sizeDefinitionPair.second << "\");" << std::endl; + if (isTableCollection()) + os << lhs << " = new ParserKeyword(\"" << m_name << "\",\"" << m_sizeDefinitionPair.first << "\",\"" << m_sizeDefinitionPair.second << "\" , true);" << std::endl; + else + os << lhs << " = new ParserKeyword(\"" << m_name << "\",\"" << m_sizeDefinitionPair.first << "\",\"" << m_sizeDefinitionPair.second << "\");" << std::endl; break; } diff --git a/opm/parser/eclipse/Parser/ParserKeyword.hpp b/opm/parser/eclipse/Parser/ParserKeyword.hpp index 61803de98..a5f57f95f 100644 --- a/opm/parser/eclipse/Parser/ParserKeyword.hpp +++ b/opm/parser/eclipse/Parser/ParserKeyword.hpp @@ -37,15 +37,19 @@ namespace Opm { ParserKeyword(const char * name); ParserKeyword(const std::string& name); ParserKeyword(const std::string& name, size_t fixedKeywordSize); - ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem); + ParserKeyword(const std::string& name , const std::string& sizeKeyword , const std::string& sizeItem, bool isTableCollection = false); ParserKeyword(const Json::JsonObject& jsonConfig); static bool validName(const std::string& name); ParserRecordPtr getRecord() const; const std::string& getName() const; + size_t getFixedSize() const; bool hasFixedSize() const; + bool isTableCollection() const; + + size_t numItems() const; DeckKeywordPtr parse(RawKeywordConstPtr rawKeyword) const; @@ -63,6 +67,7 @@ namespace Opm { enum ParserKeywordSizeEnum m_keywordSizeType; size_t m_fixedSize; bool m_isDataKeyword; + bool m_isTableCollection; void initData( const Json::JsonObject& jsonConfig ); void initSize( const Json::JsonObject& jsonConfig ); diff --git a/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp b/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp index 75907a3e6..d6bd5134e 100644 --- a/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserKeywordTests.cpp @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(construct_withname_nameSet) { BOOST_AUTO_TEST_CASE(NamedInit) { std::string keyword("KEYWORD"); - ParserKeyword parserKeyword(keyword, 100); + ParserKeyword parserKeyword(keyword, (size_t) 100); BOOST_CHECK_EQUAL(parserKeyword.getName(), keyword); } @@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(ParserKeyword_default_SizeTypedefault) { BOOST_AUTO_TEST_CASE(ParserKeyword_withSize_SizeTypeFIXED) { std::string keyword("KEYWORD"); - ParserKeyword parserKeyword(keyword, 100); + ParserKeyword parserKeyword(keyword, (size_t) 100); BOOST_CHECK_EQUAL(parserKeyword.getSizeType() , FIXED); } @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ParserKeyword_validName) { BOOST_AUTO_TEST_CASE(AddDataKeyword_correctlyConfigured) { - ParserKeyword parserKeyword("PORO" , 1); + ParserKeyword parserKeyword("PORO" , (size_t) 1); ParserIntItemConstPtr item = ParserIntItemConstPtr(new ParserIntItem( "ACTNUM" , ALL , 0 )); BOOST_CHECK_EQUAL( false , parserKeyword.isDataKeyword() ); parserKeyword.addDataItem( item ); @@ -102,7 +102,7 @@ BOOST_AUTO_TEST_CASE(WrongConstructor_addDataItem_throws) { BOOST_AUTO_TEST_CASE(MixingDataAndItems_throws1) { - ParserKeyword parserKeyword("PORO" , 1); + ParserKeyword parserKeyword("PORO" , (size_t) 1); ParserIntItemConstPtr dataItem = ParserIntItemConstPtr(new ParserIntItem( "ACTNUM" , ALL , 0 )); ParserIntItemConstPtr item = ParserIntItemConstPtr(new ParserIntItem( "XXX" , ALL , 0 )); parserKeyword.addDataItem( dataItem ); @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE(MixingDataAndItems_throws1) { BOOST_AUTO_TEST_CASE(MixingDataAndItems_throws2) { - ParserKeyword parserKeyword("PORO" , 1); + ParserKeyword parserKeyword("PORO" , (size_t) 1); ParserIntItemConstPtr dataItem = ParserIntItemConstPtr(new ParserIntItem( "ACTNUM" , ALL , 0 )); ParserIntItemConstPtr item = ParserIntItemConstPtr(new ParserIntItem( "XXX" , ALL , 0 )); parserKeyword.addItem( item ); @@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE(AddDataKeywordFromJson_correctlyConfigured) { BOOST_AUTO_TEST_CASE(constructor_nametoolongwithfixedsize_exceptionthrown) { std::string keyword("KEYWORDTOOLONG"); - BOOST_CHECK_THROW(ParserKeyword parserKeyword(keyword, 100), std::invalid_argument); + BOOST_CHECK_THROW(ParserKeyword parserKeyword(keyword, (size_t) 100), std::invalid_argument); } @@ -267,11 +267,11 @@ BOOST_AUTO_TEST_CASE(constructor_nametoolong_exceptionthrown) { BOOST_AUTO_TEST_CASE(MixedCase) { std::string keyword("KeyWord"); - BOOST_CHECK_THROW(ParserKeyword parserKeyword(keyword, 100), std::invalid_argument); + BOOST_CHECK_THROW(ParserKeyword parserKeyword(keyword, (size_t) 100), std::invalid_argument); } BOOST_AUTO_TEST_CASE(getFixedSize_sizeObjectHasFixedSize_sizeReturned) { - ParserKeywordPtr parserKeyword(new ParserKeyword("JA", 3)); + ParserKeywordPtr parserKeyword(new ParserKeyword("JA", (size_t) 3)); BOOST_CHECK_EQUAL(3U, parserKeyword->getFixedSize()); } @@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(getFixedSize_sizeObjectDoesNotHaveFixedSizeObjectSet_Except BOOST_AUTO_TEST_CASE(hasFixedSize_hasFixedSizeObject_returnstrue) { - ParserKeywordPtr parserKeyword(new ParserKeyword("JA", 2)); + ParserKeywordPtr parserKeyword(new ParserKeyword("JA", (size_t) 2)); BOOST_CHECK(parserKeyword->hasFixedSize()); } @@ -295,6 +295,21 @@ BOOST_AUTO_TEST_CASE(hasFixedSize_sizeObjectDoesNotHaveFixedSize_returnsfalse) { /******/ /* Tables: */ +BOOST_AUTO_TEST_CASE(DefaultIsNot_TableKeyword) { + ParserKeywordPtr parserKeyword(new ParserKeyword("JA")); + BOOST_CHECK(!parserKeyword->isTableCollection()); +} + +BOOST_AUTO_TEST_CASE(ConstructorIsTableCollection) { + ParserKeywordPtr parserKeyword(new ParserKeyword("JA" , "TABDIMS" , "NTPVT" , true)); + const std::pair& sizeKW = parserKeyword->getSizeDefinitionPair(); + BOOST_CHECK(parserKeyword->isTableCollection()); + BOOST_CHECK(!parserKeyword->hasFixedSize()); + + BOOST_CHECK_EQUAL( parserKeyword->getSizeType() , OTHER); + BOOST_CHECK_EQUAL("TABDIMS", sizeKW.first ); + BOOST_CHECK_EQUAL("NTPVT" , sizeKW.second ); +} diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index 3ef422a4e..77a0d3dc8 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -25,22 +25,37 @@ namespace Opm { - RawKeyword::RawKeyword(const std::string& name, size_t fixedSize) { - setKeywordName(name); - m_fixedSizeKeyword = true; - m_fixedSize = fixedSize; - if (fixedSize == 0) - m_isFinished = true; - else - m_isFinished = false; - } RawKeyword::RawKeyword(const std::string& name) { - setKeywordName(name); - m_fixedSizeKeyword = false; - m_isFinished = false; + commonInit(name); } + + RawKeyword::RawKeyword(const std::string& name , size_t inputSize, bool isTableCollection ) { + commonInit(name); + if (isTableCollection) { + m_isTableCollection = true; + m_numTables = inputSize; + } else { + m_fixedSizeKeyword = true; + m_fixedSize = inputSize; + if (m_fixedSize == 0) + m_isFinished = true; + else + m_isFinished = false; + } + } + + + void RawKeyword::commonInit(const std::string& name) { + setKeywordName( name ); + m_isFinished = false; + m_fixedSizeKeyword = false; + m_isTableCollection = false; + m_currentNumTables = 0; + } + + const std::string& RawKeyword::getKeywordName() const { return m_name; } @@ -49,16 +64,29 @@ namespace Opm { return m_records.size(); } + + + /// Important method, being repeatedly called. When a record is terminated, /// it is added to the list of records, and a new record is started. void RawKeyword::addRawRecordString(const std::string& partialRecordString) { - m_partialRecordString += partialRecordString; + m_partialRecordString += " " + partialRecordString; if (!m_fixedSizeKeyword && isTerminator( m_partialRecordString )) { - m_isFinished = true; - m_partialRecordString.clear(); - } else { + if (m_isTableCollection) { + m_currentNumTables += 1; + if (m_currentNumTables == m_numTables) { + m_isFinished = true; + m_partialRecordString.clear(); + } + } else { + m_isFinished = true; + m_partialRecordString.clear(); + } + } + + if (!m_isFinished) { if (RawRecord::isTerminatedRecordString(partialRecordString)) { RawRecordPtr record(new RawRecord(m_partialRecordString)); m_records.push_back(record); @@ -79,6 +107,10 @@ namespace Opm { } + bool RawKeyword::isTableCollection() const { + return m_isTableCollection; + } + RawRecordPtr RawKeyword::getRecord(size_t index) const { if (index < m_records.size()) { return m_records[index]; diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.hpp b/opm/parser/eclipse/RawDeck/RawKeyword.hpp index 2a2173f3f..1898bf71c 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.hpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.hpp @@ -37,7 +37,8 @@ namespace Opm { class RawKeyword { public: RawKeyword(const std::string& name); - RawKeyword(const std::string& name, size_t fixedSize); + RawKeyword(const std::string& name , size_t inputSize , bool isTableCollection = false); + const std::string& getKeywordName() const; void addRawRecordString(const std::string& partialRecordString); size_t size() const; @@ -49,15 +50,21 @@ namespace Opm { bool isPartialRecordStringEmpty() const; bool isFinished() const; + bool isTableCollection() const; private: + bool m_isTableCollection; bool m_isFinished; bool m_fixedSizeKeyword; size_t m_fixedSize; + size_t m_numTables; + size_t m_currentNumTables; std::string m_name; std::vector m_records; std::string m_partialRecordString; + + void commonInit(const std::string& name); void setKeywordName(const std::string& keyword); static bool isValidKeyword(const std::string& keywordCandidate); }; diff --git a/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp b/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp index ac1ee8061..643e354f3 100644 --- a/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp +++ b/opm/parser/eclipse/RawDeck/tests/RawKeywordTests.cpp @@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(isFinished_undef_size) { BOOST_AUTO_TEST_CASE(isFinished_Fixedsize0) { - RawKeyword keyword("TEST" , 0U); + RawKeyword keyword("TEST" ,0U); BOOST_CHECK( keyword.isFinished() ); } @@ -144,3 +144,18 @@ BOOST_AUTO_TEST_CASE(useLine) { BOOST_CHECK( RawKeyword::useLine("/")); } + +BOOST_AUTO_TEST_CASE(isTableCollection) { + RawKeyword keyword1("TEST" , 4U , false); + RawKeyword keyword2("TEST2"); + BOOST_CHECK_EQUAL( false , keyword1.isTableCollection()); + BOOST_CHECK_EQUAL( false , keyword2.isTableCollection()); + } + + +BOOST_AUTO_TEST_CASE(CreateTableCollection) { + RawKeyword keyword1("TEST" , 2, true); + BOOST_CHECK_EQUAL( true , keyword1.isTableCollection()); +} + + diff --git a/opm/parser/eclipse/RawDeck/tests/RawRecordTests.cpp b/opm/parser/eclipse/RawDeck/tests/RawRecordTests.cpp index 7f8381e15..5e5c6f347 100644 --- a/opm/parser/eclipse/RawDeck/tests/RawRecordTests.cpp +++ b/opm/parser/eclipse/RawDeck/tests/RawRecordTests.cpp @@ -93,3 +93,15 @@ BOOST_AUTO_TEST_CASE(Rawrecord_sizeEmpty_OK) { BOOST_CHECK_EQUAL( 0U , record->size()); } + + +BOOST_AUTO_TEST_CASE(Rawrecord_spaceOnlyEmpty_OK) { + Opm::RawRecordPtr record(new Opm::RawRecord(" /")); + BOOST_CHECK_EQUAL( "" , record->getRecordString()); + BOOST_CHECK_EQUAL( 0U , record->size()); +} + + + + +