0001 function [mpc, warnings] = cdf2mpc(cdf_file_name, mpc_name, verbose)
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
0058
0059
0060
0061
0062 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0063 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0064 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0065 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0066 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0067 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0068 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0069 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0070 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0071
0072
0073 if nargin < 2
0074 verbose = 1;
0075 mpc_name = '';
0076 elseif ischar(mpc_name)
0077 if nargin < 3
0078 verbose = 1;
0079 end
0080 else
0081 verbose = mpc_name;
0082 mpc_name = '';
0083 end
0084
0085
0086
0087 [cdf_path cdf_name cdf_ext] = fileparts(cdf_file_name);
0088 if isempty(cdf_ext)
0089 cdf_ext = '.cdf';
0090 cdf_file_name = strcat(cdf_name , cdf_ext);
0091 end
0092
0093
0094 [fid, msg] = fopen(cdf_file_name, 'r');
0095 if fid < 0
0096 disp(msg);
0097 error('cdf2mpc: Can not read the input file: %s', cdf_file_name )
0098 end
0099 if verbose
0100 fprintf('Converting file ''%s''\n', cdf_file_name);
0101 fprintf(' WARNINGS:\n');
0102 end
0103
0104
0105 warnings = {};
0106
0107
0108 title_cdf = fgetl(fid);
0109 if isnumeric(str2num(title_cdf(32:37))) && not(isempty(str2num(title_cdf(32:37))))
0110 baseMVA = str2num(title_cdf(32:37));
0111 if length(findstr(title_cdf(2:9), '/')) == 2
0112 warnings{end+1} = sprintf('check the title format in the first line of the cdf file.');
0113 end
0114 else
0115 error('cdf2mpc: Error getting the Base MVA, check the title format in the first line of the file.')
0116 end
0117
0118
0119 while 1
0120 line = fgetl(fid);
0121 if line(1:16) == 'BUS DATA FOLLOWS', break, end
0122 end
0123
0124
0125 ibus = 0;
0126 igen = 0;
0127 iarea = 0;
0128
0129 while 1
0130 line = fgetl(fid);
0131 if line(1:4) == '-999', break, end
0132
0133
0134 ibus = ibus + 1;
0135 bus(ibus, BUS_I) = str2num(line(1:4));
0136 bus_name{ibus} = strtrim(line(6:17));
0137 bus(ibus, BUS_TYPE) = str2num(line(25:26));
0138 if bus(ibus, BUS_TYPE) == 0
0139 bus(ibus, BUS_TYPE) = 1;
0140 end
0141 if (bus(ibus, BUS_TYPE) < 2)
0142 bus(ibus, PD) = str2num(line(41:49)) - str2num(line(60:67));
0143 elseif (bus(ibus, BUS_TYPE) >= 2)
0144 bus(ibus, PD) = str2num(line(41:49));
0145 end
0146 bus(ibus, QD) = str2num(line(50:59));
0147 bus(ibus, GS) = baseMVA*str2num(line(107:114));
0148 bus(ibus, BS) = baseMVA*str2num(line(115:122));
0149 bus(ibus, BUS_AREA) = str2num(line(19:20));
0150 bus(ibus, VM) = str2num(line(28:33));
0151 bus(ibus, VA) = str2num(line(34:40));
0152 bus(ibus, BASE_KV) = str2num(line(77:83));
0153 bus(ibus, ZONE) = str2num(line(21:23));
0154 bus(ibus, VMAX) = 1.06;
0155 bus(ibus, VMIN) = 0.94;
0156
0157
0158 Pg = str2num(line(60:67));
0159 Qg = str2num(line(68:75));
0160 Qmax = str2num(line(91:98));
0161 Qmin = str2num(line(99:106));
0162 if bus(ibus, BUS_TYPE) >= 2
0163 igen = igen + 1;
0164 if bus(ibus, BUS_TYPE) == 3, refgen = igen; end
0165 gen(igen, GEN_BUS) = bus(ibus, BUS_I);
0166 gen(igen, PG) = Pg;
0167 if gen(igen, PG) < 0
0168 bus(ibus, PD) = bus(ibus, PD) - gen(igen, PG);
0169 warnings{end+1} = sprintf('negative Pg at bus %g treated as Pd', bus(ibus, BUS_I));
0170 if verbose
0171 fprintf(' %s\n', warnings{end});
0172 end
0173 gen(igen, PG) = 0;
0174 end
0175 gen(igen, QG) = Qg;
0176 gen(igen, QMAX) = Qmax;
0177 gen(igen, QMIN) = Qmin;
0178 if Qmax - Qmin < 0.01
0179 gen(igen, QMAX) = Qmin + 0.1 * baseMVA;
0180 warnings{end+1} = sprintf('Qmax = Qmin at generator at bus %4i (Qmax set to Qmin + %g)', bus(ibus, BUS_I), baseMVA/10);
0181 if verbose
0182 fprintf(' %s\n', warnings{end});
0183 end
0184 end
0185 gen(igen, VG) = str2num(line(85:90));
0186 gen(igen, MBASE) = baseMVA;
0187 gen(igen, GEN_STATUS) = 1;
0188 gen(igen, PMAX) = gen(igen, 2) + baseMVA;
0189 gen(igen, PMIN) = 0;
0190
0191 gencost(igen, MODEL) = POLYNOMIAL;
0192 gencost(igen, STARTUP) = 0;
0193 gencost(igen, SHUTDOWN) = 0;
0194 gencost(igen, NCOST) = 3;
0195
0196
0197
0198 end
0199 end
0200
0201 totload = sum(bus(:, PD));
0202 totgen = sum(gen(:, PG));
0203 if totgen < 1.04 * totload
0204 gen(refgen, PMAX) = gen(refgen, PG) + 1.1 * totload - totgen;
0205 warnings{end+1} = sprintf('Insufficient generation, setting Pmax at slack bus (bus %d) to %g', gen(refgen, [GEN_BUS, PMAX]));
0206 if verbose
0207 fprintf(' %s\n', warnings{end});
0208 end
0209 end
0210
0211
0212 ng = size(gen, 1);
0213
0214
0215
0216 zg = find(gen(:, PG) == 0);
0217 gencost(zg, COST) = 0.01 * ones(size(zg));
0218 gencost(zg, COST+1) = 40 * ones(size(zg));
0219 nzg = find(gen(:, PG) ~= 0);
0220 gencost(nzg, COST) = 10 * ones(size(nzg)) ./ gen(nzg, PG);
0221 gencost(nzg, COST+1) = 20 * ones(size(nzg));
0222 gencost(:, COST+2) = zeros(ng, 1);
0223
0224
0225 while 1
0226 line = fgetl(fid);
0227 if line(1:19) == 'BRANCH DATA FOLLOWS', break, end
0228 end
0229
0230
0231 k = 0;
0232 while 1
0233 line = fgetl(fid);
0234 if line(1:4) == '-999', break, end
0235
0236 k = k + 1;
0237 branch(k, F_BUS) = str2num(line(1:4));
0238 branch(k, T_BUS) = str2num(line(6:9));
0239 branch(k, BR_R) = str2num(line(20:29));
0240 branch(k, BR_X) = str2num(line(30:40));
0241 branch(k, BR_B) = str2num(line(41:50));
0242 branch(k, RATE_A) = str2num(line(51:55));
0243 if branch(k, RATE_A) < 0.000001
0244 branch(k, RATE_A) = 0;
0245 warnings{end+1} = sprintf('MVA limit of branch %d - %d not given, set to %g', branch(k, [F_BUS, T_BUS, RATE_A]));
0246 if verbose
0247 fprintf(' %s\n', warnings{end});
0248 end
0249 end
0250 branch(k, RATE_B) = str2num(line(57:61));
0251 branch(k, RATE_C) = str2num(line(63:67));
0252 branch(k, TAP) = str2num(line(77:82));
0253 branch(k, SHIFT) = 0;
0254 branch(k, BR_STATUS) = 1;
0255 end
0256 if verbose
0257 fprintf('Done.\n');
0258 end
0259 fclose(fid);
0260
0261
0262 mpc.baseMVA = baseMVA;
0263 mpc.bus = bus;
0264 mpc.branch = branch;
0265 mpc.gen = gen;
0266 mpc.gencost = gencost;
0267 mpc.bus_name = bus_name;
0268 mpc = loadcase(mpc);
0269
0270
0271 if ~isempty(mpc_name)
0272 comments = {''};
0273 if ~isempty(title_cdf)
0274 comments{end+1} = sprintf(' %s', title_cdf);
0275 end
0276 comments{end+1} = '';
0277 comments{end+1} = sprintf(' Converted by MATPOWER %s using CDF2MPC on %s', mpver, date);
0278 comments{end+1} = sprintf(' from ''%s''.', cdf_file_name);
0279
0280
0281 comments{end+1} = '';
0282 comments{end+1} = ' WARNINGS:';
0283 for k = 1:length(warnings)
0284 comments{end+1} = sprintf(' %s', warnings{k});
0285 end
0286 comments{end+1} = '';
0287 comments{end+1} = sprintf(' See CASEFORMAT for details on the MATPOWER case file format.');
0288
0289 if verbose
0290 spacers = repmat('.', 1, 45-length(mpc_name));
0291 fprintf('Saving to MATPOWER case ''%s'' %s', mpc_name, spacers);
0292 end
0293 savecase(mpc_name, comments, mpc);
0294 if verbose
0295 fprintf(' done.\n');
0296 end
0297 end