Home > matpower4.1 > t > t_opf_constr.m

t_opf_constr

PURPOSE ^

------------------------------ deprecated ------------------------------

SYNOPSIS ^

function t_opf_constr(quiet)

DESCRIPTION ^

------------------------------  deprecated  ------------------------------
   OPF solver based on CONSTR to be removed in a future version.
--------------------------------------------------------------------------
T_OPF_CONSTR  Tests for CONSTR-based optimal power flow.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_opf_constr(quiet)
0002 %------------------------------  deprecated  ------------------------------
0003 %   OPF solver based on CONSTR to be removed in a future version.
0004 %--------------------------------------------------------------------------
0005 %T_OPF_CONSTR  Tests for CONSTR-based optimal power flow.
0006 
0007 %   MATPOWER
0008 %   $Id: t_opf_constr.m,v 1.19 2011/12/01 21:43:56 cvs Exp $
0009 %   by Ray Zimmerman, PSERC Cornell
0010 %   Copyright (c) 2004-2010 by Power System Engineering Research Center (PSERC)
0011 %
0012 %   This file is part of MATPOWER.
0013 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0014 %
0015 %   MATPOWER is free software: you can redistribute it and/or modify
0016 %   it under the terms of the GNU General Public License as published
0017 %   by the Free Software Foundation, either version 3 of the License,
0018 %   or (at your option) any later version.
0019 %
0020 %   MATPOWER is distributed in the hope that it will be useful,
0021 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0022 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0023 %   GNU General Public License for more details.
0024 %
0025 %   You should have received a copy of the GNU General Public License
0026 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0027 %
0028 %   Additional permission under GNU GPL version 3 section 7
0029 %
0030 %   If you modify MATPOWER, or any covered work, to interface with
0031 %   other modules (such as MATLAB code and MEX-files) available in a
0032 %   MATLAB(R) or comparable environment containing parts covered
0033 %   under other licensing terms, the licensors of MATPOWER grant
0034 %   you additional permission to convey the resulting work.
0035 
0036 if nargin < 1
0037     quiet = 0;
0038 end
0039 
0040 num_tests = 101;
0041 
0042 t_begin(num_tests, quiet);
0043 
0044 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0045     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0046 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0047     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0048     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0049 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0050     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0051     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0052 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0053 
0054 casefile = 't_case9_opf';
0055 if quiet
0056     verbose = 0;
0057 else
0058     verbose = 0;
0059 end
0060 
0061 t0 = 'constr OPF : ';
0062 mpopt = mpoption('OUT_ALL', 0, 'VERBOSE', verbose, 'OPF_ALG', 300);
0063 mpopt = mpoption(mpopt, 'OPF_VIOLATION', 1e-6, 'CONSTR_TOL_X', 1e-5, 'CONSTR_TOL_F', 1e-5, 'CONSTR_MAX_IT', 300);
0064 % mpopt = mpoption(mpopt, 'VERBOSE', 2);
0065 
0066 if have_fcn('constr')
0067     s = warning('query', 'all');
0068     warning('off', 'all');
0069 
0070     %% set up indices
0071     ib_data     = [1:BUS_AREA BASE_KV:VMIN];
0072     ib_voltage  = [VM VA];
0073     ib_lam      = [LAM_P LAM_Q];
0074     ib_mu       = [MU_VMAX MU_VMIN];
0075     ig_data     = [GEN_BUS QMAX QMIN MBASE:APF];
0076     ig_disp     = [PG QG VG];
0077     ig_mu       = (MU_PMAX:MU_QMIN);
0078     ibr_data    = (1:ANGMAX);
0079     ibr_flow    = (PF:QT);
0080     ibr_mu      = [MU_SF MU_ST];
0081     ibr_angmu   = [MU_ANGMIN MU_ANGMAX];
0082 
0083     %% get solved AC power flow case from MAT-file
0084     load soln9_opf;     %% defines bus_soln, gen_soln, branch_soln, f_soln
0085 
0086     %% run OPF
0087     t = t0;
0088     [bus, gen, branch, f, success] = opf(casefile, mpopt);
0089     t_ok(success, [t 'success']);
0090     t_is(f, f_soln, 3, [t 'f']);
0091     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0092     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0093     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0094     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0095     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0096     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0097     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0098     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0099     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0100     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0101 
0102     %% run with automatic conversion of single-block pwl to linear costs
0103     t = [t0 '(single-block PWL) : '];
0104     mpc = loadcase(casefile);
0105     mpc.gencost(3, NCOST) = 2;
0106     [r, success] = runopf(mpc, mpopt);
0107     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0108     t_ok(success, [t 'success']);
0109     t_is(f, f_soln, 3, [t 'f']);
0110     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0111     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0112     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0113     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0114     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0115     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0116     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0117     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0118     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0119     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0120     xr = [r.var.val.Va;r.var.val.Vm;r.var.val.Pg;r.var.val.Qg;0;r.var.val.y];
0121     t_is(r.x, xr, 8, [t 'check on raw x returned from OPF']);
0122 
0123     %% get solved AC power flow case from MAT-file
0124     load soln9_opf_Plim;       %% defines bus_soln, gen_soln, branch_soln, f_soln
0125     
0126     %% run OPF with active power line limits
0127     t = [t0 '(P line lim) : '];
0128     mpopt1 = mpoption(mpopt, 'OPF_FLOW_LIM', 1);
0129     [bus, gen, branch, f, success] = opf(casefile, mpopt1);
0130     t_ok(success, [t 'success']);
0131     t_is(f, f_soln, 3, [t 'f']);
0132     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0133     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0134     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0135     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0136     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0137     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0138     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0139     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0140     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0141     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0142 
0143     %%-----  test OPF with quadratic gen costs moved to generalized costs  -----
0144     mpc = loadcase(casefile);
0145     mpc.gencost = [
0146         2   1500    0   3   0.11    5   0;
0147         2   2000    0   3   0.085   1.2 0;
0148         2   3000    0   3   0.1225  1   0;
0149     ];
0150     [bus_soln, gen_soln, branch_soln, f_soln, success] = opf(mpc, mpopt);
0151     branch_soln = branch_soln(:,1:MU_ST);
0152     
0153     A = sparse(0,0);
0154     l = [];
0155     u = [];
0156     nb = size(mpc.bus, 1);      % number of buses
0157     ng = size(mpc.gen, 1);      % number of gens
0158     thbas = 1;                thend    = thbas+nb-1;
0159     vbas     = thend+1;       vend     = vbas+nb-1;
0160     pgbas    = vend+1;        pgend    = pgbas+ng-1;
0161     qgbas    = pgend+1;       qgend    = qgbas+ng-1;
0162     nxyz = 2*nb + 2*ng;
0163     N = sparse((1:ng)', (pgbas:pgend)', mpc.baseMVA * ones(ng,1), ng, nxyz);
0164     fparm = ones(ng,1) * [ 1 0 0 1 ];
0165     [junk, ix] = sort(mpc.gen(:, 1));
0166     H = 2 * spdiags(mpc.gencost(ix, 5), 0, ng, ng);
0167     Cw = mpc.gencost(ix, 6);
0168     mpc.gencost(:, 5:7) = 0;
0169 
0170     %% run OPF with quadratic gen costs moved to generalized costs
0171     t = [t0 'w/quadratic generalized gen cost : '];
0172     [r, success] = opf(mpc, A, l, u, mpopt, N, fparm, H, Cw);
0173     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0174     t_ok(success, [t 'success']);
0175     t_is(f, f_soln, 3, [t 'f']);
0176     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0177     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0178     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0179     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0180     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0181     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0182     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0183     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0184     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0185     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0186     t_is(r.cost.usr, f, 12, [t 'user cost']);
0187 
0188     %%-----  run OPF with extra linear user constraints & costs  -----
0189     %% single new z variable constrained to be greater than or equal to
0190     %% deviation from 1 pu voltage at bus 1, linear cost on this z
0191     %% get solved AC power flow case from MAT-file
0192     load soln9_opf_extras1;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0193     A = sparse([1;1;2;2],[10;25;10;25],[-1;1;1;1],2,25);
0194     u = [Inf; Inf];
0195     l = [-1; 1];
0196     
0197     N = sparse(1, 25, 1, 1, 25);    %% new z variable only
0198     fparm = [1 0 0 1];              %% w = r = z
0199     H = sparse(1,1);                %% no quadratic term
0200     Cw = 100;
0201 
0202     t = [t0 'w/extra constraints & costs 1 : '];
0203     [r, success] = opf(casefile, A, l, u, mpopt, N, fparm, H, Cw);
0204     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0205     t_ok(success, [t 'success']);
0206     t_is(f, f_soln, 3, [t 'f']);
0207     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0208     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0209     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  2, [t 'bus lambda']);
0210     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0211     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0212     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0213     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0214     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0215     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0216     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0217     t_is(r.var.val.z, 0.0254185, 6, [t 'user variable']);
0218     t_is(r.cost.usr, 2.54185, 4, [t 'user cost']);
0219 
0220     %%-----  test OPF with capability curves  -----
0221     mpc = loadcase('t_case9_opfv2');
0222     %% remove angle diff limits
0223     mpc.branch(1, ANGMAX) = 360;
0224     mpc.branch(9, ANGMIN) = -360;
0225     
0226     %% get solved AC power flow case from MAT-file
0227     load soln9_opf_PQcap;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0228         
0229     %% run OPF with capability curves
0230     t = [t0 'w/capability curves : '];
0231     [bus, gen, branch, f, success] = opf(mpc, mpopt);
0232     t_ok(success, [t 'success']);
0233     t_is(f, f_soln, 3, [t 'f']);
0234     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0235     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0236     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0237     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0238     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0239     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0240     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0241     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0242     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0243     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0244 
0245     %%-----  test OPF with angle difference limits  -----
0246     mpc = loadcase('t_case9_opfv2');
0247     %% remove capability curves
0248     mpc.gen(2:3, [PC1, PC2, QC1MIN, QC1MAX, QC2MIN, QC2MAX]) = zeros(2,6);
0249     
0250     %% get solved AC power flow case from MAT-file
0251     load soln9_opf_ang;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0252         
0253     %% run OPF with angle difference limits
0254     t = [t0 'w/angle difference limits : '];
0255     %% this one does not converge, but if you start at the solution it stays
0256     mpc.bus(:, [VM VA]) = bus_soln(:, [VM VA]);
0257     mpc.gen(:, [PG QG VG]) = gen_soln(:, [PG QG VG]);
0258     [bus, gen, branch, f, success] = opf(mpc, mpopt);
0259     t_ok(success, [t 'success']);
0260     t_is(f, f_soln, 3, [t 'f']);
0261     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0262     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0263     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  0, [t 'bus lambda']);
0264     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  -2, [t 'bus mu']);
0265     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0266     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0267     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  0, [t 'gen mu']);
0268     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0269     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0270     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  0, [t 'branch mu']);
0271     t_is(branch(:,ibr_angmu ), branch_soln(:,ibr_angmu ),  -1, [t 'branch angle mu']);
0272 
0273     %%-----  test OPF with ignored angle difference limits  -----
0274     %% get solved AC power flow case from MAT-file
0275     load soln9_opf;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0276     
0277     %% run OPF with ignored angle difference limits
0278     t = [t0 'w/ignored angle difference limits : '];
0279     mpopt1 = mpoption(mpopt, 'OPF_IGNORE_ANG_LIM', 1);
0280     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(mpc, mpopt1);
0281     %% ang limits are not in this solution data, so let's remove them
0282     branch(1, ANGMAX) = 360;
0283     branch(9, ANGMIN) = -360;
0284     t_ok(success, [t 'success']);
0285     t_is(f, f_soln, 3, [t 'f']);
0286     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0287     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0288     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0289     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0290     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0291     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0292     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0293     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0294     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0295     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0296 
0297     warning(s);
0298 else
0299     t_skip(num_tests, 'constr not available');
0300 end
0301 
0302 t_end;

Generated on Mon 26-Jan-2015 15:00:13 by m2html © 2005