{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick Start\n", "\n", "In this quick start we create an extension of the Grid object, generate a random instance of it and perform power flow calculations and modifications.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting up a grid extension\n", "\n", "This shows how to add extra values to a `Grid` object. If these are present in the PGM output they will be updated after a power flow calculation.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "from typing import ClassVar\n", "\n", "import numpy as np\n", "from numpy.typing import NDArray\n", "\n", "from power_grid_model_ds import GraphContainer, Grid\n", "from power_grid_model_ds.arrays import LineArray, NodeArray\n", "\n", "\n", "class ExtendedNodeArray(NodeArray):\n", " \"\"\"Extends the node array with an output value\"\"\"\n", "\n", " _defaults: ClassVar = {\"u\": 0}\n", "\n", " u: NDArray[np.float64]\n", "\n", "\n", "class ExtendedLineArray(LineArray):\n", " \"\"\"Extends the line array with an output value\"\"\"\n", "\n", " _defaults: ClassVar = {\"i_from\": 0}\n", "\n", " i_from: NDArray[np.float64]\n", "\n", "\n", "@dataclass\n", "class ExtendedGrid(Grid):\n", " \"\"\"\n", " This is my own grid to extend.\n", " \"\"\"\n", "\n", " node: ExtendedNodeArray\n", " line: ExtendedLineArray\n", " graphs: GraphContainer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grid Generation\n", "\n", "The `RadialGridGenerator` can be used to create a randomised grid of the preferred size\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from power_grid_model_ds.generators import RadialGridGenerator\n", "\n", "grid_generator = RadialGridGenerator(grid_class=ExtendedGrid, nr_nodes=5, nr_sources=1, nr_nops=0)\n", "grid = grid_generator.run(seed=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Feeder IDs\n", "\n", "To analyse network structure from the arrays, you can use feeder ids which identify how the network is connected. All structures connected to a substation node through the same feeding branch get the same feeder_branch_id.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid.set_feeder_ids()\n", "print(grid.node)\n", "print(grid.line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Performing power flow calculations\n", "\n", "Using the `PowerGridModelInterface` the `Grid` data can be provided to the calculation engine. Using `update_grid` values can be transferred to the `Grid` object.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from power_grid_model_ds import PowerGridModelInterface\n", "\n", "core_interface = PowerGridModelInterface(grid=grid)\n", "\n", "core_interface.create_input_from_grid()\n", "core_interface.calculate_power_flow()\n", "core_interface.update_grid()\n", "\n", "print(grid.node)\n", "print(grid.line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modifying the Grid\n", "\n", "The Grid object can be changed by adding objects or changing normally open points (NOPs).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_line = ExtendedLineArray(\n", " from_node=[3],\n", " to_node=[1],\n", " r1=[0.6],\n", " x1=[0.2],\n", " i_n=[100],\n", " tan1=[0.0],\n", " from_status=[1],\n", " to_status=[1],\n", " c1=[0.0],\n", ")\n", "\n", "grid.append(new_line)\n", "\n", "old_line = grid.line.get(19)\n", "grid.make_inactive(branch=old_line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will give new outputs of the previous functions\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "core_interface = PowerGridModelInterface(grid=grid)\n", "core_interface.create_input_from_grid()\n", "core_interface.calculate_power_flow()\n", "core_interface.update_grid()\n", "\n", "grid.set_feeder_ids()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(grid.node)\n", "print(grid.line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analyzing Grid structure\n", "\n", "The `Grid` also contains a graph representation which can be used for analyzing structure. Such as finding the shortest path between two nodes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path, length = grid.graphs.active_graph.get_shortest_path(1, 4)\n", "print(f\"Shortest path: {path}, Length: {length}\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }