0001 function [bus, gen, branch] = pfsoln(baseMVA, bus0, gen0, branch0, Ybus, Yf, Yt, V, ref, pv, pq)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0018 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0019 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0020 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0021 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0022 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0023 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0024 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0025
0026
0027 bus = bus0;
0028 gen = gen0;
0029 branch = branch0;
0030
0031
0032 bus(:, VM) = abs(V);
0033 bus(:, VA) = angle(V) * 180 / pi;
0034
0035
0036
0037 on = find(gen(:, GEN_STATUS) > 0 & ...
0038 bus(gen(:, GEN_BUS), BUS_TYPE) ~= PQ);
0039 off = find(gen(:, GEN_STATUS) <= 0);
0040 gbus = gen(on, GEN_BUS);
0041
0042
0043 Sbus = V(gbus) .* conj(Ybus(gbus, :) * V);
0044
0045
0046 gen(off, QG) = zeros(length(off), 1);
0047
0048 gen(on, QG) = imag(Sbus) * baseMVA + bus(gbus, QD);
0049
0050
0051
0052
0053
0054 if length(on) > 1
0055
0056 nb = size(bus, 1);
0057 ngon = size(on, 1);
0058 Cg = sparse((1:ngon)', gbus, ones(ngon, 1), ngon, nb);
0059
0060
0061 ngg = Cg * sum(Cg)';
0062 gen(on, QG) = gen(on, QG) ./ ngg;
0063
0064
0065 Cmin = sparse((1:ngon)', gbus, gen(on, QMIN), ngon, nb);
0066 Cmax = sparse((1:ngon)', gbus, gen(on, QMAX), ngon, nb);
0067 Qg_tot = Cg' * gen(on, QG);
0068 Qg_min = sum(Cmin)';
0069 Qg_max = sum(Cmax)';
0070 ig = find(Cg * Qg_min == Cg * Qg_max);
0071 Qg_save = gen(on(ig), QG);
0072 gen(on, QG) = gen(on, QMIN) + ...
0073 (Cg * ((Qg_tot - Qg_min)./(Qg_max - Qg_min + eps))) .* ...
0074 (gen(on, QMAX) - gen(on, QMIN));
0075 gen(on(ig), QG) = Qg_save;
0076 end
0077
0078
0079 for k = 1:length(ref)
0080 refgen = find(gbus == ref(k));
0081 gen(on(refgen(1)), PG) = real(Sbus(refgen(1))) * baseMVA ...
0082 + bus(ref(k), PD);
0083 if length(refgen) > 1
0084
0085 gen(on(refgen(1)), PG) = gen(on(refgen(1)), PG) ...
0086 - sum(gen(on(refgen(2:length(refgen))), PG));
0087 end
0088 end
0089
0090
0091 out = find(branch(:, BR_STATUS) == 0);
0092 br = find(branch(:, BR_STATUS));
0093 Sf = V(branch(br, F_BUS)) .* conj(Yf(br, :) * V) * baseMVA;
0094 St = V(branch(br, T_BUS)) .* conj(Yt(br, :) * V) * baseMVA;
0095 branch(br, [PF, QF, PT, QT]) = [real(Sf) imag(Sf) real(St) imag(St)];
0096 branch(out, [PF, QF, PT, QT]) = zeros(length(out), 4);