apply_passflow Scheme Example

This example shows how to use the ReservoirModel.apply_passflow() scheme when modelling a single reservoir model.

Note

For details about the full model file structure please see Basic Single Reservoir.

We consider a reservoir with a single inflow, Q_in, and an outflow Q_out. The reservoir outflow is determined based upon the day of the month at each timestep.

\[ \begin{align}\begin{aligned}\begin{split}Q_{out} = \begin{cases} Q_{in} & \text{from 12th to 19th of each month}\\ Q_{out,target} & \text{otherwise}\\ \end{cases}\\\end{split}\\Q_{out,target} = \text{is a timeseries with values all 3$m^3/s$}\end{aligned}\end{align} \]

The ReservoirModel.apply_passflow() and ReservoirModel.set_q() schemes can be applied to model these operations.

Main Model (python) File

An example of the main model file passflow_example.py is given below.

 1"""Example that illustrates use of the passflow scheme."""
 2
 3from pathlib import Path
 4
 5from rtctools.util import run_simulation_problem
 6
 7from rtctools_simulation.reservoir.model import InputVar, ModelConfig, ReservoirModel
 8
 9CONFIG = ModelConfig(base_dir=Path(__file__).parent)
10
11
12class SingleReservoir(ReservoirModel):
13    """Example single reservoir model."""
14
15    def apply_schemes(self):
16        """Apply schemes for controlling the reservoir."""
17
18        # Get current time.
19        datetime = self.get_current_datetime()
20        # Apply schemes.
21        day_12 = 12
22        day_19 = 19
23        if day_12 <= datetime.day <= day_19:
24            self.apply_passflow()
25        else:
26            self.set_q(
27                target_variable=InputVar.Q_OUT,
28                input_type="timeseries",
29                input_data="Q_out_target",
30                apply_func="INST",
31            )
32
33
34# Create and run the model.
35if __name__ == "__main__":
36    run_simulation_problem(SingleReservoir, config=CONFIG)

The template file mentioned in the Basic Single Reservoir will look very similar to this file, except that the apply_schemes() method still needs to be filled out.

The line

CONFIG = ModelConfig(base_dir=Path(__file__).parent)

sets the model configuration. This model configuration is defined by the base directory base_dir. In most cases, the base directory is Path(__file__).parent, which is the directory of the current file.

The line

class SingleReservoir(ReservoirModel):

defines a class SingleReservoir that inherits all properties and functionalities of the predefined class ReservoirModel. An overview of this class can be found in Reservoir API and details of the underlying model it uses can be found in Single Reservoir Model.

The method ReservoirModel.apply_schemes() is called every timestep and contains the logic for which schemes are applied. The first argument self is the SingleReservoir object itself. Since SingleReservoir inherits from ReservoirModel, self can call any of the ReservoirModel methods, such as ReservoirModel.get_current_datetime(), ReservoirModel.set_q(), and ReservoirModel.apply_passflow(), An overview of all available ReservoirModel methods can be found in Reservoir API.

In this example, the ReservoirModel.apply_schemes() method starts by collecting the current timestep as this is used to determine operations. The method then checks if the current timestep is between the 12th and 19th of the month. The set_q or apply_passflow scheme is then applied to set the reservoir outflow.

Lookup tables

This model uses only the standard lookup table h_from_v, for other lookup tables, defaults from the generated template files can be used.

Note

For further details about the lookup tables please see Basic Single Reservoir.

Input Data Files

This implementation of the ReservoirModel.set_q() scheme requires input data for the Q_out_target, supplied via the timeseries_import.xml

    <series>
      <header>
        <type>instantaneous</type>
        <moduleInstanceId>reservoir</moduleInstanceId>
        <locationId>reservoir</locationId>
        <parameterId>Q.out.target</parameterId>
        <timeStep unit="second" multiplier="3600"/>
        <startDate date="2022-06-07" time="06:00:00"/>
        <endDate date="2022-06-27" time="06:00:00"/>
        <forecastDate date="2022-06-07" time="06:00:00"/>
        <missVal>-999.0</missVal>
        <stationName>Reservoir1</stationName>
        <units>m3/s</units>
    </header>
      <event date="2022-06-07" time="06:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="07:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="08:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="09:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="10:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="11:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="12:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="13:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="14:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="15:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="16:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="17:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="18:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="19:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="20:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="21:00:00" value="3" flag="8"/>
      <event date="2022-06-07" time="22:00:00" value="3" flag="8"/>

This additional input data is mapped to the variables, Q_out_target using the rtcDataConfig.xml.

    <timeSeries id="Q_out_target">
        <PITimeSeries>
            <locationId>reservoir</locationId>
            <parameterId>Q.out.target</parameterId>
        </PITimeSeries>
    </timeSeries>

Note

For further details about input file structure please see Basic Single Reservoir.

Output Data

The results of the simulation will appear in the output folder in a file called timeseries_export.xml. The data is linked to model variables via the rtcDataConfig.xml in the same way as with timeseries_import.xml.

Automatic Plotting

You can optionally include a plot_table.csv in the input folder. This is used by the rtc-tools-interfaces module (automatically installed with this package) to plot the model output. For more details on how to use this file and visualize results, see RTC-Tools-Interface.

The results of the simulation run can be seen in the plot below.