% Humboldt University Berlin
% Institute for Mathematics - Dr. Rene Lamour
% *************************************************************
% Shared Parameter Version for matlab program main_index
% *************************************************************
% Name:                   main_index.m
%                         ------------
% Author:                 Rene Lamour / Sigrid Wehner
%                  
% Date:                  1.10.03
%
%
% The code main_index calculates consistent initial values for DAEs. 
% The DAE has the structure
%                         f((d(x,t)',x,t) = 0.
% 
% The user has to implement a matlab function, which describes at least 
% the functions f and d (see example function daefile.m). 
% For sensitive examples the explicit code of the Jacobians is recommended.
%
%                         A:=f'_z, D:=f'_x, B:=f'_x.
%
% The use is controlled by parameters (see main_consi_example.m). 
%
% Parameters:
%   daefile    - m-File, which describes the DAE
%   t          - time point
%   xfunction  - function x(t), around which the linearisation takes place
%   argx       - number of arguments used by xfunction
%   varargin   - arguments of argx followed by additional arguments of daefile (if
%                exists)
%
% See also main_index_example.m.
%
% References:    R. Lamour: Index Determination and Calculation of
% Consistent Initial Values for DAEs, Proceedings of NMCM 2002, Miskolc,
% Hungary, see http://www.math.hu-berlin.de/~lamour/software/matlab

% The code main_index reads the binary
% parameter files:        parameter_mainconsi.mat     
%                         parameter_general.mat
%
%                         these files contain either the original default
%                         parameters 
%                         or the specific user definitions
%
% try      help setuser_para_general
%          help setuser_para_mainconsi
%                                          for more information
%                                           
% **********************************************************************

function index=main_index(daefile,t,xfunction,argx,varargin)

% read the actual parameter version from the binary files

load parameter_mainconsi threshold;
load parameter_general print_level s_A s_D s_B s_log;

%

if s_log == 1
    %
    %   delete of log-files of previous runs
    %
    if exist([daefile,'_',date,'.log']) > 0
        delete([daefile,'_',date,'.log']);
    end;
end;
fid=file_open_date(print_level,daefile);
fprintf(fid,'%s\n',headline(daefile));
%
given_index=-1;
[ok,index]=matrix_sequence(daefile,xfunction,t,s_A,s_D,s_B,given_index,threshold,fid,argx,varargin{:});
index


% for the creation of mainindex-logfile 
% =====================================
if (s_log == 1 & print_level == 2)
    logmainindex=[daefile,'_',date,'.log'];
    log_parameter_mainindex(logmainindex);
end;
    % delete the logfile if switch s_log==0
if (s_log == 0)
    if (exist(logmainindex) >0)
        delete(logmainindex);
    end;
end;


fprintf('---------------------------------------------------------------------\n');
fprintf('END OF main_index ... The following logfiles have been created:\n');
if (s_log == 1 & print_level == 2)
  fprintf('from main_index:                      %s\n',logmainindex);
else
  fprintf(' NO logfiles');
end;
