-
Notifications
You must be signed in to change notification settings - Fork 465
/
makeBdc.py
91 lines (71 loc) · 3.5 KB
/
makeBdc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
# Copyright 1996-2015 PSERC. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.
"""Builds the B matrices and phase shift injections for DC power flow.
"""
from numpy import ones, r_, pi, flatnonzero as find, real
from pandapower.pypower.idx_brch import F_BUS, T_BUS, BR_X, TAP, SHIFT, BR_STATUS
from pandapower.pypower.idx_bus import BUS_I
from scipy.sparse import csr_matrix, csc_matrix
try:
import pplog as logging
except ImportError:
import logging
logger = logging.getLogger(__name__)
def makeBdc(bus, branch, return_csr=True):
"""Builds the B matrices and phase shift injections for DC power flow.
Returns the B matrices and phase shift injection vectors needed for a
DC power flow.
The bus real power injections are related to bus voltage angles by::
P = Bbus * Va + PBusinj
The real power flows at the from end the lines are related to the bus
voltage angles by::
Pf = Bf * Va + Pfinj
Does appropriate conversions to p.u.
@see: L{dcpf}
@author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad
Autonoma de Manizales)
@author: Ray Zimmerman (PSERC Cornell)
@author: Richard Lincoln
"""
## Select csc/csr B matrix
sparse = csr_matrix if return_csr else csc_matrix
## constants
nb = bus.shape[0] ## number of buses
nl = branch.shape[0] ## number of lines
## check that bus numbers are equal to indices to bus (one set of bus nums)
if any(bus[:, BUS_I] != list(range(nb))):
logger.error('makeBdc: buses must be numbered consecutively in '
'bus matrix\n')
## for each branch, compute the elements of the branch B matrix and the phase
## shift "quiescent" injections, where
##
## | Pf | | Bff Bft | | Vaf | | Pfinj |
## | | = | | * | | + | |
## | Pt | | Btf Btt | | Vat | | Ptinj |
##
stat = branch[:, BR_STATUS] ## ones at in-service branches
b = stat / branch[:, BR_X] ## series susceptance
tap = ones(nl) ## default tap ratio = 1
i = find(real(branch[:, TAP])) ## indices of non-zero tap ratios
tap[i] = real(branch[i, TAP]) ## assign non-zero tap ratios
b = b / tap
## build connection matrix Cft = Cf - Ct for line and from - to buses
f = real(branch[:, F_BUS]).astype(int) ## list of "from" buses
t = real(branch[:, T_BUS]).astype(int) ## list of "to" buses
i = r_[range(nl), range(nl)] ## double set of row indices
## connection matrix
Cft = sparse((r_[ones(nl), -ones(nl)], (i, r_[f, t])), (nl, nb))
## build Bf such that Bf * Va is the vector of real branch powers injected
## at each branch's "from" bus
Bf = sparse((r_[b, -b], (i, r_[f, t])), (nl, nb))## = spdiags(b, 0, nl, nl) * Cft
## build Bbus
Bbus = Cft.T * Bf
## build phase shift injection vectors
Pfinj = b * (-branch[:, SHIFT] * pi / 180.) ## injected at the from bus ...
# Ptinj = -Pfinj ## and extracted at the to bus
Pbusinj = Cft.T * Pfinj ## Pbusinj = Cf * Pfinj + Ct * Ptinj
return Bbus, Bf, Pbusinj, Pfinj