-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
345 lines (277 loc) · 10.5 KB
/
main.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
clc;
clear all;
close all;
% bus to study
bts = 13 ;
%% setting parameters
% tolerance error for voltage and angle for consecutive values
tol_volt = 1e-3;
tol_ang = 1e-3;
tap_include = 1;
maxiters = 25;
%% getting data
[bus_data, branch_data] = data_extract();
baseMVA = 100;
%% Y bus formation
% calling function for ybus calculation
Ybus = y_bus_calculation(bus_data, branch_data, tap_include);
% getting G and B from Y bus
G = real(Ybus);
B = imag(Ybus);
% converting to polar
[Theta Y_mag]=cart2pol(G,B);
%% initializing solution
% finding types of bus
nbus = length(Ybus); % total bus number
% find indexing of PV bus
PV_bus = find(bus_data.data(:,3)==2);
% find indexing of swing bus
Swing_bus =find(bus_data.data(:,3)==3);
% find indexing of PQ bus
PQ_bus = find(bus_data.data(:,3)==0);
% flat start; initialization
% assign 1 to all voltage
Voltage= ones(nbus,1);
% fix voltage of swing bus and PV bus to given value
% column 4 of bus data contains bus voltage, you can also use column 11
Voltage(Swing_bus) = bus_data.data(Swing_bus,4);
Voltage(PV_bus) = bus_data.data(PV_bus,4);
% assign 0 to all bus angles
Delta= zeros(nbus,1);
% fix bus angle of Swing bus to given value
% column 5 of bus data contains angle
Delta(Swing_bus) = bus_data.data(Swing_bus,5) * pi/180;
Non_swing_bus = union(PQ_bus, PV_bus);
% schedule power calculation, For Pv only P, for PQ, both P and Q
% P_sch_ori is P for non-swing bus, Q_schi_ori is Q for PQ bus only
[P_sch_ori, Q_sch_ori, P_sch_all, Q_sch_all] = schedule_power_calc(bus_data, baseMVA,Swing_bus, PV_bus);
% getting K vetor
K = [P_sch_ori; Q_sch_ori];
% continuation parameters
sigma = 0.1; % for first stage
lambda = 0; % for initialization
Voltage_history1 = [];
Delta_history1 = [];
% getting voltage and angle for starting iteration
[Voltage, Delta,~] = NRPF(tol_volt,tol_ang,Voltage,Delta,Swing_bus,PQ_bus,PV_bus,nbus...
,Y_mag,Theta,bus_data,G,B,baseMVA,bts,lambda,maxiters);
%% part I %%%% lambda as continuation parameter
Voltage_collects1 = [];
lambda_collects1 = [];
Voltage_collects1(end+1) = Voltage(bts);
lambda_collects1(end+1) = lambda;
Voltage_history1(:,end+1) = Voltage;
Delta_history1(:,end+1) = Delta;
while (1)
%%%%%%%%% Predictor %%%%%
% calculating jacobian using last voltage and angle
%listing parameters for jacobian calculation
jacobian_params.nbus = nbus;
jacobian_params.G = G;
jacobian_params.B = B;
jacobian_params.Theta = Theta;
jacobian_params.Y_mag = Y_mag;
jacobian_params.PV_bus = PV_bus;
jacobian_params.Swing_bus = Swing_bus;
jacobian_params.PQ_bus = PQ_bus;
jacobian_params.Voltage = Voltage;
jacobian_params.Delta = Delta;
% calling jacobian function
[J11,J12,J21,J22] = jacobian_calc_original(jacobian_params);
J = [J11 J12;J21 J22];
% finding ek
ek = [zeros(1,length(J)) 1];
% finding augmented jacobian
J_aug = [J K;ek];
% getting del_delta, del_voltage and del_lambda using crout based solver
del_x = crout_solver(J_aug,ek');
% getting del_delta, del_voltage and del_lambda
del_delta = del_x([1:nbus-length(Swing_bus)]);
del_voltage = del_x(nbus-length(Swing_bus)+1:end-1);
del_lambda = del_x(end);
% updating
Delta(Non_swing_bus) = Delta(Non_swing_bus) + sigma* del_delta;
Voltage(PQ_bus) = Voltage(PQ_bus) + sigma* del_voltage;
lambda = lambda + sigma*del_lambda;
%%%%%% Corrector %%%%%%%%
% updating studies load value for NR
[Voltage, Delta, iter] = NRPF(tol_volt,tol_ang,Voltage,Delta,Swing_bus,PQ_bus,PV_bus,nbus...
,Y_mag,Theta,bus_data,G,B,baseMVA,bts,lambda,maxiters);
if (iter == maxiters)
% assign previous value of V and lambda as the solution diverges
Voltage = Voltage_history1(:,end);
lambda = lambda_collects1(end);
Voltage_collects1(end+1) = Voltage(bts);
lambda_collects1(end+1) = lambda;
Delta = Delta_history1(:,end);
break;
else
Voltage_history1(:,end+1) = Voltage;
Delta_history1(:,end+1) = Delta;
Voltage_collects1(end+1) = Voltage(bts);
lambda_collects1(end+1) = lambda;
end
end
%% Part II %%%% Voltage as continuation parameter
sigma = 0.005; % for 12 and 13 use 0.0005
factor = 0.75; % factor where the continuation parameter switches to lambda for 12 and 13 use 0.75
lambda = lambda_collects1(end);
Voltage = Voltage_history1(:,end);
Delta = Delta_history1(:,end);
Voltage_history2 = [];
Delta_history2 = [];
lambda_collects2 = [lambda_collects1(end)];
Voltage_collects2 = [Voltage_collects1(end)];
%%% Predictor step %%%
lambda_2start = lambda;
iteration = 1;
while (lambda>lambda_2start*factor) && iteration < 100
iteration = iteration+1;
%finding jacobian
%listing parameters for jacobian calculation
jacobian_params.nbus = nbus;
jacobian_params.G = G;
jacobian_params.B = B;
jacobian_params.Theta = Theta;
jacobian_params.Y_mag = Y_mag;
jacobian_params.PV_bus = PV_bus;
jacobian_params.Swing_bus = Swing_bus;
jacobian_params.PQ_bus = PQ_bus;
jacobian_params.Voltage = Voltage;
jacobian_params.Delta = Delta;
% calling jacobian function
[J11,J12,J21,J22] = jacobian_calc_original(jacobian_params);
J = [J11 J12;J21 J22];
% finding ek
ek = [zeros(1,length(J)) 1];
% finding ekv i.e. last row vector of augmented jacobian for part II
ekv = [zeros(1,length(J)) 0];
% finding index of voltage continuation variable in augmented jacobian
modified_Qindex = find(PQ_bus(:)==bts);
v_index = length(Non_swing_bus) + modified_Qindex;
ekv(v_index) = -1;
% augmented jacobian
J_aug = [J K;ekv];
% getting del_delta, del_voltage and del_lambda using crout based solver
del_x = crout_solver(J_aug,ek');
% getting del_delta, del_voltage and del_lambda
del_delta = del_x([1:nbus-length(Swing_bus)]);
del_voltage = del_x(nbus-length(Swing_bus)+1:end-1);
del_lambda = del_x(end);
% updating
Delta(Non_swing_bus) = Delta(Non_swing_bus) + sigma* del_delta;
Voltage(PQ_bus) = Voltage(PQ_bus) + sigma* del_voltage;
lambda = lambda + sigma*del_lambda;
%%%% Corrector Step %%%%%%%%%%%%%%%%%%%%%%
jacobian_params.nbus = nbus;
jacobian_params.G = G;
jacobian_params.B = B;
jacobian_params.Theta = Theta;
jacobian_params.Y_mag = Y_mag;
jacobian_params.PV_bus = PV_bus;
jacobian_params.Swing_bus = Swing_bus;
jacobian_params.PQ_bus = PQ_bus;
jacobian_params.Voltage = Voltage;
jacobian_params.Delta = Delta;
J_aug = [J -K; ekv];
% mismatch power calculations
% listing parameters for mismatch calculation
mismatch_calc_params.Swing_bus = Swing_bus;
mismatch_calc_params.PQ_bus = PQ_bus;
mismatch_calc_params.PV_bus = PV_bus;
mismatch_calc_params.nbus = nbus;
mismatch_calc_params.Y_mag = Y_mag;
mismatch_calc_params.Theta = Theta;
mismatch_calc_params.Delta = Delta;
mismatch_calc_params.Voltage = Voltage;
mismatch_calc_params.bus_data = bus_data;
mismatch_calc_params.baseMVA = baseMVA;
mismatch_calc_params.bts = bts;
mismatch_calc_params.lambda = lambda;
% calling function
[del_P del_Q P_calc Q_calc] = mismatch_calc(mismatch_calc_params);
del_PQ = [del_P; del_Q];
% getting rhs for corrector
del_PQV = [del_PQ; 0];
% passing to solver
% getting del_delta, del_voltage and del_lambda using crout based solver
del_x = crout_solver(J_aug,del_PQV);
% getting del_delta, del_voltage and del_lambda
del_delta = del_x([1:nbus-length(Swing_bus)]);
del_voltage = del_x(nbus-length(Swing_bus)+1:end-1);
del_lambda = del_x(end);
Delta(Non_swing_bus) = Delta(Non_swing_bus) + del_delta;
Voltage(PQ_bus) = Voltage(PQ_bus) + del_voltage;
lambda = lambda + del_lambda;
% listings values
%Voltage = Voltage_pred; % added later for debug
Voltage_history2(:,end+1) = Voltage;
Delta_history2(:,end+1) = Delta;
Voltage_collects2(end+1) = Voltage(bts);
lambda_collects2(end+1) = lambda;
end
%% Part III Lambda as continuation parameter %%%%
sigma = 0.1;
lambda_collects3 = [lambda_collects2(end)];
Voltage_collects3 = [Voltage_collects2(end)];
Voltage_history3 = [];
Delta_history3 = [];
iteration = 1;
while iteration < 100
%%%%%%%%% Predictor %%%%%
iteration = iteration +1;
% calculating jacobian using last voltage and angle
%listing parameters for jacobian calculation
jacobian_params.nbus = nbus;
jacobian_params.G = G;
jacobian_params.B = B;
jacobian_params.Theta = Theta;
jacobian_params.Y_mag = Y_mag;
jacobian_params.PV_bus = PV_bus;
jacobian_params.Swing_bus = Swing_bus;
jacobian_params.PQ_bus = PQ_bus;
jacobian_params.Voltage = Voltage;
jacobian_params.Delta = Delta;
% calling jacobian function
[J11,J12,J21,J22] = jacobian_calc_original(jacobian_params);
J = [J11 J12;J21 J22];
% finding ek
ek = [zeros(1,length(J)) -1];
% finding augmented jacobian
J_aug = [J -K;ek];
% getting del_delta, del_voltage and del_lambda using crout based solver
del_x = crout_solver(J_aug,abs(ek)');
% getting del_delta, del_voltage and del_lambda
del_delta = del_x([1:nbus-length(Swing_bus)]);
del_voltage = del_x(nbus-length(Swing_bus)+1:end-1);
del_lambda = del_x(end);
% updating
Delta(Non_swing_bus) = Delta(Non_swing_bus) + sigma* del_delta;
Voltage(PQ_bus) = Voltage(PQ_bus) + sigma* del_voltage;
lambda = lambda + sigma*del_lambda;
%% corrector %%%
[Voltage, Delta, iter] = NRPF(tol_volt,tol_ang,Voltage,Delta,Swing_bus,PQ_bus,PV_bus,nbus...
,Y_mag,Theta,bus_data,G,B,baseMVA,bts,lambda,maxiters);
if lambda < 0
Voltage_collects3(end+1) = Voltage(bts);
lambda_collects3(end+1) = 0;
break;
else
Voltage_history3(:,end+1) = Voltage;
Delta_history3(:,end+1) = Delta;
Voltage_collects3(end+1) = Voltage(bts);
lambda_collects3(end+1) = lambda;
end
end
%% plotting
figure;
plot(lambda_collects1, Voltage_collects1, '-r',lambda_collects2,Voltage_collects2,'-g',lambda_collects3,Voltage_collects3,'-k', 'LineWidth', 2.5);
grid on;
xlabel('lambda','Fontsize', 12);
ylabel('Voltage in pu', 'Fontsize',12);
title(['Continuation power flow for bus ' num2str(bts)]);
% folder to save result
folderPath = "C:\Users\shish\OneDrive - Washington State University (email.wsu.edu)\WSU_class\fall 2023\EE521\Homework\Homework2\results";
fileName = ['bus', num2str(bts),'.png'];
fullfilepath = fullfile(folderPath,fileName);
saveas(gcf,fullfilepath);