{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Internal Datastructure: Bus branch model, Admittance and Jacobian Matrix\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This jupyter notebooks explains how to access and interpret the internal datastructure with relevant matrices."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Internal Datastructure\n",
"\n",
"We use the simple example network from the create_simple tutorial as an example for how to access internal calculation parameters:\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:35.863414Z",
"start_time": "2024-03-30T10:51:31.939777Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LightSimBackend import error: No module named 'grid2op'\n",
"PhysicalLawChecker import error: No module named 'grid2op'\n",
"TimeSerie import error: cannot import name 'TimeSerie' from 'lightsim2grid.timeSerie' (C:\\ProgramData\\miniconda3\\envs\\py3.11\\Lib\\site-packages\\lightsim2grid\\timeSerie.py)\n",
"ContingencyAnalysis import error: cannot import name 'ContingencyAnalysis' from 'lightsim2grid.contingencyAnalysis' (C:\\ProgramData\\miniconda3\\envs\\py3.11\\Lib\\site-packages\\lightsim2grid\\contingencyAnalysis.py)\n",
"rewards import error: No module named 'grid2op'\n",
"This pandapower network includes the following parameter tables:\n",
" - bus (7 elements)\n",
" - load (1 element)\n",
" - sgen (1 element)\n",
" - gen (1 element)\n",
" - switch (8 elements)\n",
" - shunt (1 element)\n",
" - ext_grid (1 element)\n",
" - line (4 elements)\n",
" - trafo (1 element)\n"
]
}
],
"source": [
"import pandapower as pp\n",
"import pandapower.networks as nw\n",
"\n",
"net = nw.example_simple()\n",
"print(net)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we run a power flow in this network:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.154189Z",
"start_time": "2024-03-30T10:51:35.865424Z"
}
},
"outputs": [],
"source": [
"pp.runpp(net)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When a power flow is carried out, the element based grid model is translated into a bus-branch model. That bus-branch model is stored in a data structure that is based on the PYPOWER/MATPOWER casefile (with some extensions). This ppc can be accesed after power flow:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false,
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.172601Z",
"start_time": "2024-03-30T10:51:40.155198Z"
}
},
"outputs": [
{
"data": {
"text/plain": "{'baseMVA': 1,\n 'version': 2,\n 'bus': array([[ 0. , 3. , 0. , 0. ,\n 0. , 0. , 1. , 1.02 ,\n 50. , 110. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 1. , 1. , 0. , 0. ,\n 0. , 0.96 , 1. , 1.02082951,\n 50.03241357, 110. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 2. , 1. , 0. , 0. ,\n 0. , 0. , 1. , 1.02456241,\n -98.19715207, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 3. , 2. , 0. , 0. ,\n 0. , 0. , 1. , 1.03 ,\n -98.12954543, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 4. , 1. , -0.8 , 2.9 ,\n 0. , 0. , 1. , 1.02320534,\n -98.04777562, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 5. , 1. , 0. , 0. ,\n 0. , 0. , 1. , 1.030007 ,\n -98.13016747, 20. , 1. , 1.1 ,\n 0.9 , 0. , 0. , 0. ],\n [ 6. , 4. , 0. , 0. ,\n 0. , 0. , 1. , nan,\n nan, 110. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 7. , 4. , 0. , 0. ,\n 0. , 0. , 1. , nan,\n nan, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ]]),\n 'branch': array([[ 0.00000000e+00+0.j , 1.00000000e+00+0.j ,\n 4.95867769e-05+0.j , 1.19008264e-04+0.j ,\n 5.47391104e+00+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -6.74111530e+00+0.j ,\n -7.14688297e+00+0.j , 6.74416214e+00+0.j ,\n 1.45450502e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 2.00000000e+00+0.j , 3.00000000e+00+0.j ,\n 6.10000000e-04+0.j , 5.60000000e-04+0.j ,\n 7.64035333e-02+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -5.97238973e+00+0.j ,\n -3.48162502e+00+0.j , 5.99999998e+00+0.j ,\n 3.42634241e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 3.00000000e+00+0.j , 5.00000000e+00+0.j ,\n 5.19662500e-03+0.j , 3.25500000e-03+0.j ,\n 4.17831823e-03+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , 2.40627158e-08+0.j ,\n -4.43279288e-03+0.j , 0.00000000e+00+0.j ,\n -0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 4.00000000e+00+0.j , 2.00000000e+00+0.j ,\n 7.62500000e-04+0.j , 7.00000000e-04+0.j ,\n 9.55044167e-02+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , 8.00000000e-01+0.j ,\n -2.90000000e+00+0.j , -7.93618189e-01+0.j ,\n 2.80573774e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 1.00000000e+00+0.j , 2.00000000e+00+0.j ,\n 1.63923679e-04+0.j , 4.79726336e-03+0.j ,\n -1.05000908e-02-0.01399964j, 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 1.50000000e+02+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -6.74416214e+00+0.j ,\n -4.54095852e-01+0.j , 6.76600792e+00+0.j ,\n 6.75887284e-01+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ]]),\n 'tcsc': array([], shape=(0, 17), dtype=float64),\n 'svc': array([], shape=(0, 11), dtype=float64),\n 'ssc': array([], shape=(0, 10), dtype=float64),\n 'gen': array([[ 0.00000000e+00, -6.74111530e+00, -7.14688297e+00,\n 0.00000000e+00, 0.00000000e+00, 1.02000000e+00,\n 1.00000000e+00, 1.00000000e+00, 1.00000000e+09,\n -1.00000000e+09, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 1.00000000e+00],\n [ 3.00000000e+00, 6.00000000e+00, 3.42190962e+00,\n 3.00000000e+00, -3.00000000e+00, 1.03000000e+00,\n nan, 1.00000000e+00, 1.00000000e+09,\n -1.00000000e+09, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00]]),\n 'internal': {'Ybus': <6x6 sparse matrix of type ''\n \twith 16 stored elements in Compressed Sparse Row format>,\n 'Yf': <5x6 sparse matrix of type ''\n \twith 10 stored elements in Compressed Sparse Row format>,\n 'Yt': <5x6 sparse matrix of type ''\n \twith 10 stored elements in Compressed Sparse Row format>,\n 'branch_is': array([ True, True, True, True, True]),\n 'gen_is': array([ True, True]),\n 'DLF': array([], dtype=complex128),\n 'buses_ord_bfs_nets': array([], dtype=float64),\n 'svc_is': array([], dtype=bool),\n 'ssc_is': array([], dtype=bool),\n 'tcsc_is': array([], dtype=bool),\n 'ref_gens': array([0], dtype=int64),\n 'baseMVA': 1,\n 'bus': array([[ 0. , 3. , 0. , 0. ,\n 0. , 0. , 1. , 1.02 ,\n 50. , 110. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 1. , 1. , 0. , 0. ,\n 0. , 0.96 , 1. , 1.02082951,\n 50.03241357, 110. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 2. , 1. , 0. , 0. ,\n 0. , 0. , 1. , 1.02456241,\n -98.19715207, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 3. , 2. , 0. , 0. ,\n 0. , 0. , 1. , 1.03 ,\n -98.12954543, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 4. , 1. , -0.8 , 2.9 ,\n 0. , 0. , 1. , 1.02320534,\n -98.04777562, 20. , 1. , 2. ,\n 0. , 0. , 0. , 0. ],\n [ 5. , 1. , 0. , 0. ,\n 0. , 0. , 1. , 1.030007 ,\n -98.13016747, 20. , 1. , 1.1 ,\n 0.9 , 0. , 0. , 0. ]]),\n 'gen': array([[ 0.00000000e+00, -6.74111530e+00, -7.14688297e+00,\n 0.00000000e+00, 0.00000000e+00, 1.02000000e+00,\n 1.00000000e+00, 1.00000000e+00, 1.00000000e+09,\n -1.00000000e+09, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 1.00000000e+00],\n [ 3.00000000e+00, 6.00000000e+00, 3.42190962e+00,\n 3.00000000e+00, -3.00000000e+00, 1.03000000e+00,\n nan, 1.00000000e+00, 1.00000000e+09,\n -1.00000000e+09, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00]]),\n 'branch': array([[ 0.00000000e+00+0.j , 1.00000000e+00+0.j ,\n 4.95867769e-05+0.j , 1.19008264e-04+0.j ,\n 5.47391104e+00+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -6.74111530e+00+0.j ,\n -7.14688297e+00+0.j , 6.74416214e+00+0.j ,\n 1.45450502e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 2.00000000e+00+0.j , 3.00000000e+00+0.j ,\n 6.10000000e-04+0.j , 5.60000000e-04+0.j ,\n 7.64035333e-02+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -5.97238973e+00+0.j ,\n -3.48162502e+00+0.j , 5.99999998e+00+0.j ,\n 3.42634241e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 3.00000000e+00+0.j , 5.00000000e+00+0.j ,\n 5.19662500e-03+0.j , 3.25500000e-03+0.j ,\n 4.17831823e-03+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , 2.40627158e-08+0.j ,\n -4.43279288e-03+0.j , 0.00000000e+00+0.j ,\n -0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 4.00000000e+00+0.j , 2.00000000e+00+0.j ,\n 7.62500000e-04+0.j , 7.00000000e-04+0.j ,\n 9.55044167e-02+0.j , 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , 8.00000000e-01+0.j ,\n -2.90000000e+00+0.j , -7.93618189e-01+0.j ,\n 2.80573774e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ],\n [ 1.00000000e+00+0.j , 2.00000000e+00+0.j ,\n 1.63923679e-04+0.j , 4.79726336e-03+0.j ,\n -1.05000908e-02-0.01399964j, 0.00000000e+00+0.j ,\n 2.50000000e+02+0.j , 2.50000000e+02+0.j ,\n 1.00000000e+00+0.j , 1.50000000e+02+0.j ,\n 1.00000000e+00+0.j , -3.60000000e+02+0.j ,\n 3.60000000e+02+0.j , -6.74416214e+00+0.j ,\n -4.54095852e-01+0.j , 6.76600792e+00+0.j ,\n 6.75887284e-01+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j , 0.00000000e+00+0.j ,\n 0.00000000e+00+0.j ]]),\n 'ref': array([0], dtype=int64),\n 'pv': array([3], dtype=int64),\n 'pq': array([1, 2, 4, 5], dtype=int64),\n 'Bbus': <6x6 sparse matrix of type ''\n \twith 16 stored elements in Compressed Sparse Column format>,\n 'Bf': <5x6 sparse matrix of type ''\n \twith 10 stored elements in Compressed Sparse Row format>,\n 'Pbusinj': array([ 0. +0.j, -545.726528+0.j, 545.726528+0.j, 0. +0.j,\n 0. +0.j, 0. +0.j]),\n 'Pfinj': array([ 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j,\n -545.726528+0.j]),\n 'Cft': <5x6 sparse matrix of type ''\n \twith 10 stored elements in Compressed Sparse Row format>,\n 'shift': array([ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 150.+0.j]),\n 'V': array([ 0.65564336+0.78136533j, 0.65573406+0.78237186j,\n -0.14608184-1.01409479j, -0.14565409-1.01964939j,\n -0.1432475 -1.01312848j, -0.14566615-1.01965475j]),\n 'J': <9x9 sparse matrix of type ''\n \twith 41 stored elements in Compressed Sparse Column format>,\n 'Vm_it': None,\n 'Va_it': None,\n 'svc': array([], shape=(0, 11), dtype=float64),\n 'tcsc': array([], shape=(0, 17), dtype=float64),\n 'ssc': array([], shape=(0, 10), dtype=float64),\n 'Sbus': array([-6.8+0.j , 0. +0.j , 0. +0.j , 6. +0.j , 0.8-2.9j, 0. +0.j ]),\n 'r_theta_kelvin_per_mw': None,\n 'T': None},\n 'success': True,\n 'et': 2.052844199934043,\n 'iterations': 3}"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"net._ppc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For information on how this datastructure is defined, please refer to the MATPOWER documentation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Note:** For linear power flow (DC load flow) 'Ybus' is no longer created, but 'Bbus' as a new 'internal' key."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.193610Z",
"start_time": "2024-03-30T10:51:40.174610Z"
}
},
"outputs": [
{
"data": {
"text/plain": "array([[ 8402.77777778+0.j, -8402.77777778+0.j, 0. +0.j,\n 0. +0.j, 0. +0.j, 0. +0.j],\n [-8402.77777778+0.j, 8611.22995659+0.j, -208.45217882+0.j,\n 0. +0.j, 0. +0.j, 0. +0.j],\n [ 0. +0.j, -208.45217882+0.j, 3422.7378931 +0.j,\n -1785.71428571+0.j, -1428.57142857+0.j, 0. +0.j],\n [ 0. +0.j, 0. +0.j, -1785.71428571+0.j,\n 2092.93394777+0.j, 0. +0.j, -307.21966206+0.j],\n [ 0. +0.j, 0. +0.j, -1428.57142857+0.j,\n 0. +0.j, 1428.57142857+0.j, 0. +0.j],\n [ 0. +0.j, 0. +0.j, 0. +0.j,\n -307.21966206+0.j, 0. +0.j, 307.21966206+0.j]])"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pp.rundcpp(net)\n",
"net._ppc['internal']['Bbus'].A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nodal Point Admittance Matrix\n",
"\n",
"The nodal point admittance matrix is saved in the ppc and can be accessed directly:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.224863Z",
"start_time": "2024-03-30T10:51:40.194632Z"
}
},
"outputs": [
{
"data": {
"text/plain": "matrix([[ 2983.234714 -7157.02635809j, -2983.234714 +7159.76331361j,\n 0. +0.j , 0. +0.j ,\n 0. +0.j , 0. +0.j ],\n [-2983.234714 +7159.76331361j, 2990.35626947-7364.28068082j,\n -97.94315042 -183.87162407j, 0. +0.j ,\n 0. +0.j , 0. +0.j ],\n [ 0. +0.j , 110.26592227 -176.75706842j,\n 1608.40491554-1678.15899439j, -889.60186671 +816.68368091j,\n -711.68149336 +653.34694473j, 0. +0.j ],\n [ 0. +0.j , 0. +0.j ,\n -889.60186671 +816.68368091j, 1027.81021222 -903.21268537j,\n 0. +0.j , -138.20834551 +86.56929539j],\n [ 0. +0.j , 0. +0.j ,\n -711.68149336 +653.34694473j, 0. +0.j ,\n 711.68149336 -653.29919252j, 0. +0.j ],\n [ 0. +0.j , 0. +0.j ,\n 0. +0.j , -138.20834551 +86.56929539j,\n 0. +0.j , 138.20834551 -86.56720623j]])"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pp.runpp(net)\n",
"net._ppc[\"internal\"][\"Ybus\"].todense()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the nodal point admittance matrix is given in per unit values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jacobian Matrix\n",
"\n",
"The jacobian Matrix J in the last iteration step is also stored in the ppc and can be accessed:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.232737Z",
"start_time": "2024-03-30T10:51:40.225872Z"
}
},
"outputs": [
{
"data": {
"text/plain": "matrix([[ 9.54796429e+02, 0.00000000e+00, -8.62952847e+02,\n 0.00000000e+00, -9.18435819e+01, 0.00000000e+00,\n -9.15296722e+02, 0.00000000e+00, -1.42353628e+02],\n [ 0.00000000e+00, 7.67426446e+03, -2.17432759e+02,\n 0.00000000e+00, 0.00000000e+00, 3.05264391e+03,\n -1.38258877e+01, 0.00000000e+00, 0.00000000e+00],\n [-8.60737366e+02, -2.17892547e+02, 1.76161072e+03,\n -6.82980810e+02, 0.00000000e+00, -6.95207957e-01,\n 1.64791122e+03, -7.30904816e+02, 0.00000000e+00],\n [ 0.00000000e+00, 0.00000000e+00, -6.86871045e+02,\n 6.86871045e+02, 0.00000000e+00, 0.00000000e+00,\n -7.26450957e+02, 7.28978161e+02, 0.00000000e+00],\n [-9.18403982e+01, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, 9.18403982e+01, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 1.42355564e+02],\n [ 0.00000000e+00, -3.11622898e+03, 1.41654849e+01,\n 0.00000000e+00, 0.00000000e+00, 7.51767501e+03,\n -2.12220120e+02, 0.00000000e+00, 0.00000000e+00],\n [ 9.39812505e+02, 7.09688796e-01, -1.68838790e+03,\n 7.47865710e+02, 0.00000000e+00, -2.13446561e+02,\n 1.71937863e+03, -6.67491444e+02, 0.00000000e+00],\n [ 0.00000000e+00, 0.00000000e+00, 7.44294346e+02,\n -7.44294346e+02, 0.00000000e+00, 0.00000000e+00,\n -6.70404297e+02, 6.65624991e+02, 0.00000000e+00],\n [ 1.46627228e+02, 0.00000000e+00, 0.00000000e+00,\n 0.00000000e+00, -1.46627228e+02, 0.00000000e+00,\n 0.00000000e+00, 0.00000000e+00, 8.91648289e+01]])"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"net._ppc[\"internal\"][\"J\"].todense()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The jacobian matrix is also given in per unit values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mapping the Buses\n",
"\n",
"The pandapower indices are not equal to the ppc indices for several reasons. Some buses are fused together in case of closed bus-bus switches and auxiliary buses are created for elements like extended wards or three winding transformers.\n",
"See here for more details: https://pandapower.readthedocs.io/en/latest/elements/switch.html\n",
"\n",
"There is however a mapping between pandapower indices and ppc indices that is created during the conversion to keep track of the dependencies that is also stored in the net:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.241491Z",
"start_time": "2024-03-30T10:51:40.234750Z"
}
},
"outputs": [
{
"data": {
"text/plain": "array([0, 1, 1, 2, 2, 3, 4], dtype=int64)"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"net._pd2ppc_lookups[\"bus\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get a ppc index from the pandapower index, simply call the lookup like this:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.249297Z",
"start_time": "2024-03-30T10:51:40.242504Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"pandapower_bus_idx = 3\n",
"ppc_index = net._pd2ppc_lookups[\"bus\"][pandapower_bus_idx]\n",
"print(ppc_index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, pandapower bus index 3 corresponds to ppc bus index 2. So if we would like to find the diagonal entry of the Ybus matrix for bus 2, we could now access it with that internal index:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.258281Z",
"start_time": "2024-03-30T10:51:40.251310Z"
}
},
"outputs": [
{
"data": {
"text/plain": "(2990.356269470474-7364.280680821408j)"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ybus = net._ppc[\"internal\"][\"Ybus\"]\n",
"int_idx = net._pd2ppc_lookups[\"bus\"][ppc_index]\n",
"Ybus[int_idx, int_idx]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also see that some buses are mapped to the same internal bus, such as bus 1 and bus 2:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.267788Z",
"start_time": "2024-03-30T10:51:40.261350Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"1\n"
]
}
],
"source": [
"print(net._pd2ppc_lookups[\"bus\"][1])\n",
"print(net._pd2ppc_lookups[\"bus\"][2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That is because buses 1 and 2 are connected by a closed bus-bus switch and are therefore represented internally as the same bus:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.276542Z",
"start_time": "2024-03-30T10:51:40.268796Z"
}
},
"outputs": [
{
"data": {
"text/plain": "bus 1\nelement 2\net b\ntype CB\nclosed True\nname None\nz_ohm 0.0\nin_ka NaN\nName: 0, dtype: object"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"net.switch.loc[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Obtaining Jacobian Entries of Generators\n",
"\n",
"As an example we show how to obtain the Jacobian entries of generator buses using the pandapower -> ppc bus mapping. First we get buses of the in-service generators:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.284228Z",
"start_time": "2024-03-30T10:51:40.277557Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pandapower gen bus: [5]\n"
]
}
],
"source": [
"gen_buses = net.gen.loc[net.gen.in_service.values, \"bus\"].values\n",
"print(f\"pandapower gen bus: {gen_buses}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Second, we geht the Jacobian matrix:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.291248Z",
"start_time": "2024-03-30T10:51:40.285169Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jacobian shape: (9, 9)\n"
]
}
],
"source": [
"J = net._ppc[\"internal\"][\"J\"]\n",
"print(f\"Jacobian shape: {J.shape}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why has the Jacobian the shape 9x9?\n",
"It consists of the partial derivatives J11 = dP_dVa, J12 = dP_Vm, J21 = dQ_dVa, J22 = dQ_dVm. Except the reference bus, all PV- and PQ-buses are included in J. Vm is constant for PV nodes and dS/dVm is 0 for PV-buses (gens in pandapower) and Q is a variable. \n",
"\n",
"In our case we have 1 reference bus (at bus 0), 1 gen at bus 5, and 3 pq buses (at buses 1, 2, 4)\n",
"\n",
"This is the reason why J11 to J22 have these shapes:\n",
"\n",
"J11 = pvpq x pvpq (dP_dVa)\n",
"\n",
"J12 = pvpq x pq (dP_dVm)\n",
"\n",
"J21 = pq x pvpq (dQ_dVa)\n",
"\n",
"J22 = pq x pq (dQ_dVm)\n",
"\n",
"Only J11 contains values relevant for gens."
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"Now we get the \"internal\" ppc buses with the lookup:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.300214Z",
"start_time": "2024-03-30T10:51:40.292195Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pandapower to ppc lookup: [0 1 1 2 2 3 4]\n",
"pandapower gen bus: [5] maps to ppc gen bus: [3]\n"
]
}
],
"source": [
"bus_lookup = ppc_index = net._pd2ppc_lookups[\"bus\"]\n",
"print(f\"pandapower to ppc lookup: {bus_lookup}\")\n",
"ppc_gen_buses = bus_lookup[gen_buses]\n",
"print(f\"pandapower gen bus: {gen_buses} maps to ppc gen bus: {ppc_gen_buses}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we need the pv and pq entries in J to obtain the Jacobian sub-matrices:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.320713Z",
"start_time": "2024-03-30T10:51:40.302226Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pv and pq nodes as in the newtonpf() function\n",
"pv buses: [3]\n",
"pq buses: [1 2 4 5]\n",
"pvpq buses: [3 1 2 4 5]\n",
"shape of J sub-matrices:\n",
"j11 = (5, 5)\n",
"j12 = (5, 4)\n",
"j21 = (4, 5)\n",
"j22 = (4, 4)\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# get pv and pq values from newtonpf()\n",
"pv = net._ppc[\"internal\"][\"pv\"]\n",
"pq = net._ppc[\"internal\"][\"pq\"]\n",
"# stack these as done in newtonpf()\n",
"pvpq = np.hstack((pv, pq))\n",
"\n",
"print(\"pv and pq nodes as in the newtonpf() function\")\n",
"print(f\"pv buses: {pv}\\npq buses: {pq}\\npvpq buses: {pvpq}\")\n",
"\n",
"# get len of pv and pq\n",
"n_pvpq = len(pvpq)\n",
"n_pq = len(pq)\n",
"n_pv = len(pv)\n",
"# get J11, J12, J21, and J22\n",
"j11 = J[:n_pvpq, :n_pvpq]\n",
"j12 = J[:n_pvpq, n_pvpq:]\n",
"j21 = J[n_pvpq:, :n_pvpq]\n",
"j22 = J[n_pvpq:, n_pvpq:]\n",
"\n",
"print(\"shape of J sub-matrices:\")\n",
"print(f\"j11 = {j11.shape}\")\n",
"print(f\"j12 = {j12.shape}\")\n",
"print(f\"j21 = {j21.shape}\")\n",
"print(f\"j22 = {j22.shape}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we finally get the generator entries in J:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.332920Z",
"start_time": "2024-03-30T10:51:40.322679Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"J11 indices: m = [ True False False False False], n = [ True False False False False]\n",
"pandapower gen [5] entries (ppc PV nodes [3]) in J11 (=dP/dVa): [[954.79642892]]\n"
]
}
],
"source": [
"# j11 gen entries\n",
"m = np.isin(pvpq, pv)\n",
"n = m\n",
"\n",
"j11_gen_entries = j11[m, n]\n",
"print(f\"J11 indices: m = {m}, n = {n}\")\n",
"print(f\"pandapower gen {gen_buses} entries (ppc PV nodes {ppc_gen_buses}) in J11 (=dP/dVa): {j11_gen_entries}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visiualizing the Matrices\n",
"You can easily visualize the matrices with matplotlib and the plot function spy(). Let's look at an example.\n",
"\n",
"First, we define a plot function to visiualize the sparse Ybus matrix (works also with any other matrix like J) including some labels:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.342410Z",
"start_time": "2024-03-30T10:51:40.334935Z"
}
},
"outputs": [],
"source": [
"import matplotlib.pyplot as mpl\n",
"import numpy as np\n",
"\n",
"import pandapower as pp\n",
"import pandapower.networks as nw\n",
"import pandapower.plotting as plt\n",
"from pandapower.plotting import get_collection_sizes\n",
"\n",
"def plot_ybus(net=None, ax=None, ybus=None):\n",
" if ax is None:\n",
" fig, ax = mpl.subplots(1, 2)\n",
" if ybus is None:\n",
" ybus = net._ppc[\"internal\"][\"Ybus\"]\n",
" ax.spy(ybus)\n",
" ax.set_title(\"Ybus shape {}\\n\".format(str(ybus.shape)))\n",
" ax.set_xticks(np.arange(ybus.shape[0]))\n",
" ax.set_xticklabels(np.arange(ybus.shape[0]))\n",
" ax.set_yticklabels(np.arange(ybus.shape[1]))\n",
" ax.set_yticks(np.arange(ybus.shape[1]))\n",
"\n",
" ax.grid(which=\"both\", linestyle=\"dotted\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Second, we define a function to plot the power system including some bus labels and other collections:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.352770Z",
"start_time": "2024-03-30T10:51:40.344422Z"
}
},
"outputs": [],
"source": [
"def plot_net(net, ax=None):\n",
" import geojson\n",
" if ax is None:\n",
" fig, ax = mpl.subplots(1, 1, figsize=(10, 8))\n",
"\n",
" sizes = get_collection_sizes(net)\n",
"\n",
" # create collections for elements\n",
" collections = list()\n",
" collections.append(plt.create_bus_collection(net, size=sizes[\"bus\"]))\n",
" collections.append(plt.create_line_collection(net, use_bus_geodata=True))\n",
" collections.append(plt.create_trafo_collection(net, size=sizes[\"trafo\"]))\n",
" collections.append(plt.create_ext_grid_collection(net, size=sizes[\"ext_grid\"], orientation=1.5))\n",
" collections.append(plt.create_bus_bus_switch_collection(net, size=sizes[\"switch\"]))\n",
" collections.append(\n",
" plt.create_line_switch_collection(net, distance_to_bus=sizes[\"switch_distance\"], size=sizes[\"switch\"]))\n",
" collections.append(plt.create_load_collection(net, size=sizes[\"load\"]))\n",
"\n",
" # add labels for each bus\n",
" for idx in net.bus.geo.index:\n",
" x, y = geojson.loads(net.bus.loc[idx, 'geo']).coordinates \n",
" y += sizes[\"bus\"] * 1.\n",
" ax.text(x, y, str(idx), fontsize=12, color=\"r\")\n",
"\n",
" plt.draw_collections(collections, ax=ax)\n",
" mpl.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Third, we create a plot function with three subplots. The first subplots shows the power system including the bus labels. The second and third plot shows the Ybus matrix with different settings. If you just call runpp() the buses are fused internally and the Ybus matrix has a lower dimension. You can also use the parameter `r_switch` to set impedance values for switches. In this case, the buses are not fused and the Ybus matrix has a higher dimension. This also changes your power flow result."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:40.360146Z",
"start_time": "2024-03-30T10:51:40.353781Z"
}
},
"outputs": [],
"source": [
"def plot_overview(net):\n",
" fig, axes = mpl.subplots(1, 3, figsize=(16, 8))\n",
" pp.runpp(net)\n",
" plot_net(net, ax=axes[0])\n",
" # plot y bus with max. const shape (no fused switches)\n",
" pp.runpp(net, check_connectivity=False, r_switch=0.1, init=\"flat\", neglect_open_switch_branches=True)\n",
" plot_ybus(net, ax=axes[1])\n",
" # plot ybus for power flow (with fused switches)\n",
" pp.runpp(net)\n",
" plot_ybus(net, ax=axes[2])\n",
" mpl.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we call the overview function with the cigre example power system:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-30T10:51:41.739192Z",
"start_time": "2024-03-30T10:51:40.361157Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\mvogt\\AppData\\Local\\Temp\\ipykernel_17836\\3533381797.py:18: UserWarning: FixedFormatter should only be used together with FixedLocator\n",
" ax.set_yticklabels(np.arange(ybus.shape[1]))\n",
"C:\\Users\\mvogt\\AppData\\Local\\Temp\\ipykernel_17836\\3533381797.py:18: UserWarning: FixedFormatter should only be used together with FixedLocator\n",
" ax.set_yticklabels(np.arange(ybus.shape[1]))\n"
]
},
{
"data": {
"text/plain": "