Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate WELPI for use in PYACTION #4133

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opm/input/eclipse/Schedule/Action/PyAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool PyAction::valid_keyword(const std::string& keyword) {
"GCONINJE", "GCONPROD", "GCONSUMP","GRUPTREE",
"METRIC", "MULTX", "MULTX-", "MULTY", "MULTY-", "MULTZ", "MULTZ-",
"NEXT", "NEXTSTEP",
"WCONINJE", "WCONPROD", "WECON", "WEFAC", "WELOPEN", "WELTARG", "WGRUPCON", "WELSEGS", "WELSPECS", "WSEGVALV", "WTEST"
"WCONINJE", "WCONPROD", "WECON", "WEFAC", "WELOPEN", "WELPI", "WELTARG", "WGRUPCON", "WELSEGS", "WELSPECS", "WSEGVALV", "WTEST"
};
return pyaction_allowed_list.find(keyword) != pyaction_allowed_list.end();
}
Expand Down
6 changes: 3 additions & 3 deletions opm/input/eclipse/Schedule/HandlerContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HandlerContext
const ScheduleGrid& grid_,
const std::size_t currentStep_,
const std::vector<std::string>& matching_wells_,
bool actionx_mode_,
bool welpi_action_mode_,
const ParseContext& parseContext_,
ErrorGuard& errors_,
SimulatorUpdate* sim_update_,
Expand All @@ -67,7 +67,7 @@ class HandlerContext
, keyword(keyword_)
, currentStep(currentStep_)
, matching_wells(matching_wells_)
, actionx_mode(actionx_mode_)
, welpi_action_mode(welpi_action_mode_)
, parseContext(parseContext_)
, errors(errors_)
, wpimult_global_factor(wpimult_global_factor_)
Expand Down Expand Up @@ -157,7 +157,7 @@ class HandlerContext
const DeckKeyword& keyword;
const std::size_t currentStep;
const std::vector<std::string>& matching_wells;
const bool actionx_mode;
const bool welpi_action_mode;
const ParseContext& parseContext;
ErrorGuard& errors;
std::unordered_map<std::string, double>& wpimult_global_factor;
Expand Down
17 changes: 13 additions & 4 deletions opm/input/eclipse/Schedule/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Schedule::Schedule(const Deck& deck, const EclipseState& es, const std::optional
result.completed_cells = CompletedCells::serializationTestObject();
result.current_report_step = 0;
result.simUpdateFromPython = std::make_shared<SimulatorUpdate>(SimulatorUpdate::serializationTestObject());
result.wellPIPointer = nullptr;

return result;
}
Expand Down Expand Up @@ -294,15 +295,15 @@ Schedule::Schedule(const Deck& deck, const EclipseState& es, const std::optional
ErrorGuard& errors,
const ScheduleGrid& grid,
const std::vector<std::string>& matching_wells,
bool actionx_mode,
bool welpi_action_mode,
SimulatorUpdate* sim_update,
const std::unordered_map<std::string, double>* target_wellpi,
std::unordered_map<std::string, double>& wpimult_global_factor,
WelSegsSet* welsegs_wells,
std::set<std::string>* compsegs_wells)
{
HandlerContext handlerContext { *this, block, keyword, grid, currentStep,
matching_wells, actionx_mode,
matching_wells, welpi_action_mode,
parseContext, errors, sim_update, target_wellpi,
wpimult_global_factor, welsegs_wells, compsegs_wells};

Expand Down Expand Up @@ -1439,6 +1440,11 @@ File {} line {}.)", pattern, location.keyword, location.filename, location.linen
ScheduleGrid grid(this->completed_cells);
SimulatorUpdate sim_update;
std::unordered_map<std::string, double> target_wellpi;
// Get the well production indices from the simulation up until now
// and use in the calculation for the overall production indices.
if (this->wellPIPointer) {
target_wellpi = *(this->wellPIPointer);
}
lisajulia marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::string> matching_wells;
const std::string prefix = "| "; /* logger prefix string */
this->snapshots.resize(reportStep + 1);
Expand All @@ -1454,7 +1460,7 @@ File {} line {}.)", pattern, location.keyword, location.filename, location.linen
errors,
grid,
matching_wells,
/*actionx_mode=*/false,
this->welpi_action_mode,
&sim_update,
&target_wellpi,
wpimult_global_factor);
Expand Down Expand Up @@ -1640,6 +1646,8 @@ File {} line {}.)", pattern, location.keyword, location.filename, location.linen


SimulatorUpdate Schedule::runPyAction(std::size_t reportStep, const Action::PyAction& pyaction, Action::State& action_state, EclipseState& ecl_state, SummaryState& summary_state) {
// Set welpi_action_mode to true, this is necessary for the keyword WELPI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should explain why?

this->welpi_action_mode = true;
// Reset simUpdateFromPython, pyaction.run(...) will run through the PyAction script, the calls that trigger a simulator update will append this to simUpdateFromPython.
this->simUpdateFromPython->reset();
// Set the current_report_step to the report step in which this PyAction was triggered.
Expand All @@ -1654,7 +1662,8 @@ File {} line {}.)", pattern, location.keyword, location.filename, location.linen
auto result = pyaction.run(ecl_state, *this, reportStep, summary_state, apply_action_callback);
action_state.add_run(pyaction, result);

// The whole pyaction script was executed, now the simUpdateFromPython is returned.
// The whole pyaction script was executed, welpi_action_mode is set to false and the simUpdateFromPython is returned.
this->welpi_action_mode = false;
return *(this->simUpdateFromPython);
}

Expand Down
9 changes: 8 additions & 1 deletion opm/input/eclipse/Schedule/Schedule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ namespace Opm
serializer(this->completed_cells);
serializer(this->m_treat_critical_as_non_critical);
serializer(this->current_report_step);
serializer(this->welpi_action_mode);
serializer(this->simUpdateFromPython);

this->template pack_unpack<PAvg>(serializer);
Expand Down Expand Up @@ -453,6 +454,9 @@ namespace Opm
friend std::ostream& operator<<(std::ostream& os, const Schedule& sched);
void dump_deck(std::ostream& os) const;

// The wellPIPointer is used when a PYACTION is executed. It is filled from the ActionHandler is
// over, it is set to false again. This is needed for handling the keyword WELPI from a PYACTION.
std::shared_ptr<std::unordered_map<std::string, double>> wellPIPointer = nullptr;
private:
friend class HandlerContext;

Expand All @@ -470,6 +474,9 @@ namespace Opm
WriteRestartFileEvents restart_output{};
CompletedCells completed_cells{};

// The action mode is set to true when a PYACTION call is executed, when the PYACTION execution is
// over, it is set to false again. This is needed for handling the keyword WELPI from a PYACTION.
bool welpi_action_mode = false;
// The current_report_step is set to the current report step when a PYACTION call is executed.
// This is needed since the Schedule object does not know the current report step of the simulator and
// we only allow PYACTIONS for the current and future report steps.
Expand Down Expand Up @@ -531,7 +538,7 @@ namespace Opm
ErrorGuard& errors,
const ScheduleGrid& grid,
const std::vector<std::string>& matching_wells,
bool actionx_mode,
bool welpi_action_mode,
SimulatorUpdate* sim_update,
const std::unordered_map<std::string, double>* target_wellpi,
std::unordered_map<std::string, double>& wpimult_global_factor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void handleWELPIRuntime(HandlerContext& handlerContext)

void handleWELPI(HandlerContext& handlerContext)
{
if (handlerContext.actionx_mode) {
if (handlerContext.welpi_action_mode) {
handleWELPIRuntime(handlerContext);
return;
}
Expand Down