% Humboldt University Berlin
% Department of Mathematics - Dr. Rene Lamour
% *************************************************************
% Shared Parameter Version for matlab program main_consi
% *************************************************************
%
% Name:                   main_consi.m
%                         ------------
% Author:                 Rene' Lamour/Sigrid Wehner
% Date:                   26.6.03
%
% The code main_consi calculates consistent initial values for DAEs up to index 2. 
% The DAE has the structure
%                         f((d(x,t)',x,t) = 0  and the program delivers
% values z0=d(x(t0),t0)', x0=x(t0) for t=t0  that  f(z0,x0,t0)=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 of main_consi:
%
%   daefile  - m-File, which describes the DAE
%   z0       - initial guess of z0
%   x0       - initial guess of x0
%   t0       - time point
%   z        - function with additional user requirements (for default use 'z')
%   varargin - variable arguments of daefile (if exists).
%
% See also main_consi_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_consi reads the binary
% parameter files:         parameter_mainconsi.mat,
%                          parameter_general.mat and
%                          parameter_nlsolve.mat
%
% These files contain either the original default parameters 
%                         or the specific user definitions
%
% original parameters are:  
%
% parameter_general.mat:
%                        s_A=0            (switches for calculation:
%                                           0 numerical, 1 analytical)
%                        s_D=0
%                        s_B=0  
%                        print_level=2    (level of output:
%                                           0 no output
%                                           1 standard output (screen)
%                                           2 separate files )
%                         ----------------------------------------------
% parameter_mainconsi.mat
%                         threshold=1e-9   (threshold for rank determination)
%                         ep=1e-13         (accuracy of Newton method)
%                         given_index=-1   (index check or not)
%                         s_Bt=0           (switches for dependency of t:
%                                          (-1 not, 0 yes) - numerical
%                                          calculation of B_t )
%                         s_Dt=0
%                         s_ft=0           (switches for dependency of t
%                                           and calculation:
%                                          -1 not t dependent
%                                           0 numerical, 1 analytical )
%                         ----------------------------------------------
% parameter_nlsolve.mat
%                         max_of_count=10    (maximal number of internal
%                         iterations)
%                         max_of_bigcount=20 (maximal number of outer iterations)
%                         fak_min            (advice if 1/(speed of iteration)
%                         greater then fak_min)
%                         lambda = 1 (initial damping parameter)
% 
% **********************************************************************

function [ok,y1,x1]=main_consi(daefile,z0,x0,t0,z,varargin)

% read the actual parameter version from the binary file
% and log the actual status

load parameter_mainconsi;
load parameter_general;

m=size(z0,1);
n=size(x0,1);
alpha=x0;

% **********************************************************************

%   delete of log-files of previous runs
%
if print_level==2
    if exist(['nlsolve_',date,'.log']) > 0
        delete(['nlsolve_',date,'.log']);
    end;
    if exist(['nl_consi_',date,'.log']) > 0
        delete(['nl_consi_',date,'.log']);
    end;
end;
%
xy0=[z0;x0];
%
[xy,ok] = nlsolve('nl_consi',xy0,ep,'lin_sep_and_trans','lin_sep_solve',print_level,...
    n,daefile,t0,z,alpha,s_A,s_D,s_B,s_Bt,s_Dt,s_ft,given_index,threshold,varargin{:});
if ok
    format long e
    y1=xy(1:m)
    x1=xy(m+1:end)
else
    disp('main_consi: no convergence');
    disp('main_consi: last iteration:');
    format long e
    y1=xy(1:m)
    x1=xy(m+1:end)
end;

% logfile information

logmain=['nl_consi_',date,'.log'];
lognlsolve=['nlsolve_',date,'.log'];
log_parameter_mainconsi(logmain);
if (s_log == 0)
    if (exist(logmain) > 0)
        delete(logmain);
    end;
    if (exist(lognlsolve) > 0)
        delete(lognlsolve);
    end;
end;

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






