def runuopf(casedata=None, ppopt=None, fname='', solvedcase=''):
"""Runs an optimal power flow with unit-decommitment heuristic.
@see: L{rundcopf}, L{runuopf}
@author: Ray Zimmerman (PSERC Cornell)
@author: Richard Lincoln
"""
## default arguments
if casedata is None:
casedata = join(dirname(__file__), 'case9')
ppopt = ppoption(ppopt)
##----- run the unit de-commitment / optimal power flow -----
r = uopf(casedata, ppopt)
##----- output results -----
if fname:
fd = None
try:
fd = open(fname, "wb")
except Exception as detail:
stderr.write("Error opening %s: %s.\n" % (fname, detail))
finally:
if fd is not None:
printpf(r, fd, ppopt)
fd.close()
printpf(r, ppopt=ppopt)
## save solved case
if solvedcase:
savecase(solvedcase, r)
return r
def dcopf(*args, **kw_args):
"""Solves a DC optimal power flow.
This is a simple wrapper function around L{opf} that sets the C{PF_DC}
option to C{True} before calling L{opf}.
See L{opf} for the details of input and output arguments.
@see: L{rundcopf}
@author: Ray Zimmerman (PSERC Cornell)
"""
ppc, ppopt = opf_args2(*args, **kw_args);
ppopt = ppoption(ppopt, PF_DC=1)
return opf(ppc, ppopt)
开发者ID:Anastien,项目名称:PYPOWER,代码行数:15,代码来源:dcopf.py
示例10: calc_power_flow
def calc_power_flow(case):
# we run an opf, but real power output is fixed everywhere except a single quasi-slack bus,
# so it just adjusts the voltage setpoints to minimize losses (and hopefully get more
# reasonable solutions than a raw power flow)
#results = runopf(case)
#results = runpf(case, ppopt=ppoption(ENFORCE_Q_LIMS=1))[0]
results = runpf(case, ppopt=ppoption(OUT_ALL=0))[0]
slack_bus = case["slack_bus"]
slack_gens = case["slack_gens"]
# add in the extra slack generation introduced by the model, so the results
# show the operating state accurately (even if it differs from the proposed state)
results["net_injection"][slack_bus] += (
np.sum(results["gen"][slack_gens, PG] - case["gen"][slack_gens, PG])
)
return results
def newtonpf(Ybus, Sbus, V0, ref, pv, pq, ppopt=None):
"""Solves the power flow using a full Newton's method.
Solves for bus voltages given the full system admittance matrix (for
all buses), the complex bus power injection vector (for all buses),
the initial vector of complex bus voltages, and column vectors with
the lists of bus indices for the swing bus, PV buses, and PQ buses,
respectively. The bus voltage vector contains the set point for
generator (including ref bus) buses, and the reference angle of the
swing bus, as well as an initial guess for remaining magnitudes and
angles. C{ppopt} is a PYPOWER options vector which can be used to
set the termination tolerance, maximum number of iterations, and
output options (see L{ppoption} for details). Uses default options if
this parameter is not given. Returns the final complex voltages, a
flag which indicates whether it converged or not, and the number of
iterations performed.
@see: L{runpf}
@author: Ray Zimmerman (PSERC Cornell)
@author: Richard Lincoln
"""
## default arguments
if ppopt is None:
ppopt = ppoption()
## options
tol = ppopt['PF_TOL']
max_it = ppopt['PF_MAX_IT']
verbose = ppopt['VERBOSE']
## initialize
converged = 0
i = 0
V = V0
Va = angle(V)
Vm = abs(V)
## set up indexing for updating V
pvpq = r_[pv, pq]
npv = len(pv)
npq = len(pq)
j1 = 0; j2 = npv ## j1:j2 - V angle of pv buses
j3 = j2; j4 = j2 + npq ## j3:j4 - V angle of pq buses
j5 = j4; j6 = j4 + npq ## j5:j6 - V mag of pq buses
## evaluate F(x0)
mis = V * conj(Ybus * V) - Sbus
F = r_[ mis[pv].real,
mis[pq].real,
mis[pq].imag ]
## check tolerance
normF = linalg.norm(F, Inf)
if verbose > 1:
sys.stdout.write('\n it max P & Q mismatch (p.u.)')
sys.stdout.write('\n---- ---------------------------')
sys.stdout.write('\n%3d %10.3e' % (i, normF))
if normF < tol:
converged = 1
if verbose > 1:
sys.stdout.write('\nConverged!\n')
## do Newton iterations
while (not converged and i < max_it):
## update iteration counter
i = i + 1
## evaluate Jacobian
dS_dVm, dS_dVa = dSbus_dV(Ybus, V)
J11 = dS_dVa[array([pvpq]).T, pvpq].real
J12 = dS_dVm[array([pvpq]).T, pq].real
J21 = dS_dVa[array([pq]).T, pvpq].imag
J22 = dS_dVm[array([pq]).T, pq].imag
J = vstack([
hstack([J11, J12]),
hstack([J21, J22])
], format="csr")
## compute update step
dx = -1 * spsolve(J, F)
## update voltage
if npv:
Va[pv] = Va[pv] + dx[j1:j2]
if npq:
Va[pq] = Va[pq] + dx[j3:j4]
Vm[pq] = Vm[pq] + dx[j5:j6]
V = Vm * exp(1j * Va)
Vm = abs(V) ## update Vm and Va again in case
Va = angle(V) ## we wrapped around with a negative Vm
## evalute F(x)
mis = V * conj(Ybus * V) - Sbus
F = r_[ mis[pv].real,
mis[pq].real,
mis[pq].imag ]
#.........这里部分代码省略.........
def gausspf(Ybus, Sbus, V0, ref, pv, pq, ppopt=None):
"""Solves the power flow using a Gauss-Seidel method.
Solves for bus voltages given the full system admittance matrix (for
all buses), the complex bus power injection vector (for all buses),
the initial vector of complex bus voltages, and column vectors with
the lists of bus indices for the swing bus, PV buses, and PQ buses,
respectively. The bus voltage vector contains the set point for
generator (including ref bus) buses, and the reference angle of the
swing bus, as well as an initial guess for remaining magnitudes and
angles. C{ppopt} is a PYPOWER options vector which can be used to
set the termination tolerance, maximum number of iterations, and
output options (see C{ppoption} for details). Uses default options
if this parameter is not given. Returns the final complex voltages,
a flag which indicates whether it converged or not, and the number
of iterations performed.
@see: L{runpf}
@author: Ray Zimmerman (PSERC Cornell)
@author: Alberto Borghetti (University of Bologna, Italy)
"""
## default arguments
if ppopt is None:
ppopt = ppoption()
## options
tol = ppopt['PF_TOL']
max_it = ppopt['PF_MAX_IT_GS']
verbose = ppopt['VERBOSE']
## initialize
converged = 0
i = 0
V = V0.copy()
#Va = angle(V)
Vm = abs(V)
## set up indexing for updating V
npv = len(pv)
npq = len(pq)
pvpq = r_[pv, pq]
## evaluate F(x0)
mis = V * conj(Ybus * V) - Sbus
F = r_[ mis[pvpq].real,
mis[pq].imag ]
## check tolerance
normF = linalg.norm(F, Inf)
if verbose > 1:
sys.stdout.write('\n it max P & Q mismatch (p.u.)')
sys.stdout.write('\n---- ---------------------------')
sys.stdout.write('\n%3d %10.3e' % (i, normF))
if normF < tol:
converged = 1
if verbose > 1:
sys.stdout.write('\nConverged!\n')
## do Gauss-Seidel iterations
while (not converged and i < max_it):
## update iteration counter
i = i + 1
## update voltage
## at PQ buses
for k in pq[list(range(npq))]:
tmp = (conj(Sbus[k] / V[k]) - Ybus[k, :] * V) / Ybus[k, k]
V[k] = V[k] + asscalar(tmp)
## at PV buses
if npv:
for k in pv[list(range(npv))]:
tmp = (V[k] * conj(Ybus[k,:] * V)).imag
Sbus[k] = Sbus[k].real + 1j * asscalar(tmp)
tmp = (conj(Sbus[k] / V[k]) - Ybus[k, :] * V) / Ybus[k, k]
V[k] = V[k] + asscalar(tmp)
# V[k] = Vm[k] * V[k] / abs(V[k])
V[pv] = Vm[pv] * V[pv] / abs(V[pv])
## evalute F(x)
mis = V * conj(Ybus * V) - Sbus
F = r_[ mis[pv].real,
mis[pq].real,
mis[pq].imag ]
## check for convergence
normF = linalg.norm(F, Inf)
if verbose > 1:
sys.stdout.write('\n%3d %10.3e' % (i, normF))
if normF < tol:
converged = 1
if verbose:
sys.stdout.write('\nGauss-Seidel power flow converged in '
'%d iterations.\n' % i)
if verbose:
if not converged:
sys.stdout.write('Gauss-Seidel power did not converge in %d '
'iterations.' % i)
#.........这里部分代码省略.........
def opf_args(*args):
"""Parses and initializes OPF input arguments.
Returns the full set of initialized OPF input arguments, filling in
default values for missing arguments. See Examples below for the
possible calling syntax options.
Input arguments options::
opf_args(ppc)
opf_args(ppc, ppopt)
opf_args(ppc, userfcn, ppopt)
opf_args(ppc, A, l, u)
opf_args(ppc, A, l, u, ppopt)
opf_args(ppc, A, l, u, ppopt, N, fparm, H, Cw)
opf_args(ppc, A, l, u, ppopt, N, fparm, H, Cw, z0, zl, zu)
opf_args(baseMVA, bus, gen, branch, areas, gencost)
opf_args(baseMVA, bus, gen, branch, areas, gencost, ppopt)
opf_args(baseMVA, bus, gen, branch, areas, gencost, userfcn, ppopt)
opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u)
opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ppopt)
opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ...
ppopt, N, fparm, H, Cw)
opf_args(baseMVA, bus, gen, branch, areas, gencost, A, l, u, ...
ppopt, N, fparm, H, Cw, z0, zl, zu)
The data for the problem can be specified in one of three ways:
1. a string (ppc) containing the file name of a PYPOWER case
which defines the data matrices baseMVA, bus, gen, branch, and
gencost (areas is not used at all, it is only included for
backward compatibility of the API).
2. a dict (ppc) containing the data matrices as fields.
3. the individual data matrices themselves.
The optional user parameters for user constraints (C{A, l, u}), user costs
(C{N, fparm, H, Cw}), user variable initializer (z0), and user variable
limits (C{zl, zu}) can also be specified as fields in a case dict,
either passed in directly or defined in a case file referenced by name.
When specified, C{A, l, u} represent additional linear constraints on the
optimization variables, C{l <= A*[x z] <= u}. If the user specifies an C{A}
matrix that has more columns than the number of "C{x}" (OPF) variables,
then there are extra linearly constrained "C{z}" variables. For an
explanation of the formulation used and instructions for forming the
C{A} matrix, see the MATPOWER manual.
A generalized cost on all variables can be applied if input arguments
C{N}, C{fparm}, C{H} and C{Cw} are specified. First, a linear
transformation of the optimization variables is defined by means of
C{r = N * [x z]}. Then, to each element of r a function is applied as
encoded in the C{fparm} matrix (see Matpower manual). If the resulting
vector is named C{w}, then C{H} and C{Cw} define a quadratic cost on
C{w}: C{(1/2)*w'*H*w + Cw * w}.
C{H} and C{N} should be sparse matrices and C{H} should also be symmetric.
The optional C{ppopt} vector specifies PYPOWER options. See L{ppoption}
for details and default values.
@author: Ray Zimmerman (PSERC Cornell)
@author: Carlos E. Murillo-Sanchez (PSERC Cornell & Universidad
Autonoma de Manizales)
@author: Richard Lincoln
"""
# nargin = len([arg for arg in [baseMVA, bus, gen, branch, areas, gencost,
# Au, lbu, ubu, ppopt, N, fparm, H, Cw,
# z0, zl, zu] if arg is not None])
nargin = len(args)
userfcn = array([])
## passing filename or dict
if isinstance(args[0], basestring) or isinstance(args[0], dict):
# ----opf( baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu)
# 12 opf(casefile, Au, lbu, ubu, ppopt, N, fparm, H, Cw, z0, zl, zu)
# 9 opf(casefile, Au, lbu, ubu, ppopt, N, fparm, H, Cw)
# 5 opf(casefile, Au, lbu, ubu, ppopt)
# 4 opf(casefile, Au, lbu, ubu)
# 3 opf(casefile, userfcn, ppopt)
# 2 opf(casefile, ppopt)
# 1 opf(casefile)
if nargin in [1, 2, 3, 4, 5, 9, 12]:
casefile = args[0]
if nargin == 12:
baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu, ppopt, N, fparm = args
zu = fparm
zl = N
z0 = ppopt
Cw = ubu
H = lbu
fparm = Au
N = gencost
ppopt = areas
ubu = branch
lbu = gen
Au = bus
elif nargin == 9:
baseMVA, bus, gen, branch, areas, gencost, Au, lbu, ubu = args
zu = array([])
zl = array([])
z0 = array([])
#.........这里部分代码省略.........
请发表评论