The Newton finish (eclipse_opt_newton_finish): from the optimization's point, solve
halo's continuity constraints and, as equalities , the
eclipse rows that are at or near their bound ( plus
eclipse_opt_newton_active_tol), with halo's minimum-norm Newton solver (nlesolver, with
halo's sparse linear solver). The optimization's point is near feasible but its
continuity isn't converged; a solve of the continuity alone (eclipse_opt_polish) moves
the trajectory back into the shadow, but with the active rows held at their bound it
can't. A row left free that falls below its bound is added to the equalities, and the
solve repeated (from where it ended).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(eclipse_opt_data), | intent(inout), | target | :: | d | ||
| real(kind=wp), | intent(inout), | dimension(:) | :: | x |
the optimization's point (halo's scaled variables); replaced by the solution |
subroutine newton_finish(d, x) type(eclipse_opt_data),intent(inout),target :: d real(wp),dimension(:),intent(inout) :: x !! the optimization's point (halo's scaled variables); replaced by the solution type(eclipse_newton_type) :: nle real(wp),dimension(:),allocatable :: ks integer,dimension(:),allocatable :: irow, icol, irow_all, icol_all logical,dimension(:),allocatable :: is_active, below integer :: attempt, n, m, na, k, j, nnz, istat, mode, stage_iter real(wp) :: phi_min, damp, fnorm0, fnorm real(wp),dimension(:),allocatable :: fvec, x_save character(len=:),allocatable :: message integer(int64) :: count0, count1, rate associate (mission => d%mission) call system_clock(count0, rate) n = size(x) m = d%m_halo phi_min = mission%eclipse_opt_phi_min call mission%get_sparsity_pattern(irow, icol) mode = mission%solver_mode if (mission%eclipse_opt_newton_solver_mode > 0) mode = mission%eclipse_opt_newton_solver_mode if (all(mode /= [2,3,4,6,7])) mode = 7 ! (a sparse solver of nlesolver's own) allocate(ks(size(d%rows)), is_active(size(d%rows)), below(size(d%rows))) call mission%put_x_in_segments(x) call eval_rows(d, ks) is_active = ks < phi_min + mission%eclipse_opt_newton_active_tol block ! TEMPORARY experiment: hold only the eclipse_opt_newton_max_active rows with the smallest KS integer :: kk, imin real(wp),dimension(:),allocatable :: kt if (mission%eclipse_opt_newton_max_active >= 0) then is_active = .false. kt = ks do kk = 1, min(mission%eclipse_opt_newton_max_active, size(ks)) imin = minloc(kt, 1) is_active(imin) = .true. kt(imin) = huge(one) end do end if end block write(*,'(A,*(F10.6))') ' Newton finish: the rows'' KS at the start: ', ks do attempt = 1, 5 nle%d => d nle%mask = is_active nle%active = pack([(k, k=1,size(d%rows))], is_active) na = size(nle%active) write(*,'(A,I0,A,I0,A)') ' Newton finish ', attempt, ': the continuity and ', na, & ' eclipse rows (KS = phi_min)' nnz = size(irow) + sum(d%rows(nle%active)%ncol) allocate(irow_all(nnz), icol_all(nnz)) irow_all(1:size(irow)) = irow icol_all(1:size(irow)) = icol j = size(irow) do k = 1, na associate (row => d%rows(nle%active(k))) irow_all(j+1:j+row%ncol) = m + k icol_all(j+1:j+row%ncol) = row%col(1:row%ncol) j = j + row%ncol end associate end do ! the damping stages: from eclipse_opt_newton_damp down by 10 each stage to 0 (a damped ! stage has eclipse_opt_newton_damp_iter iterations; the undamped last one, up to ! eclipse_opt_newton_max_iter). A stage that ends worse than it started is undone. damp = mission%eclipse_opt_newton_damp allocate(fvec(m+na)) do stage_iter = mission%eclipse_opt_newton_max_iter if (damp > zero) stage_iter = mission%eclipse_opt_newton_damp_iter nle%d => d nle%mask = is_active nle%active = pack([(k, k=1,size(d%rows))], is_active) call nle%initialize(n = n, & m = m + na, & max_iter = stage_iter, & func = newton_func, & grad_sparse = newton_grad, & tol = mission%nlesolver_tol, & tolx = mission%nlesolver_tolx, & step_mode = 2, & ! backtracking (as halo's solve) n_intervals = 2, & use_broyden = .false., & export_iteration = newton_export, & sparsity_mode = mode, & atol = 1.0e-12_wp, & btol = 1.0e-12_wp, & damp = damp, & itnlim = 1000000, & irow = irow_all, & icol = icol_all, & lusol_method = 0) call nle%status(istat=istat, message=message) if (istat /= 0) then write(*,'(A)') ' Newton finish: '//message error stop 'error initializing the Newton finish' end if call newton_func(nle, x, fvec) fnorm0 = norm2(fvec) x_save = x write(*,'(A,ES9.1,A,ES12.4)') ' Newton finish, damp ', damp, ': start norm ', fnorm0 call nle%solve(x) call nle%status(istat=istat, message=message) call newton_func(nle, x, fvec) fnorm = norm2(fvec) write(*,'(A,ES12.4)') ' Newton finish: '//message//', norm ', fnorm if (.not. fnorm <= fnorm0) then write(*,'(A)') ' Newton finish: worse; the stage is undone' x = x_save fnorm = fnorm0 end if if (damp <= zero) exit if (fnorm <= mission%nlesolver_tol) exit ! (converged with damping) damp = damp / 10.0_wp if (damp < mission%eclipse_opt_newton_damp_min) damp = zero end do call nle%destroy() deallocate(irow_all, icol_all, fvec) ! the rows left free must still be at or above their bound: call mission%put_x_in_segments(x) call eval_rows(d, ks) below = (.not. is_active) .and. ks < phi_min - 1.0e-6_wp write(*,'(A,F10.6,A,F10.6,A,I0,A)') ' Newton finish: smallest KS ', minval(ks), ' (of the free rows: ', & minval(ks, mask=.not. is_active), '), ', count(below), ' free rows below phi_min' if (.not. any(below)) exit is_active = is_active .or. below end do call system_clock(count1) write(*,'(A,F10.2,A)') ' Newton finish wall time: ', real(count1-count0,wp)/real(rate,wp), ' s' end associate end subroutine newton_finish