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

📄 fm_simrep.m

📁 这是一个很适合研究和学习用的电力系统仿真软件
💻 M
字号:
function fm_simrep(flag,fontsize,fontname)
% FM_SIMREP generate data report in Simulink models
%
% FM_SIMREP(FLAG,FONTSIZE,FONTNAME)
%       FLAG: 1 - set up voltage report on current loaded network
%             2 - wipe voltage report on current loaded network
%             3 - set up power flows on current loaded network
%             4 - wipe power flows on current loaded network
%             5 - hide component names except for buses
%             6 - show component names
%             7 - hide bus names
%             8 - show bus names
%             9 - set font name
%            10 - set font size
%            11 - save model diagram to eps file
%       FONTSIZE: font size (integer)
%       FONTNAME: font name (string)
%
%see also FM_LIB, FM_SIMSET
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Version:   1.0.0
%
%E-mail:    fmilano@thunderbox.uwaterloo.ca
%Web-site:  http://thunderbox.uwaterloo.ca/~fmilano
%
% Copyright (C) 2002-2006 Federico Milano

fm_var

if isempty(File.data),
  fm_disp(['No loaded system is present at the ' ...
           'moment.'],2),
  return
end
if isempty(findstr(File.data,'(mdl)')),
  fm_disp(['The actual Data ' ...
           'File is not generated from a Simulink model.'],2),
  return
end
if ~Settings.init & ~strcmp(flag,{'mdl2eps','ViewModel'})
  fm_disp(['Perform Power Flow before using ' ...
           'this utility.'],2),
  return
end

lasterr('');

% load Simulink model
cd(Path.data);
filedata = File.data(1:end-5);
open_sys = find_system('type','block_diagram');
if ~sum(strcmp(open_sys,filedata))
  open_system(filedata);
end
cur_sys = get_param(filedata,'Handle');
set_param(cur_sys,'Open','on')
blocks = find_system(cur_sys,'Type','block');
lines = find_system(cur_sys, ...
                    'FindAll','on', ...
                    'type','line');
masks = get_param(blocks,'Masktype');
nblock = length(blocks);

switch flag

 case 'ViewModel'

  % view the current Simulink model.
  hilite_system(cur_sys,'none')

 case 'VoltageReport'

  busidx = find(strcmp(masks,'Bus'));
  for i = 1:Bus.n
    valore = ['|V| = ', ...
              fvar(DAE.V(i),7), ...
              ' p.u.\n<V  = ', ...
              fvar(DAE.a(i),7), ...
              ' rad '];
    set_param(blocks(busidx(i)),'AttributesFormatString',valore);
  end

 case 'WipeVoltageReport'

  busidx = find(strcmp(masks,'Bus'));
  for i = 1:Bus.n,
    set_param(blocks(busidx(i)),'AttributesFormatString','');
  end

 case 'PowerFlowReport'

  tps = Line.con(:,11).*exp(jay*Line.con(:,12)*pi/180);
  VV = DAE.V.*exp(jay*DAE.a);
  r = Line.con(:,8);
  rx = Line.con(:,9);
  chrg = Line.con(:,10)/2;
  chrg1 = chrg - jay*Shunt.g(Line.from) + Shunt.b(Line.from);
  chrg2 = chrg - jay*Shunt.g(Line.to) + Shunt.b(Line.to);
  z = r + jay*rx;
  y = ones(Line.n,1)./z;

  % power flows in transmission lines
  MW_s = VV(Line.from).*conj((VV(Line.from) - ...
         tps.*VV(Line.to)).*y + ...
         VV(Line.from).*(jay*chrg1))./(tps.*conj(tps));
  P_s = real(MW_s);     % P i -> j
  Q_s = imag(MW_s);     % Q i -> j
  MW_r = VV(Line.to).*conj((VV(Line.to) - ...
         VV(Line.from)./tps).*y + VV(Line.to).*(jay*chrg2));
  P_r = real(MW_r);     % P j -> i
  Q_r = imag(MW_r);     % Q j -> i

  lineidx = find(strcmp(masks,'Line'));
  for i = 1:length(lineidx)
    line_out = find_system(lines, ...
                           'SrcBlockHandle',blocks(lineidx(i)));
    line_in  = find_system(lines, ...
                           'DstBlockHandle',blocks(lineidx(i)));
    v_out = [' <- P = ', ...
             fvar(P_r(i),7), ...
             ' p.u.', ...
             char(10), ...
             ' <- Q = ', ...
             fvar(Q_r(i),7), ...
             ' p.u.'];
    v_in  = ['P = ', ...
             fvar(P_s(i),7), ...
             ' p.u. ->', ...
             char(10), ...
             'Q = ', ...
             fvar(Q_s(i),7), ...
             ' p.u. ->'];
    set_param(line_out,'Name',v_out)
    set_param(line_in ,'Name',v_in)
  end

  tipi = Comp.n;
  type_con = Comp.funct;
  n_type = Comp.number;
  type_dat = Varname.fcomp;
  tipi_con = length(Varname.fcomp);

  gp_old = DAE.gp;
  gq_old = DAE.gq;

  for i = 1:tipi
    if eval(n_type{i}) > 0

      type_old = eval([n_type{i}(1:end-2),'.con']);
      typeidx = find(strcmp(masks,n_type{i}(1:end-2)));

      switch n_type{i}(1:end-2)

       case 'Tg'

        for h = 1:length(typeidx)
          line_out = find_system(lines,'SrcBlockHandle', ...
                                 blocks(typeidx(h)));
          v_out = ['Pm = ',fvar(Syn.pm(type_old(h,1)),7),' p.u.'];
          set_param(line_out,'Name',v_out);
        end

       case 'Exc'

        for h = 1:length(typeidx)
          line_out = find_system(lines,'SrcBlockHandle', ...
                                 blocks(typeidx(h)));
          v_out = ['Vf = ',fvar(Syn.vf(type_old(h,1)),7),' p.u.'];
          set_param(line_out,'Name',v_out);
        end

       case 'Oxl'

        for h = 1:length(typeidx)
          line_out = find_system(lines,'SrcBlockHandle', ...
                                 blocks(typeidx(h)));
          v_out = ['Voxl = ',fvar(DAE.x(Oxl.v(h)),7),' p.u.'];
          set_param(line_out,'Name',v_out);
        end

       case 'Pss'

        for h = 1:length(typeidx)
          line_out = find_system(lines,'SrcBlockHandle', ...
                                 blocks(typeidx(h)));
          vst = -DAE.x(Pss.v1,h) + Pss.con(h,3).* ...
                DAE.x(Syn.omega(Pss.exc(h))) + ...
                Pss.con(h,2).*DAE.gp(Syn.bus(Exc.syn(Pss.exc(h))));
          vst = max(vst,Pss.con(h,6));
          vst = min(vst,Pss.con(h,5));
          v_out = ['Vsst = ',fvar(vst,7),' p.u.'];
          set_param(line_out,'Name',v_out);
        end

       otherwise

        tipo = n_type{i}(1:end-2);
        eval(['if isfield(',tipo,',''dat''), dat_old = ', ...
              tipo,'.dat; end'])
        n_type_old = eval(n_type{i});
        eval([n_type{i},' = 1;'])
        for k = 1:length(Varname.fname)
          if strcmp(tipo,Varname.comp{k})
            eval([Varname.uname{k},'_idx_old = ',Varname.comp{k}, ...
                  '.',Varname.uname{k},';'])
          end
        end
        for j = 1:n_type_old
          eval([tipo,'.con = type_old(',int2str(j),',:);'])
          eval(['if isfield(',tipo,',''dat''),',tipo, ...
               '.dat = dat_old(',int2str(j),',:); end'])
          for k = 1:length(Varname.fname)
            if strcmp(tipo,Varname.comp{k})
              if ~isempty(eval([Varname.uname{k},'_idx_old']))
                eval([Varname.comp{k},'.',Varname.uname{k}, ' = ', ...
                      Varname.uname{k},'_idx_old(',int2str(j),');'])
              end
            end
          end
          DAE.gp = zeros(Bus.n,1);
          DAE.gq = zeros(Bus.n,1);
          feval(type_con{i},1)
          line_out = find_system(lines,'SrcBlockHandle', ...
                                 blocks(typeidx(j)));
          line_in  = find_system(lines,'DstBlockHandle', ...
                                 blocks(typeidx(j)));
          if ~isempty(line_out)
            line_out = line_out(1);
            switch tipo
             case 'PV',  d = '1';
             case 'SW',  d = '1';
             case 'Syn', d = '1';
             case 'SSR', d = '1';
             otherwise,  d = '2';
            end
            eval(['a1 = Bus.int(',tipo,'.con(',d,'));'])
            v_out = [' <- P = ',fvar(DAE.gp(a1),7),' p.u.',char(10),[' ' ...
                                '<- Q = '],fvar(DAE.gq(a1),7),' p.u.'];
            if strcmp(tipo,'PV')
              a3 = length(find(Bus.int(type_old(:,1)) == a1));
              v_out = ['P = ',fvar(-DAE.gp(a1),7),' p.u. ->',char(10),['Q ' ...
                                  '= '],fvar(Bus.Qg(a1)/a3,7),' p.u. ->'];
            elseif strcmp(tipo,'SW')
              v_out = ['P = ',fvar(Bus.Pg(a1),7),' p.u. ->',char(10),['Q ' ...
                                  '= '],fvar(Bus.Qg(a1),7),' p.u. ->'];
            elseif strcmp(tipo,'Syn')
              v_out = ['P = ',fvar(Bus.Pg(a1)*Syn.con(22),7),' p.u. ->',char(10) ...
                       ,'Q = ',fvar(Bus.Qg(a1)*Syn.con(23),7),' p.u. ->'];
            end
            set_param(line_out,'Name',v_out);

          end
          if ~isempty(line_in) & ~strcmp(tipo,'Syn')
            for iii = 1:length(line_in)
              switch tipo
               case 'Ltc',
                Porta = get_param(line_in(iii),'DstPortHandle');
                Blocco = get_param(blocks(typeidx(j)),'PortHandles');
                if find(Blocco.Inport==Porta), d = '1'; end
                if ~isempty(Blocco.Enable)
                  if find(Blocco.Enable==Porta), d = '15'; end
                end
                if isfield(Blocco,'LConn')
                  if find(Blocco.LConn==Porta), d = '1'; end
                end
               case {'SAE1','SAE2','SAE3'},
                Porta = get_param(line_in(iii),'DstPortHandle');
                Blocco = get_param(blocks(typeidx(j)),'PortHandles');
                Numero_Porta = find(Blocco.Inport==Porta);
                if isempty(Numero_Porta)
                  Numero_Porta = find(Blocco.LConn==Porta);
                end
                if Numero_Porta == 1, d = '2'; else, d = '1'; end
               otherwise
                d = '1';
              end

              eval(['a2 = Bus.int(',tipo,'.con(',d,'));'])
              v_in  = ['P = ',fvar(DAE.gp(a2),7),' p.u. ->',char(10), ...
                      'Q = ',fvar(DAE.gq(a2),7),' p.u. ->'];
              set_param(line_in(iii),'Name',v_in);

            end

          end

        end

        eval([tipo,'.con = type_old;'])
        eval(['if isfield(',tipo,',''dat''), ',tipo, ...
             '.dat = dat_old; end'])
        eval([n_type{i},'= n_type_old;'])
        for k = 1:length(Varname.fname)
          if strcmp(tipo,Varname.comp{k})
            eval([Varname.comp{k},'.',Varname.uname{k}, ...
                 ' = ',Varname.uname{k},'_idx_old;'])
          end
        end
      end
    end
  end

  DAE.gp = gp_old;
  DAE.gq = gq_old;

 case 'WipePowerFlowReport'

  for i = 1:length(lines),
    set_param(lines(i),'Name','');
  end

 case 'HideNames'

  nobusidx = find(~strcmp(masks,'Bus'));
  for i = 1:length(nobusidx),
    set_param(blocks(nobusidx(i)),'ShowName','off');
  end
  nobusidx = find(strcmp(masks,''));
  for i = 1:length(nobusidx),
    set_param(blocks(nobusidx(i)),'ShowName','on');
  end

 case 'ShowNames'

  for i = 1:nblock,
    set_param(blocks(i),'ShowName','on');
  end

 case 'HideBusNames'

  busidx = find(strcmp(masks,'Bus'));
  for i = 1:Bus.n,
    set_param(blocks(busidx(i)),'ShowName','off');
  end

 case 'ShowBusNames'

  busidx = find(strcmp(masks,'Bus'));
  for i = 1:Bus.n,
    set_param(blocks(busidx(i)),'ShowName','on');
  end

 case 'FontType'

  for i = 1:nblock,
    set_param(blocks(i),'FontName',fontname);
  end
  for i = 1:length(lines),
    set_param(lines(i),'FontName',fontname);
  end

 case 'FontSize'

  for i = 1:nblock,
    set_param(blocks(i),'FontSize',fontsize);
  end
  for i = 1:length(lines),
    set_param(lines(i),'FontSize',fontsize);
  end

 case 'mdl2eps'
  
  % fontsize == 0:  export to color eps
  % fontsize == 1:  export to grey-scale eps
  grey_eps = fontsize;
  pat = '^/c\d+\s\{.* sr\} bdef';
  
  cd(Path.data)
  fileeps = [filedata,'.eps'];
  a = dir(fileeps);
  Settings.ok = 1;
  if ~isempty(a)
    uiwait(fm_choice(['Overwrite "',fileeps,'" ?']))
  end
  if ~Settings.ok, return, end
  orient portrait
  print('-s','-depsc',fileeps)

  if ~Settings.noarrows
    cd(Path.local)
    fm_disp(['PSAT model saved in ',Path.data,fileeps])
    return
  end

  file = textread(fileeps,'%s','delimiter','\n');

  idx = [];
  d2 = zeros(1,4);

  for i = 1:length(file)
    if grey_eps & ~isempty(regexp(file{i},pat))
      matchexp = regexp(file{i},'^/c\d+\s\{','match');
      colors = strrep(file{i},matchexp{1},'[');
      colors = regexprep(colors,' sr\} bdef',']');
      rgb = sum(str2num(colors));
      colors = num2str([rgb rgb rgb]/3);
      file{i} = [matchexp{1},colors,' sr} bdef'];
    end      
    if strcmp(file{i},'PP')
      if strcmp(file{i-1}(end-1:end),'MP')
        d1 = str2num(strrep(file{i-1},'MP',''));
        if ~d1(3)
          d2(2) = d1(6)-d1(2);
          d2(4) = d1(6)-d1(2);
          d2(1) = d1(5)-d1(2);
          d2(3) = d1(5)+d1(1);
        else
          d2(2) = d1(6)+d1(2);
          d2(4) = d1(6)-d1(1);
          d2(1) = d1(5)-d1(1);
          d2(3) = d1(5)-d1(1);
        end
        file{i-1} = sprintf('%4d %4d mt %4d %4d L',d2);
        idx = [idx, i];
      end
    end
    if ~isempty(findstr(file{i}(max(1,end-1):end),'PO'))
      d1 = str2num(strrep(file{i},'PO',''));
      nextline = strrep(file{i+1},'L','');
      d2 = str2num(strrep(nextline,'mt',''));
      if d1(4) == d2(2)
        if d2(1) > d1(3);
          d2(1) = d1(3)-d1(1);
        else
          d2(1) = d2(1)+d1(1)+abs(d1(3)-d2(1));
        end
      else
        if d2(2) > d1(4)
          d2(2) = d1(4)-d1(2);
        else
          d2(2) = d2(2)+d1(2)+abs(d1(4)-d2(2));
        end
      end
      file{i+1} = sprintf('%4d %4d mt %4d %4d L',d2);
      idx = [idx, i];
    end
  end

  file(idx) = [];

  fid = fopen(fileeps,'wt+');
  for i = 1:length(file)
    fprintf(fid,'%s\n',file{i});
  end
  fclose(fid);
  cd(Path.local)
  fm_disp(['PSAT model saved in ',Path.data,fileeps])

 otherwise

  fm_disp('Unknown command for Simulink Settings GUI...',2)

end

cd(Path.local)


⌨️ 快捷键说明

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