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

Source Code for Module pypower.t.t_makePTDF

  1  # Copyright (C) 2006-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 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   
39 -def t_makePTDF(quiet=False):
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#not quiet 51 52 ## load case 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 ## compute injections and flows 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 ## create corresponding slack distribution matrices 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 ## create some PTDF matrices 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 ## matrices get properly transformed by slack dist matrices 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 ## PTDFs can reconstruct flows 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 ## other 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