0001 function fname_out = savecase(fname, varargin)
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
0052
0053
0054
0055
0056
0057 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0058 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0059 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0060 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0061 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0062 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0063 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0064 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0065 [AREA_I, PRICE_REF_BUS] = idx_area;
0066 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0067
0068
0069 if ischar(varargin{1}) || iscell(varargin{1})
0070 comment = varargin{1};
0071 [args{1:(length(varargin)-1)}] = deal(varargin{2:end});
0072 else
0073 comment = '';
0074 args = varargin;
0075 end
0076 mpc_ver = '2';
0077 if isstruct(args{1})
0078 mpc = args{1};
0079 if length(args) > 1
0080 mpc.version = args{2};
0081 mpc_ver = mpc.version;
0082 end
0083 baseMVA = mpc.baseMVA;
0084 bus = mpc.bus;
0085 gen = mpc.gen;
0086 branch = mpc.branch;
0087 if isfield(mpc, 'areas')
0088 areas = mpc.areas;
0089 end
0090 if isfield(mpc, 'gencost')
0091 gencost = mpc.gencost;
0092 end
0093 else
0094 baseMVA = args{1};
0095 bus = args{2};
0096 gen = args{3};
0097 branch = args{4};
0098 mpc.baseMVA = baseMVA;
0099 mpc.bus = bus;
0100 mpc.gen = gen;
0101 mpc.branch = branch;
0102 if length(args) == 5
0103 gencost = args{5};
0104 mpc.gencost = gencost;
0105 end
0106 if length(args) == 6
0107 areas = args{5};
0108 gencost = args{6};
0109 mpc.areas = areas;
0110 mpc.gencost = gencost;
0111 end
0112 end
0113
0114
0115 if strcmp(mpc_ver, '1')
0116
0117 if size(gen, 2) >= MU_QMIN
0118 gen = gen(:, [1:PMIN, MU_PMAX:MU_QMIN]);
0119 else
0120 gen = gen(:, 1:PMIN);
0121 end
0122
0123 shift = MU_PMAX - PMIN - 1;
0124 tmp = num2cell([MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] - shift);
0125 [MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = deal(tmp{:});
0126
0127
0128 if size(branch, 2) >= MU_ST
0129 branch = branch(:, [1:BR_STATUS, PF:MU_ST]);
0130 elseif size(branch, 2) >= QT
0131 branch = branch(:, [1:BR_STATUS, PF:QT]);
0132 else
0133 branch = branch(:, 1:BR_STATUS);
0134 end
0135
0136 shift = PF - BR_STATUS - 1;
0137 tmp = num2cell([PF, QF, PT, QT, MU_SF, MU_ST] - shift);
0138 [PF, QF, PT, QT, MU_SF, MU_ST] = deal(tmp{:});
0139 end
0140
0141
0142 l = length(fname);
0143 rootname = [];
0144 if l > 2
0145 if strcmp(fname(l-1:l), '.m')
0146 rootname = fname(1:l-2);
0147 extension = '.m';
0148 elseif l > 4
0149 if strcmp(fname(l-3:l), '.mat')
0150 rootname = fname(1:l-4);
0151 extension = '.mat';
0152 end
0153 end
0154 end
0155 if isempty(rootname)
0156 rootname = fname;
0157 extension = '.m';
0158 fname = [rootname, extension];
0159 end
0160
0161
0162 if strcmp(extension, '.mat')
0163 vflag = '';
0164 if str2double(version('-release')) > 13
0165 vflag = ' -V6';
0166 end
0167 if strcmp(mpc_ver, '1')
0168 if exist('areas', 'var') && exist('gencost', 'var')
0169 cmd = sprintf('save %s baseMVA bus gen branch areas gencost%s;', rootname, vflag);
0170 else
0171 cmd = sprintf('save %s baseMVA bus gen branch%s;', rootname, vflag);
0172 end
0173 else
0174 cmd = sprintf('save %s mpc%s;', rootname, vflag);
0175 end
0176 eval(cmd);
0177 else
0178
0179 [fd, msg] = fopen(fname, 'wt');
0180 if fd == -1
0181 error(['savecase: ', msg]);
0182 end
0183
0184
0185 if strcmp(mpc_ver, '1')
0186 if exist('areas', 'var') && exist('gencost', 'var') && ~isempty(gencost)
0187 fprintf(fd, 'function [baseMVA, bus, gen, branch, areas, gencost] = %s\n', rootname);
0188 else
0189 fprintf(fd, 'function [baseMVA, bus, gen, branch] = %s\n', rootname);
0190 end
0191 prefix = '';
0192 else
0193 fprintf(fd, 'function mpc = %s\n', rootname);
0194 prefix = 'mpc.';
0195 end
0196 if ~isempty(comment)
0197 if ischar(comment)
0198 fprintf(fd, '%%%s\n', comment);
0199 elseif iscell(comment)
0200 for k = 1:length(comment)
0201 fprintf(fd, '%%%s\n', comment{k});
0202 end
0203 end
0204 end
0205 fprintf(fd, '\n%%%% MATPOWER Case Format : Version %s\n', mpc_ver);
0206 if ~strcmp(mpc_ver, '1')
0207 fprintf(fd, 'mpc.version = ''%s'';\n', mpc_ver);
0208 end
0209 fprintf(fd, '\n%%%%----- Power Flow Data -----%%%%\n');
0210 fprintf(fd, '%%%% system MVA base\n');
0211 fprintf(fd, '%sbaseMVA = %.9g;\n', prefix, baseMVA);
0212
0213
0214 ncols = size(bus, 2);
0215 fprintf(fd, '\n%%%% bus data\n');
0216 fprintf(fd, '%%\tbus_i\ttype\tPd\tQd\tGs\tBs\tarea\tVm\tVa\tbaseKV\tzone\tVmax\tVmin');
0217 if ncols >= MU_VMIN
0218 fprintf(fd, '\tlam_P\tlam_Q\tmu_Vmax\tmu_Vmin');
0219 end
0220 fprintf(fd, '\n%sbus = [\n', prefix);
0221 if ncols < MU_VMIN
0222 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', bus(:, 1:VMIN).');
0223 else
0224 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', bus(:, 1:MU_VMIN).');
0225 end
0226 fprintf(fd, '];\n');
0227
0228
0229 ncols = size(gen, 2);
0230 fprintf(fd, '\n%%%% generator data\n');
0231 fprintf(fd, '%%\tbus\tPg\tQg\tQmax\tQmin\tVg\tmBase\tstatus\tPmax\tPmin');
0232 if ~strcmp(mpc_ver, '1')
0233 fprintf(fd, '\tPc1\tPc2\tQc1min\tQc1max\tQc2min\tQc2max\tramp_agc\tramp_10\tramp_30\tramp_q\tapf');
0234 end
0235 if ncols >= MU_QMIN
0236 fprintf(fd, '\tmu_Pmax\tmu_Pmin\tmu_Qmax\tmu_Qmin');
0237 end
0238 fprintf(fd, '\n%sgen = [\n', prefix);
0239 if ncols < MU_QMIN
0240 if strcmp(mpc_ver, '1')
0241 fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', gen(:, 1:PMIN).');
0242 else
0243 fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g;\n', gen(:, 1:APF).');
0244 end
0245 else
0246 if strcmp(mpc_ver, '1')
0247 fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', gen(:, 1:MU_QMIN).');
0248 else
0249 fprintf(fd, '\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', gen(:, 1:MU_QMIN).');
0250 end
0251 end
0252 fprintf(fd, '];\n');
0253
0254
0255 ncols = size(branch, 2);
0256 fprintf(fd, '\n%%%% branch data\n');
0257 fprintf(fd, '%%\tfbus\ttbus\tr\tx\tb\trateA\trateB\trateC\tratio\tangle\tstatus');
0258 if ~strcmp(mpc_ver, '1')
0259 fprintf(fd, '\tangmin\tangmax');
0260 end
0261 if ncols >= QT
0262 fprintf(fd, '\tPf\tQf\tPt\tQt');
0263 end
0264 if ncols >= MU_ST
0265 fprintf(fd, '\tmu_Sf\tmu_St');
0266 if ~strcmp(mpc_ver, '1')
0267 fprintf(fd, '\tmu_angmin\tmu_angmax');
0268 end
0269 end
0270 fprintf(fd, '\n%sbranch = [\n', prefix);
0271 if ncols < QT
0272 if strcmp(mpc_ver, '1')
0273 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d;\n', branch(:, 1:BR_STATUS).');
0274 else
0275 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g;\n', branch(:, 1:ANGMAX).');
0276 end
0277 elseif ncols < MU_ST
0278 if strcmp(mpc_ver, '1')
0279 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:QT).');
0280 else
0281 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:QT).');
0282 end
0283 else
0284 if strcmp(mpc_ver, '1')
0285 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:MU_ST).');
0286 else
0287 fprintf(fd, '\t%d\t%d\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%.9g\t%d\t%.9g\t%.9g\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f;\n', branch(:, 1:MU_ANGMAX).');
0288 end
0289 end
0290 fprintf(fd, '];\n');
0291
0292
0293 if (exist('areas', 'var') && ~isempty(areas)) || ...
0294 (exist('gencost', 'var') && ~isempty(gencost))
0295 fprintf(fd, '\n%%%%----- OPF Data -----%%%%');
0296 end
0297 if exist('areas', 'var') && ~isempty(areas)
0298
0299 fprintf(fd, '\n%%%% area data\n');
0300 fprintf(fd, '%%\tarea\trefbus\n');
0301 fprintf(fd, '%sareas = [\n', prefix);
0302 if ~isempty(areas)
0303 fprintf(fd, '\t%d\t%d;\n', areas(:, 1:PRICE_REF_BUS).');
0304 end
0305 fprintf(fd, '];\n');
0306 end
0307 if exist('gencost', 'var') && ~isempty(gencost)
0308
0309 fprintf(fd, '\n%%%% generator cost data\n');
0310 fprintf(fd, '%%\t1\tstartup\tshutdown\tn\tx1\ty1\t...\txn\tyn\n');
0311 fprintf(fd, '%%\t2\tstartup\tshutdown\tn\tc(n-1)\t...\tc0\n');
0312 fprintf(fd, '%sgencost = [\n', prefix);
0313 if ~isempty(gencost)
0314 n1 = 2 * max(gencost(gencost(:, MODEL) == PW_LINEAR, NCOST));
0315 n2 = max(gencost(gencost(:, MODEL) == POLYNOMIAL, NCOST));
0316 n = max([n1; n2]);
0317 if size(gencost, 2) < n + 4
0318 error('savecase: gencost data claims it has more columns than it does');
0319 end
0320 template = '\t%d\t%.9g\t%.9g\t%d';
0321 for i = 1:n
0322 template = [template, '\t%.9g'];
0323 end
0324 template = [template, ';\n'];
0325 fprintf(fd, template, gencost.');
0326 end
0327 fprintf(fd, '];\n');
0328 end
0329
0330
0331 if (isfield(mpc, 'A') && ~isempty(mpc.A)) || ...
0332 (isfield(mpc, 'N') && ~isempty(mpc.N))
0333 fprintf(fd, '\n%%%%----- Generalized OPF User Data -----%%%%');
0334 end
0335
0336
0337 if isfield(mpc, 'A') && ~isempty(mpc.A)
0338
0339 fprintf(fd, '\n%%%% user constraints\n');
0340 print_sparse(fd, sprintf('%sA', prefix), mpc.A);
0341 if isfield(mpc, 'l') && ~isempty(mpc.l) && ...
0342 isfield(mpc, 'u') && ~isempty(mpc.u)
0343 fprintf(fd, 'lu = [\n');
0344 fprintf(fd, '\t%.9g\t%.9g;\n', [mpc.l mpc.u].');
0345 fprintf(fd, '];\n');
0346 fprintf(fd, '%sl = lu(:, 1);\n', prefix);
0347 fprintf(fd, '%su = lu(:, 2);\n\n', prefix);
0348 elseif isfield(mpc, 'l') && ~isempty(mpc.l)
0349 fprintf(fd, '%sl = [\n', prefix);
0350 fprintf(fd, '\t%.9g;\n', mpc.l);
0351 fprintf(fd, '];\n\n');
0352 elseif isfield(mpc, 'u') && ~isempty(mpc.u)
0353 fprintf(fd, '%su = [\n', prefix);
0354 fprintf(fd, '\t%.9g;\n', mpc.u);
0355 fprintf(fd, '];\n');
0356 end
0357 end
0358
0359
0360 if isfield(mpc, 'N') && ~isempty(mpc.N)
0361 fprintf(fd, '\n%%%% user costs\n');
0362 print_sparse(fd, sprintf('%sN', prefix), mpc.N);
0363 if isfield(mpc, 'H') && ~isempty(mpc.H)
0364 print_sparse(fd, sprintf('%sH', prefix), mpc.H);
0365 end
0366 if isfield(mpc, 'fparm') && ~isempty(mpc.fparm)
0367 fprintf(fd, 'Cw_fparm = [\n');
0368 fprintf(fd, '\t%.9g\t%d\t%.9g\t%.9g\t%.9g;\n', [mpc.Cw mpc.fparm].');
0369 fprintf(fd, '];\n');
0370 fprintf(fd, '%sCw = Cw_fparm(:, 1);\n', prefix);
0371 fprintf(fd, '%sfparm = Cw_fparm(:, 2:5);\n', prefix);
0372 else
0373 fprintf(fd, '%sCw = [\n', prefix);
0374 fprintf(fd, '\t%.9g;\n', mpc.Cw);
0375 fprintf(fd, '];\n');
0376 end
0377 end
0378
0379
0380 if isfield(mpc, 'z0') || isfield(mpc, 'zl') || isfield(mpc, 'zu')
0381 fprintf(fd, '\n%%%% user vars\n');
0382 end
0383 if isfield(mpc, 'z0') && ~isempty(mpc.z0)
0384 fprintf(fd, '%sz0 = [\n', prefix);
0385 fprintf(fd, '\t%.9g;\n', mpc.z0);
0386 fprintf(fd, '];\n');
0387 end
0388 if isfield(mpc, 'zl') && ~isempty(mpc.zl)
0389 fprintf(fd, '%szl = [\n', prefix);
0390 fprintf(fd, '\t%.9g;\n', mpc.zl);
0391 fprintf(fd, '];\n');
0392 end
0393 if isfield(mpc, 'zu') && ~isempty(mpc.zu)
0394 fprintf(fd, '%szu = [\n', prefix);
0395 fprintf(fd, '\t%.9g;\n', mpc.zu);
0396 fprintf(fd, '];\n');
0397 end
0398
0399
0400 if isfield(mpc, 'userfcn')
0401 run_userfcn(mpc.userfcn, 'savecase', mpc, fd, prefix);
0402 end
0403
0404
0405 if fd ~= 1
0406 fclose(fd);
0407 end
0408 end
0409
0410 if nargout > 0
0411 fname_out = fname;
0412 end
0413
0414
0415
0416 function print_sparse(fd, varname, A)
0417
0418 [i, j, s] = find(A);
0419 [m, n] = size(A);
0420
0421 if isempty(s)
0422 fprintf(fd, '%s = sparse(%d, %d);\n', varname, m, n);
0423 else
0424 fprintf(fd, 'ijs = [\n');
0425 if m == 1
0426 fprintf(fd, '\t%d\t%d\t%.9g;\n', [i; j; s]);
0427 else
0428 fprintf(fd, '\t%d\t%d\t%.9g;\n', [i j s].');
0429 end
0430 fprintf(fd, '];\n');
0431 fprintf(fd, '%s = sparse(ijs(:, 1), ijs(:, 2), ijs(:, 3), %d, %d);\n', varname, m, n);
0432 end