# Expression Evaluation

All intrinsic Fortran functions are available through direct coding in the Fortran, DSDYN or DSOUT segments.  However in segments such as Computations, some mathematical and logical functionality is also required:  PSCAD possesses a limited set of its own intrinsic mathematical functions and logical operators for use within segments where direct access Fortran functions is not available.

## Mathematical Functions

Mathematical computations can be performed on input parameters, input signals, or on results of other computations.

The table below lists all of the available intrinsic mathematical functions.  These functions are used only in the Computations segment:

 Function Description CEIL(x) Rounds fraction to next upper integer FLOOR(x) Rounds fraction to next lower integer ROUND(x) Adds 0.5 to a REAL value and then performs an INT function (i.e. ROUND(X) = INT(X+0.5)).  Do not use with negative numbers TRUNC(x) Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x. FRAC(x) Removes integer part of REAL value (left side of decimal) REAL(x) Real part of complex number IMAG(x) Imaginary part of complex number ABS(x) Absolute value ARG(x) Returns the phase angle (or angular component) of the complex number x, expressed in radians. NORM(x) Norm of complex number (x2 + y2) SIN(x) Sine function COS(x) Cosine function TAN(x) Tangent function ASIN(x) Inverse sine function ACOS(x) Inverse cosine function ATAN(x) Inverse tangent function SINH(x) Hyperbolic sine function COSH(x) Hyperbolic cosine function TANH(x) Hyperbolic tangent function SQRT(x) Square root LOG(x) Natural logarithm LOG10(x) Base 10 logarithm EXP(x) Exponential RAND(x) Random value between 0 and x P2RX(m,q) Polar to rectangular conversion (q in degrees) P2RY(m,q) Polar to rectangular conversion (q in degrees) R2PM(x,y) Rectangular to polar conversion R2PA(x,y) Rectangular to polar conversion INT(x) Removes fractional part of REAL value (right side of decimal) NINT(x) Returns the nearest integer to the argument

## Arithmetic Operators

Listed below are the arithmetic operators available in PSCAD.  These operators are used to perform mathematical calculations primarily in the Computations, Fortran, DSDYN and DSOUT segments.

 Function Description + Add - Subtract * Multiply / Divide % Remainder ** Raise to power \ Parallel (xy) / (x +y)

## Logical Operators

Listed below are the logical operators available in PSCAD.  Logical expressions are primarily used in conjunction with #IF, #ELSEIF, #ELSE, #ENDIF directives and the Ternary Operator.  They are also used in the Checks segment.

NOTE:  Logical expressions will return a value of 1 if true, and a value of 0 if false.

 Function Description == Equal to != Not equal to ! Not < Less than > Greater than <= Less than or equal to >= Greater than or equal to || OR && AND

EXAMPLE 10-24:

Logical operators can appear in many different areas.  The following examples illustrate the various ways these operators can be used:

 ! ! ...with #IF, #ELSEIF, #ELSE, #ENDIF Directives ! #IF F >= 60.0       Fout = 60.0 #ENDIF ! ! ...with a Ternary Operator in the Computations !    segment ! REAL L = X == 0.0 ? Y*2.0 : Y/3 ! ! ...in the Checks segment ! ERROR Value too small : R > 0.001 !

## Ternary Operator

The ternary operator is yet another short form method offered in definition script for representing an IF-ELSE-ENDIF type expression.  It allows the user to define a variable, according to certain conditions, in a single line.

The ternary operator is used only in the Computations segment, and should appear as follows:

<Logic> ? <Value_if_True> : <Value_if_False>

<Logic> is a logical expression using logical operators.  <Value_if_True> and <Value_if_False> can either be a single constant, or a mathematical expression.

NOTE:  Care must be taken when designing components with ternary operators:  Ensure that variables used within the ternary expression will not be disabled under otherwise valid conditions.  In other words, unrelated logic may disable one or more of the variables used within the ternary expression, which may render the ternary result invalid.  Input variables are enabled/disabled by way of Conditional Statements, Layers and Filters.

EXAMPLE 10-25:

A user wants to define a REAL variable X in the Computations segment of a component definition.  The value of X is to be 1.0 if an input parameter N is 2 or 3, and defined by a mathematical expression otherwise.

The following code should appear in the Computations segment:

 REAL X = (N==2 || N==3) ? 1.0 : SQRT(2)*V

Where V is a pre-defined constant.

EXAMPLE 10-26:

A user wants to define a REAL variable Torq in the Computations segment of a component definition.  The value of Torq is defined by a mathematical expression, where one element of this expression varies according to a condition.  This can be accomplished by using the ternary operator as follows:

 REAL Torq = (X > 1 ? 0.0 : Tm) + Te*100

Where X, Tm and Te are pre-defined constants.