1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """Tests for DC optimal power flow using PIPS-sc solver.
18 """
19
20 from os.path import dirname, join
21
22 from numpy import array, ones, Inf, arange, r_
23
24 from scipy.io import loadmat
25 from scipy.sparse import csr_matrix as sparse
26
27 from pypower.ppoption import ppoption
28 from pypower.rundcopf import rundcopf
29 from pypower.loadcase import loadcase
30
31 from pypower.idx_bus import \
32 BUS_AREA, BASE_KV, VMIN, VM, VA, LAM_P, LAM_Q, MU_VMIN, MU_VMAX
33
34 from pypower.idx_gen import \
35 GEN_BUS, QMAX, QMIN, MBASE, APF, PG, QG, VG, MU_PMAX, MU_QMIN
36
37 from pypower.idx_brch import \
38 ANGMAX, PF, QT, MU_SF, MU_ST
39
40 from pypower.t.t_begin import t_begin
41 from pypower.t.t_is import t_is
42 from pypower.t.t_ok import t_ok
43 from pypower.t.t_end import t_end
44
45
47 """Tests for DC optimal power flow using PIPS-sc solver.
48
49 @author: Ray Zimmerman (PSERC Cornell)
50 @author: Richard Lincoln
51 """
52 num_tests = 23
53
54 t_begin(num_tests, quiet)
55
56 tdir = dirname(__file__)
57 casefile = join(tdir, 't_case9_opf')
58 verbose = 0
59
60 t0 = 'DC OPF (PIPS-sc): '
61 ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0, OPF_ALG_DC=250)
62
63
64
65
66 ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)]
67 ib_voltage = arange(VM, VA + 1)
68 ib_lam = arange(LAM_P, LAM_Q + 1)
69 ib_mu = arange(MU_VMAX, MU_VMIN + 1)
70 ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)]
71 ig_disp = array([PG, QG, VG])
72 ig_mu = arange(MU_PMAX, MU_QMIN + 1)
73 ibr_data = arange(ANGMAX + 1)
74 ibr_flow = arange(PF, QT + 1)
75 ibr_mu = array([MU_SF, MU_ST])
76
77
78
79 soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True)
80
81 bus_soln = soln9_dcopf['bus_soln']
82 gen_soln = soln9_dcopf['gen_soln']
83 branch_soln = soln9_dcopf['branch_soln']
84 f_soln = soln9_dcopf['f_soln'][0]
85
86
87 t = t0
88 r = rundcopf(casefile, ppopt)
89 bus, gen, branch, f, success = \
90 r['bus'], r['gen'], r['branch'], r['f'], r['success']
91 t_ok(success, [t, 'success'])
92 t_is(f, f_soln, 3, [t, 'f'])
93 t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data'])
94 t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage'])
95 t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda'])
96 t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu'])
97 t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data'])
98 t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch'])
99 t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu'])
100 t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data'])
101 t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow'])
102 t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu'])
103
104
105
106
107
108
109 ppc = loadcase(casefile)
110 row = [0, 0, 0, 1, 1, 1]
111 col = [9, 10, 12, 10, 11, 13]
112 ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 14))
113 ppc['u'] = array([0, 0])
114 ppc['l'] = array([-Inf, -Inf])
115 ppc['zl'] = array([0, 0])
116
117 ppc['N'] = sparse(([1, 1], ([0, 1], [12, 13])), (2, 14))
118 ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]])
119 ppc['H'] = sparse((2, 2))
120 ppc['Cw'] = array([1000, 1])
121
122 t = ''.join([t0, 'w/extra constraints & costs 1 : '])
123 r = rundcopf(ppc, ppopt)
124 t_ok(r['success'], [t, 'success'])
125 t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974'])
126 t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974'])
127 t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars'])
128 t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs'])
129
130
131 ppc = loadcase(casefile)
132 row = [0, 0, 0, 1, 1, 1]
133 col = [18, 19, 24, 19, 20, 25]
134 ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 26))
135 ppc['u'] = array([0, 0])
136 ppc['l'] = array([-Inf, -Inf])
137 ppc['zl'] = array([0, 0])
138
139 ppc['N'] = sparse(([1, 1], ([0, 1], [24, 25])), (2, 26))
140 ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]])
141 ppc['H'] = sparse((2, 2))
142 ppc['Cw'] = array([1000, 1])
143
144 t = ''.join([t0, 'w/extra constraints & costs 2 : '])
145 r = rundcopf(ppc, ppopt)
146 t_ok(r['success'], [t, 'success'])
147 t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974'])
148 t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974'])
149 t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars'])
150 t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs'])
151
152 t = ''.join([t0, 'infeasible : '])
153
154 ppc = loadcase(casefile)
155 ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14))
156 ppc['u'] = array([Inf])
157 ppc['l'] = array([600])
158 r = rundcopf(ppc, ppopt)
159 t_ok(not r['success'], [t, 'no success'])
160
161 t_end()
162
163
164 if __name__ == '__main__':
165 t_opf_dc_pips_sc(quiet=False)
166