Package pypower :: Module opf_args
[hide private]
[frames] | no frames]

Source Code for Module pypower.opf_args

  1  # Copyright (C) 1996-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  """Parses and initializes OPF input arguments. 
 18  """ 
 19   
 20  from sys import stderr 
 21   
 22  from numpy import array 
 23  from scipy.sparse import issparse 
 24   
 25  from ppoption import ppoption 
 26  from loadcase import loadcase 
 27   
 28   
29 -def opf_args(*args):
30 """Parses and initializes OPF input arguments. 31 32 Returns the full set of initialized OPF input arguments, filling in 33 default values for missing arguments. See Examples below for the 34 possible calling syntax options. 35 36 Input arguments options:: 37 38 opf_args(ppc) 39 opf_args(ppc, ppopt) 40 opf_args(ppc, userfcn, ppopt) 41 opf_args(ppc, A, l, u) 42 opf_args(ppc, A, l, u, ppopt) 43 opf_args(ppc, A, l, u, ppopt, N, fparm, H, Cw) 44 opf_args(ppc, A, l, u, ppopt, N, fparm, H, Cw, z0, zl, zu) 45 46 opf_args(baseMVA, bus, gen, branch, areas, gencost) 47 opf_args(baseMVA, bus, gen, branch, areas, gencost, ppopt) 48 opf_args(baseMVA, bus, gen, branch, areas, gencost, userfcn, ppopt) 49 opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u) 50 opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ppopt) 51 opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ... 52 ppopt, N, fparm, H, Cw) 53 opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ... 54 ppopt, N, fparm, H, Cw, z0, zl, zu) 55 56 The data for the problem can be specified in one of three ways: 57 1. a string (ppc) containing the file name of a PYPOWER case 58 which defines the data matrices baseMVA, bus, gen, branch, and 59 gencost (areas is not used at all, it is only included for 60 backward compatibility of the API). 61 2. a dict (ppc) containing the data matrices as fields. 62 3. the individual data matrices themselves. 63 64 The optional user parameters for user constraints (C{A, l, u}), user costs 65 (C{N, fparm, H, Cw}), user variable initializer (z0), and user variable 66 limits (C{zl, zu}) can also be specified as fields in a case dict, 67 either passed in directly or defined in a case file referenced by name. 68 69 When specified, C{A, l, u} represent additional linear constraints on the 70 optimization variables, C{l <= A*[x z] <= u}. If the user specifies an C{A} 71 matrix that has more columns than the number of "C{x}" (OPF) variables, 72 then there are extra linearly constrained "C{z}" variables. For an 73 explanation of the formulation used and instructions for forming the 74 C{A} matrix, see the MATPOWER manual. 75 76 A generalized cost on all variables can be applied if input arguments 77 C{N}, C{fparm}, C{H} and C{Cw} are specified. First, a linear 78 transformation of the optimization variables is defined by means of 79 C{r = N * [x z]}. Then, to each element of r a function is applied as 80 encoded in the C{fparm} matrix (see Matpower manual). If the resulting 81 vector is named C{w}, then C{H} and C{Cw} define a quadratic cost on 82 C{w}: C{(1/2)*w'*H*w + Cw * w}. 83 C{H} and C{N} should be sparse matrices and C{H} should also be symmetric. 84 85 The optional C{ppopt} vector specifies PYPOWER options. See L{ppoption} 86 for details and default values. 87 88 @author: Ray Zimmerman (PSERC Cornell) 89 @author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad 90 Autonoma de Manizales) 91 @author: Richard Lincoln 92 """ 93 # nargin = len([arg for arg in [baseMVA, bus, gen, branch, areas, gencost, 94 # Au, lbu, ubu, ppopt, N, fparm, H, Cw, 95 # z0, zl, zu] if arg is not None]) 96 nargin = len(args) 97 98 userfcn = array([]) 99 ## passing filename or dict 100 if isinstance(args[0], basestring) or isinstance(args[0], dict): 101 # ----opf( baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu) 102 # 12 opf(casefile, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu) 103 # 9 opf(casefile, Au, lbu, ubu, ppopt, N, fparm, H, Cw) 104 # 5 opf(casefile, Au, lbu, ubu, ppopt) 105 # 4 opf(casefile, Au, lbu, ubu) 106 # 3 opf(casefile, userfcn, ppopt) 107 # 2 opf(casefile, ppopt) 108 # 1 opf(casefile) 109 if nargin in [1, 2, 3, 4, 5, 9, 12]: 110 casefile = args[0] 111 if nargin == 12: 112 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm = args 113 zu = fparm 114 zl = N 115 z0 = ppopt 116 Cw = ubu 117 H = lbu 118 fparm = Au 119 N = gencost 120 ppopt = areas 121 ubu = branch 122 lbu = gen 123 Au = bus 124 elif nargin == 9: 125 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu = args 126 zu = array([]) 127 zl = array([]) 128 z0 = array([]) 129 Cw = ubu 130 H = lbu 131 fparm = Au 132 N = gencost 133 ppopt = areas 134 ubu = branch 135 lbu = gen 136 Au = bus 137 elif nargin == 5: 138 baseMVA, bus, gen, branch, areas = args 139 zu = array([]) 140 zl = array([]) 141 z0 = array([]) 142 Cw = array([]) 143 H = None 144 fparm = array([]) 145 N = None 146 ppopt = areas 147 ubu = branch 148 lbu = gen 149 Au = bus 150 elif nargin == 4: 151 baseMVA, bus, gen, branch = args 152 zu = array([]) 153 zl = array([]) 154 z0 = array([]) 155 Cw = array([]) 156 H = None 157 fparm = array([]) 158 N = None 159 ppopt = ppoption() 160 ubu = branch 161 lbu = gen 162 Au = bus 163 elif nargin == 3: 164 baseMVA, bus, gen = args 165 userfcn = bus 166 zu = array([]) 167 zl = array([]) 168 z0 = array([]) 169 Cw = array([]) 170 H = None 171 fparm = array([]) 172 N = None 173 ppopt = gen 174 ubu = array([]) 175 lbu = array([]) 176 Au = None 177 elif nargin == 2: 178 baseMVA, bus = args 179 zu = array([]) 180 zl = array([]) 181 z0 = array([]) 182 Cw = array([]) 183 H = None 184 fparm = array([]) 185 N = None 186 ppopt = bus 187 ubu = array([]) 188 lbu = array([]) 189 Au = None 190 elif nargin == 1: 191 zu = array([]) 192 zl = array([]) 193 z0 = array([]) 194 Cw = array([]) 195 H = None 196 fparm = array([]) 197 N = None 198 ppopt = ppoption() 199 ubu = array([]) 200 lbu = array([]) 201 Au = None 202 else: 203 stderr.write('opf_args: Incorrect input arg order, number or type\n') 204 205 ppc = loadcase(casefile) 206 baseMVA, bus, gen, branch, gencost = \ 207 ppc['baseMVA'], ppc['bus'], ppc['gen'], ppc['branch'], ppc['gencost'] 208 if 'areas' in ppc: 209 areas = ppc['areas'] 210 else: 211 areas = array([]) 212 if Au is None and 'A' in ppc: 213 Au, lbu, ubu = ppc["A"], ppc["l"], ppc["u"] 214 if N is None and 'N' in ppc: ## these two must go together 215 N, Cw = ppc["N"], ppc["Cw"] 216 if H is None and 'H' in ppc: ## will default to zeros 217 H = ppc["H"] 218 if (fparm is None or len(fparm) == 0) and 'fparm' in ppc: ## will default to [1 0 0 1] 219 fparm = ppc["fparm"] 220 if (z0 is None or len(z0) == 0) and 'z0' in ppc: 221 z0 = ppc["z0"] 222 if (zl is None or len(zl) == 0) and 'zl' in ppc: 223 zl = ppc["zl"] 224 if (zu is None or len(zu) == 0) and 'zu' in ppc: 225 zu = ppc["zu"] 226 if (userfcn is None or len(userfcn) == 0) and 'userfcn' in ppc: 227 userfcn = ppc['userfcn'] 228 else: ## passing individual data matrices 229 # ----opf(baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu) 230 # 17 opf(baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu) 231 # 14 opf(baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw) 232 # 10 opf(baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt) 233 # 9 opf(baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu) 234 # 8 opf(baseMVA, bus, gen, branch, areas, gencost, userfcn, ppopt) 235 # 7 opf(baseMVA, bus, gen, branch, areas, gencost, ppopt) 236 # 6 opf(baseMVA, bus, gen, branch, areas, gencost) 237 if nargin in [6, 7, 8, 9, 10, 14, 17]: 238 if nargin == 17: 239 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu = args 240 elif nargin == 14: 241 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw = args 242 zu = array([]) 243 zl = array([]) 244 z0 = array([]) 245 elif nargin == 10: 246 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt = args 247 zu = array([]) 248 zl = array([]) 249 z0 = array([]) 250 Cw = array([]) 251 H = None 252 fparm = array([]) 253 N = None 254 elif nargin == 9: 255 baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu = args 256 zu = array([]) 257 zl = array([]) 258 z0 = array([]) 259 Cw = array([]) 260 H = None 261 fparm = array([]) 262 N = None 263 ppopt = ppoption() 264 elif nargin == 8: 265 baseMVA, bus, gen, branch, areas, gencost, userfcn, ppopt = args 266 zu = array([]) 267 zl = array([]) 268 z0 = array([]) 269 Cw = array([]) 270 H = None 271 fparm = array([]) 272 N = None 273 ubu = array([]) 274 lbu = array([]) 275 Au = None 276 elif nargin == 7: 277 baseMVA, bus, gen, branch, areas, gencost, ppopt = args 278 zu = array([]) 279 zl = array([]) 280 z0 = array([]) 281 Cw = array([]) 282 H = None 283 fparm = array([]) 284 N = None 285 ubu = array([]) 286 lbu = array([]) 287 Au = None 288 elif nargin == 6: 289 baseMVA, bus, gen, branch, areas, gencost = args 290 zu = array([]) 291 zl = array([]) 292 z0 = array([]) 293 Cw = array([]) 294 H = None 295 fparm = array([]) 296 N = None 297 ppopt = ppoption() 298 ubu = array([]) 299 lbu = array([]) 300 Au = None 301 else: 302 stderr.write('opf_args: Incorrect input arg order, number or type\n') 303 304 if N is not None: 305 nw = N.shape[0] 306 else: 307 nw = 0 308 309 if nw: 310 if Cw.shape[0] != nw: 311 stderr.write('opf_args.m: dimension mismatch between N and Cw in ' 312 'generalized cost parameters\n') 313 if len(fparm) > 0 and fparm.shape[0] != nw: 314 stderr.write('opf_args.m: dimension mismatch between N and fparm ' 315 'in generalized cost parameters\n') 316 if (H is not None) and (H.shape[0] != nw | H.shape[0] != nw): 317 stderr.write('opf_args.m: dimension mismatch between N and H in ' 318 'generalized cost parameters\n') 319 if Au is not None: 320 if Au.shape[0] > 0 and N.shape[1] != Au.shape[1]: 321 stderr.write('opf_args.m: A and N must have the same number ' 322 'of columns\n') 323 ## make sure N and H are sparse 324 if not issparse(N): 325 stderr.write('opf_args.m: N must be sparse in generalized cost ' 326 'parameters\n') 327 if not issparse(H): 328 stderr.write('opf_args.m: H must be sparse in generalized cost parameters\n') 329 330 if Au is not None and not issparse(Au): 331 stderr.write('opf_args.m: Au must be sparse\n') 332 if ppopt == None or len(ppopt) == 0: 333 ppopt = ppoption() 334 335 return baseMVA, bus, gen, branch, gencost, Au, lbu, ubu, \ 336 ppopt, N, fparm, H, Cw, z0, zl, zu, userfcn, areas
337 338
339 -def opf_args2(*args):
340 """Parses and initializes OPF input arguments. 341 """ 342 baseMVA, bus, gen, branch, gencost, Au, lbu, ubu, \ 343 ppopt, N, fparm, H, Cw, z0, zl, zu, userfcn, areas = opf_args(*args) 344 345 ppc = args[0] if isinstance(args[0], dict) else {} 346 347 ppc['baseMVA'] = baseMVA 348 ppc['bus'] = bus 349 ppc['gen'] = gen 350 ppc['branch'] = branch 351 ppc['gencost'] = gencost 352 353 if areas is not None and len(areas) > 0: 354 ppc["areas"] = areas 355 if lbu is not None and len(lbu) > 0: 356 ppc["A"], ppc["l"], ppc["u"] = Au, lbu, ubu 357 if Cw is not None and len(Cw) > 0: 358 ppc["N"], ppc["Cw"] = N, Cw 359 if len(fparm) > 0: 360 ppc["fparm"] = fparm 361 #if len(H) > 0: 362 ppc["H"] = H 363 if z0 is not None and len(z0) > 0: 364 ppc["z0"] = z0 365 if zl is not None and len(zl) > 0: 366 ppc["zl"] = zl 367 if zu is not None and len(zu) > 0: 368 ppc["zu"] = zu 369 if userfcn is not None and len(userfcn) > 0: 370 ppc["userfcn"] = userfcn 371 372 return ppc, ppopt
373