MODULE initializat  ! Initialization of parameters and x for SMDB.

  USE r_precision, ONLY : prec  ! Precision for reals.
  IMPLICIT NONE

! Parameters 
  INTEGER, PARAMETER :: &
    n      = 1000, &        ! Number of the variables.
    mcu    =    7, &        ! Maximum number of stored corrections, mcu >=1.
    mnma   =   10, &        ! Maximum number of function values used in nonmonotone line search.
    inma   =    0, &        ! Selection of line search method:
                            !   inma = 0, Armijo line search,
                            !   inma = 1, nonmonotone Armijo line search.
    maxnin =    2           ! Maximum number of interpolations in Armijo line search, maxnin >= 0.
                            ! The value maxint = 2 is recommented with inma=0 and maxint=20 with inma=1.

  REAL, PARAMETER :: &
    time = 7200.0           ! Maximum CPU-time in seconds. If time <= 0.0 the maximum time 
                            ! is ignored. REAL argument.

! Real parameters (if parameter value <= 0.0 the default value of the parameter will be used).
  REAL(KIND=prec), SAVE :: &
    tolb  =    0.0_prec, &  ! Tolerance for the function value (default = -large).
    tolf  = 1.0E-8_prec, &  ! Tolerance for change of function values (default = 1.0E-8).
    tolf2 =    0.0_prec, &  ! Second tolerance for change of function values.
                            !   - If tolf2 < 0 the the parameter and the corresponding termination 
                            !   criterion will be ignored (recommended with inma=1).
                            !   - If tolf2 = 0 the default value 1.0E+4 will be used. 
    tolg  = 1.0E-5_prec, &  ! Tolerance for the termination criterion (default = 1.0E-6).
    eta   =    0.0_prec, &  ! Distance measure parameter, eta >= 0. 
                            !   - If eta < 0  the default value 0.001 will be used.
    epsl  =    0.0_prec, &  ! Line search parameter, 0 < epsl < 0.25 (default = 1.0E-4).
    told  =    1.0_prec, &  ! Upper bound for the diagonal elements (default = 1.0).
    mintold  = 1.0E-10_prec, &  ! Lower bound for the diagonal elements (default = 1.0E-10).
    sigma = 1.0E-10_prec    ! -sigma is the smallest value that is considered as positive
                            ! linearization error (default = 0).

! Integer parameters (if value <= 0 the default value of the parameter will be used).
  INTEGER, SAVE :: &
    mit    = 500000, &      ! Maximun number of iterations (default = 10000).
    mfe    = 50000000, &    ! Maximun number of function evaluations (default = n*mit).
    mtesf  =      0, &      ! Maximum number of iterations with changes of
                            ! function values smaller than tolf (default = 10).
    iprint =      1         ! Printout specification:
                            !    -1  - No printout.
                            !     0  - Only the error messages.
                            !     1  - The final values of the objective function 
                            !          (default used if iprint < -1).
                            !     2  - The final values of the objective function and the 
                            !          most serious warning messages.
                            !     3  - The whole final solution. 
                            !     4  - At each iteration values of the objective function.
                            !     5  - At each iteration the whole solution


  REAL(KIND=prec), DIMENSION(n), SAVE :: x  ! Vector of variables (initialize x in subroutine init_x)


CONTAINS

  SUBROUTINE init_x()  ! User supplied subroutine for initialization of vector x(n).

    USE problem_data, ONLY : next      ! Number of the problem (needed with large-scale test  
                                       ! problems in testps).
    USE large_problems, ONLY : startx  ! Initialization of x for large-scale nonsmooth test problems.
    IMPLICIT NONE

    CALL startx(n,x)
    IF (next == -1) STOP 'Error in the problem number next.'
  
  END SUBROUTINE init_x


  SUBROUTINE init_par()  ! User supplied subroutine for further initialization of parameters (when needed).
                         ! This subroutine may be usefull if you want to run some test problems in loop.
                         ! May be left empty.
 
    USE problem_data, ONLY : next  ! Number of the problem (needed with large scale test problems in tnsunc).
    IMPLICIT NONE

    IF (next <= 5) THEN

       eta = 0.0_prec
    ELSE
       eta = 0.00010_prec
    END IF

  END SUBROUTINE init_par

END MODULE initializat
