📄 sim2psat.m
字号:
function dispno(num,msg)%------------------------------------------------------------------if num, fm_disp([msg,': #',num2str(num),'#']), end%------------------------------------------------------------------function block_name = MaskType(block_handle)%------------------------------------------------------------------block_name = get_param(block_handle,'MaskType');if isempty(block_name) block_name = get_param(block_handle,'BlockType');endif isempty(block_name) hilite_system(block_handle) block_name = 'Error'; returnendif iscell(block_name) block_name = block_name{1};end%------------------------------------------------------------------function hdl2 = SeekDstBlock(hdl1,name2,pos)%------------------------------------------------------------------ports = get_param(hdl1,'PortConnectivity');if length(ports) < pos SimWarnMsg(hdl1,'has the wrong number of ports') hdl2 = hdl1; % to avoid errors in evaluating UserData returnendhandles = [ports.DstBlock];try idx = find(strcmp({ports.Type},'RConn1')); if isempty(idx), idx = pos; end if idx(pos) ~= pos hdl2 = ports(idx(pos)).DstBlock; else hdl2 = handles(pos); endcatch hdl2 = ports(pos).DstBlock;endhdl0 = hdl1;while 1 switch MaskType(hdl2) case name2 break case 'Outport' port_no = str2num(get_param(hdl2,'Port')); ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(port_no).DstBlock; case 'PMIOPort' port_no = str2num(get_param(hdl2,'Port')); ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(port_no).DstBlock; case 'SubSystem' ports = get_param(hdl2,'PortConnectivity'); port_no = num2str(find([ports(:).SrcBlock] == hdl1)); if isempty(port_no) port_no = num2str(find([ports(:).DstBlock] == hdl1)); end hdl0 = hdl2; hdl2 = find_system(hdl2,'SearchDepth',1,'Port',port_no); case 'Goto' tag = get_param(hdl2,'GotoTag'); name = find_system(gcs,'BlockType','From','GotoTag',tag); from = get_param(name{1},'Handle'); ports = get_param(from,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(1).DstBlock; case 'Link' ports = get_param(hdl2,'PortConnectivity'); if sum(strcmp(MaskType(hdl1),{'Pod','Cluster'})) if strcmp(ports(3).DstBlock,'Bus') hdl0 = hdl2; hdl2 = ports(2).DstBlock; % Input Port else hdl0 = hdl2; hdl2 = ports(3).DstBlock; % Output Port end elseif strcmp(MaskType(hdl0),MaskType(ports(2).DstBlock)) hdl0 = hdl2; hdl2 = ports(3).DstBlock; % Output Port else hdl0 = hdl2; hdl2 = ports(2).DstBlock; % Input Port end case 'Line' switch MaskType(hdl1) case {'Breaker','Upfc','Tcsc','Sssc'} hdl0 = hdl2; hdl2 = SeekSrcBlock(hdl1,'Bus',1); otherwise SimWarnMsg(hdl1,'cannot be connected to',hdl2) end break case {'Breaker','Sssc','Upfc','Tcsc','Mass'} ports = get_param(hdl2,'PortConnectivity'); hdl_temp = hdl0; hdl0 = hdl2; hdl2 = ports(2).DstBlock; % Output Port if hdl2 == hdl_temp hdl2 = ports(1).DstBlock; % Output Port end case 'Link2' ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(3).DstBlock; % Output Port case 'Error' SimWarnMsg(hdl1,'is badly connected') hdl0 = hdl2; hdl2 = hdl1; % to avoid errors in evaluating UserData break otherwise SimWarnMsg(hdl1,'cannot be connected to',hdl2) break endend%------------------------------------------------------------------function hdl2 = SeekSrcBlock(hdl1,name2,pos)%------------------------------------------------------------------ports = get_param(hdl1,'PortConnectivity');if length(ports) < pos SimWarnMsg(hdl1,'has the wrong number of ports') hdl2 = hdl1; % to avoid errors in evaluating UserData returnendswitch ports(pos).Type case {'1','enable'} hdl2 = ports(pos).SrcBlock; otherwise hdl2 = ports(pos).DstBlock;endhdl0 = hdl1;while 1 switch MaskType(hdl2) case name2 break case 'Inport' port_no = str2num(get_param(hdl2,'Port')); ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(port_no).SrcBlock; case 'PMIOPort' port_no = str2num(get_param(hdl2,'Port')); ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(port_no).DstBlock; case 'SubSystem' ports = get_param(hdl2,'PortConnectivity'); port_no = num2str(find([ports(:).DstBlock] == hdl1)); hdl0 = hdl2; hdl2 = find_system(hdl2,'SearchDepth',1,'Port',port_no); case 'From' tag = get_param(hdl2,'GotoTag'); name = find_system(gcs,'BlockType','Goto','GotoTag',tag) goto = get_param(name{1},'Handle'); ports = get_param(goto,'PortConnectivity'); hdl0 = hdl2; hdl2 = ports(1).SrcBlock; case 'Link' ports = get_param(hdl2,'PortConnectivity'); if strcmp(MaskType(hdl0),MaskType(ports(2).DstBlock)) hdl0 = hdl2; hdl2 = ports(3).DstBlock; % Output Port else hdl0 = hdl2; hdl2 = ports(2).DstBlock; % Input Port end case 'Bus' switch MaskType(hdl1) case {'Breaker','Sssc','Upfc','Tcsc'} hdl0 = hdl2; hdl2 = SeekDstBlock(hdl1,'Line',2); otherwise SimWarnMsg(hdl1,'cannot be connected to',hdl2) end break case {'Breaker','Sssc','Upfc','Tcsc'} ports = get_param(hdl2,'PortConnectivity'); hdl_temp = hdl0; hdl0 = hdl2; hdl2 = ports(1).DstBlock; % Output Port if hdl2 == hdl_temp hdl2 = ports(2).DstBlock; % Output Port end case 'Link2' ports = get_param(hdl2,'PortConnectivity'); hdl0 = hdl2; if strcmp(MaskType(hdl1),'Pod') if strcmp(MaskType(ports(2).DstBlock),name2) hdl2 = ports(2).DstBlock; % Input Port break elseif strcmp(MaskType(ports(3).DstBlock),name2) hdl2 = ports(3).DstBlock; % Output Port break else % try to follow one path (50% likely to succeed) hdl2 = ports(3).DstBlock; % Output Port end else hdl2 = ports(2).DstBlock; % Input Port end case 'Error' SimWarnMsg(hdl1,'is badly connected') hdl0 = hdl2; hdl2 = hdl1; % to avoid errors in evaluating UserData break otherwise SimWarnMsg(hdl1,'cannot be connected to',hdl2) break endend%------------------------------------------------------------------function SimWarnMsg(varargin)%------------------------------------------------------------------handle1 = varargin{1};msg = varargin{2};hilite_system(handle1,'default')name1 = get_param(handle1,'Name');if nargin == 2 fm_disp(['Error: Block <',name1,'> ',msg,'.'])elseif nargin == 3 handle2 = varargin{3}; name2 = get_param(handle2,'Name'); fm_disp(['Error: Block <',name1,'> ',msg,' block <',name2,'>.'])end%------------------------------------------------------------------function WriteNames(fid,type,names)%------------------------------------------------------------------if isempty(names), return, endn = length(names);count = fprintf(fid, [type,'.names = {... \n ']);for i = 1:n-1 name = strrep(names{i,1},char(10),' '); count = fprintf(fid, ['''',name,'''; ']); if rem(i,5) == 0; count = fprintf(fid,'\n '); endendif iscell(names) name = strrep(names{n,1},char(10),' '); count = fprintf(fid, ['''',name,'''};\n\n']);else name = strrep(names,char(10),' '); count = fprintf(fid, ['''',name,'''};\n\n']);end%------------------------------------------------------------------function SimUpdate(sys)%------------------------------------------------------------------global Settingssys = getfullname(sys);hilite_system(sys,'none')block = find_system(sys,'Type','block');mask = get_param(block,'MaskType');nblock = length(block);% check if all blocks belong to the PSAT LibraryTags = get_param(block,'Tag');BlockTypes = get_param(block,'BlockType');idx = ones(nblock,1);idx(strmatch('PSATblock',Tags,'exact')) = 0;idx(strmatch('SubSystem',BlockTypes,'exact')) = 0;idx(strmatch('PMIOPort',BlockTypes,'exact')) = 0;if sum(idx) idx = find(idx); fm_disp(fm_strjoin('* * Warning: Block <',get_param(block(idx),'Name'), ... '> does not belong to the PSAT Simulink Library.')) Settings.ok = 0; uiwait(fm_choice(['Some blocks do not seem to belong to the ', ... 'PSAT library, but could be old blocks. ', ... 'Do you want to fix them?'])) if Settings.ok for iii = 1:length(idx) blocktype = mask{idx(iii)}; if isempty(blocktype) blocktype = get_param(block{idx(iii)},'BlockType'); end switch blocktype case {'Bus','Link','Goto','From'} prop = 'Connection'; case {'Supply','Demand','Rmpg','Rrsv','Vltn','Rmpl','Ypdp'} prop = 'OPF & CPF'; case {'Breaker','Fault'} prop = 'Faults & Breakers'; case 'Busfreq' prop = 'Measurements'; case {'Mn','Pl','Thload','Fl','Exload'} prop = 'Loads'; case {'Syn','Mot'} prop = 'Machines'; case {'Ltc','Tap'} prop = 'ULTC'; case 'Phs' prop = 'Phase Shifter'; case {'Tg','Exc','Cac','Cluster','Pss','Oxl'} prop = 'Controls'; case {'Statcom','Upfc','Svc','Hvdc','Tcsc','Sssc'} prop = 'FACTS'; case {'Dfig','Cswt','Ddsg'} prop = 'Wind Turbines'; case {'Sofc','SSR','RLC','Mass'} prop = 'Sparse Dynamic Component'; %case {'SAE1','SAE2','SAE3'} % prop = 'SAE'; otherwise prop = 'Power Flow'; end set_param(block{idx(iii)}, ... 'Tag','PSATblock', ... 'Description',prop) end save_system(sys); endelse fm_disp(' ') fm_disp('* * All blocks belong to the PSAT-Simulink Library.')end% check for old modelsslackbus = find_system(sys,'MaskType','SW');ports = get_param(slackbus,'Ports');if isempty(ports) fm_disp('* * Error: No Slack bus found!') pvbus = find_system(sys,'MaskType','PV'); ports = get_param(pvbus,'Ports');endif iscell(ports) check = sum(ports{1});elseif isnumeric(ports) check = sum(ports);end% check if model needs to be updatedif ~check fm_disp(' ') fm_disp('* * Warning: The model is referring to an old PSAT-Simulink') fm_disp(' library. PSAT will try to update models.') fm_disp(' ') Settings.ok = 0; uiwait(fm_choice(['The model refer to an old PSAT-Simulink ' ... 'library. Update?'],1)) if ~Settings.ok, return, endelse returnendload_system('fm_lib');open_system(sys);for i = 1:nblock % fix source block if it has changed try source = get_param(block{i},'SourceBlock'); switch source case 'fm_lib/Power Flow/Transf5' set_param(block{i},'SourceBlock','fm_lib/Power Flow/Twt') case ['fm_lib/Wind',char(10),'Turbines/Cswt1'] set_param(block{i},'SourceBlock',['fm_lib/Wind',char(10),'Turbines/Cswt']) case ['fm_lib/Wind',char(10),'Turbines/Dfig1'] set_param(block{i},'SourceBlock',['fm_lib/Wind',char(10),'Turbines/Ddsg']) case ['fm_lib/Wind',char(10),'Turbines/Dfig2'] set_param(block{i},'SourceBlock',['fm_lib/Wind',char(10),'Turbines/Dfig']) case ['fm_lib/Wind',char(10),'Turbines/Wind1'] set_param(block{i},'SourceBlock',['fm_lib/Wind',char(10),'Turbines/Wind']) case 'fm_lib/Power Flow/Extra Line' set_param(block{i},'SourceBlock','fm_lib/Power Flow/Lines') case 'fm_lib/Power Flow/PQ1' set_param(block{i},'SourceBlock','fm_lib/Power Flow/PQgen') case 'fm_lib/Machines/Gen' set_param(block{i},'SourceBlock','fm_lib/Machines/Syn') case 'fm_lib/ULTC/LTC' set_param(block{i},'SourceBlock','fm_lib/ULTC/Ltc') case 'fm_lib/ULTC/OLTC' set_param(block{i},'SourceBlock','fm_lib/ULTC/Tap') case 'fm_lib/ULTC/PHS' set_param(block{i},'SourceBlock','fm_lib/ULTC/Phs') case 'fm_lib/Others/SOFC' set_param(block{i},'SourceBlock','fm_lib/Others/Sofc') case 'fm_lib/Others/SSR' set_param(block{i},'SourceBlock','fm_lib/Others/Ssr') case 'fm_lib/Measurements/PMU' set_param(block{i},'SourceBlock','fm_lib/Measurements/Pmu') case 'fm_lib/Loads/FDL' set_param(block{i},'SourceBlock','fm_lib/Loads/Fl') case 'fm_lib/Loads/LRL' set_param(block{i},'SourceBlock','fm_lib/Loads/Exload') case 'fm_lib/Loads/TCL' set_param(block{i},'SourceBlock','fm_lib/Loads/Thload') case 'fm_lib/Loads/Mixed' set_param(block{i},'SourceBlock','fm_lib/Loads/Mixload') case 'fm_lib/Loads/VDL' set_param(block{i},'SourceBlock','fm_lib/Loads/Mn') case 'fm_lib/Loads/ZIP' set_param(block{i},'SourceBlock','fm_lib/Loads/Pl') case 'fm_lib/FACTS/HVDC' set_param(block{i},'SourceBlock','fm_lib/FACTS/Hvdc') case 'fm_lib/FACTS/SSSC' set_param(block{i},'SourceBlock','fm_lib/FACTS/Sssc') case 'fm_lib/FACTS/SVC (1)' set_param(block{i},'SourceBlock','fm_lib/FACTS/Svc') case 'fm_lib/FACTS/SVC (2)' set_param(block{i},'SourceBlock','fm_lib/FACTS/Svc2') case 'fm_lib/FACTS/StatCom' set_param(block{i},'SourceBlock','fm_lib/FACTS/Statcom') case 'fm_lib/FACTS/TCSC (1)' set_param(block{i},'SourceBlock','fm_lib/FACTS/Tcsc') case 'fm_lib/FACTS/TCSC (2)' set_param(block{i},'SourceBlock','fm_lib/FACTS/Tcsc2') case 'fm_lib/FACTS/UPFC' set_param(block{i},'SourceBlock','fm_lib/FACTS/Upfc') case 'fm_lib/Connections/Link' set_param(block{i},'SourceBlock','fm_lib/Connections/Link1') case 'fm_lib/OPF & CPF/RMPG' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Rmpg') case 'fm_lib/OPF & CPF/RMPL' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Rmpl') case 'fm_lib/OPF & CPF/RSRV' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Rsrv') case 'fm_lib/OPF & CPF/VLTN' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Vltn') case 'fm_lib/OPF & CPF/YPDP' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Ypdp') case 'fm_lib/OPF & CPF/YPDP1' set_param(block{i},'SourceBlock','fm_lib/OPF & CPF/Ypdp1') case 'fm_lib/Controls/AVR' set_param(block{i},'SourceBlock','fm_lib/Controls/Exc') case 'fm_lib/Controls/TG' set_param(block{i},'SourceBlock','fm_lib/Controls/Tg') case 'fm_lib/Controls/SSCL' set_param(block{i},'SourceBlock','fm_lib/Controls/Pod') case 'fm_lib/Controls/OXL' set_param(block{i},'SourceBlock','fm_lib/Controls/Oxl') case 'fm_lib/Controls/PSS' set_param(block{i},'SourceBlock','fm_lib/Controls/Pss') case 'fm_lib/Controls/CAC' set_param(block{i},'SourceBlock','fm_lib/Controls/Cac') case 'fm_lib/Controls/Shaft' set_param(block{i},'SourceBlock','fm_lib/Others/Mass') end mask{i} = get_param(block{i},'MaskType'); catch % the source block has not changed end switch mask{i} case {'Bus','Link','Line','Lines','Breaker','Twt' ... 'Phs','Tcsc','Sssc','Upfc','Hvdc','Dfig', ... 'Cswt','Ddsg','RLC','PV','SW','PQgen', ... 'Rmpg','Rsrv','Vltn','Wind','Sofc','Ssr', ... 'PQ','Shunt','Rmpl','Fault','Mn','Pl','Mot', ... 'Fl','Exload','Mixload','Thload','Jimma','Tap', ... 'Svc','Statcom','Busfreq','Pmu','Supply', ... 'Demand','Syn','Ltc','SAE1','SAE2','SAE3', ... 'Exc','Tg','Sscl','Cac','Oxl','Pss','Cluster'} cloneblock(block{i},sys) endendlines = find_system(sys,'FindAll','on','type','line');for i = 1:length(lines) points = get_param(lines(i),'Points'); parent = get_param(lines(i),'Parent'); delete_line(parent,points(1,:)); try add_line(parent,points); catch fm_disp(['* * Connection line ',num2str(i),' could not be replaced.']) endenduiwait(fm_choice('Now please take a moment to doublecheck connections...',2))fm_disp(' ')fm_disp(['* * Update of model <',sys,'> completed.'])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -