Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Functions

A component's behaviour can depend on other dynamic values within a model. Functions allow you to control this behaviour via an arithmetic expression, or function.

About the Function manager

The Function manager (shown in Figure 1) allows you to centrally manage all functions that have been defined in Source. It is used in places where you might otherwise employ a time series or a single value. 

Info
iconfalse
Note: As of Source version 3.5.0, the Expression Editor has been superseded by the Function manager. Click here to access documentation for the old Expression Editor.

The Function manager can be accessed in one of two ways:

  • Choosing View » Function Manager; or
  • Clicking on the Function Manager icon in the Scenario toolbar.
Figure 1. Function manager

 

Items in the Function manager are sorted into groups, and then by name, and they are ordered as follows:

  • In-built functions, which includes End, Now, Start and $ActiveInputSet;
  • Variables - there are four types of variables available in Source. Refer to Types of variables for more information;  and
  • Custom functions

The $ActiveInputSet function can be referenced as a model variable in another function. It is used to specify the use of a particular input set (if one has been defined). For the function shown in the example below, different values are assigned to the function depending on the value of the variable.

if($ActiveInputSet = "Wet", 15, if($ActiveInputSet = "Dry", 0, 7))

In this case, for all instances of Wet, the function will return a value of 15; for instances of Dry, a value of 7 will be output. All other instances will result in a value of 7.

The Function Manager only shows direct uses of a function. For example, if an inflow node has been assigned a function, $ExampleFunction, this is indicated in the Function Manager. If $ExampleFunction is comprised of two separate functions, $ExFn1 and $ExFn2, these two functions will not be part of the Inflow node.

 You can also search for a variable or function using the area under the Function manager toolbar. Start entering the search item, and the results will be filtered as you type the text.

Function Editor

The Function Editor (shown in Figure 2) allows you to add, modify and delete individual functions and variables. It can be accessed in one of two ways:

  • Choosing Edit » Functions…; or
  • Choose the variable you want to change in the Function manager (Figure 1). Right click on it, then choose Edit from the contextual menu.
Figure 2. Function Editor


Adding a variable or function

To add a variable or function, click on the Add Variable button on the Functions toolbar (as shown in Figure 1) and choose the appropriate drop-down item;

You can define and configure it using the Configuration pane on the right. The view of this pane will depend on the type of variable or function that has been specified. 

Deleting and renaming a variable or function

To delete a variable or function, choose the appropriate item in the function list, right click, and choose Delete from the contextual menu.

To rename it, choose Rename from the contextual menu and type the name you wish to assign. 

Info
iconfalse
Note: Source warns you if you attempt to delete a modelled variable which is referenced in a function.

The Results Units drop down menu allows you to choose which units to display the output in.

By default, if a function or variable is not referenced at any node, it is not recorded during a scenario run. The Force Evaluation scroll toggle (Figure 2) ensures that a function is evaluated and recorded during a scenario run. In some cases (eg. if a function is not used in a model), the function will not be evaluated if the scroll if toggled to OFF.

You can also enabling recording of a function using the Project Explorer (choose Miscellaneous).

Info
iconfalse
Note: A function always returns a single value at each time-step.

About variables and functions

Played vs non-played variables

The Function manager distinguishes between two classes of variables:

  • Played - These are provided via time series or other inputs such as the Piecewise Linear or Patternsvariables of the Function manager. Conceptually, any played variable can be viewed as a list with the next value taken from the list each time the time-step advances. Played variables are updated at the start of the time-step and maintain the same value throughout the time-step.
  • Non-played - Also known as model variables, in which variables are produced by the operation of the models which execute during a run. Within a time-step, the moment when any given model variable is updated depends on the model.

General rules for functions

The following rules apply to functions:

  • White-space, including new-line markers, is ignored;
  • Function names, operator names, variable names and label names are not case sensitive;
  • All variable names must begin with the "$" character;
  • All functions are global;
  • All variables used in functions must be defined;
  • Circular references are prohibited; and
  • A blank function returns zero.
Working with global

Global functions

Functions and variables can have non-unique names, so long as they are in different scopes. For example, consider the following setup in the Function manager:

Code Block
	Folder1
		$Function1
		$Function2
	Folder2
		$Function3
	$Function4
  • In this case, $Function1’s expression can reference $Function2 directly as they are both part of Folder1. So, if($Function2, 0, 1) is a valid expression;
  • If $Function3 references $Function2, you must use the full path to reference it - if($Folder1.Function2, 0, 1); and
  • Any function can reference $Function4 directly because it is in the global scope (ie. it is not part of a folder).

Writing functions

The following features There are several in-built functions and operators that allow you to enter functions:

  • You can drag and drop variable names into a function;
  • Use Ctrl + Space to show a list of all available options;

define otherwise complex functions in simple expressions. These are described next.

Operator precedence

Unless you use parentheses to modify the order of evaluation, operations are performed according to the precedence rules shown in Table 1. Where two operators have the same level of precedence, the operations are performed left-to-right.

Table 1. Function manager (evaluation order)
Order of precedenceOperator SymbolMeaning
1 (highest)( )explicit ordering
2-negation
3** and ^exponentiation
4

 * and /

multiplication and division
5+ and -addition and subtraction
6 (lowest)<  <=  =  >=  >  <>comparison operators

In-built functions

In-built functions are predefined formulae that perform calculations which usually depend on the arguments you supply as parameters. They can be entered manually or by using the pull down menu at the top of the Function Editor (Figure 2). The available functions are summarised in Tables 6-10. Some examples of function use are shown in Table 2.

...

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.  

The following features in the Function Manager make it easier to write expressions:

  • When you start writing an expression, Source lists all the available arguments, including the in-built variables (such as $now.day and $now.dayofyear). Alternatively, you can press Ctrl + Space on your keyboard to list these options; 
  • You can drag and drop variable names into a function;

 

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 3);
  • The expanded section (shown in Figure 4) 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 3. Function Editor (Parse)

Figure 4. 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 5. Function manager (Variables tab)

 

...