0001 function [max_lambda, predicted_list, corrected_list, combined_list, success, et] = cpf(casedata, loadvarloc, sigmaForLambda, sigmaForVoltage)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0048 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0049 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, ...
0050 RATE_C, TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST] = idx_brch;
0051 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, ...
0052 GEN_STATUS, PMAX, PMIN, MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = idx_gen;
0053
0054
0055 if nargin < 3
0056 sigmaForLambda = 0.1;
0057 sigmaForVoltage = 0.025;
0058 end
0059
0060
0061 max_iter = 400;
0062 verbose = 1;
0063
0064
0065
0066
0067
0068
0069 slopeThresh_Phase1 = 0.5;
0070 slopeThresh_Phase2 = 0.3;
0071
0072
0073 [baseMVA, bus, gen, branch] = loadcase(casedata);
0074 [i2e, bus, gen, branch] = ext2int(bus, gen, branch);
0075 e2i = sparse(max(i2e), 1);
0076 e2i(i2e) = (1:size(bus, 1))';
0077 loadvarloc_i = e2i(loadvarloc);
0078
0079
0080 [ref, pv, pq] = bustypes(bus, gen);
0081
0082
0083 on = find(gen(:, GEN_STATUS) > 0);
0084 gbus = gen(on, GEN_BUS);
0085
0086
0087 [Ybus, Yf, Yt] = makeYbus(baseMVA, bus, branch);
0088
0089
0090 npv = length(pv);
0091 npq = length(pq);
0092 pv_bus = ~isempty(find(pv == loadvarloc_i));
0093
0094
0095
0096 flag_lambdaIncrease = true;
0097 if bus(loadvarloc_i, PD) == 0
0098 initQPratio = 0;
0099 fprintf('\t[Warning]:\tLoad real power at bus %d is 0. Q/P ratio will be fixed at 0.\n', loadvarloc);
0100 else
0101 initQPratio = bus(loadvarloc_i, QD)./bus(loadvarloc_i, PD);
0102 end
0103 lambda0 = 0;
0104 lambda = lambda0;
0105 Vm = ones(size(bus, 1), 1);
0106 Va = bus(ref(1), VA) * Vm;
0107 V = Vm .* exp(sqrt(-1) * pi/180 * Va);
0108 V(gbus) = gen(on, VG) ./ abs(V(gbus)).* V(gbus);
0109
0110 pointCnt = 0;
0111
0112
0113 lambda_predicted = lambda;
0114 V_predicted = V;
0115 [V, lambda, success, iterNum] = cpf_correctVoltage(baseMVA, bus, gen, Ybus, V_predicted, lambda_predicted, initQPratio, loadvarloc_i);
0116
0117 pointCnt = pointCnt + 1;
0118 corrected_list(:, pointCnt) = [V;lambda];
0119
0120
0121
0122
0123 t0 = clock;
0124
0125 if verbose > 0
0126 fprintf('Start Phase 1: voltage prediction-correction (lambda increasing).\n');
0127 end
0128 i = 0;
0129 while i < max_iter
0130
0131 i = i + 1;
0132
0133
0134 V_saved = V;
0135 lambda_saved = lambda;
0136
0137
0138 [V_predicted, lambda_predicted, J] = cpf_predict(Ybus, ref, pv, pq, V, lambda, sigmaForLambda, 1, initQPratio, loadvarloc, flag_lambdaIncrease);
0139
0140
0141 [V, lambda, success, iterNum] = cpf_correctVoltage(baseMVA, bus, gen, Ybus, V_predicted, lambda_predicted, initQPratio, loadvarloc_i);
0142
0143
0144 slope = abs(V(loadvarloc_i) - V_saved(loadvarloc_i))/(lambda - lambda_saved);
0145
0146
0147
0148
0149
0150 if abs(slope) >= slopeThresh_Phase1 | success == false
0151
0152 V = V_saved;
0153 lambda = lambda_saved;
0154
0155 if verbose > 0
0156 fprintf('\t[Info]:\tApproaching nose area of PV curve, or voltage correction fails.\n');
0157 end
0158 break;
0159 else
0160 if verbose > 2
0161 fprintf('\nVm_predicted\tVm_corrected\n');
0162 [[abs(V_predicted);lambda_predicted] [abs(V);lambda]]
0163 end
0164
0165
0166 pointCnt = pointCnt + 1;
0167 predicted_list(:, pointCnt-1) = [V_predicted;lambda_predicted];
0168 corrected_list(:, pointCnt) = [V;lambda];
0169 end
0170 end
0171 pointCnt_Phase1 = pointCnt;
0172 if verbose > 0
0173 fprintf('\t[Info]:\t%d data points contained in phase 1.\n', pointCnt_Phase1);
0174 end
0175
0176
0177 if verbose > 0
0178 fprintf('Switch to Phase 2: lambda prediction-correction (voltage decreasing).\n');
0179 end
0180 k = 0;
0181 while k < max_iter
0182
0183 k = k + 1;
0184
0185
0186 V_saved = V;
0187 lambda_saved = lambda;
0188
0189
0190 [V_predicted, lambda_predicted, J] = cpf_predict(Ybus, ref, pv, pq, V, lambda, sigmaForVoltage, 2, initQPratio, loadvarloc);
0191
0192 Vm_assigned = abs(V_predicted);
0193 [V, lambda, success, iterNum] = cpf_correctLambda(baseMVA, bus, gen, Ybus, Vm_assigned, V_predicted, lambda_predicted, initQPratio, loadvarloc, ref, pv, pq);
0194
0195
0196 slope = abs(V(loadvarloc_i) - V_saved(loadvarloc_i))/(lambda - lambda_saved);
0197
0198
0199
0200
0201
0202 if abs(slope) <= slopeThresh_Phase2 | success == false
0203
0204 V = V_saved;
0205 lambda = lambda_saved;
0206
0207
0208 if verbose > 0
0209 fprintf('\t[Info]:\tLeaving nose area of PV curve, or lambda correction fails.\n');
0210 end
0211 break;
0212 else
0213 if verbose > 2
0214 fprintf('\nVm_predicted\tVm_corrected\n');
0215 [[abs(V_predicted);lambda_predicted] [abs(V);lambda]]
0216 end
0217
0218
0219 pointCnt = pointCnt + 1;
0220 predicted_list(:, pointCnt-1) = [V_predicted;lambda_predicted];
0221 corrected_list(:, pointCnt) = [V;lambda];
0222 end
0223 end
0224 pointCnt_Phase2 = pointCnt - pointCnt_Phase1;
0225 if verbose > 0
0226 fprintf('\t[Info]:\t%d data points contained in phase 2.\n', pointCnt_Phase2);
0227 end
0228
0229
0230 if verbose > 0
0231 fprintf('Switch to Phase 3: voltage prediction-correction (lambda decreasing).\n');
0232 end
0233
0234 flag_lambdaIncrease = false;
0235 i = 0;
0236 while i < max_iter
0237
0238 i = i + 1;
0239
0240
0241 [V_predicted, lambda_predicted, J] = cpf_predict(Ybus, ref, pv, pq, V, lambda, sigmaForLambda, 1, initQPratio, loadvarloc, flag_lambdaIncrease);
0242
0243
0244 [V, lambda, success, iterNum] = cpf_correctVoltage(baseMVA, bus, gen, Ybus, V_predicted, lambda_predicted, initQPratio, loadvarloc_i);
0245
0246
0247 slope = abs(V(loadvarloc_i) - V_saved(loadvarloc_i))/(lambda - lambda_saved);
0248
0249 if lambda < 0
0250 if verbose > 0
0251 fprintf('\t[Info]:\tlambda is less than 0.\n\t\t\tCPF finished.\n');
0252 end
0253 break;
0254 end
0255
0256
0257
0258
0259
0260 if success == false
0261 if verbose > 0
0262 fprintf('\t[Info]:\tVoltage correction step fails..\n');
0263 end
0264 break;
0265 else
0266 if verbose > 2
0267 fprintf('\nVm_predicted\tVm_corrected\n');
0268 [[abs(V_predicted);lambda_predicted] [abs(V);lambda]]
0269 end
0270
0271
0272 pointCnt = pointCnt + 1;
0273 predicted_list(:, pointCnt-1) = [V_predicted;lambda_predicted];
0274 corrected_list(:, pointCnt) = [V;lambda];
0275 end
0276 end
0277 pointCnt_Phase3 = pointCnt - pointCnt_Phase2 - pointCnt_Phase1;
0278 if verbose > 0
0279 fprintf('\t[Info]:\t%d data points contained in phase 3.\n', pointCnt_Phase3);
0280 end
0281
0282 et = etime(clock, t0);
0283
0284
0285
0286 predictedCnt = size(predicted_list, 2);
0287 combined_list(:, 1) = corrected_list(:, 1);
0288 for i = 1:predictedCnt
0289 combined_list(:, 2*i) = predicted_list(:, i);
0290 combined_list(:, 2*i+1) = corrected_list(:, i+1);
0291 end
0292
0293
0294 [bus, gen, branch] = int2ext(i2e, bus, gen, branch);
0295
0296
0297 nb = size(bus, 1);
0298 max_lambda = max(corrected_list(nb+1, :));
0299 predicted_list = [[bus(:, BUS_I);0] predicted_list];
0300 corrected_list = [[bus(:, BUS_I);0] corrected_list];
0301 combined_list = [[bus(:, BUS_I);0] combined_list];
0302
0303 if verbose > 1
0304 Vm_corrected = abs(corrected_list);
0305 Vm_predicted = abs(predicted_list);
0306 Vm_combined = abs(combined_list);
0307 Vm_corrected
0308 Vm_predicted
0309 Vm_combined
0310 pointCnt_Phase1
0311 pointCnt_Phase2
0312 pointCnt_Phase3
0313 pointCnt
0314 end