User-Defined Models
CODEGEN allows you to define custom models that are compiled into Fortran 2003 code and linked with RAMSES. This page covers the model framework, state variables, equations, discrete transitions, and the complete model file syntax specification.
Model Categories
Section titled “Model Categories”| Category | Acronym | Description |
|---|---|---|
| Excitation controller | EXC | Excitation system and AVR (including PSS) |
| Torque controller | TOR | Turbine and speed governor |
| Injector | INJ | Component connected to a single AC bus |
| Two-port | TWOP | Component connecting two buses |
States
Section titled “States”Every model has three categories of states:
- Input states () — provided by the system
- Internal states () — computed by the model
- Output states () — returned to the system
Input and Output States by Category
Section titled “Input and Output States by Category”| Model Type | Input States | Output States |
|---|---|---|
| exc | [v], [p], [q], [omega], [if] | [vf] |
| tor | [p], [omega] | [tm] |
| inj | [vx], [vy], [omega] | [ix], [iy] |
| twop | [vx1], [vy1], [vx2], [vy2], [omega1], [omega2] | [ix1], [iy1], [ix2], [iy2] |
Notation:
[v]: terminal voltage (pu),[p]/[q]: active/reactive power (pu on )[omega]: rotor speed (pu),[if]/[vf]: field current/voltage (pu on exciter base)[tm]: mechanical torque (pu on turbine rated torque)[vx],[vy]: rectangular voltage components,[ix],[iy]: rectangular current components[omega](forinj): center-of-inertia angular frequency (pu)[omega1],[omega2](fortwop): COI angular frequency for each subsystem (pu)
Note that not all input states need to be used by a given model.
Equations
Section titled “Equations”The model state vector is:
The differential-algebraic equations take the form:
where:
- is a (mostly zero) matrix. Row is all zeros for algebraic equations, or contains a single for differential equations.
- contains operating-point dependent parameters
- contains discrete variables
The model must have a number of equations at least equal to the number of output states.
Initialization
Section titled “Initialization”At initialization (, steady state assumed):
- Input states: given from power flow solution
- Output states: also given (from machine/network equations)
- Internal states: initialized explicitly or automatically
- Parameters : determined from the initial state (the number of parameters equals the number of output states)
During simulation, the input, internal, and output states are computed together with the other system states. The operating-point dependent parameters remain constant unless modified by a user action.
Example: Simple AVR
Section titled “Example: Simple AVR”A simple excitation system with gain and time constant :
- Input: (terminal voltage)
- Internal: (algebraic state)
- Output: (differential state)
- Parameter: (voltage setpoint)
At initialization (): , then .
Discrete Transitions
Section titled “Discrete Transitions”Discrete variables control which set of equations is active. Transitions occur when inequality constraints are violated (e.g., a state exceeds its limit).
Example: AVR with Non-Windup Limits
Section titled “Example: AVR with Non-Windup Limits”When the integrator output is limited between and :
- Normal ():
- Upper limit ():
- Lower limit ():
Transition logic (pseudo-code):
if z == 0: if v_f > v_f_max: z = 1 # hit upper limit elif v_f < v_f_min: z = -1 # hit lower limitelif z == 1: if (-v_f + G*dV)/T < 0: z = 0 # release from upperelif z == -1: if (-v_f + G*dV)/T > 0: z = 0 # release from lowerModel File Structure
Section titled “Model File Structure”A CODEGEN model file is a text description containing six mandatory sections, which must appear in the following exact order:
- Header: model type and name
- %data: input data values read from the simulation data file
- %parameters: operating-point dependent parameters computed at initialization
- %states: internal and output state declarations with initial conditions
- %observables: quantities available for plotting
- %models: modelling blocks and their interconnections
All keywords must be written exactly as specified (case-sensitive, no extra spaces).
Model File Syntax Specification
Section titled “Model File Syntax Specification”Header
Section titled “Header”The header consists of exactly two lines:
<type of model><name of model>- Line 1: model type — must be
exc,tor,inj, ortwop - Line 2: model name — a string of at most 16 characters
The output file will be named {type}_{name}.f90. For example, type exc and name simple_avr produces exc_simple_avr.f90. This combined name is also used in the simulation data files to reference the model.
Data Section
Section titled “Data Section”Starts with the keyword %data.
Each data item must be given a unique name. That name, enclosed with curly braces {name}, is used everywhere else in the model description.
Important: braces must not be used when a data name is first defined (i.e., before the = sign). If braces are used at definition, they become part of the name and will cause an error.
Each name may be followed by a comment on the same line, starting with !.
At initialization, RAMSES maps the data values from the simulation data file to the names declared in this section, in the order they are listed.
Reserved Base Power Names
Section titled “Reserved Base Power Names”The following base power names are available by default in inj and twop models and must not be used for other data:
| Model Type | Name | Meaning |
|---|---|---|
| exc | — | — |
| tor | — | — |
| inj | {sbase} | System base power (MVA) |
| twop | {sbase1} | Base power of subnetwork including bus 1 (MVA) |
| twop | {sbase2} | Base power of subnetwork including bus 2 (MVA) |
Parameter Section
Section titled “Parameter Section”Starts with the keyword %parameters.
Each parameter is defined with:
<name> = <mathematical expression>Parameter names are enclosed with {name} when referenced in expressions, but not at the point of definition (before =).
Rules:
- A data and a parameter cannot share the same name
- Expressions may involve data names (in
{}) or previously defined parameter names (in{}) - Mathematical expressions use FORTRAN syntax:
- Exponent:
**(not^as in MATLAB) - Boolean operators:
.lt.(less than),.le.(less or equal),.gt.(greater than),.ge.(greater or equal),.eq.(equal),.ne.(not equal) - Standard math functions (
cos,sqrt,abs, etc.) follow Intel Fortran syntax
- Exponent:
State Section
Section titled “State Section”Starts with the keyword %states.
Only internal states are declared here. Input and output states (whose initial values are known from the power flow) are not declared.
Each internal state is defined with:
<name> = <initial value expression>State names are enclosed with [name] when referenced in expressions, but not at the point of definition (before =).
Rules:
- A state cannot share a name with a data or parameter
- Initial value expressions may involve data (
{}), parameters ({}), or previously defined states ([]) - Input and output states are also referenced with brackets (e.g.,
[vf],[vx]) in expressions
Reserved State Names
Section titled “Reserved State Names”The following names are reserved for input and output states and must not be used for internal states:
| Model Type | Name | Meaning |
|---|---|---|
| exc | [v] | Terminal voltage (pu) |
| exc | [p] | Active power produced (pu on Snom) |
| exc | [q] | Reactive power produced (pu on Snom) |
| exc | [omega] | Rotor speed (pu) |
| exc | [if] | Field current (pu on exciter base) |
| exc | [vf] | Field voltage (pu on exciter base) |
| tor | [p] | Mechanical power (pu on turbine rated) |
| tor | [omega] | Rotor speed (pu) |
| tor | [tm] | Mechanical torque (pu on turbine rated) |
| inj | [vx] | x-component of bus voltage |
| inj | [vy] | y-component of bus voltage |
| inj | [omega] | COI angular frequency (pu) |
| inj | [ix] | x-component of injected current |
| inj | [iy] | y-component of injected current |
| twop | [vx1], [vy1] | Voltage at bus 1 |
| twop | [vx2], [vy2] | Voltage at bus 2 |
| twop | [omega1] | COI frequency of subsystem with bus 1 (pu) |
| twop | [omega2] | COI frequency of subsystem with bus 2 (pu) |
| twop | [ix1], [iy1] | Current injected at bus 1 |
| twop | [ix2], [iy2] | Current injected at bus 2 |
Observable Section
Section titled “Observable Section”Starts with the keyword %observables.
Observables are quantities that can be plotted as functions of time. They may be input, output, or internal states, as well as data or parameters (useful for plotting a controller setpoint, for instance).
Observable names must be written without enclosing braces or brackets — exactly as defined in their respective sections.
Model Section
Section titled “Model Section”Starts with the keyword %models.
Modelling blocks and their arguments are listed sequentially. Order does not matter. Each block is identified by:
& <block name>The & symbol must be followed by a space, then the block name. Each block declaration may be followed by a comment starting with !.
After the block name, list the states (inputs/outputs of the block) followed by the data, parameters, or expressions required by the block, in the order specified by the block’s definition (see CODEGEN Blocks Library).
The model section ends at the end of the file.
Time Variable
Section titled “Time Variable”Time is neither a data, parameter, nor a state. When needed in expressions, use t (without brackets or braces).
Complete File Format
Section titled “Complete File Format”<type of model><name of model>%data<name of data 1> ! optional comment<name of data 2>...%parameters<name of parameter 1> = <math expression>...%states<name of state 1> = <initial value expression>...%observables<name of observable 1>...%models& <block name> ! optional comment<state name><state name><data, parameter, or expression>...& <block name 2>...Complete Example: simple_avr
Section titled “Complete Example: simple_avr”The following is the complete model file for the simple AVR with non-windup limits described above. The model type is exc, the name is simple_avr, and it has three observables: one parameter (Vo), one internal state (dv), and one output state (vf).
excsimple_avr%dataG ! gain of exciterT ! time constant of the excitervfmin ! lower field voltagevfmax ! upper field voltage%parametersVo = [v]+ [vf]/{G} ! voltage setpoint of AVR%statesdv = [vf]/{G} ! voltage error%observablesVodvvf%models& algeq ! calculation of voltage error{Vo}-[v]-[dv]& tf1plim ! exciter transfer functiondvvf{G}{T}{vfmin}{vfmax}When CODEGEN processes this file it produces exc_simple_avr.f90 and prints an execution trace showing the counts of equations and states as each block is assembled.
Error Detection
Section titled “Error Detection”CODEGEN performs sanity checks to detect common mistakes.
Errors CODEGEN catches
Section titled “Errors CODEGEN catches”- Unbalanced braces
{}or brackets[] - Missing section keywords (
%data,%parameters,%states,%observables,%models) - Multiply defined data, states, or parameters
- Reserved name used for an internal state
- Unknown state or block name (likely a typo)
- Output variable not appearing in any equation
- Number of states ≠ number of equations
Errors CODEGEN does NOT catch (cause compiler errors)
Section titled “Errors CODEGEN does NOT catch (cause compiler errors)”- Typos in math function names (e.g.,
cusinstead ofcos) - Exponent written as
^instead of** - Forgotten
{}around data or parameter names - Forgotten
[]around state names
Data Record Formats
Section titled “Data Record Formats”INJEC Record (User-Defined Injectors)
Section titled “INJEC Record (User-Defined Injectors)”User-defined injector models are instantiated in the simulation data file using an INJEC record:
INJEC MODEL_NAME INJ_NAME BUS FP FQ P Q DATA1 DATA2 ... ;| Field | Description |
|---|---|
MODEL_NAME | Name of the injector model, as defined in the model file header (max 20 characters) |
INJ_NAME | Name of this particular instance of the model (max 20 characters) |
BUS | Name of the bus to which the injector is connected (max 8 characters) |
FP | Power participation fraction for active power |
FQ | Power participation fraction for reactive power |
P | Initial active power. Constraint: FP × P = 0 |
Q | Initial reactive power. Constraint: FQ × Q = 0 |
DATA1 DATA2 ... | Successive data values, in the order defined in the %data section of the model |
TWOP Record (User-Defined Two-Ports)
Section titled “TWOP Record (User-Defined Two-Ports)”User-defined two-port models are instantiated using a TWOP record:
TWOP MODEL_NAME TWOP_NAME BUS1 BUS2 IND FP1 FQ1 P1 Q1 FP2 FQ2 P2 Q2 DATA1 DATA2 ... ;| Field | Description |
|---|---|
MODEL_NAME | Name of the two-port model, as defined in the model file header (max 20 characters) |
TWOP_NAME | Name of this particular instance of the model (max 20 characters) |
BUS1 | Name of the first connection bus (max 8 characters) |
BUS2 | Name of the second connection bus (max 8 characters) |
IND | Synchronization indicator: S = synchronous (same frequency, e.g. AC link); A = asynchronous (different frequencies, e.g. HVDC) |
FP1 | Active power participation fraction at BUS1 |
FQ1 | Reactive power participation fraction at BUS1 |
P1 | Initial active power at BUS1. Constraint: FP1 × P1 = 0 |
Q1 | Initial reactive power at BUS1. Constraint: FQ1 × Q1 = 0 |
FP2 | Active power participation fraction at BUS2 |
FQ2 | Reactive power participation fraction at BUS2 |
P2 | Initial active power at BUS2. Constraint: FP2 × P2 = 0 |
Q2 | Initial reactive power at BUS2. Constraint: FQ2 × Q2 = 0 |
DATA1 DATA2 ... | Successive data values, in the order defined in the %data section of the model |
The IND field is operationally important: setting IND = S causes RAMSES to treat the two subnetworks as operating at the same frequency (appropriate for AC interconnections), while IND = A allows them to operate at different frequencies (required for HVDC links, where [omega1] and [omega2] will differ).
Building User Models
Section titled “Building User Models”After writing a model file, CODEGEN translates it into Fortran 2003 code. The workflow is:
- Write the model description file
- Run CODEGEN (via GUI or command line) to generate Fortran source
- Compile with Intel Fortran compiler
- Link with RAMSES to create a custom executable or DLL
For compilation details, see URAMSES.
See the CODEGEN Blocks Library for the available modelling blocks.