set_q Scheme Example

This example shows how to use the ReservoirModel.set_q() 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 a single outflow Q_out. The reservoir outflow is determined based upon the reservoir elevation, H, at each timestep.

\[\begin{split}Q_{out} = \begin{cases} 0.4 & \text{if $H > 1598.54$}\\ 0.2 & \text{otherwise}\\ \end{cases}\end{split}\]

The ReservoirModel.set_q() scheme can be applied to model these operations.

Note

There are many options which can be chosen when applying the ReservoirModel.set_q() scheme. For other example implementations please see also include_rainevap Scheme Example and apply_passflow Scheme Example.

Main Model (python) File

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

 1"""Example that illustrates use of set_q scheme only."""
 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        # Collect reservoir elevation.
19        h = self.get_var("H")
20        critical_h = 1598.54
21        if h > critical_h:
22            self.set_q(
23                target_variable=InputVar.Q_OUT,
24                input_type="parameter",
25                input_data=0.4,
26            )
27        else:
28            self.set_q(
29                target_variable=InputVar.Q_OUT,
30                input_type="parameter",
31                input_data=0.2,
32            )
33
34
35# Create and run the model.
36if __name__ == "__main__":
37    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_var() and ReservoirModel.set_q(). 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 reservoir elevation as this is used to determine operations. The method then checks if the elevation H is higher than threshold value 1598.54. The set_q() scheme is then applied to set the reservoir outflow equal to a value of 0.2 or 0.4 based on whether the defined elevation threshold is exceeded.

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 details about the lookup tables please see Basic Single Reservoir.

Input Data Files

Note

For 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.