Package pypower :: Package t :: Module t_opf_dc_pips_sc
[hide private]
[frames] | no frames]

Source Code for Module pypower.t.t_opf_dc_pips_sc

  1  # Copyright (C) 2004-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 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   
46 -def t_opf_dc_pips_sc(quiet=False):
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#not quiet 59 60 t0 = 'DC OPF (PIPS-sc): ' 61 ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0, OPF_ALG_DC=250) 62 63 ## run DC OPF 64 65 ## set up indices 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 #ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) 77 78 ## get solved DC power flow case from MAT-file 79 soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True) 80 ## defines bus_soln, gen_soln, branch_soln, f_soln 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 ## run OPF 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 ##----- run OPF with extra linear user constraints & costs ----- 105 ## two new z variables 106 ## 0 <= z1, P2 - P1 <= z1 107 ## 0 <= z2, P2 - P3 <= z2 108 ## with A and N sized for DC opf 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)) ## new z variables only 118 ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z 119 ppc['H'] = sparse((2, 2)) ## no quadratic term 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 ## with A and N sized for AC opf 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)) ## new z variables only 140 ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z 141 ppc['H'] = sparse((2, 2)) ## no quadratic term 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 ## with A and N sized for DC opf 154 ppc = loadcase(casefile) 155 ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14)) ## Pg1 + Pg2 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