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