Contents - Index


Expressions in MODELS


This page contains some general information on how to write expressions in MODELS. An expression is a description of the method by which a value is determined. An expression can be used for specifying the value of a single element or of a group of array elements. The type of value (numerical or logical) resulting from the evaluation of an expression is determined by the type of the operators used in the expression.
The following forms of expressions can be used in MODELS:
- - regular expressions
- - sum expressions
- - derivative expressions
- - use of integrals
________________________________________
1   Regular expressions
A regular expression is either a logical expression or a numerical expression depending on the operators used in the expression. The value of a regular expression is either a single value or an array of values depending on the value descriptions used in the expression. 
Relational operators are:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
= equal to
<> not equal to
Numerical operators:
+ add
- subtract
* multiply
/ divide
** power
MOD modulo
Logical operators
OR
AND

A sub-expression is a regular expression enclosed in parentheses (expr)

A function is called in the following way:  y:=function-name(var1, var2,....varN)

Limits can be applied to all expressions. y:=expr {­plainlimits}
Some examples of regular expressions follow:
a + b[i+2] sum of single element a and of element [i+2] of array b
-sin(2*omega*t) use of resident function sin( )
a AND NOT b logical expression
a>=2*b +c**(2*d) logical expression
-3.582 * 3E6 product of two numerical values
[3, 5, 9, cnt-2] array value of length 4
________________________________________
2  Sum expressions
An expression  is specified as a sum expression when it is necessary to put in evidence the linearity of the expression with respect to the x elements. It can be used as the right-hand side of any value assignment, but is required only in a COMBINE group of statements.
A linear polynomial expression of the form
a0 + a1 x^1 + a2 x^2 + ...
can be used for expressing the value of a variable y.
A linear polynomial is specified in MODELS as follows:
y := sum ( polynomial )
y:=sum(1|+0.3|x1-34.7|x2)
where y can refer to any variable of the model, specified using a single name or a single element of an array
Minimum and maximum limits can be assigned to the value of the sum as follows:
y := sum ( polynomial ) {­ limits }

The following are examples of sum terms:
3.0 | coef = 3.0,  x-element = a
1.0 | b[2*i-6]  coef = 1.0,  x-element = b[2*i-6] 
6.42*a |   coef = 6.42*a, no x-element
1|+0.3|x1-34.7|x2 = 1 + 0.3*x - 34.7*x^2
____________________________________
3   Derivative expressions
References to the value of the first- and second-order time derivatives of an input or variable can be used in any expression of a model, by means of the functions deriv(x) and deriv2(x) (see residential functions). Alternately, a linear polynomial of the time derivatives can be used, of the form:
( a0 + a1 D + a2 D2 + ... ) x
where D is the time derivative operator representing d/dt.
In MODELS, a derivative expression can be used for describing a linear polynomial of derivatives, as follows:
y := derivpol ( D-polynomial ) | x
- where y can refer to any variable of the model, and x can refer to any input or variable of the model, each specified using a single name or a single element of an array
- and where the polynomial is separated from the element to which it is applied by a vertical bar, the "applies to" operator of MODELS, with the meaning of "polynomial applied to x".

Minimum and maximum limit values can be assigned to a derivative polynomial as follows:
y := derivpol ( D-polynomial ) | xlimits }

An expression is specified as a derivative expression when it is necessary to put in evidence the linearity of the expression with respect to the derivatives of x. It can be used as the right-hand side of any value assignment, but is required only in a COMBINE group of statements.
An example of a derivative polynomial is shown below, with its interpretation:
(cos(a)| +2|D +3.56*a|D2 -1|D3) | x         : cos(a)×x +2×dx/dt +3.56×a×d2x/dt2 -d3x/dt3
________________________________________
4   Integrals
The time integral of any input or variable of a model is automatically calculated during the simulation whenever an integral expression is evaluated. The value of the integral of an element at a given time step is a cumulative value dependent on the previous value of the integral and on the present and previous value of the integrated element.
An integral expression is specified as follows:
y := integral ( x )
where y and x can be references to a single element or to a single array element.
Minimum and maximum limits can be assigned to the value of an integral as follows:
y := integral ( x ) {­ limits }
The limit applied to an integral is a "dynamic limit". While an external static limit would simply clip the value of the integral separately from the procedure by which it is calculated, a dynamic limit modifies the calculation procedure itself by recognizing that the past value used in a later calculation of the integral is the integral's limited value. 
When more than one integral expressions affect the same element at a given time step of a simulation, the integral is re-evaluated each time it is used, to reflect possible changes in the value of the element to which it applies. Each new evaluation of the integral during the same time step also releases any previously-applied limit to that integral at that time step.
________________________________________
Integral value reset
The value of an integral can be reset at any time of a simulation, using an integral value assignment. A single value can be assigned to the integral of a single element, of a single array element, or of a group of array elements sharing the same value, by using a single-value integral assignment as
integral(name):=expr
integral(x):=0, integral(x):=t*y+3/x
and an array of values can be assigned to the integral of a group of array elements, using an array-value integral assignment as follows:
integral ( name [ expr .. expr ] ) := array-value expr
integral(x[1..10]):=a[1..10]
Minimum and maximum limits can be attached to the value assigned to the integral, in the expression describing the value.
From the point where a reset value is assigned to an integral until  the end of the execution of that model at that simulation time, the value of the integral becomes fixed, and is not influenced anymore by the value of the integrated variable.
An integral must be initialized in the model's HISTORY part