Skip to content

Commit

Permalink
Merge pull request #1161 from joakim-hove/aqutab-parsing
Browse files Browse the repository at this point in the history
Added optional shift attribute in keyword size configuration.
  • Loading branch information
joakim-hove authored Nov 28, 2017
2 parents d7dbc99 + 4fc709d commit 6f198ea
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 37 deletions.
16 changes: 8 additions & 8 deletions lib/eclipse/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,30 +433,30 @@ 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(),
targetSize,
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(),
Expand Down
26 changes: 14 additions & 12 deletions lib/eclipse/Parser/ParserKeyword.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -187,16 +187,20 @@ namespace Opm {



void ParserKeyword::initSizeKeyword(const std::string& sizeKeyword, const std::string& sizeItem) {
m_sizeDefinitionPair = std::pair<std::string, std::string>(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;
}

void ParserKeyword::initSizeKeyword(const Json::JsonObject& sizeObject) {
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() );
}
Expand Down Expand Up @@ -518,10 +522,10 @@ void set_dimensions( ParserItem& item,
return m_keywordSizeType;
}

const std::pair<std::string, std::string>& 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 32 additions & 3 deletions lib/eclipse/include/opm/parser/eclipse/Parser/ParserKeyword.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,48 @@ 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);

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<std::string> DeckNameSet;
Expand Down Expand Up @@ -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<std::string,std::string>& getSizeDefinitionPair() const;
const KeywordSize& getKeywordSize() const;
bool isDataKeyword() const;

std::string createDeclaration(const std::string& indent) const;
Expand All @@ -108,7 +137,7 @@ namespace Opm {
bool operator!=( const ParserKeyword& ) const;

private:
std::pair<std::string,std::string> m_sizeDefinitionPair;
KeywordSize keyword_size;
std::string m_name;
DeckNameSet m_deckNames;
DeckNameSet m_validSectionNames;
Expand Down
11 changes: 7 additions & 4 deletions lib/eclipse/share/keywords/000_Eclipse100/A/AQUTAB
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{"name" : "AQUTAB" , "sections" : ["PROPS"],
"items" : [
{"name" : "TD" , "value_type" : "DOUBLE"},
{"name" : "PD" , "value_type" : "DOUBLE"}]}
{"name" : "AQUTAB" ,
"sections" : ["PROPS"],
"size" : {"keyword" : "AQUDIMS" , "item" : "NIFTBL", "shift" : -1},
"items" : [{"name" : "table" ,
"value_type" : "DOUBLE" ,
"size_type" : "ALL",
"dimension" : ["1" , "1"]}]}
44 changes: 34 additions & 10 deletions lib/eclipse/tests/ParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::shared_ptr<ParserKeyword> createTable(const std::string& name,
const std::string& sizeItem,
bool isTableCollection) {
std::shared_ptr<ParserKeyword> pkw = std::make_shared<ParserKeyword>(name);
pkw->initSizeKeyword(sizeKeyword , sizeItem);
pkw->initSizeKeyword(sizeKeyword , sizeItem, 0);
pkw->setTableCollection(isTableCollection);
return pkw;
}
Expand Down Expand Up @@ -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<std::string,std::string>& 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) {
Expand Down Expand Up @@ -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<const ParserKeyword>(jsonObject);
const std::pair<std::string,std::string>& 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) {
Expand Down Expand Up @@ -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<std::string,std::string>& 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) {
Expand Down Expand Up @@ -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());
}

0 comments on commit 6f198ea

Please sign in to comment.