1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Tests for C{makePTDF}.
18 """
19
20 from os.path import dirname, join
21
22 from numpy import ones, zeros, eye, arange, dot, matrix, flatnonzero as find
23
24 from scipy.sparse import csr_matrix as sparse
25
26 from pypower.ppoption import ppoption
27 from pypower.rundcopf import rundcopf
28 from pypower.ext2int import ext2int1
29 from pypower.makePTDF import makePTDF
30 from pypower.idx_gen import GEN_BUS, PG
31 from pypower.idx_bus import PD
32 from pypower.idx_brch import PF
33
34 from pypower.t.t_begin import t_begin
35 from pypower.t.t_is import t_is
36 from pypower.t.t_end import t_end
37
38
40 """Tests for C{makePTDF}.
41
42 @author: Ray Zimmerman (PSERC Cornell)
43 @author: Richard Lincoln
44 """
45 ntests = 24
46 t_begin(ntests, quiet)
47
48 tdir = dirname(__file__)
49 casefile = join(tdir, 't_case9_opf')
50 verbose = 0
51
52
53 ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0)
54 r = rundcopf(casefile, ppopt)
55 baseMVA, bus, gen, branch = r['baseMVA'], r['bus'], r['gen'], r['branch']
56 _, bus, gen, branch = ext2int1(bus, gen, branch)
57 nb = bus.shape[0]
58 nbr = branch.shape[0]
59 ng = gen.shape[0]
60
61
62 Cg = sparse((ones(ng), (gen[:, GEN_BUS], arange(ng))), (nb, ng))
63 Pg = Cg * gen[:, PG]
64 Pd = bus[:, PD]
65 P = Pg - Pd
66 ig = find(P > 0)
67 il = find(P <= 0)
68 F = branch[:, PF]
69
70
71 e1 = zeros((nb, 1)); e1[0] = 1
72 e4 = zeros((nb, 1)); e4[3] = 1
73 D1 = eye(nb, nb) - dot(e1, ones((1, nb)))
74 D4 = eye(nb, nb) - dot(e4, ones((1, nb)))
75 Deq = eye(nb, nb) - ones((nb, 1)) / nb * ones((1, nb))
76 Dg = eye(nb) - matrix( Pd / sum(Pd) ).T * ones(nb)
77 Dd = eye(nb) - matrix( Pg / sum(Pg) ).T * ones(nb)
78
79
80 H1 = makePTDF(baseMVA, bus, branch, 0)
81 H4 = makePTDF(baseMVA, bus, branch, 3)
82 Heq = makePTDF(baseMVA, bus, branch, ones(nb))
83 Hg = makePTDF(baseMVA, bus, branch, Pd)
84 Hd = makePTDF(baseMVA, bus, branch, Pg)
85
86
87 t_is(H1, dot(H1, D1), 8, 'H1 == H1 * D1')
88 t_is(H4, dot(H1, D4), 8, 'H4 == H1 * D4')
89 t_is(Heq, dot(H1, Deq), 8, 'Heq == H1 * Deq')
90 t_is(Hg, dot(H1, Dg), 8, 'Hg == H1 * Dg')
91 t_is(Hd, dot(H1, Dd), 8, 'Hd == H1 * Dd')
92 t_is(H1, dot(Heq, D1), 8, 'H1 == Heq * D1')
93 t_is(H4, dot(Heq, D4), 8, 'H4 == Heq * D4')
94 t_is(Heq, dot(Heq, Deq), 8, 'Heq == Heq * Deq')
95 t_is(Hg, dot(Heq, Dg), 8, 'Hg == Heq * Dg')
96 t_is(Hd, dot(Heq, Dd), 8, 'Hd == Heq * Dd')
97 t_is(H1, dot(Hg, D1), 8, 'H1 == Hg * D1')
98 t_is(H4, dot(Hg, D4), 8, 'H4 == Hg * D4')
99 t_is(Heq, dot(Hg, Deq), 8, 'Heq == Hg * Deq')
100 t_is(Hg, dot(Hg, Dg), 8, 'Hg == Hg * Dg')
101 t_is(Hd, dot(Hg, Dd), 8, 'Hd == Hg * Dd')
102
103
104 t_is(F, dot(H1, P), 3, 'Flow == H1 * P')
105 t_is(F, dot(H4, P), 3, 'Flow == H4 * P')
106 t_is(F, dot(Heq, P), 3, 'Flow == Heq * P')
107 t_is(F, dot(Hg, P), 3, 'Flow == Hg * P')
108 t_is(F, dot(Hd, P), 3, 'Flow == Hd * P')
109
110
111 t_is(F, dot(Hg, Pg), 3, 'Flow == Hg * Pg')
112 t_is(F, dot(Hd, (-Pd)), 3, 'Flow == Hd * (-Pd)')
113 t_is(zeros(nbr), dot(Hg, (-Pd)), 3, 'zeros == Hg * (-Pd)')
114 t_is(zeros(nbr), dot(Hd, Pg), 3, 'zeros == Hd * Pg')
115
116 t_end()
117
118
119 if __name__ == '__main__':
120 t_makePTDF(quiet=False)
121