Skip to content

Commit 54e0904

Browse files
author
Lisa Julia Nebel
committed
Add an example for stateful behavior using Python classes
1 parent 1825c45 commit 54e0904

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

python/sphinx_docs/docs/embedded-python.rst

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,118 @@ In order to enable the PYACTION keyword:
5454
- ``current_report_step``: This is an integer for the report step we are currently working on. Observe that the PYACTION is called for every simulator timestep, i.e. it will typically be called multiple times with the same value for the report step argument.
5555

5656
- ``current_summary_state``: An instance of the `SummaryState <common.html#opm.io.sim.SummaryState>`_ class — this is where the current summary results of the simulator are stored. The `SummaryState <common.html#opm.io.sim.SummaryState>`_ class has methods to get hold of well, group, and general variables.
57+
58+
59+
Stateful behavior using Python classes
60+
--------------------------------------
61+
62+
In the below code snippet, we use a ``WellController`` class to manage production wells by tracking their status and simulation timing.
63+
We create one instance of the WellController and use its internal state across multiple timesteps and PYACTION calls.
64+
65+
.. code-block:: python
66+
67+
import opm_embedded
68+
from datetime import datetime, timedelta
69+
70+
# Check if the setup has already been done to avoid reinitialization
71+
if 'setup_done' not in locals():
72+
# Target oil production rate in standard units (e.g., stb/day)
73+
OIL_RATE_TARGET = 8000
74+
# Minimum time in days between opening new wells
75+
MIN_DAYS_BETWEEN_OPENINGS = 50
76+
77+
class WellController:
78+
"""
79+
A controller to manage the opening of production wells based on
80+
oil rate targets and elapsed simulation time.
81+
82+
Attributes:
83+
closed_wells (list[str]): List of wells yet to be opened.
84+
last_opening_time (datetime): Simulation time of the last well opening.
85+
Initially, this is set to the simulation start time.
86+
"""
87+
def __init__(self, well_names, start_time):
88+
"""
89+
Initialize the WellController.
90+
91+
Args:
92+
well_names (list[str]): Names of wells to be controlled.
93+
start_time (datetime): Simulation start time.
94+
"""
95+
self.closed_wells = list(well_names)
96+
self.last_opening_time = start_time
97+
98+
def update(self, current_oil_rate, current_time):
99+
"""
100+
Evaluate the current oil production and determine whether to open
101+
a new well based on the target rate and time since the last opening.
102+
103+
Args:
104+
current_oil_rate (float): The current oil rate.
105+
current_time (datetime): Current simulation time.
106+
"""
107+
days_since_last_opening = (current_time - self.last_opening_time).days
108+
109+
if (current_oil_rate < OIL_RATE_TARGET and
110+
days_since_last_opening >= MIN_DAYS_BETWEEN_OPENINGS and
111+
len(self.closed_wells) > 0):
112+
113+
next_well = self.closed_wells.pop(0)
114+
self.last_opening_time = current_time
115+
116+
schedule.open_well(next_well)
117+
opm_embedded.OpmLog.info(f"Opened well {next_well}")
118+
119+
def set_next_dt(self, current_time):
120+
"""
121+
Insert the NEXTSTEP keyword to control the simulator's timestep,
122+
adjusting based on whether a well was just opened.
123+
124+
Args:
125+
current_time (datetime): Current simulation time.
126+
"""
127+
if self.closed_wells:
128+
days_since_last_opening = (current_time - self.last_opening_time).days
129+
if days_since_last_opening >= MIN_DAYS_BETWEEN_OPENINGS:
130+
next_dt = 10.0
131+
else:
132+
next_dt = 50.0
133+
kw = f"""
134+
NEXTSTEP
135+
{next_dt} /
136+
"""
137+
schedule.insert_keywords(kw)
138+
139+
# Instantiate the controller with a list of wells and simulation start time
140+
# This controller will be instatiated once and be used in all following PYACTION calls.
141+
controller = WellController(well_names=['PROD01', 'PROD02'],
142+
start_time=opm_embedded.current_schedule.start)
143+
setup_done = True
144+
145+
# Retrieve current simulation components from the OPM embedded module
146+
schedule = opm_embedded.current_schedule
147+
report_step = opm_embedded.current_report_step
148+
summary_state = opm_embedded.current_summary_state
149+
150+
# Compute the current simulation time
151+
current_time = schedule.start + timedelta(seconds=summary_state.elapsed())
152+
current_oil_rate = summary_state.group_var('P', 'GOPR')
153+
154+
# Update well control logic based on current state
155+
controller.update(current_oil_rate, current_time)
156+
# Set the next simulation step duration
157+
controller.set_next_dt(current_time)
158+
159+
# Optional logs to track the status of the two wells:
160+
# opm_embedded.OpmLog.info("PROD01: {}".format(schedule.get_well("PROD01", report_step).status()))
161+
# opm_embedded.OpmLog.info("PROD02: {}".format(schedule.get_well("PROD02", report_step).status()))
162+
163+
Use this code snippet with the example `MSW-3D-TWO-PRODUCERS <https://github.com/OPM/opm-tests/blob/master/msw/MSW-3D-TWO-PRODUCERS.DATA>`_ by saving the file as ``wellcontroller.py`` at the same location as ``MSW-3D-TWO-PRODUCERS.DATA`` and adding
164+
165+
.. code-block:: none
166+
167+
PYACTION
168+
WELLCONTROLLER UNLIMITED /
169+
'wellcontroller.py' /
170+
171+
to the ``SCHEDULE`` section of ``MSW-3D-TWO-PRODUCERS.DATA``.

0 commit comments

Comments
 (0)