Make sure actionx keywords can be redefined

This commit is contained in:
Joakim Hove
2019-08-30 13:07:45 +02:00
parent 2aa10a0435
commit af2a09bde5
3 changed files with 92 additions and 16 deletions

View File

@@ -23,7 +23,7 @@
#include <string>
#include <ctime>
#include <map>
#include <vector>
#include <opm/parser/eclipse/EclipseState/Schedule/Action/ActionX.hpp>
@@ -42,10 +42,14 @@ public:
bool empty() const;
void add(const ActionX& action);
bool ready(std::time_t sim_time) const;
const ActionX& at(const std::string& name) const;
const ActionX& get(const std::string& name) const;
const ActionX& get(std::size_t index) const;
std::vector<const ActionX *> pending(std::time_t sim_time) const;
std::vector<ActionX>::const_iterator begin() const;
std::vector<ActionX>::const_iterator end() const;
private:
std::map<std::string, ActionX> actions;
std::vector<ActionX> actions;
};
}
}

View File

@@ -16,6 +16,7 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <opm/parser/eclipse/EclipseState/Schedule/Action/Actions.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/Action/ActionX.hpp>
@@ -34,22 +35,34 @@ bool Actions::empty() const {
void Actions::add(const ActionX& action) {
auto iter = this->actions.find(action.name());
if (iter != this->actions.end())
this->actions.erase(iter);
this->actions.insert(std::pair<std::string,ActionX>(action.name(), action));
printf("Adding: %s\n", action.name().c_str());
auto iter = std::find_if( this->actions.begin(), this->actions.end(), [&action](const ActionX& arg) { return arg.name() == action.name(); });
if (iter == this->actions.end()) {
printf("push_back\n");
this->actions.push_back(action);
} else {
printf("update\n");
*iter = action;
}
}
const ActionX& Actions::at(const std::string& name) const {
return this->actions.at(name);
const ActionX& Actions::get(const std::string& name) const {
const auto iter = std::find_if( this->actions.begin(), this->actions.end(), [&name](const ActionX& action) { return action.name() == name; });
if (iter == this->actions.end())
throw std::range_error("No such action: " + name);
return *iter;
}
const ActionX& Actions::get(std::size_t index) const {
return this->actions[index];
}
bool Actions::ready(std::time_t sim_time) const {
for (const auto& pair : this->actions) {
if (pair.second.ready(sim_time))
for (const auto& action : this->actions) {
if (action.ready(sim_time))
return true;
}
return false;
@@ -58,13 +71,20 @@ bool Actions::ready(std::time_t sim_time) const {
std::vector<const ActionX *> Actions::pending(std::time_t sim_time) const {
std::vector<const ActionX *> action_vector;
for (auto& pair : this->actions) {
auto& action = pair.second;
for (const auto& action : this->actions) {
if (action.ready(sim_time))
action_vector.push_back( &action );
}
return action_vector;
}
std::vector<ActionX>::const_iterator Actions::begin() const {
return this->actions.begin();
}
std::vector<ActionX>::const_iterator Actions::end() const {
return this->actions.end();
}
}
}

View File

@@ -604,7 +604,7 @@ TSTEP
10 /
ACTIONX
'ACTION1' /
'B' /
WWCT 'OPX' > 0.75 AND / -- The spaces will/should be normalized in Condition::expression()
FPR < 100 /
/
@@ -617,6 +617,27 @@ ENDACTIO
TSTEP
10 /
ACTIONX
'A' /
WOPR 'OPX' = 1000 /
/
ENDACTIO
ACTIONX
'B' /
FWCT < 0.50 /
/
ENDACTIO
TSTEP
10 /
)"};
Opm::Parser parser;
@@ -633,7 +654,7 @@ TSTEP
BOOST_CHECK_EQUAL(actions1.size(), 1);
const auto& act1 = actions1.at("ACTION1");
const auto& act1 = actions1.get("B");
const auto& strings = act1.keyword_strings();
BOOST_CHECK_EQUAL(strings.size(), 4);
BOOST_CHECK_EQUAL(strings.back(), "ENDACTIO");
@@ -661,6 +682,37 @@ TSTEP
BOOST_CHECK_EQUAL(cond1.quantity, "FPR");
BOOST_CHECK(cond1.cmp == Action::Condition::Comparator::LESS);
BOOST_CHECK(cond1.logic == Action::Condition::Logical::END);
/*****************************************************************/
const auto& actions2 = sched.actions(2);
BOOST_CHECK_EQUAL(actions2.size(), 2);
const auto& actB = actions2.get("B");
const auto& condB = actB.conditions();
BOOST_CHECK_EQUAL(condB.size() , 1);
BOOST_CHECK_EQUAL(condB[0].expression, "FWCT < 0.50");
BOOST_CHECK_EQUAL(condB[0].quantity, "FWCT");
BOOST_CHECK(condB[0].cmp == Action::Condition::Comparator::LESS);
BOOST_CHECK(condB[0].logic == Action::Condition::Logical::END);
const auto& actA = actions2.get("A");
const auto& condA = actA.conditions();
BOOST_CHECK_EQUAL(condA.size() , 1);
BOOST_CHECK_EQUAL(condA[0].expression, "WOPR 'OPX' = 1000");
BOOST_CHECK_EQUAL(condA[0].quantity, "WOPR");
BOOST_CHECK(condA[0].cmp == Action::Condition::Comparator::EQUAL);
BOOST_CHECK(condA[0].logic == Action::Condition::Logical::END);
std::size_t index = 0;
for (const auto& act : actions2) {
if (index == 0)
BOOST_CHECK_EQUAL("B", act.name());
if (index == 1)
BOOST_CHECK_EQUAL("A", act.name());
index++;
}
}