0001 function fname_out = savechgtab(fname, chgtab, warnings)
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 if nargin < 3
0035 warnings = {};
0036 end
0037
0038
0039 [CT_LABEL, CT_PROB, CT_TABLE, CT_TBUS, CT_TGEN, CT_TBRCH, CT_TAREABUS, ...
0040 CT_TAREAGEN, CT_TAREABRCH, CT_ROW, CT_COL, CT_CHGTYPE, CT_REP, ...
0041 CT_REL, CT_ADD, CT_NEWVAL, CT_TLOAD, CT_TAREALOAD, CT_LOAD_ALL_PQ, ...
0042 CT_LOAD_FIX_PQ, CT_LOAD_DIS_PQ, CT_LOAD_ALL_P, CT_LOAD_FIX_P, ...
0043 CT_LOAD_DIS_P, CT_TGENCOST, CT_TAREAGENCOST, CT_MODCOST_F, ...
0044 CT_MODCOST_X] = idx_ct;
0045
0046
0047 bus_cols = {'BUS_I', 'BUS_TYPE', 'PD', 'QD', 'GS', 'BS', 'BUS_AREA', ...
0048 'VM', 'VA', 'BASE_KV', 'ZONE', 'VMAX', 'VMIN'};
0049 br_cols = {'F_BUS', 'T_BUS', 'BR_R', 'BR_X', 'BR_B', 'RATE_A', ...
0050 'RATE_B', 'RATE_C', 'TAP', 'SHIFT', 'BR_STATUS', 'ANGMIN', 'ANGMAX'};
0051 gen_cols = {'GEN_BUS', 'PG', 'QG', 'QMAX', 'QMIN', 'VG', 'MBASE', ...
0052 'GEN_STATUS', 'PMAX', 'PMIN', 'PC1', 'PC2', 'QC1MIN', 'QC1MAX', ...
0053 'QC2MIN', 'QC2MAX', 'RAMP_AGC', 'RAMP_10', 'RAMP_30', 'RAMP_Q', 'APF'};
0054 gc_cols = {'CT_MODCOST_X', 'CT_MODCOST_F', '0', ...
0055 'MODEL', 'STARTUP', 'SHUTDOWN', 'NCOST', 'COST'};
0056 load_cols = {'CT_LOAD_ALL_PQ', 'CT_LOAD_FIX_PQ', 'CT_LOAD_DIS_PQ', ...
0057 'CT_LOAD_ALL_P', 'CT_LOAD_FIX_P', 'CT_LOAD_DIS_P'};
0058 chgtypes = {'CT_REP', 'CT_REL', 'CT_ADD'};
0059
0060
0061 [pathstr, fcn_name, extension] = fileparts(fname);
0062 if isempty(extension)
0063 extension = '.m';
0064 end
0065 if regexp(fcn_name, '\W')
0066 old_fcn_name = fcn_name;
0067 fcn_name = regexprep(fcn_name, '\W', '_');
0068 fprintf('WARNING: ''%s'' is not a valid function name, changed to ''%s''\n', old_fcn_name, fcn_name);
0069 end
0070 fname = fullfile(pathstr, [fcn_name extension]);
0071
0072
0073 if strcmp(upper(extension), '.MAT')
0074 save(fname, 'chgtab', 'warnings');
0075 else
0076
0077 [fd, msg] = fopen(fname, 'wt');
0078 if fd == -1
0079 error(['savechgtab: ', msg]);
0080 end
0081
0082
0083 fprintf(fd, 'function chgtab = %s\n', fcn_name);
0084 fprintf(fd, '%%%s Returns a change table suitable for use with APPLY_CHANGES\n', upper(fcn_name));
0085
0086 if ~isempty(warnings)
0087 fprintf(fd, '%%\n');
0088 fprintf(fd, '%% Warnings:\n');
0089 for k = 1:length(warnings)
0090 fprintf(fd, '%% %s\n', warnings{k});
0091 end
0092 end
0093
0094 fprintf(fd, '\ndefine_constants;\n');
0095
0096 fprintf(fd, '\n%%%% Change Table\n');
0097 fprintf(fd, '%%\tlabel\tprob\ttable\trow\tcol\tchgtype\tnewval\n');
0098 fprintf(fd, 'chgtab = [\n');
0099 for c = 1:size(chgtab, 1)
0100 fprintf(fd, '\t%d\t%g\t', ...
0101 chgtab(c, CT_LABEL), chgtab(c, CT_PROB)');
0102 switch chgtab(c, CT_TABLE)
0103 case CT_TBUS
0104 fprintf(fd, '%s\t', 'CT_TBUS');
0105 col = bus_cols{chgtab(c, CT_COL)};
0106 case CT_TGEN
0107 fprintf(fd, '%s\t', 'CT_TGEN');
0108 col = gen_cols{chgtab(c, CT_COL)};
0109 case CT_TGENCOST
0110 fprintf(fd, '%s\t', 'CT_TGENCOST');
0111 if chgtab(c, CT_COL)+3 > 8
0112 col = sprintf('%s+%d', gc_cols{5+3}, chgtab(c, CT_COL)-5);
0113 else
0114 col = gc_cols{chgtab(c, CT_COL)+3};
0115 end
0116 case CT_TBRCH
0117 fprintf(fd, '%s\t', 'CT_TBRCH');
0118 col = br_cols{chgtab(c, CT_COL)};
0119 case CT_TLOAD
0120 fprintf(fd, '%s\t', 'CT_TLOAD');
0121 col = load_cols{abs(chgtab(c, CT_COL))};
0122 if chgtab(c, CT_COL) < 0
0123 col = ['-' col];
0124 end
0125 case CT_TAREABUS
0126 fprintf(fd, '%s\t', 'CT_TAREABUS');
0127 col = bus_cols{chgtab(c, CT_COL)};
0128 case CT_TAREAGEN
0129 fprintf(fd, '%s\t', 'CT_TAREAGEN');
0130 col = gen_cols{chgtab(c, CT_COL)};
0131 case CT_TAREAGENCOST
0132 fprintf(fd, '%s\t', 'CT_TAREAGENCOST');
0133 if chgtab(c, CT_COL)+3 > 8
0134 col = sprintf('%s+%d', gc_cols{5+3}, chgtab(c, CT_COL)-5);
0135 else
0136 col = gc_cols{chgtab(c, CT_COL)+3};
0137 end
0138 case CT_TAREABRCH
0139 fprintf(fd, '%s\t', 'CT_TAREABRCH');
0140 col = br_cols{chgtab(c, CT_COL)};
0141 case CT_TAREALOAD
0142 fprintf(fd, '%s\t', 'CT_TAREALOAD');
0143 col = load_cols{abs(chgtab(c, CT_COL))};
0144 if chgtab(c, CT_COL) < 0
0145 col = ['-' col];
0146 end
0147 end
0148 fprintf(fd, '%g\t%s\t%s\t%g;\n', ...
0149 chgtab(c, CT_ROW), col, chgtypes{chgtab(c, CT_CHGTYPE)}, chgtab(c, CT_NEWVAL));
0150 end
0151 fprintf(fd, '];\n');
0152
0153
0154 if fd ~= 1
0155 fclose(fd);
0156 end
0157 end
0158
0159 if nargout > 0
0160 fname_out = fname;
0161 end