⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sim2psat.m

📁 基于PSAT 软件的多目标最优潮流计算用于中小型电力系统的分析和管理
💻 M
📖 第 1 页 / 共 2 页
字号:
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 Milanoglobal File Fig Settings Hdl Path Varname 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,'>.']);lasterr('');for i = 1:length(Varname.fcomp),  eval([Varname.fcomp{i}, ' = [];']);endtipi = 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 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', ...                                '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]), 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'}    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])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(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])endcount = 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'); endendif 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 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%------------------------------------------------------------------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;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'

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -