Home > matpower5.1 > t > t_get_losses.m

t_get_losses

PURPOSE ^

T_GET_LOSSES Tests for code in GET_LOSSES.

SYNOPSIS ^

function t_get_losses(quiet)

DESCRIPTION ^

T_GET_LOSSES  Tests for code in GET_LOSSES.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_get_losses(quiet)
0002 %T_GET_LOSSES  Tests for code in GET_LOSSES.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2014-2015 by Power System Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   $Id: t_get_losses.m 2662 2015-03-20 20:02:08Z ray $
0009 %
0010 %   This file is part of MATPOWER.
0011 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0012 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0013 
0014 if nargin < 1
0015     quiet = 0;
0016 end
0017 
0018 n_tests = 20;
0019 
0020 t_begin(n_tests, quiet);
0021 
0022 %% define named indices into data matrices
0023 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0024     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
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_fcn('octave')
0036     if have_fcn('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 mpopt = mpoption('opf.violation', 1e-6, 'mips.gradtol', 1e-8, ...
0046         'mips.comptol', 1e-8, 'mips.costtol', 1e-9);
0047 mpopt = mpoption(mpopt, 'out.all', 0, 'verbose', verbose, 'opf.ac.solver', 'MIPS');
0048 mpc = loadcase(casefile);
0049 mpc.branch(8, TAP) = 0.95;
0050 mpc.branch(4, SHIFT) = -2;
0051 r = runopf(mpc, mpopt);
0052 p_loss = [0; 0.250456; 0.944337; 0; 0.151545; 0.298009; 0; 1.600081; 0.216618];
0053 q_loss = [3.890347; 1.355409; 4.116342; 5.017914; 1.283672; 2.524309; 10.20216; 8.050407; 1.841254];
0054 the_fchg = [0; 9.476543; 20.751015; 0; 12.113509; 8.563392; 0; 20.056039; 10.431575];
0055 the_tchg = [0; 9.158269; 20.749455; 0; 12.011738; 8.813679; 0; 18.136716; 10.556150];
0056 
0057 %%-----  all load  -----
0058 t = 'loss = get_losses(results) : ';
0059 loss = get_losses(r);
0060 t_is(real(loss), p_loss, 6, [t 'P loss']);
0061 t_is(imag(loss), q_loss, 6, [t 'Q loss']);
0062 
0063 t = 'loss = get_losses(baseMVA, bus, branch) : ';
0064 loss = get_losses(r.baseMVA, r.bus, r.branch);
0065 t_is(real(loss), p_loss, 6, [t 'P loss']);
0066 t_is(imag(loss), q_loss, 6, [t 'Q loss']);
0067 
0068 t = '[loss, chg, chg] = get_losses(results) : ';
0069 [loss, chg] = get_losses(r);
0070 t_is(real(loss), p_loss, 6, [t 'P loss']);
0071 t_is(imag(loss), q_loss, 6, [t 'Q loss']);
0072 t_is(chg, the_fchg+the_tchg, 6, [t 'Q inj (total)']);
0073 
0074 t = '[loss, fchg, tchg] = get_losses(results) : ';
0075 [loss, fchg, tchg] = get_losses(r);
0076 t_is(real(loss), p_loss, 6, [t 'P loss']);
0077 t_is(imag(loss), q_loss, 6, [t 'Q loss']);
0078 t_is(fchg, the_fchg, 6, [t 'Q inj (from)']);
0079 t_is(tchg, the_tchg, 6, [t 'Q inj (to)']);
0080 t_is(real(loss), r.branch(:, PF)+r.branch(:, PT), 12, [t 'P_loss = Pf+Pt']);
0081 t_is(imag(loss), r.branch(:, QF)+r.branch(:, QT)+tchg+fchg, 12, [t 'Q_loss = Qf+Qt+fchg+tchg']);
0082 
0083 
0084 t = '[loss, fchg, tchg, dloss_dV, dchg_dVm] = get_losses(r) : ';
0085 [loss, fchg, tchg, dloss_dV, dchg_dVm] = get_losses(r);
0086 epsilon = 1e-8;
0087 %% build numerical versions of dloss_dVa and dloss_dVm
0088 nb = size(r.bus, 1);    %% number of buses
0089 nl = size(r.branch, 1); %% number of branches
0090 dloss_dVa = sparse(nl, nb);
0091 dloss_dVm = sparse(nl, nb);
0092 dfchg_dVm = sparse(nl, nb);
0093 dtchg_dVm = sparse(nl, nb);
0094 for j = 1:nb
0095     mpc = r;
0096     mpc.bus(j, VA) = mpc.bus(j, VA) + 180 / pi * epsilon;
0097     loss2 = get_losses(mpc);
0098     dloss_dVa(:, j) = (loss2 - loss) / epsilon;
0099 end
0100 for j = 1:nb
0101     mpc = r;
0102     mpc.bus(j, VM) = mpc.bus(j, VM) + epsilon;
0103     [loss2, fchg2, tchg2] = get_losses(mpc);
0104     dloss_dVm(:, j) = (loss2 - loss) / epsilon;
0105     dfchg_dVm(:, j) = (fchg2 - fchg) / epsilon;
0106     dtchg_dVm(:, j) = (tchg2 - tchg) / epsilon;
0107 end
0108 t_is(full(real(dloss_dV.a)), full(real(dloss_dVa)), 5, [t 'dPloss/dVa']);
0109 t_is(full(imag(dloss_dV.a)), full(imag(dloss_dVa)), 4, [t 'dQloss/dVa']);
0110 t_is(full(real(dloss_dV.m)), full(real(dloss_dVm)), 5, [t 'dPloss/dVm']);
0111 t_is(full(imag(dloss_dV.m)), full(imag(dloss_dVm)), 4, [t 'dQloss/dVm']);
0112 t_is(full(dchg_dVm.f), full(dfchg_dVm), 5, [t 'dfchg/dVm']);
0113 t_is(full(dchg_dVm.t), full(dtchg_dVm), 5, [t 'dtchg/dVm']);
0114 
0115 t = 'Loss Sensitivity Factors (LSF)';
0116 %% convert to internal indexing to use makeJac()
0117 ri = ext2int(r);            %% results with internal indexing
0118 [loss, fchg, tchg, dloss_dV] = get_losses(ri);
0119 nb = size(ri.bus, 1);
0120 nl = size(ri.branch, 1);
0121 [ref, pv, pq] = bustypes(ri.bus, ri.gen);
0122 J = makeJac(ri);
0123 dL = real([dloss_dV.a(:, [pv;pq]) dloss_dV.m(:, pq)]) / ri.baseMVA;
0124 LSFi = zeros(nl, 2*nb);
0125 LSFi(:, [pv; pq; nb+pq]) = dL / J;  %% loss sensitivity factors in internal indexing
0126 
0127 %% convert to external indexing
0128 nb = size(r.bus, 1);
0129 nl = size(r.branch, 1);
0130 LSF = zeros(nl, 2*nb);
0131 LSF(ri.order.branch.status.on, [ri.order.bus.status.on; nb+ri.order.bus.status.on]) = LSFi;
0132 
0133 numLSF = zeros(nl, 2*nb);
0134 epsilon = 1e-4;
0135 mpopt = mpoption('out.all', 0, 'verbose', 0, 'pf.tol', 1e-12);
0136 for j = 1:nb
0137     mpc = r;
0138     mpc.bus(j, PD) = mpc.bus(j, PD) - epsilon;
0139     rr = runpf(mpc, mpopt);
0140     loss2 = get_losses(rr);
0141     numLSF(:, j) = real(loss2 - loss) / epsilon;
0142     
0143     mpc = r;
0144     mpc.bus(j, QD) = mpc.bus(j, QD) - epsilon;
0145     rr = runpf(mpc, mpopt);
0146     loss2 = get_losses(rr);
0147     numLSF(:, nb+j) = real(loss2 - loss) / epsilon;
0148 end
0149 t_is(LSF, numLSF, 1e-6, t);
0150 
0151 % norm(numLSF - LSF, Inf)
0152 % norm(numLSF(:, 1:nb) - LSF(:, 1:nb), Inf)
0153 
0154 % LSF
0155 % numLSF
0156 
0157 % sum(LSF)
0158 % sum(numLSF)
0159 
0160 % norm(sum(numLSF) - sum(LSF), Inf)
0161 
0162 t_end;

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005