PGM Calculation Engine Examples

These examples show how to interact with the power-grid-model calculation engine to perform power flow calculations. For a detailed documentation on the calculation engine please refer to power-grid-model docs.

Single Power Flow example

We will demonstrate how to use the PGMCoreInterface to perform Power Flow calculations on a Grid object. In the examples the RadialGridGenerator is used to create randomised input networks.

from power_grid_model_ds import Grid, PowerGridModelInterface
from power_grid_model_ds.generators import RadialGridGenerator
grid_generator = RadialGridGenerator(grid_class=Grid, nr_nodes=5, nr_sources=1, nr_nops=0)
grid = grid_generator.run(seed=0)

print("Created Grid:")
print(grid.branches)

core_interface = PowerGridModelInterface(grid=grid)

# Create input from grid and calculate power flow
core_interface.create_input_from_grid()
output = core_interface.calculate_power_flow()

print("Power Flow Results:")
display(output["node"])
display(output["line"])
Created Grid:
 id | from_node | to_node | from_status | to_status | feeder_branch_id | feeder_node_id | is_feeder 
 18 |     16    |    5    |      1      |     1     |        0         |       0        |   False   
 19 |     5     |    3    |      1      |     1     |        0         |       0        |   False   
 20 |     5     |    4    |      1      |     1     |        0         |       0        |   False   
 21 |     4     |    2    |      1      |     1     |        0         |       0        |   False   
 22 |     16    |    1    |      1      |     1     |        0         |       0        |   False   
Power Flow Results:
array([( 1, 1, 1.00022909, 10502.40547296, 1.99595527e-04,   318145.99999988,  30628.99999997),
       ( 2, 1, 1.00181391, 10519.04604187, 8.18373270e-05,    85535.99999973,  24048.99999998),
       ( 3, 1, 1.00120197, 10512.62069113, 3.07703301e-04,   913761.00000028,  16071.00000004),
       ( 4, 1, 1.00164458, 10517.26805716, 1.15203146e-04,   176577.00000024,  -9406.99999999),
       ( 5, 1, 1.00008458, 10500.88803896, 2.02276426e-04,   536068.9999966 , -31488.00000006),
       (16, 1, 1.00002312, 10500.2427774 , 2.01540352e-04, -2028475.46388834, -29731.71601102)],
      dtype={'names': [id, energized, u_pu, u, u_angle, p, q], 'formats': ['<i4', 'i1', '<f8', '<f8', '<f8', '<f8', '<f8'], 'offsets': [0, 4, 8, 16, 24, 32, 40], 'itemsize': 48, 'aligned': True})
array([(18, 1, 0.30944115, -1710395.03728345,    891.59535897, 94.04517498, 1710395.26966902, 1710500.14523965,  -890.39109607, 94.04517498, 1710500.37698364),
       (19, 1, 0.11782317,  -912742.88075081, -15956.83635493, 50.19126661,  912882.35112075,  913761.00000028, 16071.00000004, 50.19126661,  913902.31543777),
       (20, 1, 0.0109873 ,  -261688.26448943, -14640.7725491 , 14.41040626,  262097.50092728,  262097.7399271 , 14640.78859394, 14.41040626,  262506.33890545),
       (21, 1, 0.04259208,   -85520.73992672, -24047.78859392,  4.87677287,   88837.45321581,   85535.99999973, 24048.99999998,  4.87677287,   88852.47152979),
       (22, 1, 0.02406958,  -318080.42660057, -30623.31136999, 17.57035082,  319551.16176547,  318145.99999988, 30628.99999997, 17.57035082,  319616.97851792)],
      dtype={'names': [id, energized, loading, p_from, q_from, i_from, s_from, p_to, q_to, i_to, s_to], 'formats': ['<i4', 'i1', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8'], 'offsets': [0, 4, 8, 16, 24, 32, 40, 48, 56, 64, 72], 'itemsize': 80, 'aligned': True})

Updating the Grid object

If you want to store specific outputs of the calculation in the Grid object you can specify the columns in extended arrays. For a more detailed how to on extending arrays there is a seperate example.

from dataclasses import dataclass
from typing import ClassVar

import numpy as np
from numpy.typing import NDArray

from power_grid_model_ds import GraphContainer, Grid
from power_grid_model_ds.arrays import LineArray, NodeArray


class ExtendedNodeArray(NodeArray):
    """Extends the node array with an output value"""

    _defaults: ClassVar = {"u": 0}

    u: NDArray[np.float64]


class ExtendedLineArray(LineArray):
    """Extends the line array with an output value"""

    _defaults: ClassVar = {"i_from": 0}

    i_from: NDArray[np.float64]


@dataclass
class ExtendedGrid(Grid):
    """
    This is my own grid to extend.
    """

    node: ExtendedNodeArray
    line: ExtendedLineArray
    graphs: GraphContainer
grid_generator = RadialGridGenerator(grid_class=ExtendedGrid, nr_nodes=5, nr_sources=1, nr_nops=0)
grid = grid_generator.run(seed=0)

core_interface = PowerGridModelInterface(grid=grid)
core_interface.create_input_from_grid()
core_interface.calculate_power_flow()
core_interface.update_grid()

print(grid.node)
print(grid.line)
 id | u_rated | node_type | feeder_branch_id | feeder_node_id |     u     
 1  | 10500.0 |     0     |        0         |       0        |10502.405..
 2  | 10500.0 |     0     |        0         |       0        |10519.046..
 3  | 10500.0 |     0     |        0         |       0        |10512.620..
 4  | 10500.0 |     0     |        0         |       0        |10517.268..
 5  | 10500.0 |     0     |        0         |       0        |10500.888..
 16 | 10500.0 |     1     |   -2147483648    |  -2147483648   |10500.242..
 id | from_node | to_node | from_status | to_status | feeder_branch_id | feeder_node_id | is_feeder |   r1  |   x1  |  c1 | tan1 |   i_n    | i_from 
 18 |     16    |    5    |      1      |     1     |        0         |       0        |   False   |0.003..|4.538..| 0.0 | 0.0  |303.9194..|94.045..
 19 |     5     |    3    |      1      |     1     |        0         |       0        |   False   |0.134..|0.015..| 0.0 | 0.0  |425.9880..|50.191..
 20 |     5     |    4    |      1      |     1     |        0         |       0        |   False   |0.657..|2.575..| 0.0 | 0.0  |1311.550..|14.410..
 21 |     4     |    2    |      1      |     1     |        0         |       0        |   False   |0.213..|0.016..| 0.0 | 0.0  |114.4995..|4.8767..
 22 |     16    |    1    |      1      |     1     |        0         |       0        |   False   |0.070..|0.006..| 0.0 | 0.0  |729.9817..|17.570..

Batch Load Flow Example

To use the PGM batch calculation functionality you can supply a dictionary with update data to the calculate_power_flow function.

from power_grid_model import AttributeType, ComponentType, DatasetType, initialize_array

grid_generator = RadialGridGenerator(grid_class=Grid, nr_nodes=5, nr_sources=1, nr_nops=0)
grid = grid_generator.run(seed=0)
core_interface = PowerGridModelInterface(grid=grid)

update_sym_load = initialize_array(DatasetType.update, ComponentType.sym_load, (10, len(grid.sym_load)))
update_sym_load[AttributeType.id] = [grid.sym_load.id.tolist()]
update_sym_load[AttributeType.p_specified] = [grid.sym_load.p_specified.tolist()] * np.linspace(0, 1, 10).reshape(-1, 1)
update_sym_load[AttributeType.q_specified] = [grid.sym_load.q_specified.tolist()] * np.linspace(0, 1, 10).reshape(-1, 1)
update_data = {
    ComponentType.sym_load: update_sym_load,
}
output = core_interface.calculate_power_flow(update_data=update_data)

# Results have been calculated for all 10 scenarios
display(output["line"])
array([[(18, 1, 0.00000000e+00,  0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00),
        (19, 1, 0.00000000e+00,  0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00),
        (20, 1, 3.57606251e-15, -8.52651283e-08, -2.37657116e-09, 4.69018698e-12, 8.52982426e-08, 8.52651283e-08,  2.37657116e-09, 4.69018698e-12, 8.52982426e-08),
        (21, 1, 0.00000000e+00,  0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00),
        (22, 1, 0.00000000e+00,  0.00000000e+00, -0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,  0.00000000e+00, 0.00000000e+00, 0.00000000e+00)],
       [(18, 1, 3.44107076e-02, -1.90196733e+05,  8.75536458e+01, 1.04580823e+01, 1.90196753e+05, 1.90198033e+05, -8.75387538e+01, 1.04580823e+01, 1.90198053e+05),
        (19, 1, 1.31054467e-02, -1.01516404e+05, -1.78425423e+03, 5.58276417e+00, 1.01532083e+05, 1.01529000e+05,  1.78566667e+03, 5.58276417e+00, 1.01544702e+05),
        (20, 1, 1.22265894e-03, -2.91184071e+04, -1.62687369e+03, 1.60357909e+00, 2.91638192e+04, 2.91234777e+04,  1.62687389e+03, 1.60357909e+00, 2.91688819e+04),
        (21, 1, 4.74008024e-03, -9.50381100e+03, -2.67209611e+03, 5.42736993e-01, 9.87231083e+03, 9.50400000e+03,  2.67211111e+03, 5.42736993e-01, 9.87249684e+03),
        (22, 1, 2.67494191e-03, -3.53487457e+04, -3.40315196e+03, 1.95265873e+00, 3.55121847e+04, 3.53495556e+04,  3.40322222e+03, 1.95265873e+00, 3.55129976e+04)],
       [(18, 1, 6.88143111e-02, -3.80355178e+05,  1.77990815e+02, 2.09140055e+01, 3.80355220e+05, 3.80360376e+05, -1.77931259e+02, 2.09140055e+01, 3.80360418e+05),
        (19, 1, 2.62073910e-02, -2.03007629e+05, -3.56568508e+03, 1.11640363e+01, 2.03038940e+05, 2.03058000e+05,  3.57133333e+03, 1.11640363e+01, 2.03089403e+05),
        (20, 1, 2.44485484e-03, -5.82263031e+04, -3.25371699e+03, 3.20655087e+00, 5.83171419e+04, 5.82465776e+04,  3.25371779e+03, 3.20655087e+00, 5.83373850e+04),
        (21, 1, 9.47824841e-03, -1.90072443e+04, -5.34416223e+03, 1.08525505e+00, 1.97442499e+04, 1.90080000e+04,  5.34422222e+03, 1.08525505e+00, 1.97449937e+04),
        (22, 1, 5.34974762e-03, -7.06958718e+04, -6.80616342e+03, 3.90521802e+00, 7.10227439e+04, 7.06991111e+04,  6.80644444e+03, 3.90521802e+00, 7.10259952e+04)],
       [(18, 1, 1.03210817e-01, -5.70475368e+05,  2.71309189e+02, 3.13677717e+01, 5.70475432e+05, 5.70487061e+05, -2.71175217e+02, 3.13677717e+01, 5.70487125e+05),
        (19, 1, 3.93058357e-02, -3.04473694e+05, -5.34429483e+03, 1.67438177e+01, 3.04520594e+05, 3.04587000e+05,  5.35700000e+03, 1.67438177e+01, 3.04634105e+05),
        (20, 1, 3.66658822e-03, -8.73236998e+04, -4.88052995e+03, 4.80891604e+00, 8.74599801e+04, 8.73693003e+04,  4.88053174e+03, 4.80891604e+00, 8.75055097e+04),
        (21, 1, 1.42145067e-02, -2.85103003e+04, -8.01619841e+03, 1.62755444e+00, 2.96158178e+04, 2.85120000e+04,  8.01633333e+03, 1.62755444e+00, 2.96174905e+04),
        (22, 1, 8.02441714e-03, -1.06041379e+05, -1.02090344e+04, 5.85767791e+00, 1.06531678e+05, 1.06048667e+05,  1.02096667e+04, 5.85767791e+00, 1.06538993e+05)],
       [(18, 1, 1.37600231e-01, -7.60557336e+05,  3.67506454e+02, 4.18193825e+01, 7.60557425e+05, 7.60578120e+05, -3.67268330e+02, 4.18193825e+01, 7.60578209e+05),
        (19, 1, 5.24007836e-02, -4.05914622e+05, -7.12008573e+03, 2.23221096e+01, 4.05977063e+05, 4.06116000e+05,  7.14266667e+03, 2.23221096e+01, 4.06178807e+05),
        (20, 1, 4.88785959e-03, -1.16410609e+05, -6.50731261e+03, 6.41067526e+00, 1.16592346e+05, 1.16491646e+05,  6.50731578e+03, 6.41067526e+00, 1.16673257e+05),
        (21, 1, 1.89488575e-02, -3.80129796e+04, -1.06882047e+04, 2.16963541e+00, 3.94870148e+04, 3.80160000e+04,  1.06884444e+04, 2.16963541e+00, 3.94899873e+04),
        (22, 1, 1.06989505e-02, -1.41385266e+05, -1.36117649e+04, 7.81003841e+00, 1.42038986e+05, 1.41398222e+05,  1.36128889e+04, 7.81003841e+00, 1.42051990e+05)],
       [(18, 1, 1.71982560e-01, -9.50601117e+05,  4.66580299e+02, 5.22688401e+01, 9.50601232e+05, 9.50633585e+05, -4.66208307e+02, 5.22688401e+01, 9.50633699e+05),
        (19, 1, 6.54922375e-02, -5.07330430e+05, -8.89306003e+03, 2.78989130e+01, 5.07408368e+05, 5.07645000e+05,  8.92833333e+03, 2.78989130e+01, 5.07723509e+05),
        (20, 1, 6.10866949e-03, -1.45487044e+05, -8.13406499e+03, 8.01182923e+00, 1.45714251e+05, 1.45613616e+05,  8.13406995e+03, 8.01182923e+00, 1.45840626e+05),
        (21, 1, 2.36813029e-02, -4.75152825e+04, -1.33601811e+04, 2.71149821e+00, 4.93578414e+04, 4.75200000e+04,  1.33605556e+04, 2.71149821e+00, 4.93624842e+04),
        (22, 1, 1.33733478e-02, -1.76727535e+05, -1.70143550e+04, 9.76229954e+00, 1.77544670e+05, 1.76747778e+05,  1.70161111e+04, 9.76229954e+00, 1.77564988e+05)],
       [(18, 1, 2.06357811e-01, -1.14060674e+06,  5.68528416e+02, 6.27161462e+01, 1.14060689e+06, 1.14065349e+06, -5.67992857e+02, 6.27161462e+01, 1.14065363e+06),
        (19, 1, 7.85802003e-02, -6.08721140e+05, -1.06632200e+04, 3.34742292e+01, 6.08814529e+05, 6.09174000e+05,  1.07140000e+04, 3.34742292e+01, 6.09268210e+05),
        (20, 1, 7.32901843e-03, -1.74553014e+05, -9.76078714e+03, 9.61237864e+00, 1.74825707e+05, 1.74735210e+05,  9.76079428e+03, 9.61237864e+00, 1.75007619e+05),
        (21, 1, 2.84118451e-02, -5.70172096e+04, -1.60321276e+04, 3.25314311e+00, 5.92282982e+04, 5.70240000e+04,  1.60326667e+04, 3.25314311e+00, 5.92349810e+04),
        (22, 1, 1.60476089e-02, -2.12068185e+05, -2.04168047e+04, 1.17144613e+01, 2.13048729e+05, 2.12097333e+05,  2.04193333e+04, 1.17144613e+01, 2.13077986e+05)],
       [(18, 1, 2.40725988e-01, -1.33057425e+06,  6.73348499e+02, 7.31613028e+01, 1.33057442e+06, 1.33063786e+06, -6.72619694e+02, 7.31613028e+01, 1.33063803e+06),
        (19, 1, 9.16646747e-02, -7.10086772e+05, -1.24305679e+04, 3.90480594e+01, 7.10195567e+05, 7.10703000e+05,  1.24996667e+04, 3.90480594e+01, 7.10812912e+05),
        (20, 1, 8.54890694e-03, -2.03608533e+05, -1.13874791e+04, 1.12123242e+01, 2.03926726e+05, 2.03856428e+05,  1.13874888e+04, 1.12123242e+01, 2.04174235e+05),
        (21, 1, 3.31404864e-02, -6.65187612e+04, -1.87040444e+04, 3.79457035e+00, 6.90983854e+04, 6.65280000e+04,  1.87047778e+04, 3.79457035e+00, 6.91074779e+04),
        (22, 1, 1.87217340e-02, -2.47407217e+05, -2.38191139e+04, 1.36665238e+01, 2.48551164e+05, 2.47446889e+05,  2.38225556e+04, 1.36665238e+01, 2.48590983e+05)],
       [(18, 1, 2.75087099e-01, -1.52050367e+06,  7.81038246e+02, 8.36043117e+01, 1.52050387e+06, 1.52058674e+06, -7.80086534e+02, 8.36043117e+01, 1.52058694e+06),
        (19, 1, 1.04745664e-01, -8.11427346e+05, -1.41951059e+04, 4.46204048e+01, 8.11551501e+05, 8.12232000e+05,  1.42853333e+04, 4.46204048e+01, 8.12357614e+05),
        (20, 1, 9.76833552e-03, -2.32653613e+05, -1.30141409e+04, 1.28116665e+01, 2.33017320e+05, 2.32977271e+05,  1.30141536e+04, 1.28116665e+01, 2.33340475e+05),
        (21, 1, 3.78672290e-02, -7.60199378e+04, -2.13759313e+04, 4.33578018e+00, 7.89681036e+04, 7.60320000e+04,  2.13768889e+04, 4.33578018e+00, 7.89799747e+04),
        (22, 1, 2.13957231e-02, -2.82744631e+05, -2.72212828e+04, 1.56184869e+01, 2.84051975e+05, 2.82796444e+05,  2.72257778e+04, 1.56184869e+01, 2.84103981e+05)],
       [(18, 1, 3.09441150e-01, -1.71039504e+06,  8.91595359e+02, 9.40451750e+01, 1.71039527e+06, 1.71050015e+06, -8.90391096e+02, 9.40451750e+01, 1.71050038e+06),
        (19, 1, 1.17823170e-01, -9.12742881e+05, -1.59568364e+04, 5.01912666e+01, 9.12882351e+05, 9.13761000e+05,  1.60710000e+04, 5.01912666e+01, 9.13902315e+05),
        (20, 1, 1.09873047e-02, -2.61688264e+05, -1.46407725e+04, 1.44104063e+01, 2.62097501e+05, 2.62097740e+05,  1.46407886e+04, 1.44104063e+01, 2.62506339e+05),
        (21, 1, 4.25920750e-02, -8.55207399e+04, -2.40477886e+04, 4.87677287e+00, 8.88374532e+04, 8.55360000e+04,  2.40490000e+04, 4.87677287e+00, 8.88524715e+04),
        (22, 1, 2.40695761e-02, -3.18080427e+05, -3.06233114e+04, 1.75703508e+01, 3.19551162e+05, 3.18146000e+05,  3.06290000e+04, 1.75703508e+01, 3.19616979e+05)]],
      dtype={'names': [id, energized, loading, p_from, q_from, i_from, s_from, p_to, q_to, i_to, s_to], 'formats': ['<i4', 'i1', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8', '<f8'], 'offsets': [0, 4, 8, 16, 24, 32, 40, 48, 56, 64, 72], 'itemsize': 80, 'aligned': True})

Create grid from input data example

To create a Grid object from PGM input data, the create_grid_from_input_data() method can be used.

input_data = core_interface.create_input_from_grid()

core_interface = PowerGridModelInterface(input_data=input_data)
output = core_interface.create_grid_from_input_data()

print(input_data["node"])
print(output.node)
[( 1, 10500.) ( 2, 10500.) ( 3, 10500.) ( 4, 10500.) ( 5, 10500.)
 (16, 10500.)]
 id | u_rated | node_type | feeder_branch_id | feeder_node_id 
 1  | 10500.0 |     0     |   -2147483648    |  -2147483648   
 2  | 10500.0 |     0     |   -2147483648    |  -2147483648   
 3  | 10500.0 |     0     |   -2147483648    |  -2147483648   
 4  | 10500.0 |     0     |   -2147483648    |  -2147483648   
 5  | 10500.0 |     0     |   -2147483648    |  -2147483648   
 16 | 10500.0 |     0     |   -2147483648    |  -2147483648