CODEGEN Model Examples
This page provides complete, annotated CODEGEN model files for each of the four model categories. These are real models from the RAMSES model library and can serve as templates for developing your own models.
For the block reference, see the CODEGEN Blocks Library. For the model framework (states, equations, initialization), see User-Defined Models.
Exciter Model — exc_ST1A
Section titled “Exciter Model — exc_ST1A”The IEEE ST1A is a bus-fed static excitation system. This model demonstrates the typical structure of an exc-type CODEGEN file: voltage compensation, AVR with lead-lag stages, field current limiting, UEL/OEL gating, and variable-limit output clamp.
Source: IEEE_models/exc_ST1A.txt (from the RAMSES model library)
excST1A
%data
KvRcXcTRUELVIMINVIMAXVUEL ERROR - Vuel is the UEL signal not a parameter!TCTBTC1TB1KATAVAMINVAMAXVRMINVRMAXKCKFTFKLRILR
%parameters
VREF = vcomp([v],[p],[q],{Kv},{Rc},{Xc})+([vf]/{KA})
%states
Vc1 = vcomp([v],[p],[q],{Kv},{Rc},{Xc})Vc = vcomp([v],[p],[q],{Kv},{Rc},{Xc})deltaV = ([vf]/{KA})V1 = [vf]/{KA}V2 = [vf]/{KA}V3 = [vf]/{KA}V4 = [vf]/{KA}VA = [vf]VLR = {KLR}*([if]-{ILR})VLRlim = 0.V5 = [vf]V6 = [vf]V7 = [vf]uplim = [v]*{VRMAX}-{KC}*[if]lolim = [v]*{VRMIN}VF = 0.
VOEL = 99999.
%observables
VAVc1VcV1V2V3V4vfpqvVLRlimdeltaV
VREFVOEL
%models& algeq fictitious over excitation limit[VOEL]-99999.& algeq line drop compensation[Vc1]-vcomp([v],[p],[q],{Kv},{Rc},{Xc})& tf1p voltage measurement time constantVc1Vc1.d0{TR}& algeq summation point of AVR[deltaV]-{VREF}+[Vc]-equal({UEL},1.d0)*{VUEL}+[VF]& lim limiter on deltaVdeltaVV1{VIMIN}{VIMAX}& max1v1c HV gate for VUEL if UEL=2V1V2{VUEL}*equal({UEL},2.d0) - 9999.*(1-equal({UEL},2.d0))& tf1p1z first lead-lagV2V31.{TC}{TB}& tf1p1z second lead-lagV3V41.{TC1}{TB1}& tf1plim amplifierV4VA{KA}{TA}{VAMIN}{VAMAX}& algeq immediate field current limiting signal[VLR]-{KLR}*([if]-{ILR})& lim ... lower limited to zeroVLRVLRlim0.99999.& algeq ... and subtracted from main signal[V5]-[VA]+[VLRlim]& max1v1c HV gate for VUEL if UEL=3V5V6{VUEL}*equal({UEL},3.d0) - 9999.*(1-equal({UEL},3.d0))& min2v LV gate for VOELV6VOELV7& algeq variable lower limit of final limiter[v]*{VRMIN}-[lolim]& algeq variable upper limit of final limiter[v]*{VRMAX}-{KC}*[if]-[uplim]& limvb final limiterV7lolimuplimvf& tfder1p derivative feedbackV7VF{KF}/{TF}{TF}File Header
Section titled “File Header”exc ← model type (exciter)ST1A ← model name (used in the dynamic data file as EXC ST1A)%data Section
Section titled “%data Section”Lists the parameters that the user provides in the dynamic data file. Each name becomes a {name} reference in expressions. The order here defines the order of values in the data file.
%parameters Section
Section titled “%parameters Section”Operating-point parameters computed at initialization. The number of parameters must equal the number of output states (here 1: vf). VREF is the voltage setpoint, computed from the initial operating point so that the steady-state field voltage is matched.
%states Section
Section titled “%states Section”All internal and output states with their initial values. The initialization expressions use system variables ([v], [p], [q], [if], [vf]) and data values ({KA}, etc.). The last state vf is the output state (field voltage).
%observables Section
Section titled “%observables Section”States that can be recorded during simulation.
%models Section — Signal Flow
Section titled “%models Section — Signal Flow”algeq— OEL placeholder (set to large value, overridden by OEL model if present)algeq— line drop compensation:tf1p— voltage measurement filter with time constantalgeq— AVR summation:lim— error limitermax1v1c— HV gate for UEL (active when UEL=2)tf1p1z— first lead-lag:tf1p1z— second lead-lag:tf1plim— amplifier with gain , time constant , limitsalgeq+lim— instantaneous field current limiter , lower-limited to 0algeq— subtract limiter signal from main pathmax1v1c— HV gate for UEL (active when UEL=3)min2v— LV gate for OELalgeq× 2 — compute variable limits: andlimvb— bus-fed variable-limit output clamp →vftfder1p— derivative feedback:
Governor Model — tor_hygov
Section titled “Governor Model — tor_hygov”A hydraulic turbine-governor model with speed droop, PI controller, rate limiter, gate servo, and non-linear turbine (head-flow-power). This demonstrates a tor-type model with both differential and algebraic equations.
Source: IEEE_models/tor_hygov.txt (from the RAMSES model library)
torhygov
%data
RrTrTfTgVELMGmaxGminTwAtDturbQnl
%parameters
Po = ([p]/{At})+{Qnl} ! power setpoint
%states
Q = {Po} ! water flowdH = 0. ! deviation of head from 1.g = {Po} ! gate openingc = {Po} ! desired gate openingx5 = {Po} ! output of PI controllerx4 = {Po} ! output of integrator of PI controllerx3 = 0. ! output of rate limiter = input of integrator of PI controllerx2 = 0. ! input of rate limiter of PI controllere = 0. ! output of Tf time constant blockx1 = 0. ! input of Tf time constant blockPm = [p] ! mechanical power
%observables
PmQdHg
%models
& algeq ! input of Tf time constant block1.-[omega]-{R}*([c]-{Po}) - [x1]& tf1p ! Tf time constantx1e1.{Tf}& algeq ! input of rate limiter[e]/({r}*{Tr}) - [x2]& lim ! rate limiterx2x3-{VELM} {VELM}& inlim ! integrator of PI controllerx3x41.{Gmin}{Gmax}& algeq ! output of PI controller[x4]+([e]/{r}) - [x5]& lim ! gate limiterx5c{Gmin}{Gmax}& tf1p ! speed governor : time constantcg1.{Tg}
& algeq ! hydro turbine: head as function of water flow and gate opening1. -([Q]/[g])**2 -[dH]& int ! hydro turbine: water column inertiadHQ{Tw}& algeq ! hydro turbine: mechanical torque{At}*(1.-[dH])*([Q]-{Qnl})-{Dturb}*(1.-[omega])*[g] - [tm]*[omega]& algeq[tm]*[omega] - [Pm]File Header
Section titled “File Header”tor ← model type (torque/governor)hygov ← model name (used as TOR hygov in the data file)Input/Output States
Section titled “Input/Output States”- Input:
[omega](rotor speed),[p](electrical power) - Output:
[tm](mechanical torque)
Signal Flow
Section titled “Signal Flow”Governor section:
- Speed error with droop:
- Filter:
tf1pwith time constant - PI controller path: rate limiter (
lim) → integrator (inlim) with gate limits → proportional bypass - Gate servo:
tf1pwith time constant
Turbine section:
5. Head-flow relation: (non-linear algebraic)
6. Water column inertia: (int block)
7. Mechanical torque:
8. Mechanical power:
Key Parameters
Section titled “Key Parameters”| Parameter | Description |
|---|---|
R | Permanent droop |
r | Temporary droop |
Tr | Reset time |
Tf | Filter time constant |
Tg | Gate servo time constant |
VELM | Gate velocity limit |
Gmax/Gmin | Gate position limits |
Tw | Water starting time |
At | Turbine gain |
Dturb | Turbine damping |
Qnl | No-load flow |
Injector Model — inj_vfd_load
Section titled “Injector Model — inj_vfd_load”A voltage- and frequency-dependent load model with exponential characteristics and low-voltage protection (constant admittance below a threshold). This is the most widely used load model in RAMSES. It demonstrates an inj-type model with current injection equations.
Source: custom_models/inj_vfd_load.txt (from the RAMSES model library)
injvfd_load
%data
Dp ! sensitivity of active power to frequencya1 ! proportion of type 1 in active poweralpha1 ! exponent of type 1 in active powera2 ! proportion of type 2 in active poweralpha2 ! exponent of type 2 in active poweralpha3 ! exponent of type 3 in active power (proportion = 1-a1-a2)Dq ! sensitivity of reactive power to frequencyb1 ! proportion of type 1 in reactive powerbeta1 ! exponent of type 1 in reactive powerb2 ! proportion of type 2 in reactive powerbeta2 ! exponent of type 2 in reactive powerbeta3 ! exponent of type 3 in reactive power (proportion = 1-b1-b2)Vinit ! initial voltage at equivalent distribution busVlow ! voltage below which the load behaves as constant admittancefoption ! set to 1 to use bus frequency; otherwise COI speedTmes ! measurement time constant for bus frequency estimation
%parameters
Vo = equal({Vinit},0.d0)*dsqrt([vx]**2+[vy]**2) + {Vinit}r = dsqrt([vx]**2+[vy]**2)/{Vo}denomP = {a1}*{Vo}**{alpha1}+{a2}*{Vo}**{alpha2}+(1.d0-{a1}-{a2})*{Vo}**{alpha3}denomQ = {b1}*{Vo}**{beta1}+{b2}*{Vo}**{beta2}+(1.d0-{b1}-{b2})*{Vo}**{beta3}Po = -[vx]*[ix]-[vy]*[iy]Qo = [vx]*[iy]-[vy]*[ix]Vmin = max(1.d-01,{Vlow})Geq = {Po}*({a1}*{Vmin}**{alpha1}+{a2}*{Vmin}**{alpha2} +(1.d0-{a1}-{a2})*{Vmin}**{alpha3})/({denomP}*{Vmin}**2)Beq =-{Qo}*({b1}*{Vmin}**{beta1}+{b2}*{Vmin}**{beta2} +(1.d0-{b1}-{b2})*{Vmin}**{beta3})/({denomQ}*{Vmin}**2)
%states
Vreal = {Vo} ! voltage at equivalent distribution busV = {Vo} ! Vreal slightly delayedP = {Po} ! active powerQ = {Qo} ! reactive powerfbus = 1.d0 ! bus frequencydf = 0.d0 ! frequency deviationu = 1 ! model switch: =1 if V > Vmin, =0 otherwise
%observables
PQdfu
%models
& f_injfbus{Tmes}& algeqequal(1.d0,{foption})*([fbus]-1.d0) + (1.d0-equal(1.d0,{foption}))*([omega]-1.d0) - [df]& algeq ! voltage at distribution bus = network voltage / trfo ratiodsqrt([vx]**2+[vy]**2)/{r}-[Vreal]& tf1p ! small measurement time constantVrealV1.0.003& pwlin4 ! determine if u=1 or u=0Vu-1.d00.d0{Vmin}0.d0{Vmin}1.d02.d01.d0& algeq ! active power function of V, u and f[u]*({Po}*(1.d0+{Dp}*[df]) *({a1}*[V]**{alpha1}+{a2}*[V]**{alpha2} +(1.d0-{a1}-{a2})*[V]**{alpha3})/{denomP}) + (1.d0-[u])*{Geq}*[V]**2 -[P]& algeq ! reactive power function of V, u and f[u]*({Qo}*(1.d0+{Dq}*[df]) *({b1}*[V]**{beta1}+{b2}*[V]**{beta2} +(1.d0-{b1}-{b2})*[V]**{beta3})/{denomQ}) - (1.d0-[u])*{Beq}*[V]**2 -[Q]& algeq ! ix component of injected current(-[P]*[vx] - [Q]*[vy])/max([V]**2,1.d-02) -[ix]& algeq ! iy component of injected current( [Q]*[vx] - [P]*[vy])/max([V]**2,1.d-02) -[iy]File Header
Section titled “File Header”inj ← model type (injector)vfd_load ← model name (used as INJEC vfd_load in the data file)Input/Output States
Section titled “Input/Output States”- Input:
[vx],[vy](bus voltage components),[omega](COI frequency) - Output:
[ix],[iy](injected current components)
Model Logic
Section titled “Model Logic”Frequency estimation:
f_inj— estimates bus frequency from voltage phasor derivativesalgeq— selects bus frequency or COI frequency based onfoption
Voltage processing:
3. algeq — compute distribution bus voltage:
4. tf1p — small delay (3 ms) for numerical stability
5. pwlin4 — switch: if , otherwise
Power computation: 6. Active power: 7. Reactive power: analogous with / coefficients
Current injection: 8–9. Convert P, Q to rectangular currents: ,
Key Design Patterns
Section titled “Key Design Patterns”- Low-voltage protection: below , the load switches to constant admittance (, ) to prevent numerical issues
- Distribution transformer: the ratio models the voltage step-down to the load bus
- Frequency dependence: , scale power with frequency deviation
Two-Port Model — twop_HVDC_LCC
Section titled “Two-Port Model — twop_HVDC_LCC”A Line-Commutated Converter (LCC) HVDC link model connecting two AC buses through a DC line. This demonstrates a twop-type model with complex AC/DC interaction, multiple control modes (current control, voltage control, gamma control), and non-linear commutation equations.
Source: custom_models/twop_HVDC_LCC.txt (from the RAMSES model library)
twopHVDC_LCC
%data
p ! number of poles (1 or 2)rec_ctl ! rectifier control: 1=current, 2=power, -1=alpha min, 3=blockedinv_ctl ! inverter control: 1=voltage, 2=gamma, -2=gamma min, -1=reduced current, 3=blockedBr ! number of bridges in series in rectifierBi ! number of bridges in series in inverterEr ! phase-to-phase open-circuit voltage (kV) on DC side of rectifier transformerEi ! phase-to-phase open-circuit voltage (kV) on DC side of inverter transformerRcr ! rectifier commutating resistance per bridge (ohm)Xcr ! rectifier commutating reactance per bridge (ohm)Rci ! inverter commutating resistance per bridge (ohm)Xci ! inverter commutating reactance per bridge (ohm)RL ! DC line series resistance (ohm)Rcomp ! compounding resistance for inverter voltage control (ohm)Tp ! time constant of power control (s)Idset ! initial DC current setpoint (kA)nr ! initial rectifier transformer ratio (pu/pu)ni ! initial inverter transformer ratio (pu/pu)
%parameters
Pset = -([vx1]*[ix1]+[vy1]*[iy1])*sbase1Vdr0 = {Pset}/({p}*{Idset})alpha0 = dacos( ({nr}/(1.3504745*{Er}*dsqrt([vx1]**2+[vy1]**2))) * (({Vdr0}/{Br})+(0.9549297*{Xcr}+2*{Rcr})*{Idset}) )mur0 = dacos( dcos({alpha0}) - 1.4142136*{Xcr}*{nr}*{Idset} /(dsqrt([vx1]**2+[vy1]**2)*{Er})) - {alpha0}Qhvdc1 = {Pset}*(2.d0*{mur0}+dsin(2*{alpha0})-dsin(2d0*({mur0}+{alpha0}))) /(dcos(2.d0*{alpha0})-dcos(2d0*({mur0}+{alpha0})))B1 = ({Qhvdc1}/sbase1-[vx1]*[iy1]+[vy1]*[ix1])/([vx1]**2+[vy1]**2)Vdi0 = {Vdr0}-{RL}*{Idset}gamma0 = dacos( ({ni}/(1.3504745*{Ei}*dsqrt([vx2]**2+[vy2]**2))) * (({Vdi0}/{Bi})+(0.9549297*{Xci}+2*{Rci})*{Idset}) )mui0 = dacos( dcos({gamma0}) - 1.4142136*{Xci}*{ni}*{Idset} /(dsqrt([vx2]**2+[vy2]**2)*{Ei})) - {gamma0}P2in = {p}*{Vdi0}*{Idset}G2 = ({P2in}/sbase2-[vx2]*[ix2]-[vy2]*[iy2]) /dsqrt([vx2]**2+[vy2]**2)Qhvdc2 = {P2in}*(2.d0*{mui0}+dsin(2*{gamma0})-dsin(2d0*({mui0}+{gamma0}))) /(dcos(2.d0*{gamma0})-dcos(2d0*({mui0}+{gamma0})))B2 = ({Qhvdc2}/sbase2-[vx2]*[iy2]+[vy2]*[ix2])/([vx2]**2+[vy2]**2)Vset = {Vdi0}+{Rcomp}*{Idset}alphamin = 0.d0Idred = 0.d0gammamin = 0.d0Td = 0.02
%states
Vdr = {Vdr0} ! rectifier DC voltage (kV)alpha = {alpha0} ! firing angle (rad)mur = {mur0} ! overlap angle (rad)Id = {Idset} ! DC current (kA)Vdi = {Vdi0} ! inverter DC voltage (kV)gamma = {gamma0} ! extinction angle (rad)mui = {mui0} ! inverter overlap angle (rad)Ides = {Idset} ! desired DC current (kA)Iord = {Idset} ! DC current order (kA)P1 = (-[vx1]*[ix1]-[vy1]*[iy1])*sbase1Q1 = ([vx1]*[iy1]-[vy1]*[ix1])*sbase1P2 = ([vx2]*[ix2]+[vy2]*[iy2])*sbase2Q2 = ([vx2]*[iy2]-[vy2]*[ix2])*sbase2V1 = dsqrt([vx1]**2+[vy1]**2)V1d = dsqrt([vx1]**2+[vy1]**2)V2 = dsqrt([vx2]**2+[vy2]**2)V2d = dsqrt([vx2]**2+[vy2]**2)alphad = {alpha0}*57.2957795gammad = {gamma0}*57.2957795
%observables
VdralphadmurVdigammadmuiIdesIordIdP1Q1P2Q2Pset
%models
& algeq ! Vdr: rectifier DC voltage equation[Vdr] - {Br}*( 1.3504745*[V1d]*{Er}*dcos([alpha])/{nr} - (0.9549297*{Xcr}+2*{Rcr})*[Id] )& algeq ! overlap angle at rectifierdcos([alpha]+[mur]) - dcos([alpha]) + 1.4142136*{Xcr}*{nr}*[Id]/(max([V1d],1.d-03)*{Er})& algeq ! ix1: rectifier AC current (x-component){B1}*[vy1]-([vx1]+[vy1]*(2*[mur]+dsin(2*[alpha]) -dsin(2*([mur]+[alpha])))/max((dcos(2*[alpha]) -dcos(2*([mur]+[alpha]))),1d-03)) *{p}*[Vdr]*[Id]/(sbase1*max([V1d]**2,1d-06)) - [ix1]& algeq ! iy1: rectifier AC current (y-component)-{B1}*[vx1]-([vy1]-[vx1]*(2*[mur]+dsin(2*[alpha]) -dsin(2*([mur]+[alpha])))/max((dcos(2*[alpha]) -dcos(2*([mur]+[alpha]))),1d-03)) *{p}*[Vdr]*[Id]/(sbase1*max([V1d]**2,1d-06)) - [iy1]& algeq ! Vdi: inverter DC voltage equation[Vdi] - {Bi}*( 1.3504745*[V2d]*{Ei}*dcos([gamma])/{ni} - (0.9549297*{Xci}+2*{Rci})*[Id] )& algeq ! overlap angle at inverterdcos([gamma]+[mui]) - dcos([gamma]) + 1.4142136*{Xci}*{ni}*[Id]/(max([V2d],1.d-03)*{Er})& algeq ! ix2: inverter AC current (x-component)-{G2}*[vx2]+{B2}*[vy2]+([vx2]-[vy2]*(2*[mui]+dsin(2*[gamma]) -dsin(2*([mui]+[gamma])))/max((dcos(2*[gamma]) -dcos(2*([mui]+[gamma]))),1d-03)) *{p}*[Vdi]*[Id]/(sbase2*max([V2d]**2,1d-06)) - [ix2]& algeq ! iy2: inverter AC current (y-component)-{B2}*[vx2]-{G2}*[vy2]+([vy2]+[vx2]*(2*[mui]+dsin(2*[gamma]) -dsin(2*([mui]+[gamma])))/max((dcos(2*[gamma]) -dcos(2*([mui]+[gamma]))),1d-03)) *{p}*[Vdi]*[Id]/(sbase2*max([V2d]**2,1d-06)) - [iy2]& algeq ! DC link: Kirchhoff voltage law[Vdr]-[Vdi]-{RL}*[Id]
& algeq ! desired DC current (depends on control mode)(equal({rec_ctl},1.d0)*{Idset}) +(equal({rec_ctl},2.d0)*({Pset}/max([Vdr],1d-02))) -(equal({rec_ctl},3.d0)*50.d0) -(equal({rec_ctl},-1.d0)*50.d0) -[Ides]& tf1plim ! current order (filtered and limited)IdesIord1.d0Tp0.d09999.d0
& algeq ! rectifier control equationequal({rec_ctl},1.d0)*([Iord]-[Id]) +equal({rec_ctl},2.d0)*([Iord]-[Id]) +equal({rec_ctl},-1d0)*({alphamin}-[alpha]) +equal({rec_ctl},3.d0)*[Id]& algeq ! inverter control equationequal({inv_ctl},1.d0)*([Vdi]+{Rcomp}*[Id]-{Vset}) +equal({inv_ctl},2.d0)*({gamma0}-[gamma]) +equal({inv_ctl},-2d0)*({gammamin}-[gamma]) +equal({inv_ctl},-1d0)*({Idred}-[Id]) +equal({inv_ctl},3.d0)*(1.570796327-[gamma])
& algeq ! AC voltage at bus 1dsqrt([vx1]**2+[vy1]**2) - [V1]& tf1p ! delayed AC voltage at bus 1V1V1d1.Td& algeq ! AC voltage at bus 2dsqrt([vx2]**2+[vy2]**2) - [V2]& tf1p ! delayed AC voltage at bus 2V2V2d1.Td& algeq ! observable: P1(-[vx1]*[ix1]-[vy1]*[iy1])*sbase1-[P1]& algeq ! observable: Q1( [vx1]*[iy1]-[vy1]*[ix1])*sbase1-[Q1]& algeq ! observable: P2([vx2]*[ix2]+[vy2]*[iy2])*sbase2-[P2]& algeq ! observable: Q2([vx2]*[iy2]-[vy2]*[ix2])*sbase2-[Q2]& algeq ! alpha in degrees[alphad]-[alpha]*57.2957795& algeq ! gamma in degrees[gammad]-[gamma]*57.2957795File Header
Section titled “File Header”twop ← model type (two-port)HVDC_LCC ← model name (used as TWOPOR ... HVDC_LCC in data file)Input/Output States
Section titled “Input/Output States”- Input:
[vx1],[vy1],[vx2],[vy2](voltage at both buses) - Output:
[ix1],[iy1],[ix2],[iy2](current injected at both buses) - System variables:
sbase1,sbase2(base power at each bus)
Model Structure
Section titled “Model Structure”DC equations (algebraic):
- Rectifier DC voltage:
- Inverter DC voltage: analogous with , ,
- Overlap angles: from commutation equations
- DC link:
AC current injection:
- Includes fundamental-frequency reactive power from commutation overlap
- Shunt compensation (, , ) to match initial operating point
Control modes (selected by rec_ctl and inv_ctl using equal() function):
- Rectifier: constant current, constant power, minimum alpha, blocked
- Inverter: constant voltage, constant gamma, minimum gamma, reduced current, blocked
Key Design Patterns
Section titled “Key Design Patterns”equal()function: used to switch between control modes without discrete variables — acts as a continuous selectorsbase1/sbase2: system variables for per-unit conversion at each busTddelay: small artificial time constant on terminal voltages for numerical robustness- Initialization: complex parameter expressions compute initial angles, overlap, and compensation from the power flow solution