-
Notifications
You must be signed in to change notification settings - Fork 109
/
bustypes.py
50 lines (39 loc) · 1.76 KB
/
bustypes.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
# Copyright (c) 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.
"""Builds index lists of each type of bus.
"""
from numpy import ones, flatnonzero as find
from numpy import zeros
from scipy.sparse import csr_matrix as sparse
from pypower.idx_bus import BUS_TYPE, REF, PV, PQ
from pypower.idx_gen import GEN_BUS, GEN_STATUS
def bustypes(bus, gen):
"""Builds index lists of each type of bus (C{REF}, C{PV}, C{PQ}).
Generators with "out-of-service" status are treated as L{PQ} buses with
zero generation (regardless of C{Pg}/C{Qg} values in gen). Expects C{bus}
and C{gen} have been converted to use internal consecutive bus numbering.
@param bus: bus data
@param gen: generator data
@return: index lists of each bus type
@author: Ray Zimmerman (PSERC Cornell)
"""
# get generator status
nb = bus.shape[0]
ng = gen.shape[0]
# gen connection matrix, element i, j is 1 if, generator j at bus i is ON
Cg = sparse((gen[:, GEN_STATUS] > 0,
(gen[:, GEN_BUS], range(ng))), (nb, ng))
# number of generators at each bus that are ON
bus_gen_status = (Cg * ones(ng, int)).astype(bool)
# form index lists for slack, PV, and PQ buses
ref = find((bus[:, BUS_TYPE] == REF) & bus_gen_status) # ref bus index
pv = find((bus[:, BUS_TYPE] == PV) & bus_gen_status) # PV bus indices
pq = find((bus[:, BUS_TYPE] == PQ) | ~bus_gen_status) # PQ bus indices
# pick a new reference bus if for some reason there is none (may have been
# shut down)
if len(ref) == 0:
ref = zeros(1, dtype=int)
ref = pv[0] # use the first PV bus
pv = pv[1:] # take it off PV list
return ref, pv, pq