From f59fd256683ad6d87ba2b9651b24460808cde704 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Thu, 10 Apr 2014 22:29:18 +0200 Subject: [PATCH] Added boolan flag scalar = true to DeckItem. The purpose of this flag is to keep track of whether a keyword is supposed to have only one element, i.e. scalar, or several. The defaultApplied method only makes sense in the case of scalar items, this method will now throw if it is called on a non-scalar item. --- opm/parser/eclipse/Deck/DeckDoubleItem.hpp | 2 +- opm/parser/eclipse/Deck/DeckFloatItem.hpp | 2 +- opm/parser/eclipse/Deck/DeckIntItem.hpp | 2 +- opm/parser/eclipse/Deck/DeckItem.cpp | 10 +++++-- opm/parser/eclipse/Deck/DeckItem.hpp | 3 +- opm/parser/eclipse/Deck/DeckStringItem.hpp | 2 +- .../eclipse/Deck/tests/DeckIntItemTests.cpp | 14 ++++++++++ .../IntegrationTests/ParseDATAWithDefault.cpp | 28 +++++++++++++++++++ .../eclipse/IntegrationTests/ParsePVTG.cpp | 2 +- .../eclipse/Parser/ParserDoubleItem.cpp | 2 +- opm/parser/eclipse/Parser/ParserFloatItem.cpp | 2 +- opm/parser/eclipse/Parser/ParserIntItem.cpp | 5 ++-- opm/parser/eclipse/Parser/ParserItem.cpp | 7 +++++ opm/parser/eclipse/Parser/ParserItem.hpp | 1 + .../eclipse/Parser/ParserStringItem.cpp | 2 +- .../eclipse/Parser/tests/ParserItemTests.cpp | 8 ++++++ .../Utility/SimpleMultiRecordTable.cpp | 2 +- 17 files changed, 80 insertions(+), 14 deletions(-) diff --git a/opm/parser/eclipse/Deck/DeckDoubleItem.hpp b/opm/parser/eclipse/Deck/DeckDoubleItem.hpp index 45dd5efd9..d9c17bcff 100644 --- a/opm/parser/eclipse/Deck/DeckDoubleItem.hpp +++ b/opm/parser/eclipse/Deck/DeckDoubleItem.hpp @@ -32,7 +32,7 @@ namespace Opm { class DeckDoubleItem : public DeckItem { public: - DeckDoubleItem(std::string name) : DeckItem(name) {} + DeckDoubleItem(std::string name, bool scalar = true) : DeckItem(name , scalar) {} double getRawDouble(size_t index) const; const std::vector& getRawDoubleData() const; double getSIDouble(size_t index) const; diff --git a/opm/parser/eclipse/Deck/DeckFloatItem.hpp b/opm/parser/eclipse/Deck/DeckFloatItem.hpp index 8979d0a34..e8354e113 100644 --- a/opm/parser/eclipse/Deck/DeckFloatItem.hpp +++ b/opm/parser/eclipse/Deck/DeckFloatItem.hpp @@ -32,7 +32,7 @@ namespace Opm { class DeckFloatItem : public DeckItem { public: - DeckFloatItem(std::string name) : DeckItem(name) {} + DeckFloatItem(std::string name , bool scalar = true) : DeckItem(name , scalar) {} float getRawFloat(size_t index) const; //const std::vector& getRawFloatData() const; float getSIFloat(size_t index) const; diff --git a/opm/parser/eclipse/Deck/DeckIntItem.hpp b/opm/parser/eclipse/Deck/DeckIntItem.hpp index 34a954db2..b7c2ccae8 100644 --- a/opm/parser/eclipse/Deck/DeckIntItem.hpp +++ b/opm/parser/eclipse/Deck/DeckIntItem.hpp @@ -30,7 +30,7 @@ namespace Opm { class DeckIntItem : public DeckItem { public: - DeckIntItem(std::string name) : DeckItem(name) {} + DeckIntItem(std::string name , bool scalar = true) : DeckItem(name , scalar) {} int getInt(size_t index) const; const std::vector& getIntData() const; diff --git a/opm/parser/eclipse/Deck/DeckItem.cpp b/opm/parser/eclipse/Deck/DeckItem.cpp index c1c146d03..f10729a70 100644 --- a/opm/parser/eclipse/Deck/DeckItem.cpp +++ b/opm/parser/eclipse/Deck/DeckItem.cpp @@ -19,11 +19,14 @@ #include +#include + namespace Opm { - DeckItem::DeckItem(const std::string& name) { + DeckItem::DeckItem(const std::string& name , bool scalar) { m_name = name; m_defaultApplied = false; + m_scalar = scalar; } const std::string& DeckItem::name() const { @@ -31,7 +34,10 @@ namespace Opm { } bool DeckItem::defaultApplied() const { - return m_defaultApplied; + if (m_scalar) + return m_defaultApplied; + else + throw std::invalid_argument("Tried query deckItem: " + m_name + " if default has been applied - that only applies to scalar items"); } } diff --git a/opm/parser/eclipse/Deck/DeckItem.hpp b/opm/parser/eclipse/Deck/DeckItem.hpp index c74d0873f..8fd259b76 100644 --- a/opm/parser/eclipse/Deck/DeckItem.hpp +++ b/opm/parser/eclipse/Deck/DeckItem.hpp @@ -31,7 +31,7 @@ namespace Opm { class DeckItem { public: - DeckItem(const std::string& name); + DeckItem(const std::string& name , bool m_scalar = true); const std::string& name() const; bool defaultApplied() const; @@ -106,6 +106,7 @@ namespace Opm { bool m_defaultApplied; private: std::string m_name; + bool m_scalar; }; typedef std::shared_ptr DeckItemPtr; diff --git a/opm/parser/eclipse/Deck/DeckStringItem.hpp b/opm/parser/eclipse/Deck/DeckStringItem.hpp index 70681865b..e285f721a 100644 --- a/opm/parser/eclipse/Deck/DeckStringItem.hpp +++ b/opm/parser/eclipse/Deck/DeckStringItem.hpp @@ -31,7 +31,7 @@ namespace Opm { class DeckStringItem : public DeckItem { public: - DeckStringItem(std::string name) : DeckItem(name) { + DeckStringItem(std::string name, bool scalar = true) : DeckItem(name, scalar) { } std::string getString(size_t index) const; const std::vector& getStringData() const; diff --git a/opm/parser/eclipse/Deck/tests/DeckIntItemTests.cpp b/opm/parser/eclipse/Deck/tests/DeckIntItemTests.cpp index 3550eecf6..801d5cb9e 100644 --- a/opm/parser/eclipse/Deck/tests/DeckIntItemTests.cpp +++ b/opm/parser/eclipse/Deck/tests/DeckIntItemTests.cpp @@ -35,6 +35,20 @@ BOOST_AUTO_TEST_CASE(GetIntAtIndex_NoData_ExceptionThrown) { BOOST_CHECK_THROW(deckIntItem.getInt(0), std::out_of_range); } + +BOOST_AUTO_TEST_CASE(InitializeDefaultApplied) { + DeckIntItem deckIntItem("TEST"); + BOOST_REQUIRE_NO_THROW( deckIntItem.defaultApplied() ); +} + + +BOOST_AUTO_TEST_CASE(InitializeDefaultApplied_Throws_for_nonScalar) { + DeckIntItem deckIntItem("TEST" , false); + BOOST_REQUIRE_THROW( deckIntItem.defaultApplied() , std::invalid_argument); +} + + + BOOST_AUTO_TEST_CASE(PushBack_VectorPushed_ElementsCorrect) { DeckIntItem deckIntItem("TEST"); std::deque pushThese; diff --git a/opm/parser/eclipse/IntegrationTests/ParseDATAWithDefault.cpp b/opm/parser/eclipse/IntegrationTests/ParseDATAWithDefault.cpp index 66bf76aba..b5271b9fc 100644 --- a/opm/parser/eclipse/IntegrationTests/ParseDATAWithDefault.cpp +++ b/opm/parser/eclipse/IntegrationTests/ParseDATAWithDefault.cpp @@ -58,3 +58,31 @@ BOOST_AUTO_TEST_CASE( ParseMissingRECORD_THrows) { +const char *data = "\n\ +ENDSCALE\n\ + 1* 1* 2 /\n\ +\n\ +ENKRVD\n\ +100 * 2 3 4 5 6 7 200 11 22 33 44 55 66 77 /\n\ +100 1 2 3 4 5 6 7 200 11 22 33 44 55 66 77 /\n\ +"; + + + +BOOST_AUTO_TEST_CASE( parse_DATAWithDefult_OK ) { + ParserPtr parser(new Parser()); + DeckConstPtr deck = parser->parseString( data ); + DeckKeywordConstPtr keyword = deck->getKeyword( "ENKRVD" ); + DeckRecordConstPtr rec0 = keyword->getRecord(0); + DeckRecordConstPtr rec1 = keyword->getRecord(1); + + DeckItemConstPtr item0 = rec0->getItem(0); + DeckItemConstPtr item1 = rec1->getItem(0); + + BOOST_CHECK_EQUAL( 2U , keyword->size()); + + BOOST_CHECK_THROW( item0->defaultApplied() , std::invalid_argument); +} + + + diff --git a/opm/parser/eclipse/IntegrationTests/ParsePVTG.cpp b/opm/parser/eclipse/IntegrationTests/ParsePVTG.cpp index 9a3e255b8..39683b3c6 100644 --- a/opm/parser/eclipse/IntegrationTests/ParsePVTG.cpp +++ b/opm/parser/eclipse/IntegrationTests/ParsePVTG.cpp @@ -104,7 +104,7 @@ void check_parser(ParserPtr parser) { BOOST_CHECK_EQUAL(9U , item4_1->size()); BOOST_CHECK_EQUAL(2U , record4->size()); - + { Opm::PvtgTable pvtgTable(kw1, 0); diff --git a/opm/parser/eclipse/Parser/ParserDoubleItem.cpp b/opm/parser/eclipse/Parser/ParserDoubleItem.cpp index 9e8bea4e1..183ac06a4 100644 --- a/opm/parser/eclipse/Parser/ParserDoubleItem.cpp +++ b/opm/parser/eclipse/Parser/ParserDoubleItem.cpp @@ -128,7 +128,7 @@ namespace Opm /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserDoubleItem::scan(RawRecordPtr rawRecord) const { - DeckDoubleItemPtr deckItem(new DeckDoubleItem(name())); + DeckDoubleItemPtr deckItem(new DeckDoubleItem(name() , scalar())); double defaultValue = m_default; if (sizeType() == ALL) { // This can probably not be combined with a default value .... diff --git a/opm/parser/eclipse/Parser/ParserFloatItem.cpp b/opm/parser/eclipse/Parser/ParserFloatItem.cpp index 6340e9309..f7320899d 100644 --- a/opm/parser/eclipse/Parser/ParserFloatItem.cpp +++ b/opm/parser/eclipse/Parser/ParserFloatItem.cpp @@ -128,7 +128,7 @@ namespace Opm /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserFloatItem::scan(RawRecordPtr rawRecord) const { - DeckFloatItemPtr deckItem(new DeckFloatItem(name())); + DeckFloatItemPtr deckItem(new DeckFloatItem(name() , scalar())); float defaultValue = m_default; if (sizeType() == ALL) { // This can probably not be combined with a default value .... diff --git a/opm/parser/eclipse/Parser/ParserIntItem.cpp b/opm/parser/eclipse/Parser/ParserIntItem.cpp index d9b1ca064..7a86f55a6 100644 --- a/opm/parser/eclipse/Parser/ParserIntItem.cpp +++ b/opm/parser/eclipse/Parser/ParserIntItem.cpp @@ -62,10 +62,11 @@ namespace Opm { /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserIntItem::scan(RawRecordPtr rawRecord) const { - DeckIntItemPtr deckItem(new DeckIntItem(name())); + DeckIntItemPtr deckItem(new DeckIntItem( name() , scalar() )); int defaultValue = m_default; - if (sizeType() == ALL) { // This can probably not be combined with a default value .... + if (sizeType() == ALL) { + // This can probably not be combined with a default value .... // The '*' should be interpreted as a multiplication sign while (rawRecord->size() > 0) { std::string token = rawRecord->pop_front(); diff --git a/opm/parser/eclipse/Parser/ParserItem.cpp b/opm/parser/eclipse/Parser/ParserItem.cpp index 291b2f256..227157396 100644 --- a/opm/parser/eclipse/Parser/ParserItem.cpp +++ b/opm/parser/eclipse/Parser/ParserItem.cpp @@ -83,6 +83,13 @@ namespace Opm { return m_sizeType; } + bool ParserItem::scalar() const { + if (m_sizeType == SINGLE) + return true; + else + return false; + } + std::string ParserItem::getDescription() const { return m_description; } diff --git a/opm/parser/eclipse/Parser/ParserItem.hpp b/opm/parser/eclipse/Parser/ParserItem.hpp index 44c9c7e8c..533593fc7 100644 --- a/opm/parser/eclipse/Parser/ParserItem.hpp +++ b/opm/parser/eclipse/Parser/ParserItem.hpp @@ -50,6 +50,7 @@ namespace Opm { const std::string& name() const; ParserItemSizeEnum sizeType() const; std::string getDescription() const; + bool scalar() const; void setDescription(std::string helpText); static int defaultInt(); diff --git a/opm/parser/eclipse/Parser/ParserStringItem.cpp b/opm/parser/eclipse/Parser/ParserStringItem.cpp index ea55961e8..7c7619bb2 100644 --- a/opm/parser/eclipse/Parser/ParserStringItem.cpp +++ b/opm/parser/eclipse/Parser/ParserStringItem.cpp @@ -64,7 +64,7 @@ namespace Opm { /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserStringItem::scan(RawRecordPtr rawRecord) const { - DeckStringItemPtr deckItem(new DeckStringItem(name())); + DeckStringItemPtr deckItem(new DeckStringItem(name() , scalar())); std::string defaultValue = m_default; if (sizeType() == ALL) { // This can probably not be combined with a default value .... diff --git a/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp b/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp index 714338e2f..c5e76c9d8 100644 --- a/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserItemTests.cpp @@ -45,6 +45,14 @@ BOOST_AUTO_TEST_CASE(Initialize) { BOOST_CHECK_NO_THROW(ParserFloatItem item1("ITEM1", sizeType)); } +BOOST_AUTO_TEST_CASE(ScalarCheck) { + ParserIntItem item1("ITEM1", SINGLE); + ParserIntItem item2("ITEM1", ALL); + + BOOST_CHECK( item1.scalar()); + BOOST_CHECK( !item2.scalar()); +} + BOOST_AUTO_TEST_CASE(Initialize_DefaultSizeType) { ParserIntItem item1(std::string("ITEM1")); ParserStringItem item2(std::string("ITEM1")); diff --git a/opm/parser/eclipse/Utility/SimpleMultiRecordTable.cpp b/opm/parser/eclipse/Utility/SimpleMultiRecordTable.cpp index 77c1fd435..a953bdf23 100644 --- a/opm/parser/eclipse/Utility/SimpleMultiRecordTable.cpp +++ b/opm/parser/eclipse/Utility/SimpleMultiRecordTable.cpp @@ -73,7 +73,7 @@ size_t SimpleMultiRecordTable::getNumFlatItems_(Opm::DeckRecordConstPtr deckReco int result = 0; for (unsigned i = 0; i < deckRecord->size(); ++ i) { Opm::DeckItemConstPtr item(deckRecord->getItem(i)); - if (item->defaultApplied()) + if (i == 0 && item->defaultApplied()) return result; result += item->size(); }