sim2psat.m

来自「基于PSAT 软件的多目标最优潮流计算用于中小型电力系统的分析和管理」· M 代码 · 共 1,022 行 · 第 1/2 页

M
1,022
字号
    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  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');    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  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 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(strcat('* * 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 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/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','Mixed','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.'])%------------------------------------------------------------------function CloneBlock(block,sys)%------------------------------------------------------------------name = get_param(block,'Name');clone = find_system(sys,'Name',name);type = get_param(block,'MaskType');pos = get_param(block,'Position');orient = get_param(block,'Orientation');bgcolor = get_param(block,'BackgroundColor');fontname = get_param(block,'FontName');fontsize = get_param(block,'FontSize');maskvalue = get_param(block,'MaskValueString');nameplace = get_param(block,'NamePlacement');switch type case 'Bus'  template = 'Bus1'; case 'Link'  template = 'Link1'; case 'SW'  template = 'Slack'; case 'Shunt'  a = get_param(block,'MaskPrompts');  if strmatch('Conductance',a{2})    template = 'Shunt';  elseif strmatch('Susceptance',a{2})    template = 'Shunt1';  end case 'PV'  if length(get_param(block,'MaskVarAliases')) == 6    template = 'PV';  else    template = 'SC';  end case 'Line'  if length(get_param(block,'MaskVarAliases')) == 6    a = get_param(block,'MaskDescription');    if ~isempty(strfind(a,'shifter'))      template = 'Transf3';    elseif ~isempty(strfind(a,'cable'))      template = 'Line1';    elseif ~isempty(strfind(a,'tap ratio'))      template = 'Transf2';    else      template = 'Line';    end  elseif length(get_param(block,'MaskVarAliases')) == 7    template = 'Transf4';  elseif length(get_param(block,'MaskVarAliases')) == 5    a = get_param(block,'MaskDescription');    if ~isempty(strfind(a,'auto'))      template = 'Auto';    else      template = 'Transf1';    end  end otherwise  template = type;endupdatesyn = 0;updatesup = 0;if strcmp(type,'Syn')  values = get_param(block,'MaskValues');  if length(values) == 12        in = get_param(block,'in');    updatesyn = 1;  endelseif strcmp(type,'Supply')  values = get_param(block,'MaskValues');  if length(values) == 8        in = get_param(block,'in');    updatesup = 1;  end  elseif strcmp(type,'Demand')  values = get_param(block,'MaskValues');  if length(values) == 9        out = get_param(block,'out');    updatesup = 1;  end  endancestor = find_system('fm_lib','LookUnderMasks','functional','Name',template);if isempty(clone)  fm_disp('* * Troubles in defining destination block')elseif isempty(ancestor)  fm_disp('* * Troubles in finding library source (broken library link)')else  if iscell(clone), clone = clone{1}; end  if iscell(ancestor), ancestor = ancestor{1}; end  try    delete_block(block)        newblock = add_block(ancestor,clone);        set_param(newblock, ...              'Name',name, ...              'Position',pos, ...              'Orientation',orient, ...              'BackgroundColor',bgcolor, ...              'FontName',fontname, ...              'FontSize',fontsize, ...              'NamePlacement',nameplace, ...              'MaskValueString',maskvalue)        if strcmp(type,'Supply')      set_param(newblock,'p15q','1')      set_param(newblock,'p16_17q','[0 0]')      set_param(newblock,'p18_19q','[0 0]')      set_param(newblock,'in',in)              end    if strcmp(type,'Demand')      set_param(newblock,'p16_17q','[0 0]')      set_param(newblock,'out',out)              end    if updatesyn      set_param(newblock,'p25_26q','[0 0]')      set_param(newblock,'in',in)              end      catch    lasterr    fm_disp(['* * Troubles in replacing block <', ...             clone,'> with <',ancestor,'>'])      endend

⌨️ 快捷键说明

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