From 091dc196f35bfd7104ccc528e0ae77c093591af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Gr=C3=B8n=C3=A5s=20Drange?= Date: Fri, 6 May 2016 10:00:58 +0200 Subject: [PATCH] Added parse functions for Parser, for simpler creation of state and grids --- .../eclipse/EclipseState/EclipseState.cpp | 4 +- .../eclipse/EclipseState/EclipseState.hpp | 4 +- .../IOConfigIntegrationTest.cpp | 6 +-- opm/parser/eclipse/Parser/ParseContext.cpp | 15 ++++++ opm/parser/eclipse/Parser/ParseContext.hpp | 8 ++++ opm/parser/eclipse/Parser/Parser.cpp | 47 +++++++++++++++++++ opm/parser/eclipse/Parser/Parser.hpp | 22 +++++++++ 7 files changed, 97 insertions(+), 9 deletions(-) diff --git a/opm/parser/eclipse/EclipseState/EclipseState.cpp b/opm/parser/eclipse/EclipseState/EclipseState.cpp index 5f0fbb7ea..b0105a1ce 100644 --- a/opm/parser/eclipse/EclipseState/EclipseState.cpp +++ b/opm/parser/eclipse/EclipseState/EclipseState.cpp @@ -50,11 +50,11 @@ namespace Opm { - EclipseState::EclipseState(std::shared_ptr deckptr, const ParseContext parseContext) : + EclipseState::EclipseState(std::shared_ptr deckptr, ParseContext parseContext) : EclipseState(*deckptr, parseContext) {} - EclipseState::EclipseState(const Deck& deck, const ParseContext parseContext) : + EclipseState::EclipseState(const Deck& deck, ParseContext parseContext) : m_deckUnitSystem( deck.getActiveUnitSystem() ), m_parseContext( parseContext ), m_tables( deck ), diff --git a/opm/parser/eclipse/EclipseState/EclipseState.hpp b/opm/parser/eclipse/EclipseState/EclipseState.hpp index f45a4bf9f..9228800c7 100644 --- a/opm/parser/eclipse/EclipseState/EclipseState.hpp +++ b/opm/parser/eclipse/EclipseState/EclipseState.hpp @@ -69,10 +69,10 @@ namespace Opm { AllProperties = IntProperties | DoubleProperties }; - EclipseState(const Deck& deck , const ParseContext parseContext = ParseContext()); + EclipseState(const Deck& deck , ParseContext parseContext = ParseContext()); /// [deprecated] - EclipseState(std::shared_ptr< const Deck > deck , const ParseContext parseContext = ParseContext()); + EclipseState(std::shared_ptr< const Deck > deck , ParseContext parseContext = ParseContext()); const ParseContext& getParseContext() const; std::shared_ptr< const Schedule > getSchedule() const; diff --git a/opm/parser/eclipse/IntegrationTests/IOConfigIntegrationTest.cpp b/opm/parser/eclipse/IntegrationTests/IOConfigIntegrationTest.cpp index e0229ab58..262ced9c1 100644 --- a/opm/parser/eclipse/IntegrationTests/IOConfigIntegrationTest.cpp +++ b/opm/parser/eclipse/IntegrationTests/IOConfigIntegrationTest.cpp @@ -299,11 +299,7 @@ BOOST_AUTO_TEST_CASE( NorneResttartConfig ) { rptConfig.push_back( std::make_tuple(241 , true , boost::gregorian::date( 2006,10,10)) ); - ParseContext parseContext; - ParserPtr parser(new Parser()); - DeckConstPtr deck = parser->parseFile("testdata/integration_tests/IOConfig/RPTRST_DECK.DATA", parseContext); - EclipseState state( deck , parseContext ); - + auto state = Parser::parse("testdata/integration_tests/IOConfig/RPTRST_DECK.DATA"); verifyRestartConfig(state.getIOConfigConst(), rptConfig); } diff --git a/opm/parser/eclipse/Parser/ParseContext.cpp b/opm/parser/eclipse/Parser/ParseContext.cpp index 140f96b78..6df004a4b 100644 --- a/opm/parser/eclipse/Parser/ParseContext.cpp +++ b/opm/parser/eclipse/Parser/ParseContext.cpp @@ -100,6 +100,19 @@ namespace Opm { return m_errorContexts.end(); } + ParseContext ParseContext::withKey(const std::string& key, InputError::Action action) const { + ParseContext pc(*this); + pc.addKey(key); + pc.updateKey(key, action); + return pc; + } + + ParseContext& ParseContext::withKey(const std::string& key, InputError::Action action) { + this->addKey(key); + this->updateKey(key, action); + return *this; + } + bool ParseContext::hasKey(const std::string& key) const { if (m_errorContexts.find( key ) == m_errorContexts.end()) return false; @@ -218,6 +231,8 @@ namespace Opm { const std::string ParseContext::UNSUPPORTED_INITIAL_THPRES = "UNSUPPORTED_INITIAL_THPRES"; const std::string ParseContext::INTERNAL_ERROR_UNINITIALIZED_THPRES = "INTERNAL_ERROR_UNINITIALIZED_THPRES"; + + const std::string ParseContext::PARSE_MISSING_SECTIONS = "PARSE_MISSING_SECTIONS"; } diff --git a/opm/parser/eclipse/Parser/ParseContext.hpp b/opm/parser/eclipse/Parser/ParseContext.hpp index c0d78a056..ec83c9a9e 100644 --- a/opm/parser/eclipse/Parser/ParseContext.hpp +++ b/opm/parser/eclipse/Parser/ParseContext.hpp @@ -82,6 +82,8 @@ namespace Opm { ParseContext(const std::vector> initial); Message::type handleError( const std::string& errorKey, const std::string& msg ) const; bool hasKey(const std::string& key) const; + ParseContext withKey(const std::string& key, InputError::Action action = InputError::WARN) const; + ParseContext& withKey(const std::string& key, InputError::Action action = InputError::WARN); void updateKey(const std::string& key , InputError::Action action); void update(InputError::Action action); void update(const std::string& keyString , InputError::Action action); @@ -191,6 +193,12 @@ namespace Opm { */ const static std::string INTERNAL_ERROR_UNINITIALIZED_THPRES; + /* + If the deck is partial deck, and thus a full EclipseState is + meaningless, we can still construct a slim EclipseGrid. + */ + const static std::string PARSE_MISSING_SECTIONS; + private: void initDefault(); void initEnv(); diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 1d21fb687..c8cb018b8 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include #include @@ -596,6 +598,51 @@ bool parseState( ParserState& parserState, const Parser& parser ) { return parserState.deck; } + void assertFullDeck(const ParseContext& context) { + if (context.hasKey(ParseContext::PARSE_MISSING_SECTIONS)) + throw new std::logic_error("Cannot construct a state in partial deck context"); + } + + EclipseState Parser::parse(const std::string &filename, const ParseContext& context) { + assertFullDeck(context); + Parser p; + auto deck = p.parseFile(filename, context); + return EclipseState(deck, context); + } + + EclipseState Parser::parse(const Deck& deck, const ParseContext& context) { + assertFullDeck(context); + return EclipseState(deck, context); + } + + EclipseState Parser::parseData(const std::string &data, const ParseContext& context) { + assertFullDeck(context); + Parser p; + auto deck = p.parseString(data, context); + return parse(*deck, context); + } + + std::shared_ptr Parser::parseGrid(const std::string &filename, const ParseContext& context) { + if (context.hasKey(ParseContext::PARSE_MISSING_SECTIONS)) + return std::make_shared(filename); + return parse(filename, context).getInputGrid(); + } + + std::shared_ptr Parser::parseGrid(const Deck& deck, const ParseContext& context) + { + if (context.hasKey(ParseContext::PARSE_MISSING_SECTIONS)) + return std::make_shared(deck); + return parse(deck, context).getInputGrid(); + } + + std::shared_ptr Parser::parseGridData(const std::string &data, const ParseContext& context) { + Parser parser; + auto deck = parser.parseString(data, context); + if (context.hasKey(ParseContext::PARSE_MISSING_SECTIONS)) { + return std::make_shared(deck); + } + return parse(*deck, context).getInputGrid(); + } DeckPtr Parser::parseFile(const std::string &dataFileName, const ParseContext& parseContext) const { return std::shared_ptr( newDeckFromFile( dataFileName , parseContext)); diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 986e1a124..8bbfc3940 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -93,6 +94,27 @@ namespace Opm { addParserKeyword( std::unique_ptr< ParserKeyword >( new T ) ); } + static EclipseState parse(const Deck& deck, const ParseContext& context = ParseContext()); + static EclipseState parse(const std::string &filename, const ParseContext& context = ParseContext()); + static EclipseState parseData(const std::string &data, const ParseContext& context = ParseContext()); + + /// Parses the deck specified in filename. If context contains ParseContext::PARSE_PARTIAL_DECK, + /// we construct only a lean grid, otherwise, we construct a full EclipseState and return the + /// fully constructed InputGrid + static std::shared_ptr parseGrid(const std::string &filename, + const ParseContext& context = ParseContext()); + + /// Parses the provided deck. If context contains ParseContext::PARSE_PARTIAL_DECK, + /// we construct only a lean grid, otherwise, we construct a full EclipseState and return the + /// fully constructed InputGrid + static std::shared_ptr parseGrid(const Deck& deck, + const ParseContext& context = ParseContext()); + + /// Parses the provided deck string. If context contains ParseContext::PARSE_PARTIAL_DECK, + /// we construct only a lean grid, otherwise, we construct a full EclipseState and return the + /// fully constructed InputGrid + static std::shared_ptr parseGridData(const std::string &data, + const ParseContext& context = ParseContext()); private: // associative map of the parser internal name and the corresponding ParserKeyword object