Skip to content

Commit

Permalink
Merge pull request #1091 from stefoss23/deck_keyword_vector
Browse files Browse the repository at this point in the history
deckkeyword: new constructors takes (ParserKeyword, std::vector).
  • Loading branch information
joakim-hove authored Oct 11, 2019
2 parents d66f0a6 + 17f246c commit 32ac003
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions opm/parser/eclipse/Deck/DeckKeyword.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ namespace Opm {

DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<std::vector<DeckValue>>& record_list);

DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<int>& data);
DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<double>& data);

const std::string& name() const;
void setFixedSize();
void setLocation(const std::pair<const std::string&, std::size_t>& location);
Expand Down
55 changes: 55 additions & 0 deletions src/opm/parser/eclipse/Deck/DeckKeyword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,61 @@ namespace Opm {

}


DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<int>& 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<int>.");

DeckRecord deck_record;
deck_record.addItem( std::move(item) );
addRecord( std::move(deck_record) );
}


DeckKeyword::DeckKeyword(const ParserKeyword& parserKeyword, const std::vector<double>& 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<double>.");

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;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/parser/DeckValueTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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<double> 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<double> 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<int> data_int = {1, 2};
poro_kw = DeckKeyword(poro, data_int);
std::vector<double> raw_int = {1.0, 2.0};
BOOST_CHECK( poro_kw.getRawDoubleData() == raw_int );
}





0 comments on commit 32ac003

Please sign in to comment.