Home > matpower5.0 > extras > state_estimator > runse.m

runse

PURPOSE ^

RUNSE Runs a state estimator.

SYNOPSIS ^

function [MVAbase, bus, gen, branch, success, et] = runse(casedata, mpopt, fname, solvedcase)

DESCRIPTION ^

RUNSE  Runs a state estimator.
   [BASEMVA, BUS, GEN, BRANCH, SUCCESS, ET] = ...
           RUNSE(CASEDATA, MPOPT, FNAME, SOLVEDCASE)

   Runs a state estimator (after a Newton power flow). Under construction with
   parts based on code from James S. Thorp.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [MVAbase, bus, gen, branch, success, et] = runse(casedata, mpopt, fname, solvedcase)
0002 %RUNSE  Runs a state estimator.
0003 %   [BASEMVA, BUS, GEN, BRANCH, SUCCESS, ET] = ...
0004 %           RUNSE(CASEDATA, MPOPT, FNAME, SOLVEDCASE)
0005 %
0006 %   Runs a state estimator (after a Newton power flow). Under construction with
0007 %   parts based on code from James S. Thorp.
0008 
0009 %   MATPOWER
0010 %   $Id: runse.m 2411 2014-11-04 20:13:56Z ray $
0011 %   by Ray Zimmerman, PSERC Cornell
0012 %   parts based on code by James S. Thorp, June 2004
0013 %   Copyright (c) 1996-2010 by Power System Engineering Research Center (PSERC)
0014 %
0015 %   This file is part of MATPOWER.
0016 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0017 %
0018 %   MATPOWER is free software: you can redistribute it and/or modify
0019 %   it under the terms of the GNU General Public License as published
0020 %   by the Free Software Foundation, either version 3 of the License,
0021 %   or (at your option) any later version.
0022 %
0023 %   MATPOWER is distributed in the hope that it will be useful,
0024 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0025 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0026 %   GNU General Public License for more details.
0027 %
0028 %   You should have received a copy of the GNU General Public License
0029 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0030 %
0031 %   Additional permission under GNU GPL version 3 section 7
0032 %
0033 %   If you modify MATPOWER, or any covered work, to interface with
0034 %   other modules (such as MATLAB code and MEX-files) available in a
0035 %   MATLAB(R) or comparable environment containing parts covered
0036 %   under other licensing terms, the licensors of MATPOWER grant
0037 %   you additional permission to convey the resulting work.
0038 
0039 %%-----  initialize  -----
0040 %% define named indices into bus, gen, branch matrices
0041 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0042     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0043 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0044     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0045     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
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 
0050 %% default arguments
0051 if nargin < 4
0052     solvedcase = '';                %% don't save solved case
0053     if nargin < 3
0054         fname = '';                 %% don't print results to a file
0055         if nargin < 2
0056             mpopt = mpoption;       %% use default options
0057             if nargin < 1
0058                 casedata = 'case9'; %% default data file is 'case9.m'
0059             end
0060         end
0061     end
0062 end
0063 
0064 %% options
0065 dc = strcmp(upper(mpopt.model), 'DC');  %% use DC formulation?
0066 
0067 %% read data & convert to internal bus numbering
0068 [baseMVA, bus, gen, branch] = loadcase(casedata);
0069 [i2e, bus, gen, branch] = ext2int(bus, gen, branch);
0070 
0071 %% get bus index lists of each type of bus
0072 [ref, pv, pq] = bustypes(bus, gen);
0073 
0074 %% generator info
0075 on = find(gen(:, GEN_STATUS) > 0);      %% which generators are on?
0076 gbus = gen(on, GEN_BUS);                %% what buses are they at?
0077 
0078 %%-----  run the power flow  -----
0079 t0 = clock;
0080 if dc                               %% DC formulation
0081     %% initial state
0082     Va0 = bus(:, VA) * (pi/180);
0083     
0084     %% build B matrices and phase shift injections
0085     [B, Bf, Pbusinj, Pfinj] = makeBdc(baseMVA, bus, branch);
0086     
0087     %% compute complex bus power injections (generation - load)
0088     %% adjusted for phase shifters and real shunts
0089     Pbus = real(makeSbus(baseMVA, bus, gen)) - Pbusinj - bus(:, GS) / baseMVA;
0090     
0091     %% "run" the power flow
0092     Va = dcpf(B, Pbus, Va0, ref, pv, pq);
0093     
0094     %% update data matrices with solution
0095     branch(:, [QF, QT]) = zeros(size(branch, 1), 2);
0096     branch(:, PF) = (Bf * Va + Pfinj) * baseMVA;
0097     branch(:, PT) = -branch(:, PF);
0098     bus(:, VM) = ones(size(bus, 1), 1);
0099     bus(:, VA) = Va * (180/pi);
0100     %% update Pg for swing generator (note: other gens at ref bus are accounted for in Pbus)
0101     %%      Pg = Pinj + Pload + Gs
0102     %%      newPg = oldPg + newPinj - oldPinj
0103     refgen = find(gbus == ref);             %% which is(are) the reference gen(s)?
0104     gen(on(refgen(1)), PG) = gen(on(refgen(1)), PG) + (B(ref, :) * Va - Pbus(ref)) * baseMVA;
0105     
0106     success = 1;
0107 else                                %% AC formulation
0108     %% initial state
0109     % V0    = ones(size(bus, 1), 1);            %% flat start
0110     V0  = bus(:, VM) .* exp(sqrt(-1) * pi/180 * bus(:, VA));
0111     V0(gbus) = gen(on, VG) ./ abs(V0(gbus)).* V0(gbus);
0112     
0113     %% build admittance matrices
0114     [Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);
0115     
0116     %% compute complex bus power injections (generation - load)
0117     Sbus = makeSbus(baseMVA, bus, gen);
0118     
0119     %% run the power flow
0120     alg = upper(mpopt.pf.alg);
0121     switch alg
0122         case 'NR'
0123             [V, success, iterations] = newtonpf(Ybus, Sbus, V0, ref, pv, pq, mpopt);
0124         case {'FDXB', 'FDBX'}
0125             [Bp, Bpp] = makeB(baseMVA, bus, branch, alg);
0126             [V, success, iterations] = fdpf(Ybus, Sbus, V0, Bp, Bpp, ref, pv, pq, mpopt);
0127         case 'GS'
0128             [V, success, iterations] = gausspf(Ybus, Sbus, V0, ref, pv, pq, mpopt);
0129         otherwise
0130             error('Only Newton''s method, fast-decoupled, and Gauss-Seidel power flow algorithms currently implemented.');
0131     end
0132     
0133     %% update data matrices with solution
0134     [bus, gen, branch] = pfsoln(baseMVA, bus, gen, branch, Ybus, Yf, Yt, V, ref, pv, pq);
0135 end
0136 et = etime(clock, t0);
0137 
0138 %%--------------------  begin state estimator code  --------------------
0139 %% save some values from load flow solution
0140 Pflf=branch(:,PF);
0141 Qflf=branch(:,QF);
0142 Ptlf=branch(:,PT);
0143 Qtlf=branch(:,QT);
0144 Sbuslf = V .* conj(Ybus * V);
0145 Vlf=V;
0146 
0147 %% run state estimator
0148 [V, converged, i] = state_est(branch, Ybus, Yf, Yt, Sbuslf, Vlf, ref, pv, pq, mpopt);
0149 
0150 %% update data matrices to match estimator solution ...
0151 %% ... bus injections at PQ buses
0152 Sbus = V .* conj(Ybus * V);
0153 bus(pq, PD) = -real(Sbus(pq)) * baseMVA;
0154 bus(pq, QD) = -imag(Sbus(pq)) * baseMVA;
0155 %% ... gen outputs at PV buses
0156 on = find(gen(:, GEN_STATUS) > 0);      %% which generators are on?
0157 gbus = gen(on, GEN_BUS);                %% what buses are they at?
0158 gen(on, PG) = real(Sbus(gbus)) * baseMVA + bus(gbus, PD);   %% inj P + local Pd
0159 %% ... line flows, reference bus injections, etc.
0160 [bus, gen, branch] = pfsoln(baseMVA, bus, gen, branch, Ybus, Yf, Yt, V, ref, pv, pq);
0161 
0162 %% plot differences from load flow solution
0163 Pfe=branch(:,PF);
0164 Qfe=branch(:,QF);
0165 Pte=branch(:,PT);
0166 Qte=branch(:,QT);
0167 nbr = length(Pfe);
0168 subplot(3,2,1), plot(180/pi*(angle(Vlf)-angle(V)),'.'), title('Voltage Angle (deg)');
0169 subplot(3,2,2), plot(abs(Vlf)-abs(V),'.'), title('Voltage Magnitude (p.u.)');
0170 subplot(3,2,3), plot((1:nbr),(Pfe-Pflf),'r.',(1:nbr),(Pte-Ptlf),'b.'), title('Real Flow (MW)');
0171 subplot(3,2,4), plot((1:nbr),(Qfe-Qflf),'r.',(1:nbr),(Qte-Qtlf),'b.'), title('Reactive Flow (MVAr)');
0172 subplot(3,2,5), plot(baseMVA*real(Sbuslf-Sbus), '.'), title('Real Injection (MW)');
0173 subplot(3,2,6), plot(baseMVA*imag(Sbuslf-Sbus), '.'), title('Reactive Injection (MVAr)');
0174 %%--------------------  end state estimator code  --------------------
0175 
0176 %%-----  output results  -----
0177 %% convert back to original bus numbering & print results
0178 [bus, gen, branch] = int2ext(i2e, bus, gen, branch);
0179 if fname
0180     [fd, msg] = fopen(fname, 'at');
0181     if fd == -1
0182         error(msg);
0183     else
0184         if mpopt.out.all == 0
0185             printpf(baseMVA, bus, gen, branch, [], success, et, fd, ...
0186                 mpoption(mpopt, 'out.all', -1));
0187         else
0188             printpf(baseMVA, bus, gen, branch, [], success, et, fd, mpopt);
0189         end
0190         fclose(fd);
0191     end
0192 end
0193 printpf(baseMVA, bus, gen, branch, [], success, et, 1, mpopt);
0194 
0195 %% save solved case
0196 if solvedcase
0197     savecase(solvedcase, baseMVA, bus, gen, branch);
0198 end
0199 
0200 %% this is just to prevent it from printing baseMVA
0201 %% when called with no output arguments
0202 if nargout, MVAbase = baseMVA; end

Generated on Mon 26-Jan-2015 15:21:31 by m2html © 2005