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

📄 fm_build.m

📁 电力系统分析计算程序
💻 M
📖 第 1 页 / 共 2 页
字号:
function fm_build%FM_BUILD build new component functions (Symbolic Toolbox is needed)%%FM_BUILD%%see also FM_MAKE FM_COMPONENT%%Author:    Federico Milano%Date:      11-Nov-2002%Update:    19-Dec-2003%Version:   1.0.1%%E-mail:    Federico.Milano@uclm.es%Web-site:  http://www.uclm.es/area/gsee/Web/Federico%% Copyright (C) 2002-2008 Federico Milanoglobal Comp Settings Fig Pathglobal Algeb Buses Initl Param Servc State% ***********************************************************************% some control variableserror_v = [];lasterr('');null = '0';% useful stringsc_name = Comp.name;c_name(1) = upper(c_name(1));% variable arraysstate_vect = varvect(State.name, ' ');algeb_vect = varvect(Algeb.name, ' ');param_vect = varvect(Param.name, ' ');initl_vect = varvect(Initl.name, ' ');servc_vect = varvect(Servc.name, ' ');pq_Servc = 0;% equation arraysservc_eq = varvect(Servc.eq,'; ');state_eq = varvect(State.eq,'; ');algeb_eq = varvect(Algeb.eq,'; ');% ********************************************************************************% check equationsif State.neq > 0  state_check = strmatch('null',State.eq,'exact');  if state_check    error_v = [error_v; ...               fm_strjoin('Differential equation for "', ...                      State.eqidx(state_check), ...                      '" has not been defined.')];  endendif Servc.neq > 0  servc_foo = fm_strjoin(Servc.type,Servc.eq);  servc_check = strmatch('Innernull',servc_foo,'exact');  servc_check = [servc_check; strmatch('Outputnull',servc_foo,'exact')];  if servc_check    error_v = [error_v; ...               fm_strjoin('Service equation for "', ...                      Servc.eqidx(servc_check), ...                      '" has not been defined.')];  endend% ********************************************************************************% check variable usageservc_idx = [strmatch('Inner',Servc.type); strmatch('Output',Servc.type)];total_var = [State.name; Algeb.name; Servc.eqidx(servc_idx); Param.name; Initl.name];total_eqn = [' ',servc_eq,' ',state_eq,' ',algeb_eq,' ',varvect(State.time,'*'),' '];for i = 1:length(total_var)  idx = findstr(total_eqn,total_var{i});  if isempty(idx)    error_v{end+1,1} = ['The variable "',total_var{i},'" is not used in any equation.'];  else    before = total_eqn(idx-1);    after  = total_eqn(idx+length(total_var{i}));    check = 1;    for j = 1:length(idx)      a = double(after(j));       b = double(before(j));      a1 = ~isletter(after(j));   a2 = (a ~= 95);  a3 = (a > 57 | a < 48);      b1 = ~isletter(before(j));  b2 = (b ~= 95);  b3 = (b > 57 | b < 48);      if a1 & a2 & a3 & b1 & b2 & b3, check = 0; break, end    end    if check      error_v{end+1,1} = ['The variable "',total_var{i}, ...                          '" is not used in any equation.'];    end  endend% ********************************************************************************% symbolic variablestry  if state_vect, eval(['syms ',state_vect]), end  if algeb_vect, eval(['syms ',algeb_vect]), end  if param_vect, eval(['syms ',param_vect]), end  if servc_vect, eval(['syms ',servc_vect]), end  if initl_vect, eval(['syms ',initl_vect]), end  % compute Jacobians matrices (Maple Symbolic Toolbox)  if ~isempty(state_eq)    if ~isempty(state_vect)      eval(['Fx = jacobian([',state_eq, '],[', state_vect, ']);']);    end    if ~isempty(algeb_vect)      eval(['Fy = jacobian([',state_eq, '],[', algeb_vect, ']);']);    end    if ~isempty(servc_vect)      eval(['Fz = jacobian([',state_eq, '],[', servc_vect, ']);']);    end    if pq_Servc      eval(['Fpq = jacobian([',state_eq, '],[', pq_vect, ']);']);    end  end  if ~isempty(algeb_eq)    if ~isempty(state_vect)      eval(['Gx = jacobian([',algeb_eq, '],[', state_vect, ']);']);    end    if ~isempty(algeb_vect)      eval(['Gy = jacobian([',algeb_eq, '],[', algeb_vect, ']);']);    end    if ~isempty(servc_vect)      eval(['Gz = jacobian([',algeb_eq, '],[', servc_vect, ']);']);    end    if pq_Servc      eval(['Gpq = jacobian([',alg_eqeb, '],[', pq_vect, ']);']);    end  end  if ~isempty(servc_eq)    if ~isempty(state_vect)      eval(['Zx = jacobian([',servc_eq, '],[', state_vect, ']);']);    end    if ~isempty(algeb_vect)      eval(['Zy = jacobian([',servc_eq, '],[', algeb_vect, ']);']);    end    if ~isempty(servc_vect)      eval(['Zz = jacobian([',servc_eq, '],[', servc_vect, ']);']);    end    if pq_Servc      eval(['Zpq = jacobian([',servc_eq, '],[', pq_vect, ']);']);    end  endend% ********************************************************************************% check synthax of equationsfor i = 1:State.neq  try    eval([State.eq{i,1},';'])  catch    error_v{end+1,1} = [lasterr, ' (In differential equation "', ...                        State.eq{i,1}, '")'];  end  try    eval([State.init{i,1},';'])  catch    error_v{end+1,1} = [lasterr, ' (In state variable  "', ...                        State.name{i,1}, '" initialization expression)'];  end  state_init{i,1} = vectorize(State.init{i,1});endfor i = 1:Algeb.neq  try    eval([Algeb.eq{i,1},';'])  catch    error_v{end+1,1} = [lasterr, ' (In algebraic equation "', ...                        State.eq{i,1}, '")'];  endendfor j = 1:Servc.neq  try    eval([Servc.eq{i,1},';'])  catch    error_v{end+1,1} = [lasterr, ' (In service equation "', ...                        Servc.eqidx{i,1}, '")'];  endend% check component nameif isempty(Comp.name)  error_v{end+1,1} = 'Component name is empty.';  returnend% ********************************************************************************% display errorsif ~isempty(error_v)  error_v = fm_strjoin('Error#',num2str([1:length(error_v)]'),': ',error_v);  error_v = [{['REPORT OF ERRORS ENCOUNTERED WHILE BUILDING ', ...               'NEW COMPONENT "',Comp.name,'.m"']}; error_v];  error_v{end+1,1} = ['BUILDING NEW COMPONENT FILE "', ...                      Comp.name,'.m" FAILED'];  fm_disp  fm_disp(error_v{1:end-1})  fm_disp(['BUILDING NEW COMPONENT FILE "',Comp.name,'.m" FAILED'])  fm_update  set(findobj(Fig.update,'Tag','Listbox1'),'String',error_v, ...                    'BackgroundColor','w', ...                    'ForegroundColor','r', ...                    'Enable','inactive', ...                    'max',2, ...                    'Value',[]);  set(findobj(Fig.update,'Tag','Pushbutton2'),'Enable','off');  returnend% ***********************************************************************% check for previous versionsa = what(Path.psat);olderfile = strmatch(['fm_',Comp.name,'.m'],a.m,'exact');if ~isempty(olderfile)  uiwait(fm_choice(['Overwrite Existing File "fm_',Comp.name,'.m" ?']));  if ~Settings.ok, return, endend% ***********************************************************************% open new component filefid = fopen([Path.psat, 'fm_', Comp.name,'.m'], 'wt');if fid == -1  fm_disp(['Cannot open file fm_',Comp.name,'. Check permissions'])  returnendfprintf(fid, ['function  fm_', Comp.name, '(flag)']);% write help of the functionif isempty(Comp.descr)  Comp.descr = ['Algebraic Differential Equation ', ...                Comp.name, '.m'];endfprintf(fid, ['\n\n%%FM_', upper(Comp.name),' defines ',Comp.descr]);% ********************************************************************% data format .confprintf(fid, ['\n%%\n%%Data Format ', c_name, '.con:']);fprintf(fid, '\n%%  col #%d: Bus %d number',[1:Buses.n;1:Buses.n]);idx_inn = strmatch('Inner',  Servc.type, 'exact');idx_inp = strmatch('Input',  Servc.type, 'exact');idx_out = strmatch('Output', Servc.type, 'exact');fprintf(fid, '\n%%  col #%d: Power rate [MVA]',Buses.n+1);fprintf(fid, '\n%%  col #%d: Bus %d Voltage Rate [kV]', ...        [Buses.n+1+[1:Buses.n];1:Buses.n]);fprintf(fid, '\n%%  col #%d: Frequency rate [Hz]',2*Buses.n+2);inip = 2*Buses.n+3;endp  = 2*Buses.n+2+Param.n;pidx = inip:endp;for i=1:length(pidx)  fprintf(fid, '\n%%  col #%d: %s %s [%s]', ...          pidx(i),Param.name{i},Param.descr{i},Param.unit{i});endx_max = [1:State.n];if State.n  x_idx = strmatch('None',State.limit(:,1),'exact');  x_max(x_idx) = [];endx_min = [1:State.n];if State.n  x_idx = strmatch('None',State.limit(:,2),'exact');  x_min(x_idx) = [];endn_xmax = length(x_max); n_xmin = length(x_min);for i=1:n_xmax  fprintf(fid,'\n%%  col #%d: %s',endp+i, ...          State.limit{x_max(i),1});endfor i=1:n_xmin  fprintf(fid,'\n%%  col #%d: %s',endp+n_xmax+i, ...          State.limit{x_min(i),1});ends_max = [1:Servc.neq];if Servc.n  s_idx = strmatch('None',Servc.limit(:,1),'exact');  s_max(s_idx) = [];ends_min = [1:Servc.neq];if Servc.n  s_idx = strmatch('None',Servc.limit(:,2),'exact');  s_min(s_idx) = [];endn_smax = length(s_max); n_smin = length(s_min);for i=1:n_smax  fprintf(fid,'\n%%  col #%d: %s', ...          endp+n_xmax+n_xmin+i,Servc.limit{s_max(i),1});endfor i=1:n_smin  fprintf(fid,'\n%%  col #%d: %s', ...          endp+n_xmax+n_xmin+n_smax+i,Servc.limit{s_min(i),1});endokdata = 0;nidx = 0;if ~isempty(idx_inn) | ~isempty(idx_out)  okdata = 1;endif Initl.n | okdata  fprintf(fid, ['\n%% \n%%Data Structure: ', c_name, '.dat:']);endfor i=1:Initl.n  fprintf(fid,'\n%%  col #%d: %s', i,Initl.name{i});endif okdata  nidx = length(idx_inn)+length(idx_out);  iidx = [idx_inn;idx_out];  for i=1:nidx    fprintf(fid,'\n%%  col #%d: %s', ...            Initl.n+i,Servc.eqidx{iidx(i)});  endend% function callsfprintf(fid, ['\n%% \n%%FM_', upper(Comp.name),'(FLAG)']);if Comp.init  fprintf(fid, ['\n%%   FLAG = 0 -> initialization']);endif ~isempty(algeb_eq)  fprintf(fid, ['\n%%   FLAG = 1 -> algebraic equations']);  fprintf(fid, ['\n%%   FLAG = 2 -> algebraic Jacobians']);endif ~isempty(state_eq);  fprintf(fid, ['\n%%   FLAG = 3 -> differential equations']);  fprintf(fid, ['\n%%   FLAG = 4 -> state Jacobians']);endif n_xmax | n_xmin > 0  fprintf(fid, ['\n%%   FLAG = 5 -> non-windup limiters)']);endfprintf(fid, '\n%% \n%%Author:    File automatically generated by PSAT');fprintf(fid, '\n%%Date:      %s',date);% global variablesfprintf(fid, ['\n\nglobal ',c_name,' DAE Bus Settings']);% ************************************************************************% general settingsfprintf(fid, '\n');for i=1:State.n  fprintf(fid,'\n%s = DAE.x(%s.%s);',State.name{i},c_name, ...          State.name{i});endif Algeb.n  idx_v = strmatch('V',Algeb.name);  idx_a = strmatch('t',Algeb.name);  if idx_v    num_v = strrep(Algeb.name(idx_v),'V','');    if Buses.n == 1      fprintf(fid,'\n%s = DAE.y(%s.bus+Bus.n);', ...              Algeb.name{idx_v},c_name);    else      for i=1:length(idx_v)        fprintf(fid,'\n%s = DAE.y(%s.bus%s+Bus.n);', ...                Algeb.name{idx_v(i)},c_name,num_v{i});      end    end  end  if idx_a    num_a = strrep(Algeb.name(idx_a),'theta','');    if Buses.n == 1      fprintf(fid,'\n%s = DAE.y(%s.bus);', ...              Algeb.name{idx_a},c_name);    else      for i=1:length(idx_a)        fprintf(fid,'\n%s = DAE.y(%s.bus%s);', ...                Algeb.name{idx_a(i)},c_name,num_a{i});      end    end  endendfor i=1:Param.n  fprintf(fid,'\n%s = %s.con(:,%d);',Param.name{i},c_name,pidx(i));endfor i=1:n_xmax,  fprintf(fid,'\n%s = %s.con(:,%d);',State.limit{x_max(i),1}, ...	  c_name,endp+i);endfor i=1:n_xmin,  fprintf(fid,'\n%s = %s.con(:,%d);',State.limit{x_min(i),2}, ...	  c_name,endp+n_xmax+i);endfor i=1:n_smax,  fprintf(fid,'\n%s = %s.con(:,%d);',Servc.limit{s_max(i),1}, ...	  c_name,endp+n_xmax+n_xmin+i);endfor i=1:n_smin,  fprintf(fid,'\n%s = %s.con(:,%d);',Servc.limit{s_min(i),2}, ...	  c_name,endp+n_xmax+n_xmin+n_smax+i);endfor i=1:Initl.n,  fprintf(fid,'\n%s = %s.dat(:,%d);',Initl.name{i},c_name,i);endfor i=1:nidx,  fprintf(fid,'\n%s = %s.dat(:,%d);',Servc.eqidx{iidx(i)},c_name, ...	  Initl.n+i);end% **********************************************************************% initializationif Comp.init  fprintf(fid, '\n\nswitch flag\n case 0 %% initialization');  msg = ['Component'];  idx_T = [1:State.n];  idx = strmatch('None',State.time,'exact');  idx_T(idx) = [];  if idx_T    fprintf(fid,'\n\n  %%check time constants');  end  for i=1:length(idx_T),

⌨️ 快捷键说明

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