0001 function [baseMVA, bus, gen, branch, areas, gencost, info] = loadcase(casefile)
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
0063
0064
0065
0066
0067
0068
0069 info = 0;
0070 if nargout < 3
0071 return_as_struct = true;
0072 else
0073 return_as_struct = false;
0074 end
0075 if nargout >= 5
0076 expect_gencost = true;
0077 if nargout > 5
0078 expect_areas = true;
0079 else
0080 expect_areas = false;
0081 end
0082 else
0083 expect_gencost = false;
0084 expect_areas = false;
0085 end
0086
0087
0088 if ischar(casefile)
0089
0090 l = length(casefile);
0091 if l > 2
0092 if strcmp(casefile(l-1:l), '.m')
0093 rootname = casefile(1:l-2);
0094 extension = '.m';
0095 elseif l > 4
0096 if strcmp(casefile(l-3:l), '.mat')
0097 rootname = casefile(1:l-4);
0098 extension = '.mat';
0099 end
0100 end
0101 end
0102
0103
0104 if ~exist('rootname', 'var')
0105 rootname = casefile;
0106 if exist([casefile '.mat'], 'file') == 2
0107 extension = '.mat';
0108 elseif exist([casefile '.m'], 'file') == 2
0109 extension = '.m';
0110 else
0111 info = 2;
0112 end
0113 end
0114
0115
0116 if info == 0
0117 if strcmp(extension,'.mat')
0118 try
0119 s = load(rootname);
0120 if isfield(s, 'mpc')
0121 s = s.mpc;
0122 else
0123 s.version = '1';
0124 end
0125 catch
0126 info = 3;
0127 end
0128 elseif strcmp(extension,'.m')
0129 try
0130 s = feval(rootname);
0131 catch
0132 info = 4;
0133 end
0134 if info == 0 && ~isstruct(s)
0135 clear s;
0136 s.version = '1';
0137 if expect_gencost
0138 try
0139 [s.baseMVA, s.bus, s.gen, s.branch, ...
0140 s.areas, s.gencost] = feval(rootname);
0141 catch
0142 info = 4;
0143 end
0144 else
0145 if return_as_struct
0146 try
0147 [s.baseMVA, s.bus, s.gen, s.branch, ...
0148 s.areas, s.gencost] = feval(rootname);
0149 catch
0150 try
0151 [s.baseMVA, s.bus, s.gen, s.branch] = feval(rootname);
0152 catch
0153 info = 4;
0154 end
0155 end
0156 else
0157 try
0158 [s.baseMVA, s.bus, s.gen, s.branch] = feval(rootname);
0159 catch
0160 info = 4;
0161 end
0162 end
0163 end
0164 end
0165 if info == 4 && exist([rootname '.m'], 'file') == 2
0166 info = 5;
0167 err5 = lasterr;
0168 end
0169 end
0170 end
0171 elseif isstruct(casefile)
0172 s = casefile;
0173 else
0174 info = 1;
0175 end
0176
0177
0178 if info == 0
0179
0180 if ~( isfield(s,'baseMVA') && isfield(s,'bus') && ...
0181 isfield(s,'gen') && isfield(s,'branch') ) || ...
0182 ( expect_gencost && ~isfield(s, 'gencost') ) || ...
0183 ( expect_areas && ~isfield(s,'areas') )
0184 info = 5;
0185 err5 = 'missing data';
0186 else
0187
0188 if isfield(s, 'areas') && isempty(s.areas) && ~expect_areas
0189 s = rmfield(s, 'areas');
0190 end
0191
0192
0193 mpc = s;
0194 if ~isfield(mpc, 'version')
0195 if size(mpc.gen, 2) < 21
0196 mpc.version = '1';
0197 else
0198 mpc.version = '2';
0199 end
0200 end
0201 if strcmp(mpc.version, '1')
0202
0203 [mpc.gen, mpc.branch] = mpc_1to2(mpc.gen, mpc.branch);
0204 mpc.version = '2';
0205 end
0206 end
0207 end
0208
0209
0210 if return_as_struct
0211 bus = info;
0212 end
0213
0214 if info == 0
0215 if return_as_struct
0216 baseMVA = mpc;
0217 else
0218 baseMVA = mpc.baseMVA;
0219 bus = mpc.bus;
0220 gen = mpc.gen;
0221 branch = mpc.branch;
0222 if expect_gencost
0223 if expect_areas
0224 areas = mpc.areas;
0225 gencost = mpc.gencost;
0226 else
0227 areas = mpc.gencost;
0228 end
0229 end
0230 end
0231 else
0232 if nargout == 2 || nargout == 7
0233 if return_as_struct
0234 baseMVA = struct([]);
0235 else
0236 baseMVA = []; bus = []; gen = []; branch = [];
0237 areas = []; gencost = [];
0238 end
0239 else
0240 switch info
0241 case 1,
0242 error('loadcase: input arg should be a struct or a string containing a filename');
0243 case 2,
0244 error('loadcase: specified case not in MATLAB''s search path');
0245 case 3,
0246 error('loadcase: specified MAT file does not exist');
0247 case 4,
0248 error('loadcase: specified M file does not exist');
0249 case 5,
0250 error('loadcase: syntax error or undefined data matrix(ices) in the file\n%s', err5);
0251 otherwise,
0252 error('loadcase: unknown error');
0253 end
0254 end
0255 end
0256
0257
0258 function [gen, branch] = mpc_1to2(gen, branch)
0259
0260
0261 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0262 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0263 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0264 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0265 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0266 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0267
0268
0269
0270 if size(gen, 2) > APF
0271 error('mpc_1to2: gen matrix appears to already be in version 2 format');
0272 end
0273 shift = MU_PMAX - PMIN - 1;
0274 tmp = num2cell([MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] - shift);
0275 [MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN] = deal(tmp{:});
0276
0277
0278 tmp = zeros(size(gen, 1), shift);
0279 if size(gen, 2) >= MU_QMIN
0280 gen = [ gen(:, 1:PMIN) tmp gen(:, MU_PMAX:MU_QMIN) ];
0281 else
0282 gen = [ gen(:, 1:PMIN) tmp ];
0283 end
0284
0285
0286
0287 shift = PF - BR_STATUS - 1;
0288 tmp = num2cell([PF, QF, PT, QT, MU_SF, MU_ST] - shift);
0289 [PF, QF, PT, QT, MU_SF, MU_ST] = deal(tmp{:});
0290
0291
0292 tmp = ones(size(branch, 1), 1) * [-360 360];
0293 tmp2 = zeros(size(branch, 1), 2);
0294 if size(branch, 2) >= MU_ST
0295 branch = [ branch(:, 1:BR_STATUS) tmp branch(:, PF:MU_ST) tmp2 ];
0296 elseif size(branch, 2) >= QT
0297 branch = [ branch(:, 1:BR_STATUS) tmp branch(:, PF:QT) ];
0298 else
0299 branch = [ branch(:, 1:BR_STATUS) tmp ];
0300 end