Parser: handles PYINPUT. Python code in eclipse .Data files now possible!!!

test EmbeddedPython: now with actual python code.

parser.cpp: parsing python code.

introduced RawConst::pyinput.

EmbeddedPython: tests with data keyword.
This commit is contained in:
Steinar Foss
2019-11-07 14:48:49 +01:00
parent 277ec850a7
commit f3701c3f93
3 changed files with 59 additions and 10 deletions

View File

@@ -28,6 +28,7 @@
#include <opm/json/JsonObject.hpp>
#include <opm/parser/eclipse/Python/Python.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckItem.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
@@ -349,11 +350,13 @@ class ParserState {
std::map< std::string, std::string > pathMap;
boost::filesystem::path rootPath;
public:
ParserKeywordSizeEnum lastSizeType = SLASH_TERMINATED;
std::string lastKeyWord;
Deck deck;
Python python;
const ParseContext& parseContext;
ErrorGuard& errors;
bool unknown_keyword = false;
@@ -836,12 +839,17 @@ bool parseState( ParserState& parserState, const Parser& parser ) {
OpmLog::info(ss.str());
}
try {
parserState.deck.addKeyword( parserKeyword.parse( parserState.parseContext,
parserState.errors,
*rawKeyword,
parserState.deck.getActiveUnitSystem(),
parserState.deck.getDefaultUnitSystem(),
filename ) );
if (rawKeyword->getKeywordName() == Opm::RawConsts::pyinput) {
std::string python_string = rawKeyword->getFirstRecord().getRecordString();
parserState.python.exec(python_string, parser, parserState.deck);
}
else
parserState.deck.addKeyword( parserKeyword.parse( parserState.parseContext,
parserState.errors,
*rawKeyword,
parserState.deck.getActiveUnitSystem(),
parserState.deck.getDefaultUnitSystem(),
filename ) );
} catch (const std::exception& exc) {
/*
This catch-all of parsing errors is to be able to write a good

View File

@@ -32,6 +32,7 @@ namespace Opm {
const std::string end = "END";
const std::string endinclude = "ENDINC";
const std::string paths = "PATHS";
const std::string pyinput = "PYINPUT";
const unsigned int maxKeywordLength = 8;
/* The lookup uses some bit-tricks to achieve branchless lookup in the

View File

@@ -29,10 +29,12 @@
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
using namespace Opm;
#ifndef EMBEDDED_PYTHON
BOOST_AUTO_TEST_CASE(INSTANTIATE) {
Opm::Python python;
Python python;
BOOST_CHECK(!python);
BOOST_CHECK_THROW(python.exec("print('Hello world')"), std::logic_error);
}
@@ -40,12 +42,12 @@ BOOST_AUTO_TEST_CASE(INSTANTIATE) {
#else
BOOST_AUTO_TEST_CASE(INSTANTIATE) {
Opm::Python python;
Python python;
BOOST_CHECK(python);
BOOST_CHECK_NO_THROW(python.exec("print('Hello world')"));
Opm::Parser parser;
Opm::Deck deck;
Parser parser;
Deck deck;
std::string python_code = R"(
print('Parser: {}'.format(context.parser))
print('Deck: {}'.format(context.deck))
@@ -56,5 +58,43 @@ context.deck.add(kw)
BOOST_CHECK( deck.hasKeyword("FIELD") );
}
BOOST_AUTO_TEST_CASE(PYINPUT_BASIC) {
Parser parser;
std::string input = R"(
START -- 0
31 AUG 1993 /
RUNSPEC
PYINPUT
kw = context.DeckKeyword( context.parser['FIELD'] )
context.deck.add(kw)
<<<
DIMENS
2 2 1 /
PYINPUT
import numpy as np
dx = np.array([0.25, 0.25, 0.25, 0.25])
active_unit_system = context.deck.active_unit_system()
default_unit_system = context.deck.default_unit_system()
kw = context.DeckKeyword( context.parser['DX'], dx, active_unit_system, default_unit_system )
context.deck.add(kw)
<<<
DY
4*0.25 /
)";
Deck deck = parser.parseString(input);
BOOST_CHECK( deck.hasKeyword("START") );
BOOST_CHECK( deck.hasKeyword("FIELD") );
BOOST_CHECK( deck.hasKeyword("DIMENS") );
BOOST_CHECK( deck.hasKeyword("DX") );
auto DX = deck.getKeyword("DX");
std::vector<double> dx_data = DX.getSIDoubleData();
BOOST_CHECK_EQUAL( dx_data.size(), 4 );
BOOST_CHECK_EQUAL( dx_data[2], 0.25 * 0.3048 );
BOOST_CHECK( deck.hasKeyword("DY") );
}
#endif