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.

from typing import ClassVar

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: 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]

Adding the new arrays to the Grid

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

We advise to use repr=False with the dataclass wrapper, to get our grid representation function instead of the default dataclass one.

from dataclasses import dataclass

from power_grid_model_ds import GraphContainer, Grid


@dataclass(repr=False)
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(
graphs=GraphContainer(active_graph=RustworkxGraphModel(nodes=0, branches=0, active_only=True), complete_graph=RustworkxGraphModel(nodes=0, branches=0, active_only=False))
)