newton_finish Subroutine

subroutine newton_finish(d, x)

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).

Arguments

Type IntentOptional 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


Calls

proc~~newton_finish~~CallsGraph proc~newton_finish newton_finish destroy destroy proc~newton_finish->destroy initialize initialize proc~newton_finish->initialize kt kt proc~newton_finish->kt proc~eval_rows eval_rows proc~newton_finish->proc~eval_rows proc~get_sparsity_pattern mission_type%get_sparsity_pattern proc~newton_finish->proc~get_sparsity_pattern proc~newton_func newton_func proc~newton_finish->proc~newton_func proc~put_x_in_segments mission_type%put_x_in_segments proc~newton_finish->proc~put_x_in_segments solve solve proc~newton_finish->solve status status proc~newton_finish->status proc~row_ks row_ks proc~eval_rows->proc~row_ks proc~define_problem_size mission_type%define_problem_size proc~get_sparsity_pattern->proc~define_problem_size proc~removed_columns mission_type%removed_columns proc~get_sparsity_pattern->proc~removed_columns proc~newton_func->proc~eval_rows constraint_violations constraint_violations proc~newton_func->constraint_violations proc~epochs_in_range epochs_in_range proc~newton_func->proc~epochs_in_range extract_vector extract_vector proc~put_x_in_segments->extract_vector proc~put_x_in_segments->proc~define_problem_size proc~set_segment_inputs segment%set_segment_inputs proc~put_x_in_segments->proc~set_segment_inputs proc~removed_columns->proc~define_problem_size proc~row_ks->proc~set_segment_inputs proc~destroy_trajectory trajectory%destroy_trajectory proc~row_ks->proc~destroy_trajectory proc~get_sun_fraction get_sun_fraction proc~row_ks->proc~get_sun_fraction proc~propagate_segment segment%propagate_segment proc~row_ks->proc~propagate_segment icrf_frame icrf_frame proc~set_segment_inputs->icrf_frame transform transform proc~set_segment_inputs->transform two_body_rotating_frame two_body_rotating_frame proc~set_segment_inputs->two_body_rotating_frame apparent_position apparent_position proc~get_sun_fraction->apparent_position cubic_shadow_model cubic_shadow_model proc~get_sun_fraction->cubic_shadow_model from_j2000body_to_j2000ssb from_j2000body_to_j2000ssb proc~get_sun_fraction->from_j2000body_to_j2000ssb proc~propagate_segment->icrf_frame proc~propagate_segment->transform proc~propagate_segment->two_body_rotating_frame first_call first_call proc~propagate_segment->first_call integrate integrate proc~propagate_segment->integrate proc~identity_stm identity_stm proc~propagate_segment->proc~identity_stm proc~set_segment_outputs segment%set_segment_outputs proc~propagate_segment->proc~set_segment_outputs

Called by

proc~~newton_finish~~CalledByGraph proc~newton_finish newton_finish proc~eclipse_optimize eclipse_optimize proc~eclipse_optimize->proc~newton_finish interface~eclipse_optimize my_solver_type%eclipse_optimize interface~eclipse_optimize->proc~eclipse_optimize proc~halo_solver_main halo_solver_main proc~halo_solver_main->interface~eclipse_optimize

Source Code

    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