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 . Groups are ordered as follows:

  • Folders associated with In-built functionsvariables, which includes End, Now, Start and $ActiveInputSet;
  • Functions
  • 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 0 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

Usages

When a Function is used in the model, as an example at an inflow node, then that usage is shown in the tree under the function.  If a Function is used in multiple places, then all usages are shown under the Function in the Function Manager tree.

 

 

 

 

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.

Execution of order

You can view the order in which expressions are processed using the Function Execution Order dialog (shown in Figure 2). Choose Tools » Function Execution Order to open this dialog.

Figure 2. Function Execution Order

Function Editor

The Function Editor (shown in Figure 3) 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 3. 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. 

Anchor
Adding a note to a function
Adding a note to a function
Adding a note to a function

You can include a text-based message, or note, to be associated with a function. Refer to About notes for more information about notes.

Info
iconfalse

Note: You can add a note to a function only. As of 3.5.0, variables do not have this facility available.

To associate a note with a function (as shown in Figure 4):

  • Open the Function Editor;
  • Click on the function you want to add a note to;
  • In the Function Editor toolbar, click on the Add/Edit Note icon; and
  • Add the text message in the window that appears.
Figure 4. Function Editor, Adding a note

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 modeled variable which is referenced in a function.

The Results Units drop down menu allows you to choose which units to display the output inthe variable or function will evaluate to.  The result units of a function have to commensurate to the required units of the model input it is used for.  If the result units of the function aren't the same as the required units of the model input, a conversion is done of the function result before it passes the value to the model.

By default, if a function or variable is not referenced at any nodemodel input, or any other functions, it is not recorded evaluated 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 You would use Force Evaluation if you wanted to record a function that wasn't being used, or if you had a modeled variable pointing to a function that wasn't evaluated.   The only downside to turning on Force Evaluation for all functions is that the performance will be effected.

The Initial Value for a function is only used when a function references itself.  The Initial Value is in this case for the first evaluation of the function.

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

Info
iconfalse
Note: A function always returns a single value at each time-step. The will be the last evaluated value for the function.

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-

    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;
    • Functions can reference themselves.  The initial value is used in this case for the first evaluation of the function.
    • Circular references are prohibited between multiple functions; and
    • A blank function returns zero.
    Global functions

    Scoping

    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:manager:

    ANU: CAN WE USE PROPER SCREENSHOT HERE

    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 expressionfunction;
    • 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).

    There are several in-built functions and operators that allow you to 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 4. Function manager (comparison operators)

    Operator

    Meaning

    Example

    Result

    >

    greater than

    10 > 5

    true

    <

    less than

    10 < 5

    false

    >=

    greater than or equal

    10 >= 5

    true

    <=

    less than or equal

    10 <= 5

    false

    =

    equal

    10 = 5

    false

    <>

    not equal

    10 <> 5

    true

     

    Table 5. Function manager (logical operators)

    Operator

    Meaning

    Example

    Result

    OR

    or

    false OR true

    true

    AND

    and

    false AND true

    false

    NOT

    not

    NOT false

    true

     

    Table 6. Function manager (general mathematical

    = 5

    true

    <=

    less than or equal

    10 <= 5

    false

    =

    equal

    10 = 5

    false

    <>

    not equal

    10 <> 5

    true

     

    Table 5. Function manager (logical operators)

    Operator

    Meaning

    Example

    Result

    OR

    or

    false OR true

    true

    AND

    and

    false AND true

    false

    NOT

    not

    NOT false

    true

     

    Table 6. Function manager (general mathematical functions)

    Function

    Meaning

    Example use

    EXP

    Returns e raised to the power of a given number

    EXP(number)

    LOG10

    Returns the base-10 logarithm of a number

    LOG10(number)

    LN

    Returns the natural logarithm of a number

    LN(number)

    ABS

    Returns the absolute value of a number

    ABS(number)

    SQRT

    Returns a positive square root

    SQRT(number)

    MOD

    Returns the remainder from division

    MOD(number,divisor)

    ROUND

    Rounds a number to the nearest integer

    ROUND(number)

    INT

    Rounds the number down to the nearest integer

    INT(number)

     

    Table 7. Function manager (statistical functions)

    Function

    Meaning

    Example use

    EXP

    Returns e raised to the power of a given number

    EXP(number)

    LOG10

    Returns the base-10 logarithm of a number

    LOG10(number)

    LN

    Returns the natural logarithm of a number

    LN(number)

    ABS

    Returns the absolute value of a number

    ABS(number)

    SQRT

    Returns a positive square root

    SQRT(number)

    MOD

    Returns the remainder from division

    MOD(number,divisor)

    ROUND

    Rounds a number to the nearest integer

    ROUND(number)

    INT

    Rounds the number down to the nearest integer

    INT(number)

     

    Table 7. Function manager (statistical functions)

    Function

    Meaning

    Example use

    AVERAGE

    Returns the average of its arguments

    AVERAGE(variable_name)

    COUNT

    Counts how many numbers are in the list of arguments

    COUNT(variable_name)

    COUNTIF

    Counts how many numbers in the list of arguments satisfy the expression

    COUNTIF(variable_name,expression)

    MAX

    Returns the maximum value in a list of arguments

    MAX(variable_name) or MAX(number,number)

    MEDIAN

    Returns the median of its arguments

    MEDIAN(variable_name)

    MIN

    Returns the minimum value in a list of arguments

    MIN(variable_name) or MIN(number,number)

    STDEV

    Estimates standard deviation based on a sample

    STDEV(variable_name)

    SUM

    Adds its arguments

    SUM(variable_name)

     

    Table 8. Function manager (trigonometric

    AVERAGE

    Returns the average of its arguments

    AVERAGE(variable_name)

    COUNT

    Counts how many numbers are in the list of arguments

    COUNT(variable_name)

    COUNTIF

    Counts how many numbers in the list of arguments satisfy the expression

    COUNTIF(variable_name,expression)

    MAX

    Returns the maximum value in a list of arguments

    MAX(variable_name) or MAX(number,number)

    MEDIAN

    Returns the median of its arguments

    MEDIAN(variable_name)

    MIN

    Returns the minimum value in a list of arguments

    MIN(variable_name) or MIN(number,number)

    STDEV

    Estimates standard deviation based on a sample

    STDEV(variable_name)

    SUM

    Adds its arguments

    SUM(variable_name)

     

    Table 8. Function manager (trigonometric functions)

    Function

    Meaning

    Example use

    ARCCOS

    Returns the inverse cosine of a number

    ARCCOS(number)

    ARCSIN

    Returns the inverse sine of a number

    ARCSIN(number)

    ARCTAN

    Returns the inverse tangent of a number

    ARCTAN(number)

    COS

    Returns the cosine of a number

    COS(number)

    SIN

    Returns the sine of a number

    SIN(number)

    TAN

    Returns the tangent of a number

    TAN(number)

     

    Table 9. Function manager (miscellaneous functions)

    Function

    Meaning

    Example use

    ARCCOS

    Returns the inverse cosine of a number

    ARCCOS(number)

    ARCSIN

    Returns the inverse sine of a number

    ARCSIN(number)

    ARCTAN

    Returns the inverse tangent of a number

    ARCTAN(number)

    COS

    Returns the cosine of a number

    COS(number)

    SIN

    Returns the sine of a number

    SIN(number)

    TAN

    Returns the tangent of a number

    TAN(number)

     

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

    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)
    current time-step$now.month current time-step $nowday current time-step $nowhour current time-step

    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)

    $Now.DaysInMonthReturns the number of days in the month for the current time-step (range, 0..31)
    $Now.DayOfYear 
    $Start.YearReturns the 4-digit year of the start of simulation date
    $Start.MonthReturns the month of the start of simulation start date(range: 1...12)
    $Start.DayReturns the day of the start of simulation start date (range: 1...31)
    $Start.HourReturns the hour of the start of simulation start date (range 0...23)
      
      
    $End.YearReturns the 4-digit year of the end of simulation start date
    $End.MonthReturns the month of the end of simulation start date (range: 1...12)
    $End.DayReturns the day of the end of simulation start date (range: 1...31)
    $End.HourReturns the hour of the end of simulation start date (range 0...23)
    For example, you can calculate your place in the water year using the calendar date functions.
      
      
    $ActiveInputSetReturns the simulation input set. Can be used in statements like if($ActiveInputSet = "Wet", 15, 10)

     

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

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

     

    ...