📄 fm_vst.m
字号:
function check = fm_vst(filename, pathname, flag)% FM_VST Conversion from Voltage Stability Toolbox (VST)% into PSAT data format%% CHECK = FM_VST(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%Version: 1.0.0%%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 = 0;pathname = [pathname,filesep];fm_dispfm_disp('Conversion from VST Data Format ...');fm_disp(['Source data file "',pathname,filename,'"'])fid = fopen([pathname,filename]);if fid == -1, fm_disp(['Can''t open file ',pathname,filename],2), return,end% load bus data% ----------------------------------------------------------------------riga = fgetl(fid);try, nbus = str2num(riga); catch, nbus = 0; endif ~nbus fm_disp('No Bus Data found. Conversion Process Interrupted',2); fclose(fid); returnendbus_idx = zeros(nbus,1);bus_num = zeros(nbus,1);bus_name = cell(nbus,1);bus_type = zeros(nbus,1);bus_data = zeros(nbus,6);for i = 1:nbus riga = fgetl(fid); if riga == -1 fm_disp('Bus Data abnormally terminated. Conversion Process Interrupted',2); fclose(fid); return end try bus_idx(i) = str2num(riga(1:5)); bus_num(i) = str2num(riga(7:11)); bus_name{i,1} = deblank(riga(13:24)); bus_type(i) = str2num(riga(26)); bus_data(i,:) = str2num(riga(28:end)); catch fm_disp(['Something wrong in the bus data (row ',int2srt(1+i),'). Conversion Process Interrupted'],2); fclose(fid); return endendriga = fgetl(fid);try, bus_end = str2num(riga); catch, bus_end = 0; endif bus_end ~= -999 | riga == -1 fm_disp('Bus Data abnormally terminated. Conversion Process Interrupted',2); fclose(fid); returnend% load branch data% ----------------------------------------------------------------------riga = fgetl(fid);try, nbranch = str2num(riga); catch, nbranch = 0; endif ~nbranch fm_disp('No Branch Data found. Conversion Process Interrupted',2); returnendbus_from = zeros(nbranch,1);bus_to = zeros(nbranch,1);bus_ctrl = zeros(nbranch,1);branch_type = zeros(nbranch,1);branch_data = zeros(nbranch,5);for i = 1:nbranch riga = fgetl(fid); if riga == -1 fm_disp('Branch Data abnormally terminated. Conversion Process Interrupted',2); fclose(fid); return end try bus_from(i) = str2num(riga(1:5)); bus_to(i) = str2num(riga(7:11)); bus_ctrl(i) = str2num(riga(37:41)); branch_type(i) = str2num(riga(13)); branch_data(i,:) = str2num([riga(15:36),' ',riga(43:end)]); catch fm_disp(['Something wrong in the branch data (row ',int2srt(3+nbus+i),'). Conversion Process Interrupted'],2); fclose(fid); return endendriga = fgetl(fid);try, branch_end = str2num(riga); catch, branch_end = 0; endif branch_end ~= -999 | riga == -1 fm_disp('Branch Data abnormally terminated. Conversion Process Interrupted',2); fclose(fid); returnend% close file datafclose(fid);% load original file into cell stringif flag == 1, orig_file = textread([pathname,filename],'%s','delimiter','\n','whitespace',''); end% definition of file name for PSAT data fileextension = findstr(filename,'.');newfile = ['d_',filename(1:extension(end)-1),'.m'];% open *.m file for writing datafid = fopen([pathname,newfile], 'wt');if fid == -1, fm_disp(['Can''t open file ',pathname,newfile],2), return,end% Bus data: Bus.con% ----------------------------------------------------------------------count = fprintf(fid,['%% ',datestr(now,2),' File data originated from VST data file\n']);count = fprintf(fid,['%% \n\n']);count = fprintf(fid, 'Bus.con = [ ...\n');for i = 1:nbus-1 count = fprintf(fid,'%4d %8.4g %8.4g %8.4g; ', bus_num(i), 1, bus_data(i,[5 6])); if rem(i,3) == 0; count = fprintf(fid,'\n'); endendcount = fprintf(fid, '%4d %8.4g %8.4g %8.4g];\n\n\n', bus_num(end), 1, bus_data(end,[5 6]));% Swing Generator data: SW.con% ----------------------------------------------------------------------k = find(bus_type == 3);if ~isempty(k) n = length(k); swline = [bus_num(k), 100*ones(n,1), ones(n,1), bus_data(k,[5 6]), 99*ones(n,1), ... -99*ones(n,1), 1.2*ones(n,1), 0.8*ones(n,1), -bus_data(k,1), zeros(n,1)]; 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_type == 2);if ~isempty(k) n = length(k); pvline = [bus_num(k), 100*ones(n,1), ones(n,1),-bus_data(k,1), bus_data(k,5), 99*ones(n,1), ... -99*ones(n,1), 1.2*ones(n,1), 0.8*ones(n,1), zeros(n,1)]; 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_type == 1);if ~isempty(k) n = length(k); pqline = [bus_num(k), 100*ones(n,1), ones(n,1), bus_data(k,[1 2]), ... 1.2*ones(n,1), 0.8*ones(n,1), zeros(n,1)]; 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_data(:,3) ~= 0 | bus_data(:,4) ~= 0);if ~isempty(k) count = fprintf(fid, 'Shunt.con = [ ...\n'); n = length(k); shuntline = [bus_num(k), 100*ones(n,1), ones(n,1), 60*ones(n,1), bus_data(k,[3 4])]; 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% ----------------------------------------------------------------------Line_con = zeros(nbranch,13);Line_con(:,1) = bus_num(bus_from);Line_con(:,2) = bus_num(bus_to);Line_con(:,3) = 100*ones(nbranch,1);Line_con(:,4) = ones(nbranch,1);Line_con(:,5) = 60*ones(nbranch,1);Line_con(:,[8 9]) = branch_data(:,[1 2]);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:nbus-1 count = fprintf(fid, ['''', deblank(bus_name{i}),'''; ']); if rem(i,5) == 0; count = fprintf(fid,'\n '); endendcount = fprintf(fid, ['''', deblank(bus_name{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, endfclose(fid);check = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -