In this page
GreyCat Powergrid Library
@library("powergrid", "0.0.0");
Power flow analysis for electrical networks. Uses a Newton-Raphson solver to compute voltages, currents, power flows, and losses across buses and transmission lines.
Quick Start
// Define a simple 3-bus network: external grid → line → load
var network = PowerNetwork {};
network.configure(3, 2, 1, 1); // 3 buses, 2 lines, 1 ext grid, 1 load
// Create buses at 20 kV
network.createBus(0, 20.0);
network.createBus(1, 20.0);
network.createBus(2, 20.0);
// External grid (slack bus) at bus 0, voltage = 1.0 p.u.
network.createExtGrid(0, 1.0);
// Transmission lines
network.createLine(0, 0, 1, 10.0, 0.1, 0.4, 9.7, 0.5); // bus 0 → bus 1, 10 km
network.createLine(1, 1, 2, 5.0, 0.1, 0.4, 9.7, 0.5); // bus 1 → bus 2, 5 km
// Load at bus 2: 2 MW active, 0.5 MVar reactive
network.createLoad(2, 2.0, 0.5);
// Solve power flow
network.compute();
// Read results
var busResult = network.getBusResult(2);
info("Bus 2 voltage: ${busResult.abs} p.u., angle: ${busResult.angle_radians} rad");
var lineResult = network.getLineResult(0);
info("Line 0: ${lineResult.p_from_mw} MW, losses: ${lineResult.pl_mw} MW, loading: ${lineResult.loading_percent}%");
Workflow
- Configure — declare network dimensions with
configure() - Build topology — create buses, lines, loads, and external grids
- Solve — run Newton-Raphson power flow with
compute() - Read results — query bus and line results
API Reference
PowerNetwork
Electrical network model and power flow solver.
Constructor:
| Field | Type | Description |
|---|---|---|
tolerance |
float? |
Convergence tolerance for Newton-Raphson solver |
max_iteration |
int? |
Maximum solver iterations |
Methods:
| Method | Returns | Description |
|---|---|---|
configure(nb_bus, nb_lines, nb_ext_grids, nb_loads) |
void |
Set network dimensions. Must be called first. |
createBus(bus_id, vn_kv) |
void |
Create a bus at given nominal voltage (kV) |
createLine(line_id, from, to, length_km, r, x, c, max_i_ka) |
void |
Create a transmission line between two buses |
createLoad(bus_id, p_mw, q_mvar) |
void |
Create a load at a bus |
createExtGrid(bus_id, vm_pu) |
void |
Create an external grid (slack bus) with fixed voltage |
compute() |
void |
Run Newton-Raphson power flow analysis |
getBusResult(bus_id) |
PowerBusResult |
Get voltage/current results for a bus |
getLineResult(line_id) |
PowerLineResult |
Get power flow results for a line |
getCheckSum() |
Array<float> |
Get solver convergence metrics |
createLine Parameters
| Parameter | Unit | Description |
|---|---|---|
line_id |
— | Unique line identifier (0 to nb_lines-1) |
from_bus_id |
— | Starting bus ID |
to_bus_id |
— | Ending bus ID |
lenght_km |
km | Line length |
r_ohm_per_km |
Ω/km | Resistance per kilometer |
x_ohm_per_km |
Ω/km | Reactance per kilometer |
c_n_f_per_km |
nF/km | Capacitance per kilometer |
max_i_ka |
kA | Maximum current rating |
PowerBusResult
Results for a single bus after power flow computation.
| Field | Type | Unit | Description |
|---|---|---|---|
abs |
float |
p.u. | Voltage magnitude |
angle_radians |
float |
rad | Voltage phase angle |
voltage |
float |
— | Real component of voltage |
voltage_img |
float |
— | Imaginary component of voltage |
current |
float |
— | Real component of current |
current_img |
float |
— | Imaginary component of current |
PowerLineResult
Results for a transmission line after power flow computation.
| Field | Type | Unit | Description |
|---|---|---|---|
p_from_mw |
float |
MW | Active power at “from” bus |
q_from_mvar |
float |
MVar | Reactive power at “from” bus |
p_to_mw |
float |
MW | Active power at “to” bus |
q_to_mvar |
float |
MVar | Reactive power at “to” bus |
pl_mw |
float |
MW | Active power losses |
ql_mvar |
float |
MVar | Reactive power consumption |
i_from_ka |
float |
kA | Current at “from” bus |
i_to_ka |
float |
kA | Current at “to” bus |
i_ka |
float |
kA | Maximum current (max of from/to) |
vm_from_pu |
float |
p.u. | Voltage magnitude at “from” bus |
vm_to_pu |
float |
p.u. | Voltage magnitude at “to” bus |
va_from_radians |
float |
rad | Voltage angle at “from” bus |
va_to_radians |
float |
rad | Voltage angle at “to” bus |
loading_percent |
float |
% | Line loading percentage |
Example — Analyzing Line Loading
var network = PowerNetwork { tolerance: 1e-8, max_iteration: 50 };
network.configure(2, 1, 1, 1);
network.createBus(0, 110.0); // 110 kV bus
network.createBus(1, 110.0);
network.createExtGrid(0, 1.0);
network.createLine(0, 0, 1, 20.0, 0.05, 0.3, 11.0, 0.6);
network.createLoad(1, 10.0, 3.0); // 10 MW, 3 MVar
network.compute();
var line = network.getLineResult(0);
info("Power flow: ${line.p_from_mw} MW → ${line.p_to_mw} MW");
info("Losses: ${line.pl_mw} MW + ${line.ql_mvar} MVar");
info("Current: ${line.i_ka} kA");
info("Loading: ${line.loading_percent}%");
if (line.loading_percent > 80.0) {
warn("Line 0 is heavily loaded!");
}