...
Table 9. Function manager (miscellaneous functions)
Function | Meaning | Example use |
---|---|---|
IF | Specifies a logical test to perform | IF(logical_test,value_if_true,value_if_false) |
LOOKUP | Looks up the Y-value corresponding to an X-value via a piecewise linear editor (uses linear interpolation) | LOOKUP(variable_name, number) |
N1 | Returns 1 if the number is less than zero, 0 otherwise | N1(number) |
P1 | Returns 1 if the number is greater than zero, 0 otherwise | P1(number) |
PI | Returns the mathematical constant π to 14 decimal places | PI() |
/* Comment */ | /* begins a comment and */ ends a comment. Comments can go over multiple lines. | 5 + 4 /* Some sort of comment about the function*/ |
Table 10. Function manager (predefined variables)
Function | Meaning |
---|---|
$now.year | Returns the 4-digit year of the current time-step |
$now.month | Returns the month of the current time-step (range: 1...12) |
$now.day | Returns the day of the current time-step (range: 1...31) |
$now.hour | Returns the hour of the current time-step (range 0...23) |
For example, you can calculate your place in the water year using the calendar date functions.
Writing functions and expressions
The following features in Source help when writing functions:
- When you start writing an expression, Source lists all the available arguments, including the in-built variables (such as $now.day and $now.dayofyear), an example of which is shown in Figure 4. Alternatively, you can press Ctrl + Space on your keyboard to list these options; You can choose the appropriate argument from the list.
- You can drag and drop variable names into a function;
Figure 4. Maximum Constraint node (Function Editor)
Referencing
A function can reference other functions within the same scenario. It can also reference itself.
For example: $Function1 = $Function1 + $Function2
In this case, $Function1 will use its previous evaluation value to calculate its new one.
Notice that the list shown in Figure 3 contains $Function1 as an option.
Testing Functions
Once you have defined an expression or function, you can test its operation using the Parse button. You can enter values for the various terms in your expression and confirm that the result of evaluating your expression with those values is correct. The Parse button is, by default, hidden in the Function editor, and is used as follows:
- Highlight the function you wish to test. This will appear in the Configuration pane of the Function editor;
- Click on the area below the Configuration pane (as shown in Figure 5);
- The expanded section (shown in Figure 5) provides a table of all the variables used in the expression. Enter a value for the variable you wish to test (double-click on the cell, then enter a number);
- Click Parse; and
- The test result will be displayed in the cell under the Parse button.
To close this area, click on the same position.
Figure 5. Function Editor (Parse)
Figure 6. Function Editor (Parse-window)
If the function cannot be parsed, you will get an error icon next to the function in the Function manager. Additionally, an error in the Log Reporter will indicate the same.
Types of variables
Modelled variables
A modelled variable provides a connection between your function and model (non-played) variables. You define a name (or meta-name) by which you will refer to a model variable in your function and configure other parameters governing its use. Once a variable has been defined, it can be used in a function. To configure a new modelled variable using the Function Editor:
- Add a modelled variable, as described in Adding a variable or function;
- A new variable will appear in the position that was selected. Note that at this point, the variable has not been allocated a recorder, so a warning icon will appear;
- The Configuration pane will show a list of all the recorder variables available (this will depend on the model);
- Using the disclosure triangles, choose the appropriate recorder that you wish to be associated with the variable;
- Depending on the recorder you choose, you can also specify the following:
- Result Units - units of the results;
- The variable's Initial Value - this defines the value that the variable takes on between the start of the first time-step and the point during that time-step when the corresponding model variable acquires a defined value;
- Date Range - the date range that the Function manager uses as a filter when obtaining data during each time-step. For example, you can choose to use the value from the current time-step (assuming it has been updated by the time the function is evaluated) or the previous time-step. You can also use values like the average of the last 100 time-steps. See Date ranges for more information; and
- Time of Evaluation - specifies when the variable will be evaluated.
Figure 7. Function manager (Variables tab)
...