The matching wells are compined with && or ||

This commit is contained in:
Joakim Hove
2019-01-17 11:13:33 +01:00
parent 8a2c2304e7
commit 586f296de0
3 changed files with 116 additions and 25 deletions

View File

@@ -23,6 +23,7 @@
#include <string>
#include <vector>
#include <unordered_set>
namespace Opm {
@@ -62,17 +63,54 @@ struct ParseNode {
};
/*
Small utility class to hold information about which wells match the
various expressions in the ACTIONX parsing.
*/
class WellSet {
public:
void add(const std::string& well) { this->well_set.insert(well); }
std::size_t size() const { return this->well_set.size(); }
std::vector<std::string> wells() const {
return std::vector<std::string>( this->well_set.begin(), this->well_set.end() );
}
WellSet& intersect(const WellSet& other) {
for (auto& well : this->well_set)
if (! other.contains(well))
this->well_set.erase(well);
return *this;
}
WellSet& add(const WellSet& other) {
for (auto& well : other.well_set)
this->add(well);
return *this;
}
bool contains(const std::string& well) const {
return (this->well_set.find(well) != this->well_set.end());
}
private:
std::unordered_set<std::string> well_set;
};
class ActionValue {
public:
explicit ActionValue(double value);
ActionValue() = default;
bool eval_cmp(TokenType op, const ActionValue& rhs, std::vector<std::string>& matching_wells) const;
bool eval_cmp(TokenType op, const ActionValue& rhs, WellSet& matching_wells) const;
void add_well(const std::string& well, double value);
double scalar() const;
private:
bool eval_cmp_wells(TokenType op, double rhs, std::vector<std::string>& matching_wells) const;
bool eval_cmp_wells(TokenType op, double rhs, WellSet& matching_wells) const;
double scalar_value;
double is_scalar = false;
@@ -107,7 +145,7 @@ ASTNode(TokenType type_arg, const std::string& func_arg, const std::vector<std::
arg_list(arg_list_arg)
{}
bool eval(const ActionContext& context, std::vector<std::string>& matching_wells) const;
bool eval(const ActionContext& context, WellSet& matching_wells) const;
ActionValue value(const ActionContext& context) const;
TokenType type;
void add_child(const ASTNode& child);

View File

@@ -28,6 +28,9 @@
namespace Opm {
ActionParser::ActionParser(const std::vector<std::string>& tokens_arg) :
tokens(tokens_arg)
{}
@@ -162,25 +165,14 @@ namespace {
}
bool ActionValue::eval_cmp_wells(TokenType op, double rhs, std::vector<std::string>& matching_wells) const {
bool ActionValue::eval_cmp_wells(TokenType op, double rhs, WellSet& matching_wells) const {
bool ret_value = false;
for (const auto& pair : this->well_values) {
const std::string& well = pair.first;
const double value = pair.second;
/*
It is less than clear how the matching_wells should be treated when
multiple conditons are nested; in the current implementation a
matching well is quite simply added to the list. This has two
consequences which are not-obviously-correct:
1. The wells might appear multiple times.
2. If a well matches in *one* conditon, and not in another - it will
be in the matching_wells list.
*/
if (eval_cmp_scalar(value, op, rhs)) {
matching_wells.push_back(well);
matching_wells.add(well);
ret_value = true;
}
}
@@ -188,7 +180,7 @@ bool ActionValue::eval_cmp_wells(TokenType op, double rhs, std::vector<std::stri
}
bool ActionValue::eval_cmp(TokenType op, const ActionValue& rhs, std::vector<std::string>& matching_wells) const {
bool ActionValue::eval_cmp(TokenType op, const ActionValue& rhs, WellSet& matching_wells) const {
if (op == TokenType::number ||
op == TokenType::ecl_expr ||
op == TokenType::open_paren ||
@@ -239,17 +231,27 @@ ActionValue ASTNode::value(const ActionContext& context) const {
}
bool ASTNode::eval(const ActionContext& context, std::vector<std::string>& matching_wells) const {
bool ASTNode::eval(const ActionContext& context, WellSet& matching_wells) const {
if (this->children.size() == 0)
throw std::invalid_argument("bool eval should not reach leafnodes");
if (this->type == TokenType::op_or || this->type == TokenType::op_and) {
bool value = (this->type == TokenType::op_and);
WellSet wells;
for (const auto& child : this->children) {
if (this->type == TokenType::op_or)
value = value || child.eval(context, matching_wells);
else
value = value && child.eval(context, matching_wells);
/*
Observe that we require that the set of matching wells is
evaluated for all conditions, to ensure that we must protect
against C++ short circuting of the || operator, that is currently
achieved by evaluating the child value first.
*/
if (this->type == TokenType::op_or) {
value = child.eval(context, wells) || value;
matching_wells.add(wells);
} else {
value = child.eval(context, wells) && value;
matching_wells.intersect(wells);
}
}
return value;
}
@@ -416,8 +418,12 @@ ActionAST::ActionAST(const std::vector<std::string>& tokens) {
throw std::invalid_argument("Failed to parse");
}
bool ActionAST::eval(const ActionContext& context, std::vector<std::string>& matching_wells) const {
return this->tree.eval(context, matching_wells);
WellSet wells;
bool eval_result = this->tree.eval(context, wells);
matching_wells = wells.wells();
return eval_result;
}
}

View File

@@ -510,7 +510,7 @@ BOOST_AUTO_TEST_CASE(ActionValueTest) {
well_values.add_well("B", 200);
well_values.add_well("C", 300);
std::vector<std::string> matching_wells;
WellSet matching_wells;
// Invalid operator
BOOST_REQUIRE_THROW(well_values.eval_cmp(TokenType::number, scalar_value, matching_wells), std::invalid_argument);
// Right hand side is not scalar
@@ -521,7 +521,7 @@ BOOST_AUTO_TEST_CASE(ActionValueTest) {
BOOST_CHECK( well_values.eval_cmp(TokenType::op_eq, scalar_value, matching_wells) );
BOOST_CHECK_EQUAL(1, matching_wells.size());
BOOST_CHECK_EQUAL("B", matching_wells[0]);
BOOST_CHECK(matching_wells.contains("B"));
}
@@ -541,3 +541,50 @@ BOOST_AUTO_TEST_CASE(TestMatchingWells) {
BOOST_CHECK_EQUAL( matching_wells.size(), 1);
BOOST_CHECK_EQUAL( matching_wells[0], "OPZ" );
}
BOOST_AUTO_TEST_CASE(TestMatchingWells_AND) {
ActionAST ast({"WOPR", "*", ">", "1.0", "AND", "WWCT", "*", "<", "0.50"});
SummaryState st;
std::vector<std::string> matching_wells;
st.add_well_var("OPX", "WOPR", 0);
st.add_well_var("OPY", "WOPR", 0.50);
st.add_well_var("OPZ", "WOPR", 2.0); // The WOPR check matches this well.
st.add_well_var("OPX", "WWCT", 1.0);
st.add_well_var("OPY", "WWCT", 0.0); // The WWCT check matches this well.
st.add_well_var("OPZ", "WWCT", 1.0);
ActionContext context(st);
BOOST_CHECK( ast.eval(context, matching_wells) );
// Even though condition as a whole matches, there is no finite set of wells
// which mathes both conditions when combined with AND - i.e. the matching_wells
// variable should be empty.
BOOST_CHECK( matching_wells.empty() );
}
BOOST_AUTO_TEST_CASE(TestMatchingWells_OR) {
ActionAST ast({"WOPR", "*", ">", "1.0", "OR", "WWCT", "*", "<", "0.50"});
SummaryState st;
std::vector<std::string> matching_wells;
st.add_well_var("OPX", "WOPR", 0);
st.add_well_var("OPY", "WOPR", 0.50);
st.add_well_var("OPZ", "WOPR", 2.0); // The WOPR check matches this well.
st.add_well_var("OPX", "WWCT", 1.0);
st.add_well_var("OPY", "WWCT", 0.0); // The WWCT check matches this well.
st.add_well_var("OPZ", "WWCT", 1.0);
ActionContext context(st);
BOOST_CHECK( ast.eval(context, matching_wells) );
// The well 'OPZ' matches the first condition and the well 'OPY' matches the
// second condition, since the two conditions are combined with || the
// resulting mathcing_wells variable should contain both these wells.
BOOST_CHECK_EQUAL( matching_wells.size(), 2);
BOOST_CHECK( std::find(matching_wells.begin(), matching_wells.end(), "OPZ") != matching_wells.end());
BOOST_CHECK( std::find(matching_wells.begin(), matching_wells.end(), "OPY") != matching_wells.end());
}