1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Updates bus, gen, branch data structures to match power flow soln.
18 """
19
20 from numpy import asarray, angle, pi, conj, zeros, ones, finfo, c_, ix_
21 from numpy import flatnonzero as find
22
23 from scipy.sparse import csr_matrix
24
25 from idx_bus import VM, VA, PD, QD
26 from idx_gen import GEN_BUS, GEN_STATUS, PG, QG, QMIN, QMAX
27 from idx_brch import F_BUS, T_BUS, BR_STATUS, PF, PT, QF, QT
28
29 EPS = finfo(float).eps
30
31
32 -def pfsoln(baseMVA, bus0, gen0, branch0, Ybus, Yf, Yt, V, ref, pv, pq):
33 """Updates bus, gen, branch data structures to match power flow soln.
34
35 @author: Ray Zimmerman (PSERC Cornell)
36 @author: Richard Lincoln
37 """
38
39 bus = bus0
40 gen = gen0
41 branch = branch0
42
43
44 bus[:, VM] = abs(V)
45 bus[:, VA] = angle(V) * 180 / pi
46
47
48
49 on = find(gen[:, GEN_STATUS] > 0)
50 gbus = gen[on, GEN_BUS].astype(int)
51 refgen = find(gbus == ref)
52
53
54 Sg = V[gbus] * conj(Ybus[gbus, :] * V)
55
56
57 gen[:, QG] = zeros(gen.shape[0])
58 gen[on, QG] = Sg.imag * baseMVA + bus[gbus, QD]
59
60
61
62
63
64 if len(on) > 1:
65
66 nb = bus.shape[0]
67 ngon = on.shape[0]
68 Cg = csr_matrix((ones(ngon), (range(ngon), gbus)), (ngon, nb))
69
70
71 ngg = Cg * Cg.sum(0).T
72 ngg = asarray(ngg).flatten()
73 gen[on, QG] = gen[on, QG] / ngg
74
75
76
77 Cmin = csr_matrix((gen[on, QMIN], (range(ngon), gbus)), (ngon, nb))
78 Cmax = csr_matrix((gen[on, QMAX], (range(ngon), gbus)), (ngon, nb))
79 Qg_tot = Cg.T * gen[on, QG]
80 Qg_min = Cmin.sum(0).T
81 Qg_max = Cmax.sum(0).T
82 Qg_min = asarray(Qg_min).flatten()
83 Qg_max = asarray(Qg_max).flatten()
84
85 ig = find(Cg * Qg_min == Cg * Qg_max)
86 Qg_save = gen[on[ig], QG]
87 gen[on, QG] = gen[on, QMIN] + \
88 (Cg * ((Qg_tot - Qg_min) / (Qg_max - Qg_min + EPS))) * \
89 (gen[on, QMAX] - gen[on, QMIN])
90 gen[on[ig], QG] = Qg_save
91
92
93
94 gen[on[refgen[0]], PG] = Sg[refgen[0]].real * baseMVA + bus[ref, PD]
95 if len(refgen) > 1:
96
97 gen[on[refgen[0]], PG] = \
98 gen[on[refgen[0]], PG] - sum(gen[on[ refgen[1:len(refgen)] ], PG])
99
100
101 out = find(branch[:, BR_STATUS] == 0)
102 br = find(branch[:, BR_STATUS]).astype(int)
103
104
105 Sf = V[ branch[br, F_BUS].astype(int) ] * conj(Yf[br, :] * V) * baseMVA
106
107 St = V[ branch[br, T_BUS].astype(int) ] * conj(Yt[br, :] * V) * baseMVA
108 branch[ ix_(br, [PF, QF, PT, QT]) ] = c_[Sf.real, Sf.imag, St.real, St.imag]
109 branch[ ix_(out, [PF, QF, PT, QT]) ] = zeros((len(out), 4))
110
111 return bus, gen, branch
112