From fcad042bdde0f316e1eeab4f724acbcab4b29d37 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Mon, 20 Sep 2021 09:03:16 +0200 Subject: [PATCH 1/2] Round numerical month values to integer before comparison --- .../Schedule/Action/ActionValue.hpp | 1 + .../EclipseState/Schedule/Action/ASTNode.cpp | 22 ++++++++++++++++++- .../Schedule/Action/ActionParser.cpp | 2 +- tests/parser/ACTIONX.cpp | 13 +++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/opm/parser/eclipse/EclipseState/Schedule/Action/ActionValue.hpp b/opm/parser/eclipse/EclipseState/Schedule/Action/ActionValue.hpp index 84170374a..375281e50 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/Action/ActionValue.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/Action/ActionValue.hpp @@ -23,6 +23,7 @@ enum TokenType { enum class FuncType { none, time, + time_month, region, field, group, diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.cpp index 5d3a237cc..b1641899d 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -161,7 +162,26 @@ Action::Result ASTNode::eval(const Action::Context& context) const { } auto v1 = this->children[0].value(context); - auto v2 = this->children[1].value(context); + Action::Value v2; + + /* + Special casing of MONTH comparisons where in addition symbolic month names + we can compare with numeric months, in the case of numeric months the + numerical value should be rounded before comparison - i.e. + + MNTH = 4.3 + + Should evaluate to true for the month April. + */ + if (this->children[0].func_type == FuncType::time_month) { + const auto& rhs = this->children[1]; + if (rhs.type == TokenType::number) { + v2 = Action::Value(std::round(rhs.number)); + } else + v2 = rhs.value(context); + } else + v2 = this->children[1].value(context); + return v1.eval_cmp(this->type, v2); } diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Action/ActionParser.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Action/ActionParser.cpp index d16b79c6e..a36f9b696 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Action/ActionParser.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Action/ActionParser.cpp @@ -88,7 +88,7 @@ TokenType Parser::get_type(const std::string& arg) { FuncType Parser::get_func(const std::string& arg) { if (arg == "YEAR") return FuncType::time; - if (arg == "MNTH") return FuncType::time; + if (arg == "MNTH") return FuncType::time_month; if (arg == "DAY") return FuncType::time; using Cat = SummaryConfigNode::Category; diff --git a/tests/parser/ACTIONX.cpp b/tests/parser/ACTIONX.cpp index b590ae53a..5fdf9c476 100644 --- a/tests/parser/ACTIONX.cpp +++ b/tests/parser/ACTIONX.cpp @@ -355,6 +355,19 @@ BOOST_AUTO_TEST_CASE(DATE) { BOOST_CHECK( !ast.eval(context)); } +BOOST_AUTO_TEST_CASE(MNTH_NUMERIC) { + Action::AST ast(std::vector{"MNTH", ">=", "6.3"}); + SummaryState st(TimeService::now()); + WListManager wlm; + Action::Context context(st, wlm); + + context.add("MNTH", 5); + BOOST_CHECK( !ast.eval(context)); + + context.add("MNTH", 6); + BOOST_CHECK( ast.eval(context) ); +} + BOOST_AUTO_TEST_CASE(MANUAL1) { Action::AST ast({"GGPR", "FIELD", ">", "50000", "AND", "WGOR", "PR", ">" ,"GGOR", "FIELD"}); From daaee6d2a4430bdc81d2bcde2ffb500eea132c69 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Mon, 20 Sep 2021 09:21:24 +0200 Subject: [PATCH 2/2] Accept numerical month values when outputting ACTIONX state for rst --- opm/common/utility/TimeService.hpp | 1 + src/opm/common/utility/TimeService.cpp | 9 +++++++++ src/opm/output/eclipse/AggregateActionxData.cpp | 13 ++----------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/opm/common/utility/TimeService.hpp b/opm/common/utility/TimeService.hpp index 5bd2af8ef..87545a6cd 100644 --- a/opm/common/utility/TimeService.hpp +++ b/opm/common/utility/TimeService.hpp @@ -40,6 +40,7 @@ namespace Opm { std::time_t makeUTCTime(std::tm timePoint); const std::unordered_map& eclipseMonthIndices(); const std::unordered_map& eclipseMonthNames(); + int eclipseMonth(const std::string& name); bool valid_month(const std::string& month_name); std::time_t mkdatetime(int in_year, int in_month, int in_day, int hour, int minute, int second); diff --git a/src/opm/common/utility/TimeService.cpp b/src/opm/common/utility/TimeService.cpp index 485a98cd4..5f4134f33 100644 --- a/src/opm/common/utility/TimeService.cpp +++ b/src/opm/common/utility/TimeService.cpp @@ -107,6 +107,15 @@ const std::unordered_map& eclipseMonthIndices() { return month_indices; } +int eclipseMonth(const std::string& name) { + auto iter = month_indices.find(name); + if (iter != month_indices.end()) + return iter->second; + + return std::stod(name); +} + + const std::unordered_map& eclipseMonthNames() { return month_names; } diff --git a/src/opm/output/eclipse/AggregateActionxData.cpp b/src/opm/output/eclipse/AggregateActionxData.cpp index bbbb7c099..dc9e9c004 100644 --- a/src/opm/output/eclipse/AggregateActionxData.cpp +++ b/src/opm/output/eclipse/AggregateActionxData.cpp @@ -454,17 +454,8 @@ const std::map logicalToIndex_17 = { if (it_rhsq == rhsQuantityToIndex.end()) { //come here if constant value condition double t_val = 0.; - if (lhsQtype == "M") { - const auto& monthToNo = Opm::TimeService::eclipseMonthIndices(); - const auto& it_mnth = monthToNo.find(condition.rhs.quantity); - if (it_mnth != monthToNo.end()) { - t_val = it_mnth->second; - } - else { - std::cout << "Unknown Month: " << condition.rhs.quantity << std::endl; - throw std::invalid_argument("Actionx: " + action.name() + " Condition: " + condition.lhs.quantity ); - } - } + if (lhsQtype == "M") + t_val = Opm::TimeService::eclipseMonth(condition.rhs.quantity); else { t_val = std::stod(condition.rhs.quantity); }