eclipse_optimize Module Subroutine

module subroutine eclipse_optimize(me, x)

Move a converged solution out of the Earth's shadow (see the module documentation).

Arguments

Type IntentOptional Attributes Name
class(my_solver_type), intent(inout), target :: me
real(kind=wp), intent(inout), dimension(:) :: x

the converged solution (scaled); replaced by the result


Calls

proc~~eclipse_optimize~~CallsGraph proc~eclipse_optimize eclipse_optimize c c proc~eclipse_optimize->c destroy destroy proc~eclipse_optimize->destroy get_results get_results proc~eclipse_optimize->get_results get_solution get_solution proc~eclipse_optimize->get_solution initialize initialize proc~eclipse_optimize->initialize proc~constraint_violations mission_type%constraint_violations proc~eclipse_optimize->proc~constraint_violations proc~define_problem_size mission_type%define_problem_size proc~eclipse_optimize->proc~define_problem_size proc~equilibrate equilibrate proc~eclipse_optimize->proc~equilibrate proc~eval_rows eval_rows proc~eclipse_optimize->proc~eval_rows proc~find_rows find_rows proc~eclipse_optimize->proc~find_rows proc~get_sparsity_pattern mission_type%get_sparsity_pattern proc~eclipse_optimize->proc~get_sparsity_pattern proc~ks_aggregate ks_aggregate proc~eclipse_optimize->proc~ks_aggregate proc~merge_rows merge_rows proc~eclipse_optimize->proc~merge_rows proc~newton_finish newton_finish proc~eclipse_optimize->proc~newton_finish proc~put_x_in_segments mission_type%put_x_in_segments proc~eclipse_optimize->proc~put_x_in_segments proc~report_change report_change proc~eclipse_optimize->proc~report_change proc~step_limits step_limits proc~eclipse_optimize->proc~step_limits proc~union_columns union_columns proc~eclipse_optimize->proc~union_columns set_bounds set_bounds proc~eclipse_optimize->set_bounds set_functions set_functions proc~eclipse_optimize->set_functions set_jacobian_sparsity set_jacobian_sparsity proc~eclipse_optimize->set_jacobian_sparsity set_max_step set_max_step proc~eclipse_optimize->set_max_step set_problem_size set_problem_size proc~eclipse_optimize->set_problem_size solve solve proc~eclipse_optimize->solve status status proc~eclipse_optimize->status status_message status_message proc~eclipse_optimize->status_message proc~constraint_violations->proc~put_x_in_segments proc~get_problem_arrays mission_type%get_problem_arrays proc~constraint_violations->proc~get_problem_arrays proc~propagate_segment segment%propagate_segment proc~constraint_violations->proc~propagate_segment proc~segs_to_propagate mission_type%segs_to_propagate proc~constraint_violations->proc~segs_to_propagate proc~compute_jacobian_stm mission_type%compute_jacobian_stm proc~equilibrate->proc~compute_jacobian_stm proc~row_ks row_ks proc~eval_rows->proc~row_ks proc~find_rows->proc~define_problem_size proc~find_rows->proc~put_x_in_segments proc~destroy_trajectory trajectory%destroy_trajectory proc~find_rows->proc~destroy_trajectory proc~get_sun_fraction get_sun_fraction proc~find_rows->proc~get_sun_fraction proc~find_rows->proc~propagate_segment proc~removed_columns mission_type%removed_columns proc~find_rows->proc~removed_columns proc~row_columns row_columns proc~find_rows->proc~row_columns proc~get_sparsity_pattern->proc~define_problem_size proc~get_sparsity_pattern->proc~removed_columns proc~newton_finish->destroy proc~newton_finish->initialize proc~newton_finish->proc~eval_rows proc~newton_finish->proc~get_sparsity_pattern proc~newton_finish->proc~put_x_in_segments proc~newton_finish->solve proc~newton_finish->status kt kt proc~newton_finish->kt proc~newton_func newton_func proc~newton_finish->proc~newton_func proc~put_x_in_segments->proc~define_problem_size extract_vector extract_vector proc~put_x_in_segments->extract_vector proc~set_segment_inputs segment%set_segment_inputs proc~put_x_in_segments->proc~set_segment_inputs proc~report_change->proc~define_problem_size proc~report_change->proc~removed_columns proc~step_limits->proc~define_problem_size proc~step_limits->proc~removed_columns proc~compute_jacobian_stm->proc~define_problem_size proc~compute_jacobian_stm->proc~get_sparsity_pattern proc~compute_jacobian_stm->proc~put_x_in_segments proc~compute_jacobian_stm->proc~removed_columns proc~earth_moon_frame_matrices earth_moon_frame_matrices proc~compute_jacobian_stm->proc~earth_moon_frame_matrices proc~propagate_segment_stm segment%propagate_segment_stm proc~compute_jacobian_stm->proc~propagate_segment_stm fill_vector fill_vector proc~get_problem_arrays->fill_vector proc~compute_rdot compute_rdot proc~get_problem_arrays->proc~compute_rdot 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~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 first_call first_call proc~propagate_segment->first_call icrf_frame icrf_frame proc~propagate_segment->icrf_frame 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 transform transform proc~propagate_segment->transform two_body_rotating_frame two_body_rotating_frame proc~propagate_segment->two_body_rotating_frame proc~removed_columns->proc~define_problem_size proc~row_ks->proc~destroy_trajectory proc~row_ks->proc~get_sun_fraction proc~row_ks->proc~propagate_segment proc~row_ks->proc~set_segment_inputs proc~segs_to_propagate->proc~define_problem_size proc~set_segment_inputs->icrf_frame proc~set_segment_inputs->transform proc~set_segment_inputs->two_body_rotating_frame get_rv get_rv proc~earth_moon_frame_matrices->get_rv get_rv_derivative get_rv_derivative proc~earth_moon_frame_matrices->get_rv_derivative proc~two_body_rotating_frame_matrices two_body_rotating_frame_matrices proc~earth_moon_frame_matrices->proc~two_body_rotating_frame_matrices proc~propagate_segment_stm->first_call proc~propagate_segment_stm->icrf_frame proc~propagate_segment_stm->integrate proc~propagate_segment_stm->proc~earth_moon_frame_matrices proc~propagate_segment_stm->proc~identity_stm proc~propagate_segment_stm->proc~set_segment_outputs proc~propagate_segment_stm->transform proc~propagate_segment_stm->two_body_rotating_frame proc~ballistic_derivs ballistic_derivs proc~propagate_segment_stm->proc~ballistic_derivs get_acc get_acc proc~ballistic_derivs->get_acc get_r get_r proc~ballistic_derivs->get_r icrf_to_iau_moon icrf_to_iau_moon proc~ballistic_derivs->icrf_to_iau_moon j2000_to_frame j2000_to_frame proc~ballistic_derivs->j2000_to_frame proc~pines_gravity pines_gravity proc~ballistic_derivs->proc~pines_gravity proc~pointmass_gravity_gradient pointmass_gravity_gradient proc~ballistic_derivs->proc~pointmass_gravity_gradient proc~third_body_gravity_gradient third_body_gravity_gradient proc~ballistic_derivs->proc~third_body_gravity_gradient third_body_gravity third_body_gravity proc~ballistic_derivs->third_body_gravity proc~cross cross proc~two_body_rotating_frame_matrices->proc~cross proc~cross_matrix cross_matrix proc~two_body_rotating_frame_matrices->proc~cross_matrix proc~outer outer proc~pines_gravity->proc~outer proc~point_gradient point_gradient proc~pointmass_gravity_gradient->proc~point_gradient proc~third_body_gravity_gradient->proc~point_gradient proc~point_gradient->proc~outer

Called by

proc~~eclipse_optimize~~CalledByGraph proc~eclipse_optimize eclipse_optimize 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

    module subroutine eclipse_optimize(me,x)

    class(my_solver_type),intent(inout),target :: me
    real(wp),dimension(:),intent(inout) :: x !! the converged solution (scaled); replaced by the result

    type(eclipse_opt_data),target :: d
    type(sqpopt_problem_type) :: problem
    type(sqpopt_options_type) :: options
    type(sqpopt_qp_solver_type) :: qp_solver
    type(sqpopt_type) :: sqp
    type(sqpopt_results_type) :: res
    type(eclipse_row),dimension(:),allocatable :: new
    real(wp),dimension(:),allocatable :: lambda, x_lb, x_ub, c_lb, c_ub, ks, limits
    integer,dimension(:),allocatable :: irow, icol, irow_all, icol_all
    integer :: n, m, mt, iouter, istat, k, j, nnz, nr
    real(wp) :: phimin, shadow_min
    character(len=:),allocatable :: message
    integer(int64) :: count0, count1, rate

    associate (mission => me%mission)

    if (mission%jacobian_mode /= 2) error stop 'eclipse optimization requires jacobian_mode=2'

    call system_clock(count0, rate)
    d%mission => me%mission
    call mission%define_problem_size(n=n, m=m)
    d%x0 = x
    ! the optimization's own scaling of the variables: y = x/r
    allocate(d%r(n))
    d%r = one
    if (mission%eclipse_opt_epoch_scale > zero) then
        where (mission%x_is_epoch) d%r = mission%eclipse_opt_epoch_scale / mission%xscale
    end if
    d%m_halo = m
    d%single = mission%eclipse_opt_single_row
    call mission%get_sparsity_pattern(irow, icol)
    d%nnz_halo = size(irow)
    d%icol = icol
    d%irow = irow
    allocate(d%w(m))
    d%w = one
    if (mission%eclipse_opt_equilibrate) call equilibrate(d, x)
    allocate(d%rows(0))
    mission%segs%soft_fail = .true.  ! (a trial point can be far off: an integrator failure is reported to sqpopt)

    write(*,'(A)') ''
    write(*,'(A)') ' * Eclipse optimization'
    if (mission%sqpopt_solve) then
        ! TEMPORARY experiment: solve halo's problem (continuity only) with sqpopt
        allocate(new(0))
        write(*,'(A)') '   (sqpopt_solve: the continuity constraints only, from the initial guess)'
    else
        call find_rows(mission, x, new, phimin, shadow_min)
        write(*,'(A,F8.4,A,F8.1,A,I0,A)') '   converged solution: min phi = ', phimin, ', ', shadow_min, &
                                          ' min in the shadow, ', size(new), ' segments near the shadow'
    end if

    do iouter = 1, mission%eclipse_opt_max_outer

        call merge_rows(d%rows, new)
        nr = size(d%rows)
        if (nr==0 .and. .not. mission%sqpopt_solve) then
            write(*,'(A)') '   nothing near the shadow: no optimization needed'
            exit
        end if
        if (d%single) then
            mt = m + 1
            call union_columns(d)
            write(*,'(A,I0,A,I0,A,I0,A)') '   pass ', iouter, ': 1 eclipse constraint (', nr, ' segments, ', &
                                          sum(d%rows%k_hi - d%rows%k_lo + 1), ' samples)'
        else
            mt = m + nr
            write(*,'(A,I0,A,I0,A,I0,A)') '   pass ', iouter, ': ', nr, ' eclipse constraints (', &
                                          sum(d%rows%k_hi - d%rows%k_lo + 1), ' samples)'
        end if
        if (nr > 0) then
            allocate(ks(nr))
            call mission%put_x_in_segments(x)
            call eval_rows(d, ks)
            write(*,'(A,F8.4,A,F8.4)') '   smallest eclipse constraint (KS) at the start: ', minval(ks), &
                                       ', their aggregate: ', ks_aggregate(ks, mission%eclipse_opt_ks_rho)
            deallocate(ks)
        end if

        ! the problem: halo's continuity rows (equalities), then the eclipse rows
        ! (inequalities, KS >= phi_min)
        if (d%single) then
            nnz = d%nnz_halo + size(d%ucol)
        else
            nnz = d%nnz_halo + sum(d%rows%ncol)
        end if
        allocate(irow_all(nnz), icol_all(nnz))
        irow_all(1:d%nnz_halo) = irow
        icol_all(1:d%nnz_halo) = icol
        if (d%single) then
            irow_all(d%nnz_halo+1:) = m + 1
            icol_all(d%nnz_halo+1:) = d%ucol
        else
            j = d%nnz_halo
            do k = 1, nr
                irow_all(j+1:j+d%rows(k)%ncol) = m + k
                icol_all(j+1:j+d%rows(k)%ncol) = d%rows(k)%col(1:d%rows(k)%ncol)
                j = j + d%rows(k)%ncol
            end do
        end if
        allocate(x_lb(n), x_ub(n), c_lb(mt), c_ub(mt), lambda(mt))
        x_lb = -sqpopt_infinity
        x_ub =  sqpopt_infinity
        c_lb(1:m) = zero
        c_ub(1:m) = zero
        c_lb(m+1:) = mission%eclipse_opt_phi_min
        c_ub(m+1:) = sqpopt_infinity

        call problem%set_problem_size(n=n, m=mt)
        call problem%set_bounds(x_lb, x_ub, c_lb, c_ub)
        call problem%set_jacobian_sparsity(nnz, irow_all, icol_all)
        call problem%set_functions(fc=eclipse_fc, gjac=eclipse_gjac, data=d)
        if (mission%eclipse_opt_max_step_epoch > zero .or. mission%eclipse_opt_max_step_position > zero .or. &
            mission%eclipse_opt_max_step_velocity > zero) then
            limits = step_limits(d)
            call problem%set_max_step(limits)
        end if

        options%max_iter             = mission%eclipse_opt_max_iter
        options%print_level          = mission%eclipse_opt_print_level
        options%hessian_mode         = mission%eclipse_opt_hessian_mode
        options%direct_qp            = mission%eclipse_opt_direct_qp
        options%direct_least_squares = mission%eclipse_opt_direct_least_squares
        options%qp_solver_mode       = mission%eclipse_opt_qp_solver_mode
        options%linesearch_mode      = mission%eclipse_opt_linesearch_mode
        options%lbfgs_memory         = mission%eclipse_opt_lbfgs_memory
        options%scaling              = mission%eclipse_opt_scaling
        options%ctol                 = mission%eclipse_opt_ctol
        options%ktol                 = mission%eclipse_opt_ktol
        options%elastic_multiplier_limit = mission%eclipse_opt_elastic_multiplier_limit
        options%scaling_max_gradient = mission%eclipse_opt_scaling_max_gradient
        options%hessian_scale0       = mission%eclipse_opt_hessian_scale0
        options%inertia_control      = mission%eclipse_opt_inertia_control
        options%merit_mode           = mission%eclipse_opt_merit_mode
        options%restoration_mode     = mission%eclipse_opt_restoration_mode
        options%max_time             = mission%eclipse_opt_max_time
        options%linear_solver        = mission%eclipse_opt_linear_solver
        options%factorization_threads = mission%eclipse_opt_factorization_threads
        qp_solver%max_step           = mission%eclipse_opt_qp_max_step
        qp_solver%direct_max_changes = mission%eclipse_opt_direct_max_changes
        qp_solver%direct_tol         = mission%eclipse_opt_direct_tol
        qp_solver%sparse_qp%basis_rel_tol         = mission%eclipse_opt_basis_rel_tol
        qp_solver%sparse_qp%basis_repair          = mission%eclipse_opt_basis_repair
        qp_solver%sparse_qp%basis_eq_slack_weight = mission%eclipse_opt_basis_eq_slack_weight
        qp_solver%direct_relative_tol   = mission%eclipse_opt_direct_relative_tol
        qp_solver%direct_accept_inexact = mission%eclipse_opt_direct_accept_inexact

        call sqp%initialize(problem=problem, options=options, qp_solver=qp_solver)
        call sqp%solve(x/d%r, istat)
        call sqp%get_solution(x, lambda)
        x = x * d%r
        call sqp%get_results(res)
        write(*,'(A)') '   sqpopt: '//sqp%status_message()
        write(*,'(A,I0,A,I0,A,I0,A,ES10.3,A,ES10.3,A,F9.2,A)') '   sqpopt: ', res%iterations, ' iterations, ', &
            res%n_eval_fc, ' fc, ', res%n_eval_gjac, ' gjac, f = ', res%f, ', infeasibility = ', &
            res%feasibility_error, ', ', res%time, ' s'
        call sqp%destroy()
        deallocate(irow_all, icol_all, x_lb, x_ub, c_lb, c_ub, lambda)

        if (mission%sqpopt_solve) then
            ! TEMPORARY experiment: how well sqpopt alone converged halo's problem
            block
                real(wp),dimension(:),allocatable :: c
                allocate(c(m))
                call mission%constraint_violations(x, c)  ! (propagates the segments at x)
                write(*,'(A,ES12.4,A,ES12.4,A,ES10.2)') '   sqpopt_solve: halo''s constraint norm ', norm2(c), &
                    ', largest |c| ', maxval(abs(c)), ' (nlesolver_tol ', mission%nlesolver_tol
            end block
            exit
        end if

        ! converge the continuity, with the eclipse rows at their bound held there:
        if (mission%eclipse_opt_newton_finish) call newton_finish(d, x)

        ! re-converge the continuity (only):
        if (mission%eclipse_opt_polish) then
            write(*,'(A)') '   polish (Newton, continuity only):'
            call me%solve(x)
            call me%status(istat=istat, message=message)
            write(*,'(A)') '   polish: '//message
        end if

        ! check the whole trajectory:
        call find_rows(mission, x, new, phimin, shadow_min)
        write(*,'(A,I0,A,F8.4,A,F8.1,A)') '   after pass ', iouter, ': min phi = ', phimin, ', ', &
                                          shadow_min, ' min in the shadow'
        if (phimin >= zero) then
            write(*,'(A)') '   eclipse free'
            exit
        end if

    end do

    mission%segs%soft_fail = .false.
    call report_change(mission, d%x0, x)
    call system_clock(count1)
    write(*,'(A,F10.2,A)') '   eclipse optimization wall time: ', real(count1-count0,wp)/real(rate,wp), ' s'
    write(*,'(A)') ''

    end associate

    end subroutine eclipse_optimize