Home > matpower7.1 > lib > t > t_opf_dc_osqp.m

t_opf_dc_osqp

PURPOSE ^

T_OPF_DC_OSQP Tests for DC optimal power flow using OSQP solver.

SYNOPSIS ^

function t_opf_dc_osqp(quiet)

DESCRIPTION ^

T_OPF_DC_OSQP  Tests for DC optimal power flow using OSQP solver.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_opf_dc_osqp(quiet)
0002 %T_OPF_DC_OSQP  Tests for DC optimal power flow using OSQP solver.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2004-2016, 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 num_tests = 24;
0017 
0018 t_begin(num_tests, quiet);
0019 
0020 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0021     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0022 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0023     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0024     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0025 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0026     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0027     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0028 
0029 casefile = 't_case9_opf';
0030 if quiet
0031     verbose = 0;
0032 else
0033     verbose = 0;
0034 end
0035 if have_feature('octave')
0036     if have_feature('octave', 'vnum') >= 4
0037         file_in_path_warn_id = 'Octave:data-file-in-path';
0038     else
0039         file_in_path_warn_id = 'Octave:load-file-in-path';
0040     end
0041     s1 = warning('query', file_in_path_warn_id);
0042     warning('off', file_in_path_warn_id);
0043 end
0044 
0045 t0 = 'DC OPF (OSQP): ';
0046 mpopt = mpoption('out.all', 0, 'verbose', verbose);
0047 mpopt = mpoption(mpopt, 'opf.dc.solver', 'OSQP');
0048 osqp_opts = struct( ...
0049         'eps_rel', 5e-4, ...
0050         'rho', 0.01, ...
0051         'adaptive_rho_interval', 1000, ...
0052         'max_iter', 20000, ...
0053         'scaling', 1 ...
0054     );
0055 mpopt = mpoption(mpopt, 'osqp.opts', osqp_opts);
0056 
0057 if have_feature('octave')
0058     sing_matrix_warn_id = 'Octave:singular-matrix';
0059 else
0060     sing_matrix_warn_id = 'MATLAB:singularMatrix';
0061 end
0062 s2 = warning('query', sing_matrix_warn_id);
0063 
0064 %% set up indices
0065 ib_data     = [1:BUS_AREA BASE_KV:VMIN];
0066 ib_voltage  = [VM VA];
0067 ib_lam      = [LAM_P LAM_Q];
0068 ib_mu       = [MU_VMAX MU_VMIN];
0069 ig_data     = [GEN_BUS QMAX QMIN MBASE:APF];
0070 ig_disp     = [PG QG VG];
0071 ig_mu       = (MU_PMAX:MU_QMIN);
0072 ibr_data    = (1:ANGMAX);
0073 ibr_flow    = (PF:QT);
0074 ibr_mu      = [MU_SF MU_ST];
0075 ibr_angmu   = [MU_ANGMIN MU_ANGMAX];
0076 
0077 %% get solved DC power flow case from MAT-file
0078 load soln9_dcopf;       %% defines bus_soln, gen_soln, branch_soln, f_soln
0079 
0080 %% run OPF
0081 t = t0;
0082 [baseMVA, bus, gen, gencost, branch, f, success, et] = rundcopf(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 
0096 %%-----  run OPF with extra linear user constraints & costs  -----
0097 %% two new z variables
0098 %%      0 <= z1, P3 - P1 <= z1
0099 %%      0 <= z2, P3 - P2 <= z2
0100 %% with A and N sized for DC opf
0101 mpc = loadcase(casefile);
0102 mpc.A = sparse([1;1;1;2;2;2],[10;12;13;12;11;14],[-1;1;-1;1;-1;-1],2,14);
0103 mpc.u = [0; 0];
0104 mpc.l = [-Inf; -Inf];
0105 mpc.zl = [0; 0];
0106 
0107 mpc.N = sparse([1;2], [13;14], [1;1], 2, 14);   %% new z variables only
0108 mpc.fparm = ones(2,1) * [1 0 0 1];              %% w = r = z
0109 mpc.H = sparse(2,2);                            %% no quadratic term
0110 mpc.Cw = [1000;1];
0111 
0112 t = [t0 'w/extra constraints & costs 1 : '];
0113 [r, success] = rundcopf(mpc, mpopt);
0114 t_ok(success, [t 'success']);
0115 t_is(r.gen(1, PG), 116.15974, 4, [t 'Pg1 = 116.15974']);
0116 t_is(r.gen(3, PG), 116.15974, 4, [t 'Pg3 = 116.15974']);
0117 t_is(r.var.val.z, [0; 0.3348], 4, [t 'user vars']);
0118 t_is(r.cost.usr, 0.3348, 3, [t 'user costs']);
0119 
0120 %% with A and N sized for AC opf
0121 mpc = loadcase(casefile);
0122 mpc.A = sparse([1;1;1;2;2;2],[19;21;25;21;20;26],[-1;1;-1;1;-1;-1],2,26);
0123 mpc.u = [0; 0];
0124 mpc.l = [-Inf; -Inf];
0125 mpc.zl = [0; 0];
0126 
0127 mpc.N = sparse([1;2], [25;26], [1;1], 2, 26);   %% new z variables only
0128 mpc.fparm = ones(2,1) * [1 0 0 1];              %% w = r = z
0129 mpc.H = sparse(2,2);                            %% no quadratic term
0130 mpc.Cw = [1000;1];
0131 
0132 t = [t0 'w/extra constraints & costs 2 : '];
0133 [r, success] = rundcopf(mpc, mpopt);
0134 t_ok(success, [t 'success']);
0135 t_is(r.gen(1, PG), 116.15974, 4, [t 'Pg1 = 116.15974']);
0136 t_is(r.gen(3, PG), 116.15974, 4, [t 'Pg3 = 116.15974']);
0137 t_is(r.var.val.z, [0; 0.3348], 4, [t 'user vars']);
0138 t_is(r.cost.usr, 0.3348, 3, [t 'user costs']);
0139 
0140 t = [t0 'infeasible : '];
0141 warning('off', sing_matrix_warn_id);
0142 %% with A and N sized for DC opf
0143 mpc = loadcase(casefile);
0144 mpc.A = sparse([1;1], [10;11], [1;1], 1, 14);   %% Pg1 + Pg2
0145 mpc.u = Inf;
0146 mpc.l = 600;
0147 [r, success] = rundcopf(mpc, mpopt);
0148 t_ok(~success, [t 'no success']);
0149 
0150 %% OPF with all buses isolated
0151 t = [t0 'all buses isolated : '];
0152 mpc = loadcase(casefile);
0153 mpc.bus(:, BUS_TYPE) = NONE;
0154 try
0155     r = rundcopf(mpc, mpopt);
0156     t_is(r.success, 0, 12, [t 'success = 0']);
0157 catch
0158     t_ok(0, [t 'unexpected fatal error']);
0159 end
0160 
0161 if have_feature('octave')
0162     warning(s1.state, file_in_path_warn_id);
0163 end
0164 warning(s2.state, sing_matrix_warn_id);
0165 
0166 t_end;

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005