📄 sim2psat.m
字号:
end
fm_disp(' * * *')
% writing data file
idx1 = 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; end
if 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).'];
end
else
File_Data = [File_Data,'_mdl'];
[fid, message] = fopen([Path_Data,File_Data,'.m'], 'wt');
end
if ~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;
return
else
fm_disp('Writing Data File',1)
end
fm_bar([(2*nblock)*tipi3,(2*nblock+1)*tipi3])
for j = 1:length(Varname.fcomp)
values = eval(Varname.fcomp{j});
if ~isempty(values)
form = eval(Varname.format{j});
count = fprintf(fid,'%s.con = [ ... \n',Varname.fcomp{j});
count = fprintf(fid,[form,';\n'],values');
count = fprintf(fid,' ];\n\n');
end
fm_bar([(2*nblock+j-1)*tipi3,(2*nblock+j)*tipi3])
end
count = fprintf(fid, 'Varname.bus = {... \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']);
else
namebus = strrep(busname,char(10),' ');
count = fprintf(fid, ['''',namebus,'''};\n']);
end
% closing data file
count = fclose(fid);
exist(File_Data);
% closing Simulink model
if ~OpenModel & ~strcmp(get_param(cur_sys,'Dirty'),'on')
close_system(cur_sys);
end
fm_disp(['Construction of Data File <',File_Data,'.m> completed.'])
% close status bar
fm_bar close
% last operations
% cd(Path.local);
if Settings.beep, beep, end
if ~nargin, File.data = [File_Data(1:end-4),'(mdl)']; end
%------------------------------------------------------------------
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');
end
if isempty(block_name)
hilite_system(block_handle)
block_name = 'Error';
return
end
if 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
return
end
handles = [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);
end
catch
hdl2 = ports(pos).DstBlock;
end
hdl0 = 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'
hdl0 = hdl2;
hdl2 = SeekSrcBlock(hdl1,'Bus',1);
otherwise
SimWarnMsg(hdl1,'cannot be connected to',hdl2)
end
break
case {'Breaker','Sssc','Ufpc','Tcsc'}
ports = get_param(hdl2,'PortConnectivity');
hdl0 = hdl2;
hdl2 = ports(2).DstBlock; % Output Port
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
end
end
%------------------------------------------------------------------
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
return
end
switch ports(pos).Type
case {'1','enable'}
hdl2 = ports(pos).SrcBlock;
otherwise
hdl2 = ports(pos).DstBlock;
end
hdl0 = 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');
hdl0 = hdl2;
hdl2 = ports(1).DstBlock; % Output Port
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
end
end
%------------------------------------------------------------------
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 SimUpdate(sys)
%------------------------------------------------------------------
global Settings
sys = 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 Library
Tags = 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -