!****************************************************************************************************** !> ! A basic multidimensional nonlinear equation solver, using a Newton-Raphson type direct method. ! !### Features ! * Works with square, under-determined, or over-determined systems. ! * Uses LAPACK routines (`dgesv` or `dgels`) to solve the linear system. ! * Has a Broyden update option. ! * Has various line search options. ! !### References ! * https://en.wikipedia.org/wiki/Backtracking_line_search ! * http://projecteuclid.org/download/pdf_1/euclid.pjm/1102995080 ! * http://help.agi.com/stk/index.htm#gator/eq-diffcorr.htm ! * http://gmat.sourceforge.net/doc/R2015a/html/DifferentialCorrector.html ! * https://openmdao.org/newdocs/versions/latest/features/building_blocks/solvers/bounds_enforce.html ! !### Author ! * Jacob Williams ! !### License ! * BSD-3 ! !@todo add an `istat` output to func and grad, for user stopping ! or to take a smaller stop (if istat>0 take a smaller step, if istat<0 abort) ! !@note The default real kind (`wp`) can be ! changed using optional preprocessor flags. ! This library was built with real kind: #ifdef REAL32 ! `real(kind=real32)` [4 bytes] #elif REAL64 ! `real(kind=real64)` [8 bytes] #elif REAL128 ! `real(kind=real128)` [16 bytes] #else ! `real(kind=real64)` [8 bytes] #endif module nlesolver_module use iso_fortran_env use fmin_module, only: fmin use lsqr_module, only: lsqr_solver_ez use lusol_ez_module, only: solve, lusol_settings use lsmrModule, only: lsmr_ez use nlesolver_cholesky_module, only: cholesky_minnorm_solver, CHOLESKY_ERR_NOT_PD, CHOLESKY_ERR_MEMORY implicit none private #ifdef REAL32 integer,parameter,public :: nlesolver_rk = real32 !! real kind used by this module [4 bytes] #elif REAL64 integer,parameter,public :: nlesolver_rk = real64 !! real kind used by this module [8 bytes] #elif REAL128 integer,parameter,public :: nlesolver_rk = real128 !! real kind used by this module [16 bytes] #else integer,parameter,public :: nlesolver_rk = real64 !! real kind used by this module [8 bytes] #endif integer,parameter :: wp = nlesolver_rk !! local copy of `nlesolver_rk` with a shorter name real(wp),parameter :: zero = 0.0_wp real(wp),parameter :: one = 1.0_wp real(wp),parameter :: two = 2.0_wp real(wp),parameter :: eps = epsilon(one) !! machine \( \epsilon \) real(wp),parameter :: big = huge(one) ! options for sparsity_mode integer,parameter,public :: NLESOLVER_SPARSITY_DENSE = 1 !! [[nlesolver_type:sparsity_mode]] : assume dense (use dense solver). integer,parameter,public :: NLESOLVER_SPARSITY_LSQR = 2 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use LSQR sparse solver). integer,parameter,public :: NLESOLVER_SPARSITY_LUSOL = 3 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use LUSOL sparse solver). integer,parameter,public :: NLESOLVER_SPARSITY_LSMR = 4 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use LSMR sparse solver). integer,parameter,public :: NLESOLVER_SPARSITY_CUSTOM_SPARSE = 5 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use a user provided sparse solver). integer,parameter,public :: NLESOLVER_SPARSITY_CRAIG = 6 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use the built-in CRAIG !! minimum-norm sparse solver). integer,parameter,public :: NLESOLVER_SPARSITY_CHOLESKY = 7 !! [[nlesolver_type:sparsity_mode]] : assume sparse (use the built-in sparse direct !! minimum-norm solver: Cholesky factorization of `A*A^T`). !integer,parameter,public :: NLESOLVER_SPARSITY_CUSTOM_DENSE = 8 !! [[nlesolver_type:sparsity_mode]] : assume dense (use a user provided dense solver). [not available] ! if add will have to update code below where sparsity_modes /= 1 is assumed sparse ! bounds mode options: integer,parameter,public :: NLESOLVER_IGNORE_BOUNDS = 0 !! [[nlesolver_type:bounds_mode]] : ignore bounds integer,parameter,public :: NLESOLVER_SCALAR_BOUNDS = 1 !! [[nlesolver_type:bounds_mode]] : scalar mode integer,parameter,public :: NLESOLVER_VECTOR_BOUNDS = 2 !! [[nlesolver_type:bounds_mode]] : vector mode integer,parameter,public :: NLESOLVER_WALL_BOUNDS = 3 !! [[nlesolver_type:bounds_mode]] : wall mode ! options to compute vector norm for function value: integer,parameter,public :: NLESOLVER_2_NORM = 1 !! [[nlesolver_type:norm_mode]] : 2-norm integer,parameter,public :: NLESOLVER_INF_NORM = 2 !! [[nlesolver_type:norm_mode]] : infinity-norm integer,parameter,public :: NLESOLVER_1_NORM = 3 !! [[nlesolver_type:norm_mode]] : 1-norm ! options for linesearch step mode: integer,parameter,public :: NLESOLVER_LINESEARCH_SIMPLE = 1 !! [[nlesolver_type:linesearch]] : use the specified `alpha` (0,1] integer,parameter,public :: NLESOLVER_LINESEARCH_BACKTRACKING = 2 !! [[nlesolver_type:linesearch]] : backtracking linesearch (between `alpha_min` and `alpha_max`) integer,parameter,public :: NLESOLVER_LINESEARCH_EXACT = 3 !! [[nlesolver_type:linesearch]] : exact linesearch (between `alpha_min` and `alpha_max`) integer,parameter,public :: NLESOLVER_LINESEARCH_FIXEDPOINT = 4 !! [[nlesolver_type:linesearch]] : evaluate function at specified fixed points (between `alpha_min` and `alpha_max`) !********************************************************* type,public :: nlesolver_type !! Nonlinear equations solver class. private integer :: n = 0 !! number of opt vars integer :: m = 0 !! number of constraints integer :: max_iter = 100 !! maximum number of iterations real(wp) :: tol = 1.0e-6_wp !! convergence tolerance for function values real(wp) :: alpha = 1.0_wp !! step length (when specified constant) real(wp) :: alpha_min = 0.1_wp !! minimum step length (when allowed to vary) real(wp) :: alpha_max = 1.0_wp !! maximum step length (when allowed to vary) real(wp) :: tolx = 1.0e-8_wp !! convergence tolerance for `x` real(wp) :: c = 1.0e-4_wp !! backtracking linesearch parameter (0,1): !! the sufficient decrease (Armijo) constant. !! must be `< 0.5` to allow full Newton steps. real(wp) :: tau = 0.5_wp !! backtracking linesearch parameter (0,1) real(wp) :: fmin_tol = 1.0e-5_wp !! tolerance for "exact" linesearch integer :: n_intervals = 2 !! number of intervals for fixed point linesearch logical :: use_broyden = .false. !! if true, a Broyden update is used !! rather than computing the Jacobian !! at every step. The `grad` function is !! only called for the initial evaluation. integer :: broyden_update_n = 4 !! if this value is `>0`, the Broyden update !! is computed at most this many times before !! the full Jacobian is recomputed. integer :: n_uphill_max = 5 !! maximum number of consecutive steps !! to allow where the value of `f` increases logical :: verbose = .false. !! verbose output printing integer :: iunit = output_unit !! output unit for printing (assumed to be open). character(len=:),allocatable :: message !! latest status message integer :: istat = -999 !! latest status message integer :: norm_mode = NLESOLVER_2_NORM !! how to compute the norm of the function vector: !! !! * 1 = 2-norm (default) !! * 2 = infinity-norm !! * 3 = 1-norm integer :: bounds_mode = NLESOLVER_IGNORE_BOUNDS !! how to handle the `x` variable bounds: !! !! * 0 = ignore bounds (default). !! * 1 = scalar mode : adjust the step vector (`xnew-x`) by moving each individual scalar component !! of `xnew` to be within the bounds. Note that this can change the direction and magnitude of !! the search vector. !! * 2 = vector mode: backtrack the search vector so that no bounds are violated. note that !! this does not change the direction of the vector, only the magnitude. !! * 3 = wall mode: adjust the step vector (`xnew-x`) by moving each individual scalar component !! of `xnew` to be within the bounds. And then holding the ones changed fixed during the line search. !! !! Note: if `bounds_mode>0`, then the initial `x` vector is also adjusted !! to be within the bounds before the first step, if necessary. This is just done !! by adjusting each scalar component of `x` to be within bounds. real(wp),dimension(:),allocatable :: xlow !! lower bounds for `x` (size is `n`). only used if `bounds_mode>0`. real(wp),dimension(:),allocatable :: xupp !! upper bounds for `x` (size is `n`). only used if `bounds_mode>0`. procedure(func_func),pointer :: func => null() !! user-supplied routine to compute the function procedure(export_func),pointer :: export_iteration => null() !! user-supplied routine to export iterations procedure(wait_func),pointer :: user_input_check => null() !! user-supplied routine to enable user to stop iterations procedure(linesearch_func),pointer :: linesearch => null() !! line search method (determined by `step_mode` user input in [[nlesolver_type:initialize]]) ! sparsity options: integer :: sparsity_mode = NLESOLVER_SPARSITY_DENSE !! sparsity mode: !! !! * 1 - assume dense (use dense solver). !! * 2 - assume sparse (use LSQR sparse solver). !! * 3 - assume sparse (use LUSOL sparse solver). !! * 4 - assume sparse (use LSMR sparse solver). !! * 5 - assume sparse (use a user provided sparse solver). !! * 6 - assume sparse (use the built-in CRAIG minimum-norm solver). !! * 7 - assume sparse (use the built-in sparse direct minimum-norm !! solver: Cholesky factorization of `A*A^T`). integer :: n_nonzeros = -1 !! number of nonzero Jacobian elements (used for `sparsity_mode > 1`) integer,dimension(:),allocatable :: irow !! sparsity pattern nonzero elements row indices. integer,dimension(:),allocatable :: icol !! sparsity pattern nonzero elements column indices ! LSQR parameters: real(wp) :: atol = zero !! relative error in definition of `A` real(wp) :: btol = zero !! relative error in definition of `b` real(wp) :: conlim = zero !! An upper limit on `cond(Abar)`, the apparent !! condition number of the matrix `Abar`. integer :: itnlim = 100 !! max iterations integer :: nout = 0 !! output unit for printing real(wp) :: damp = zero !! damp parameter for LSQR integer :: localSize = 0 !! LSMR: Number of vectors for local reorthogonalization: !! !! * 0 No reorthogonalization is performed. !! * >0 This many n-vectors "v" (the most recent ones) !! are saved for reorthogonalizing the next v. !! !! localSize need not be more than `min(m,n)`. !! At most `min(m,n)` vectors will be allocated. ! sparse Cholesky (minimum-norm) solver: type(cholesky_minnorm_solver) :: cholesky !! the solver (the symbolic analysis is done once !! for the sparsity pattern, and reused for each iteration) integer :: cholesky_refine = 2 !! max number of iterative refinement steps for the Cholesky solver ! LUSOL parameters: integer :: lusol_method = 0 !! * 0 => TPP: Threshold Partial Pivoting. !! * 1 => TRP: Threshold Rook Pivoting. !! * 2 => TCP: Threshold Complete Pivoting. ! dense version: procedure(grad_func),pointer :: grad => null() !! user-supplied routine to compute the gradient of the function (dense version) ! sparse version: procedure(grad_func_sparse),pointer :: grad_sparse => null() !! user-supplied routine to compute the gradient of the function (sparse version) ! custom sparse solver: procedure(sparse_solver_func),pointer :: custom_solver_sparse => null() !! user-supplied sparse linear solver routine (used for `sparsity_mode=5`) procedure(norm_func),pointer :: norm => null() !! function for computing the norm of the `f` vector contains private procedure,public :: initialize => initialize_nlesolver_variables procedure,public :: solve => nlesolver_solver procedure,public :: destroy => destroy_nlesolver_variables procedure,public :: status => get_status procedure :: set_status procedure :: adjust_x_for_bounds procedure :: adjust_search_direction procedure :: compute_next_step end type nlesolver_type !********************************************************* abstract interface subroutine sparse_solver_func(me,n_cols,n_rows,n_nonzero,irow,icol,a,b,x,istat) !! for a custom user-provided linear solver (sparse version) !! solve `Ax = b` for `x`, given `A` and `b`. import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me integer,intent(in) :: n_cols !! `n`: number of columns in A. integer,intent(in) :: n_rows !! `m`: number of rows in A. integer,intent(in) :: n_nonzero !! number of nonzero elements of A. integer,dimension(n_nonzero),intent(in) :: irow, icol !! sparsity pattern (size is `n_nonzero`) real(wp),dimension(n_nonzero),intent(in) :: a !! matrix elements (size is `n_nonzero`) real(wp),dimension(n_rows),intent(in) :: b !! right hand side (size is `m`) real(wp),dimension(n_cols),intent(out) :: x !! solution (size is `n`) integer,intent(out) :: istat !! status code (=0 for success) end subroutine sparse_solver_func subroutine func_func(me,x,f) !! compute the function import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(:),intent(in) :: x real(wp),dimension(:),intent(out) :: f end subroutine func_func subroutine grad_func(me,x,g) !! compute the gradient of the function (Jacobian). Dense version. import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(:),intent(in) :: x real(wp),dimension(:,:),intent(out) :: g end subroutine grad_func subroutine grad_func_sparse(me,x,g) !! compute the gradient of the function (Jacobian). Sparse version. import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(:),intent(in) :: x real(wp),dimension(:),intent(out) :: g !! sparse jacobian. length is `n_nonzeros` end subroutine grad_func_sparse subroutine export_func(me,x,f,iter) !! export an iteration: import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(:),intent(in) :: x real(wp),dimension(:),intent(in) :: f integer,intent(in) :: iter !! iteration number end subroutine export_func subroutine wait_func(me,user_stop) !! enable a user-triggered stop of the iterations: import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me logical,intent(out) :: user_stop end subroutine wait_func subroutine linesearch_func(me,xold,p,x,f,fvec,fjac,fjac_sparse) !! line search method. Note that not all inputs/outputs are !! used by all methods. import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! previous value of `x` real(wp),dimension(me%n),intent(in) :: p !! search direction real(wp),dimension(me%n),intent(out) :: x !! new `x` real(wp),intent(inout) :: f !! * input: current magnitude of `fvec`, !! * output: new value of `f` real(wp),dimension(me%m),intent(inout) :: fvec !! * input: current function vector, !! * output: new function vector real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense] real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse] end subroutine linesearch_func pure function norm_func(me, fvec) result(f) !! function vector norm. import :: wp,nlesolver_type implicit none class(nlesolver_type),intent(in) :: me real(wp),dimension(me%m),intent(in) :: fvec !! the function vector real(wp) :: f !! norm of the vector end function norm_func end interface contains !******************************************************************************************************* !***************************************************************************************** !> ! Set status flag and message. subroutine set_status(me,istat,string,i,r) implicit none class(nlesolver_type),intent(inout) :: me integer,intent(in) :: istat !! status code character(len=*),intent(in) :: string !! status message integer,intent(in),optional :: i !! an integer value to append real(wp),intent(in),optional :: r !! a real value to append character(len=:),allocatable :: message !! the full message to log integer :: iostat !! write `iostat` code message = trim(string) if (present(i)) message = message//' '//int2str(i) if (present(r)) message = message//' '//real2str(r) if (me%verbose) write(me%iunit,'(A)',iostat=iostat) message ! store in the class: me%istat = istat me%message = message end subroutine set_status !***************************************************************************************** !***************************************************************************************** !> ! Convert an integer to a string. Works for up to 256 digits. function int2str(i) result(s) integer, intent(in) :: i !! integer to convert character(len=:),allocatable :: s !! string result character(len=256) :: tmp !! temp string integer :: iostat !! write `iostat` code write(tmp,fmt=*,iostat=iostat) i if (iostat/=0) then s = '****' else s = trim(adjustl(tmp)) end if end function int2str !***************************************************************************************** !***************************************************************************************** !> ! Convert a real to a string. Works for up to 256 digits. function real2str(r) result(s) real(wp), intent(in) :: r !! real to convert character(len=:),allocatable :: s !! string result character(len=256) :: tmp !! temp string integer :: iostat !! write `iostat` code write(tmp,fmt=*,iostat=iostat) r if (iostat/=0) then s = '****' else s = trim(adjustl(tmp)) end if end function real2str !***************************************************************************************** !***************************************************************************************** !> ! Return the status code and message from the [[nlesolver_type]] class. ! !### Status codes ! ! * -1 -- Error: Invalid alpha ! * -2 -- Error: Invalid alpha_min ! * -3 -- Error: Invalid alpha_max ! * -4 -- Error: Alpha_min must be < alpha_max ! * -5 -- Error: Invalid step_mode ! * -6 -- Error solving linear system ! * -7 -- Error: More than 5 steps in the uphill direction ! * -8 -- Error: Divide by zero when computing Broyden update ! * -9 -- Error: Out of memory ! * -10 -- Error: function routine is not associated ! * -11 -- Error: gradient routine is not associated ! * -12 -- Error: backtracking linesearch c must be in range (0, 1) ! * -13 -- Error: backtracking linesearch tau must be in range (0, 1) ! * -14 -- Error: must specify grad_sparse, irow, and icol for sparsity_mode > 1 ! * -15 -- Error: irow and icol must be the same length ! * -16 -- Error: xlow > xupp ! * -17 -- Error adjusting line search direction for bounds ! * -18 -- Error: invalid norm_mode ! * -19 -- Error: xlow and xupp must be specified for bounds_mode > 0 ! * -20 -- Error: invalid bounds_mode ! * -999 -- Error: class has not been initialized ! * 0 -- Class successfully initialized in [[nlesolver_type:initialize]] ! * 1 -- Required accuracy achieved ! * 2 -- Solution cannot be improved ! * 3 -- Maximum number of iterations reached ! * 4 -- Stopped by the user subroutine get_status(me, istat, message) implicit none class(nlesolver_type),intent(inout) :: me integer,intent(out),optional :: istat !! Integer status code. character(len=:),allocatable,intent(out),optional :: message !! Text status message if (present(istat)) istat = me%istat if (present(message)) then if (allocated(me%message)) then message = trim(me%message) else message = 'Error: class has not been initialized' end if end if end subroutine get_status !***************************************************************************************** !***************************************************************************************** !> ! Constructor for the class. subroutine initialize_nlesolver_variables(me,& n,m,max_iter,tol,alpha,alpha_min,alpha_max,tolx,fmin_tol,& backtrack_c,backtrack_tau,& use_broyden,broyden_update_n,step_mode,& func,grad,grad_sparse,& export_iteration,user_input_check,& verbose,iunit,n_uphill_max,n_intervals,& sparsity_mode,irow,icol,& atol,btol,conlim,damp,itnlim,nout,& lusol_method,localSize,custom_solver_sparse,& bounds_mode,xlow,xupp,norm_mode,cholesky_refine) implicit none class(nlesolver_type),intent(inout) :: me integer,intent(in) :: n !! number of optimization variables integer,intent(in) :: m !! number of constraints integer,intent(in) :: max_iter !! maximum number of iterations real(wp),intent(in) :: tol !! function convergence tolerance procedure(func_func) :: func !! computes the function vector procedure(grad_func),optional :: grad !! computes the jacobian [required for dense mode: `sparsity_mode=1`] procedure(grad_func_sparse),optional :: grad_sparse !! computes the jacobian [required for sparse mode: `sparsity_mode>1`] integer,intent(in),optional :: step_mode !! step mode: !! !! * 1 = use the specified `alpha` (0,1] !! * 2 = backtracking linesearch (between `alpha_min` and `alpha_max`) !! * 3 = exact linesearch (between `alpha_min` and `alpha_max`) !! * 4 = evaluate function at specified fixed points (between `alpha_min` and `alpha_max`) real(wp),intent(in),optional :: alpha !! constant step length for `step_mode=1` (0,1] real(wp),intent(in),optional :: alpha_min !! minimum step length (0,1] real(wp),intent(in),optional :: alpha_max !! maximum step length (0,1] real(wp),intent(in),optional :: tolx !! convergence tolerance for changes in `x` real(wp),intent(in),optional :: fmin_tol !! convergence tolerance for [[fmin]] (used when `step_mode=3`) real(wp),intent(in),optional :: backtrack_c !! backtracking linesearch parameter (0,1): !! the sufficient decrease (Armijo) constant !! (default is `1e-4`). Must be `< 0.5` to !! allow full Newton steps to be accepted. real(wp),intent(in),optional :: backtrack_tau !! backtracking linesearch parameter (0,1) logical,intent(in),optional :: use_broyden !! use a Broyden update (default is False) integer,intent(in),optional :: broyden_update_n !! For Broyden mode, update the full Jacobian !! at most every this many iterations (must be >1) !! If <=1 then Jacobian is only computed on the !! first iteration. procedure(export_func),optional :: export_iteration !! function to export each iteration procedure(wait_func),optional :: user_input_check !! check for user input (to stop solver if necessary) logical,intent(in),optional :: verbose !! for verbose status printing integer,intent(in),optional :: iunit !! unit for verbose printing (assumed to be open). !! by default this is `output_unit`. integer,intent(in),optional :: n_uphill_max !! maximum number of consecutive steps !! to allow where the value of `f` increases integer,intent(in),optional :: n_intervals !! number of intervals for fixed point linesearch integer,intent(in),optional :: sparsity_mode !! sparsity mode: !! !! * 1 - assume dense (use dense solver) !! Must specify `grad` for this mode. !! * 2 - assume sparse (use LSQR sparse solver). !! * 3 - assume sparse (use LUSOL sparse solver). !! * 4 - assume sparse (use LSMR sparse solver). !! * 5 - assume sparse (use the user provided sparse !! solver `custom_solver_sparse`). !! * 6 - assume sparse (use the built-in CRAIG !! minimum-norm solver. Uses `atol`, `btol`, !! `damp`, `itnlim`, and `nout`). !! * 7 - assume sparse (use the built-in sparse direct !! minimum-norm solver: Cholesky factorization of `A*A^T`. !! Uses `damp` and `cholesky_refine`). !! Must also specify `grad_sparse` and the `irow` and `icol` !! sparsity pattern for `mode>1`. integer,dimension(:),intent(in),optional :: irow !! sparsity pattern nonzero elements row indices. !! must be specified with `icol` and be the same length (`n_nonzeros`). integer,dimension(:),intent(in),optional :: icol !! sparsity pattern nonzero elements column indices !! must be specified with `icol` and be the same length (`n_nonzeros`). real(wp),intent(in),optional :: atol !! `LSQR`: relative error in definition of `A` real(wp),intent(in),optional :: btol !! `LSQR`: relative error in definition of `b` real(wp),intent(in),optional :: conlim !! `LSQR`: An upper limit on `cond(Abar)`, the apparent !! condition number of the matrix `Abar`. real(wp),intent(in),optional :: damp !! `LSQR`: damp factor integer,intent(in),optional :: itnlim !! `LSQR`: max iterations integer,intent(in),optional :: nout !! `LSQR`: output unit for printing integer,intent(in),optional :: lusol_method !! `LUSOL` method: !! !! * 0 => TPP: Threshold Partial Pivoting. !! * 1 => TRP: Threshold Rook Pivoting. !! * 2 => TCP: Threshold Complete Pivoting. integer,intent(in),optional :: localSize !! `LSMR`: Number of vectors for local reorthogonalization: !! !! * 0 No reorthogonalization is performed. !! * >0 This many n-vectors "v" (the most recent ones) !! are saved for reorthogonalizing the next v. !! !! localSize need not be more than `min(m,n)`. !! At most `min(m,n)` vectors will be allocated. procedure(sparse_solver_func),optional :: custom_solver_sparse !! for `sparsity_mode=5`, this is the !! user-provided linear solver. integer,intent(in),optional :: cholesky_refine !! for `sparsity_mode=7`: maximum number of iterative !! refinement steps (default is 2). Refinement stops early !! if the residual does not decrease. integer,intent(in),optional :: bounds_mode !! how to handle the `x` variable bounds: !! !! * 0 = ignore bounds !! * 1 = scalar mode !! * 2 = vector mode !! * 3 = wall mode !! !! See [[nlesolver_type:bounds_mode]] for full descriptions. real(wp),dimension(n),intent(in),optional :: xlow !! lower bounds for `x` (size is `n`). only used if `bounds_mode>0` and !! both `xlow` and `xupp` are specified. real(wp),dimension(n),intent(in),optional :: xupp !! upper bounds for `x` (size is `n`). only used if `bounds_mode>0` and !! both `xlow` and `xupp` are specified. integer,intent(in),optional :: norm_mode !! how to compute the norm of the function vector: !! !! * 1 = 2-norm (optional) !! * 2 = infinity-norm !! * 3 = 1-norm !! !! See [[nlesolver_type:norm_mode]] for full descriptions. logical :: status_ok !! true if there were no errors status_ok = .true. !required: me%n = abs(n) me%m = abs(m) me%max_iter = abs(max_iter) me%tol = abs(tol) me%func => func if (present(grad)) me%grad => grad if (present(grad_sparse)) me%grad_sparse => grad_sparse !optional: me%bounds_mode = NLESOLVER_IGNORE_BOUNDS ! default if (present(bounds_mode)) then select case (bounds_mode) case (NLESOLVER_IGNORE_BOUNDS) ! nothing to do case (NLESOLVER_SCALAR_BOUNDS, NLESOLVER_VECTOR_BOUNDS, NLESOLVER_WALL_BOUNDS) ! the bounds must be given (they are never silently ignored): if (.not. (present(xlow) .and. present(xupp))) then status_ok = .false. call me%set_status(istat = -19, string = 'Error: xlow and xupp must be specified for bounds_mode =', & i=bounds_mode) return end if if (any(xlow>xupp)) then ! check for consistency status_ok = .false. call me%set_status(istat = -16, string = 'Error: xlow > xupp') return end if me%bounds_mode = bounds_mode me%xupp = xupp me%xlow = xlow case default status_ok = .false. call me%set_status(istat = -20, string = 'Error: invalid bounds_mode:', i=bounds_mode) return end select end if if (present(norm_mode)) then select case (norm_mode) case(NLESOLVER_2_NORM ); me%norm => norm_2 case(NLESOLVER_INF_NORM ); me%norm => norm_inf case(NLESOLVER_1_NORM ); me%norm => norm_1 case default status_ok = .false. call me%set_status(istat = -18, string = 'Error: invalid norm_mode:',i=norm_mode) return end select else me%norm => norm_2 ! default end if if (present(step_mode)) then select case (step_mode) case(NLESOLVER_LINESEARCH_SIMPLE); me%linesearch => simple_step case(NLESOLVER_LINESEARCH_BACKTRACKING); me%linesearch => backtracking_linesearch case(NLESOLVER_LINESEARCH_EXACT); me%linesearch => exact_linesearch case(NLESOLVER_LINESEARCH_FIXEDPOINT); me%linesearch => fixed_point_linesearch case default status_ok = .false. call me%set_status(istat = -5, string = 'Error: invalid step_mode:',i=step_mode) return end select else me%linesearch => simple_step end if if (present(alpha)) me%alpha = abs(alpha) if (present(alpha_min)) me%alpha_min = abs(alpha_min) if (present(alpha_max)) me%alpha_max = abs(alpha_max) if (present(tolx)) me%tolx = abs(tolx) if (present(backtrack_c)) me%c = abs(backtrack_c) if (present(backtrack_tau)) me%tau = abs(backtrack_tau) if (present(use_broyden)) me%use_broyden = use_broyden if (present(broyden_update_n)) me%broyden_update_n = abs(broyden_update_n) if (present(verbose)) me%verbose = verbose if (present(iunit)) me%iunit = iunit if (present(n_uphill_max)) me%n_uphill_max = abs(n_uphill_max) if (present(n_intervals)) me%n_intervals = max(abs(n_intervals),1) if (present(fmin_tol)) me%fmin_tol = abs(fmin_tol) if (present(export_iteration)) me%export_iteration => export_iteration if (present(user_input_check)) me%user_input_check => user_input_check ! error checks: if (me%alpha<zero .or. me%alpha>one) then status_ok = .false. call me%set_status(istat = -1, string = 'Error: invalid alpha:',r=me%alpha) return end if if (me%alpha_min<zero .or. me%alpha_min>one) then status_ok = .false. call me%set_status(istat = -2, string = 'Error: invalid alpha_min:',r=me%alpha_min) return end if if (me%alpha_max<zero .or. me%alpha_max>one) then status_ok = .false. call me%set_status(istat = -3, string = 'Error: invalid alpha_max:',r=me%alpha_max) return end if if (me%alpha_max<=me%alpha_min) then status_ok = .false. call me%set_status(istat = -4, string = 'Error: alpha_min must be < alpha_max') return end if if (me%c<zero .or. me%c>one) then status_ok = .false. call me%set_status(istat = -12, string = 'Error: backtracking linesearch c must be in range (0, 1):',r=me%c) return end if if (me%tau<zero .or. me%tau>one) then status_ok = .false. call me%set_status(istat = -13, string = 'Error: backtracking linesearch tau must be in range (0, 1):',r=me%tau) return end if ! initialize: call me%cholesky%destroy() ! the sparsity pattern may have changed me%n_nonzeros = -1 ! not used if (allocated(me%irow)) deallocate(me%irow) if (allocated(me%icol)) deallocate(me%icol) if (present(custom_solver_sparse)) me%custom_solver_sparse => custom_solver_sparse if (present(sparsity_mode)) then me%sparsity_mode = sparsity_mode if (sparsity_mode>NLESOLVER_SPARSITY_DENSE) then ! sparse solver method if (present(irow) .and. present(icol) .and. present(grad_sparse)) then if (size(irow) == size(icol)) then me%n_nonzeros = size(irow) me%irow = irow me%icol = icol else call me%set_status(istat = -15, string = 'Error: irow and icol must be the same length') return end if else call me%set_status(istat = -14, string = 'Error: must specify grad_sparse, irow, and icol for sparsity_mode > 1') return end if ! LSQR optional inputs: if (present(atol)) me%atol = atol if (present(btol)) me%btol = btol if (present(conlim)) me%conlim = conlim if (present(damp)) me%damp = damp if (present(itnlim)) me%itnlim = itnlim if (present(nout)) me%nout = nout ! LUSOL method if (present(lusol_method)) me%lusol_method = lusol_method ! LSMR optional inputs: if (present(localSize)) me%localSize = localSize ! Cholesky optional inputs: if (present(cholesky_refine)) me%cholesky_refine = max(0, cholesky_refine) end if if (sparsity_mode==NLESOLVER_SPARSITY_CUSTOM_SPARSE) then if (.not. associated(me%custom_solver_sparse)) then call me%set_status(istat = -16, string = 'Error: must specify custom_solver_sparse for sparsity_mode = 5') return end if end if end if if (status_ok) then call me%set_status(istat = 0, string = 'Class successfully initialized') end if end subroutine initialize_nlesolver_variables !***************************************************************************************** !***************************************************************************************** !> ! Main solver. subroutine nlesolver_solver(me,x) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(:),intent(inout) :: x real(wp),dimension(:) ,allocatable :: fvec !! function vector real(wp),dimension(:,:),allocatable :: fjac !! jacobian matrix [dense] real(wp),dimension(:), allocatable :: fjac_sparse !! jacobian matrix [sparse] real(wp),dimension(:), allocatable :: prev_fjac_sparse !! previous jacobian matrix [sparse] real(wp),dimension(:) ,allocatable :: rhs !! linear system right-hand side real(wp),dimension(:) ,allocatable :: p !! search direction real(wp),dimension(:) ,allocatable :: xold !! previous value of `x` real(wp),dimension(:) ,allocatable :: prev_fvec !! previous function vector real(wp),dimension(:,:),allocatable :: prev_fjac !! previous jacobian matrix real(wp),dimension(:,:),allocatable :: delf !! used for Broyden (rank 2 for `matmul`) real(wp),dimension(:,:),allocatable :: delx !! used for Broyden (rank 2 for `matmul`) logical :: user_stop !! user stop button flag integer :: info !! status flag from the [[linear_solver]] integer :: iter !! iteration counter real(wp) :: f !! magnitude of `fvec` real(wp) :: fold !! previous value of `f` integer :: n_uphill !! number of steps taken in the "uphill" direction !! (where `f` is increasing) real(wp) :: delxmag2 !! used for Broyden logical :: recompute_jac !! if using Broyden, and we want to call the user !! jacobian routine instead integer :: broyden_counter !! number of times the broyden update has been used integer :: alloc_stat !! allocation status flag type(lsqr_solver_ez) :: sparse_solver !! sparse LSQR solver class type(lusol_settings) :: lusol_options integer :: i !! counter integer :: k, k1, k2 !! counters for sparse indexing integer,dimension(:),allocatable :: row_ptr !! sparse Broyden: nonzeros of row `i` are !! `row_nz(row_ptr(i):row_ptr(i+1)-1)` (size `m+1`) integer,dimension(:),allocatable :: row_nz !! sparse Broyden: indices of the nonzero elements, !! grouped by row (size `n_nonzeros`) real(wp),dimension(:),allocatable :: dx !! sparse Broyden: `delx` elements for one row real(wp) :: normA, condA, normr, normAr, normx !! for LSMR integer :: itn !! for LSMR if (me%istat<0) return ! class was not initialized properly if (.not. associated(me%func)) then call me%set_status(istat = -10, string = 'Error: function routine is not associated') return end if if (me%sparsity_mode==NLESOLVER_SPARSITY_DENSE .and. .not. associated(me%grad)) then call me%set_status(istat = -11, string = 'Error: gradient routine is not associated') return end if if (me%sparsity_mode>NLESOLVER_SPARSITY_DENSE .and. .not. associated(me%grad_sparse)) then call me%set_status(istat = -11, string = 'Error: gradient routine is not associated') return end if ! initialize: iter = 0 n_uphill = 0 recompute_jac = .false. broyden_counter = 0 ! allocate the arrays: alloc_stat = 0 if (alloc_stat==0) allocate(fvec (me%m) , stat=alloc_stat) if (me%sparsity_mode==NLESOLVER_SPARSITY_DENSE) then ! dense if (alloc_stat==0) allocate(fjac (me%m,me%n) , stat=alloc_stat) else ! sparse if (alloc_stat==0) allocate(fjac_sparse (me%n_nonzeros) , stat=alloc_stat) if (me%use_broyden .and. alloc_stat==0) allocate(prev_fjac_sparse(me%n_nonzeros) , stat=alloc_stat) end if if (alloc_stat==0) allocate(rhs (me%m) , stat=alloc_stat) if (alloc_stat==0) allocate(p (me%n) , stat=alloc_stat) if (alloc_stat==0) allocate(xold (me%n) , stat=alloc_stat) if (me%use_broyden) then ! only need these for broyden: if (alloc_stat==0) allocate(prev_fvec(me%m) , stat=alloc_stat) if (me%sparsity_mode==NLESOLVER_SPARSITY_DENSE) then if (alloc_stat==0) allocate(prev_fjac(me%m,me%n) , stat=alloc_stat) else ! group the sparse jacobian elements by row (counting sort, O(n_nonzeros)), ! so the row-by-row Broyden update never has to search the whole sparsity pattern if (alloc_stat==0) allocate(row_ptr(me%m+1), stat=alloc_stat) if (alloc_stat==0) allocate(row_nz(me%n_nonzeros), stat=alloc_stat) if (alloc_stat==0) then row_ptr = 0 do k = 1, me%n_nonzeros row_ptr(me%irow(k)+1) = row_ptr(me%irow(k)+1) + 1 end do row_ptr(1) = 1 do i = 1, me%m row_ptr(i+1) = row_ptr(i+1) + row_ptr(i) end do ! fill row_nz, using row_ptr(i) as the insertion point for row i: do k = 1, me%n_nonzeros row_nz(row_ptr(me%irow(k))) = k row_ptr(me%irow(k)) = row_ptr(me%irow(k)) + 1 end do ! restore row_ptr (each entry was advanced to the start of the next row): do i = me%m, 1, -1 row_ptr(i+1) = row_ptr(i) end do row_ptr(1) = 1 end if end if if (alloc_stat==0) allocate(delf (me%m,1) , stat=alloc_stat) if (alloc_stat==0) allocate(delx (me%n,1) , stat=alloc_stat) end if if (alloc_stat/=0) then call me%set_status(istat = -9, string = 'Error: Out of memory') return else me%istat = -998 me%message = 'Unknown error' end if ! evaluate the function: call me%adjust_x_for_bounds(x) ! if the guess is out of bounds it may also be adjusted first. call me%func(x,fvec) f = me%norm(fvec) ! check to see if initial guess is a root: if (f <= me%tol) then call me%set_status(istat = 1, string = 'Required accuracy achieved') else ! main iteration loop: do iter = 1,me%max_iter ! Export the current iteration: if (associated(me%export_iteration)) call me%export_iteration(x,fvec,iter) ! Check for user stop: if (associated(me%user_input_check)) then call me%user_input_check(user_stop) if (user_stop) then call me%set_status(istat = 4, string = 'Stopped by the user') exit end if end if if (me%use_broyden .and. .not. recompute_jac) then if (iter==1) then ! always compute Jacobian on the first iteration !call me%grad(x,fjac) select case (me%sparsity_mode) case (NLESOLVER_SPARSITY_DENSE) call me%grad(x,fjac) case default ! sparse modes call me%grad_sparse(x,fjac_sparse) end select broyden_counter = 0 else ! and use Broyden update to estimate Jacobian ! for subsequent iterations. ! note: fvec was computed the last iteration delx(:,1) = x - xold delf(:,1) = fvec - prev_fvec if (me%sparsity_mode==NLESOLVER_SPARSITY_DENSE) then ! dense delxmag2 = dot_product(delx(:,1),delx(:,1)) if (delxmag2 < eps) then call me%set_status(istat = -8, & string = 'Error: Divide by zero when computing Broyden update') exit end if ! Jacobian estimate: ! This is the equation from wikipedia : https://en.wikipedia.org/wiki/Broyden%27s_method fjac = prev_fjac + matmul((delf-matmul(prev_fjac,delx)), transpose(delx)) / delxmag2 ! see also: eqn 4 in Schubert, which is different ... else ! using a sparse option ! Just a row-by-row version of the fjac equation above. ! see L.K. Schubert, 1970 fjac_sparse = prev_fjac_sparse do i = 1, me%m k1 = row_ptr(i) k2 = row_ptr(i+1) - 1 if (k2 < k1) cycle ! no non-zeros in this row associate (idx => row_nz(k1:k2)) ! the nonzero elements in this row dx = delx(me%icol(idx),1) ! nonzero x vec for this row delxmag2 = dot_product(dx,dx) ! only those x's for this row ! if none of the variables in this row changed, there is ! no new information, so this row of the jacobian is unchanged. if (delxmag2 < eps) cycle fjac_sparse(idx) = prev_fjac_sparse(idx) + & (delf(i,1) - dot_product(prev_fjac_sparse(idx),dx)) * dx / delxmag2 end associate end do end if broyden_counter = broyden_counter + 1 end if prev_fvec = fvec if (me%sparsity_mode==NLESOLVER_SPARSITY_DENSE) then prev_fjac = fjac else prev_fjac_sparse = fjac_sparse end if else ! compute the jacobian: select case (me%sparsity_mode) case (NLESOLVER_SPARSITY_DENSE) call me%grad(x,fjac) case default ! sparse call me%grad_sparse(x,fjac_sparse) end select recompute_jac = .false. ! for broyden broyden_counter = 0 end if xold = x fold = f ! compute the search direction p by solving linear system: rhs = -fvec ! RHS of the linear system select case (me%sparsity_mode) case (NLESOLVER_SPARSITY_DENSE) ! use dense solver call linear_solver(me%m,me%n,fjac,rhs,p,info) case (NLESOLVER_SPARSITY_LSQR) ! initialize the LSQR sparse solver call sparse_solver%initialize(me%m,me%n,fjac_sparse,me%irow,me%icol,& atol = me%atol, & btol = me%btol, & conlim = me%conlim, & itnlim = me%itnlim, & nout = me%nout) call sparse_solver%solve(rhs,me%damp,p,info) ! solve the linear system ! check convergence: select case (info) case(4) call me%set_status(istat = -1004, & string = 'LSQR Error: The system appears to be ill-conditioned. istop =', i=info) exit case(5) call me%set_status(istat = -1005, & string = 'LSQR Error: The iteration limit was reached. istop =', i=info) exit case default info = 0 end select case (NLESOLVER_SPARSITY_LUSOL) ! use lusol solver lusol_options%method = me%lusol_method call solve(me%n,me%m,me%n_nonzeros,me%irow,me%icol,fjac_sparse,rhs,p,info,& settings=lusol_options) case (NLESOLVER_SPARSITY_LSMR) ! use LSMR solver: !me%conlim = 1.0_wp/(100*epsilon(1.0_wp)) call lsmr_ez ( me%m, me%n, me%irow, me%icol, fjac_sparse, rhs, me%damp, & me%atol, me%btol, me%conlim, me%itnlim, me%localSize, me%nout, & p, info, itn, normA, condA, normr, normAr, normx ) ! check convergence: select case (info) case(4) call me%set_status(istat = -1004, & string = 'LSMR Error: The system appears to be ill-conditioned. istop =', i=info) exit case(5) call me%set_status(istat = -1005, & string = 'LSMR Error: The iteration limit was reached. istop =', i=info) exit case default info = 0 end select case (NLESOLVER_SPARSITY_CRAIG) ! use the built-in CRAIG minimum-norm solver: call craig_solver(me%m, me%n, me%n_nonzeros, me%irow, me%icol, fjac_sparse, rhs, & me%damp, me%atol, me%btol, me%itnlim, me%nout, p, info) ! check convergence: select case (info) case(2) call me%set_status(istat = -1007, & string = 'CRAIG Error: Breakdown (system appears to be incompatible; '//& 'try damp > 0). istop =', i=info) exit case(3) call me%set_status(istat = -1005, & string = 'CRAIG Error: The iteration limit was reached. istop =', i=info) exit case default info = 0 end select case (NLESOLVER_SPARSITY_CHOLESKY) ! use the built-in sparse direct minimum-norm solver. ! the symbolic analysis is only done once for the sparsity pattern: info = 0 if (.not. me%cholesky%is_analyzed()) then call me%cholesky%analyze(me%m, me%n, me%irow, me%icol, info) if (info == 0 .and. me%verbose) then write(me%iunit,'(A,I0,A,I0)') 'Cholesky analysis: bandwidth = ', me%cholesky%bandwidth(), & ', envelope size = ', me%cholesky%envelope_size() end if end if if (info == 0) call me%cholesky%factor(fjac_sparse, me%damp, info) if (info == 0) call me%cholesky%solve(rhs, p, info, n_refine = me%cholesky_refine) ! check for errors: select case (info) case(0) case(CHOLESKY_ERR_NOT_PD) call me%set_status(istat = -1008, & string = 'CHOLESKY Error: A*A^T is not positive definite '//& '(the Jacobian appears to be rank deficient; try damp > 0). info =', i=info) exit case(CHOLESKY_ERR_MEMORY) call me%set_status(istat = -1009, & string = 'CHOLESKY Error: Out of memory (envelope of A*A^T is too large). info =', i=info) exit case default call me%set_status(istat = -1010, string = 'CHOLESKY Error. info =', i=info) exit end select case (NLESOLVER_SPARSITY_CUSTOM_SPARSE) if (associated(me%custom_solver_sparse)) then ! a user-provided sparse solver: call me%custom_solver_sparse(me%n,me%m,me%n_nonzeros,me%irow,me%icol,fjac_sparse,rhs,p,info) else call me%set_status(istat = -1006, & string = 'Error: The custom_solver_sparse procedure has not been set.') exit end if case default error stop 'invalid sparsity_mode' end select ! check for errors: if (info /= 0) then call me%set_status(istat = -6, string = 'Error solving linear system. info =', i=info) exit else ! next step, using the specified method: call me%linesearch(xold,p,x,f,fvec,fjac,fjac_sparse) ! keep track of the number of steps in the "uphill" direction: if (f>fold) then n_uphill = n_uphill + 1 else n_uphill = 0 end if ! check for stopping conditions if (f <= me%tol) then call me%set_status(istat = 1, string = 'Required accuracy achieved') exit elseif ( maxval(abs(x-xold)) <= me%tolx ) then call me%set_status(istat = 2, string = 'Solution cannot be improved') exit elseif (iter == me%max_iter) then call me%set_status(istat = 3, string = 'Maximum number of iterations reached') exit elseif (n_uphill > me%n_uphill_max) then call me%set_status(istat = 5, string = 'Too many steps in the uphill direction') exit elseif (me%use_broyden) then ! If delxmag2 is too small when using broyden, just ! call the user-supplied jacobian function to avoid ! a divide by zero on the next step. This should ! normally only happen when the solution is almost converged. if (norm2(x-xold)**2 <= eps) then recompute_jac = .true. else if (me%broyden_update_n>0) then ! Note that we also recompute if we have taken an uphill step if (broyden_counter==me%broyden_update_n .or. n_uphill>0) then ! time to recompute the full jacobian recompute_jac = .true. end if end if end if endif end if end do !end of iterations loop end if !Export the last iteration: iter = iter + 1 if (associated(me%export_iteration)) call me%export_iteration(x,fvec,iter) end subroutine nlesolver_solver !***************************************************************************************** !***************************************************************************************** !> ! if necessary, adjust the `x` vector to be within the bounds. ! used for the initial guess. subroutine adjust_x_for_bounds(me,x) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(inout) :: x !! the `x` vector to adjust integer :: i !! counter if (me%bounds_mode/=NLESOLVER_IGNORE_BOUNDS) then ! for all bounds modes, adjust the initial guess to be within the bounds ! x = min(max(x,me%xlow),me%xupp) do i = 1, me%n if (x(i)<me%xlow(i)) then x(i) = me%xlow(i) if (me%verbose) write(me%iunit, '(A)') 'Initial x('//int2str(i)//') < xlow(i) : adjusting to lower bound' else if (x(i)>me%xupp(i)) then x(i) = me%xupp(i) if (me%verbose) write(me%iunit, '(A)') 'Initial x('//int2str(i)//') > xupp(i) : adjusting to upper bound' end if end do end if end subroutine adjust_x_for_bounds !***************************************************************************************** !***************************************************************************************** !> ! if necessary, adjust the search direction vector `p` so that bounds are not violated. ! !### Reference ! * https://openmdao.org/newdocs/versions/latest/features/building_blocks/solvers/bounds_enforce.html subroutine adjust_search_direction(me,x,p,alpha_top,pnew,modified) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: x !! initial `x`. it is assumed that this does not violate any bounds. real(wp),dimension(me%n),intent(in) :: p !! search direction `p = xnew - x` real(wp),intent(in) :: alpha_top !! the largest step length that will be attempted in the line search !! (the bounds are checked for the step `alpha_top*p`, not the full step `p`, !! so that variables are not put on a bound that the steps actually !! attempted do not reach). real(wp),dimension(me%n),intent(out) :: pnew !! new search direction: `x + alpha_top*pnew` is the !! bounded version of `x + alpha_top*p`. logical,dimension(me%n),intent(out) :: modified !! indicates the elements of `p` that were modified integer :: i !! counter real(wp),dimension(:),allocatable :: xnew !! `x + alpha_top*p`, adjusted for the bounds logical :: search_direction_modifed !! if `p` has been modified real(wp) :: t !! indep var in the step equation: `xnew = x + t*alpha_top*p` if (me%bounds_mode==NLESOLVER_IGNORE_BOUNDS .or. alpha_top <= 0.0_wp) then ! no change modified = .false. pnew = p return end if allocate(xnew(me%n)) xnew = x + alpha_top * p ! the largest step that will be attempted search_direction_modifed = .false. ! will be set to true if any bounds are violated if (me%bounds_mode==NLESOLVER_VECTOR_BOUNDS) t = 1.0_wp ! for mode 2, start with the largest step ! note: have to check each variable and ! choose the smallest t. don't need to ! keep track of xnew. modified = .false. do i = 1, me%n if (xnew(i)<me%xlow(i)) then search_direction_modifed = .true. modified(i) = .true. if (me%verbose) write(me%iunit, '(A)') 'x('//int2str(i)//') < xlow(i) : adjusting to lower bound' select case (me%bounds_mode) case(NLESOLVER_SCALAR_BOUNDS,NLESOLVER_WALL_BOUNDS); xnew(i) = me%xlow(i) case(NLESOLVER_VECTOR_BOUNDS); t = min(t,(me%xlow(i)-x(i))/(alpha_top*p(i))) end select else if (xnew(i)>me%xupp(i)) then search_direction_modifed = .true. modified(i) = .true. if (me%verbose) write(me%iunit, '(A)') 'x('//int2str(i)//') > xupp(i) : adjusting to upper bound' select case (me%bounds_mode) case(NLESOLVER_SCALAR_BOUNDS,NLESOLVER_WALL_BOUNDS); xnew(i) = me%xupp(i) case(NLESOLVER_VECTOR_BOUNDS); t = min(t,(me%xupp(i)-x(i))/(alpha_top*p(i))) end select end if end do if (search_direction_modifed) then ! adjust the search direction: select case (me%bounds_mode) case (NLESOLVER_SCALAR_BOUNDS,NLESOLVER_WALL_BOUNDS) ! here we have changed the search direction vector (only for the modified components, ! so that x + alpha_top*pnew is on the bound for these): pnew = p where (modified) pnew = (xnew - x) / alpha_top if (all(pnew==0.0_wp)) call me%set_status(istat = -17, string = 'Error adjusting line search direction for bounds') case (NLESOLVER_VECTOR_BOUNDS) ! here we are staying on the original search direction vector, just walking back. ! t is the fraction of the largest step that stays within the bounds. If a variable ! is already on a bound and the step points outward, then t = 0, and no step is ! possible in this mode (the bounds are never ignored). pnew = p * max(t, 0.0_wp) end select if (me%verbose) write(me%iunit, '(A)') 'Search direction modified to be within bounds' else pnew = p end if end subroutine adjust_search_direction !***************************************************************************************** !***************************************************************************************** !> ! Compute the next step. subroutine compute_next_step(me, xold, search_direction, alpha, alpha_top, modified, xnew) class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! initial `x` real(wp),dimension(me%n),intent(in) :: search_direction !! search direction vector real(wp),intent(in) :: alpha !! step length to take in the search direction real(wp),intent(in) :: alpha_top !! the largest step length that will be attempted !! in the line search (see [[adjust_search_direction]]) logical,dimension(me%n),intent(in) :: modified !! indicates the elements of `p` that were !! modified because they violated the bounds. !! Output of [[adjust_search_direction]]. real(wp),dimension(me%n),intent(out) :: xnew !! final `x` if (me%bounds_mode == NLESOLVER_WALL_BOUNDS) then ! for the 'wall' mode, the modified variables are put on the bounds and held ! fixed there during the line search (independent of alpha). Note that for these, ! `xold + alpha_top*search_direction` is the bound (see `adjust_search_direction`). where (modified) xnew = xold + alpha_top * search_direction else where xnew = xold + search_direction * alpha end where else ! all other modes just use the computed search direction xnew = xold + search_direction * alpha end if if (me%bounds_mode /= NLESOLVER_IGNORE_BOUNDS) then ! For alpha in (0,1], the steps computed by `adjust_search_direction` are within the ! bounds in exact arithmetic, but a point meant to be on a bound can be just outside ! it due to rounding (e.g., x + (xupp - x) > xupp). Make sure it never is: xnew = min(max(xnew, me%xlow), me%xupp) end if end subroutine compute_next_step !***************************************************************************************** !***************************************************************************************** !> ! 2-norm function pure function norm_2(me, fvec) result(f) class(nlesolver_type),intent(in) :: me real(wp),dimension(me%m),intent(in) :: fvec !! the function vector real(wp) :: f !! norm of the vector f = norm2(fvec) end function norm_2 !***************************************************************************************** !***************************************************************************************** !> ! 1-norm function pure function norm_1(me, fvec) result(f) class(nlesolver_type),intent(in) :: me real(wp),dimension(me%m),intent(in) :: fvec !! the function vector real(wp) :: f !! norm of the vector f = sum(abs(fvec)) end function norm_1 !***************************************************************************************** !***************************************************************************************** !> ! Infinity-norm function pure function norm_inf(me, fvec) result(f) class(nlesolver_type),intent(in) :: me real(wp),dimension(me%m),intent(in) :: fvec !! the function vector real(wp) :: f !! norm of the vector f = maxval(abs(fvec)) end function norm_inf !***************************************************************************************** !***************************************************************************************** !> ! Destructor subroutine destroy_nlesolver_variables(me) implicit none class(nlesolver_type),intent(out) :: me me%message = 'Error: class has not been initialized' me%istat = -999 end subroutine destroy_nlesolver_variables !***************************************************************************************** !***************************************************************************************** !> ! Sparse minimum-norm linear solver using CRAIG's method. ! ! Computes the minimum-norm solution of the compatible system \( A x = b \), ! where `A` is an `m x n` sparse matrix stored in coordinate (triplet) format. ! This is intended for the underdetermined case (`n > m`), but works for any ! compatible system. The dense matrix is never formed: the only operations ! on `A` are the products \( A v \) and \( A^T u \), each costing `O(nnz)`. ! ! The method is based on the Golub-Kahan bidiagonalization ! \( A V_k = U_k L_k + \beta_{k+1} u_{k+1} e_k^T \), where \( L_k \) is lower ! bidiagonal. The iterate is \( x_k = V_k z_k \) with \( L_k z_k = \beta_1 e_1 \). ! Since every \( v_i \in \mathrm{range}(A^T) \), \( x_k \) converges to the ! minimum-norm solution \( x = A^T (A A^T)^{-1} b \). It is mathematically ! equivalent to CG applied to \( A A^T y = b \), \( x = A^T y \), but without ! forming \( A A^T \), and it minimizes the error \( \| x - x_k \| \) at each step. ! The residual is available for free: \( \| b - A x_k \| = | \beta_{k+1} \zeta_k | \). ! ! If `damp > 0`, the regularized problem is solved instead: ! \( \min \| x \|^2 + \| s \|^2 \) subject to \( A x + \delta s = b \), ! whose solution is \( x = A^T (A A^T + \delta^2 I)^{-1} b \). This is done by applying ! the same method to the operator \( [A, \; \delta I] \), which always has full row rank, ! so the iteration cannot break down. This is useful when `A` is rank deficient. ! !### Status codes (`istop`) ! * 0 -- `b = 0`, so the solution is `x = 0` ! * 1 -- converged: \( \| r \| \le \mathrm{btol} \| b \| + \mathrm{atol} \| A \|_F \| x \| \) ! * 2 -- breakdown: \( \alpha_k = 0 \). `b` is not in the range of `A` (incompatible system). ! * 3 -- iteration limit `itnlim` reached ! !### References ! * C. C. Paige and M. A. Saunders, "LSQR: An algorithm for sparse linear equations and ! sparse least squares", ACM TOMS 8(1), 1982. ! * M. A. Saunders, "Solution of sparse rectangular systems using LSQR and CRAIG", ! BIT 35, 1995. subroutine craig_solver(m,n,nnz,irow,icol,a,b,damp,atol,btol,itnlim,nout,x,istop,itn,rnorm,xnorm) implicit none integer,intent(in) :: m !! number of rows in `A` integer,intent(in) :: n !! number of columns in `A` integer,intent(in) :: nnz !! number of nonzero elements of `A` integer,dimension(nnz),intent(in) :: irow !! row indices of the nonzero elements of `A` integer,dimension(nnz),intent(in) :: icol !! column indices of the nonzero elements of `A` real(wp),dimension(nnz),intent(in) :: a !! nonzero elements of `A` real(wp),dimension(m),intent(in) :: b !! right hand side real(wp),intent(in) :: damp !! damping (regularization) parameter \( \delta \ge 0 \) real(wp),intent(in) :: atol !! relative error in the definition of `A` !! (values less than machine epsilon are replaced by it) real(wp),intent(in) :: btol !! relative error in the definition of `b` !! (values less than machine epsilon are replaced by it) integer,intent(in) :: itnlim !! maximum number of iterations integer,intent(in) :: nout !! output unit for printing iterations (none if `<=0`) real(wp),dimension(n),intent(out) :: x !! minimum-norm solution integer,intent(out) :: istop !! status code (see above) integer,intent(out),optional :: itn !! number of iterations performed real(wp),intent(out),optional :: rnorm !! estimate of \( \| b - A x - \delta s \| \) real(wp),intent(out),optional :: xnorm !! estimate of \( \| [x; s] \| \) real(wp),dimension(m) :: u !! left Lanczos vector real(wp),dimension(n) :: v !! right Lanczos vector (`x` part) real(wp),dimension(:),allocatable :: w !! right Lanczos vector (`s` part, only if `damp>0`) real(wp) :: alpha, beta, zeta !! bidiagonalization and solution coefficients real(wp) :: anorm, bnorm !! norms of `[A, damp*I]` (Frobenius) and `b` real(wp) :: xnrm2 !! \( \| [x; s] \|^2 \) real(wp) :: res !! residual norm real(wp) :: atol_, btol_ !! tolerances used logical :: damped !! if `damp>0` integer :: k !! iteration counter x = zero istop = 0 k = 0 res = zero xnrm2 = zero damped = damp > zero atol_ = max(atol, eps) btol_ = max(btol, eps) ! beta_1 u_1 = b u = b beta = norm2(u) bnorm = beta if (beta > zero) then u = u / beta ! alpha_1 v_1 = A^T u_1 call atx(u, v) if (damped) then w = damp * u alpha = sqrt(dot_product(v,v) + dot_product(w,w)) else alpha = norm2(v) end if if (alpha <= zero) then istop = 2 ! A^T b = 0 : b is orthogonal to range(A) res = bnorm else v = v / alpha if (damped) w = w / alpha anorm = sqrt(sum(a**2) + m*damp**2) ! Frobenius norm of [A, damp*I] zeta = beta / alpha ! first element of z: L_k z_k = beta_1 e_1 if (nout > 0) write(nout,'(A)') ' itn ||r|| ||x||' istop = 3 do k = 1, itnlim ! update the solution: x_k = x_{k-1} + zeta_k v_k x = x + zeta * v xnrm2 = xnrm2 + zeta**2 ! continue the bidiagonalization: ! beta_{k+1} u_{k+1} = A v_k + damp w_k - alpha_k u_k call ax(v, u, -alpha) if (damped) u = u + damp * w beta = norm2(u) ! residual: r_k = -beta_{k+1} zeta_k u_{k+1} res = abs(beta * zeta) if (nout > 0) write(nout,'(I6,1P,2E16.6)') k, res, sqrt(xnrm2) if (res <= btol_*bnorm + atol_*anorm*sqrt(xnrm2)) then istop = 1 exit end if u = u / beta ! alpha_{k+1} v_{k+1} = A^T u_{k+1} - beta_{k+1} v_k call atx(u, v, -beta) if (damped) then w = damp * u - beta * w alpha = sqrt(dot_product(v,v) + dot_product(w,w)) else alpha = norm2(v) end if if (alpha <= eps*anorm) then istop = 2 ! breakdown: system is incompatible (rank deficient A) exit end if v = v / alpha if (damped) w = w / alpha ! next element of z (forward substitution with L_k) zeta = -beta * zeta / alpha end do k = min(k, itnlim) end if end if if (present(itn)) itn = k if (present(rnorm)) rnorm = res if (present(xnorm)) xnorm = sqrt(xnrm2) contains subroutine ax(xv, y, c) !! compute `y = A*xv + c*y` using the sparse triplet representation real(wp),dimension(n),intent(in) :: xv real(wp),dimension(m),intent(inout) :: y real(wp),intent(in) :: c integer :: i y = c * y do i = 1, nnz y(irow(i)) = y(irow(i)) + a(i) * xv(icol(i)) end do end subroutine ax subroutine atx(yv, xv, c) !! compute `xv = A^T*yv + c*xv` using the sparse triplet representation !! (if `c` is not present, `xv` is an output only) real(wp),dimension(m),intent(in) :: yv real(wp),dimension(n),intent(inout) :: xv real(wp),intent(in),optional :: c integer :: i if (present(c)) then xv = c * xv else xv = zero end if do i = 1, nnz xv(icol(i)) = xv(icol(i)) + a(i) * yv(irow(i)) end do end subroutine atx end subroutine craig_solver !***************************************************************************************** !***************************************************************************************** !> ! Solve the linear system: \( Ax = b \), using a dense, direct method. ! ! * if `n=m` : use LAPACK `dgesv` (LU decomposition) ! * if `n/=m` : use LAPACK `dgels` (if m>n uses QR factorization, ! if m<n uses LQ factorization) subroutine linear_solver(m,n,a,b,x,info) implicit none integer,intent(in) :: n !! number of columns in `a` integer,intent(in) :: m !! number of rows in `a` real(wp),dimension(m,n),intent(in) :: a !! `A` matrix of the linear system real(wp),dimension(m),intent(in) :: b !! RHS of the linear system real(wp),dimension(n),intent(out) :: x !! the solution of the linear system. integer,intent(out) :: info !! output status flag (`=0` if success) ! LAPACK routine interfaces: interface subroutine dgesv(n,nrhs,a,lda,ipiv,b,ldb,info) !! See: [?gesv](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/lapack-routines/lapack-linear-equation-routines/lapack-linear-equation-driver-routines/gesv.html) import :: wp implicit none integer :: info integer :: lda integer :: ldb integer :: n integer :: nrhs integer :: ipiv(*) real(wp) :: a(lda,*) real(wp) :: b(ldb,*) end subroutine dgesv subroutine dgels(trans,m,n,nrhs,a,lda,b,ldb,work,lwork,info) !! See: [?gels](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/lapack-routines/lapack-least-squares-and-eigenvalue-problem/lapack-least-squares-eigenvalue-problem-driver/linear-least-squares-lls-problems-lapack-driver/gels.html) import :: wp implicit none character :: trans integer :: info integer :: lda integer :: ldb integer :: lwork integer :: m integer :: n integer :: nrhs real(wp) :: a(lda,*) real(wp) :: b(ldb,*) real(wp) :: work(*) end subroutine dgels end interface integer,dimension(:),allocatable :: ipiv !! pivot indices array real(wp),dimension(:,:),allocatable :: bmat !! copy of `b` so it won't be overwritten real(wp),dimension(:),allocatable :: work !! work array for `dgels` real(wp),dimension(:,:),allocatable :: amat !! copy of `a` so it won't be overwritten integer :: lwork !! size of `work` allocate(amat(m,n)) allocate(bmat(max(1,m,n),1)) if (n==m) then !normal inverse allocate(ipiv(n)) amat = a bmat(1:n,1) = b call dgesv(n, 1, amat, n, ipiv, bmat, n, info) x = bmat(1:n,1) else amat = a bmat = zero bmat(1:m,1) = b lwork = min(m,n)+max(1,m,n) allocate(work(lwork)) call dgels('N', m, n, 1, amat, m, bmat, max(1,m,n), work, lwork, info) x = bmat(1:n,1) end if end subroutine linear_solver !***************************************************************************************** !***************************************************************************************** !> ! Take a simple step in the search direction of `p * alpha`. subroutine simple_step(me,xold,p,x,f,fvec,fjac,fjac_sparse) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! previous value of `x` real(wp),dimension(me%n),intent(in) :: p !! search direction real(wp),dimension(me%n),intent(out) :: x !! new `x` real(wp),intent(inout) :: f !! magnitude of `fvec` real(wp),dimension(me%m),intent(inout) :: fvec !! function vector real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense] real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse] real(wp),dimension(:),allocatable :: search_direction logical,dimension(:),allocatable :: modified !! indicates the elements of p that were modified allocate(search_direction(me%n)) allocate(modified(me%n)) call me%adjust_search_direction(xold,p,me%alpha,search_direction,modified) call me%compute_next_step(xold, search_direction, me%alpha, me%alpha, modified, x) !evaluate the function at the new point: call me%func(x,fvec) f = me%norm(fvec) end subroutine simple_step !***************************************************************************************** !***************************************************************************************** !> ! Backtracking line search. ! !### See also ! * [Backtracking line search](https://en.wikipedia.org/wiki/Backtracking_line_search) ! !@note Either `fjac` or `fjac_sparse` should be present. subroutine backtracking_linesearch(me,xold,p,x,f,fvec,fjac,fjac_sparse) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! previous value of `x` real(wp),dimension(me%n),intent(in) :: p !! search direction real(wp),dimension(me%n),intent(out) :: x !! new `x` real(wp),intent(inout) :: f !! magnitude of `fvec` real(wp),dimension(me%m),intent(inout) :: fvec !! function vector real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense] real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse] integer :: i !! counter real(wp) :: slope !! local slope of the function of `alpha` along the search direction used for line search logical :: min_alpha_reached !! if the minimum step size is reached during the line search real(wp) :: alpha !! `alpha` for the line search real(wp) :: ftmp !! `f` value for linesearch real(wp) :: t !! used for line search real(wp) :: phi0 !! merit function \( \frac{1}{2} \| f \|_2^2 \) at `xold` real(wp) :: phi !! merit function \( \frac{1}{2} \| f \|_2^2 \) at the trial point real(wp),dimension(:),allocatable :: gradf !! line search objective function gradient vector real(wp),dimension(:),allocatable :: xtmp !! `x` value for linesearch real(wp),dimension(:),allocatable :: fvectmp !! `fvec` value for linesearch real(wp),dimension(:),allocatable :: search_direction !! search direction to use (can be modified from `p` if bounds are violated) logical,dimension(:),allocatable :: modified !! indicates the elements of p that were modified ! allocate arrays: allocate(gradf(me%n)) allocate(xtmp(me%n)) allocate(fvectmp(me%m)) allocate(search_direction(me%n)) allocate(modified(me%n)) ! set the search direction: call me%adjust_search_direction(xold,p,me%alpha_max,search_direction,modified) ! compute the gradient of the merit function to be minimized: ! phi = 1/2 * ||fvec||_2^2, so grad(phi) = J^T * fvec. ! Use the chain rule and the Jacobian matrix already computed. ! Note: the 2-norm is always used for the merit function here (regardless ! of `norm_mode`), so that the slope and the sufficient decrease test are consistent. if (present(fjac)) then ! dense do i=1,me%n gradf(i) = dot_product(fvec,fjac(:,i)) end do else ! sparse: gradf = J^T * fvec gradf = zero do i=1,me%n_nonzeros gradf(me%icol(i)) = gradf(me%icol(i)) + fjac_sparse(i)*fvec(me%irow(i)) end do end if slope = dot_product(search_direction, gradf) t = -me%c * slope phi0 = 0.5_wp * dot_product(fvec,fvec) if (me%verbose) then write(me%iunit,'(1P,*(A,1X,E16.6))') ' slope = ', slope write(me%iunit,'(1P,*(A,1X,E16.6))') ' t = ', t end if ! perform the line search: min_alpha_reached = .false. ! initialize alpha = me%alpha_max ! start with the largest step do call me%compute_next_step(xold, search_direction, alpha, me%alpha_max, modified, xtmp) call me%func(xtmp,fvectmp) ftmp = me%norm(fvectmp) phi = 0.5_wp * dot_product(fvectmp,fvectmp) if (me%verbose) then write(me%iunit,'(1P,*(A,1X,E16.6))') ' alpha = ', alpha, ' f = ', ftmp if (phi0 - phi >= alpha*t) then write(me%iunit,'(1P,2(A,1X,E16.6),1X,A)') ' phi0-phi = ', phi0 - phi, ' alpha*t = ', alpha*t, ' [ACCEPTED]' else write(me%iunit,'(1P,*(A,1X,E16.6))') ' phi0-phi = ', phi0 - phi, ' alpha*t = ', alpha*t end if end if ! Armijo condition: phi(xold + alpha*p) <= phi(xold) + c * alpha * slope if ((phi0 - phi >= alpha*t) .or. min_alpha_reached) then if (me%verbose .and. min_alpha_reached) then write(me%iunit,'(A)') ' Minimum alpha reached' end if ! Armijo-Goldstein condition is satisfied ! (or the min step has been reached) x = xtmp fvec = fvectmp f = ftmp exit end if alpha = alpha * me%tau ! reduce step size if (alpha<=me%alpha_min) then alpha = me%alpha_min min_alpha_reached = .true. ! will stop on the next step end if end do end subroutine backtracking_linesearch !***************************************************************************************** !***************************************************************************************** !> ! An exact linesearch that uses a derivative-free minimizer to ! find the minimum value of `f(x)` between ! `x = xold + p * alpha_min` and ! `x = xold + p * alpha_max`. ! ! Usually this is overkill and not necessary, but is here as an option for testing. subroutine exact_linesearch(me,xold,p,x,f,fvec,fjac,fjac_sparse) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! previous value of `x` real(wp),dimension(me%n),intent(in) :: p !! search direction real(wp),dimension(me%n),intent(out) :: x !! new `x` real(wp),intent(inout) :: f !! magnitude of `fvec` real(wp),dimension(me%m),intent(inout) :: fvec !! function vector real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense] real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse] real(wp),dimension(:),allocatable :: xnew !! trial `x` (used in [[func_for_fmin]]) real(wp),dimension(:),allocatable :: fvecnew !! trial `fvec` (used in [[func_for_fmin]]) real(wp),dimension(:),allocatable :: search_direction !! search direction to use (may be modified from `p` if bounds are violated) logical,dimension(:),allocatable :: modified !! indicates the elements of p that were modified real(wp) :: alpha !! step length returned by [[fmin]] real(wp) :: phi !! value returned by [[func_for_fmin]] real(wp) :: phi_best !! best (smallest) value of `norm(fvec)**2` found so far real(wp) :: alpha_best !! `alpha` corresponding to `phi_best` allocate(xnew(me%n)) allocate(fvecnew(me%m)) allocate(search_direction(me%n)) allocate(modified(me%n)) ! set the search direction (this must be done before the line search): call me%adjust_search_direction(xold,p,me%alpha_max,search_direction,modified) ! the full step is evaluated explicitly, since fmin never evaluates ! the endpoints of the interval, and near the solution the full ! Newton step is usually the minimizer. phi_best = huge(one) phi = func_for_fmin(me%alpha_max) ! find the minimum value of f in the range of alphas. ! Note: the function minimized is norm(fvec)**2, which has the same minimizer as ! norm(fvec), but (for the 2-norm) is smooth at a zero, which is much better for the ! parabolic interpolation used by fmin. The best point is saved in func_for_fmin. alpha = fmin(func_for_fmin,me%alpha_min,me%alpha_max,me%fmin_tol) if (me%verbose) write(me%iunit,'(1P,*(A,1X,E16.6))') ' alpha_min = ', alpha_best ! x, fvec, and f were saved for the best point evaluated: f = sqrt(phi_best) contains real(wp) function func_for_fmin(alpha) !! function for [[fmin]]: `norm(fvec)**2` at `xold + alpha*search_direction`. !! Also saves the best point evaluated so far in `x` and `fvec`. implicit none real(wp),intent(in) :: alpha !! indep variable call me%compute_next_step(xold, search_direction, alpha, me%alpha_max, modified, xnew) call me%func(xnew,fvecnew) func_for_fmin = me%norm(fvecnew)**2 if (func_for_fmin < phi_best .or. phi_best == huge(one)) then ! (always save the first one) phi_best = func_for_fmin alpha_best = alpha x = xnew fvec = fvecnew end if end function func_for_fmin end subroutine exact_linesearch !***************************************************************************************** !***************************************************************************************** !> ! A simple search that just evaluates the function at a specified ! number of points and picks the one with the minimum function value. subroutine fixed_point_linesearch(me,xold,p,x,f,fvec,fjac,fjac_sparse) implicit none class(nlesolver_type),intent(inout) :: me real(wp),dimension(me%n),intent(in) :: xold !! previous value of `x` real(wp),dimension(me%n),intent(in) :: p !! search direction real(wp),dimension(me%n),intent(out) :: x !! new `x` real(wp),intent(inout) :: f !! magnitude of `fvec` real(wp),dimension(me%m),intent(inout) :: fvec !! function vector real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense] real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse] integer :: i !! counter integer :: n_points !! number of points to compute real(wp),dimension(:),allocatable :: alphas_to_try !! set of `alpha` values to try real(wp),dimension(:),allocatable :: x_tmp !! temp `x` real(wp),dimension(:),allocatable :: fvec_tmp !! temp `fvec` real(wp) :: f_tmp !! temp `f` real(wp) :: step_size !! step size for `alpha` integer :: n !! number of steps to divide the interval real(wp),dimension(:),allocatable :: search_direction !! search direction to use (may be modified from `p` if bounds are violated) logical,dimension(:),allocatable :: modified !! indicates the elements of p that were modified ! 1 o-----------o ! 2 o-----o-----o ! 3 o---o---o---o n = me%n_intervals n_points = n + 1 allocate(alphas_to_try(n_points)) allocate(x_tmp(me%n)) allocate(fvec_tmp(me%m)) allocate(search_direction(me%n)) allocate(modified(me%n)) step_size = (me%alpha_max - me%alpha_min) / real(n,wp) ! compute the alphas: alphas_to_try(1) = me%alpha_min do i = 2, n alphas_to_try(i) = alphas_to_try(i-1) + step_size end do alphas_to_try(n_points) = me%alpha_max ! now compute the functions at these alphas: f = big call me%adjust_search_direction(xold,p,me%alpha_max,search_direction,modified) do i = 1, n_points call me%compute_next_step(xold, search_direction, alphas_to_try(i), me%alpha_max, modified, x_tmp) ! evaluate the function at this point: call me%func(x_tmp,fvec_tmp) f_tmp = me%norm(fvec_tmp) if (f_tmp<=f) then ! new best point x = x_tmp f = f_tmp fvec = fvec_tmp end if end do end subroutine fixed_point_linesearch !***************************************************************************************** !****************************************************************************************************** end module nlesolver_module !******************************************************************************************************