Home > matpower7.1 > lib > savechgtab.m

savechgtab

PURPOSE ^

SAVECHGTAB Save a change table to a file.

SYNOPSIS ^

function fname_out = savechgtab(fname, chgtab, warnings)

DESCRIPTION ^

SAVECHGTAB  Save a change table to a file.
   SAVECHGTAB(FNAME, CHGTAB)
   SAVECHGTAB(FNAME, CHGTAB, WARNINGS)
   FNAME = SAVECHGTAB(FNAME, ...)

   Writes a CHGTAB, suitable for use with APPLY_CHANGES to a file
   specified by FNAME. If FNAME ends with '.mat' it saves CHGTAB
   and WARNINGS to a MAT-file as the variables 'chgtab' and 'warnings',
   respectively. Otherwise, it saves an M-file function that returns
   the CHGTAB, with the optional WARNINGS in comments.

   Optionally returns the filename, with extension added if necessary.

   Input:
       FNAME :  name of the file to be saved
       CHGTAB :  change table suitable for use with APPLY_CHANGES
       WARNINGS : optional cell array of warning messages (to be
                   included in comments), such as those returned by
                   PSSECON2CHGTAB

   Output(s):
       FNAME :  name of the file, with extention added if necessary

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function fname_out = savechgtab(fname, chgtab, warnings)
0002 %SAVECHGTAB  Save a change table to a file.
0003 %   SAVECHGTAB(FNAME, CHGTAB)
0004 %   SAVECHGTAB(FNAME, CHGTAB, WARNINGS)
0005 %   FNAME = SAVECHGTAB(FNAME, ...)
0006 %
0007 %   Writes a CHGTAB, suitable for use with APPLY_CHANGES to a file
0008 %   specified by FNAME. If FNAME ends with '.mat' it saves CHGTAB
0009 %   and WARNINGS to a MAT-file as the variables 'chgtab' and 'warnings',
0010 %   respectively. Otherwise, it saves an M-file function that returns
0011 %   the CHGTAB, with the optional WARNINGS in comments.
0012 %
0013 %   Optionally returns the filename, with extension added if necessary.
0014 %
0015 %   Input:
0016 %       FNAME :  name of the file to be saved
0017 %       CHGTAB :  change table suitable for use with APPLY_CHANGES
0018 %       WARNINGS : optional cell array of warning messages (to be
0019 %                   included in comments), such as those returned by
0020 %                   PSSECON2CHGTAB
0021 %
0022 %   Output(s):
0023 %       FNAME :  name of the file, with extention added if necessary
0024 
0025 %   MATPOWER
0026 %   Copyright (c) 2017, Power Systems Engineering Research Center (PSERC)
0027 %   by Ray Zimmerman, PSERC Cornell
0028 %
0029 %   This file is part of MATPOWER.
0030 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0031 %   See https://matpower.org for more info.
0032 
0033 %% handle input args
0034 if nargin < 3
0035     warnings = {};
0036 end
0037 
0038 %% define named indices into data matrices
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 %% constant names
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', ...  %% use val+3
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 %% verify valid filename
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 %% open and write the file
0073 if strcmp(upper(extension), '.MAT')     %% MAT-file
0074     save(fname, 'chgtab', 'warnings');
0075 else                                %% M-file
0076     %% open file
0077     [fd, msg] = fopen(fname, 'wt');     %% print it to an M-file
0078     if fd == -1
0079         error(['savechgtab: ', msg]);
0080     end
0081     
0082     %% function header, etc.
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', ...       %% CT_LABEL, CT_PROB
0101             chgtab(c, CT_LABEL), chgtab(c, CT_PROB)');
0102         switch chgtab(c, CT_TABLE)          %% 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', ...%% CT_ROW, %% CT_COL, CT_CHGTYPE, CT_NEWVAL
0149             chgtab(c, CT_ROW), col, chgtypes{chgtab(c, CT_CHGTYPE)}, chgtab(c, CT_NEWVAL));
0150     end
0151     fprintf(fd, '];\n');
0152 
0153     %% close file
0154     if fd ~= 1
0155         fclose(fd);
0156     end
0157 end
0158 
0159 if nargout > 0
0160     fname_out = fname;
0161 end

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005