From 539c86c21cabeb29eb3ac857b3bc30083570af76 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 8 Sep 2021 08:51:50 +0200 Subject: [PATCH] Move action condition related enums to separate compilation unit --- CMakeLists_files.cmake | 2 + .../Schedule/Action/Condition.hpp | 18 +---- .../EclipseState/Schedule/Action/Enums.hpp | 51 ++++++++++++ .../output/eclipse/AggregateActionxData.cpp | 13 +-- .../Schedule/Action/Condition.cpp | 80 ++++-------------- .../EclipseState/Schedule/Action/Enums.cpp | 81 +++++++++++++++++++ tests/parser/ACTIONX.cpp | 24 +++--- 7 files changed, 172 insertions(+), 97 deletions(-) create mode 100644 opm/parser/eclipse/EclipseState/Schedule/Action/Enums.hpp create mode 100644 src/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 73d50a162..0a41522a1 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -104,6 +104,7 @@ if(ENABLE_ECL_INPUT) src/opm/parser/eclipse/EclipseState/Schedule/Action/ActionValue.cpp src/opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.cpp src/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.cpp + src/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.cpp src/opm/parser/eclipse/EclipseState/Schedule/Action/State.cpp src/opm/parser/eclipse/EclipseState/Schedule/ArrayDimChecker.cpp src/opm/parser/eclipse/EclipseState/Schedule/eval_uda.cpp @@ -761,6 +762,7 @@ if(ENABLE_ECL_INPUT) opm/parser/eclipse/EclipseState/Schedule/Action/Actions.hpp opm/parser/eclipse/EclipseState/Schedule/Action/ActionX.hpp opm/parser/eclipse/EclipseState/Schedule/Action/Condition.hpp + opm/parser/eclipse/EclipseState/Schedule/Action/Enums.hpp opm/parser/eclipse/EclipseState/Schedule/Action/ASTNode.hpp opm/parser/eclipse/EclipseState/Schedule/Action/PyAction.hpp opm/parser/eclipse/EclipseState/Schedule/Action/State.hpp diff --git a/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.hpp b/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.hpp index 16496431c..e7e152f77 100644 --- a/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.hpp +++ b/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.hpp @@ -24,6 +24,7 @@ #include #include +#include namespace Opm { @@ -63,23 +64,8 @@ public: class Condition { public: -enum class Logical { - AND, - OR, - END -}; - -enum class Comparator { - EQUAL, - GREATER, - LESS, - GREATER_EQUAL, - LESS_EQUAL, - INVALID -}; - - Condition() = default; + explicit Condition(const RestartIO::RstAction::Condition& rst_condition); Condition(const std::vector& tokens, const KeywordLocation& location); diff --git a/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.hpp b/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.hpp new file mode 100644 index 000000000..e224124e2 --- /dev/null +++ b/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.hpp @@ -0,0 +1,51 @@ +/* + Copyright 2021 Equinor ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#ifndef ACTION_ENUMS_HPP +#define ACTION_ENUMS_HPP + +#include + +namespace Opm { +namespace Action { + +enum class Logical { + AND, + OR, + END +}; + +enum class Comparator { + EQUAL, + GREATER, + LESS, + GREATER_EQUAL, + LESS_EQUAL, + INVALID +}; + + +Comparator comparator_from_int(int cmp_int); +std::string comparator_as_string(Comparator cmp); +Logical logic_from_int(int int_logic); + +} +} + +#endif diff --git a/src/opm/output/eclipse/AggregateActionxData.cpp b/src/opm/output/eclipse/AggregateActionxData.cpp index af75bd08d..89b1ff24e 100644 --- a/src/opm/output/eclipse/AggregateActionxData.cpp +++ b/src/opm/output/eclipse/AggregateActionxData.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -73,7 +74,7 @@ namespace { {"Y", 0}, };*/ - using cmp_enum = Opm::Action::Condition::Comparator; + using cmp_enum = Opm::Action::Comparator; const std::map cmpToIacn_12 = { {cmp_enum::GREATER, 0}, {cmp_enum::LESS, 1}, @@ -90,7 +91,7 @@ const std::map rhsQuantityToIndex = { {"G", 3}, }; -using logic_enum = Opm::Action::Condition::Logical; +using logic_enum = Opm::Action::Logical; const std::map logicalToIndex_17 = { {logic_enum::AND, 1}, @@ -331,7 +332,7 @@ const std::map logicalToIndex_17 = { int first_greater = 0; { const auto& first_cond = actx_cond[0]; - if (first_cond.cmp == Opm::Action::Condition::Comparator::LESS) + if (first_cond.cmp == Opm::Action::Comparator::LESS) first_greater = 1; } @@ -378,7 +379,7 @@ const std::map logicalToIndex_17 = { parenFirstCond = true; insideParen = true; } - if (cond_it->logic == Opm::Action::Condition::Logical::AND) { + if (cond_it->logic == Opm::Action::Logical::AND) { allPrevLogicOp_AND = true; } } else { @@ -402,9 +403,9 @@ const std::map logicalToIndex_17 = { } // update the previous logic-sequence - if (parenFirstCond && cond_it->logic == Opm::Action::Condition::Logical::OR) { + if (parenFirstCond && cond_it->logic == Opm::Action::Logical::OR) { allPrevLogicOp_AND = false; - } else if (!insideParen && cond_it->logic == Opm::Action::Condition::Logical::OR) { + } else if (!insideParen && cond_it->logic == Opm::Action::Logical::OR) { allPrevLogicOp_AND = false; } } diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.cpp index ca6f95ef4..42c31d5a0 100644 --- a/src/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.cpp +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Action/Condition.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "ActionParser.hpp" @@ -34,44 +35,26 @@ namespace Action { namespace { -Condition::Comparator comparator(TokenType tt) { +Comparator comparator(TokenType tt) { if (tt == TokenType::op_eq) - return Condition::Comparator::EQUAL; + return Comparator::EQUAL; if (tt == TokenType::op_gt) - return Condition::Comparator::GREATER; + return Comparator::GREATER; if (tt == TokenType::op_lt) - return Condition::Comparator::LESS; + return Comparator::LESS; if (tt == TokenType::op_le) - return Condition::Comparator::LESS_EQUAL; + return Comparator::LESS_EQUAL; if (tt == TokenType::op_ge) - return Condition::Comparator::GREATER_EQUAL; + return Comparator::GREATER_EQUAL; - return Condition::Comparator::INVALID; + return Comparator::INVALID; } -std::string cmp2string(Condition::Comparator cmp) { - if (cmp == Condition::Comparator::EQUAL) - return "="; - - if (cmp == Condition::Comparator::GREATER) - return ">"; - - if (cmp == Condition::Comparator::LESS) - return "<"; - - if (cmp == Condition::Comparator::LESS_EQUAL) - return "<="; - - if (cmp == Condition::Comparator::GREATER_EQUAL) - return ">="; - - throw std::logic_error("Bug in opm/flow - should not be here"); -} std::string strip_quotes(const std::string& s) { if (s[0] == '\'') @@ -146,7 +129,7 @@ Condition::Condition(const std::vector& tokens, const KeywordLocati token_index += 1; } else { this->cmp = comp; - this->cmp_string = cmp2string(this->cmp); + this->cmp_string = comparator_as_string(this->cmp); token_index += 1; break; } @@ -164,9 +147,9 @@ Condition::Condition(const std::vector& tokens, const KeywordLocati auto token_type = Parser::get_type(tokens[token_index]); if (token_type == TokenType::op_and) - this->logic = Condition::Logical::AND; + this->logic = Logical::AND; else if (token_type == TokenType::op_or) - this->logic = Condition::Logical::OR; + this->logic = Logical::OR; else if (token_type == TokenType::close_paren) this->right_paren = true; else @@ -207,19 +190,6 @@ int Condition::paren_as_int() const { } -Condition::Logical Condition::logic_from_int(int int_logic) { - if (int_logic == 0) - return Logical::END; - - if (int_logic == 1) - return Logical::AND; - - if (int_logic == 2) - return Logical::OR; - - throw std::logic_error("Unknown integer value"); -} - int Condition::logic_as_int() const { switch (this->logic) { case Logical::END: @@ -233,37 +203,21 @@ int Condition::logic_as_int() const { } } -Condition::Comparator Condition::comparator_from_int(int cmp_int) { - switch (cmp_int) { - case 1: - return Condition::Comparator::GREATER; - case 2: - return Condition::Comparator::LESS; - case 3: - return Condition::Comparator::GREATER_EQUAL; - case 4: - return Condition::Comparator::LESS_EQUAL; - case 5: - return Condition::Comparator::EQUAL; - default: - throw std::logic_error("What the f...?"); - } -} int Condition::comparator_as_int() const { switch (this->cmp) { - case Condition::Comparator::GREATER: + case Comparator::GREATER: return 1; - case Condition::Comparator::LESS: + case Comparator::LESS: return 2; - case Condition::Comparator::GREATER_EQUAL: + case Comparator::GREATER_EQUAL: return 3; - case Condition::Comparator::LESS_EQUAL: + case Comparator::LESS_EQUAL: return 4; - case Condition::Comparator::EQUAL: + case Comparator::EQUAL: return 5; default: - throw std::logic_error("What the f...?"); + throw std::logic_error(fmt::format("Unhandeled value: {} in enum comparison", static_cast(this->cmp))); } } diff --git a/src/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.cpp b/src/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.cpp new file mode 100644 index 000000000..a7b48e875 --- /dev/null +++ b/src/opm/parser/eclipse/EclipseState/Schedule/Action/Enums.cpp @@ -0,0 +1,81 @@ +/* + Copyright 2021 Equinor ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#include +#include + +#include + +namespace Opm { +namespace Action { + +Logical logic_from_int(int int_logic) { + if (int_logic == 0) + return Logical::END; + + if (int_logic == 1) + return Logical::AND; + + if (int_logic == 2) + return Logical::OR; + + throw std::logic_error("Unknown integer value"); +} + + + + +Comparator comparator_from_int(int cmp_int) { + switch (cmp_int) { + case 1: + return Comparator::GREATER; + case 2: + return Comparator::LESS; + case 3: + return Comparator::GREATER_EQUAL; + case 4: + return Comparator::LESS_EQUAL; + case 5: + return Comparator::EQUAL; + default: + throw std::logic_error(fmt::format("Integer value: {} could not be converted to ACTIONX comparator", cmp_int)); + } +} + +std::string comparator_as_string(Comparator cmp) { + if (cmp == Comparator::EQUAL) + return "="; + + if (cmp == Comparator::GREATER) + return ">"; + + if (cmp == Comparator::LESS) + return "<"; + + if (cmp == Comparator::LESS_EQUAL) + return "<="; + + if (cmp == Comparator::GREATER_EQUAL) + return ">="; + + throw std::logic_error("Bug in opm/flow - should not be here"); +} + +} +} diff --git a/tests/parser/ACTIONX.cpp b/tests/parser/ACTIONX.cpp index d3a327b2d..2068f9df2 100644 --- a/tests/parser/ACTIONX.cpp +++ b/tests/parser/ACTIONX.cpp @@ -702,7 +702,7 @@ BOOST_AUTO_TEST_CASE(Conditions) { BOOST_CHECK_THROW(Action::Condition cond({"WWCT", "OPX", ">"}, location), std::invalid_argument); Action::Condition cond({"WWCT", "OPX", ">", "0.75", "AND"}, location); - BOOST_CHECK(cond.cmp == Action::Condition::Comparator::GREATER); + BOOST_CHECK(cond.cmp == Action::Comparator::GREATER); BOOST_CHECK(cond.cmp_string == ">" ); BOOST_CHECK_EQUAL(cond.lhs.quantity, "WWCT"); BOOST_CHECK_EQUAL(cond.lhs.args.size(), 1U); @@ -712,10 +712,10 @@ BOOST_AUTO_TEST_CASE(Conditions) { BOOST_CHECK_EQUAL(cond.rhs.quantity, "0.75"); BOOST_CHECK_EQUAL(cond.rhs.args.size(), 0U); - BOOST_CHECK(cond.logic == Action::Condition::Logical::AND); + BOOST_CHECK(cond.logic == Action::Logical::AND); Action::Condition cond2({"WWCT", "OPX", "<=", "WSOPR", "OPX", "235"}, location); - BOOST_CHECK(cond2.cmp == Action::Condition::Comparator::LESS_EQUAL); + BOOST_CHECK(cond2.cmp == Action::Comparator::LESS_EQUAL); BOOST_CHECK(cond2.cmp_string == "<=" ); BOOST_CHECK_EQUAL(cond2.lhs.quantity, "WWCT"); BOOST_CHECK_EQUAL(cond2.lhs.args.size(), 1U); @@ -725,7 +725,7 @@ BOOST_AUTO_TEST_CASE(Conditions) { BOOST_CHECK_EQUAL(cond2.rhs.args.size(), 2U); BOOST_CHECK_EQUAL(cond2.rhs.args[0], "OPX"); BOOST_CHECK_EQUAL(cond2.rhs.args[1], "235"); - BOOST_CHECK(cond2.logic == Action::Condition::Logical::END); + BOOST_CHECK(cond2.logic == Action::Logical::END); } @@ -808,8 +808,8 @@ TSTEP const auto& cond0 = conditions[0]; BOOST_CHECK_EQUAL(cond0.lhs.quantity, "WWCT"); - BOOST_CHECK(cond0.cmp == Action::Condition::Comparator::GREATER); - BOOST_CHECK(cond0.logic == Action::Condition::Logical::AND); + BOOST_CHECK(cond0.cmp == Action::Comparator::GREATER); + BOOST_CHECK(cond0.logic == Action::Logical::AND); BOOST_CHECK_EQUAL(cond0.lhs.args.size(), 1U); BOOST_CHECK_EQUAL(cond0.lhs.args[0], "OPX"); BOOST_CHECK_EQUAL(cond0.rhs.args.size(), 0U); @@ -817,8 +817,8 @@ TSTEP const auto& cond1 = conditions[1]; BOOST_CHECK_EQUAL(cond1.lhs.quantity, "FPR"); - BOOST_CHECK(cond1.cmp == Action::Condition::Comparator::LESS); - BOOST_CHECK(cond1.logic == Action::Condition::Logical::END); + BOOST_CHECK(cond1.cmp == Action::Comparator::LESS); + BOOST_CHECK(cond1.logic == Action::Logical::END); /*****************************************************************/ @@ -829,16 +829,16 @@ TSTEP const auto& condB = actB.conditions(); BOOST_CHECK_EQUAL(condB.size() , 1U); BOOST_CHECK_EQUAL(condB[0].lhs.quantity, "FWCT"); - BOOST_CHECK(condB[0].cmp == Action::Condition::Comparator::LESS_EQUAL); - BOOST_CHECK(condB[0].logic == Action::Condition::Logical::END); + BOOST_CHECK(condB[0].cmp == Action::Comparator::LESS_EQUAL); + BOOST_CHECK(condB[0].logic == Action::Logical::END); BOOST_CHECK_EQUAL(condB[0].cmp_string, "<="); const auto& actA = actions2["A"]; const auto& condA = actA.conditions(); BOOST_CHECK_EQUAL(condA.size() , 1U); BOOST_CHECK_EQUAL(condA[0].lhs.quantity, "WOPR"); - BOOST_CHECK(condA[0].cmp == Action::Condition::Comparator::EQUAL); - BOOST_CHECK(condA[0].logic == Action::Condition::Logical::END); + BOOST_CHECK(condA[0].cmp == Action::Comparator::EQUAL); + BOOST_CHECK(condA[0].logic == Action::Logical::END); BOOST_CHECK_EQUAL(condA[0].cmp_string , "="); std::size_t index = 0;