📄 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
%Version: 2.0.0
%
%E-mail: fmilano@thunderbox.uwaterloo.ca
%Web-site: http://thunderbox.uwaterloo.ca/~fmilano
%
% Copyright (C) 2002-2006 Federico Milano
global File Fig Settings Hdl Path Varname Theme History
if ~nargin
File_Data = File.data;
Path_Data = Path.data;
else
File_Data = varargin{1};
Path_Data = varargin{2};
end
if ~strcmp(Path_Data(end),filesep)
Path_Data = [Path_Data,filesep];
end
check_model = 1;
fm_disp
fm_disp('Simulink Model Conversion');
fm_disp(['Simulink File <',File_Data,'>.']);
lasterr('');
for i = 1:length(Varname.fcomp),
eval([Varname.fcomp{i}, ' = [];']);
end
tipi = length(Varname.fcomp)-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;
% 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 bar
fm_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')
end
masks = 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', ...
'Subtransmission Equivalent Area'));
vector(12) = length(find_system(blocks,'Description','Wind Turbines'));
vector(13) = 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),'Subtransmission Equivalent Areas')
dispno(vector(12),'Wind Power Components')
dispno(vector(13),'Measurement Components')
% component data matrices
% ----------------------------------------------------------------
fm_disp(' * * *')
fm_disp('Definition of component data ...')
kinds = zeros(length(Varname.fcomp),1);
idx_old = 0;
for i = 1:nblock
tipo = masks{i};
idx = strmatch(tipo,Varname.fcomp,'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
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]), end
end
% "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'}
Source = '';
src = []; possrc = [];
Destin = '';
dst = []; posdst = [];
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,';']);
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])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -