Move a converged solution out of the Earth's shadow (see the module documentation).
| Type | Intent | Optional | 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 |
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