📄 sim2psat.m
字号:
function check_model = sim2psat(varargin)% SIM2PSAT convert Simulink models into PSAT data files%% CHECK = SIM2PSAT% CHECK = 0 conversion failed% CHECK = 1 conversion completed%%see also FM_LIB, FM_SIMREP, FM_SIMSET%%Author: Federico Milano%Date: 01-Jan-2006%%E-mail: Federico.Milano@uclm.es%Web-site: http://www.uclm.es/area/gsee/Web/Federico%% Copyright (C) 2002-2008 Federico Milanoglobal File Fig Settings Hdl Path Theme Historyif ~nargin File_Data = File.data; Path_Data = Path.data;else File_Data = varargin{1}; Path_Data = varargin{2};endif ~strcmp(Path_Data(end),filesep) Path_Data = [Path_Data,filesep];endcheck_model = 1;fm_dispfm_disp('Simulink Model Conversion');fm_disp(['Simulink File <',File_Data,'>.']);% component names% NB. 'Varname' must be the last elementCompnames = {'Bus','Line','Shunt','Breaker', ... 'Fault','SW','PV','PQ','PQgen', ... 'Mn','Pl','Fl','Lines','Twt','Syn', ... 'Mot','Ltc','Thload','Tg','Exc', ... 'Pss','Oxl','Hvdc','Svc','Tcsc', ... 'Statcom','Sssc','Upfc','Mass','SSR', ... 'Tap','Demand','Supply','Rsrv','Rmpg', ... 'Rmpl','Vltn','Ypdp','Sofc','Cac', ... 'Cluster','Exload','Phs','Cswt','Dfig', ... 'Ddsg','Wind','Busfreq','Pmu','Jimma', ... 'Mixload','Pod','Areas','Regions','Varname'};lasterr('');for i = 1:length(Compnames), eval([Compnames{i}, ' = [];']);endtipi = length(Compnames)-1;% constants used in the component masks% ----------------------------------------------------------------on = 1;off = 0;omega = 1;power = 2;voltage = 3;monday = 1;tuesday = 2;wednesday = 3;thursday = 4;friday = 5;saturday = 6;sunday = 7;winter_week_day = 1;winter_week_end = 2;summer_week_day = 3;summer_week_end = 4;spring_fall_week_day = 5;spring_fall_week_end = 6;measurements = 1;weibull = 2;composite = 3;Bus_V = 1;Line_P_from_bus = 2;Line_P_to_bus = 3;Line_I_from_bus = 4;Line_I_to_bus = 5;Line_Q_from_bus = 6;Line_Q_to_bus = 7;in = 1;out = 1;ins = 1;ous = 1;constant_voltage = 1;constant_reactance = 2;constant_power = 3;constant_line_power = 1;constant_angle = 2;SVC_control = 1;TCSC_control = 2;STATCOM_control = 3;SSSC_control = 4;UPFC_control = 5;Xc = 1;Alpha = 2;constant_admittance = 1;constant_power_flow = 2;Current_control = 1;Power_control = 2;Voltage_control = 3;% loading Simulink model% ----------------------------------------------------------------File_Data = strrep(File_Data,'(mdl)','');File_Data = strrep(File_Data,'.mdl','');fm_disp('Loading Simulink Model')%cd(Path_Data);open_sys = find_system('type','block_diagram');OpenModel = sum(strcmp(open_sys,File_Data));if OpenModel cur_sys = get_param(File_Data,'Handle');else localpath = pwd; cd(Path_Data) if exist(File_Data,'file') ~= 4 fm_disp(['File <',File_Data,'> is not a Simulink model.'],2) check_model = 0; return end cur_sys = load_system(File_Data); cd(localpath)end% open status barfm_bar open% load block and mask properties% ----------------------------------------------------------------fm_disp(' * * *')fm_disp('Check model version and blocks ...')SimUpdate(cur_sys)Settings.mv = str2num(get_param(cur_sys,'ModelVersion'));blocks = find_system(cur_sys,'Type','block');if strcmp(get_param(cur_sys,'Open'),'on') hilite_system(cur_sys,'none')endmasks = get_param(blocks,'MaskType');nblock = length(blocks);tipi3 = 1/(tipi + 1 + 2*nblock);fm_bar([1e-3,tipi3])fm_disp(' * * *')fm_disp('Statistics ...')vector = zeros(13,1);vector(1) = length(find_system(blocks,'Description','Connection'));vector(2) = length(find_system(blocks,'Description','Power Flow'));vector(3) = length(find_system(blocks,'Description','OPF & CPF'));vector(4) = length(find_system(blocks,'Description','Faults & Breakers'));vector(5) = length(find_system(blocks,'Description','Loads'));vector(6) = length(find_system(blocks,'Description','Machines'));vector(7) = length(find_system(blocks,'Description','ULTC'));vector(8) = length(find_system(blocks,'Description','Controls'));vector(9) = length(find_system(blocks,'Description','FACTS'));vector(10) = length(find_system(blocks,'Description','Sparse Dynamic Component'));vector(11) = length(find_system(blocks,'Description','Wind Turbines'));vector(12) = length(find_system(blocks,'Description','Measurements'));dispno(vector(1),'Connections')dispno(vector(2),'Power Flow Components')dispno(vector(3),'OPF & CPF Components')dispno(vector(4),'Faults & Breakers')dispno(vector(5),'Special Loads')dispno(vector(6),'Machines')dispno(vector(7),'Regulating Transformers')dispno(vector(8),'Controls')dispno(vector(9),'FACTS')dispno(vector(10),'Spare Dynamic Components')dispno(vector(11),'Wind Power Components')dispno(vector(12),'Measurement Components')% component data matrices% ----------------------------------------------------------------fm_disp(' * * *')fm_disp('Definition of component data ...')kinds = zeros(length(Compnames),1);idx_old = 0;for i = 1:nblock tipo = masks{i}; idx = strmatch(tipo,Compnames,'exact'); if ~isempty(idx) kinds(idx) = kinds(idx)+1; sidx = num2str(kinds(idx)); if idx ~= idx_old idx_old = idx; fm_disp(['Data "',tipo,'.con"']) end comp_data = get_param(blocks(i),'MaskVariables'); comp_value = get_param(blocks(i),'MaskValueString'); valori = strrep(['[',comp_value,']'],'|',','); indici = comp_data; if strmatch(indici,'pxq=@1;','exact') indici = ':'; else indici = ['[',indici,']']; indici = strrep(indici,'x',':'); indici = strrep(indici,'p',''); indici = strrep(indici,'_',' '); indici = strrep(indici,'q',''); end indici = regexprep(indici,'=@([0-9]*);',' '); try eval([tipo,'(',sidx,',',indici,') = ',valori,';']); catch %[tipo,'(',sidx,',',indici,') = ',valori,';'] fm_disp(['Error: ',tipo,' block <', ... get_param(blocks(i),'Name'), ... '> has a wrong number of data.'],2) hilite_system(blocks(i),'default') eval([tipo,'(',sidx,',',indici,') = 0;']); end set_param(blocks(i),'UserData',sidx); end if ~rem(i,5), fm_bar([(i-1)*tipi3,i*tipi3]), endend% "Bus" number% ----------------------------------------------------------------busidx = find(strcmp(masks,'Bus'));busname = get_param(blocks(busidx),'Name');Bus_n = length(busidx);Bus(:,1) = [1:Bus_n]';fm_disp(' * * *')fm_disp('Definition of system connections ...')for i = 1:nblock if isempty(masks{i}), continue, end if strcmp(get_param(blocks(i),'Description'),'Connection') continue end rowno = get_param(blocks(i),'UserData'); % define connections switch masks{i} case {'Exc','Tg','Mass'} Destin = {'Syn'}; dst = 1; posdst = 1; Source = ''; src = []; possrc = []; case {'Pss','Oxl'} Destin = {'Exc'}; dst = 1; posdst = 1; Source = ''; src = []; possrc = []; case 'Rmpg' Destin = {'Supply'}; dst = 1; posdst = 1; Source = ''; src = []; possrc = []; case 'Rmpl' Destin = ''; dst = []; posdst = []; Source = {'Demand'}; src = 1; possrc = 1; case 'Breaker' Destin = {'Bus'}; dst = 2; posdst = 2; Source = {'Line'}; src = 1; possrc = 1; case 'Pod' Destin = {'Statcom','Sssc','Svc','Upfc','Tcsc'}; dst = 2; posdst = 2; MaskValues = get_param(blocks(i),'MaskValues'); if strcmp(MaskValues{1},'Bus_V') Source = {'Bus'}; else Source = {'Line'}; end src = 1; possrc = 1; case 'Cluster' Source = {'Cac'}; src = 1; possrc = 1; Destin = {'Exc','Svc'}; dst = 2; posdst = 2; case {'PV','SW','Supply','Rsrv','Rmpg','Vltn', ... 'SSR','Sofc','PQgen','Syn','Supply'} Destin = {'Bus'}; dst = 1; posdst = 1; Source = ''; src = []; possrc = []; case {'Line','Lines','Phs','RLC','Hvdc'} Destin = {'Bus'}; dst = 2; posdst = 2; Source = {'Bus'}; src = 1; possrc = 1; case {'Sssc','Upfc','Tcsc'} Destin = ''; dst = []; posdst = []; Source = {'Line'}; src = 1; possrc = 1; case 'Ltc' MaskValues = get_param(blocks(i),'MaskValues'); if strcmp(MaskValues{3},'3') Destin = {'Bus'}; dst = 3; posdst = 2; Source = {'Bus'}; src = [1 2]; possrc = [15 1]; else Destin = {'Bus'}; dst = 2; posdst = 2; Source = {'Bus'}; src = 1; possrc = 1; end case {'Cswt','Dfig','Ddsg'} Destin = {'Bus'}; dst = 2; posdst = 1; Source = {'Wind'}; src = 1; possrc = 2; case 'Twt' Destin = {'Bus'}; dst = [2 3]; posdst = [2 3]; Source = {'Bus'}; src = 1; possrc = 1; %case {'SAE1','SAE2','SAE3'} % Source = {'Bus'}; % src = [1 2]; possrc = [1 2]; % Destin = ''; % dst = []; posdst = []; case {'Ypdp','Wind','Varname'} Source = ''; src = []; possrc = []; Destin = ''; dst = []; posdst = []; case {'Areas','Regions'} Source = ''; src = []; possrc = []; MaskValues = get_param(blocks(i),'MaskValues'); if strcmp(MaskValues{1},'1') Destin = {'Bus'}; dst = 1; posdst = 2; else Destin = ''; dst = []; posdst = []; end otherwise Destin = ''; dst = []; posdst = []; Source = {'Bus'}; src = 1; possrc = 1; end % find connections for j = 1:length(dst) block2_handle = SeekDstBlock(blocks(i),Destin,dst(j)); busno = get_param(block2_handle,'UserData'); eval([masks{i},'(',rowno,',',num2str(posdst(j)),') = ',busno,';']); if strcmp(masks(i),'Cluster') switch get_param(block2_handle,'MaskType') case 'Exc', ctype = '1'; case 'Svc', ctype = '2'; end eval([masks{i},'(',rowno,',3) = ',ctype,';']); end if strcmp(masks(i),'Pod') switch get_param(block2_handle,'MaskType') case 'Svc', ctype = '1'; case 'Tcsc', ctype = '2'; case 'Statcom', ctype = '3'; case 'Sssc', ctype = '4'; case 'Upfc', ctype = '5'; end eval([masks{i},'(',rowno,',4) = ',ctype,';']); end end for j = 1:length(src) block2_handle = SeekSrcBlock(blocks(i),Source,src(j)); busno = get_param(block2_handle,'UserData'); eval([masks{i},'(',rowno,',',num2str(possrc(j)),') = ',busno,';']); end fm_bar([(nblock+i-1)*tipi3,(nblock+i)*tipi3])endfm_disp(' * * *')% writing data fileidx1 = strmatch('Definition of component data ...',History.text);idx2 = strmatch('Definition of system connections ...',History.text);idx3 = strmatch('Error:',History.text);if isempty(idx3), idx3 = 0; endif idx3(end) > idx1(end) if idx3(end) > idx2(end), message = 'Simulink model is not well-formed (check links).'; end if find(idx3 < idx2(end) & idx3 > idx1(end)), message = ['Component data are not well-formed (check ' ... 'masks).']; endelse File_Data = [File_Data,'_mdl']; [fid, message] = fopen([Path_Data,File_Data,'.m'], 'wt');endif ~isempty(message), if strcmp(message, ... ['Sorry. No help in figuring out the problem ...']), fm_disp(['Most likely the folder "',Path_Data, ... '" is read only. Try to change the permission.']) else fm_disp(['Failed conversion from Simulink model: ',message],2) end if Fig.main set(Fig.main,'Pointer','arrow'); delete(Hdl.bar); Hdl.bar = 0; set(Hdl.frame,'Visible','on'); set(Hdl.text,'Visible','on'); end check_model = 0; returnelse fm_disp('Writing Data File',1)endfm_bar([(2*nblock)*tipi3,(2*nblock+1)*tipi3])for j = 1:length(Compnames)-1 values = eval(Compnames{j}); if ~isempty(values) count = fprintf(fid,'%s.con = [ ... \n',Compnames{j}); for i = 1:length(values(:,1)) count = fprintf(fid,[' ',regexprep(num2str(values(i,:)),'\s*',' '),';\n']); end count = fprintf(fid,' ];\n\n'); end fm_bar([(2*nblock+j-1)*tipi3,(2*nblock+j)*tipi3])end% count = fprintf(fid, 'Bus.names = {... \n ');% for i = 1:Bus_n-1% namebus = strrep(busname{i,1},char(10),' ');% count = fprintf(fid, ['''',namebus,'''; ']);% if rem(i,5) == 0; count = fprintf(fid,'\n '); end% end% if iscell(busname)% namebus = strrep(busname{length(busname),1},char(10),' ');% count = fprintf(fid, ['''',namebus,'''};\n\n']);% else% namebus = strrep(busname,char(10),' ');% count = fprintf(fid, ['''',namebus,'''};\n\n']);% endWriteNames(fid,'Bus',busname);areaidx = find(strcmp(masks,'Areas'));areaname = get_param(blocks(areaidx),'Name');WriteNames(fid,'Areas',areaname);zoneidx = find(strcmp(masks,'Regions'));zonename = get_param(blocks(zoneidx),'Name');WriteNames(fid,'Regions',zonename);% print indexes of variables to be plottedif ~isempty(Varname) count = fprintf(fid, 'Varname.idx = [... \n'); nidx = length(Varname); count = fprintf(fid,'%5d; %5d; %5d; %5d; %5d; %5d; %5d;\n',Varname); if rem(nidx,7) ~= 0, count = fprintf(fid,'\n'); end count = fprintf(fid,' ];\n');end% closing data filecount = fclose(fid);exist(File_Data);% closing Simulink modelif ~OpenModel & ~strcmp(get_param(cur_sys,'Dirty'),'on') close_system(cur_sys);endfm_disp(['Construction of Data File <',File_Data,'.m> completed.'])% close status barfm_bar close% last operations% cd(Path.local);if Settings.beep, beep, endif ~nargin, File.data = [File_Data(1:end-4),'(mdl)']; end%------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -