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

Source Code for Module pypower.opf

  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  """Solves an optimal power flow. 
 18  """ 
 19   
 20  from time import time 
 21   
 22  from numpy import zeros, c_, shape, ix_ 
 23   
 24  from pypower.idx_bus import MU_VMIN 
 25  from pypower.idx_gen import PG, QG, MU_QMIN, MU_PMAX, MU_PMIN 
 26  from pypower.idx_brch import PF, QF, PT, QT, MU_SF, MU_ST, MU_ANGMIN, MU_ANGMAX 
 27   
 28  from pypower.ext2int import ext2int 
 29  from pypower.opf_args import opf_args2 
 30  from pypower.opf_setup import opf_setup 
 31  from pypower.opf_execute import opf_execute 
 32  from int2ext import int2ext 
 33   
 34   
35 -def opf(*args):
36 """Solves an optimal power flow. 37 38 Returns a C{results} dict. 39 40 The data for the problem can be specified in one of three ways: 41 1. a string (ppc) containing the file name of a PYPOWER case 42 which defines the data matrices baseMVA, bus, gen, branch, and 43 gencost (areas is not used at all, it is only included for 44 backward compatibility of the API). 45 2. a dict (ppc) containing the data matrices as fields. 46 3. the individual data matrices themselves. 47 48 The optional user parameters for user constraints (C{A, l, u}), user costs 49 (C{N, fparm, H, Cw}), user variable initializer (C{z0}), and user variable 50 limits (C{zl, zu}) can also be specified as fields in a case dict, 51 either passed in directly or defined in a case file referenced by name. 52 53 When specified, C{A, l, u} represent additional linear constraints on the 54 optimization variables, C{l <= A*[x z] <= u}. If the user specifies an C{A} 55 matrix that has more columns than the number of "C{x}" (OPF) variables, 56 then there are extra linearly constrained "C{z}" variables. For an 57 explanation of the formulation used and instructions for forming the 58 C{A} matrix, see the MATPOWER manual. 59 60 A generalized cost on all variables can be applied if input arguments 61 C{N}, C{fparm}, C{H} and C{Cw} are specified. First, a linear transformation 62 of the optimization variables is defined by means of C{r = N * [x z]}. 63 Then, to each element of C{r} a function is applied as encoded in the 64 C{fparm} matrix (see MATPOWER manual). If the resulting vector is named 65 C{w}, then C{H} and C{Cw} define a quadratic cost on w: 66 C{(1/2)*w'*H*w + Cw * w}. C{H} and C{N} should be sparse matrices and C{H} 67 should also be symmetric. 68 69 The optional C{ppopt} vector specifies PYPOWER options. If the OPF 70 algorithm is not explicitly set in the options PYPOWER will use the default 71 solver, based on a primal-dual interior point method. For the AC OPF this 72 is C{OPF_ALG = 560}. For the DC OPF, the default is C{OPF_ALG_DC = 200}. 73 See L{ppoption} for more details on the available OPF solvers and other OPF 74 options and their default values. 75 76 The solved case is returned in a single results dict (described 77 below). Also returned are the final objective function value (C{f}) and a 78 flag which is C{True} if the algorithm was successful in finding a solution 79 (success). Additional optional return values are an algorithm specific 80 return status (C{info}), elapsed time in seconds (C{et}), the constraint 81 vector (C{g}), the Jacobian matrix (C{jac}), and the vector of variables 82 (C{xr}) as well as the constraint multipliers (C{pimul}). 83 84 The single results dict is a PYPOWER case struct (ppc) with the 85 usual baseMVA, bus, branch, gen, gencost fields, along with the 86 following additional fields: 87 88 - C{order} see 'help ext2int' for details of this field 89 - C{et} elapsed time in seconds for solving OPF 90 - C{success} 1 if solver converged successfully, 0 otherwise 91 - C{om} OPF model object, see 'help opf_model' 92 - C{x} final value of optimization variables (internal order) 93 - C{f} final objective function value 94 - C{mu} shadow prices on ... 95 - C{var} 96 - C{l} lower bounds on variables 97 - C{u} upper bounds on variables 98 - C{nln} 99 - C{l} lower bounds on nonlinear constraints 100 - C{u} upper bounds on nonlinear constraints 101 - C{lin} 102 - C{l} lower bounds on linear constraints 103 - C{u} upper bounds on linear constraints 104 - C{g} (optional) constraint values 105 - C{dg} (optional) constraint 1st derivatives 106 - C{df} (optional) obj fun 1st derivatives (not yet implemented) 107 - C{d2f} (optional) obj fun 2nd derivatives (not yet implemented) 108 - C{raw} raw solver output in form returned by MINOS, and more 109 - C{xr} final value of optimization variables 110 - C{pimul} constraint multipliers 111 - C{info} solver specific termination code 112 - C{output} solver specific output information 113 - C{alg} algorithm code of solver used 114 - C{var} 115 - C{val} optimization variable values, by named block 116 - C{Va} voltage angles 117 - C{Vm} voltage magnitudes (AC only) 118 - C{Pg} real power injections 119 - C{Qg} reactive power injections (AC only) 120 - C{y} constrained cost variable (only if have pwl costs) 121 - (other) any user defined variable blocks 122 - C{mu} variable bound shadow prices, by named block 123 - C{l} lower bound shadow prices 124 - C{Va}, C{Vm}, C{Pg}, C{Qg}, C{y}, (other) 125 - C{u} upper bound shadow prices 126 - C{Va}, C{Vm}, C{Pg}, C{Qg}, C{y}, (other) 127 - C{nln} (AC only) 128 - C{mu} shadow prices on nonlinear constraints, by named block 129 - C{l} lower bounds 130 - C{Pmis} real power mismatch equations 131 - C{Qmis} reactive power mismatch equations 132 - C{Sf} flow limits at "from" end of branches 133 - C{St} flow limits at "to" end of branches 134 - C{u} upper bounds 135 - C{Pmis}, C{Qmis}, C{Sf}, C{St} 136 - C{lin} 137 - C{mu} shadow prices on linear constraints, by named block 138 - C{l} lower bounds 139 - C{Pmis} real power mistmatch equations (DC only) 140 - C{Pf} flow limits at "from" end of branches (DC only) 141 - C{Pt} flow limits at "to" end of branches (DC only) 142 - C{PQh} upper portion of gen PQ-capability curve(AC only) 143 - C{PQl} lower portion of gen PQ-capability curve(AC only) 144 - C{vl} constant power factor constraint for loads 145 - C{ycon} basin constraints for CCV for pwl costs 146 - (other) any user defined constraint blocks 147 - C{u} upper bounds 148 - C{Pmis}, C{Pf}, C{Pf}, C{PQh}, C{PQl}, C{vl}, C{ycon}, 149 - (other) 150 - C{cost} user defined cost values, by named block 151 152 @see: L{runopf}, L{dcopf}, L{uopf}, L{caseformat} 153 154 @author: Ray Zimmerman (PSERC Cornell) 155 @author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad 156 Autonoma de Manizales) 157 @author: Richard Lincoln 158 """ 159 ##----- initialization ----- 160 t0 = time() ## start timer 161 162 ## process input arguments 163 ppc, ppopt = opf_args2(*args) 164 165 ## add zero columns to bus, gen, branch for multipliers, etc if needed 166 nb = shape(ppc['bus'])[0] ## number of buses 167 nl = shape(ppc['branch'])[0] ## number of branches 168 ng = shape(ppc['gen'])[0] ## number of dispatchable injections 169 if shape(ppc['bus'])[1] < MU_VMIN + 1: 170 ppc['bus'] = c_[ppc['bus'], zeros((nb, MU_VMIN + 1 - shape(ppc['bus'])[1]))] 171 172 if shape(ppc['gen'])[1] < MU_QMIN + 1: 173 ppc['gen'] = c_[ppc['gen'], zeros((ng, MU_QMIN + 1 - shape(ppc['gen'])[1]))] 174 175 if shape(ppc['branch'])[1] < MU_ANGMAX + 1: 176 ppc['branch'] = c_[ppc['branch'], zeros((nl, MU_ANGMAX + 1 - shape(ppc['branch'])[1]))] 177 178 ##----- convert to internal numbering, remove out-of-service stuff ----- 179 ppc = ext2int(ppc) 180 181 ##----- construct OPF model object ----- 182 om = opf_setup(ppc, ppopt) 183 184 ##----- execute the OPF ----- 185 results, success, raw = opf_execute(om, ppopt) 186 187 ##----- revert to original ordering, including out-of-service stuff ----- 188 results = int2ext(results) 189 190 ## zero out result fields of out-of-service gens & branches 191 if len(results['order']['gen']['status']['off']) > 0: 192 results['gen'][ ix_(results['order']['gen']['status']['off'], [PG, QG, MU_PMAX, MU_PMIN]) ] = 0 193 194 if len(results['order']['branch']['status']['off']) > 0: 195 results['branch'][ ix_(results['order']['branch']['status']['off'], [PF, QF, PT, QT, MU_SF, MU_ST, MU_ANGMIN, MU_ANGMAX]) ] = 0 196 197 ##----- finish preparing output ----- 198 et = time() - t0 ## compute elapsed time 199 200 results['et'] = et 201 results['success'] = success 202 results['raw'] = raw 203 204 return results
205