0001 function mpc = toggle_softlims(mpc, on_off)
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
0048
0049
0050
0051 if strcmp(upper(on_off), 'ON')
0052
0053
0054
0055
0056
0057
0058 mpc = add_userfcn(mpc, 'ext2int', @userfcn_softlims_ext2int);
0059 mpc = add_userfcn(mpc, 'formulation', @userfcn_softlims_formulation);
0060 mpc = add_userfcn(mpc, 'int2ext', @userfcn_softlims_int2ext);
0061 mpc = add_userfcn(mpc, 'printpf', @userfcn_softlims_printpf);
0062 mpc = add_userfcn(mpc, 'savecase', @userfcn_softlims_savecase);
0063 mpc.userfcn.status.softlims = 1;
0064 elseif strcmp(upper(on_off), 'OFF')
0065 mpc = remove_userfcn(mpc, 'savecase', @userfcn_softlims_savecase);
0066 mpc = remove_userfcn(mpc, 'printpf', @userfcn_softlims_printpf);
0067 mpc = remove_userfcn(mpc, 'int2ext', @userfcn_softlims_int2ext);
0068 mpc = remove_userfcn(mpc, 'formulation', @userfcn_softlims_formulation);
0069 mpc = remove_userfcn(mpc, 'ext2int', @userfcn_softlims_ext2int);
0070 mpc.userfcn.status.softlims = 0;
0071 elseif strcmp(upper(on_off), 'STATUS')
0072 if isfield(mpc, 'userfcn') && isfield(mpc.userfcn, 'status') && ...
0073 isfield(mpc.userfcn.status, 'softlims')
0074 mpc = mpc.userfcn.status.softlims;
0075 else
0076 mpc = 0;
0077 end
0078 else
0079 error('toggle_softlims: 2nd argument must be ''on'', ''off'' or ''status''');
0080 end
0081
0082
0083
0084 function mpc = userfcn_softlims_ext2int(mpc, args)
0085
0086
0087
0088
0089
0090
0091
0092
0093 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0094 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0095 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0096
0097
0098 default_cost = 1000;
0099 if isfield(mpc, 'softlims')
0100 if ~isfield(mpc.softlims, 'idx')
0101 mpc.softlims.idx = [];
0102 end
0103 if ~isfield(mpc.softlims, 'cost')
0104 mpc.softlims.cost = default_cost;
0105 end
0106 else
0107 mpc.softlims.idx = [];
0108 mpc.softlims.cost = default_cost;
0109 end
0110
0111
0112 s = mpc.softlims;
0113 o = mpc.order;
0114 nl0 = size(o.ext.branch, 1);
0115 nl = size(mpc.branch, 1);
0116
0117
0118 mpc.order.ext.softlims = s;
0119
0120
0121 s = softlims_defaults(s, o.ext.branch);
0122 e2i = zeros(nl0, 1);
0123 e2i(o.branch.status.on) = (1:nl)';
0124 s.idx = e2i(s.idx);
0125 k = find(s.idx == 0);
0126 s.idx(k) = [];
0127 s.cost(k, :) = [];
0128 k = find(mpc.branch(s.idx, RATE_A) <= 0);
0129 s.idx(k) = [];
0130 s.cost(k, :) = [];
0131
0132
0133 s.Pfmax = mpc.branch(s.idx, RATE_A) / mpc.baseMVA;
0134 mpc.branch(s.idx, RATE_A) = 0;
0135
0136 mpc.softlims = s;
0137 mpc.order.int.softlims = s;
0138
0139
0140
0141 function om = userfcn_softlims_formulation(om, args)
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0152 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0153 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0154
0155
0156 mpc = get_mpc(om);
0157 [baseMVA, bus, branch] = deal(mpc.baseMVA, mpc.bus, mpc.branch);
0158 s = mpc.softlims;
0159
0160
0161 [B, Bf, Pbusinj, Pfinj] = makeBdc(baseMVA, bus, branch);
0162 n = size(Bf, 2);
0163
0164
0165
0166
0167 ns = length(s.idx);
0168 I = speye(ns, ns);
0169 As = [-Bf(s.idx, :) -I; Bf(s.idx, :) -I];
0170 ls = -Inf(2*ns, 1);
0171 us = [ Pfinj(s.idx) + s.Pfmax;
0172 -Pfinj(s.idx) + s.Pfmax ];
0173
0174
0175 Cw = s.cost(:, 1) * mpc.baseMVA;
0176
0177
0178 om = add_vars(om, 'flv', ns, zeros(ns, 1), zeros(ns, 1), Inf(ns, 1));
0179 om = add_costs(om, 'vc', struct('N', I, 'Cw', Cw), {'flv'});
0180 om = add_constraints(om, 'softlims', As, ls, us, {'Va', 'flv'});
0181
0182
0183
0184 function results = userfcn_softlims_int2ext(results, args)
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0197 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0198 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0199
0200
0201 s = results.softlims;
0202
0203
0204 o = results.order;
0205 nl0 = size(o.ext.branch, 1);
0206 nl = size(results.branch, 1);
0207 o.branch.status.on;
0208 results.softlims = results.order.ext.softlims;
0209
0210
0211 results.branch(s.idx, RATE_A) = s.Pfmax * results.baseMVA;
0212
0213
0214
0215 n = size(results.lin.mu.u.softlims, 1) / 2;
0216 results.branch(s.idx, MU_ST) = results.lin.mu.u.softlims(1:n) / results.baseMVA;
0217 results.branch(s.idx, MU_SF) = results.lin.mu.u.softlims(n+1:end) / results.baseMVA;
0218
0219 results.softlims.overload = zeros(nl0, 1);
0220 k = find(results.branch(:, RATE_A) & ...
0221 abs(results.branch(:, PF)) > results.branch(:, RATE_A) );
0222 results.softlims.overload(o.branch.status.on(k)) = ...
0223 abs(results.branch(k, PF)) - results.branch(k, RATE_A);
0224 results.softlims.ovl_cost = zeros(size(results.softlims.overload));
0225 results.softlims.ovl_cost(o.branch.status.on(s.idx)) = ...
0226 results.softlims.overload(o.branch.status.on(s.idx)) .* s.cost(:, 1);
0227
0228
0229
0230 function results = userfcn_softlims_printpf(results, fd, mpopt, args)
0231
0232
0233
0234
0235
0236
0237
0238
0239 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0240 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0241 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0242
0243
0244 ptol = 1e-6;
0245 isOPF = isfield(results, 'f') && ~isempty(results.f);
0246 SUPPRESS = mpopt.out.suppress_detail;
0247 if SUPPRESS == -1
0248 if size(results.bus, 1) > 500
0249 SUPPRESS = 1;
0250 else
0251 SUPPRESS = 0;
0252 end
0253 end
0254 OUT_ALL = mpopt.out.all;
0255 OUT_FORCE = mpopt.out.force;
0256 OUT_BRANCH = OUT_ALL == 1 || (OUT_ALL == -1 && ~SUPPRESS && mpopt.out.branch);
0257
0258 if isOPF && OUT_BRANCH && (results.success || OUT_FORCE)
0259 s = softlims_defaults(results.softlims, results.branch);
0260 k = find(s.overload(s.idx) | sum(results.branch(s.idx, MU_SF:MU_ST), 2) > ptol);
0261
0262 fprintf(fd, '\n================================================================================');
0263 fprintf(fd, '\n| Soft Flow Limits |');
0264 fprintf(fd, '\n================================================================================');
0265 fprintf(fd, '\nBrnch From To Flow Limit Overload mu');
0266 fprintf(fd, '\n # Bus Bus (MW) (MW) (MW) ($/MW)');
0267 fprintf(fd, '\n----- ----- ----- -------- -------- -------- ---------');
0268 fprintf(fd, '\n%4d%7d%7d%10.2f%10.2f%10.2f%11.3f', ...
0269 [ s.idx(k), results.branch(s.idx(k), [F_BUS, T_BUS]), ...
0270 results.branch(s.idx(k), [PF, RATE_A]), ...
0271 s.overload(s.idx(k)), ...
0272 sum(results.branch(s.idx(k), [MU_SF:MU_ST]), 2) ...
0273 ]');
0274 fprintf(fd, '\n --------');
0275 fprintf(fd, '\n Total:%10.2f', ...
0276 sum(s.overload(s.idx(k))));
0277 fprintf(fd, '\n');
0278 end
0279
0280
0281
0282 function mpc = userfcn_softlims_savecase(mpc, fd, prefix, args)
0283
0284
0285
0286
0287
0288
0289
0290
0291 if isfield(mpc, 'softlims')
0292 s = mpc.softlims;
0293
0294 fprintf(fd, '\n%%%%----- Soft Flow Limit Data -----%%%%\n');
0295
0296 if isfield(s, 'idx')
0297 fprintf(fd, '%%%% branch indexes\n');
0298 fprintf(fd, '%%\tbranchidx\n');
0299 if isempty(s.idx)
0300 fprintf(fd, '%ssoftlims.idx = [];\n\n', prefix);
0301 else
0302 fprintf(fd, '%ssoftlims.idx = [\n', prefix);
0303 fprintf(fd, '\t%d;\n', s.idx);
0304 fprintf(fd, '];\n\n');
0305 end
0306 end
0307
0308 fprintf(fd, '%%%% violation cost coefficients\n');
0309 fprintf(fd, '%%\trate_a_cost\n');
0310 fprintf(fd, '%ssoftlims.cost = [\n', prefix);
0311 fprintf(fd, '\t%g;\n', s.cost);
0312 fprintf(fd, '];\n');
0313
0314
0315 if isfield(mpc.softlims, 'overload')
0316 fprintf(fd, '\n%%%% overloads\n');
0317 fprintf(fd, '%%\toverload\n');
0318 fprintf(fd, '%ssoftlims.overload = [\n', prefix);
0319 fprintf(fd, '\t%g;\n', s.overload);
0320 fprintf(fd, '];\n');
0321
0322 fprintf(fd, '\n%%%% overload costs\n');
0323 fprintf(fd, '%%\toverload_costs\n');
0324 fprintf(fd, '%ssoftlims.ovl_cost = [\n', prefix);
0325 fprintf(fd, '\t%g;\n', s.ovl_cost);
0326 fprintf(fd, '];\n');
0327 end
0328 end
0329
0330
0331 function s = softlims_defaults(s, branch)
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0343 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0344 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0345
0346 if isempty(s.idx)
0347 s.idx = find(branch(:, BR_STATUS) > 0 & branch(:, RATE_A) > 0);
0348 end
0349 if length(s.cost) == 1 && length(s.idx) > 1
0350 s.cost = s.cost * ones(size(s.idx));
0351 end