> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/hypersim/model-and-simulation/import-models-from-external-software-or-code/create-user-coded-models-ucm/ucm-exercices-and-solutions.md).

# UCM Exercices & Solutions

This page presents a series of **exercises** designed to help users learn and practice the development of **User Code Models (UCMs)** in HYPERSIM.

Each exercise introduces a specific modeling scenario and is followed by a complete implementation, illustrating how the problem can be translated into a UCM definition. Together, these examples demonstrate key concepts such as parameter definition, node configuration, admittance matrix formulation, and simulation logic.

These exercises are intended to provide **hands-on learning**, allowing users to understand both the modeling approach and its practical implementation through concrete examples.

## Exercice 1: Inductor description

Build an UCM to represent a three-phase resistor connected in delta. Use control outputs to send out phase-to-phase voltages and resistors currents.

Build an UCM to represent a three-phase inductor connected in Y, the neutral is grounded. The parameters of the UCM is the vector Lval\[3] of 3 values of inductance. The inductors are connected to the external node T. Calculate the current in each phase and send them to control outputs Ia, Ib, Ic which are grouped together and sent to connector I.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-e9e0b4c0bcae149067e59d222f62843575274f22%2Fimage%20(1654).png?alt=media" alt=""><figcaption></figcaption></figure>

### Solution

```
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN GENERAL INFORMATION -- Enter or modify general information
UCM_NAME = Inductor
UCM_TYPE = NetworkElement
UCM_CATEGORY = User
UCM_VERSION = "1.0"
UCM_EXEC_TIME = 5.0e-6
UCM_DESCRIPTION = "Linear Inductor" # Description du modele
%% END GENERAL INFORMATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN DOCUMENTATION -- Enter model’s documentation after this line...
Documentation de Exemple.
---------------------
Three-phase linear inductor connected in Y grounded
%% END DOCUMENTATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN TUNABLE PARAMETERS -- Enter parameters table after this line...
Lval "L" H double 3 [0.1 0.1 0.1] 0.0 1.0e12 - "Inductor values"
%% END TUNABLE PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CONTROL IOS -- Enter control IOs table after this line...
Ia A double out auto I "Ia"
Ib A double out auto I "Ib"
Ic A double out auto I "Ic"
%% END CONTROL IOS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN NODES DEFINITIONS -- Enter nodes table after this line...
T 3 extern bottom no "Network Connection"
%% END NODES DEFINITIONS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL CURRENTS -- Enter historical currents table after this...
Ihist_a ground T_a
Ihist_b ground T_b
Ihist_c ground T_c
%% END HISTORICAL CURRENTS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CALCULATED PARAMETERS -- Enter parameters table after this line...
G - double 3 "Equiv.conductance of L"
%% END CALCULATED PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATION FUNCTION CODE -- Enter code after this line...
int i, j;
/* Calculte the equivalent conductances of L */
G[0] = ucmTimeStep / (2.0 * Lval[0]);
G[1] = ucmTimeStep / (2.0 * Lval[1]);
G[2] = ucmTimeStep / (2.0 * Lval[2]);
/* Initialization of Yini and Yfill */
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
ucmYini(i,j) = 0.0;
ucmYfill(i,j) = 0;
}
}
/* Initialize Yini to the equiv. conductance of L */
/* Define Yfill */
ucmYini(T_a, T_a) = G[0];
ucmYini(T_b, T_b) = G[1];
ucmYini(T_c, T_c) = G[2];
ucmYfill(T_a, T_a) = 1;
ucmYfill(T_b, T_b) = 1;
ucmYfill(T_c, T_c) = 1;
%% END PREPARATION FUNCTION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN AFTER VOLTAGE CALCULATION -- Enter code ->...
Ia = ucmVNode(T_a) * G[0] + Ihist_a;
Ihist_a = Ia + ucmVNode(T_a) * G[0];
Ib = ucmVNode(T_b) * G[1] + Ihist_b;
Ihist_b = Ib + ucmVNode(T_b) * G[1];
Ic = ucmVNode(T_c) * G[2] + Ihist_c;
Ihist_c = Ic + ucmVNode(T_c) * G[2];
%% END AFTER VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

## Exercice 2: Series resistor-inductor

Build a UCM representing a **three‑phase RL circuit**, where the resistor RRR and inductor LLL are connected in series in each phase. Use control outputs to provide the required monitoring signals.

The three‑phase RL circuit is modeled as follows:

* Each phase consists of a **series connection of&#x20;*****R*****&#x20;and&#x20;*****L***
* The resistor is connected to the network through an **external node&#x20;*****T***
* The inductor is connected to ground
* The connection between ***R*** and ***L*** is represented by an **internal node&#x20;*****T***<sub>***i***</sub>

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-306636ea1afdd9eaa926742d53f0a6fb34d0c001%2Fucm-exercice2.png?alt=media" alt=""><figcaption></figcaption></figure>

### Solution

```
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN GENERAL INFORMATION -- Enter or modify general information
UCM_NAME = RL
UCM_TYPE = NetworkElement
UCM_CATEGORY = User
UCM_VERSION = "1.0"
UCM_EXEC_TIME = 5.0e-6
UCM_DESCRIPTION = "R and L in series" # Description du modele
%% END GENERAL INFORMATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN DOCUMENTATION -- Enter model’s documentation after this line...
Documentation de Exemple.
---------------------
Three-phase series R-L connected in Y grounded
%% END DOCUMENTATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN TUNABLE PARAMETERS -- Enter parameters table after this line...
L "L" H double 3 [0.01 0.01 0.01] 0.0 1.0e12 - "Inductors"
R "R" ohm double 3 [0.1 0.1 0.1] 0.0 1.0e12 - "Resistors"
%% END TUNABLE PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CONTROL IOS -- Enter control IOs table after this line...
ILa A double out auto IL "ILa"
ILb A double out auto IL "ILb"
ILc A double out auto IL "ILc"
IRa A double out auto IR "IRa"
IRb A double out auto IR "IRb"
IRc A double out auto IR "IRc"
%% END CONTROL IOS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN NODES DEFINITIONS -- Enter nodes table after this line...
T 3 extern bottom no "Network Connection"
Ti 3 intern - no "Internal node"
%% END NODES DEFINITIONS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL CURRENTS -- Enter historical currents table after this...
Ihist_a ground Ti_a
Ihist_b ground Ti_b
Ihist_c ground Ti_c
%% END HISTORICAL CURRENTS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CALCULATED PARAMETERS -- Enter parameters table after this line...
GL - double 3 "Equiv.L conductance"
GR - double 3 "Equiv.R conductance"
%% END CALCULATED PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATORY GLOBAL CODE -- Enter code under the appropriate comment...
/* 8.3.1.1 -- User includes */
#include <stdio.h> /* Needed for using printf */
%% END PREPARATORY GLOBAL CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATION FUNCTION CODE -- Enter code after this line...
int i, j;
/* Calculate equivalent L conductances */
GL[0] = ucmTimeStep / (2.0 * L[0]);
GL[1] = ucmTimeStep / (2.0 * L[1]);
GL[2] = ucmTimeStep / (2.0 * L[2]);
/* Calculate R’s conductances */
GR[0] = 1.0 / R[0];
GR[1] = 1.0 / R[1];
GR[2] = 1.0 / R[2];
/* Initialize Yini and Yfill to 0 */
for (i = 0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
ucmYini(i,j) = 0.0;
ucmYfill(i,j) = 0;
}
}
/* Calculate Yini and define Yfill */
ucmYini(Ti_a, Ti_a) = GL[0] + GR[0];
ucmYini(Ti_b, Ti_b) = GL[1] + GR[1];
ucmYini(Ti_c, Ti_c) = GL[2] + GR[2];
printf ("**** Preparation: GL[0] = %f, GR[0] = %f, ucmYini(Ti_a,Ti_a) = %f *****\n",
GL[0], GR[0], ucmYini(Ti_a, Ti_a));
ucmYini(T_a, T_a) = GR[0];
ucmYini(T_b, T_b) = GR[1];
ucmYini(T_c, T_c) = GR[2];
ucmYini (Ti_a, T_a) = ucmYini (T_a, Ti_a) = -GR[0];
ucmYini (Ti_b, T_b) = ucmYini (T_b, Ti_b) = -GR[1];
ucmYini (Ti_c, T_c) = ucmYini (T_c, Ti_c) = -GR[2];
ucmYfill(T_a, T_a) = 1;
ucmYfill(T_b, T_b) = 1;
ucmYfill(T_c, T_c) = 1;
ucmYfill(Ti_a, Ti_a) = 1;
ucmYfill(Ti_b, Ti_b) = 1;
ucmYfill(Ti_c, Ti_c) = 1;
ucmYfill(Ti_a, T_a) = ucmYfill(T_a, Ti_a) = 1;
ucmYfill(Ti_b, T_b) = ucmYfill(T_b, Ti_b) = 1;
ucmYfill(Ti_c, T_c) = ucmYfill(T_c, Ti_c) = 1;
%% END PREPARATION FUNCTION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN AFTER VOLTAGE CALCULATION -- Enter code ->...
ILa = ucmVNode(Ti_a) * G[0] + Ihist_a;
Ihist_a = ILa + ucmVNode(Ti_a) * G[0];
ILb = ucmVNode(Ti_b) * G[1] + Ihist_b;
Ihist_b = ILb + ucmVNode(Ti_b) * G[1];
ILc = ucmVNode(Ti_c) * G[2] + Ihist_c;
Ihist_c = ILc + ucmVNode(Ti_c) * G[2];
IRa = (ucmVNode(T_a) - ucmVNode(Ti_a)) * GR[0];
IRb = (ucmVNode(T_b) - ucmVNode(Ti_b)) * GR[1];
IRc = (ucmVNode(T_c) - ucmVNode(Ti_c)) * GR[2];
%% END AFTER VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

## Exercice 3: Voltage source

Build a UCM representing a **three‑phase voltage source with a series RL branch** in each phase. The resistor RRR and inductor LLL must be combined into a **single equivalent RL branch** using the trapezoidal integration rule (no internal node).

Compare the results with those obtained from the previous example using separate ***R*** and ***L*** branches.

The model is defined as follows:

* The source is a **three‑phase sine-wave voltage source**
* The parameter **Amp** controls the amplitude of the sine waves
* The parameter **freq** controls the frequency
* The voltage sources are connected in a **Y configuration with grounded neutral**
* Each phase includes a **series inductor&#x20;*****Ls​*** at its output

The model outputs:

* **I\_a​, I\_b​, I\_c​** (grouped at connector *I*): phase currents
* **Db1, Db2, Db3** (connector *Db*): debugging signals

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-344a9bf05ebae94c44b7f439eeb89b81345846a1%2Fucm-exercice3.png?alt=media" alt=""><figcaption></figcaption></figure>

### Solution

```
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN GENERAL INFORMATION -- Enter or modify general information
UCM_NAME = VSrc
UCM_TYPE = NetworkElement
UCM_CATEGORY = User
UCM_VERSION = "1.0"
UCM_EXEC_TIME = 5.0e-6
UCM_DESCRIPTION = "Three-phase Voltage source with inductor at output"
%% END GENERAL INFORMATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN DOCUMENTATION -- Enter model’s documentation after this line...
Three-phase sine-wave voltage source with inductor at output
%% END DOCUMENTATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN TUNABLE PARAMETERS -- Enter parameters table after this line...
Amp "Amp" V double 1 1.0 0.0 10.0 - "Source Amplitude"
Ls "Ls" H double 3 [0.1 0.1 0.1] 0.0 1.0e12 - "Output inductor"
freq "freq" Hz double 1 60.0 1.0e-12 1.0e12 - "frequency"
%% END TUNABLE PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CONTROL IOS -- Enter control IOs table after this line...
I_a - double out auto I "Output current, phase a"
I_b - double out auto I "Output current, phase b"
I_c - double out auto I "Output current, phase c"
Db1 - double out auto Db "Debugging output 1"
Db2 - double out auto Db "Debugging output 2"
Db3 - double out auto Db "Debugging output 3"
%% END CONTROL IOS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN NODES DEFINITIONS -- Enter nodes table after this line...
T 3 extern bottom no "Network Connection"
%% END NODES DEFINITIONS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CURRENT SOURCES -- Enter sources table after this line...
Isrc_a T_a ground
Isrc_b T_b ground
Isrc_c T_c ground
%% END CURRENT SOURCES
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL CURRENTS -- Enter historical currents table after this...
Ihist_a T_a ground
Ihist_b T_b ground
Ihist_c T_c ground
%% END HISTORICAL CURRENTS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CALCULATED PARAMETERS -- Enter parameters table after this line...
Gs - double 3 "Equiv.Conductance"
omega - double 1 "omega"
period - double 1 "period"
%% END CALCULATED PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATION FUNCTION CODE -- Enter code after this line...
int i, j;
doubleGLo, Lo;
/* Calculate equivalent conductances of Inductances */
Gs[0] = ucmTimeStep / (2.0 * Ls[0]);
Gs[1] = ucmTimeStep / (2.0 * Ls[1]);
Gs[2] = ucmTimeStep / (2.0 * Ls[2]);
/* Initialize Yini and Yfill */
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
ucmYini(i,j) = 0.0;
ucmYfill(i,j) = 0;
}
}
/* Calculate Yini and determine Yfill */
ucmYini(T_a, T_a) = Gs[0];
ucmYini(T_b, T_b) = Gs[1];
ucmYini(T_c, T_c) = Gs[2];
ucmYfill(T_a, T_a) = 1;
ucmYfill(T_b, T_b) = 1;
ucmYfill(T_c, T_c) = 1;
/* Calculate other Simulation parameters */
period = 1.0 / freq;
omega = 2.0 * 3.141593 * freq;
%% END PREPARATION FUNCTION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL VALUES -- Enter the table after this line...
time double 1 0.0 "time"
%% END HISTORICAL VALUES
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN SIMULATION GLOBAL CODE -- Enter code under the appropriate comment...
/* 9.3.1 -- User includes */
#include <math.h>
%%% END SIMULATION GLOBAL CODE
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN BEFORE VOLTAGE CALCULATION CODE -- Enter code ->...
/* User code: calulate Yadd */
int i;
doubleVsrc_a, Vsrc_b, Vsrc_c;
doubleTwoPiOver3 = 2.094395333; /* 2.0 Pi / 3 */
/* Calculate source voltages */
Vsrc_a = Amp * sin (omega * time);
Vsrc_b = Amp * sin (omega * time - TwoPiOver3);
Vsrc_c = Amp * sin (omega * time + TwoPiOver3);
/* Calculate current sources */
Isrc_a = Vsrc_a * Gs[0];
Isrc_b = Vsrc_b * Gs[1];
Isrc_c = Vsrc_c * Gs[2];
%% END BEFORE VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN AFTER VOLTAGE CALCULATION -- Enter code ->...
double Vsrc_a, Vsrc_b, Vsrc_c;
Vsrc_a = Isrc_a / Gs[0];
Vsrc_b = Isrc_b / Gs[1];
Vsrc_c = Isrc_c / Gs[2];
/* Calculate output currents and historic currents */
I_a = (Vsrc_a - ucmVNode(T_a)) * Gs[0] + Ihist_a;
Ihist_a = I_a + (Vsrc_a - ucmVNode(T_a)) * Gs[0];
I_b = (Vsrc_b - ucmVNode(T_b)) * Gs[1] + Ihist_b;
Ihist_b = I_b + (Vsrc_b - ucmVNode(T_b)) * Gs[1];
I_c = (Vsrc_c - ucmVNode(T_c)) * Gs[2] + Ihist_c;
Ihist_c = I_c + (Vsrc_c - ucmVNode(T_c)) * Gs[2];
/* Other calculations */
time += ucmTimeStep;
if (time >= period) /* To avoid truncation error */
time -= period;
/* Debugging outputs */
Db1 = I_a;
Db2 = Ihist_a;
Db3 = time;
%% END AFTER VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

## Exercice 4: Inductor description

Build a UCM representing a **three‑phase non‑linear inductance** driven by a sine‑wave voltage source with **modulated amplitude**. The amplitude modulation is defined by a **low-frequency triangular signal**.

The model is defined as follows:

* The inductance is **non‑linear**, with its value depending on the **rectified three‑phase voltage across it**
* The nonlinearity is defined using:
  * a vector **Lvec** containing inductance values
  * a dimension **Ldim**
  * a voltage step **Vinc**, defining the interval between values
* The inductance is connected to the network through an **external node T**
* The model uses a **three‑phase Y‑grounded configuration**

The model outputs:

* **Ia​, Ib​, Ic​** (connector *I*): phase currents
* **Vrec** (connector *Vrec*): rectified three‑phase voltage across the inductance

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-562024e5f5eaa2439c70fb19cf9fb32052b53f4e%2Fucm-exercice4.png?alt=media" alt=""><figcaption></figcaption></figure>

### Solution

```
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN GENERAL INFORMATION -- Enter or modify general information
UCM_NAME = Lnlin
UCM_TYPE = NetworkElement
UCM_CATEGORY = User
UCM_VERSION = "1.0"
UCM_EXEC_TIME = 5.0e-6
UCM_DESCRIPTION = "Non-linear inductor"
%% END GENERAL INFORMATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN DOCUMENTATION -- Enter model’s documentation after this line...
Non-linear inductor:
L depends on rectified value of three-phase voltages across it
%% END DOCUMENTATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN TUNABLE PARAMETERS -- Enter parameters table after this line...
Lvec "Lvec" H double 9 [1.0 0.60 0.36 0.22 0.13 0.08 0.04 0.03 0.01] 0.0 10.0 - "L lookup vector"
Vinc "Vinc" V double 1 500 0.0 1000 - "V look up vector"
Ldim "Ldim" - int 1 9 0.0 10.0 - "L dimension"
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CONTROL IOS -- Enter control IOs table after this line...
Ia A double out auto I "Current phase a"
Ib A double out auto I "Current phase b"
Ic A double out auto I "Current phase c"
Db1 - double out auto Db "Debugging signal no. 1"
Db2 - double out auto Db "Debugging signal no. 2"
Db3 - double out auto Db "Debugging signal no. 3"
Vrec V double out auto - "Rectified 3-phase voltage across L"
%% END CONTROL IOS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN NODES DEFINITIONS -- Enter nodes table after this line...
T 3 extern top yes "Network Connection"
%% END NODES DEFINITIONS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL CURRENTS -- Enter historical currents table after this...
Ihist_a ground T_a
Ihist_b ground T_b
Ihist_c ground T_c
%% END HISTORICAL CURRENTS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CALCULATED PARAMETERS -- Enter parameters table after this line...
GaddVec - double 9 "Additional conductance of L"
%% END CALCULATED PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATION FUNCTION CODE -- Enter code after this line...
int i, j;
double GLo, Lo;

/* Initialize Yini and Yfill */
for (i = 0; i < 3; i++)
{
  for (j = 0; j < 3; j++)
  {
    ucmYini(i,j) = 0.0;
    ucmYfill(i,j) = 0;
  }
}

/* Compute Yini and define Yfill */
Lo = Lvec[0];
GLo = ucmTimeStep / (2.0 * Lo);
ucmYini(T_a, T_a) = GLo;
ucmYini(T_b, T_b) = GLo;
ucmYini(T_c, T_c) = GLo;
ucmYfill(T_a, T_a) = 1;
ucmYfill(T_b, T_b) = 1;
ucmYfill(T_c, T_c) = 1;

/* Calculate GaddVec, conductance to be added to the initial Yini when L changes */
for (i = 0; i < Ldim; i++)
{
  GaddVec[i] = (ucmTimeStep / (2.0 * Lvec[i])) - GLo;
}
%% END PREPARATION FUNCTION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN HISTORICAL VALUES
Lchanged int 1 0 "Indicate if L has changed"
Lindex   int 1 0 "Index in the GaddVec vector"
%% END HISTORICAL VALUES
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN BEFORE VOLTAGE CALCULATION CODE -- Enter code ->...
/* User code: calulate Yadd */
int i;
/* Initialize nodes */
for (i = 0; i < 3; i++)
  ucmNodeChanged(i) = 0;

/* If L has been changed, determine the additional conductance and
indicate that corresponding nodes are affected */
if (Lchanged)
{
  ucmYadd(T_a,T_a) = GaddVec[Lindex];
  ucmYadd(T_b,T_b) = GaddVec[Lindex];
  ucmYadd(T_c,T_c) = GaddVec[Lindex];
  ucmNodeChanged(T_a) = 1;
  ucmNodeChanged(T_b) = 1;
  ucmNodeChanged(T_c) = 1;
}
%% END BEFORE VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%% BEGIN AFTER VOLTAGE CALCULATION -- Enter code ->...
double Lo, GLo, GL;
double VL_a, VL_b, VL_c;
int index;

/* Calculate 3-phase rectified voltage across L */
VL_a = ucmVNode(T_a);
VL_b = ucmVNode(T_b);
VL_c = ucmVNode(T_c);
Vrec = fabs(VL_a);
if (Vrec < fabs(VL_b))
  Vrec = fabs(VL_b);
if (Vrec < fabs(VL_c))
  Vrec = fabs(VL_c);

/* Calculate historic currents */
Lo = Lvec[0];
GLo = ucmTimeStep / (2.0 * Lo);
GL = GLo + GaddVec[Lindex];

Ia = VL_a * GL + Ihist_a;
Ihist_a = Ia + VL_a * GL;
Ib = VL_b * GL + Ihist_b;
Ihist_b = Ib + VL_b * GL;
Ic = VL_c * GL + Ihist_c;
Ihist_c = Ic + VL_c * GL;

/* New operating point of L: calculate the index in the non-linear Lvec table */
Lchanged = 0;
index = (int)(Vrec / Vinc);
if (index > Ldim-1) /* To avoid going outside of the table bounds */
  index = Ldim - 1;
if (index < 0)
  index = 0;
if (index != Lindex) /* Compare index with its past value (historic) */
{
  Lindex = index;
  Lchanged = 1;
}

/* Debugging signal */
Db1 = Lchanged;
Db2 = index;
Db3 = 0.0;
%% END AFTER VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

## Exercice 5: Resistor description

Build a UCM representing a **three‑phase resistor** connected in a **Y configuration with a grounded neutral**.

The model is defined as follows:

* The resistor values are defined by the parameter **R\[3]**, a vector containing one resistance per phase
* The resistors are connected to the network through a single **external node T**
* The configuration is **three‑phase, Y‑connected, with grounded neutral**

The model outputs:

* **Ia, Ib​, Ic​**: phase currents
* These outputs are **grouped** and connected to the connector **I**

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-ddf7a620291444a9fa3640485b60063a0e317923%2Fucm-exercice5.png?alt=media" alt=""><figcaption></figcaption></figure>

### Solution

```
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN GENERAL INFORMATION -- Enter or modify general information
UCM_NAME = Resistor
UCM_TYPE = NetworkElement
UCM_CATEGORY = User
UCM_VERSION = "1.0"
UCM_EXEC_TIME = 5.0e-6
UCM_DESCRIPTION = "Resistor 3 phase-ground" # Description du modele
%% END GENERAL INFORMATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN DOCUMENTATION -- Enter model’s documentation after this line...
Documentation de Exemple.
---------------------
Three-phase resistor connected in Y grounded
%% END DOCUMENTATION
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN TUNABLE PARAMETERS -- Enter parameters table after this line...
R "R" - double 3 [1.0 1.0 1.0] 0.0 1.0e12 - "R"
%% END TUNABLE PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CONTROL IOS -- Enter control IOs table after this line...
Ia A double out auto I "Ia"
Ib A double out auto I "Ib"
Ic A double out auto I "Ic"
%% END CONTROL IOS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN NODES DEFINITIONS -- Enter nodes table after this line...
T 3 extern bottom no "Network Connection"
%% END NODES DEFINITIONS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN CALCULATED PARAMETERS -- Enter parameters table after this line...
G - double 3 "Equiv.conductance"
%% END CALCULATED PARAMETERS
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATORY GLOBAL CODE -- Enter code under the appropriate comment...
/* 8.3.1.1 -- User includes */
#include <stdio.h>
%% END PREPARATORY GLOBAL CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN PREPARATION FUNCTION CODE -- Enter code after this line...
int i, j;
G[0] = 1.0 / R[0];
G[1] = 1.0 / R[1];
G[2] = 1.0 / R[2];
/* Initialization of Yini and Yfill */
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
ucmYini(i,j) = 0.0;
ucmYfill(i,j) = 0;
}
}
/* Calculate Yini and define Yfill */
ucmYini(T_a, T_a) = 1.0 * G[0];
ucmYini(T_b, T_b) = 1.0 * G[1];
ucmYini(T_c, T_c) = 1.0 * G[2];
ucmYfill(T_a, T_a) = 1;
ucmYfill(T_b, T_b) = 1;
ucmYfill(T_c, T_c) = 1;
%% END PREPARATION FUNCTION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%%vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
%% BEGIN AFTER VOLTAGE CALCULATION -- Enter code ->...
/* Calculate Resistor current and send them to control outputs */
Ia = ucmVNode(T_a) * G[0];
Ib = ucmVNode(T_b) * G[1];
Ic = ucmVNode(T_c) * G[2];
%% END AFTER VOLTAGE CALCULATION CODE
%%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/hypersim/model-and-simulation/import-models-from-external-software-or-code/create-user-coded-models-ucm/ucm-exercices-and-solutions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
