Expressions can consist of one or more of these components: numerical constants, symbolic names, mathematical operators, functions, and conditional expressions.
Numerical constants are any numeric values, consisting of the digits 0-9, with an optional decimal point and a leading minus sign (for negative values). A leading 0 is optional for numbers less than 1.0. Standard scientific notation is provided (e.g. 0.12345E3 for 123.45), or you can enter very small or very large numbers by multiplying a value by 10 raised to a power, e.g. 0.12345*10^11 or 0.12345*10^-11. See also the section on Arrays and lists for details of how to enter arrays of numerical constants.
A symbolic name represents the value of one of the variables, compartments or flows that influences the element in question. Each time the value of the expression is calculated, the current value of the influencing element is used.
The only symbolic names for model quantities that you can enter into an expression are those that influence the element in question. These are listed by clicking on the pull-down menu button on the equation bar, and in the "Parameter" column of the main tab of the equation dialogue box. These must be entered exactly as shown, using the same punctuation (note especially the _ underscore symbols), and using the same case. The easiest way to do this is to select them from the pull-down menu, or to double-click on the name in the "Parameter" column.
Note that the symbolic name entered into the expression is the local name. This can be different from the name displayed on the model diagram, either to make the expression easier to read, or to avoid using certain illegal characters. Local names will also be modified when values come from a multi-instance submodel.
You can use the following mathematical operators:
+ |
e.g. 15+7 (evaluates to 22) |
|
- |
e.g. 15-7 (evaluates to 8) |
|
* |
e.g. 15*7 (evaluates to 105) |
|
/ |
e.g. 15/7 (evaluates to 2.1428…) |
|
// |
integer division |
e.g. 15//7 (evaluates to 2) |
% |
integer modulo |
e.g. 94%22 (evaluates to 6) |
^ |
raise to a power |
e.g. 15^2 (evaluates to 225) |
Simile provides a large number of functions that you can use in expressions already built-in, and the facility to extend this list.
Equations can include conditional expressions. These enable a complex expression to be constructed from a number of sub-expressions, with the conditional elements being used to select between one expression or another. Standard Boolean operators are available for constructing conditional expressions.
The equation dialogue box can be used to enter one or more assignments, before the expression that returns the element's value. These intermediate variables can be used to simplify complex expressions.
In: Contents >> Working with equations
Boolean expressions return only "true" or "false". Boolean expressions are commonly used in conditional expressions to decide between two or more alternative subexpressions, but a Simile variable can have a boolean value if its whole equation is a boolean expression; indeed, condition and alarm components have to have boolean values.
The expressions "true" and "false" may be used as Boolean constants in expressions. Note that quotation marks must be used around the words. This is because these constants are implemented as built-in enumerated types.
A simple condition consists of a direct comparison between two values, using the relational operators
> |
greater than |
X > Y |
X is greater than Y |
< |
less than |
X < Y |
X is less than Y |
>= |
greater than or equal to |
X >= Y |
X is greater than or equal to Y |
<= |
less than or equal to |
X <= Y |
X is less than or equal to Y |
== |
equal to |
X == Y |
X is equal to Y |
!= |
not equal to |
X != Y |
X is not equal to Y |
Thus, the following are legal examples:
Each of the two terms, on the left and right of the comparison, can themselves be arbitrary mathematical expressions. Thus, the following are also legal:
Four Boolean operators can be used in expressions. There are "and", "or", "xor" and "not". The following table shows the two alternative forms of these operators, which can also written in symbolic form. Note also that a comma "," is accepted as an alternative symbol for "and".
A and B |
A && B |
true if and only if both A and B are true |
A or B |
A ; B |
true if either A is true or B is true |
A xor B |
A != B |
true if either A is true or B is true, but not both |
not A |
! A |
true if and only if A is not true |
The truth tables for the four operators are given here. Note that "and", "or" and "xor" require two operands, whilst "not" requires one operand. The equation parser will signal an error if an incorrect number of operands is used.
Some functions return boolean values, i.e., any(), all(), channel_is(), dies_of(), and first().
In: Contents >> Working with equations >> Components of an equation
Simple equations, like Y = 5+7*X, provide a single, continuously-varying method for calculating a value for an element from the value(s) for one or more other elements. Often, however, the expression we want to use for calculating the result depends on the values of one or more aspects of the system. This means that we need a way, in a single equation, for choosing between alternative expressions. Conditional expressions provide a means for doing this.
In general terms, a simple equation has the form:
Y = expression.
A conditional equation has the form:
Y = if condition1 then expression1 else expression2
Y = if condition1 then expression1 elseif condition2 then expression2 elseif condition3 then expression3 ...... else expressionx
where:
expression is any legal Simile mathematical expression, including numeric constants, variables, mathematical operators and functions - and indeed further conditional expressions (i.e. conditional expressions can be nested). When you nest condironal expressions, the nested if...then...else... must be enclosed in round brackets. E.g. if a>5 then (if b<3 then 5 else 4) else 0
Note that while some programming languages, such as C and Fortran, allow you to use a numerical value of 1 or 0 to represent "true" or "false" in a conditional expression, Simile does not allow this. We feel that this practice makes the equations less readable. Instead, you can convert the number n into a boolean, using the expression "n !=0 ".
In: Contents >> Working with equations >> Components of an equation
In a procedural programming language, a complicated calculation may be broken down into several steps each carried out by a separate instruction. This may be done when an intermediate result is subsequently required more than once to save calculating it each time, or just to make the program more readable.
In Simile, a similar result could easily be achieved by having a chain of variables linked by influences, where each except the first and last represents an intermediate value. However this could make the diagram more confusing than it need be.
Alternatively you can define and then use intermediate results in the equation. They are assigned with a single '=' sign and the assignment is separated from the rest of the equation with a comma, like this:
a = f(input1,...), b = g(a, input1...), result = h(a, b, input1...)
Local variable assignments can be made before an expression, separated from the expression by a comma. The expression returning the value of the element must come at the end. The local variables can be used in the expression, often to simplify it.
In the following example, the local variable q is assigned a value, then the main expression follows after a comma, with q being used to simplify what would otherwise be an extremely complex expression:
q=(Topt-Tmin)/(Tmax-Topt),
if ((T>Tmin)&&(T<Tmax)) then
(((T-Tmin)^q) * (Tmax-T)) / (((Topt-Tmin)^q) * (Tmax-Topt))
else
0
There are four parameters influencing this expression, T, Topt, Tmin, and Tmax, through influence arrows in the normal way. The assignment of a value to q is a purely local affair.
Note that the assignment of the local variable uses a single = sign. This is not to be confused with the double == of an equality test.
In: Contents >> Working with equations >> Components of an equation