% Humboldt University Berlin
% Institute for Mathematics - Dr. Rene Lamour
% *************************************************************
% Program to prepare user driven parameters for nlsolve
% *************************************************************
% Name:               setuser_para_nlsolve
%                     --------------------
% Author:             Sigrid Wehner
% Stand:              28.6.03
%                     ------------------------------------------------------
%                     Initial parameter state is recalled by empty parameter
%                     list:  setuser_para_nlsolve()
% posible
% parameters open
% to user changes:    in nlsolve
%                     ***********
%                     max_of_count    - number of inner iterations
%                     max_of_bigcount - number of outer iterations
%                     fak_min         - information about 1/(speed of iteration)
%                     lambda          - damping parameter of first Newton step
%
% usage:              setuser_para_nlsolve('nameP1',valueP1,'nameP2',
%                                             valueP2,...)
%                     parameter name to be closed in ''
%                     one or more parameters may be given in 
%                     arbitrary sequence
%
% example:            setuser_para_nlsolve('max_of_count',200)
% -----------------------------------------------------------------------

function [] = setuser_para_nlsolve(varargin)
disp('***** setuser_para_nlsolve: creating user parameters for nlsolve...');
 
% ############################################################
% first get the default values for nlsolve
% ############################################################
  [max_of_count,max_of_bigcount,fak_min,lambda]...
   = init_para_nlsolve;

% interpreting the user input
% elements 1,3,...nargin-1 of varargin list are parameter names
% elements 2,4,...nargin   of varargin list are parameter values

% checking which user parameters are set and
% checking for valid parameter values

errhead='##########           ERROR IN USER SPECIFICATION !!!           ##########';
warhead='##########         WARNING FOR USER SPECIFICATION !!!          ##########';


if (nargin >=1)
   for i=1:2:nargin-1
       valid_par=0;
       given_parname=varargin{i};
   
% max_of_count: error if <=0; warning if > 1000
% ---------------------------------------------
      if (strcmp(varargin{i},'max_of_count')~=0)
         max_of_count=varargin{i+1};
         valid_par=1;
         if (max_of_count <= 0)
           disp(errhead);
           error('ERROR setting parameter: "max_of_count"  ==> Must be greater 0 !!!');
         end;
         if (max_of_count > 1000)
           disp(warhead);
           warning('WARNING for parameter: "max_of_count"  ==> greater 1000! really so big ???');
         end;
      end;
      
% max_of_bigcount: error if <=0; warning if > 5000
% ------------------------------------------------
      if (strcmp(varargin{i},'max_of_bigcount')~=0)
         max_of_bigcount=varargin{i+1};
         valid_par=1;
         if (max_of_bigcount <= 0)
           disp(errhead);
           error('ERROR setting parameter: "max_of_bigcount"  ==> Must be greater 0 !!!');
         end;
         if (max_of_bigcount > 5000)
           disp(warhead);
           warning('WARNING for parameter: "max_of_bigcount"  ==> greater 5000! really so big ???');
         end;
      end;
      
% fak_min: error if <=0; warning if > 2
% -------------------------------------
      if (strcmp(varargin{i},'fak_min')~=0)
         fak_min=varargin{i+1};
         valid_par=1;
         if (fak_min <= 0)
           disp(errhead);
           error('ERROR setting parameter: "fak_min"  ==> Must be greater 0 !!!');
         end;
         if (fak_min > 2)
           disp(warhead);
           warning('WARNING for parameter: "fak_min"  ==> greater 2! really so big ???');
         end;
      end;

% lambda: error if <=0; warning if > 2
% ------------------------------------
      if (strcmp(varargin{i},'lambda')~=0)
         lambda=varargin{i+1};
         valid_par=1;
         if (lambda <= 0)
           disp(errhead);
           error('ERROR setting parameter: "lambda"  ==> Must be greater 0 !!!');
         end;
         if (lambda > 2)
           disp(warhead);
           warning('WARNING for parameter: "lambda"  ==> greater 2! really so big ???');
         end;
      end;
      
% ERROR: an invalid parameter name was given
% ------------------------------------------
if (valid_par == 0)
    disp(errhead);
    fprintf('invalid parameter name:  %s \n',given_parname);
    error('ERROR in parameters for nlsolve! Check it and run again!   ');
end;

end; % for-loop
end;


% save actual user definitions
save parameter_nlsolve max_of_count max_of_bigcount fak_min lambda ;

% restore default version if routine was called with empty list
if (nargin==0)
  [max_of_count,max_of_bigcount,fak_min,lambda]...
   = init_para_nlsolve(1);
end;
