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

📄 fm_install.m

📁 这是一个很适合研究和学习用的电力系统仿真软件
💻 M
📖 第 1 页 / 共 2 页
字号:
function fm_install
% FM_INSTALL install an user defined component
%
% FM_INSTALL
%
%see also FM_UNINSTALL, FM_MAKE, FM_BUILD
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    22-Feb-2004
%Version:   1.0.1
%
%E-mail:    fmilano@thunderbox.uwaterloo.ca
%Web-site:  http://thunderbox.uwaterloo.ca/~fmilano
%
% Copyright (C) 2002-2006 Federico Milano

global Algeb Buses Initl Param Servc State
global Comp Fig Path

% some settings
error_vect = [];
error_number = 0;
lasterr('');
string_update = cell(14,1);

if exist(['fm_',Comp.name,'.m']) == 2
  string_update{1,1} = ['Component File "fm_', Comp.name,'.m".'];
else
  fm_choice(['Before updating system files, make sure that the ' ...
                 'component function is in the PSAT path.'],2)
  return
end

if Fig.update == 0; fm_update; end
hdl_list = findobj(Fig.update,'Tag','Listbox1');

% structure and file names
c_name = lower(Comp.name);
c_name(1) = upper(c_name(1));
f_name = ['fm_',lower(Comp.name)];

okdata = 0;
n_serv = 0;
for i = 1:Servc.n
  if ~strcmp(Servc.type{i},'Input');
    okdata = 1;
    n_serv = n_serv + 1;
  end
end

% **********************************************************************
% update file comp.ini
fid = fopen([Path.psat,'comp.ini'], 'rt+');
if fid == -1
  fm_disp('Cannot open file "comp.ini". Check permissions.')
end
update = 1;
algeq = 0;
limit = 0;
for i = 1:length(Algeb.eq)
  if ~strcmp(Algeb.eq{i,1},'null'),
    algeq = 1;
    break
  end
end
for i = 1:State.n
  if ~strcmp(State.limit{i,1},'None') | ~strcmp(State.limit{i,2},'None')
    limit = 1;
    break
  end
end

while 1
  sline = fgetl(fid);
  if ~ischar(sline), break; end
  sline = [sline,blanks(length(f_name))];
  if strmatch(f_name,sline(1:length(f_name)),'exact')
    update = 0;
    break
  end
end
if update
  count = fseek(fid,0,'eof');
  a = [f_name,blanks(23)];
  a = a(1:23);
  b = [c_name,blanks(20)];
  b = b(1:20);
  if algeq,      c = '1 1 ';      else, c = '0 0 ';      end
  if State.n,    c = [c, '1 1 ']; else, c = [c, '0 0 ']; end
  if limit,      c = [c, '1 '];   else, c = [c, '0 '];   end
  if Comp.init,  c = [c, '0 1'];  else, c = [c, '1 0'];  end
  if Comp.shunt, c = [c, ' 1'];   else, c = [c, ' 0'];   end
  fprintf(fid,['\n%s %s %s'],a,b,c);
  comp_prop = num2str(['[',c,']']);
end
fclose(fid);

string_update{2,1} = 'Data File "comp.ini" updated.';
set(hdl_list,'String',string_update);

% **********************************************************************
% update function "fm_ncomp.m"
try
  if strcmp(Path.psat(1),'~')
    pathname = [getenv('HOME'),Path.psat(2:end)];
  else
    pathname = Path.psat;
  end
  file = textread([pathname,'fm_ncomp.m'], ...
                  '%s','delimiter','\n','whitespace','');
  idx1 = strmatch('check = 1;',file,'exact');
  idx2 = strmatch(['  ',c_name,'.n'],file);
  if isempty(idx2)
    file{idx1,1} = [];
    file{end+3} = ['global ',c_name];
    file{end+1} = sprintf('if ~isempty(%s.con)',c_name);
    file{end+1} = sprintf('  %s.n = length(%s.con(:,1));',c_name, ...
                          c_name);
    if Buses.n == 1
      file{end+1} = sprintf('  %s.bus = Bus.int(round(%s.con(:,1)));', ...
                            c_name,c_name);
    else
      for i = 1:Buses.n
        file{end+i} = sprintf('  %s.%s = Bus.int(round(%s.con(:,%d)));', ...
                              c_name,Buses.name{i},c_name,i);
      end
    end
    if Initl.n | okdata
      file{end+1} = sprintf('  %s.dat = zeros(%s.n,%d);', ...
              c_name,c_name,Initl.n+n_serv);
    end
    file{end+1} = 'end';
    file{end+1} = ' ';
    file{end+1} = 'check = 1;';
    fid = fopen([Path.psat,'fm_ncomp.m'], 'wt');
    for i=1:length(file)-1, fprintf(fid,'%s\n',file{i,1}); end
    fprintf(fid,'%s',file{end,1});
    fclose(fid);
  end
catch
  fm_disp('Cannot rewrite file "fm_ncomp.m". Check file permissions and content.')
  return
end
string_update{3,1} = 'System file "fm_ncomp.m" updated.';
set(hdl_list,'String',string_update);

% *********************************************************************
% update function "fm_inilf.m"
fid = fopen([Path.psat,'fm_inilf.m'], 'rt+');
if fid == -1
  fm_disp('Cannot open file "fm_inilf.m". Check permissions.')
end
update = 1;

while 1
  sline = fgetl(fid);
  if ~ischar(sline)
    break
  end
  if findstr([c_name,'.con'],sline)
    update = 0;
    break
  end
end
if update
  fseek(fid,0,'eof');
  fprintf(fid,'\n\n%s.con = [];',c_name);
  fprintf(fid,'\n%s.n = 0;',c_name);
  if Buses.n == 1
    fprintf(fid,'\n%s.bus = [];',c_name);
  else
    for i = 1:Buses.n
      fprintf(fid,'\n%s.%s = [];',c_name,Buses.name{i});
    end
  end
  if Initl.n | okdata
    fprintf(fid,'\n%s.dat = [];',c_name);
  end
  for i = 1:State.n
    fprintf(fid,'\n%s.%s = [];',c_name,State.name{i,1});
  end
end

fclose(fid);
string_update{4,1} = 'System file "fm_inilf.m" updated.';
set(hdl_list,'String',string_update);

% **********************************************************************
% update function "fm_var.m"
fid = fopen([Path.psat,'fm_var.m'], 'rt+');
if fid == -1
  fm_disp('Cannot open file "fm_var.m". Check permissions.')
end
update = 1;

while 1
  sline = fgetl(fid);
  if ~ischar(sline)
    break
  end
  if strmatch(['global ',c_name],sline,'exact');
    update = 0
    break
  end
end
if update
  fseek(fid,0,'eof');
  fprintf(fid,['\n\n%%     ',Comp.descr,'\nglobal ',c_name]);
end

fclose(fid);
string_update{5,1} = 'System file "fm_var.m" updated.';
set(hdl_list,'String',string_update);

% ***********************************************************************
% update file "namevar.ini"
fid = fopen([Path.psat,'namevar.ini'], 'rt+');
if fid == -1
  fm_disp('Cannot open file "namevar.ini". Check permissions.')
end

for i = 1:State.n
  update = 1;
  fseek(fid,0,'bof');
  while 1
    sline = fgetl(fid);
    if ~ischar(sline), break; end
    if ~isempty(findstr(c_name,sline)) & ...
          ~isempty(findstr(State.fn{i,1},sline))
      update = 0;
      break
    end
  end
  if update
    fseek(fid,0,'eof');
    nome1 = [State.un{i,1},blanks(19)];
    nome2 = [State.fn{i,1},blanks(19)];
    fprintf(fid,'\n%s %s %s',nome1(1:19),nome2(1:19),c_name);
  end
end

fclose(fid);
string_update{6,1} = 'Data file "namevar.ini" updated.';
set(hdl_list,'String',string_update);

% ************************************************************************
% update functions "fm_dynlf.m" & "fm_dynidx.m"
if State.n > 0
  if ~Comp.init
    fid = fopen([Path.psat,'fm_dynlf.m'], 'rt+');
    if fid == -1
      fm_disp('Cannot open file "fm_dynlf.m". Check permissions.')
    end
    update = 1;
    while update
      sline = fgetl(fid);
      if ~ischar(sline), break; end
      if findstr([c_name,'.n'],sline); update = 0; end
    end
    if update
      fseek(fid,0,'eof');
      fprintf(fid,'\n\nglobal %s',c_name);
      fprintf(fid,'\nif %s.n',c_name);
      fprintf(fid,'\n  for i = 1:%s.n',c_name);
      for i = 1:State.n
        fprintf(fid,'\n    %s.%s(i) = DAE.n + %d;', ...
                c_name,State.name{i,1},i);
      end
      fprintf(fid,'\n    DAE.n = DAE.n + %d;',State.n);
      fprintf(fid,'\n  end');
      fprintf(fid,'\nend');
    end
    fclose(fid);
    string_update{7,1} = 'System file "fm_dynlf.m" updated.';
    string_update{8,1} = 'Updating of "fm_dynidx.m is not required.';
    set(hdl_list,'String',string_update);
  else
    fid = fopen(['fm_dynidx.m'], 'rt+');
    if fid == -1
      fm_disp('Cannot open file "fm_dynidx.m". Check permissions.')
    end
    update = 1;
    while 1
      sline = fgetl(fid);
      if ~ischar(sline), break; end
      if findstr([c_name,'.n'],sline); update = 0; break; end
    end
    if update
      fseek(fid,0,'eof');
      fprintf(fid,'\n\nglobal %s',c_name);
      fprintf(fid,'\nif %s.n',c_name);
      for i = 1:State.n
        fprintf(fid,'\n  %s.%s = zeros(%s.n,1);',c_name,State.name{i,1},c_name);
      end
      fprintf(fid,'\n  for i = 1:%s.n',c_name);
      for i = 1:State.n
        fprintf(fid,'\n    %s.%s(i) = DAE.n + %d;',c_name, State.name{i,1},i);
      end
      fprintf(fid,'\n    DAE.n = DAE.n + %d;',State.n);
      fprintf(fid,'\n  end');
      fprintf(fid,'\nend');
    end
    fclose(fid);
    string_update{8,1} = 'System file "fm_dynidx.m" updated.';
    string_update{7,1} = 'Updating of "fm_dynlf.m is not required.';
    set(hdl_list,'String',string_update);
  end
else
  string_update{8,1} = 'Updating of "fm_dynidx.m" is not required (no state variables).';

⌨️ 快捷键说明

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