Versions Compared

Key

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

...

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)

$Now.DaysInMonthReturns the number of days in the month for the current time-step (range, 28..31)
$Now.DayOfYearReturns the current day in the year for the current time-step (range, 1, 366)
$Start.YearReturns the 4-digit year of the simulation start date
$Start.MonthReturns the month of the start of simulation date(range: 1...12)
$Start.DayReturns the day of the simulation start date (range: 1...31)
$Start.HourReturns the hour of the simulation start date (range 0...23)
$Start.DaysInMonthReturns the number of days in the month for the simulation start date (range, 28..31)
$Start.DayOfYearReturns the current day in the year for the simulation start date(range, 1, 366)
$End.YearReturns the 4-digit year of the simulationend date
$End.MonthReturns the month of the simulation end date (range: 1...12)
$End.DayReturns the day of the simulation end date (range: 1...31)
$End.HourReturns the hour of the simulation end date (range 0...23)
$End.DaysInMonthReturns the number of days in the month for the simulation end date (range, 28..31)
$End.DayOfYearReturns the current day in the year for the simulation end date (range, 1, 366)
$ActiveInputSetReturns the simulation input set. Can be used in statements like if($ActiveInputSet = "Wet", 15, 10)

 

Writing functions

The following features in Source help are available when writing functions: 

  • Source attempts to help the user out has an auto-complete feature, which assist with code completion.  If the user types For example, if you enter '$' they will get , a list of available choices appears, showing all the variables and functions they can use. that can be used;  
  • If you press Press Ctrl + Space you can get access to view all the keywords available.  This includes keywords such available keywords, such as 'min' and or 'average'.  These These keywords can also be viewed by using the keyword button on the toolbar.; and
  • 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 a function, you can test its operation using the Parse button. You can enter values for the various terms in your function and confirm that the result of evaluating your function with those values is correct. The Parse button is, by default, hidden in the Function editor, and is used as follows:

  • Select 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);
  • If the variables/functions used in the function aren't in the list, click Parse
  • The expanded section (shown in Figure 7) provides a table of all the variables used in the function. 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 be available at run time to indicate the same.

Types of variables

Modelled variables

A modelled variable provides a connection between your function and model 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 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. See Time of Evaluation for more information; and 
Figure 8. Function manager (Variables tab)

 

...

Custom Functions

Allows you to create your own expression and then import it for use in the Function manager. Refer to Source Plugins /wiki/spaces/SC/pages/51643422. The Parameter count column refers to the number of arguments used in the function.

Figure 13. Function Editor (Custom Functions)

Anchor
Time of Evaluation
Time of Evaluation
Time of Evaluation

Functions are evaluated at the start of each time-step. 3.5.0 Simulation phases shows details of all phases involved in a simulation time-step and Basic Concepts provides a diagrammatic representation.:

  • start of the ordering phase;
  • end of the ordering phase;
  • start of the flow distribution phase; or
  • end of the flow distribution phase.
Info
iconfalse
Note: The Function Editor dialog allows you to link two resource assessment systems using the Time of Evaluation tab. Normally, fields in the function editor are lagged by a time-step (that is, they get their value from the last time-step to use in the current time-step). Enabling the During Resource Assessment checkbox results in the resource assessment system linked parameters being executed within the current time-step. As long as the resource assessment system appears above another resource assessment system in the hierarchical list, the values will be up-to-date in the time-step. Conversely, if one resource assessment system appears below a linked resource assessment system, its values will be lagged by a time-step.

Date ranges

All events occurring during a time-step are independent, such as when a function is evaluated, or when the modeled variable on which the function depends is updated. You can choose the times in which to carry out certain actions. Tables 11 and 12 define the options which are available by default.

If none of the default options is appropriate, you can define your own date ranges using one of the options from the Date range type popup menu (Figure 15).

Table 11. Function manager (date range instances)

Date Range

Meaning

Current Time-step

The most-recently-updated value.

Current Iteration

Only applicable to NetLP. The value updated during the last iteration of the solver.

Current Day

For a daily model, this is a synonym for Current Time-step. For a sub-daily model, it is the sum of the values for the current day.

This Month

For a monthly model, this is a synonym for Current Time-step. For a sub-monthly model, it is the sum of the values for the current month.

This Calendar Year

For a yearly model, this is a synonym for Current Time-step. For a sub-yearly model, it is the sum of the values for the current calendar year.

This Water Year To Date

For a yearly model, this is a synonym for Current Time-step. For a sub-yearly model, it is the sum of the values for the current water year.

Last Time-step

The value of the variable at the completion of the previous iteration of the model.

Previous Day

For a daily model, this is a synonym for Last Time-step. For a sub-daily model, it is the sum of the values for the previous day.

Last Month

For a monthly model, this is a synonym for Last Time-step. For a sub-monthly model, it is the sum of the values for the previous month.

Last Calendar Year

For a yearly model, this is a synonym for Last Time-step. For a sub-yearly model, it is the sum of the values for the previous calendar year.

Last Water Year

For a yearly model, this is a synonym for Last Time-step. For a sub-yearly model, it is the sum of the values for the previous water year.

To calculate a particular date in the water year using the calendar date functions, you can use $now.dayofyear+184 (to indicate the next calendar year) for example, or $now.dayofyear-181 (represents the current calendar year). 

 

Table 12. Function manager (date range sets)

Date Range

Meaning

Example of use

Last Hundred Time-steps

The set of values from the model for the previous 100 iterations.

Lookup($var,35)
(finds 35th value)

Last Seven Days

For a daily or monthly model, this is the set of the values from the last seven time-steps. For a sub-daily model, it is the set of the average daily values for each of the previous seven days.

Average($var)

Previous 30 Days

For a daily or monthly model, this is the set of the values from the last 30 time-steps. For a sub-daily model, it is the set of the average daily values for each of the previous 30 days.

Average($var)

Last 365 Days

For a daily or monthly model, this is the set of the values from the last 365 time-steps. For a sub-daily model, it is the set of the average daily values for each of the previous 365 days.

Average($var)

Custom date ranges fall into three categories:

  • Date Range Calendar (Figure 13) allows you to define a precise start and end date during which the model variable is considered to be valid. Enabling the Is Recurring checkbox ignores the years. In other words, the period between [start day/start month] and [end day/end month] is valid for every year during model execution.
  • Date Range Period (Figure 14) allows you to specify the date range in terms of a period of time that is relative to the current time-step. A period can range from one hour to one year and is controlled via the Period popup menu. The number of periods included in the range is controlled by the X periods ago(start) and Y periods ago(end) fields where end ≥ start. A value of zero refers to the current period. For example:
    • Daily period where start=0 and end=0 refers to the current day;
    • Daily period where start=1 and end=1 refers to the previous day.
  • The Date Range Time Step option (Figure 15) allows you to specify the date range in terms of time-steps. The number of time-steps included in the range is controlled by the X time-steps ago(start) and Y time-steps ago(end) fields where where end ≥ start. A value of zero refers to the current time-step. For example:
    • If start = 0 and end = 0, this refers to the current time-step;
    • If start = 1 and end = 1, this refers to the last time-step;
    • If start = 0 and end = 6, this refers to the last seven time-steps including the current time-step.
Figure 14. Function Editor (Date Range, Date range calendar)

Figure 15. Function Editor (Date Range, period)

Figure 16. Function Editor (Date Range, time-step)

For all three custom calendar options:

  • The Update Frequency popup menu offers a choice of OncePerTimeStep or OncePerIteration. The latter setting is only applicable to NetLP.
  • The Return set popup menu controls the granularity of the returned result:
    • If the range represents a single value, that value is returned regardless of this setting.
    • Table 13 summarises what is returned if the range represents more than one value.
Table 13. Function Editor, Date Ranges (Variables)
OptionSummaryResult
Simple SetReturns the set of observations without modification[1,2,3,4,5]
Set of Averages

Returns a set of the same size where each member is the average of all of the members of the set

[3,3,3,3,3]
Set of Progressive Averages

Returns a set of the same size where each member is the original value of that member averaged with the value of the first member.

[1,1.5,2,2.5,3]

Where: input is the series [1,2,3,4,5]

Info
iconfalse
Note: While the user interface uses the word set to refer to the concept of returning multiple values, because the values are both ordered and can be repeated, it is probably better to think of these as lists.

Example

As an example, you can used use a function or expression at a Maximum Order Constraint node to limit the maximum orders for each time-step. Assume that a channel constraint of 80 ML/day is required to prevent flows going overbank, except during floods (defined as more than 2,000 ML/day) when up to 3,000 ML/day is permitted as an environmental flow.

Figure 16 shows a fragment of the river network. Downstream demands are represented by a supply point and water user node. In the Function Editor, you specify the internal orders and unregulated river gain of the model using modelled variables. Maximum orders are then defined using an expression.

Figure 17. Maximum Order Constraint node (example)

In this case, the following components will be assigned:

  • Storage 2: Requested flow rate - say, $Orders with a data range of Last time-step; and
  • Inflow 3: Inflow - $predictedInflow, with the same data range as Storage 2.

Note that both variables will have ML/day as designated units.

Then, the following expression will return a value of 3,000 ML/day if flows are above 2,000 ML/day but 80 ML/day otherwise.

If(($predictedinflow+$orders) > 2000, 3000, 80 

Debugging


Functions are for generally used by experienced users, and as it can be quite difficult to understand what is happening internally to within the software throughout (mainly during evaluations).  Given Given this, there are 3 current methods to gather the three ways of gathering information required to debug your functions:
  • Recording : - Recording is available for functions, pattern variables, and time series variables;
  • Logging : - The result of a function can be logged into the Log Reporter at a given day, by using the 'Add Log Date' context menu option on for a function.  This This will output a log entry for each date specified.  This This output will include the result of the function and , as well as all the functions & and variables it uses at each time of evaluation.; and
  • Execution Order : - The execution order can be viewed for each Time of Evaluation. Choose Tools » Function Execution Order to open this dialog.