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