Home > matpower7.0 > lib > t > t_opf_knitro.m

t_opf_knitro

PURPOSE ^

T_OPF_FMINCON Tests for FMINCON-based optimal power flow.

SYNOPSIS ^

function t_opf_knitro(quiet)

DESCRIPTION ^

T_OPF_FMINCON  Tests for FMINCON-based optimal power flow.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_opf_knitro(quiet)
0002 %T_OPF_FMINCON  Tests for FMINCON-based optimal power flow.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2004-2017, Power Systems Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   This file is part of MATPOWER.
0009 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0010 %   See https://matpower.org for more info.
0011 
0012 if nargin < 1
0013     quiet = 0;
0014 end
0015 
0016 %% current mismatch, cartesian V
0017 options = {
0018     {0, 0},
0019     {0, 1},
0020     {1, 0},
0021     {1, 1},
0022 };
0023 
0024 num_tests = 216;
0025 
0026 t_begin(length(options)*num_tests, quiet);
0027 
0028 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0029     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0030 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0031     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0032     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0033 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0034     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0035     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0036 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0037 
0038 casefile = 't_case9_opf';
0039 if quiet
0040     verbose = 0;
0041 else
0042     verbose = 0;
0043 end
0044 
0045 mpopt = mpoption('opf.violation', 1e-9);
0046 mpopt = mpoption(mpopt, 'out.all', 0, 'verbose', verbose, 'opf.ac.solver', 'KNITRO');
0047 mpopt = mpoption(mpopt, 'knitro.tol_x', 1e-8, 'knitro.tol_f', 1e-8);
0048 
0049 for k = 1:length(options)
0050     if options{k}{1}, bal = 'I';  else, bal = 'S'; end  %% nodal balance
0051     if options{k}{2}, crd = 'c';  else, crd = 'p'; end  %% V coordinates
0052     t0 = sprintf('Knitro OPF (%s,%s) : ', bal, crd);
0053 
0054     if ~have_fcn('knitro')
0055         t_skip(num_tests, 'Artelys Knitro not available');
0056         continue;
0057     end
0058 
0059     mpopt = mpoption(mpopt, 'opf.current_balance',  options{k}{1}, ...
0060                             'opf.v_cartesian',      options{k}{2} );
0061 
0062     %% set up indices
0063     ib_data     = [1:BUS_AREA BASE_KV:VMIN];
0064     ib_voltage  = [VM VA];
0065     ib_lam      = [LAM_P LAM_Q];
0066     ib_mu       = [MU_VMAX MU_VMIN];
0067     ig_data     = [GEN_BUS QMAX QMIN MBASE:APF];
0068     ig_disp     = [PG QG VG];
0069     ig_mu       = (MU_PMAX:MU_QMIN);
0070     ibr_data    = (1:ANGMAX);
0071     ibr_flow    = (PF:QT);
0072     ibr_mu      = [MU_SF MU_ST];
0073     ibr_angmu   = [MU_ANGMIN MU_ANGMAX];
0074 
0075     %% get solved AC OPF case from MAT-file
0076     load soln9_opf;     %% defines bus_soln, gen_soln, branch_soln, f_soln
0077 
0078     %% run OPF
0079     for s = 0:3
0080         mpopt = mpoption(mpopt, 'opf.start', s);
0081         t = sprintf('%s(start=%d): ', t0, s);
0082         [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt);
0083         t_ok(success, [t 'success']);
0084         t_is(f, f_soln, 3, [t 'f']);
0085         t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0086         t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0087         t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0088         t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0089         t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0090         t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0091         t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0092         t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0093         t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0094         t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0095     end
0096     mpopt = mpoption(mpopt, 'opf.start', 0);    %% set 'opf.start' back to default
0097 
0098     %% run with automatic conversion of single-block pwl to linear costs
0099     t = [t0 '(single-block PWL) : '];
0100     mpc = loadcase(casefile);
0101     mpc.gencost(2, NCOST) = 2;
0102     [r, success] = runopf(mpc, mpopt);
0103     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0104     t_ok(success, [t 'success']);
0105     t_is(f, f_soln, 3, [t 'f']);
0106     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0107     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0108     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0109     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0110     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0111     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0112     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0113     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0114     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0115     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0116     if mpopt.opf.v_cartesian
0117         xr = [r.var.val.Vr;r.var.val.Vi;r.var.val.Pg;r.var.val.Qg;0;r.var.val.y];
0118     else
0119         xr = [r.var.val.Va;r.var.val.Vm;r.var.val.Pg;r.var.val.Qg;0;r.var.val.y];
0120     end
0121     t_is(r.x, xr, 8, [t 'raw x returned from OPF']);
0122 
0123     %% get solved AC OPF 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', 'P');
0129     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(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     t = [t0 '(P^2 line lim) : '];
0144     mpopt1 = mpoption(mpopt, 'opf.flow_lim', '2');
0145     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(casefile, mpopt1);
0146     t_ok(success, [t 'success']);
0147     t_is(f, f_soln, 3, [t 'f']);
0148     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0149     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0150     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0151     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0152     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0153     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0154     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0155     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0156     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0157     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0158 
0159     %%-----  test OPF with quadratic gen costs moved to generalized costs  -----
0160     mpc = loadcase(casefile);
0161     mpc.gencost = [
0162         2   1500    0   3   0.11    5   0;
0163         2   2000    0   3   0.085   1.2 0;
0164         2   3000    0   3   0.1225  1   0;
0165     ];
0166     [baseMVA, bus_soln, gen_soln, gencost, branch_soln, f_soln, success, et] = runopf(mpc, mpopt);
0167     branch_soln = branch_soln(:,1:MU_ST);
0168 
0169     A = sparse(0,0);
0170     l = [];
0171     u = [];
0172     nb = size(mpc.bus, 1);      % number of buses
0173     ng = size(mpc.gen, 1);      % number of gens
0174     thbas = 1;                thend    = thbas+nb-1;
0175     vbas     = thend+1;       vend     = vbas+nb-1;
0176     pgbas    = vend+1;        pgend    = pgbas+ng-1;
0177     qgbas    = pgend+1;       qgend    = qgbas+ng-1;
0178     nxyz = 2*nb + 2*ng;
0179     N = sparse((1:ng)', (pgbas:pgend)', mpc.baseMVA * ones(ng,1), ng, nxyz);
0180     fparm = [ 1    0   0 1;
0181               1 -100 100 1;
0182               1  -10  10 1 ];
0183     H = 2 * spdiags(mpc.gencost(:, 5), 0, ng, ng);
0184     Cw = mpc.gencost(:, 6);
0185     mpc.gencost(:, 5:7) = 0;
0186 
0187     %% run OPF with quadratic gen costs moved to generalized costs
0188     t = [t0 'w/quadratic generalized gen cost : '];
0189     [r, success] = opf(mpc, A, l, u, mpopt, N, fparm, H, Cw);
0190     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0191     t_ok(success, [t 'success']);
0192     t_is(f, f_soln, 3, [t 'f']);
0193     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0194     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0195     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0196     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0197     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0198     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0199     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0200     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0201     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0202     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0203     t_is(r.cost.usr, f, 12, [t 'user cost']);
0204 
0205     %%-----  run OPF with legacy costs and deadzone  -----
0206     if mpopt.opf.v_cartesian
0207         t_skip(17, 'legacy cost example n/a to cartesian V case')
0208     else
0209         load soln9_opf;
0210         mpc = loadcase(casefile);
0211         mpc.N = sparse((1:nb)', (vbas:vend)', ones(nb,1), nb, nxyz);
0212         mpc.fparm = ones(nb,1) * [ 2 1.08 0.02 1e8 ];
0213         mpc.Cw = ones(nb, 1);
0214         t = [t0 'w/legacy cost, in deadzone : '];
0215         r = runopf(mpc, mpopt);
0216         [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0217         t_ok(r.success, [t 'success']);
0218         t_is(f, f_soln, 3, [t 'f']);
0219         t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0220         t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0221         t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0222         t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0223         t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0224         t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0225         t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0226         t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0227         t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0228         t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0229         t_is(r.cost.usr, 0, 12, [t 'user cost']);
0230 
0231         t = [t0 'w/legacy cost, not in deadzone : '];
0232         mpc.fparm = ones(nb,1) * [ 2 1.08 0.01 1e8 ];
0233         r = runopf(mpc, mpopt);
0234         [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0235         t_ok(r.success, [t 'success']);
0236         t_is(f, 9009.0890, 3, [t 'f']);
0237         t_is([min(bus(:, VM)) mean(bus(:, VM)) max(bus(:, VM))], ...
0238             [1.066624, 1.083980, 1.091698], 5, [t 'bus voltage']);
0239         t_is(r.cost.usr, 1673.065465, 5, [t 'user cost']);
0240     end
0241 
0242     %%-----  run OPF with extra linear user constraints & costs  -----
0243     %% single new z variable constrained to be greater than or equal to
0244     %% deviation from 1 pu voltage at bus 1, linear cost on this z
0245     %% get solved AC OPF case from MAT-file
0246     if mpopt.opf.v_cartesian
0247         t_skip(14, 'lin constraint/cost example n/a to cartesian V case')
0248     else
0249         load soln9_opf_extras1;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0250         A = sparse([1;1;2;2],[10;25;10;25],[-1;1;1;1],2,25);
0251         u = [Inf; Inf];
0252         l = [-1; 1];
0253 
0254         N = sparse(1, 25, 1, 1, 25);    %% new z variable only
0255         fparm = [1 0 0 1];              %% w = r = z
0256         H = sparse(1,1);                %% no quadratic term
0257         Cw = 100;
0258 
0259         t = [t0 'w/extra constraints & costs 1 : '];
0260         [r, success] = opf(casefile, A, l, u, mpopt, N, fparm, H, Cw);
0261         [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0262         t_ok(success, [t 'success']);
0263         t_is(f, f_soln, 3, [t 'f']);
0264         t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0265         t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0266         t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0267         t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0268         t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0269         t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0270         t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0271         t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0272         t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0273         t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0274         t_is(r.var.val.z, 0.025419, 6, [t 'user variable']);
0275         t_is(r.cost.usr, 2.5419, 4, [t 'user cost']);
0276     end
0277 
0278     %%-----  test OPF with capability curves  -----
0279     mpc = loadcase('t_case9_opfv2');
0280     %% remove angle diff limits
0281     mpc.branch(1, ANGMAX) = 360;
0282     mpc.branch(9, ANGMIN) = -360;
0283 
0284     %% get solved AC OPF case from MAT-file
0285     load soln9_opf_PQcap;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0286     
0287     %% run OPF with capability curves
0288     t = [t0 'w/capability curves : '];
0289     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(mpc, mpopt);
0290     t_ok(success, [t 'success']);
0291     t_is(f, f_soln, 3, [t 'f']);
0292     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0293     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0294     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0295     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0296     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0297     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0298     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0299     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0300     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0301     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0302 
0303     %%-----  test OPF with angle difference limits  -----
0304     mpc = loadcase('t_case9_opfv2');
0305     %% remove capability curves
0306     mpc.gen(2:3, [PC1, PC2, QC1MIN, QC1MAX, QC2MIN, QC2MAX]) = zeros(2,6);
0307 
0308     %% get solved AC OPF case from MAT-file
0309     load soln9_opf_ang;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0310     
0311     %% run OPF with angle difference limits
0312     t = [t0 'w/angle difference limits : '];
0313     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(mpc, mpopt);
0314     t_ok(success, [t 'success']);
0315     t_is(f, f_soln, 3, [t 'f']);
0316     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0317     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0318     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0319     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  1, [t 'bus mu']);
0320     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0321     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0322     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0323     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0324     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0325     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0326     t_is(branch(:,ibr_angmu ), branch_soln(:,ibr_angmu ),  2, [t 'branch angle mu']);
0327 
0328     %%-----  test OPF with ignored angle difference limits  -----
0329     %% get solved AC OPF case from MAT-file
0330     load soln9_opf;   %% defines bus_soln, gen_soln, branch_soln, f_soln
0331 
0332     %% run OPF with ignored angle difference limits
0333     t = [t0 'w/ignored angle difference limits : '];
0334     mpopt1 = mpoption(mpopt, 'opf.ignore_angle_lim', 1);
0335     [baseMVA, bus, gen, gencost, branch, f, success, et] = runopf(mpc, mpopt1);
0336     %% ang limits are not in this solution data, so let's remove them
0337     branch(1, ANGMAX) = 360;
0338     branch(9, ANGMIN) = -360;
0339     t_ok(success, [t 'success']);
0340     t_is(f, f_soln, 3, [t 'f']);
0341     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0342     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0343     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0344     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0345     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0346     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0347     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0348     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0349     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0350     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0351 
0352     %% angle bounded above by 0, unbounded below
0353     %% for issue/18
0354     t = [t0 'w/angle difference limit = 0 : '];
0355     mpc = loadcase(casefile);
0356     b = 5;
0357     mpc.branch(b, ANGMAX) = 0;
0358     r = runopf(mpc, mpopt);
0359     t_ok(success, [t 'success']);
0360     diff = r.bus(r.branch(b, F_BUS), VA) - r.bus(r.branch(b, T_BUS), VA);
0361     t_is(diff, 0, 5, [t 'angle diff']);
0362 
0363     %%-----  OPF with ref bus not = bus 1, ref angle not = 0  -----
0364     t = [t0 'ref bus ~= 1, ref ang ~= 0 : '];
0365     mpc = loadcase(casefile);
0366     mpc.bus([1;3], BUS_TYPE) = [PV; REF];   %% swap bus types
0367     bus_soln([1;3], BUS_TYPE) = bus_soln([3;1], BUS_TYPE);   %% swap bus types
0368     mpc.bus(3, VA) = 3.3014277;
0369     r = runopf(mpc, mpopt);
0370     [success, f, bus, gen, branch] = deal(r.success, r.f, r.bus, r.gen, r.branch);
0371     t_ok(success, [t 'success']);
0372     t_is(f, f_soln, 3, [t 'f']);
0373     t_is(   bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0374     t_is(   bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0375     t_is(   bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0376     t_is(   bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0377     t_is(   gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0378     t_is(   gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0379     t_is(   gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0380     t_is(branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0381     t_is(branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0382     t_is(branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0383 
0384     %%-----  test OPF with opf.use_vg  -----
0385     %% get solved AC OPF case from MAT-file
0386     load soln9_opf_vg;  %% defines bus_soln, gen_soln, branch_soln, f_soln
0387                         %%    and bus_soln1, gen_soln1, branch_soln1, f_soln1
0388     
0389     %% run with opf.use_vg = 1
0390     t = [t0 'w/opf.use_vg = 1 : '];
0391     mpc = loadcase(casefile);
0392     mpc.gen = mpc.gen([1 2 1 3], :);
0393     mpc.gencost = mpc.gencost([1 2 1 3], :);
0394     mpc.gen([1 3], [PMAX PMIN]) = mpc.gen([1 3], [PMAX PMIN]) / 2;
0395     mpc.gen(3, [QMIN, QMAX]) = 0;   %% no reactive capability for gen 3
0396     mpc.gencost([1 3], COST:end) = mpc.gencost([1 3], COST:end) / 2;
0397     mpc.gen(1, VG) = 1.05;
0398     mpc.gen(3, VG) = 1.06;
0399     mpopt1 = mpoption(mpopt, 'opf.use_vg', 1);
0400     r = runopf(mpc, mpopt1);
0401     t_ok(r.success, [t 'success']);
0402     t_is(r.f, f_soln, 3, [t 'f']);
0403     t_is(   r.bus(:,ib_data   ),    bus_soln(:,ib_data   ), 10, [t 'bus data']);
0404     t_is(   r.bus(:,ib_voltage),    bus_soln(:,ib_voltage),  3, [t 'bus voltage']);
0405     t_is(   r.bus(:,ib_lam    ),    bus_soln(:,ib_lam    ),  3, [t 'bus lambda']);
0406     t_is(   r.bus(:,ib_mu     ),    bus_soln(:,ib_mu     ),  2, [t 'bus mu']);
0407     t_is(   r.gen(:,ig_data   ),    gen_soln(:,ig_data   ), 10, [t 'gen data']);
0408     t_is(   r.gen(:,ig_disp   ),    gen_soln(:,ig_disp   ),  3, [t 'gen dispatch']);
0409     t_is(   r.gen(:,ig_mu     ),    gen_soln(:,ig_mu     ),  3, [t 'gen mu']);
0410     t_is(r.branch(:,ibr_data  ), branch_soln(:,ibr_data  ), 10, [t 'branch data']);
0411     t_is(r.branch(:,ibr_flow  ), branch_soln(:,ibr_flow  ),  3, [t 'branch flow']);
0412     t_is(r.branch(:,ibr_mu    ), branch_soln(:,ibr_mu    ),  2, [t 'branch mu']);
0413 
0414     %% run with opf.use_vg = 0.9
0415     t = [t0 'w/opf.use_vg = 0.9 : '];
0416     mpopt1 = mpoption(mpopt, 'opf.use_vg', 0.9);
0417     r = runopf(mpc, mpopt1);
0418     t_ok(r.success, [t 'success']);
0419     t_is(r.f, f_soln1, 3, [t 'f']);
0420     t_is(   r.bus(:,ib_data   ),    bus_soln1(:,ib_data   ), 10, [t 'bus data']);
0421     t_is(   r.bus(:,ib_voltage),    bus_soln1(:,ib_voltage),  3, [t 'bus voltage']);
0422     t_is(   r.bus(:,ib_lam    ),    bus_soln1(:,ib_lam    ),  3, [t 'bus lambda']);
0423     t_is(   r.bus(:,ib_mu     ),    bus_soln1(:,ib_mu     ),  2, [t 'bus mu']);
0424     t_is(   r.gen(:,ig_data   ),    gen_soln1(:,ig_data   ), 10, [t 'gen data']);
0425     t_is(   r.gen(:,ig_disp   ),    gen_soln1(:,ig_disp   ),  3, [t 'gen dispatch']);
0426     t_is(   r.gen(:,ig_mu     ),    gen_soln1(:,ig_mu     ),  3, [t 'gen mu']);
0427     t_is(r.branch(:,ibr_data  ), branch_soln1(:,ibr_data  ), 10, [t 'branch data']);
0428     t_is(r.branch(:,ibr_flow  ), branch_soln1(:,ibr_flow  ),  3, [t 'branch flow']);
0429     t_is(r.branch(:,ibr_mu    ), branch_soln1(:,ibr_mu    ),  2, [t 'branch mu']);
0430 
0431     t = [t0 'hi-deg polynomial costs : '];
0432     mpc = loadcase(casefile);
0433     mpc.gencost = [
0434         2   1500    0   6   1e-6/5  0   0   0   0   0;
0435         2   3000    0   5   1e-4/4  0   0   0   0   0;
0436         2   2000    0   3   1/2     0   0   0   0   0;
0437     ];
0438     r = runopf(mpc, mpopt);
0439     [f, bus, gen, branch] = deal(r.f, r.bus, r.gen, r.branch);
0440     t_ok(r.success, [t 'success']);
0441     t_is(f, 11899.4652, 4, [t 'f']);
0442     t_is(gen(:, PG), [100.703628; 88.719864; 128.679485], 5, [t 'Pg']);
0443     t_is([min(bus(:, VM)) mean(bus(:, VM)) max(bus(:, VM))], ...
0444         [1.059191 1.079404 1.1], 5, [t 'bus voltage']);
0445 
0446     %% OPF with user-defined nonlinear constraints
0447     t = [t0 'w/nonlin eq constraint : '];
0448     mpc = loadcase('case30');
0449     mpc.user_constraints.nle = {
0450         {'Pg_usr', 1, 'opf_nle_fcn1', 'opf_nle_hess1', {'Pg'}, {}}
0451     };
0452     r = runopf(mpc, mpopt);
0453     t_ok(r.success, [t 'success']);
0454     t_is(r.gen(1, PG) * r.gen(2, PG) / 100, r.gen(6, PG), 8, t);
0455     t_is(r.gen(6, PG), 20.751163, 5, t);
0456 
0457     %% OPF with all buses isolated
0458     t = [t0 'all buses isolated : '];
0459     mpc.bus(:, BUS_TYPE) = NONE;
0460     try
0461         r = runopf(mpc, mpopt);
0462         t_is(r.success, 0, 12, [t 'success = 0']);
0463     catch
0464         t_ok(0, [t 'unexpected fatal error']);
0465     end
0466 
0467     %% OPF with no branch limits
0468     t = [t0 'w/no branch limits : '];
0469     mpc = loadcase(casefile);
0470     mpc.branch(:, RATE_A) = 0;
0471     r = runopf(mpc, mpopt);
0472     t_ok(r.success, [t 'success']);
0473     t_is(r.f, 5496.128635, 4, [t 'f']);
0474     t_is(r.gen(:, PG), [90; 220.463932; 10], 5, [t 'Pg']);
0475     t_is([min(r.bus(:, VM)) mean(r.bus(:, VM)) max(r.bus(:, VM))], ...
0476         [1.070692 1.090449 1.1], 5, [t 'bus voltage']);
0477 end
0478 
0479 if have_fcn('octave')
0480     warning(s1.state, file_in_path_warn_id);
0481 end
0482 
0483 t_end;

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005