Professional Documents
Culture Documents
% Project # 2
% vitthal patnecha
% MBM enineering college ,ME part time
%single line outage contingency analysis programe
%--------------------------------------------------------------------------
% Plan for Branch Out of Service
%--------------------------------------------------------------------------
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.');
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;
branchnew = branch;
branchnew(branchout,:)=[];
end;
end;
branchout = branchout2;
%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix
%--------------------------------------------------------------------------
alg = 2; % BX Method
[Bp, Bpp] = makeB(baseMVA, bus, branchnew, alg);
%--------------------------------------------------------------------------
% LODF Factors
%--------------------------------------------------------------------------
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
%--------------------------------------------------------------------------
[V,Va] = Pit(V,Ybus,Sbus,pv,pq,bus,Bp,Vm,Va);
%--------------------------------------------------------------------------
% 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;
D = size(baseMW);
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;
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
% 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
%----------------------------------------------------
% 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;
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 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
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Setup Base Case Complex Voltage without Generator Outage
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% 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
%--------------------------------------------------------------------------
%-------------------------------------------------------------------------
% 1P half-iteration
%-------------------------------------------------------------------------
[V,Va] = Pit(V,Ybus,Sbus,pv,pq,bus,Bp,Vm,Va);
[V] = Qit(V,Ybus,Sbus,pv,pq,bus,Bpp,Vm,Va);
updatedvalues1Q = [bus(:,1) abs(V) angle(V)]
%-------------------------------------------------------------------------
% Compute Branch Flows
%-------------------------------------------------------------------------
%--------------------------------------------------------------------------
% 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');
%-------------------------------------------------------------------------
% Compute Branch Flows
%-------------------------------------------------------------------------
%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
%--------------------------------------------------------------------------
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.');
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,:)=[];
%--------------------------------------------------------------------------
% Compute the Real Power MisMatch
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% 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
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
%--------------------------------------------------------------------------
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
for k=1:D(1) % loops through the number of pv buses to eliminate them from
the bpp matrix no 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;
end;
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;
D = size(pv);
pv2 = pv;
D2 = size(Vmtemp2);
Vmtemp3 = zeros(D2(1)-1,1);
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
%-------------------------------------------------------------------------
%-------------------------------------------------------------------------
% 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
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.');
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;
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);