CODEGEN Blocks Library
CODEGEN provides a library of reusable modeling blocks for constructing user-defined dynamic models. Each block is specified by a keyword (prefixed with &), followed by input/output state names and parameters. Parameters can be literal values, named data enclosed in braces {T}, or arithmetic expressions.
Block Reference
Section titled “Block Reference”Algebraic & Mathematical
Section titled “Algebraic & Mathematical”| Block | Description |
|---|---|
abs | Absolute value of input |
algeq | Algebraic equation |
max1v1c | Maximum of a state and a constant |
max2v | Maximum of two states |
min1v1c | Minimum of a state and a constant |
min2v | Minimum of two states |
nint | Nearest integer |
pwlin | Piecewise linear function |
Transfer Functions
Section titled “Transfer Functions”| Block | Description |
|---|---|
tf1p | First-order: |
tf1plim | First-order with non-windup output limits |
tf1pvlim | First-order with variable non-windup limits |
tf1p2lim | First-order with rate limits and output limits |
tf1p1z | One zero, one pole: |
tf2p2z | Two zeros, two poles |
tfder1p | Derivative (washout): |
Integrators
Section titled “Integrators”| Block | Description |
|---|---|
int | Integrator with time constant |
inlim | Integrator with non-windup limits |
invlim | Integrator with variable non-windup limits |
Controllers
Section titled “Controllers”| Block | Description |
|---|---|
pictl | Proportional-Integral controller |
pictllim | PI with non-windup limit on integral |
pictl2lim | PI with limits on integral and total output |
pictlieee | PI with non-windup limit (IEEE formulation) |
Limiters & Switching
Section titled “Limiters & Switching”| Block | Description |
|---|---|
lim | Limiter with constant bounds |
limvb | Limiter with variable bounds |
db | Deadband |
hyst | Hysteresis |
switch | Select one of inputs |
swsign | Switch based on sign of input |
Frequency Estimation
Section titled “Frequency Estimation”| Block | Description |
|---|---|
f_inj | Frequency at bus of injector |
f_twop_bus1 | Frequency at first bus of two-port |
f_twop_bus2 | Frequency at second bus of two-port |
Automata & Timers
Section titled “Automata & Timers”| Block | Description |
|---|---|
fsa | Finite State Automaton |
tsa | Two-state automaton |
timer | Timer with piecewise-linear delay |
timersc | Timer with staircase delay |
Block Details
Section titled “Block Details”Algebraic & Mathematical
Section titled “Algebraic & Mathematical”Absolute value of input.
Syntax
& absx_ix_jx_i is the input state; x_j is the output state (the absolute value of x_i).
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = 1: if x_i < 0: z ← -1else: # z = -1 if x_i > 0: z ← 1Initialization
if x_i > 0: z ← 1else: z ← 0Notes
The model has a discontinuous derivative at . It is implemented internally with a small hysteresis; see the hyst block with , , , , , , where is the absolute accuracy used to solve the algebraic equations in RAMSES.
Algebraic equation.
Syntax
& algeqmath expressionInternal states: none
Discrete variables: none
Description
This block forces an algebraic constraint (or equation) involving one or several states:
where is the number of states ().
Notes
This block does not have distinct “inputs” and “outputs”. Those roles emerge from the rest of the model that incorporates the algebraic constraint.
max1v1c
Section titled “max1v1c”Maximum between a state and a constant.
Syntax
& max1v1cx_ix_j{C}x_i is the input state, x_j is the output (), and C is a constant (data name, parameter name, or math expression).
Internal states: none
Discrete variable:
Equations
When the output is clamped to ; when the output tracks .
Discrete transitions
if z = 1: if x_i > C: z ← 2else: # z = 2 if x_i <= C: z ← 1Initialization
if x_i < C: z ← 1else: z ← 2Maximum between two states.
Syntax
& max2vx_ix_jx_kx_i and x_j are the two input states; x_k is the output ().
Internal states: none
Discrete variable:
Equations
When the output follows ; when the output follows .
Discrete transitions
if z = 1: if x_i < x_j: z ← 2else: # z = 2 if x_j <= x_i: z ← 1Initialization
if x_i > x_j: z ← 1else: z ← 2min1v1c
Section titled “min1v1c”Minimum between a state and a constant.
Syntax
& min1v1cx_ix_j{C}x_i is the input state, x_j is the output (), and C is a constant (data name, parameter name, or math expression).
Internal states: none
Discrete variable:
Equations
When the output tracks ; when the output is clamped to .
Discrete transitions
if z = 1: if x_i > C: z ← 2else: # z = 2 if x_i <= C: z ← 1Initialization
if x_i < C: z ← 1else: z ← 2Minimum between two states.
Syntax
& min2vx_ix_jx_kx_i and x_j are the two input states; x_k is the output ().
Internal states: none
Discrete variable:
Equations
When the output follows ; when the output follows .
Discrete transitions
if z = 1: if x_i > x_j: z ← 2else: # z = 2 if x_j >= x_i: z ← 1Initialization
if x_i < x_j: z ← 1else: z ← 2Integer nearest to the input shifted by a constant .
Syntax
& nintx_ix_j{c}x_i is the input state, x_j is the output (nearest integer), and c is a shift constant (data name, parameter name, or math expression).
Internal states: none
Discrete variable: (the integers)
Equations
Discrete transitions
if nint(x_i + c) ≠ z: z ← nint(x_i + c)where returns the nearest integer.
Initialization
Notes — particular cases
- With : the output is the integer nearest to .
- With : the output is the floor of (largest integer ).
- With : the output is the ceiling of (smallest integer ).
Limiters & Switching
Section titled “Limiters & Switching”Limiter with constant bounds.
Syntax
& limx_ix_j{x_min}{x_max}x_i is the input, x_j is the (limited) output. x_min and x_max are constant bounds (data names, parameter names, or math expressions).
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = 0: if x_i > x_max: z ← 1 else if x_i < x_min: z ← -1else if z = 1: if x_i < x_max: z ← 0else: # z = -1 if x_i > x_min: z ← 0Initialization
if x_j > x_max: z ← 1else if x_j < x_min: z ← -1else: z ← 0Limiter with variable bounds.
Syntax
& limvbx_ix_minx_maxx_jx_i is the input; x_min and x_max are variable (state) lower and upper bounds; x_j is the (limited) output.
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = 0: if x_i > x_max: z ← 1 else if x_i < x_min: z ← -1else if z = 1: if x_i < x_max: z ← 0else: # z = -1 if x_i > x_min: z ← 0Initialization
if x_i > x_max: z ← 1else if x_i < x_min: z ← -1else: z ← 0Notes
Unlike lim, the bounds and are model states (variables) rather than fixed parameters, allowing dynamic limit variation during simulation.
Deadband.
Syntax
& dbx_ix_j{delta_1}{s_1}{a_1}{delta_2}{s_2}{a_2}x_i is the input, x_j is the output. Parameters , , , , , are data names, parameter names, or math expressions.
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z ∈ {0, 1}: if x_i < delta_1: z ← -1else if z ∈ {-1, 0}: if x_i > delta_2: z ← 1else if z ∈ {-1, 1}: if delta_1 < x_i < delta_2: z ← 0Initialization
if x_i > delta_2: z ← 1else if x_i < delta_1: z ← -1else: z ← 0Notes
- The data must satisfy , , and .
- A common special case is and , although all values are permitted.
- The pwlin4, pwlin5, or pwlin6 blocks can be used as alternatives.
Hysteresis.
Syntax
& hystx_ix_j{x_I}{y_IB}{y_IA}{x_D}{y_DB}{y_DA}{z0}x_i is the input, x_j is the output. The parameters , , , , , define the hysteresis shape. z0 specifies the initial discrete state when the input starts in the indeterminate region.
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = -1: if x_i > x_I: z ← 1else: # z = 1 if x_i < x_D: z ← -1Initialization
if x_i > x_I: z ← 1else if x_i < x_D: z ← -1else: if z0 >= 0: z ← 1 else: z ← 0Notes
At , if the initial operating branch is indeterminate — the system could operate on either the line () or the line (). The user must therefore supply z0 to resolve the ambiguity.
If then is initialised to ; if then is initialised to , and z0 is ignored in both cases.
The data must satisfy . The degenerate case is not allowed by this block but can be handled with the pwlin4 block.
Frequency Estimation
Section titled “Frequency Estimation”Computes , an estimate of the frequency (in per unit) at a given bus, from the evolution of the rectangular components and of the bus voltage. A measurement time constant is involved. Can be used in the model of an injector only.
Syntax
& f_injf{T}f is the name of the estimated frequency variable; T is the measurement time constant (data name, parameter name, or math expression). The voltage components and are automatically inherited and must not be declared.
Internal states: and (measured/filtered voltage components)
Discrete variables: none
Equations
Initialization
Explanation
The voltage components and are the projections of the bus voltage phasor onto the reference axes rotating at angular speed (rad/s). The instantaneous angular frequency is:
The per-unit frequency is therefore:
where is the nominal frequency (known from the system data). To filter transients, and are passed through first-order filters with time constant , yielding the measured values and . Substituting the filter derivatives into the frequency expression yields the third equation above.
Notes
- A recommended value for is in the range – s.
- is not allowed. If is too small, the solver may encounter a singularity and the simulation may fail.
f_twop_bus1
Section titled “f_twop_bus1”Similar to f_inj. Computes , an estimate of the frequency (in per unit) at the first bus of a given two-port. Can be used in the model of a two-port only.
Syntax
& f_twop_bus1f{T}f is the name of the estimated frequency variable; T is the measurement time constant (data name, parameter name, or math expression).
Internal states: and (filtered voltage components at bus 1)
Discrete variables: none
Equations, initialization, and notes: identical to f_inj — refer to that block for the full mathematical description. The only difference is that this block uses the voltage at the first bus of a two-port rather than an injector bus.
f_twop_bus2
Section titled “f_twop_bus2”Similar to f_inj. Computes , an estimate of the frequency (in per unit) at the second bus of a given two-port. Can be used in the model of a two-port only.
Syntax
& f_twop_bus2f{T}f is the name of the estimated frequency variable; T is the measurement time constant (data name, parameter name, or math expression).
Internal states: and (filtered voltage components at bus 2)
Discrete variables: none
Equations, initialization, and notes: identical to f_inj — refer to that block for the full mathematical description. The only difference is that this block uses the voltage at the second bus of a two-port rather than an injector bus.
Automata & Timers
Section titled “Automata & Timers”Finite State Automaton.
This block forces a set of algebraic equations. There are possible sets, each corresponding to a value of the discrete state . Transitions between sets occur when Boolean expressions evaluate to true.
Syntax
The block is specified via an inline example. For states and algebraic constraints per state:
& fsainitial_state# 1algebraic constraint No. 1algebraic constraint No. 2-> 2boolean expression C1# 2algebraic constraint No. 3algebraic constraint No. 4-> 1boolean expression C2-> 3boolean expression C3-> 3boolean expression C4# 3algebraic constraint No. 5algebraic constraint No. 6-> 1boolean expression C5##Internal states: none
Discrete variable: — the index of the currently active state
Syntax rules
| Token | Meaning |
|---|---|
# N | Begins the section for state . States must be numbered consecutively from 1 to in increasing order. |
## | Marks the end of the state list. |
-> N | Declares a transition to state . The following line is the Boolean expression that triggers the transition. |
| Second line | Integer specifying the initial state of the automaton. |
Notes
- The number of algebraic constraints must be identical across all states.
- Multiple transitions from the same state to the same target state are permitted (e.g., two separate conditions C3 and C4 both targeting state 3 in the example). Alternatively a single transition with the combined condition
C3 or C4may be used. - Transitions and algebraic constraints may involve any model states and parameters.
Integrators
Section titled “Integrators”Integrator with (positive) time constant .
Syntax
& intx_ix_j{T}x_i is the input, x_j is the integrated output, and T is the (positive) time constant (data name, parameter name, or math expression).
Internal states: none
Discrete variables: none
Equation
Notes
is not allowed. If is too small, the solver may encounter a singularity and the simulation may fail.
Integrator with (positive) time constant and non-windup limits on output.
Syntax
& inlimx_ix_j{T}{x_min}{x_max}x_i is the input, x_j is the limited integrated output. T is the time constant; x_min and x_max are constant output limits (data names, parameter names, or math expressions).
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = 0: if x_j > x_max: z ← 1 else if x_j < x_min: z ← -1else if z = 1: if x_i < 0: z ← 0else: # z = -1 if x_i > 0: z ← 0Initialization
if x_j > x_max: z ← 1else if x_j < x_min: z ← -1else: z ← 0Notes
The limits are non-windup: when the output reaches a limit, integration stops and the limit is enforced as an algebraic constraint. Integration resumes (in the direction away from the limit) once the input drives the output back into the feasible range.
is not allowed. If is too small, the solver may encounter a singularity and the simulation may fail.
invlim
Section titled “invlim”Integrator with (positive) time constant and non-windup limits on output. The lower and upper limits are variables.
Syntax
& invlimx_ix_minx_maxx_j{T}x_i is the input; x_min and x_max are variable (state) lower and upper limits; x_j is the limited integrated output; T is the time constant (data name, parameter name, or math expression).
Internal states: none
Discrete variable:
Equations
Discrete transitions
if z = 0: if x_j > x_max: z ← 1 else if x_j < x_min: z ← -1else if z = 1: if x_i < 0: z ← 0else: # z = -1 if x_i > 0: z ← 0Initialization
if x_j > x_max: z ← 1else if x_j < x_min: z ← -1else: z ← 0Notes
Unlike inlim, the bounds and are model states (variables) that can change during simulation, enabling dynamic limit adjustment.
The non-windup behaviour is identical to inlim: integration halts when a limit is reached and resumes once the input drives the output away from that limit.
is not allowed. If is too small, the solver may encounter a singularity and the simulation may fail.
Controllers
Section titled “Controllers”Proportional-Integral (PI) controller.
Syntax
Section titled “Syntax”& pictlname of variable x_kname of variable x_jdata name, parameter name or math expression for K_idata name, parameter name or math expression for K_pInternal States
Section titled “Internal States”
Discrete Variables
Section titled “Discrete Variables”None.
Equations
Section titled “Equations”Initialization
Section titled “Initialization”
pictllim
Section titled “pictllim”Proportional-Integral (PI) controller with non-windup limit on the integral term.
Syntax
Section titled “Syntax”& pictllimname of variable x_kname of variable x_jdata name, parameter name or math expression for K_idata name, parameter name or math expression for K_pdata name, parameter name or math expression for x_i^mindata name, parameter name or math expression for x_i^maxInternal States
Section titled “Internal States”
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”
Discrete Transitions
Section titled “Discrete Transitions”if z = 0: if x_i > x_i^max: z ← 1 else if x_i < x_i^min: z ← -1else if z = 1: if K_i * x_k < 0: z ← 0else if z = -1: if K_i * x_k > 0: z ← 0Initialization
Section titled “Initialization”if K_i * x_k > 0: z ← 1; x_i ← x_i^maxelse if K_i * x_k < 0: z ← -1; x_i ← x_i^minelse: z ← 0; x_i ← x_jpictl2lim
Section titled “pictl2lim”Proportional-Integral (PI) controller with non-windup limit on the integral term and limit on the proportional term.
Syntax
Section titled “Syntax”& pictl2limname of variable x_kname of variable x_jdata name, parameter name or math expression for K_idata name, parameter name or math expression for K_pdata name, parameter name or math expression for x_i^mindata name, parameter name or math expression for x_i^maxdata name, parameter name or math expression for x_p^mindata name, parameter name or math expression for x_p^maxInternal States
Section titled “Internal States”and
Discrete Variables
Section titled “Discrete Variables”and
Equations
Section titled “Equations”
Discrete Transitions
Section titled “Discrete Transitions”# Proportional limiter (z_1):if z_1 = 0: if x_p > x_p^max: z_1 ← 1 else if x_p < x_p^min: z_1 ← -1else if z_1 = 1: if K_p * x_k < x_p^max: z_1 ← 0else if z_1 = -1: if K_p * x_k > x_p^min: z_1 ← 0
# Integral limiter (z_2):if z_2 = 0: if x_i > x_i^max: z_2 ← 1 else if x_i < x_i^min: z_2 ← -1else if z_2 = 1: if K_i * x_k < 0: z_2 ← 0else if z_2 = -1: if K_i * x_k > 0: z_2 ← 0Initialization
Section titled “Initialization”Initialization of :
Initialization of and discrete variables:
if K_p * x_k > x_p^max: z_1 ← 1else if K_p * x_k < x_p^min: z_1 ← -1else: z_1 ← 0
if K_i * x_k > 0: z_2 ← 1; x_i ← x_i^maxelse if K_i * x_k < 0: z_2 ← -1; x_i ← x_i^minelse: z_2 ← 0; x_i ← x_j - x_ppictlieee
Section titled “pictlieee”Proportional-Integral (PI) controller with non-windup limit on the integral term, compliant with IEEE standards.
This PI controller implements the non-windup integrator specified in:
- IEEE Std 421.5-1992, IEEE Std 421.5-2005, and IEEE Std 421.5-2016 (excitation system models for power system stability studies).
The IEEE standard specifies that the integrator state is frozen as soon as reaches its lower or upper limit. To avoid limit cycles when the integrator is released, a second “open-loop” integrator tracks what would be if unconstrained. The lower integrator is re-activated only when re-enters .
Syntax
Section titled “Syntax”& pictlieeename of variable x_kname of variable x_jdata name, parameter name or math expression for K_idata name, parameter name or math expression for K_pdata name, parameter name or math expression for x_j^mindata name, parameter name or math expression for x_j^maxInternal States
Section titled “Internal States”and
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”The tests marked (*) use (the open-loop integrator) to decide when the active integrator can be released.
if z = 0: if x_j > x_j^max: z ← 2 else if x_j < x_j^min: z ← -2else if z = 2: if K_p*x_k + x_1 < x_j^max: z ← 1else if z = 1: if K_p*x_k + x_1 > x_j^max: z ← 2 else if K_p*x_k + x_2 < x_j^max: z ← 0 (*)else if z = -2: if K_p*x_k + x_1 > x_j^min: z ← -1else if z = -1: if K_p*x_k + x_1 < x_j^min: z ← -2 else if K_p*x_k + x_2 > x_j^min: z ← 0 (*)Initialization
Section titled “Initialization”if x_j >= x_j^max: z ← 2; x_1 ← x_j^max - K_p*x_k; x_2 ← x_1else if x_j <= x_j^min: z ← -2; x_1 ← x_j^min - K_p*x_k; x_2 ← x_1else: z ← 0; x_1 ← x_j; x_2 ← x_1Transfer Functions
Section titled “Transfer Functions”Transfer function between input and output: one time constant.
Syntax
Section titled “Syntax”& tf1pname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for TInternal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”None.
Equations
Section titled “Equations”
The time constant can be zero, in which case the block behaves as a simple gain .
tf1plim
Section titled “tf1plim”Transfer function between input and output: one time constant with non-windup limits on output.
Syntax
Section titled “Syntax”& tf1plimname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for Tdata name, parameter name or math expression for x_mindata name, parameter name or math expression for x_maxInternal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = 0: if x_j > x_max: z ← 1 else if x_j < x_min: z ← -1else if z = 1: if G*x_i - x_j < 0: z ← 0else if z = -1: if G*x_i - x_j > 0: z ← 0Initialization
Section titled “Initialization”if x_j >= x_max: z ← 1else if x_j <= x_min: z ← -1else: z ← 0The time constant can be zero. In this case the block behaves as a gain () but the limits and remain in effect.
tf1pvlim
Section titled “tf1pvlim”Transfer function between input and output: one time constant with non-windup limits on output. The limits are variables.
This block is identical in behaviour to tf1plim except that and are variable states rather than fixed data parameters.
Syntax
Section titled “Syntax”& tf1pvlimname of variable x_iname of variable x_jname of variable x_minname of variable x_maxdata name, parameter name or math expression for Gdata name, parameter name or math expression for TInternal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = 0: if x_j > x_max: z ← 1 else if x_j < x_min: z ← -1else if z = 1: if G*x_i - x_j < 0: z ← 0else if z = -1: if G*x_i - x_j > 0: z ← 0Initialization
Section titled “Initialization”if x_j >= x_max: z ← 1else if x_j <= x_min: z ← -1else: z ← 0The time constant can be zero. In this case the block behaves as a gain () but the variable limits remain in effect.
tf1p2lim
Section titled “tf1p2lim”Transfer function between input and output: one time constant, with limits on rate of change of output and non-windup limits on output.
Syntax
Section titled “Syntax”& tf1p2limname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for Tdata name, parameter name or math expression for x_mindata name, parameter name or math expression for x_maxdata name, parameter name or math expression for dx_min/dtdata name, parameter name or math expression for dx_max/dtInternal States
Section titled “Internal States”, initialized at
Discrete Variables
Section titled “Discrete Variables”(rate limiter) and (output limiter)
Equations
Section titled “Equations”(resp. ) is the maximum (resp. minimum) rate of change of with time.
Discrete Transitions
Section titled “Discrete Transitions”# Rate limiter (z_1):if z_1 = 0: if x_1 > T*dx_max: z_1 ← 1 else if x_1 < T*dx_min: z_1 ← -1else if z_1 = 1: if G*x_i - x_j < T*dx_max: z_1 ← 0else if z_1 = -1: if G*x_i - x_j > T*dx_min: z_1 ← 0
# Output limiter (z_2):if z_2 = 0: if x_j > x_max: z_2 ← 1 else if x_j < x_min: z_2 ← -1else if z_2 = 1: if x_1 < 0: z_2 ← 0else if z_2 = -1: if x_1 > 0: z_2 ← 0Initialization
Section titled “Initialization”if G*x_i - x_j > T*dx_max: z_1 ← 1else if G*x_i - x_j < T*dx_min: z_1 ← -1else: z_1 ← 0
if x_j > x_max: z_2 ← 1else if x_j < x_min: z_2 ← -1else: z_2 ← 0The time constant can be zero. In this case throughout the simulation, the block behaves as a gain , and both limiters are ignored (, , , ).
tfder1p
Section titled “tfder1p”Transfer function: derivative with one time constant.
The transfer function is .
Syntax
Section titled “Syntax”& tfder1pname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for TInternal States
Section titled “Internal States”
Discrete Variables
Section titled “Discrete Variables”None.
Equations
Section titled “Equations”Initialization
Section titled “Initialization”
The model allows . In this case as expected; the second equation is formally retained but becomes redundant.
tf1p1z
Section titled “tf1p1z”Transfer function between input and output: one zero and one pole.
The transfer function is .
Syntax
Section titled “Syntax”& tf1p1zname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for T_zdata name, parameter name or math expression for T_pInternal States
Section titled “Internal States”
Discrete Variables
Section titled “Discrete Variables”None.
Equations
Section titled “Equations”Initialization
Section titled “Initialization”
- is allowed: the block reduces to (i.e.,
tf1p). - is formally allowed but produces a pure differentiator ; undesired transients may occur at discontinuities of or .
- : is initialized to zero and remains zero, giving .
tf2p2z
Section titled “tf2p2z”Transfer function between input and output: two real zeros and two real poles.
The transfer function is .
Syntax
Section titled “Syntax”& tf2p2zname of variable x_iname of variable x_jdata name, parameter name or math expression for Gdata name, parameter name or math expression for n_1data name, parameter name or math expression for n_2data name, parameter name or math expression for d_1data name, parameter name or math expression for d_2Internal States
Section titled “Internal States”and
Discrete Variables
Section titled “Discrete Variables”None.
Equations
Section titled “Equations”State-space controllable canonical form:
Initialization
Section titled “Initialization”
Exception. If , both and are set to zero and the transfer function reduces to:
as in the tf1p1z block. If additionally , both and are set to zero and the block becomes a simple gain .
Switching
Section titled “Switching”Piece-wise linear function of input, defined by points. Separate blocks exist for and .
Syntax
Section titled “Syntax”& pwlin3name of variable x_iname of variable x_jdata/parameter/expression for v_x(1)data/parameter/expression for v_y(1)data/parameter/expression for v_x(2)data/parameter/expression for v_y(2)data/parameter/expression for v_x(3)data/parameter/expression for v_y(3)For pwlin4, pwlin5, and pwlin6, append additional pairs up to , , or respectively.
Internal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”
Discrete Transitions
Section titled “Discrete Transitions”if x_i < v_x(1): z ← 1else if x_i >= v_x(n): z ← n-1else: for k = 1 to n-1: if v_x(k) <= x_i < v_x(k+1): z ← kInitialization
Section titled “Initialization”Same logic as discrete transitions.
- The values must be strictly increasing at the endpoints: and ; intermediate values may be equal: .
- For or , is obtained by linear extrapolation from the first or last two points respectively.
switch
Section titled “switch”Set the output state to one among input states, based on the value of a controlling state. Separate blocks exist for and .
Syntax
Section titled “Syntax”& switch2name of variable x_i(1)name of variable x_i(2)name of variable x_k (selector)name of variable x_j (output)For switch3–switch5, prepend additional input variables through before .
Internal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”
Discrete Transitions
Section titled “Discrete Transitions”
where returns the nearest integer.
Initialization
Section titled “Initialization”Same as discrete transitions.
swsign
Section titled “swsign”Switch between two input states, based on the sign of a third input state.
Syntax
Section titled “Syntax”& swsignname of variable x_i (selected when x_k >= 0)name of variable x_j (selected when x_k < 0)name of variable x_k (sign selector)name of variable x_l (output)Internal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = 1: if x_k < 0: z ← 2else if z = 2: if x_k >= 0: z ← 1Initialization
Section titled “Initialization”if x_k < 0: z ← 2else: z ← 1Automata & Timers
Section titled “Automata & Timers”Timer with varying delay. The delay is a piecewise linear function of the monitored variable. Separate blocks exist for and characteristic points.
If is smaller than a threshold , the output is zero. Otherwise, changes from zero to one at time , where is the time at which first exceeded and is a piecewise linear function of defined by the pairs.
Syntax
Section titled “Syntax”& timer1name of variable x_iname of variable x_jdata/parameter/expression for v_1data/parameter/expression for T_1For timer2–timer5, append additional pairs up to the required .
Internal States
Section titled “Internal States”(elapsed time counter)
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = -1: if x_i >= v_1: z ← 0else (z = 0 or 1): if x_i < v_1: z ← -1
if z = 0: if x_1 >= τ(x_i): z ← 1Initialization
Section titled “Initialization”
- The values must be non-decreasing: .
- Typical use: approximating an inverse-time characteristic with .
- If at , will trip to one after unless drops below first.
timersc
Section titled “timersc”Timer with varying delay. The delay is a staircase function of the monitored variable. Separate blocks exist for and characteristic points.
Identical behaviour to timer except that the delay function is a staircase (step) function instead of a piecewise linear interpolation.
Syntax
Section titled “Syntax”& timersc1name of variable x_iname of variable x_jdata/parameter/expression for v_1data/parameter/expression for T_1For timersc2–timersc6, append additional pairs up to the required .
Internal States
Section titled “Internal States”(elapsed time counter)
Discrete Variables
Section titled “Discrete Variables”
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = -1: if x_i >= v_1: z ← 0else (z = 0 or 1): if x_i < v_1: z ← -1
if z = 0: if x_1 >= τ(x_i): z ← 1Initialization
Section titled “Initialization”
- The values must be non-decreasing: .
- The staircase characteristic is typically used to approximate an inverse-time protection curve.
- If at , trips to one after unless drops below first.
Two-state automaton with transitions based on signs of two inputs.
The output state can take two values: or . When , a positive triggers the transition to ; when , a positive triggers the return to .
Syntax
Section titled “Syntax”& 2saname of variable x_1 (trigger for transition to v_2)name of variable x_2 (trigger for transition to v_1)name of variable x_k (output)data/parameter/expression for v_1data/parameter/expression for v_2Internal States
Section titled “Internal States”None.
Discrete Variables
Section titled “Discrete Variables”represents the automaton state.
Equations
Section titled “Equations”Discrete Transitions
Section titled “Discrete Transitions”if z = 1 and x_1 > 0: z ← 2else if z = 2 and x_2 > 0: z ← 1Initialization
Section titled “Initialization”
Functions Reference
Section titled “Functions Reference”The following functions can be used in any mathematical expression within CODEGEN model definitions.
Signature: double precision function equal(var1, var2)
Arguments: double precision :: var1, var2
Description: Returns 1.0 if var1 differs from var2 by less than , and 0.0 otherwise.
Notes: General-purpose; available in all model types.
equalstr
Section titled “equalstr”Signature: double precision function equalstr(var1, var2)
Arguments: character :: var1, var2
Description: Returns 1.0 if the non-blank portions of var1 and var2 are identical strings, and 0.0 otherwise.
Notes: General-purpose; available in all model types.
ppower
Section titled “ppower”Signature: double precision ppower([vx], [vy], [ix], [iy])
Description: Returns the active power injected into the network.
Model type: inj only.
Computation:
qpower
Section titled “qpower”Signature: double precision qpower([vx], [vy], [ix], [iy])
Description: Returns the reactive power injected into the network.
Model type: inj only.
Computation:
vrectif
Section titled “vrectif”Signature: double precision function vrectif([if], [vin], {kc})
Arguments: double precision :: kc
Description: Models the voltage drop in a rectifier. Returns the output voltage as a function of output current and input voltage . Used in IEEE standard excitation models with .
Model type: exc only.
Computation:
in = kc * if / max(vin, 1e-3)
if in <= 0: vrectif = vinelse if in <= 0.433: vrectif = vin - 0.577 * kc * ifelse if in <= 0.75: vrectif = sqrt(0.75 * vin^2 - (kc * if)^2)else if in <= 1.00: vrectif = 1.732 * (vin - kc * if)else: vrectif = 0vinrectif
Section titled “vinrectif”Signature: double precision function vinrectif([if], [vrectif], {kc})
Arguments: double precision :: kc
Description: Inverse of vrectif. Given the field current and rectifier output voltage , returns the rectifier input voltage . Intended for use at initialization. The computation is iterative because the operating segment of the piecewise characteristic is not known a priori. Does not work when (indeterminate input).
Model type: exc only.
Computation:
nbtries = 1vinest = vrectif + 0.577 * kc * if
loop: in = kc * if / max(vinest, 1e-3)
if in <= 0: vinrectif = vrectif else if in <= 0.433: vinrectif = vrectif + 0.577 * kc * if else if in <= 0.75: vinrectif = sqrt(vrectif^2 + (kc * if)^2) / 0.75 else: vinrectif = (vrectif / 1.732) + kc * if
if vinrectif == vinest or nbtries > 5: exit loop nbtries = nbtries + 1 vinest = vinrectifSignature: double precision function vcomp([v], [p], [q], {Kv}, {Rc}, {Xc})
Arguments: double precision :: Kv, Rc, Xc
Description: Returns the magnitude of a combination of terminal voltage and current of a synchronous machine. Appears in IEEE standard excitation models.
Model type: exc only.
Computation:
Signature: double precision function satur([ve], {ve1}, {se1}, {ve2}, {se2})
Arguments: double precision :: ve1, se1, ve2, se2
Description: Returns the increment of field current needed to obtain a given output voltage, accounting for magnetic saturation. Appears in IEEE excitation system models.
Model type: exc only.
Computation:
where:
Exception. Returns if any of the following conditions holds: