Download PDF
Download page Static Scripts and State Variables, Scripted Rules & IF-Blocks.
Static Scripts and State Variables, Scripted Rules & IF-Blocks
Download the zip file and unzip it in C:\class\Workshops.
This workshop has two parts – in Part 1, we'll learn the value of utility scripts. In Part 2, we'll work with State Variables, Scripted Rules, and If -Blocks to learn more about how and why we'd use each one.
Part A – Working with Static Scripts
Start HEC-ResSim.
- Switch to the Simulation module.
- Open the W-03-A watershed and the Period of Record 1955 – 1975 simulation.
Create a new static script.
- Select Tools->Script Editor... from the menu bar. The Script Editor (Script Browser) will appear.
- Look at the Scripts tree at the right. This tree is showing you where the script you are working on will be stored and made available. The default location is the Current Watershed and its Simulation module. Keep that setting.
- From the File menu, select Import.
- In the Import dialog, browse to your Workshop 3 folder and select the file: W-03A_Static_Script.py and click OK.
- The edit panel will fill out with the content of the file you selected and the name in the tree will be the name of the file. Let's change that name. The Label field is editable.
- Change the name in the Label field to Workshop 3.py and click the Save button.
- A message box titled Compile Failed will be displayed with an error reported in the box. The same error message will also appear in the console/console log.
- You can dismiss the Compile Failed dialog by pressing the OK button.
View the error message at the bottom of the console log.
- What type of error is reported?
The Save button saved your script with the new name in a simulation folder under your watershed in the AppData area and it closed your script for editing. Click on your script in the tree and click the Edit button to re-enable the edit pane.
Fix the Error
- The syntax error was reported to be on line 21. However, the cause of the error is on line 20. What statement can you use after the else: line to satisfy the syntax requirements? Edit the script to insert the line.
Press the Save and Test button.
- If your dialog still mentions "SyntaxError", complete the previous step (with assistance, if necessary) and repeat.
- If you successfully fixed the previous problem you will get a Script Failed dialog that shows "NameError: name rateOfRise is not defined" on line 17.
- The NameError is caused by using variable name rateOfRise on line 17 that has not yet been defined.
- Insert one or more statements at line 12 that computes the rate of rise in feet per hour and assigns it to a variable named rateOfRise.
- The computation should subtract the value of elevBefore from elevNow and divide the difference by the number of hours between them.
- See the note at lines 3 and 4 of the script to figure the number of hours.
Save and Test the script again.
- If you completed the previous step successfully you should see a message stating the rate of rise is 0.033333 feet/hour. If you got a different answer, correct your equation. HINT – remember the order of operations; you might need parentheses.
Modify the script to output the rate of rise to only 2 decimal places.
Place the script in the Script pane of the Simulation Control Panel.
- Right-click in the area below the Compute button in the Simulation Control Panel
- Select Edit Script List from the popup menu. The Edit Scripts dialog will appear.
- Select Workshop 3 in the Available Scripts column and click the Add ► button.
- Click OK to dismiss the Edit Scripts dialog.
PART B – Compare & Contrast State Variables, Scripted Rules, & IF-Blocks
In this part of the workshop, you will implement a minimum release operating criteria using three different approaches: one relying on a State Variable script, a second relying on a Scripted Rule, and the third relying on an IF-BLOCK.
The operating criteria you are trying to achieve is to satisfy instream flow requirements by comparing reservoir inflow against predetermined inflow probabilities (the 25- and 50-percent) of exceedance, which define thresholds for setting the minimum release limits as follows:
Condition | Minimum Release |
Inflow is less than the 25-percentile | Inflow |
Inflow is greater than or equal to the 25-percentile, | 1100 |
Inflow is greater than the 50-percentile | 2400 |
You will first have to complete the code for a partially started State Variable script and simulation of its alternative. You will then have to create two extra alternatives that duplicate the functionality and results of the State Variable alternative. In the second alternative you have to create and use a Scripted Rule instead of the State Variable, while in the third alternative you have to create an IF-BLOCK and modify your operation set to achieve the same results of the State Variable and Scripted Rule alternatives.
Start HEC-ResSim.
- Switch to the Simulation module.
- Open the W-03B watershed and the Workshop 3 simulation.
PART B-1 - STATE VARIABLE ALTERNATIVE
Edit the State Variable
- Complete the code to check the inflow and determine the required minimum release according the above mentioned criteria.
- Update your operation set to ensure that the State Variable gets computed in the simulation.
How did you do that? Did you consider the different zones?
- Analyze the results of your simulation, and add print statements in your State Variable in order to check evaluations from your script. Refer to your lecture notes for examples of how to print output from your script or, you can also use the following example code to print code evaluation into the ResSim Compute Log:
message = currentRuntimestep.dateTimeString() + "variableName" + `variable`
network.printLogMessage(message)
PART B-2 - SCRIPTED RULE ALTERNATIVE
Create a new operation set and Alternative that relies on a scripted rule instead of the State Variable you completed in PART A:
- IMPORTANT – SAVE TO BASE the alternative you were working on.
- Switch to the Network Module
- Open the Validation – 1956 network
- Duplicate your operation set – call the copy Scripted Rule
- Remove the rule you added in Part B-1 and replace it with a New scripted rule. Call the new rule Instream Flow Req
- Copy and adapt your code from the State Variable script and complete your Scripted Rule
- Duplicate the StateVar alternative, call it ScrpRule, select the new operation set and save the alternative.
- Switch to the Simulation Module, add your new alternative, compute and compare results!
PART B-3 - IF-BLOCK
Create a new operation set and Alternative that relies on an IF-BLOCK and standard ResSim rules instead of the State Variable or Scripted Rule you completed in PARTs A and B:
- Switch to the Network Module
- Duplicate the Scripted Rule operation set and name the new opset IF-Block
- Remove your Instream Flow Req rule and replace it with an IF-Block. Call the IF-Block, Instream Flow Requirements.
- Add conditions and rules to your IF-Block to represent the same operating criteria mentioned above
- Duplicate the ScrpRule alternative and name it IfBlock.
- Select the new operation set and save the alternative.
- NOTE – there's more to do. Check the Time Series Tab. Map in the needed external time series. Check your StateVariable to find the filename and pathnames.
- Switch to the Simulation Module, add your new alternative, compute, and compare results!
Are your results different between the three alternatives?