Home > matpower4.0 > cdf2matp.m

cdf2matp

PURPOSE ^

CDF2MATP Converts data from IEEE Common Data Format to MATPOWER format.

SYNOPSIS ^

function cdf2matp(cdf_file, matp_file, mpcver)

DESCRIPTION ^

CDF2MATP Converts data from IEEE Common Data Format to MATPOWER format.
   CDF2MATP                         : prompts for input & output file names
   CDF2MATP(CDF_FILE, MATP_FILE)    : uses specified file names
   CDF2MATP(CDF_FILE, MATP_FILE, v) : and MATPOWER case version
                                      v = '1' or '2', default is '2'

   Optional arguments are the names of the input IEEE CDF file and the
   output MATPOWER case file. If these are not given you will be prompted
   to enter them.

   The IEEE CDF does not include some data need to run an optimal power
   flow. This script creates default values for some of this data as
   follows:

       Bus data:
           Vmin = 0.94 p.u.
           Vmax = 1.06 p.u.
       Gen data:
           Pmin = 0 MW
           Pmax = Pg + baseMVA
       Gen cost data:
           Quadratic costs with:
               c2 = 10 / Pg, c1 = 20, c0 = 0, if Pg is non-zero, and
               c2 = 0.01,    c1 = 40, c0 = 0, if Pg is zero
           This should yield an OPF solution "close" to the
           existing solution (assuming it is a solved case)
           with lambdas near $40/MWh. See 'help caseformat'
           for details on the cost curve format.

   CDF2MATP may modify some of the data which are "infeasible" for
   running optimal power flow. If so, warning information will be
   printed out on screen.

   Note: Since our code can not handle transformers with variable tap,
   you may not expect to get exactly the same power flow solution
   using converted data. This is the case when we converted ieee300.cdf.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cdf2matp(cdf_file, matp_file, mpcver)
0002 %CDF2MATP Converts data from IEEE Common Data Format to MATPOWER format.
0003 %   CDF2MATP                         : prompts for input & output file names
0004 %   CDF2MATP(CDF_FILE, MATP_FILE)    : uses specified file names
0005 %   CDF2MATP(CDF_FILE, MATP_FILE, v) : and MATPOWER case version
0006 %                                      v = '1' or '2', default is '2'
0007 %
0008 %   Optional arguments are the names of the input IEEE CDF file and the
0009 %   output MATPOWER case file. If these are not given you will be prompted
0010 %   to enter them.
0011 %
0012 %   The IEEE CDF does not include some data need to run an optimal power
0013 %   flow. This script creates default values for some of this data as
0014 %   follows:
0015 %
0016 %       Bus data:
0017 %           Vmin = 0.94 p.u.
0018 %           Vmax = 1.06 p.u.
0019 %       Gen data:
0020 %           Pmin = 0 MW
0021 %           Pmax = Pg + baseMVA
0022 %       Gen cost data:
0023 %           Quadratic costs with:
0024 %               c2 = 10 / Pg, c1 = 20, c0 = 0, if Pg is non-zero, and
0025 %               c2 = 0.01,    c1 = 40, c0 = 0, if Pg is zero
0026 %           This should yield an OPF solution "close" to the
0027 %           existing solution (assuming it is a solved case)
0028 %           with lambdas near $40/MWh. See 'help caseformat'
0029 %           for details on the cost curve format.
0030 %
0031 %   CDF2MATP may modify some of the data which are "infeasible" for
0032 %   running optimal power flow. If so, warning information will be
0033 %   printed out on screen.
0034 %
0035 %   Note: Since our code can not handle transformers with variable tap,
0036 %   you may not expect to get exactly the same power flow solution
0037 %   using converted data. This is the case when we converted ieee300.cdf.
0038 
0039 %   MATPOWER
0040 %   $Id: cdf2matp.m,v 1.23 2010/04/26 19:45:26 ray Exp $
0041 %   by Deqiang (David) Gan, PSERC Cornell & Zhejiang University
0042 %   Copyright (c) 1996-2010 by Power System Engineering Research Center (PSERC)
0043 %
0044 %   This file is part of MATPOWER.
0045 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0046 %
0047 %   MATPOWER is free software: you can redistribute it and/or modify
0048 %   it under the terms of the GNU General Public License as published
0049 %   by the Free Software Foundation, either version 3 of the License,
0050 %   or (at your option) any later version.
0051 %
0052 %   MATPOWER is distributed in the hope that it will be useful,
0053 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0054 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0055 %   GNU General Public License for more details.
0056 %
0057 %   You should have received a copy of the GNU General Public License
0058 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0059 %
0060 %   Additional permission under GNU GPL version 3 section 7
0061 %
0062 %   If you modify MATPOWER, or any covered work, to interface with
0063 %   other modules (such as MATLAB code and MEX-files) available in a
0064 %   MATLAB(R) or comparable environment containing parts covered
0065 %   under other licensing terms, the licensors of MATPOWER grant
0066 %   you additional permission to convey the resulting work.
0067 
0068 %% define named indices into bus, gen, branch matrices
0069 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0070     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0071 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0072     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0073     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0074 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0075     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0076     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0077 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0078 [AREA_I, PRICE_REF_BUS] = idx_area;
0079 
0080 rev = '$Revision: 1.23 $';
0081 
0082 % ----- get the original load flow data -----
0083 if nargin < 1
0084     cdf_file =  input('Please enter IEEE Common Data Format file name : ', 's');
0085 end
0086 if nargin < 2
0087     matp_file = input('Please enter MATPOWER file name                : ', 's');
0088 end
0089 if nargin < 3
0090     mpcver = '2';
0091 end
0092 
0093 %% verify valid filename
0094 l = length(matp_file);
0095 rootname = [];
0096 if l > 2
0097     if strcmp(matp_file(l-1:l), '.m')
0098         rootname = matp_file(1:l-2);
0099         extension = '.m';
0100     elseif l > 4
0101         if strcmp(matp_file(l-3:l), '.mat')
0102             rootname = matp_file(1:l-4);
0103             extension = '.mat';
0104         end
0105     end
0106 end
0107 if isempty(rootname)
0108     rootname = matp_file;
0109     extension = '.m';
0110     matp_file = [rootname, extension];
0111 end
0112 
0113 % set up some comments
0114 comments = { sprintf('%s', upper(rootname)), ...
0115     '  Please see ''help caseformat'' for details on the case file format.', ...
0116     '  This data was converted from IEEE Common Data Format', ...
0117     sprintf('  (%s) on %s by cdf2matp, rev. %s', cdf_file, date, rev(12:end-2)), ...
0118     '  See end of file for warnings generated during conversion.', ...
0119     '' };
0120 warnings = {};
0121 
0122 fid = fopen(cdf_file, 'r');
0123 
0124 % get baseMVA
0125 while 1
0126     title = fgetl(fid);
0127     if length(findstr(title, '/'))==2   % make sure it's a real cdf file title
0128         comments{end+1} = title;
0129         baseMVA = str2num(title(32:37)); break;
0130     end
0131 end
0132 
0133 % find string 'BUS DATA FOLLOWS'
0134 while 1
0135     line = fgetl(fid);
0136     if line(1:16) == 'BUS DATA FOLLOWS', break, end
0137 end
0138 
0139 % ----- get bus data, feed them into matrix bus, gen, gencost
0140 ibus = 0;
0141 igen = 0;
0142 iarea = 0;
0143 
0144 while 1
0145     line = fgetl(fid);
0146     if line(1:4) == '-999', break, end
0147 
0148     % feed bus data
0149     ibus = ibus + 1;
0150     bus(ibus, BUS_I) = str2num(line(1:4));          % bus number
0151     bus(ibus, BUS_TYPE) = str2num(line(25:26));
0152     if bus(ibus, BUS_TYPE) == 0                     % bus type
0153         bus(ibus, BUS_TYPE) = 1;
0154     end
0155     if (bus(ibus, BUS_TYPE) < 2)                    % Pd
0156         bus(ibus, PD) = str2num(line(41:49)) - str2num(line(60:67));
0157     elseif (bus(ibus, BUS_TYPE) >= 2)
0158         bus(ibus, PD) = str2num(line(41:49));
0159     end
0160     bus(ibus, QD) = str2num(line(50:59));           % Qd
0161     bus(ibus, GS) = baseMVA*str2num(line(107:114)); % Gs
0162     bus(ibus, BS) = baseMVA*str2num(line(115:122)); % Bs
0163     bus(ibus, BUS_AREA) = str2num(line(19:20));     % area
0164     bus(ibus, VM) = str2num(line(28:33));           % Vm
0165     bus(ibus, VA) = str2num(line(34:40));           % Va
0166     bus(ibus, BASE_KV) = str2num(line(77:83));      % baseKV
0167     bus(ibus, ZONE) = str2num(line(21:23));         % zone
0168     bus(ibus, VMAX) = 1.06;                         % default voltage upper limit
0169     bus(ibus, VMIN) = 0.94;                         % default voltage lower limit
0170 
0171     % feed gen and gencost
0172     Pg = str2num(line(60:67));
0173     Qg = str2num(line(68:75));
0174     Qmax = str2num(line(91:98));
0175     Qmin = str2num(line(99:106));
0176     if bus(ibus, BUS_TYPE) >= 2
0177         igen = igen + 1;
0178         if bus(ibus, BUS_TYPE) == 3, refgen = igen; end
0179         gen(igen, GEN_BUS) = bus(ibus, BUS_I);      % bus number
0180         gen(igen, PG) = Pg;                         % Pg
0181         if gen(igen, PG) < 0                        % negative Pg is transformed as load
0182             bus(ibus, PD) = bus(ibus, PD) - gen(igen, PG);
0183             warnings{end+1} = sprintf('***** negative Pg at bus %g treated as Pd', bus(ibus, BUS_I));
0184             fprintf('\n %s', warnings{end});
0185             gen(igen, PG) = 0;
0186         end
0187         gen(igen, QG)   = Qg;                       % Qg
0188         gen(igen, QMAX) = Qmax;                     % Qmax
0189         gen(igen, QMIN) = Qmin;                     % Qmin
0190         if Qmax - Qmin < 0.01                       % Qmax is modified
0191             gen(igen, QMAX) = Qmin + 0.1 * baseMVA;
0192             warnings{end+1} = sprintf('***** Qmax = Qmin at generator at bus %4i (Qmax set to Qmin + %g)', bus(ibus, BUS_I), baseMVA/10);
0193             fprintf('\n %s', warnings{end});
0194         end
0195         gen(igen, VG)    = str2num(line(85:90));    % specified voltage
0196         gen(igen, MBASE) = baseMVA;                 % baseMVA
0197         gen(igen, GEN_STATUS) = 1;                  % default status is 'on'
0198         gen(igen, PMAX)  = gen(igen, 2) + baseMVA;  % Pmax
0199         gen(igen, PMIN)  = 0;                       % Pmin = 0 by default
0200 
0201         gencost(igen, MODEL)    = POLYNOMIAL;       % by default, sets the model as polynomial
0202         gencost(igen, STARTUP)  = 0;                % start up cost is zero by default
0203         gencost(igen, SHUTDOWN) = 0;                % shut down cost is zero by default
0204         gencost(igen, NCOST)    = 3;                % number of coefficients in polynomial cost
0205 %       gencost(igen, COST)     = 0.01;             % default c2
0206 %       gencost(igen, COST+1)   = 0.3;              % default c1
0207 %       gencost(igen, COST+2)   = 0.2;              % default c0
0208     end
0209 end
0210 
0211 totload = sum(bus(:, PD));
0212 totgen = sum(gen(:, PG));
0213 if totgen < 1.04 * totload
0214     gen(refgen, PMAX) = gen(refgen, PG) + 1.1 * totload - totgen;   % Pg at slack bus is modified
0215     warnings{end+1} = sprintf('***** Insufficient generation, setting Pmax at slack bus (bus %d) to %g', gen(refgen, [GEN_BUS, PMAX]));
0216     fprintf('\n %s', warnings{end});
0217 end
0218 
0219 % ----- set up the cost coefficients of generators
0220 ng = size(gen, 1);
0221 % gencost(:, COST)    = zeros(ng, 1);
0222 % gencost(:, COST+1)  = 100*ones(ng, 1) ./ (gen(:, PG) + 10*ones(ng, 1));
0223 % gencost(:, COST+2)  = 100*ones(ng, 1) ./ (gen(:, PG) + 10*ones(ng, 1));
0224 zg  = find(gen(:, PG) == 0);                %% for Pg = 0
0225 gencost(zg, COST)  = 0.01 * ones(size(zg));
0226 gencost(zg, COST+1) = 40 * ones(size(zg));
0227 nzg = find(gen(:, PG) ~= 0);                %% Pg non-zero
0228 gencost(nzg, COST) = 10 * ones(size(nzg)) ./ gen(nzg, PG);
0229 gencost(nzg, COST+1) = 20 * ones(size(nzg));
0230 gencost(:, COST+2) = zeros(ng, 1);
0231 
0232 % find string 'BRANCH DATA FOLLOWS'
0233 while 1
0234     line = fgetl(fid);
0235     if line(1:19) == 'BRANCH DATA FOLLOWS', break, end
0236 end
0237 
0238 % ----- get branch data, feed them into matrix branch
0239 k = 0;
0240 while 1
0241     line = fgetl(fid);
0242     if line(1:4) == '-999', break, end
0243 
0244     k = k + 1;
0245     branch(k, F_BUS)  = str2num(line(1:4));     % fbus (also the tap bus)
0246     branch(k, T_BUS)  = str2num(line(6:9));     % tbus
0247     branch(k, BR_R)   = str2num(line(20:29));   % R
0248     branch(k, BR_X)   = str2num(line(30:40));   % X
0249     branch(k, BR_B)   = str2num(line(41:50));   % B
0250     branch(k, RATE_A) = str2num(line(51:55));   % RATE A
0251     if branch(k, RATE_A) < 0.000001
0252         branch(k, RATE_A) = 99 * baseMVA;       % RATE A is modified
0253         warnings{end+1} = sprintf('***** MVA limit of branch %d - %d not given, set to %g', branch(k, [1, 2, 6]));
0254         fprintf('\n %s', warnings{end});
0255     end
0256     branch(k, RATE_B) = str2num(line(57:61));   % RATE B
0257     branch(k, RATE_C) = str2num(line(63:67));   % RATE C
0258     branch(k, TAP)    = str2num(line(77:82));   % transformer turns ratio
0259     branch(k, SHIFT)  = 0;                      % phase shifter can not be modelled
0260     branch(k, BR_STATUS) = 1;                   % by default, branch is on
0261 end
0262 fprintf('\n');
0263 fclose(fid);
0264 
0265 % put in struct
0266 mpc.baseMVA = baseMVA;
0267 mpc.bus     = bus;
0268 mpc.branch  = branch;
0269 mpc.gen     = gen;
0270 mpc.gencost = gencost;
0271 mpc = loadcase(mpc);    %% convert to internal (e.g. v. '2') case format
0272 
0273 % ----- write data in MATPOWER format -----
0274 matp_file = savecase(matp_file, comments, mpc, mpcver);
0275 
0276 % print conversion warnings as comments
0277 if ~isempty(warnings)
0278     if length(matp_file) < 4 || ~strcmp(matp_file(end-3:end), '.mat')
0279         fid = fopen(matp_file, 'at');       % open for append
0280         fprintf(fid, '\n%% Warnings from cdf2matp conversion:\n%%\n');
0281         for i=1:length(warnings)
0282             fprintf(fid, '%% %s\n', warnings{i});
0283         end
0284         fclose(fid);
0285     end
0286 end
0287 
0288 % to do:
0289 %   PTI format can not be handled

Generated on Mon 26-Jan-2015 14:56:45 by m2html © 2005