-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.m
229 lines (188 loc) · 7.65 KB
/
project1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
% Project # 1
% Kathleen Williams
% ECE 557
% Determines Topology of the System
[baseMVA, bus, gen, branch, area, gencost] = wscc9bus;
% 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
%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix
%--------------------------------------------------------------------------
%-------------------------------------------------------------------------
% Code for Computing the B-Prime Matrix Per Class Discussion on 09-15-2005
%-------------------------------------------------------------------------
alg = 2; % BX Method
[Bp, Bpp] = makeB(baseMVA, bus, branch, alg);
%--------------------------------------------------------------------------
% This is my original code for creating the bprime matrix from scratch
% This code is only specific for a BX Method
%--------------------------------------------------------------------------
temp = [branch(:,1) branch(:,2) branch(:,4)];
clear baseMVA, clear gen, clear branch, clear area, clear gencost;
D = size(temp);
%Initialize the B' Prime Matrix to Zero
for i=1:9
for j=1:9
bus1(i,j) = [0];
end;
end;
for i=1:9
for j=1:9
if (i == j)
% Busii
for m=1:9
if temp(m,1) == i | temp(m,2) == i
bus1(i,j) = bus1(i,j) + 1/temp(m,3);
else
end;
end;
else
% Busij
for k=1:9
if (temp(k,1) == i & temp(k,2) == j)
bus1(i,j) = -1/temp(k,3);
bus1(j,i) = -1/temp(k,3);
else
end;
end ;
end;
end;
end;
bprimematrix = bus1;
clear bus1, clear temp, clear i, clear j, clear l, clear k, clear m, clear bus, clear D;
% The B prime matrix is designated as bprimematrix variable
%--------------------------------------------------------------------------
% Set-Up B-Prime Matrix Minus Swing Bus
%--------------------------------------------------------------------------
bprimematrixnoswing = zeros(8,8);
for i=1:9
for j = 1:9
if (i ~= swingbus & j ~= swingbus)
if (i < swingbus & j < swingbus)
bprimematrixnoswing(i,j) = bprimematrix(i,j);
else
if (i > swingbus & j < swingbus)
bprimematrixnoswing(i-1,j) = bprimematrix(i,j);
else
if (i < swingbus & j > swingbus)
bprimematrixnoswing(i,j-1) = bprimematrix(i,j);
else
if (i > swingbus & j > swingbus)
bprimematrixnoswing(i-1,j-1) = bprimematrix(i,j);
else
end;
end;
end;
end;
end;
end;
end;
% bprimematrixnoswing holds the matrix with the swing bus removed
%--------------------------------------------------------------------------
% Normal Case Power Flow Using Fast-Decoupled
%--------------------------------------------------------------------------
% Set-up the options for Fast-Decoupled Power Flow
options = mpoption('PF_ALG', 2);
% Run a Fast-Decoupled Power Flow for the 9-Bus system
[baseMVA, bus, gen, branch, success] = runpf('wscc9bus',options);
%--------------------------------------------------------------------------
% Save the Branch Flows and Compute the Max Flow
%--------------------------------------------------------------------------
temp = [branch(:,1) branch(:,2) abs(branch(:,12)) abs(branch(:,14)) abs(branch(:,13)) abs(branch(:,15))];
D = size(temp);
temp2 = temp;
for i=1:D(1)
if temp(i,3) > temp(i,4)
temp(i,3) = temp(i,3);
else
if temp(i,3) < temp(i,4)
temp(i,3) = temp(i,4);
else
end;
end;
if temp(i,5) > temp(i,6)
temp(i,5) = temp(i,5);
else
if temp(i,5) < temp(i,6)
temp(i,5) = temp(i,6);
else
end;
end;
end;
temp3 = [branch(:,1) branch(:,2) temp(:,3) temp(:,5)];
branchpqflows = temp3;
clear temp, clear temp2, clear temp3, clear success, clear i, clear j, clear D;
D = size(branchpqflows);
%--------------------------------------------------------------------------
% Use the From Bus as the P,Q Flows Per Class Discussion on 09-15-2005
%--------------------------------------------------------------------------
% The Branch flows were changed from the maximums of each bus to just the
% values of the from bus
%--------------------------------------------------------------------------
% Print the Branch Flows
%--------------------------------------------------------------------------
fprintf('\n=============================================');
fprintf('\n| Branch Flow Data |');
fprintf('\n=============================================');
fprintf('\n From Bus \tTo Bus \tP (MW) \tQ (MVAr) ');
fprintf('\n -------- \t-------- \t------- \t-------- ');
for i=1:D(1)
fprintf('\n %1.0f \t\t\t%1.0f \t\t%6f \t\t%6f', branchpqflows(i,1), branchpqflows(i,2), branch(i,12), branch(i,13));
end;
clear i, clear D;
fprintf('\n\n');
fprintf('\n\n');
fprintf('\n=============================================');
fprintf('\n| Contingency Analysis - XB |');
fprintf('\n=============================================');
%---------------------------------------------------------------------
% Retrieve GSF Values
%---------------------------------------------------------------------
gennumber = 3; % You must define the outaged generator bus number here
fprintf('\n\n');
fprintf('Generator Bus Number ');
fprintf('%1.0f', gennumber);
fprintf(' is currently out of service');
[deltPflo,gsfvalues] = computeGSF(gennumber,swingbus,bprimematrix,bprimematrixnoswing,branchpqflows,branch);
fprintf('\n\n');
fprintf('\n=============================================');
fprintf('\n| GSF Values By Branch |');
fprintf('\n=============================================');
fprintf('\n From Bus \tTo Bus \tValue ');
fprintf('\n -------- \t------ \t --------');
D = size(gsfvalues);
for i=1:D(1)
fprintf('\n %1.0f \t\t\t%1.0f \t\t\t%6f', gsfvalues(i,1), gsfvalues(i,2), gsfvalues(i,3));
end;
%----------------------------------------------------------------------
% Retrieve Post-Contingency MW branch flows
%----------------------------------------------------------------------
branchpqflows = [branchpqflows(:,1), branchpqflows(:,2), branch(:,12), branch(:,13)];
baseMWflows = [branchpqflows(:,1), branchpqflows(:,2) branchpqflows(:,3)];
% Determine the GenMW for the Outaged Generator
D = size(gen);
for i=1:D(1)
if gen(i,1) == gennumber
genMW = gen(i,2);
else
end;
end;
fprintf('\n\n');
[newbranchflows] = determineBranchFlows(gsfvalues, baseMWflows, genMW);
fprintf('\n=============================================');
fprintf('\n| Post-Contingency MW branch flows |');
fprintf('\n=============================================');
fprintf('\n From Bus \tTo Bus \tMW ');
fprintf('\n -------- \t------ \t --------');
D = size(newbranchflows);
for i=1:D(1)
fprintf('\n %1.0f \t\t\t%1.0f \t\t\t%6f', newbranchflows(i,1), newbranchflows(i,2), newbranchflows(i,3));
end