MODULE initializat  ! Initialization of parameters and x for DDG-bundle 

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

! Parameters 
  INTEGER, PARAMETER :: &
    n      =    1000, &     ! Number of the variables.
    mcu    =    3           ! Upper limit for maximum number of stored corrections, mcu >=1. 


  REAL, PARAMETER :: &
    time = 1800.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 :: &
    tolf  = 1.0E-5_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. 
                            !   - If tolf2 = 0 the default value 1.0E+4 will be used. 
    tolb  =    0.0_prec, &  ! Tolerance for the function value (default = -large).
    tolg  = 1.0E-4_prec, &  ! Tolerance for the first termination criterion (default = 1.0E-6).
    told  =    1.0E+10_prec, & ! Tolerance for the diagonal elements (default = 1.0E+10).
    eta   =    0.0_prec, &  ! Distance measure parameter, eta >= 0. 
                            !   - If eta < 0  the default value 0.5 will be used. 
    epsl  =    0.0_prec, &  ! Line search parameter, 0 < epsl < 0.25 (default = 1.0E-4).
    xmax  =    1.50_prec    ! Maximum stepsize, 1 < xmax (default = 1.5, however, a larger 
                            ! value, e.g. xmax = 1000, is recommend for convex problems).


! 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
    iscale =      4         ! Diagonal matrix updating:
                            !     0  - Scaling at every iteration with stu/utu 
                            !     1  - Scaling at every iteration with sts/stu.
                            !     2  - Interval scaling with stu/utu.
                            !     3  - Interval scaling with sts/stu.
                            !     4  - Herskovits's diagonal update. 
                            !     5  - Complement of Herskovits's diagonal update.
                            !     6  - Diagonal matrix is I.      


  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 ! convex problems
       eta = 0.0_prec
       xmax = 1000.0_prec
!       xmax = 1.0_prec*n
    ELSE ! nonconvex problems
       eta = 0.250_prec 
       xmax = 10000.0_prec
!        xmax = 10.0_prec*n
    END IF

  END SUBROUTINE init_par

END MODULE initializat
