44# and Energy System Technology (IEE), Kassel. All rights reserved.
55
66# Builds the DC PSDF matrix based on the DC PTDF
7- import scipy as sp
7+ import logging
88from math import pi
9+
10+ import scipy as sp
911from scipy .sparse import csr_matrix , csc_matrix
12+ import pandas as pd
13+ import numpy as np
14+ import numpy .typing as npt
1015
1116from pandapower .analysis .LODF import _LODF_ppci_to_pp , _LODF_pp_np_to_df
1217from pandapower .analysis .PTDF import _makePTDF_ppci
1318from pandapower .pypower .idx_brch import F_BUS , T_BUS
1419from pandapower .pypower .idx_bus import BUS_TYPE , REF
1520from pandapower .pypower .makeBdc import calc_b_from_branch
1621from numpy import ones , r_ , real , int64 , arange , flatnonzero as find , isscalar
17-
18- from typing import Union , Tuple
19-
20- import pandas as pd
21- import numpy as np
22-
2322from pandapower import pandapowerNet
2423from pandapower .analysis .utils import branch_dict_to_ppci_branch_list , _get_outage_branch_ix , ELE_IX_TYPE
2524
26- import logging
2725logger = logging .getLogger (__name__ )
2826
29-
3027def makePSDF (
3128 baseMVA : float ,
32- PTDF : np . ndarray ,
33- bus : np . ndarray ,
34- branch : np . ndarray ,
29+ PTDF : npt . NDArray ,
30+ bus : npt . NDArray ,
31+ branch : npt . NDArray ,
3532 using_sparse_solver : bool = False ,
3633 branch_id : int | None = None ,
3734 reduced : bool = False ,
38- slack : Union [ int , np . ndarray ] | None = None
35+ slack : int | npt . NDArray | None = None
3936):
40- """Builds the DC PSDF matrix based on the DC PTDF
37+ """
38+ Builds the DC PSDF matrix based on the DC PTDF
39+
4140 Returns the DC PSDF matrix . The matrix is
4241 C{nbr x nbr}, where C{nbr} is the number of branches. The DC PSDF is independent from the selected slack.
4342 To restrict the PSDF computation to a subset of branches, supply a list of ppci branch indices in C{branch_id}.
4443 If C{reduced==True}, the output is reduced to the branches given in C{branch_id}, otherwise the complement rows are set to NaN.
4544 @see: L{makeLODF}
4645 """
47- if reduced and not branch_id :
46+ if reduced and branch_id is None :
4847 raise ValueError ("'reduced=True' is only valid if branch_id is not None" )
4948
5049 ## Select csc/csr B matrix
5150 sparse = csr_matrix if using_sparse_solver else csc_matrix
5251
5352 ## use reference bus for slack by default
5453 if slack is None :
55- slack = find (bus [:, BUS_TYPE ] == REF )
56- slack = slack [0 ]
54+ slack = find (bus [:, BUS_TYPE ] == REF )[0 ]
5755
5856 ## set the slack bus to be used to compute initial PTDF
5957 if isscalar (slack ):
@@ -90,11 +88,11 @@ def makePSDF(
9088
9189def _get_PSDF_direct (
9290 net : pandapowerNet ,
93- phase_shift_branch_type : str ,
91+ phase_shift_branch_type : str | None ,
9492 phase_shift_branch_ix : ELE_IX_TYPE | None = None ,
9593 using_sparse_solver : bool = True ,
9694 random_verify = False ,
97- branch_dict : dict [str , Union [ list [int ], None ] ] | None = None ,
95+ branch_dict : dict [str , list [int ] | None ] | None = None ,
9896 reduced = True ,
9997):
10098 """
@@ -105,8 +103,8 @@ def _get_PSDF_direct(
105103 logger .warning ("Calculating lodf for large network, switched to sparse solver!" )
106104
107105 # If branch_dict not None compute list of ppci branch indices and its branch type intervals as lookup
108- branch_ppci_lookup = None
109- branch_id = None
106+ branch_ppci_lookup : dict | None = None
107+ branch_id : int | None = None
110108 if branch_dict is not None :
111109 branch_id , branch_ppci_lookup = branch_dict_to_ppci_branch_list (net = net , branch_dict = branch_dict )
112110 else :
@@ -159,11 +157,11 @@ def _get_PSDF_direct(
159157
160158def _get_PSDF_perturb (
161159 net : pandapowerNet ,
162- phase_shift_branch_type : str ,
160+ phase_shift_branch_type : str | None ,
163161 phase_shift_branch_ix : ELE_IX_TYPE | None = None ,
164162 distributed_slack = True ,
165- recycle = "lodf" ,
166- ) -> dict [Tuple [str , str ], pd .DataFrame ]:
163+ recycle : str | None = "lodf" ,
164+ ) -> dict [tuple [str , str ], pd .DataFrame ]:
167165 """
168166 this function calculates PSDF (ratio without unit) of branch to
169167 a pp branch with perturb method (brute-force)
@@ -173,15 +171,15 @@ def _get_PSDF_perturb(
173171
174172def run_PSDF (
175173 net : pandapowerNet ,
176- phase_shift_branch_type : Union [ None , str ] ,
174+ phase_shift_branch_type : str | None ,
177175 phase_shift_branch_ix : ELE_IX_TYPE | None = None ,
178176 distributed_slack : bool = True ,
179177 perturb : bool = False ,
180- recycle : Union [ str , None ] = None ,
178+ recycle : str | None = None ,
181179 using_sparse_solver : bool = True ,
182- branch_dict : dict [str , Union [ list [int ], None ] ] | None = None ,
180+ branch_dict : dict [str , list [int ] | None ] | None = None ,
183181 reduced : bool = True ,
184- ) -> dict [Tuple [str , str ], pd .DataFrame ]:
182+ ) -> dict [tuple [str , str ], pd .DataFrame ]:
185183 """
186184 this function is a wrapper of calculating PSDF of a pp branch from the phase shift through a pp branch
187185 with pypower matrix function or perturb function.
@@ -207,10 +205,10 @@ def run_PSDF(
207205 DataFrame(data=psdf, index=goal_branch_pp_index, columns=phase_shift_branch_ix)}
208206 """
209207 # ToDo: check if distributed slack makes any difference
210- if perturb and phase_shift_branch_type is None :
211- logger .info ("If a lot of branch required in psdf, please set perturb to False!" )
212208
213209 if perturb :
210+ if phase_shift_branch_type is None :
211+ logger .info ("If a lot of branch required in psdf, please set perturb to False!" )
214212 if recycle == "lodf" and distributed_slack == True :
215213 logger .warning ("distributed_slack deactivated! recycling does not allow distributed slack" )
216214 psdf = _get_PSDF_perturb (
0 commit comments