📄 copf.m
字号:
function [busout, genout, branchout, f, success, info, et, g, jac] = copf( ... baseMVA, bus, gen, branch, areas, gencost, mpopt)%COPF Solves an AC optimal power flow using CONSTR (Opt Tbx 1.x & 2.x).%% Uses the standard/CCV formulations from pre-3.0 versions of MATPOWER, as% opposed to the generalized formulation used by fmincopf and MINOPF.%% [bus, gen, branch, f, success] = copf(casefile, mpopt)%% [bus, gen, branch, f, success] = copf(baseMVA, bus, gen, branch, areas, ...% gencost, mpopt)%% [bus, gen, branch, f, success, info, et, g, jac] = copf(casefile)%% The data for the problem can be specified in one of 3 ways: (1) the name of% a case file which defines the data matrices baseMVA, bus, gen, branch,% areas and gencost, (2) a struct containing the data matrices as fields, or% (3) the data matrices themselves.%% The optional mpopt vector specifies MATPOWER options. Type 'help mpoption'% for details and default values.%% The solved case is returned in the data matrices, bus, gen and branch. Also,% returned are the final objective function value (f) and a flag which is% true if the algorithm was successful in finding a solution (success).% Additional optional return values are an algorithm specific return status% (info), elapsed time in seconds (et), the constraint vector (g) and the% Jacobian matrix (jac).%% Note: the original constr() in The Mathworks' Optimization Toolbox 1.x% had a bug in its handling of negative Lagrangian multipliers. For a while,% MathWorks distributed a fixed version of constr.m, but it no longer appears% to be available on their web and ftp sites.% MATPOWER% $Id: copf.m,v 1.4 2005/01/21 20:43:19 ray Exp $% by Carlos E. Murillo-Sanchez, PSERC Cornell & Universidad Autonoma de Manizales% and Ray Zimmerman, PSERC Cornell% Copyright (c) 1996-2005 by Power System Engineering Research Center (PSERC)% See http://www.pserc.cornell.edu/matpower/ for more info.% input arg sortingt1 = clock;if isstr(baseMVA) | isstruct(baseMVA) casefile = baseMVA; if nargin == 1 mpopt = mpoption; elseif nargin == 2 mpopt = bus; else error('copf.m: Incorrect input parameter order, number or type'); end [baseMVA, bus, gen, branch, areas, gencost] = loadcase(casefile);else if nargin == 6 mpopt = mpoption; elseif nargin ~= 7 error('copf.m: Incorrect input parameter order, number or type'); endend% Define constantsj = sqrt(-1);% optionsverbose = mpopt(31);npts = mpopt(14);% Load column indexes for case tables.[PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ... VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, ... RATE_C, TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST] = idx_brch;[GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, ... GEN_STATUS, PMAX, PMIN, MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = idx_gen;[PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;% If tables do not have multiplier/extra columns, append zero colsif size(bus,2) < MU_VMIN bus = [bus zeros(size(bus,1),MU_VMIN-size(bus,2)) ];endif size(gen,2) < MU_QMIN gen = [ gen zeros(size(gen,1),MU_QMIN-size(gen,2)) ];endif size(branch,2) < MU_ST branch = [ branch zeros(size(branch,1),MU_ST-size(branch,2)) ];end% Filter out inactive generators and branches; save original bus & branchcomgen = find(gen(:,GEN_STATUS) > 0);offgen = find(gen(:,GEN_STATUS) <= 0);onbranch = find(branch(:,BR_STATUS) ~= 0);offbranch = find(branch(:,BR_STATUS) == 0);genorg = gen;branchorg = branch;ng = size(gen,1); % original size(gen), at least temporallygen = gen(comgen, :);branch = branch(onbranch, :);if size(gencost,1) == ng gencost = gencost(comgen, :);else gencost = gencost( [comgen; comgen+ng], :);end% Renumber buses consecutively[i2e, bus, gen, branch, areas] = ext2int(bus, gen, branch, areas);%% get bus index lists of each type of bus[ref, pv, pq] = bustypes(bus, gen);%% build admittance matrices[Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);% Check costsif any( (gencost(:, MODEL) ~= PW_LINEAR) & (gencost(:, MODEL) ~= POLYNOMIAL) ) error('copf.m: Unrecognized generator cost model in input data');end% print warning if there are dispatchable loadsif any(isload(gen)) fprintf('\ncopf.m: Warning: Found dispatchable load in data (see ''help isload''\n'); fprintf(' for details on what consitutes a dispatchable load). Only\n'); fprintf(' the generalized formulation algorithms can maintain the power\n'); fprintf(' factor of dispatchable loads constant; the Q injection will\n'); fprintf(' be treated as a free variable.\n');endif mpopt(11) == 0 % OPF_ALG not set if any( gencost(:, MODEL) == PW_LINEAR ) mpopt(11) = 200; % CCV formulation, constr else % All are polynomial mpopt(11) = 100; % Standard, constr endendalg = mpopt(11);formulation = opf_form(alg);if (alg ~= 100) & (alg ~= 200) error('copf: constr.m solver called, but another solver specified in options');endif (alg == 200) & any(gencost(:, MODEL) == POLYNOMIAL) % mixed models if verbose fprintf('copf.m: some generators use poly cost model, all will be converted\n'); fprintf(' to piecewise linear.\n'); end [pcost, qcost] = pqcost(gencost, size(gen, 1)); i_poly = find(pcost(:, MODEL) == POLYNOMIAL); tmp = poly2pwl(pcost(i_poly, :), gen(i_poly, PMIN), ... gen(i_poly, PMAX), npts); pcost(i_poly, 1:size(tmp,2)) = tmp; if ~isempty(qcost) i_poly = find(qcost(:, MODEL) == POLYNOMIAL); tmp = poly2pwl(qcost(i_poly, :), gen(i_poly, QMIN), ... gen(i_poly, QMAX), npts); qcost(i_poly, 1:size(tmp,2)) = tmp; end gencost = [pcost; qcost];endif (alg == 100) & any(gencost(:, MODEL) == PW_LINEAR) error('copf.m: Standard formulation requested, but there are piece-wise linear costs in data');end% Now go for it.gbus = gen(:, GEN_BUS); %% what buses are gens at?%% sizes of thingsnb = size(bus, 1);nl = size(branch, 1);npv = length(pv);npq = length(pq);ng = size(gen, 1); %% number of generators that are turned on%% initial stateV = bus(:, VM) .* exp(sqrt(-1) * pi/180 * bus(:, VA));V(gbus) = gen(:, VG) ./ abs(V(gbus)).* V(gbus);Pg = gen(:, PG) / baseMVA;Qg = gen(:, QG) / baseMVA;%% check for costs for Qg[pcost, qcost] = pqcost(gencost, size(gen, 1));%% set up indexing for xj1 = 1; j2 = npv; %% j1:j2 - V angle of pv busesj3 = j2 + 1; j4 = j2 + npq; %% j3:j4 - V angle of pq busesj5 = j4 + 1; j6 = j4 + nb; %% j5:j6 - V mag of all busesj7 = j6 + 1; j8 = j6 + ng; %% j7:j8 - P of generatorsj9 = j8 + 1; j10 = j8 + ng; %% j9:j10 - Q of generatorsj11 = j10 + 1; j12 = j10 + ng; %% j11:j12 - Cp, cost of Pgj13 = j12 + 1; j14 = j12 + ng; %% j13:j14 - Cq, cost of Qg%% set up xif formulation == 1 Cp = []; Cq = [];else Cp = totcost(pcost, Pg * baseMVA); Cq = totcost(qcost, Qg * baseMVA); %% empty if qcost is emptyendx = [angle(V([pv; pq])); abs(V); Pg; Qg; Cp; Cq];%% objective and constraint function names[fun, grad] = fg_names(alg);mpopt(15) = 2 * nb; %% set number of equality constraints%% set some optionsif mpopt(19) == 0 mpopt(19) = 2 * nb + 150; %% set max number of iterations for constrend%% set up options for Optim Tbx's constrotopt = foptions; %% get default options for constrotopt(1) = (verbose > 0); %% set verbose flag appropriately% otopt(9) = 1; %% check user supplied gradients?otopt(2) = mpopt(17); %% termination tolerance on 'x'otopt(3) = mpopt(18); %% termination tolerance on 'F'otopt(4) = mpopt(16); %% termination tolerance on constraint violationotopt(13) = mpopt(15); %% number of equality constraintsotopt(14) = mpopt(19); %% maximum number of iterations%% run optimization[x, otopt, lambda] = constr(fun, x, otopt, [], [], grad, ... baseMVA, bus, gen, gencost, branch, Ybus, Yf, Yt, V, ref, pv, pq, mpopt);%% get final objective function value & constraint values[f, g] = feval(fun, x, baseMVA, bus, gen, gencost, branch, Ybus, Yf, Yt, V, ... ref, pv, pq, mpopt);%% check for convergenceif otopt(10) >= otopt(14) | max(abs(g(1:otopt(13)))) > otopt(4) ... | max(g((otopt(13)+1):length(g))) > otopt(4) success = 0; %% did NOT convergeelse success = 1; %% DID convergeendinfo = success;%% reconstruct VVa = zeros(nb, 1);Va([ref; pv; pq]) = [angle(V(ref)); x(j1:j2); x(j3:j4)];Vm = x(j5:j6);V = Vm .* exp(j * Va);%% grab Pg & QgSg = x(j7:j8) + j * x(j9:j10); %% complex power generation in p.u.%%----- calculate return values -----%% update bus, gen, branch with solution info[bus, gen, branch] = opfsoln(baseMVA, bus, gen, branch, ... Ybus, Yf, Yt, V, Sg, lambda, ref, pv, pq, mpopt);if nargout > 8 % Do we need to provide the Jacobian? [df, dg] = feval(grad, x, baseMVA, bus, gen, gencost, branch, Ybus, ... Yf, Yt, V, ref, pv, pq, mpopt); jac = dg';end% convert to original external bus ordering[bus, gen, branch, areas] = int2ext(i2e, bus, gen, branch, areas);% Now create output matrices with all lines, all generators, committed and% non-committedgenout = genorg;branchout = branchorg;genout(comgen, : ) = gen;branchout(onbranch, :) = branch;% And zero out appropriate fields of non-comitted generators and linestmp = zeros(length(offgen), 1);genout(offgen, PG) = tmp;genout(offgen, QG) = tmp;genout(offgen, MU_PMAX) = tmp;genout(offgen, MU_PMIN) = tmp;tmp = zeros(length(offbranch), 1);branchout(offbranch, PF) = tmp;branchout(offbranch, QF) = tmp;branchout(offbranch, PT) = tmp;branchout(offbranch, QT) = tmp;branchout(offbranch, MU_SF) = tmp;branchout(offbranch, MU_ST) = tmp;%% compute elapsed timeet = etime(clock, t1);if (nargout == 0) & ( info > 0) printpf(baseMVA, bus, genout, branchout, f, info, et, 1, mpopt);endif nargout, busout = bus; endreturn;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -