Grid extension Examples

You can extend the grid by inheriting from the power-grid-model-ds. In this way you can profit from existing functionality but also add your own. Here we show how to extend the Grid with output variables in the node and line arrays.

Adding columns to the Grid arrays

Output variables are not present in the basic Grid as defined in pgm-ds, since these might or might not be useful to specific projects. To add these as you please you can extend the definitions of the arrays in your own project.

import numpy as np
from numpy.typing import NDArray

from power_grid_model_ds.arrays import LineArray, NodeArray


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

    _defaults = {"u": 0}

    u: NDArray[np.float64]


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

    _defaults = {"i_from": 0}

    i_from: NDArray[np.float64]

Adding the new arrays to the Grid

When these arrays have been defined they should also be provided to the Grid object.

from dataclasses import dataclass

from power_grid_model_ds import GraphContainer, Grid


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

    node: ExtendedNodeArray
    line: ExtendedLineArray
    graphs: GraphContainer

Create a grid with this array container

ExtendedGrid.empty()
ExtendedGrid(_id_counter=0, graphs=GraphContainer(active_graph=RustworkxGraphModel(nodes=0, branches=0, active_only=True), complete_graph=RustworkxGraphModel(nodes=0, branches=0, active_only=False)), node=ExtendedNodeArray([[]]), transformer=TransformerArray([[]]), three_winding_transformer=ThreeWindingTransformerArray([[]]), line=ExtendedLineArray([[]]), link=LinkArray([[]]), generic_branch=GenericBranchArray([[]]), asym_line=AsymLineArray([[]]), source=SourceArray([[]]), sym_load=SymLoadArray([[]]), sym_gen=SymGenArray([[]]), asym_load=AsymLoadArray([[]]), asym_gen=AsymGenArray([[]]), shunt=ShuntArray([[]]), transformer_tap_regulator=TransformerTapRegulatorArray([[]]), voltage_regulator=VoltageRegulatorArray([[]]), sym_power_sensor=SymPowerSensorArray([[]]), sym_voltage_sensor=SymVoltageSensorArray([[]]), sym_current_sensor=SymCurrentSensorArray([[]]), asym_power_sensor=AsymPowerSensorArray([[]]), asym_voltage_sensor=AsymVoltageSensorArray([[]]), asym_current_sensor=AsymCurrentSensorArray([[]]), fault=FaultArray([[]]))