0001 function [MVAbase, bus, gen, branch, success, et] = runse(casedata, mpopt, fname, solvedcase)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0023 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0024 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0025 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0026 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0027 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0028 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0029 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0030
0031
0032 if nargin < 4
0033 solvedcase = '';
0034 if nargin < 3
0035 fname = '';
0036 if nargin < 2
0037 mpopt = mpoption;
0038 if nargin < 1
0039 casedata = 'case9';
0040 end
0041 end
0042 end
0043 end
0044
0045
0046 dc = strcmp(upper(mpopt.model), 'DC');
0047
0048
0049 [baseMVA, bus, gen, branch] = loadcase(casedata);
0050 [i2e, bus, gen, branch] = ext2int(bus, gen, branch);
0051
0052
0053 [ref, pv, pq] = bustypes(bus, gen);
0054
0055
0056 on = find(gen(:, GEN_STATUS) > 0);
0057 gbus = gen(on, GEN_BUS);
0058
0059
0060 t0 = clock;
0061 if dc
0062
0063 Va0 = bus(:, VA) * (pi/180);
0064
0065
0066 [B, Bf, Pbusinj, Pfinj] = makeBdc(baseMVA, bus, branch);
0067
0068
0069
0070 Pbus = real(makeSbus(baseMVA, bus, gen)) - Pbusinj - bus(:, GS) / baseMVA;
0071
0072
0073 Va = dcpf(B, Pbus, Va0, ref, pv, pq);
0074
0075
0076 branch(:, [QF, QT]) = zeros(size(branch, 1), 2);
0077 branch(:, PF) = (Bf * Va + Pfinj) * baseMVA;
0078 branch(:, PT) = -branch(:, PF);
0079 bus(:, VM) = ones(size(bus, 1), 1);
0080 bus(:, VA) = Va * (180/pi);
0081
0082
0083
0084 refgen = find(gbus == ref);
0085 gen(on(refgen(1)), PG) = gen(on(refgen(1)), PG) + (B(ref, :) * Va - Pbus(ref)) * baseMVA;
0086
0087 success = 1;
0088 else
0089
0090
0091 V0 = bus(:, VM) .* exp(sqrt(-1) * pi/180 * bus(:, VA));
0092 V0(gbus) = gen(on, VG) ./ abs(V0(gbus)).* V0(gbus);
0093
0094
0095 [Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);
0096
0097
0098 Sbus = makeSbus(baseMVA, bus, gen);
0099
0100
0101 alg = upper(mpopt.pf.alg);
0102 switch alg
0103 case 'NR'
0104 [V, success, iterations] = newtonpf(Ybus, Sbus, V0, ref, pv, pq, mpopt);
0105 case {'FDXB', 'FDBX'}
0106 [Bp, Bpp] = makeB(baseMVA, bus, branch, alg);
0107 [V, success, iterations] = fdpf(Ybus, Sbus, V0, Bp, Bpp, ref, pv, pq, mpopt);
0108 case 'GS'
0109 [V, success, iterations] = gausspf(Ybus, Sbus, V0, ref, pv, pq, mpopt);
0110 otherwise
0111 error('Only Newton''s method, fast-decoupled, and Gauss-Seidel power flow algorithms currently implemented.');
0112 end
0113
0114
0115 [bus, gen, branch] = pfsoln(baseMVA, bus, gen, branch, Ybus, Yf, Yt, V, ref, pv, pq);
0116 end
0117 et = etime(clock, t0);
0118
0119
0120
0121 Pflf=branch(:,PF);
0122 Qflf=branch(:,QF);
0123 Ptlf=branch(:,PT);
0124 Qtlf=branch(:,QT);
0125 Sbuslf = V .* conj(Ybus * V);
0126 Vlf=V;
0127
0128
0129 [V, converged, i] = state_est(branch, Ybus, Yf, Yt, Sbuslf, Vlf, ref, pv, pq, mpopt);
0130
0131
0132
0133 Sbus = V .* conj(Ybus * V);
0134 bus(pq, PD) = -real(Sbus(pq)) * baseMVA;
0135 bus(pq, QD) = -imag(Sbus(pq)) * baseMVA;
0136
0137 on = find(gen(:, GEN_STATUS) > 0);
0138 gbus = gen(on, GEN_BUS);
0139 gen(on, PG) = real(Sbus(gbus)) * baseMVA + bus(gbus, PD);
0140
0141 [bus, gen, branch] = pfsoln(baseMVA, bus, gen, branch, Ybus, Yf, Yt, V, ref, pv, pq);
0142
0143
0144 Pfe=branch(:,PF);
0145 Qfe=branch(:,QF);
0146 Pte=branch(:,PT);
0147 Qte=branch(:,QT);
0148 nbr = length(Pfe);
0149 subplot(3,2,1), plot(180/pi*(angle(Vlf)-angle(V)),'.'), title('Voltage Angle (deg)');
0150 subplot(3,2,2), plot(abs(Vlf)-abs(V),'.'), title('Voltage Magnitude (p.u.)');
0151 subplot(3,2,3), plot((1:nbr),(Pfe-Pflf),'r.',(1:nbr),(Pte-Ptlf),'b.'), title('Real Flow (MW)');
0152 subplot(3,2,4), plot((1:nbr),(Qfe-Qflf),'r.',(1:nbr),(Qte-Qtlf),'b.'), title('Reactive Flow (MVAr)');
0153 subplot(3,2,5), plot(baseMVA*real(Sbuslf-Sbus), '.'), title('Real Injection (MW)');
0154 subplot(3,2,6), plot(baseMVA*imag(Sbuslf-Sbus), '.'), title('Reactive Injection (MVAr)');
0155
0156
0157
0158
0159 [bus, gen, branch] = int2ext(i2e, bus, gen, branch);
0160 if fname
0161 [fd, msg] = fopen(fname, 'at');
0162 if fd == -1
0163 error(msg);
0164 else
0165 if mpopt.out.all == 0
0166 printpf(baseMVA, bus, gen, branch, [], success, et, fd, ...
0167 mpoption(mpopt, 'out.all', -1));
0168 else
0169 printpf(baseMVA, bus, gen, branch, [], success, et, fd, mpopt);
0170 end
0171 fclose(fd);
0172 end
0173 end
0174 printpf(baseMVA, bus, gen, branch, [], success, et, 1, mpopt);
0175
0176
0177 if solvedcase
0178 savecase(solvedcase, baseMVA, bus, gen, branch);
0179 end
0180
0181
0182
0183 if nargout, MVAbase = baseMVA; end