0001 function fname_out = save2psse(fname, mpc, rawver)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0026 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0027 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0028 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0029 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0030 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0031 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0032 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0033 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0034 c = idx_dcline;
0035
0036
0037 [pathstr, fcn_name, extension] = fileparts(fname);
0038 if isempty(extension)
0039 extension = '.raw';
0040 end
0041 fname = fullfile(pathstr, [fcn_name extension]);
0042
0043
0044 [fd, msg] = fopen(fname, 'wt');
0045 if fd == -1
0046 error(['save2psse: ', msg]);
0047 end
0048
0049
0050 i2e = mpc.bus(:, BUS_I);
0051 e2i = sparse(max(i2e), 1);
0052 e2i(i2e) = (1:size(mpc.bus, 1))';
0053
0054
0055 fprintf(fd, '0, %d, 33, 0, 0, 60 / %s - MATPOWER %s\n', mpc.baseMVA, datestr(clock), mpver);
0056 fprintf(fd, '\n');
0057 fprintf(fd, '\n');
0058
0059
0060 nb = size(mpc.bus, 1);
0061 if isfield(mpc, 'bus_name')
0062 maxlen = max(cellfun(@length, mpc.bus_name));
0063 tmp = cell2mat(cellfun( @(s)sprintf('%-*s', max(maxlen, 12), s), ...
0064 mpc.bus_name, 'UniformOutput', 0 ));
0065 bn = tmp(:, 1:12);
0066 else
0067 bn = cell2mat(cellfun( @(s)sprintf('BUS %-8d', s), ...
0068 num2cell(mpc.bus(:, BUS_I)), 'UniformOutput', 0 ));
0069 end
0070
0071 fprintf(fd, '%6d, ''%c%c%c%c%c%c%c%c%c%c%c%c'', %9.7g, %d, %4d, %4d, %d, %11.9g, %11.9g, %4g, %4g, %4g, %4g\n', ...
0072 [ mpc.bus(:, BUS_I) double(bn) ...
0073 mpc.bus(:, [BASE_KV BUS_TYPE BUS_AREA ZONE]) ones(nb, 1) ...
0074 mpc.bus(:, [VM VA VMAX VMIN VMAX VMIN]) ...
0075 ]');
0076 fprintf(fd, '0 / END OF BUS DATA, BEGIN LOAD DATA\n');
0077
0078
0079 ild = find(mpc.bus(:, PD) ~= 0 | mpc.bus(:, PD) ~= 0);
0080 nld = length(ild);
0081 if nld
0082
0083 fprintf(fd, '%6d, %2d, %d, %4d, %4d, %9.7g, %9.7g, %.9g, %.9g, %.9g, %.9g, %d, %d, %d\n', ...
0084 [ mpc.bus(ild, BUS_I) ones(nld, 2) ...
0085 mpc.bus(ild, [BUS_AREA ZONE PD QD]) ...
0086 zeros(nld, 4) ones(nld, 2) zeros(nld, 1) ...
0087 ]');
0088 end
0089 idl = find(isload(mpc.gen));
0090 ndl = length(idl);
0091 ib = e2i(mpc.gen(idl, GEN_BUS));
0092 if ndl
0093
0094 fprintf(fd, '%6d, %2d, %d, %4d, %4d, %9.7g, %9.7g, %.9g, %.9g, %.9g, %.9g, %d, %d, %d\n', ...
0095 [ mpc.bus(ib, BUS_I) ones(nld, 2) mpc.bus(ib, [BUS_AREA ZONE]) ...
0096 -mpc.gen(idl, [PMIN QMIN]) ...
0097 zeros(nld, 4) ones(nld, 3) ...
0098 ]');
0099 end
0100
0101 fprintf(fd, '0 / END OF LOAD DATA, BEGIN FIXED SHUNT DATA\n');
0102
0103
0104 ifs = find(mpc.bus(:, GS) ~= 0 | mpc.bus(:, BS) ~= 0);
0105 nfs = length(ifs);
0106 if nfs
0107
0108 fprintf(fd, '%6d, %d, %d, %7g, %7g\n', ...
0109 [ mpc.bus(ifs, BUS_I) ones(nfs, 2) mpc.bus(ifs, [GS BS]) ]');
0110 end
0111 fprintf(fd, '0 / END OF FIXED SHUNT DATA, BEGIN GENERATOR DATA\n');
0112
0113
0114 ig = find(~isload(mpc.gen));
0115 ng = length(ig);
0116 wind = zeros(ng, 1);
0117 if isfield(mpc, 'gentype')
0118 iw = find(cellfun(@(s)strcmp(s,'wind'), mpc.genfuel));
0119 wind(iw) = 1;
0120 end
0121 if isfield(mpc, 'genfuel')
0122 iw = find(cellfun(@(s)strcmp(s(1),'W'), mpc.gentype));
0123 wind(iw) = 1;
0124 end
0125 if ng
0126
0127 fprintf(fd, '%6d, %2d, %9.7g, %9.7g, %9.7g, %9.7g, %8.7g, %d, %7g, %g, %g, %g, %g, %g, %d, %g, %9.7g, %9.7g, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n', ...
0128 [ mpc.gen(ig, GEN_BUS) ones(ng, 1) mpc.gen(ig, [PG QG QMAX QMIN VG]) ...
0129 zeros(ng, 1) mpc.gen(ig, MBASE) zeros(ng, 1) ones(ng, 1) ...
0130 zeros(ng, 2) ones(ng, 1) (mpc.gen(ig, GEN_STATUS) > 0) ...
0131 100*ones(ng, 1) mpc.gen(ig, [PMAX PMIN]) ones(ng, 2) ...
0132 zeros(ng, 1) ones(ng, 1) zeros(ng, 1) ones(ng, 1) ...
0133 zeros(ng, 1) ones(ng, 1) wind ones(ng, 1) ...
0134 ]');
0135 end
0136
0137 fprintf(fd, '0 / END OF GENERATOR DATA, BEGIN BRANCH DATA\n');
0138
0139
0140 il = find(mpc.branch(:, TAP) == 0 & mpc.branch(:, SHIFT) == 0);
0141 nl = length(il);
0142 if nl
0143
0144 fprintf(fd, '%6d, %6d, %d, %8.7g, %8.7g, %8.7g, %7g, %7g, %7g, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n', ...
0145 [ mpc.branch(il, [F_BUS T_BUS]) ...
0146 generate_ckt_num(mpc.branch(il, [F_BUS T_BUS])) ...
0147 mpc.branch(il, [BR_R BR_X BR_B RATE_A RATE_B RATE_C]) ...
0148 zeros(nl, 4) mpc.branch(il, [BR_STATUS]) ones(nl, 1) ...
0149 zeros(nl, 1) ones(nl, 2) zeros(nl, 1) ones(nl, 1) ...
0150 zeros(nl, 1) ones(nl, 1) zeros(nl, 1) ones(nl, 1) ...
0151 ]');
0152 end
0153 fprintf(fd, '0 / END OF BRANCH DATA, BEGIN TRANSFORMER DATA\n');
0154
0155
0156 it = find(mpc.branch(:, TAP) ~= 0 | mpc.branch(:, SHIFT) ~= 0);
0157 nt = length(it);
0158 if nt
0159
0160
0161
0162 fprintf(fd, '%6d, %6d, %d, %d, %d, %d, %d, %d, %d, %d, '' '', %d, %d, %d, %d, %d, %d, %d, %d, %d, '' ''\n%g, %g, %g\n%5g, %d, %5g, %7g, %7g, %7g, %d, %d, %g, %g, %g, %g, %d, %d, %g, %g, %g\n%5g, %g\n', ...
0163 [ mpc.branch(it, [F_BUS T_BUS]) zeros(nt, 1) ...
0164 generate_ckt_num(mpc.branch(it, [F_BUS T_BUS])) ones(nt, 3) ...
0165 zeros(nt, 2) 2*ones(nt, 1) mpc.branch(it, BR_STATUS) ones(nt, 2) ...
0166 zeros(nt, 1) ones(nt, 1) zeros(nt, 1) ones(nt, 1) ...
0167 zeros(nt, 1) ones(nt, 1) ...
0168 mpc.branch(it, [BR_R BR_X]) mpc.baseMVA*ones(nt, 1) ...
0169 mpc.branch(it, TAP) zeros(nt, 1) mpc.branch(it, SHIFT) ...
0170 mpc.branch(it, [RATE_A RATE_B RATE_C]) zeros(nt, 2) ...
0171 1.1*ones(nt, 1) 0.9*ones(nt, 1) 1.1*ones(nt, 1) 0.9*ones(nt, 1) ...
0172 33*ones(nt, 1) zeros(nt, 4) ...
0173 ones(nt, 1) zeros(nt, 1) ...
0174 ]');
0175 end
0176 fprintf(fd, '0 / END OF TRANSFORMER DATA, BEGIN AREA DATA\n');
0177
0178
0179
0180 fprintf(fd, '0 / END OF AREA DATA, BEGIN TWO-TERMINAL DC DATA\n');
0181
0182
0183 if isfield(mpc, 'dcline')
0184 ndc = size(mpc.dcline, 1);
0185 if ndc
0186 dn = cell2mat(cellfun( @(s)sprintf('DCLINE %-5d', s), ...
0187 num2cell((1:ndc)'), 'UniformOutput', 0 ));
0188
0189
0190 fprintf(fd, '''%c%c%c%c%c%c%c%c%c%c%c%c'', 1, 0.0, %9.7g, 500, 0.0, 0.0, 0.0, I, 0.0, 20, 1.0\n%6d, 1, 25.0, 15.0, 0, 0, %8.7g, 1.0, 1.0, 1.5, 0.51, 0.00625, 0, 0, 0, ''1'', 0.0\n%6d, 1, 25.0, 15.0, 0, 0, %8.7g, 1.0, 1.0, 1.5, 0.51, 0.00625, 0, 0, 0, ''1'', 0.0\n', ...
0191 [ double(dn) mpc.dcline(:, c.PF) ...
0192 mpc.dcline(:, c.F_BUS) ...
0193 mpc.bus(e2i(mpc.dcline(:, c.F_BUS)), BASE_KV) ...
0194 mpc.dcline(:, c.T_BUS) ...
0195 mpc.bus(e2i(mpc.dcline(:, c.T_BUS)), BASE_KV) ...
0196 ]');
0197 end
0198 end
0199
0200 fprintf(fd, '0 / END OF TWO-TERMINAL DC DATA, BEGIN VOLTAGE SOURCE CONVERTER DATA\n');
0201
0202
0203
0204
0205
0206 fprintf(fd, '0 / END OF VOLTAGE SOURCE CONVERTER DATA, BEGIN IMPEDANCE CORRECTION DATA\n');
0207
0208
0209
0210 fprintf(fd, '0 / END OF IMPEDANCE CORRECTION DATA, BEGIN MULTI-TERMINAL DC DATA\n');
0211
0212
0213
0214
0215
0216
0217 fprintf(fd, '0 / END OF MULTI-TERMINAL DC DATA, BEGIN MULTI-SECTION LINE DATA\n');
0218
0219
0220
0221 fprintf(fd, '0 / END OF MULTI-SECTION LINE DATA, BEGIN ZONE DATA\n');
0222
0223
0224
0225 fprintf(fd, '0 / END OF ZONE DATA, BEGIN INTER-AREA TRANSFER DATA\n');
0226
0227
0228
0229 fprintf(fd, '0 / END OF INTER-AREA TRANSFER DATA, BEGIN OWNER DATA\n');
0230
0231
0232
0233 fprintf(fd, '0 / END OF OWNER DATA, BEGIN FACTS CONTROL DEVICE DATA\n');
0234
0235
0236
0237 fprintf(fd, '0 / END OF FACTS CONTROL DEVICE DATA, BEGIN SWITCHED SHUNT DATA\n');
0238
0239
0240
0241 fprintf(fd, '0 / END OF SWITCHED SHUNT DATA, BEGIN GNE DEVICE DATA\n');
0242
0243
0244 fprintf(fd, '0 / END OF GNE DEVICE DATA, BEGIN INDUCTION MACHINE DATA\n');
0245
0246
0247 fprintf(fd, '0 / END OF INDUCTION MACHINE DATA\n');
0248
0249
0250 fprintf(fd, 'Q\n');
0251
0252
0253 if fd ~= 1
0254 fclose(fd);
0255 end
0256
0257 if nargout > 0
0258 fname_out = fname;
0259 end
0260
0261
0262 function ckt = generate_ckt_num(ft, ckt)
0263
0264
0265 n = size(ft, 1);
0266 if nargin < 2
0267 ckt = zeros(n, 1);
0268 end
0269 ckt = ckt + 1;
0270 [Au, iA, iAu] = unique(ft, 'rows', 'first');
0271 k = find(~ismember([1:n]', iA));
0272 if k
0273 ckt(k) = generate_ckt_num(ft(k, :), ckt(k));
0274 end