⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fm_cdf.m

📁 电力系统的psat
💻 M
字号:
function check = fm_cdf(filename, pathname, flag)% FM_CDF convert IEEE Common Data Format%        into PSAT data format%%CHECK = FM_CDF(FILENAME,PATHNAME,FLAG)%       FILENAME name of the file to be converted%       PATHNAME path of the file to be converted%       FLAG = 1 append original file as a comment%       FLAG = 0 do not append original file%%       CHECK = 1 conversion completed%       CHECK = 0 problem encountered (no data file created)%%Author:    Federico Milano%Date:      11-Nov-2002%Update:    09-Jun-2003%Version:   1.0.1%%E-mail:    fmilano@thunderbox.uwaterloo.ca%Web-site:  http://thunderbox.uwaterloo.ca/~fmilano%% Copyright (C) 2002-2005 Federico Milano%% This toolbox is free software; you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation; either version 2.0 of the License, or% (at your option) any later version.%% This toolbox is distributed in the hope that it will be useful, but% WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU% General Public License for more details.%% You should have received a copy of the GNU General Public License% along with this toolbox; if not, write to the Free Software% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,% USA.global Settingscheck = 1;if ~strcmp(pathname(end),filesep)  pathname = [pathname,filesep];endfm_dispfm_disp('Conversion from IEEE Common Data Format...');fm_disp(['Source data file "',pathname,filename,'"'])fid = fopen([pathname,filename]);b100 = blanks(100);if fid == -1,  fm_disp(['Can''t open file ',pathname,filename],2),  check = 0;  returnend% header, power base and frequency% ----------------------------------------------------------------------while 1  heading = fgetl(fid);  if strmatch('C',heading)    continue;  elseif strmatch('TAPE',heading)    fm_disp('IEEE file is in "TAPE" format.')  elseif strmatch('CARD',heading)    fm_disp('IEEE file is in "CARD" format.')  else    if length(heading) < 37      fm_disp('Header Row is in a wrong format. Conversion failed.',2);      check = 0;      return    end    break  endend    mvabas = str2num(heading(32:37));freq = 60;% load bus matrix% ----------------------------------------------------------------------counter = 0;while counter < 5  rigabus = fgetl(fid);  if rigabus == -1,    trovabus = [];    break,  end  trovabus = findstr(rigabus,'BUS');  if strmatch('BUS',rigabus),    break,  end  counter = counter + 1;endif isempty(trovabus)  fm_disp('No Bus Data found. Conversion failed.',2);  fclose(fid);  check = 0;  returnendnumriga = 0;while 1  numriga = numriga + 1;  riga = [fgetl(fid),' '];  if riga == -1    fm_disp('Bus Data abnormally terminated. Conversion failed.',2);    fclose(fid);    check = 0;    return  end  numbus = str2num(riga(1:5));  if numbus == -999; break; end  bus(numriga, 1) = numbus;  namebus(numriga,:) = riga(6:17);  typebus = str2num(riga(25:26));  if isempty(typebus)    typebus = 0;   end  switch typebus   case 0, bus(numriga,10) = 3;   case 1, bus(numriga,10) = 3;   case 2, bus(numriga,10) = 2;   case 3, bus(numriga,10) = 1;  end  bus(numriga, 2) = str2num(riga(28:33));   % voltage  bus(numriga, 3) = str2num(riga(34:40))*3.14159265358979/180; % angle  bus(numriga, 4) = str2num(riga(60:67));   % generation P  bus(numriga, 5) = str2num(riga(68:75));   % generation Q  bus(numriga, 6) = str2num(riga(41:49));   % load P  bus(numriga, 7) = str2num(riga(50:59));   % load Q  bus(numriga, 8) = str2num(riga(107:114)); % shunt conductance  bus(numriga, 9) = str2num(riga(115:122)); % shunt susceptance  bus(numriga,11) = str2num(riga(77:83));   % Voltage base (kV)  bus(numriga,12) = str2num(riga(91:98));   % Q max  bus(numriga,13) = str2num(riga(99:106));  % Q min  bus(numriga,[14,15,16,17,18]) = [mvabas, 1.2, 0.8, 1, freq]; % other data  if bus(numriga,11) == 0    bus(numriga,11) = 1;   end  check = (typebus <= 1);  bus(numriga,6) = bus(numriga,6)-check*bus(numriga,4);  bus(numriga,7) = bus(numriga,7)-check*bus(numriga,5);  endbus(:,4:7) = bus(:,4:7)/mvabas;bus(:,12:13) = bus(:,12:13)/mvabas;% load branch matrix% ----------------------------------------------------------------------rigabus = fgetl(fid);trovabranch = strmatch('BRANCH',rigabus);if isempty(trovabranch)  fm_disp('No Branch data found. Conversion Process Interrupted',2);  fclose(fid);  check = 0;  returnendnumriga = 0;while 1  numriga = numriga + 1;  riga = [fgetl(fid),b100];  if riga == -1    fm_disp('Branch data abnormally terminated. Conversion Process Interrupted',2);    fclose(fid);    check = 0;    return  end  fromline = str2num(riga(1:5));  if fromline == -999; break; end  toline = str2num(riga(6:10));  Line_con(numriga, 1) = fromline;              % from bus  Line_con(numriga, 2) = toline;                % to bus  a = str2num(riga(77:82));   if isempty(a)    a = 0;   end  Line_con(numriga, 11) = a;  % tap ratio  a = str2num(riga(84:90));   if isempty(a)    a = 0;   end  Line_con(numriga, 12) = a;  % phase shifter  V1 = bus(find(bus(:,1) == fromline),11);  if V1 == 0     V1 = 1;   end  V2 = bus(find(bus(:,1) == toline),11);  if V2 ~= 0    kt = V1/V2;   else     kt = 1;   end  %NL = str2num(riga(17)); if isempty(NL), NL = 1; end  if isempty(str2num(riga(19))), riga(19) = '0'; end  if str2num(riga(19)) == 0    Line_con(numriga,7) = 0;   else    Line_con(numriga,7) = kt;   end  if kt ~= 1    Line_con(numriga,7) = kt;   end  %Line_con(numriga, 8) = str2num(riga(20:29))/NL;  % resistance  %Line_con(numriga, 9) = str2num(riga(30:40))/NL;  % reactance  %Line_con(numriga,10) = str2num(riga(41:50))*NL;  % shunt  Line_con(numriga, 8) = str2num(riga(20:29));  % resistance  Line_con(numriga, 9) = str2num(riga(30:40));  % reactance  Line_con(numriga,10) = str2num(riga(41:50));  % shunt  Line_con(numriga,[3 4 5]) = [mvabas, V1, freq];  Line_con(numriga,13) = 0;endfclose(fid);% load original file into cell stringif flag == 1  orig_file = textread([pathname,filename], ...                       '%s','delimiter','\n','whitespace',''); endsizebus  = size(bus);sizeline = size(Line_con);% definition of file name for PSAT data fileextension = findstr(filename,'.');newfile = ['d_',fm_filenum(filename(1:extension(end)-1),1),'.m'];% open *.m file for writing datafid = fopen([pathname,newfile], 'wt');% Bus data: Bus.con% ----------------------------------------------------------------------count = fprintf(fid,['%% ',datestr(now,2),' File data originated from IEEE-CDF\n']);count = fprintf(fid,['%% \n']);count = fprintf(fid, ['%%  ', heading, '\n\n']);count = fprintf(fid, 'Bus.con = [ ...\n');for i = 1:sizebus(1)-1  count = fprintf(fid,'%4d %8.4g %8.4g %8.4g; ', bus(i,[1,11,2,3]));  if rem(i,5) == 0; count = fprintf(fid,'\n'); endendcount = fprintf(fid, '%4d %8.4g %8.4g %8.4g];\n\n\n', bus(end,[1,11,2,3]));% Swing Generator data: SW.con% ----------------------------------------------------------------------k = find(bus(:,10) == 1);if ~isempty(k)  swline = bus(k,[1,14,11,2,3,12,13,15,16,4,17]);  count = fprintf(fid, 'SW.con = [ ...\n');  format = '%4d %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %2u';  count = fprintf(fid,[format,';\n'],swline(1:end-1,:)');  count = fprintf(fid,[format,'];\n\n\n'],swline(end,:));end% PV Generator data: PV.con% ----------------------------------------------------------------------k = find(bus(:,10) == 2);if ~isempty(k)  pvline = bus(k,[1,14,11,4,2,12,13,15,16,17]);  count = fprintf(fid, 'PV.con = [ ...\n');  format = '%4d %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %2u';  count = fprintf(fid,[format,';\n'],pvline(1:end-1,:)');  count = fprintf(fid,[format,'];\n\n\n'],pvline(end,:));end% Constant Power Load data: PQ.con% ----------------------------------------------------------------------k = find(bus(:,6) ~= 0 | bus(:,7) ~= 0);if ~isempty(k)  pqline = bus(k,[1,14,11,6,7,15,16,17]);  count = fprintf(fid, 'PQ.con = [ ...\n');  format = '%4d %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %2u';  count = fprintf(fid,[format,';\n'],pqline(1:end-1,:)');  count = fprintf(fid,[format,'];\n\n\n'],pqline(end,:));end% Shunt Impedance data: Shunt.con% ----------------------------------------------------------------------k = find(bus(:,8) ~= 0 | bus(:,9) ~= 0);if ~isempty(k)  count = fprintf(fid, 'Shunt.con = [ ...\n');  shuntline = bus(k,[1,14,11,18,8,9]);  count = fprintf(fid,'%4d %8.4g %8.4g %8.4g %8.4g %8.4g;\n',shuntline(1:end-1,:)');  count = fprintf(fid,'%4d %8.4g %8.4g %8.4g %8.4g %8.4g];\n\n\n',shuntline(end,:));end% Branch data: Line.con% ----------------------------------------------------------------------count = fprintf(fid, 'Line.con = [ ...\n');format = '%4d %4d %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g';count = fprintf(fid,[format,';\n'],Line_con(1:end-1,:)');count = fprintf(fid,[format,'];\n\n\n'],Line_con(end,:));% Bus Names: Varname.bus% ----------------------------------------------------------------------count = fprintf(fid, 'Varname.bus = {...\n      ');for i = 1:length(namebus(:,1))-1  count = fprintf(fid, ['''', deblank(namebus(i,:)),'''; ']);  if rem(i,5) == 0; count = fprintf(fid,'\n      '); endendcount = fprintf(fid, ['''', deblank(namebus(end,:)),'''};\n\n']);% append original data fileif flag == 1  count = fprintf(fid, '\n\n%% ORIGINAL DATA FILE: \n\n');  for i = 1:length(orig_file); count = fprintf(fid,'%% %s \n\n',orig_file{i,1}); endend% end of operationsfm_disp(['Conversion into data file "',pathname,newfile,'" completed.'])if Settings.beep, beep, endcount = fclose(fid);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -