You are on page 1of 19

MATLAB PROGRAME LODF

% Project # 2
% vitthal patnecha
% MBM enineering college ,ME part time
%single line outage contingency analysis programe

% Determines Topology of the System


[baseMVA, bus, gen, branch, gencost] = case14;

% Determines the Swing Bus


D = size(bus);
swingbus = -1;
for i=1:D(1)
if bus(i,2) == 3;
swingbus = bus(i,1);
else
end;
end;
% swingbus is now a global variable storing the reference bus

%--------------------------------------------------------------------------
% Plan for Branch Out of Service
%--------------------------------------------------------------------------

% Print the Branch Topology

fprintf('\n=============================================');
fprintf('\n| Branch Setup Data |');
fprintf('\n=============================================');
fprintf('\n Branch # \t From Bus \t To Bus \t');
fprintf('\n -------- \t -------- \t -------- ');
D = size(branch);
for i=1:D
fprintf('\n \t%1.0f \t\t\t%1.0f \t\t\t%1.0f ', i, branch(i,1),
branch(i,2));
end;
fprintf('\n');
fprintf('\n');

% Ask for a branch to be taken out of service and perform error checking

branchout = -1;
while branchout <= 0 || branchout > 20
branchout = input('Enter the branch number to be taken out of service:');
fprintf('\n');
fprintf('Your have chosen branch number: ');
fprintf('%1.0f', branchout);
fprintf(' to be out of service.');

if branchout >=5 && branchout <= 20

else
fprintf('\n');
fprintf('Your have chosen an invalid branch number: ');
fprintf('\n');
branchout = input('Enter the branch number to be taken out of
service:');
fprintf('\n');
fprintf('Your have chosen branch number: ');
fprintf('%1.0f', branchout);
fprintf(' to be out of service.');
end;
end;

% Make a new branch data based on the outaged branch

branchnew = branch;
branchnew(branchout,:)=[];

% branchnew now contains the eliminated branch

% Formulate branch info on out of service


D = size(branch);
branchout2 = [0 0 0 ];
for i=1:D(1)
if i == branchout
branchout2 = [i branch(i,1) branch(i,2)];
else

end;
end;
branchout = branchout2;
%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix
%--------------------------------------------------------------------------

alg = 2; % BX Method
[Bp, Bpp] = makeB(baseMVA, bus, branchnew, alg);

%--------------------------------------------------------------------------
% LODF Factors
%--------------------------------------------------------------------------

[deltPflo,LODFvalues] = computeLODF(Bp, swingbus, branchout, branch);


fprintf('\n\n');

fprintf('\n=============================================');
fprintf('\n| Line Outage Distribution Factors |');
fprintf('\n=============================================');
fprintf('\n From Bus \tTo Bus \tValue ');
fprintf('\n -------- \t------ \t --------');
D = size(LODFvalues);
for i=1:D(1)
fprintf('\n %1.0f \t\t\t%1.0f \t\t\t%6f', LODFvalues(i,2),
LODFvalues(i,3), LODFvalues(i,4));
end;
%--------------------------------------------------------------------------
% Run a Fast-Decoupled Power Flow for the 14-Bus system
%--------------------------------------------------------------------------
options = mpoption('PF_ALG', 2);
[baseMVA, bus, gen, newbranch, success] = runpf('case14',options);

%--------------------------------------------------------------------------
% Setup Base Case Complex Voltage without Generator Outage
%--------------------------------------------------------------------------

Vm = bus(:,8); % This is the voltage magnitude column


Va = bus(:,9); % This is the column of voltage values
Va = Va.*pi/180; % Convert the bus angle to radians
V = Vm .* exp(sqrt(-1) * Va); % This is the complex voltage
Sbus = makeSbus(baseMVA, bus, gen);
[Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);
%--------------------------------------------------------------------------
% Retrieve bus type reference matrices
%--------------------------------------------------------------------------

[ref, pv, pq] = bustypes(bus, gen);


%-------------------------------------------------------------------------
% 1P half-iteration
%-------------------------------------------------------------------------

[V,Va] = Pit(V,Ybus,Sbus,pv,pq,bus,Bp,Vm,Va);

updatedvalues1P = [bus(:,1) abs(V) angle(V)]

%--------------------------------------------------------------------------
% 1Q half-iteration
%--------------------------------------------------------------------------

[V] = Qit(V,Ybus,Sbus,pv,pq,bus,Bpp,Vm,Va);
updatedvalues1Q = [bus(:,1) abs(V) angle(V)]

%--------------------------------------------------------------------------
% Post-Contingency Branch Flows
%--------------------------------------------------------------------------

branchtemp = [];
for i=1:20
branchtemp(i,1) = i;
end;

% Determine the base MW flows taken at the FROM Bus


baseMW = [branchtemp newbranch(:,1) newbranch(:,2) newbranch(:,14)];

% Determines the branch flows


[newbranchflows] = determineBranchFlows2(LODFvalues, baseMW, branchout);
fprintf('\n\n');
fprintf('\n=============================================');
fprintf('\n| Post-Contingency MW branch flows |');
fprintf('\n=============================================');
fprintf('\n \tBranch \tFrom Bus \t To Bus \t MW ');
fprintf('\n \t-------- \t-------- \t--------- \t--------');
D = size(newbranchflows);
for i=1:D(1)
fprintf('\n \t\t%1.0f \t\t\t%1.0f \t\t\t%1.0f \t\t%6f',
newbranchflows(i,1), newbranchflows(i,2), newbranchflows(i,3),
newbranchflows(i,4));
end

function [newbranchflows] = determineBranchFlows2(LODFvalues, baseMW,


branchout)
%determineBranchFlows2 Computes the Post-Contingency Branch Flows
% Description: Computes the Post-Contingency Branch Flows for a particular
% branch being outaged
% Inputs: LODFvalues (matrix of branch number, from bus, to bus, and LODF
factor), a
% 1X3 matrix called branchout [branchno frombus tobus], and the baseMW or
% pre-contingency branch flows
% Outputs: newbranchflows contains the branch number , from bus, to bus,
% and the post-contingency branch flow

D = size(baseMW);

% Determine the MW of the branchout

for i=1:D(1)
if i == branchout(1,1);
branchMW = baseMW(i,4);
else
end;
end;

newbranch = zeros(D(1),D(2));
for i=1:D(1)
newbranchflows(i,1) = baseMW(i,1);
newbranchflows(i,2) = baseMW(i,2);
newbranchflows(i,3) = baseMW(i,3);
newbranchflows(i,4) = baseMW(i,4) + LODFvalues(i,4)*branchMW;
end;

for i=1:D(1)
if i == branchout(1,1)
newbranchflows(i,4) = 0;
else
end;
end;

return;
function [deltPflo,LODFvalues] = computeLODF(Bp, swingbus, branchout, branch)
%computeLODF Computes the Line Outage Distribution Factors
% Description: Computes the LODF (line outage distribution factors) on each
% branch
% Inputs: Admittance Matrix (with the swing bus), swingbus identifier, a
% 1X3 matrix called branchout [branchno frombus tobus], and the branch
% matrix given from wcc9bus
% Outputs: deltPlflo is the LODF matrix without the outage bus eliminated
% LODFvalues contain the final values for the effect of a branch outage on
% all other branches

%------------------------------------
% Formulate the P Matrix
%------------------------------------

D = size(Bp);
P = zeros(D(1),1);
for i=1:D(1)
if i == branchout(1,2) % Add +1 to the from bus
P(i,1) = 1;
else
if i == branchout(1,3) % Add -1 to the to bus
P(i,1) = -1;
else
end;
end;
end;

% Reduce the matrix for the swing bus

Ptemp = zeros(D(1)-1,1);

for i=1:D(1)
if i < swingbus
Ptemp(i,1) = P(i,1) ;
else
if i > swingbus
Ptemp(i-1,1) = P(i,1);
else
end;
end;

end;
P = Ptemp;

%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix Minus Swing Bus
%--------------------------------------------------------------------------

bprimematrixnoswing = zeros(13,13);

for i=1:14
for j = 1:14
if (i ~= swingbus & j ~= swingbus)
if (i < swingbus & j < swingbus)
bprimematrixnoswing(i,j) = Bp(i,j);
else
if (i > swingbus & j < swingbus)
bprimematrixnoswing(i-1,j) = Bp(i,j);
else
if (i < swingbus & j > swingbus)
bprimematrixnoswing(i,j-1) = Bp(i,j);
else
if (i > swingbus & j > swingbus)
bprimematrixnoswing(i-1,j-1) = Bp(i,j);
else
end;
end;
end;
end;
end;
end;

end;
% bprimematrixnoswing holds the matrix with the swing bus removed

%----------------------------------------------------
% Solve for the Theta Values
%----------------------------------------------------
% P = B'*Theta

% Computes the Theta Matrix


thetavalues = bprimematrixnoswing\P;
D = size(thetavalues);
thetavalues2 = zeros(D(1)+1,1);
for i=1:D(1)+1
if i < swingbus
thetavalues2(i,1) = thetavalues(i,1);
else
if i > swingbus
thetavalues2(i,1) = thetavalues(i-1,1);
else
if i == swingbus
thetavalues2(i,1) = 0;
else
end;
end;
end;
end;

% this holds all the delta thetas for each bus including the zero value for
% the swing bus
thetavalues = thetavalues2;
D = size(thetavalues);
thetavalues2 = zeros(D(1),2);
for i=1:D(1)
for j=1:2
if j==1
thetavalues2(i,j) = i;
else
thetavalues2(i,j) = thetavalues(i,1);
end;
end;
end;

thetavalues = thetavalues2;
%----------------------------------------------------
% Calculates the LODF's
%----------------------------------------------------

% Find the Branch Series Reactance


D = size(branch);
branchr = [branch(:,1) zeros(D(1),1) branch(:,2) zeros(D(1),1) branch(:,4)];
D = size(branchr);
F = size(thetavalues);
LODFvalues = [branchr zeros(D(1),1)];

% This part assigns the theta values to the columns corresponding to the
% branch t, from
for i=1:D(1)
for m=1:F(1)
if LODFvalues(i,1) == thetavalues(m,1)
LODFvalues(i,2) = thetavalues(m,2);
else
if LODFvalues(i,3) == thetavalues(m,1)
LODFvalues(i,4) = thetavalues(m,2);
else
end;
end;
end;

end;

% This calculates the LODF in the final column

D = size(LODFvalues);

for i=1:D(1)
LODFvalues(i,9) = (LODFvalues(i,2) - LODFvalues(i,4))/LODFvalues(i,5);
end;

%-----------------------------------------------------------------------
% Final Results
%-----------------------------------------------------------------------

% This is the LODF (nbranch x 1) vector per the assignment


deltPflo = [LODFvalues(:,9)];
i = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]';

% This is the LODF (nbranch x 3) including the from bus, to bus, and LODF
LODFvalues = [i LODFvalues(:,1) LODFvalues(:,3) LODFvalues(:,9) ];

LODFvalues(branchout(1),4) = -1;

return;
Generator outage MATLAB program
%==========================================================================
% Project # 2
% vitthal patnecha
% MBM engineering college , ME part time
%==========================================================================

%--------------------------------------------------------------------------
% Run a Fast-Decoupled Power Flow for the 14-Bus system Base Case
%--------------------------------------------------------------------------
options = mpoption('PF_ALG', 2);
[baseMVA, bus, gen, branch, success] = runpf('case14',options);

%--------------------------------------------------------------------------
% Generator Outage - Ask for a generator to be out
%--------------------------------------------------------------------------

[gennew, genout] = genout(gen);


gen = gennew;

%--------------------------------------------------------------------------
% Setup Base Case Complex Voltage without Generator Outage
%--------------------------------------------------------------------------

Vm = bus(:,8); % This is the voltage magnitude column


Va = bus(:,9); % This is the column of voltage values
Va = Va.*pi/180; % Convert the bus angle to radians
V = Vm .* exp(sqrt(-1) * Va); % This is the complex voltage

%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix, Sbus, Ybus
%--------------------------------------------------------------------------
branch(genout,4) = 100000000000;
branch(genout,3) = 100000000000;
alg = 2; % BX Method
[Bp, Bpp] = makeB(baseMVA, bus, branch, alg);
Sbus = makeSbus(baseMVA, bus, gen);
[Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);

%--------------------------------------------------------------------------
% Retrieve bus type reference matrices
%--------------------------------------------------------------------------

[ref, pv, pq] = bustypes(bus, gen);

%-------------------------------------------------------------------------
% 1P half-iteration
%-------------------------------------------------------------------------

[V,Va] = Pit(V,Ybus,Sbus,pv,pq,bus,Bp,Vm,Va);

updatedvalues1P = [bus(:,1) abs(V) angle(V)]


%--------------------------------------------------------------------------
% 1Q half-iteration
%--------------------------------------------------------------------------

[V] = Qit(V,Ybus,Sbus,pv,pq,bus,Bpp,Vm,Va);
updatedvalues1Q = [bus(:,1) abs(V) angle(V)]

%-------------------------------------------------------------------------
% Compute Branch Flows
%-------------------------------------------------------------------------

[br, Sf, St] = computebranchflows(bus,gen,branch,V,Yf,Yt,baseMVA);

%--------------------------------------------------------------------------
% Display Branch Flows
%--------------------------------------------------------------------------

format short g;

D = size(Sf);
fprintf('\n');
fprintf('\n');

fprintf('\n==================================================================
===========================================================================')
;
fprintf('\n| Fast-Decoupled 1P1Q
Estimated Branch flows
|');

fprintf('\n==================================================================
===========================================================================')
;
fprintf('\n From Bus \t To Bus \t From Bus Real Power (MW) \t From Bus
Reactive Power (MVAR) \t To Bus Real Power (MW) \t To Bus Reactive Power
(MVAR)');
fprintf('\n --------- \t ------- \t ------------------------ \t ---------
--------------------- \t ---------------------- \t --------------------------
--');
for i=1:D(1)
fprintf('\n \t%1.0f \t\t\t%1.0f \t\t\t\t%6f \t\t\t\t\t%6f \t\t\t\t\t\t%6f
\t\t\t\t\t%6f', branch(i,1), branch(i,2), real(Sf(i,1)), imag(Sf(i,1)),
real(St(i,1)), imag(St(i,1)));
end;
fprintf('\n');
fprintf('\n');

function [br, Sf, St] = computebranchflows(bus,gen,branch,V,Yf,Yt,baseMVA)


% COMPUTEBRANCHFLOWS
% Input: bus data, gen data (without outaged generator), branch data,
% 1P1Q final complex V, the matrices Yf and Yt
% which, when multiplied by a complex voltage vector, yield the vector
% currents injected into each line from the "from" and "to" buses
% respectively of each line, baseMVA
% Outputs: branch numbers in service, complex power at the from bus,
% complex power at the bus

%-------------------------------------------------------------------------
% Compute Branch Flows
%-------------------------------------------------------------------------

%%----- initialize -----


%% define named indices into bus, gen, branch matrices
[PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, ...
RATE_C, TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST] = idx_brch;
[GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, ...
GEN_STATUS, PMAX, PMIN, MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = idx_gen;

%% read data & convert to internal bus numbering


[i2e, bus, gen, branch] = ext2int(bus, gen, branch);

%Branch Flows
br = find(branch(:, BR_STATUS));
Sf = V(branch(br, F_BUS)) .* conj(Yf(br, :) * V) * baseMVA; % complex power
at "from" bus
St = V(branch(br, T_BUS)) .* conj(Yt(br, :) * V) * baseMVA; % complex power
injected at "to" bus
return;
function [gennew, genout] = genout(gen)
%GENOUT Displays available generators to be taken out of service
% Input, the generator matrix
% Outputs, the gen matrix with the outaged generator removed, the outage
% generator

%--------------------------------------------------------------------------
% Plan for Generator Out of Service
%--------------------------------------------------------------------------

% Print the Generator Data

% Generator Data Format


% 1 bus number
% (-) (machine identifier, 0-9, A-Z)
% 2 Pg, real power output (MW)
% 3 Qg, reactive power output (MVAR)
% 4 Qmax, maximum reactive power output (MVAR)
% 5 Qmin, minimum reactive power output (MVAR)
% 6 Vg, voltage magnitude setpoint (p.u.)
% (-) (remote controlled bus index)
% 7 mBase, total MVA base of this machine, defaults to baseMVA
% (-) (machine impedance, p.u. on mBase)
% (-) (step up transformer impedance, p.u. on mBase)
% (-) (step up transformer off nominal turns ratio)
% 8 status, 1 - machine in service, 0 - machine out of service
% (-) (% of total VARS to come from this gen in order to hold V at
% remote bus controlled by several generators)
% 9 Pmax, maximum real power output (MW)
% 10 Pmin, minimum real power output (MW)

D = size(gen);
fprintf('\n');
fprintf('\n');

fprintf('\n==================================================================
=====================================================================');
fprintf('\n| Generator Setup Data
|');

fprintf('\n==================================================================
======================================================================');
fprintf('\n Bus Number # \t Pg real power output (MW) \t Qg reactive
power output (MVAR) \t Qmax \t Qmin \t Pmax \t Pmin \t
Status');
fprintf('\n ------------ \t ------------------------- \t ----------------
--------------- \t -------- \t -------- \t -------- \t -------- \t --------
');
for i=1:D(1)
fprintf('\n \t%1.0f \t\t\t\t\t%6f \t\t\t\t\t%6f \t\t\t\t\t%6f \t%6f \t%6f
\t%6f \t%6f', gen(i,1), gen(i,2), gen(i,3), gen(i,4), gen(i,5), gen(i,9),
gen(i,10), gen(i,8));
end;
fprintf('\n');
fprintf('\n');

% Ask for a generator to be taken out of service and perform error checking

genout = -1;
while genout <= 1 || genout > 5
genout = input('Enter the Generator number to be taken out of service:');
fprintf('\n');
fprintf('Your have chosen Generator number: ');
fprintf('%1.0f', genout);
fprintf(' to be out of service.');

if genout >=2 && genout <= 5

else
fprintf('\n');
fprintf('Your have chosen an invalid Generator number: ');
fprintf('\n');
genout = input('Enter the Generator number to be taken out of
service:');
fprintf('\n');
fprintf('Your have chosen Generator number: ');
fprintf('%1.0f', genout);
fprintf(' to be out of service.');
end;
end;
% Make a new generator data based on the outaged generator

gennew = gen;
gennew(genout,:)=[];

% gennew now contains the eliminated branch

function [V,Va,swingbus] = Pit(V,Ybus,Sbus,pv,pq,bus,Bp,Vm,Va)


%Pit Performs 1P
% Input: complex voltage V vector, Ybus, Sbus, pv, pq, bus, Bpp,Vm,Va
% Outputs:complex voltage V vector, the voltage angle Va, the swing bus

%--------------------------------------------------------------------------
% Compute the Real Power MisMatch
%--------------------------------------------------------------------------

mis = V .* conj(Ybus * V) - Sbus;


delP=-real(mis([pv; pq]));

%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix Minus Swing Bus
%--------------------------------------------------------------------------

% Determines the Swing Bus


D = size(bus);
swingbus = -1;
for i=1:D(1)
if bus(i,2) == 3;
swingbus = bus(i,1);
else
end;
end;

bprimematrixnoswing = zeros(13,13);

for i=1:14
for j = 1:14
if (i ~= swingbus & j ~= swingbus)
if (i < swingbus & j < swingbus)
bprimematrixnoswing(i,j) = Bp(i,j);
else
if (i > swingbus & j < swingbus)
bprimematrixnoswing(i-1,j) = Bp(i,j);
else
if (i < swingbus & j > swingbus)
bprimematrixnoswing(i,j-1) = Bp(i,j);
else
if (i > swingbus & j > swingbus)
bprimematrixnoswing(i-1,j-1) = Bp(i,j);
else
end;
end;
end;
end;
end;
end;

end;
% bprimematrixnoswing holds the matrix with the swing bus removed

% Reduce the Vm matrix for the swing bus

Vmtemp = zeros(13,1);

for i=1:14
if i < swingbus
Vmtemp(i,1) = Vm(i,1) ;
else
if i > swingbus
Vmtemp(i-1,1) = Vm(i,1);
else
end;
end;

end;

%-------------------------------------------------------------------------
% Computes the delTheta
%-------------------------------------------------------------------------
delTheta = bprimematrixnoswing\(delP./Vmtemp);

%-------------------------------------------------------------------------
% Augment the delTheta matrix for the swing bus
%-------------------------------------------------------------------------

delTheta2 = zeros(14,1);
for i=1:9
if i < swingbus
delTheta2(i,1) = delTheta(i,1);
else
if i > swingbus
delTheta2(i,1) = delTheta(i-1,1);
else
if i == swingbus
delTheta2(i,1) = 0;
else
end;
end;
end;
end;

delTheta = delTheta2;

%-------------------------------------------------------------------------
% Update Complex Voltage
%-------------------------------------------------------------------------
Va = Va + delTheta;
V = Vm .* exp(sqrt(-1) * Va); % This is the complex voltage
return;
function [V,swingbus] = Qit(V,Ybus,Sbus,pv,pq,bus,Bpp,Vm,Va)
%Pit Performs 1P for a generator disturbance
% Input: complex voltage V vector, Ybus, Sbus, pv, pq, bus, Bpp,Vm,Va
% Outputs:complex voltage V vector, the swing bus

%--------------------------------------------------------------------------
% Set-Up Bpp Matrix Minus Swing Bus Minus PV Buses
%--------------------------------------------------------------------------

% Determines the Swing Bus


D = size(bus);
swingbus = -1;
for i=1:D(1)
if bus(i,2) == 3;
swingbus = bus(i,1);
else
end;
end;

% This section removes only the swing bus

bpprimematrixnoswing = zeros(13,13);

for i=1:14
for j = 1:14
if (i ~= swingbus & j ~= swingbus)
if (i < swingbus & j < swingbus)
bpprimematrixnoswing(i,j) = Bpp(i,j);
else
if (i > swingbus & j < swingbus)
bpprimematrixnoswing(i-1,j) = Bpp(i,j);
else
if (i < swingbus & j > swingbus)
bpprimematrixnoswing(i,j-1) = Bpp(i,j);
else
if (i > swingbus & j > swingbus)
bpprimematrixnoswing(i-1,j-1) = Bpp(i,j);
else
end;
end;
end;
end;
end;
end;

end;
% bpprimematrixnoswing holds the matrix with the swing bus removed

% This section removes the pv buses

D = size(pv); % Determines how many pv buses there are to remove..


pv2 = pv; % used for the loop down below

for k=1:D(1) % loops through the number of pv buses to eliminate them from
the bpp matrix no swing

% this is needed to keep the generator pv index algined with the


% current bpprimematrixnoswing matrix index
for t=1:D(1)
if pv2(t) >= 2
pv2(t) = pv2(t)-1;
else
pv2(t) = 1;
end;
end;

D2 = size(bpprimematrixnoswing); % Determines the current dimensions of the


bpp matrix minus swing and minus loop pv bus
bpprimematrixnoswing2 = zeros(D2(1)-1,D(2)-1); % creates a temporary matrix
to store the new Bpp matrix minus loop pv minus swing

for i=1:D2(1) % for 1 to the size of the bpp matrix with the swing removed
and current loop pv bus removed.
for j = 1:D2(1)
if (i ~= pv2(k) & j ~= pv2(k)) % pv(k) is the pv bus being removed
from the Bpp matrix without the swing.
if (i < pv2(k) & j < pv2(k))
bpprimematrixnoswing2(i,j) = bpprimematrixnoswing(i,j);
else
if (i > pv2(k) & j < pv2(k))
bpprimematrixnoswing2(i-1,j) = bpprimematrixnoswing(i,j);
else
if (i < pv2(k) & j > pv2(k))
bpprimematrixnoswing2(i,j-1) = bpprimematrixnoswing(i,j);
else
if (i > pv2(k) & j > pv2(k))
bpprimematrixnoswing2(i-1,j-1) =
bpprimematrixnoswing(i,j);
else
end;
end;
end;
end;
end;
end;

end;

bpprimematrixnoswing = bpprimematrixnoswing2; % assigns the reduced bpp


matrix

end;

% Reduce the Va matrix for the swing bus


Vmtemp = zeros(13,1);

for i=1:14
if i < swingbus
Vmtemp(i,1) = Vm(i,1) ;
else
if i > swingbus
Vmtemp(i-1,1) = Vm(i,1);
else
end;
end;

end;

Vmtemp2 = Vmtemp;

% Reduces the Va matrix minus the swing for the pv buses

D = size(pv);
pv2 = pv;

for k=1:D(1) % loops for each bus

D2 = size(Vmtemp2);
Vmtemp3 = zeros(D2(1)-1,1);

% this is needed to keep the generator pv index algined with the


% current Va matrix index
for t=1:D(1)
if pv2(t) >= 2
pv2(t) = pv2(t)-1;
else
pv2(t) = 1;
end;
end;

for i=1:D2(1)
if i < pv2(k)
Vmtemp3(i,1) = Vmtemp2(i,1) ;
else
if i > pv2(k)
Vmtemp3(i-1,1) = Vmtemp2(i,1);
else
end;
end;
end;
Vmtemp2 = Vmtemp3;

end;

Vmtemp = Vmtemp2;

%-------------------------------------------------------------------------
% Compute the Reative Power MisMatch
%-------------------------------------------------------------------------

mis = V .* conj(Ybus * V) - Sbus;


delQ=-imag(mis(pq));

%-------------------------------------------------------------------------
% Computes the delVmag
%-------------------------------------------------------------------------
delVmag = bpprimematrixnoswing\(delQ./Vmtemp);

%-------------------------------------------------------------------------
% Augment the delVmag matrix for the swing bus and pv buses
%-------------------------------------------------------------------------

delVmag2 = zeros(14,1);
D = size(pv);
increment = 1;
for i=1:14
if i == swingbus
else
flag = 0; % determines if it is a pav bus to reconstruct
for k=1:D(1)
if pv(k) == i
flag = 1;
else
end
end

if flag == 1;
else
delVmag2(i) = delVmag(increment);
increment = increment + 1;
end
end
end;

delVmag = delVmag2;

%-------------------------------------------------------------------------
% Update Complex Voltage Values
%-------------------------------------------------------------------------
Vm = Vm + delVmag;
V = Vm .* exp(sqrt(-1) * Va); % This is the complex voltage

return;
%GENOUT Displays available generators to be taken out of service
% Input, the generator matrix
% Outputs, the gen matrix with the outaged generator removed, the outage
% generator

%--------------------------------------------------------------------------
% Plan for Generator Out of Service
%--------------------------------------------------------------------------
% Print the Generator Data

% Generator Data Format


% 1 bus number
% (-) (machine identifier, 0-9, A-Z)
% 2 Pg, real power output (MW)
% 3 Qg, reactive power output (MVAR)
% 4 Qmax, maximum reactive power output (MVAR)
% 5 Qmin, minimum reactive power output (MVAR)
% 6 Vg, voltage magnitude setpoint (p.u.)
% (-) (remote controlled bus index)
% 7 mBase, total MVA base of this machine, defaults to baseMVA
% (-) (machine impedance, p.u. on mBase)
% (-) (step up transformer impedance, p.u. on mBase)
% (-) (step up transformer off nominal turns ratio)
% 8 status, 1 - machine in service, 0 - machine out of service
% (-) (% of total VARS to come from this gen in order to hold V at
% remote bus controlled by several generators)
% 9 Pmax, maximum real power output (MW)
% 10 Pmin, minimum real power output (MW)

D = size(gen);
fprintf('\n');
fprintf('\n');

fprintf('\n==================================================================
=====================================================================');
fprintf('\n| Generator Setup Data
|');

fprintf('\n==================================================================
======================================================================');
fprintf('\n Bus Number # \t Pg real power output (MW) \t Qg reactive
power output (MVAR) \t Qmax \t Qmin \t Pmax \t Pmin \t
Status');
fprintf('\n ------------ \t ------------------------- \t ----------------
--------------- \t -------- \t -------- \t -------- \t -------- \t --------
');
for i=1:D(1)
fprintf('\n \t%1.0f \t\t\t\t\t%6f \t\t\t\t\t%6f \t\t\t\t\t%6f \t%6f \t%6f
\t%6f \t%6f', gen(i,1), gen(i,2), gen(i,3), gen(i,4), gen(i,5), gen(i,9),
gen(i,10), gen(i,8));
end;
fprintf('\n');
fprintf('\n');

% Ask for a generator to be taken out of service and perform error checking

genout = -1;
while genout <= 1 || genout > 5
genout = input('Enter the Generator number to be taken out of service:');
fprintf('\n');
fprintf('Your have chosen Generator number: ');
fprintf('%1.0f', genout);
fprintf(' to be out of service.');

if genout >=2 && genout <= 5

else
fprintf('\n');
fprintf('Your have chosen an invalid Generator number: ');
fprintf('\n');
genout = input('Enter the Generator number to be taken out of
service:');
fprintf('\n');
fprintf('Your have chosen Generator number: ');
fprintf('%1.0f', genout);
fprintf(' to be out of service.');
end;
end;

% Make a new generator data based on the outaged generator

gennew = gen;
gennew(genout,:)=[];
%--------------------------------------------------------------------------
% Run a Fast-Decoupled Power Flow for the 14-Bus system Base Case
%--------------------------------------------------------------------------
options = mpoption('PF_ALG', 2);
[baseMVA, bus, gen, branch, success] = runpf('case14',options);

You might also like