From 17f246c1a7dd8d97c8ebb8d9df7d7a1f59ca0ca1 Mon Sep 17 00:00:00 2001 From: Steinar Foss Date: Thu, 10 Oct 2019 14:40:32 +0200 Subject: [PATCH] deckkeyword: new constructors takes (ParserKeyword, std::vector). DeckKeyword: test for vector data constructor. DeckKeyword takes std::vector and std::vector. ... ... ... ... --- opm/parser/eclipse/Deck/DeckKeyword.hpp | 3 ++ src/opm/parser/eclipse/Deck/DeckKeyword.cpp | 55 +++++++++++++++++++++ tests/parser/DeckValueTests.cpp | 43 ++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/opm/parser/eclipse/Deck/DeckKeyword.hpp b/opm/parser/eclipse/Deck/DeckKeyword.hpp index 9d8c5bc61..99f7a9d45 100644 --- a/opm/parser/eclipse/Deck/DeckKeyword.hpp +++ b/opm/parser/eclipse/Deck/DeckKeyword.hpp @@ -43,6 +43,9 @@ namespace Opm { DeckKeyword(const ParserKeyword& parserKeyword, const std::vector>& record_list); + DeckKeyword(const ParserKeyword& parserKeyword, const std::vector& data); + DeckKeyword(const ParserKeyword& parserKeyword, const std::vector& data); + const std::string& name() const; void setFixedSize(); void setLocation(const std::pair& location); diff --git a/src/opm/parser/eclipse/Deck/DeckKeyword.cpp b/src/opm/parser/eclipse/Deck/DeckKeyword.cpp index 41226bd89..b74428f0c 100644 --- a/src/opm/parser/eclipse/Deck/DeckKeyword.cpp +++ b/src/opm/parser/eclipse/Deck/DeckKeyword.cpp @@ -111,6 +111,61 @@ namespace Opm { } + + DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector& data) : + DeckKeyword(parserKeyword) + { + if (!parserKeyword.isDataKeyword()) + throw std::invalid_argument("Deckkeyword '" + name() + "' is not a data keyword."); + + const ParserRecord& parser_record = parserKeyword.getRecord(0); + const ParserItem& parser_item = parser_record.get(0); + + setDataKeyword(); + DeckItem item; + if (parser_item.dataType() == type_tag::fdouble) { + item = DeckItem(parser_item.name(), double()); + for (double val : data) + item.push_back(val); + } + else if (parser_item.dataType() == type_tag::integer) { + item = DeckItem(parser_item.name(), int()); + for (int val : data) + item.push_back(val); + } + else + throw std::invalid_argument("Input to DeckKeyword '" + name() + "': cannot be std::vector."); + + DeckRecord deck_record; + deck_record.addItem( std::move(item) ); + addRecord( std::move(deck_record) ); + } + + + DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector& data) : + DeckKeyword(parserKeyword) + { + if (!parserKeyword.isDataKeyword()) + throw std::invalid_argument("Deckkeyword '" + name() + "' is not a data keyword."); + + const ParserRecord& parser_record = parserKeyword.getRecord(0); + const ParserItem& parser_item = parser_record.get(0); + + setDataKeyword(); + if (parser_item.dataType() != type_tag::fdouble) + throw std::invalid_argument("Input to DeckKeyword '" + name() + "': cannot be std::vector."); + + DeckItem item(parser_item.name(), double()); + for (double val : data) + item.push_back(val); + + DeckRecord deck_record; + deck_record.addItem( std::move(item) ); + addRecord( std::move(deck_record) ); + } + + + void DeckKeyword::setFixedSize() { m_slashTerminated = false; } diff --git a/tests/parser/DeckValueTests.cpp b/tests/parser/DeckValueTests.cpp index 8ab42cfbe..83d4eba68 100644 --- a/tests/parser/DeckValueTests.cpp +++ b/tests/parser/DeckValueTests.cpp @@ -122,3 +122,46 @@ BOOST_AUTO_TEST_CASE(DeckKeywordConstructor) { } + + +BOOST_AUTO_TEST_CASE(DeckKeywordVectorInt) { + + Parser parser; + const ParserKeyword& hbnum = parser.getKeyword("HBNUM"); + const ParserKeyword& box = parser.getKeyword("BOX"); + + std::vector data = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + BOOST_CHECK_THROW( DeckKeyword(box, data), std::invalid_argument ); + DeckKeyword hbnum_kw(hbnum, data); + BOOST_CHECK(hbnum_kw.isDataKeyword()); + BOOST_CHECK_EQUAL(hbnum_kw.getDataSize(), 9); + BOOST_CHECK( hbnum_kw.getIntData() == data ); + + std::vector data_double = {1.1, 2.2}; + BOOST_CHECK_THROW(DeckKeyword(hbnum, data_double), std::invalid_argument); +} + + +BOOST_AUTO_TEST_CASE(DeckKeywordVectorDouble) { + + Parser parser; + const ParserKeyword& poro = parser.getKeyword("PORO"); + const ParserKeyword& box = parser.getKeyword("BOX"); + + std::vector data = {1.1, 2.2, 3.3}; + BOOST_CHECK_THROW(DeckKeyword(box, data), std::invalid_argument); + DeckKeyword poro_kw(poro, data); + BOOST_CHECK(poro_kw.isDataKeyword()); + BOOST_CHECK_EQUAL(poro_kw.getDataSize(), 3); + BOOST_CHECK( poro_kw.getRawDoubleData() == data ); + + std::vector data_int = {1, 2}; + poro_kw = DeckKeyword(poro, data_int); + std::vector raw_int = {1.0, 2.0}; + BOOST_CHECK( poro_kw.getRawDoubleData() == raw_int ); +} + + + + +