halo_solver_main Subroutine

public subroutine halo_solver_main(config_file_name, debug, gui, iteration_callback, stop_check, traj_file, epoch_callback)

Uses

  • proc~~halo_solver_main~~UsesGraph proc~halo_solver_main halo_solver_main module~parameters_module parameters_module proc~halo_solver_main->module~parameters_module fortran_astrodynamics_toolkit fortran_astrodynamics_toolkit module~parameters_module->fortran_astrodynamics_toolkit module~halo_kinds_module halo_kinds_module module~parameters_module->module~halo_kinds_module iso_fortran_env iso_fortran_env module~halo_kinds_module->iso_fortran_env

Main program to solve the Halo targeting problem.

Author

  • Jacob Williams : Sept. 2017

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: config_file_name

the config file to read

logical, intent(in) :: debug

for debugging prints

logical, intent(in), optional :: gui

run for the GUI (halo_gui): the JSON trajectory file is always exported, and the pyvista script is not run. [Default is False]

procedure(iteration_callback_func), optional :: iteration_callback

if present, it receives the trajectory of each solver iteration (in memory: no files are written for them). Note: this propagates all the segments at each iteration.

procedure(stop_check_func), optional :: stop_check
character(len=:), intent(out), optional, allocatable :: traj_file

the JSON trajectory file written for the solution (if any)

procedure(epoch_callback_func), optional :: epoch_callback

if present, it receives the epochs at the constraint points of each solver iteration (cheap: no propagation) if present, it is checked at each solver iteration, and the solver stops if it returns true


Calls

proc~~halo_solver_main~~CallsGraph proc~halo_solver_main halo_solver_main interface~eclipse_optimize my_solver_type%eclipse_optimize proc~halo_solver_main->interface~eclipse_optimize proc~constraint_violations mission_type%constraint_violations proc~halo_solver_main->proc~constraint_violations proc~define_problem_size mission_type%define_problem_size proc~halo_solver_main->proc~define_problem_size proc~delete_file delete_file proc~halo_solver_main->proc~delete_file proc~export_trajectory_json_file mission_type%export_trajectory_json_file proc~halo_solver_main->proc~export_trajectory_json_file proc~generate_eclipse_data mission_type%generate_eclipse_data proc~halo_solver_main->proc~generate_eclipse_data proc~generate_mkspk_setup_file generate_mkspk_setup_file proc~halo_solver_main->proc~generate_mkspk_setup_file proc~generate_pck_file generate_pck_file proc~halo_solver_main->proc~generate_pck_file proc~get_case_name mission_type%get_case_name proc~halo_solver_main->proc~get_case_name proc~get_segment_inputs segment%get_segment_inputs proc~halo_solver_main->proc~get_segment_inputs proc~initialize_the_solver my_solver_type%initialize_the_solver proc~halo_solver_main->proc~initialize_the_solver proc~plot_trajectory mission_type%plot_trajectory proc~halo_solver_main->proc~plot_trajectory proc~print_constraint_defects mission_type%print_constraint_defects proc~halo_solver_main->proc~print_constraint_defects proc~put_x_in_segments mission_type%put_x_in_segments proc~halo_solver_main->proc~put_x_in_segments proc~write_optvars_to_file mission_type%write_optvars_to_file proc~halo_solver_main->proc~write_optvars_to_file proc~write_solution_plot_data mission_type%write_solution_plot_data proc~halo_solver_main->proc~write_solution_plot_data solve solve proc~halo_solver_main->solve status status proc~halo_solver_main->status proc~eclipse_optimize eclipse_optimize interface~eclipse_optimize->proc~eclipse_optimize 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_rdot_vecs compute_rdot_vecs proc~export_trajectory_json_file->proc~compute_rdot_vecs proc~destroy_trajectory trajectory%destroy_trajectory proc~export_trajectory_json_file->proc~destroy_trajectory proc~export_rp_ra_json_file mission_type%export_rp_ra_json_file proc~export_trajectory_json_file->proc~export_rp_ra_json_file proc~export_trajectory_json_file->proc~propagate_segment proc~segment_trajectory_json segment_trajectory_json proc~export_trajectory_json_file->proc~segment_trajectory_json proc~generate_eclipse_data->proc~get_case_name icrf_frame icrf_frame proc~generate_eclipse_data->icrf_frame proc~generate_eclipse_data->proc~destroy_trajectory proc~get_sun_fraction get_sun_fraction proc~generate_eclipse_data->proc~get_sun_fraction proc~generate_eclipse_data->proc~propagate_segment proc~write_json_real_array write_json_real_array proc~generate_eclipse_data->proc~write_json_real_array transform transform proc~generate_eclipse_data->transform two_body_rotating_frame two_body_rotating_frame proc~generate_eclipse_data->two_body_rotating_frame proc~int_to_string int_to_string proc~generate_mkspk_setup_file->proc~int_to_string add add proc~generate_pck_file->add destroy destroy proc~generate_pck_file->destroy initialize initialize proc~generate_pck_file->initialize proc~generate_pck_file->proc~int_to_string proc~get_case_name->proc~int_to_string proc~initialize_the_solver->proc~define_problem_size proc~initialize_the_solver->proc~put_x_in_segments proc~initialize_the_solver->status proc~initialize_the_solver->initialize proc~get_sparsity_pattern mission_type%get_sparsity_pattern proc~initialize_the_solver->proc~get_sparsity_pattern proc~get_x_from_json_file mission_type%get_x_from_json_file proc~initialize_the_solver->proc~get_x_from_json_file proc~initialize_the_mission mission_type%initialize_the_mission proc~initialize_the_solver->proc~initialize_the_mission proc~read_config_file my_solver_type%read_config_file proc~initialize_the_solver->proc~read_config_file proc~plot_trajectory->proc~get_case_name add_3d_plot add_3d_plot proc~plot_trajectory->add_3d_plot add_sphere add_sphere proc~plot_trajectory->add_sphere proc~plot_trajectory->initialize proc~plot_trajectory->proc~destroy_trajectory proc~plot_trajectory->proc~propagate_segment savefig savefig proc~plot_trajectory->savefig proc~print_constraint_defects->proc~define_problem_size proc~print_constraint_defects->proc~get_problem_arrays 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~write_optvars_to_file->proc~get_case_name proc~write_optvars_to_file->add create_array create_array proc~write_optvars_to_file->create_array create_object create_object proc~write_optvars_to_file->create_object proc~write_optvars_to_file->destroy proc~write_optvars_to_file->initialize insert_after insert_after proc~write_optvars_to_file->insert_after proc~write_solution_plot_data->proc~constraint_violations proc~write_solution_plot_data->proc~define_problem_size proc~write_solution_plot_data->proc~get_case_name proc~write_solution_plot_data->add proc~write_solution_plot_data->destroy proc~write_solution_plot_data->initialize proc~eclipse_optimize->proc~constraint_violations proc~eclipse_optimize->proc~define_problem_size proc~eclipse_optimize->proc~put_x_in_segments proc~eclipse_optimize->solve proc~eclipse_optimize->status proc~eclipse_optimize->destroy proc~eclipse_optimize->initialize proc~eclipse_optimize->proc~get_sparsity_pattern c c proc~eclipse_optimize->c get_results get_results proc~eclipse_optimize->get_results get_solution get_solution proc~eclipse_optimize->get_solution 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~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~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 status_message status_message proc~eclipse_optimize->status_message proc~export_rp_ra_json_file->solve proc~export_rp_ra_json_file->add proc~export_rp_ra_json_file->destroy proc~export_rp_ra_json_file->initialize evaluate evaluate proc~export_rp_ra_json_file->evaluate fill_vector fill_vector proc~get_problem_arrays->fill_vector proc~compute_rdot compute_rdot proc~get_problem_arrays->proc~compute_rdot proc~get_sparsity_pattern->proc~define_problem_size proc~removed_columns mission_type%removed_columns proc~get_sparsity_pattern->proc~removed_columns 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 get get proc~get_x_from_json_file->get get_child get_child proc~get_x_from_json_file->get_child info info proc~get_x_from_json_file->info load load proc~get_x_from_json_file->load proc~initialize_the_mission->proc~define_problem_size proc~initialize_the_mission->destroy proc~initialize_the_mission->initialize proc~initialize_the_mission->proc~get_problem_arrays proc~initialize_the_mission->proc~get_sparsity_pattern proc~initialize_the_mission->proc~set_segment_inputs initialize_splinded_ephemeris initialize_splinded_ephemeris proc~initialize_the_mission->initialize_splinded_ephemeris magnitude magnitude proc~initialize_the_mission->magnitude proc~equilibrate_scales mission_type%equilibrate_scales proc~initialize_the_mission->proc~equilibrate_scales proc~get_scales_from_segs mission_type%get_scales_from_segs proc~initialize_the_mission->proc~get_scales_from_segs set_dpert set_dpert proc~initialize_the_mission->set_dpert set_sparsity_pattern set_sparsity_pattern proc~initialize_the_mission->set_sparsity_pattern 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 proc~read_config_file->initialize compute_crtpb_parameter compute_crtpb_parameter proc~read_config_file->compute_crtpb_parameter compute_jacobi_constant compute_jacobi_constant proc~read_config_file->compute_jacobi_constant proc~read_config_file->evaluate none~get config_file%get proc~read_config_file->none~get proc~generate_patch_points mission_type%generate_patch_points proc~read_config_file->proc~generate_patch_points proc~read_epoch read_epoch proc~read_config_file->proc~read_epoch proc~update_epoch mission_type%update_epoch proc~read_config_file->proc~update_epoch proc~add_int text_buffer%add_int proc~segment_trajectory_json->proc~add_int proc~add_real_array text_buffer%add_real_array proc~segment_trajectory_json->proc~add_real_array proc~add_str text_buffer%add_str proc~segment_trajectory_json->proc~add_str proc~clear text_buffer%clear proc~segment_trajectory_json->proc~clear 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 proc~add_real_list text_buffer%add_real_list proc~write_json_real_array->proc~add_real_list proc~write_json_real_array->proc~add_str proc~write_json_real_array->proc~clear proc~get_char config_file%get_char none~get->proc~get_char proc~get_int config_file%get_int none~get->proc~get_int proc~get_logical config_file%get_logical none~get->proc~get_logical proc~get_real config_file%get_real none~get->proc~get_real proc~get_real_vec config_file%get_real_vec none~get->proc~get_real_vec proc~add_digits add_digits proc~add_int->proc~add_digits proc~add_real_array->proc~add_real_list proc~add_real_array->proc~add_str proc~reserve text_buffer%reserve proc~add_real_array->proc~reserve proc~add_real_list->proc~add_str proc~add_real text_buffer%add_real proc~add_real_list->proc~add_real proc~add_real_list->proc~reserve proc~add_str->proc~reserve proc~compute_jacobian_stm mission_type%compute_jacobian_stm proc~equilibrate->proc~compute_jacobian_stm proc~equilibrate_scales->proc~get_sparsity_pattern proc~equilibrate_scales->set_dpert compute_jacobian compute_jacobian proc~equilibrate_scales->compute_jacobian proc~cache segment%cache proc~equilibrate_scales->proc~cache proc~equilibrate_scales->proc~compute_jacobian_stm proc~uncache segment%uncache proc~equilibrate_scales->proc~uncache 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~find_rows->proc~destroy_trajectory proc~find_rows->proc~get_sun_fraction proc~find_rows->proc~propagate_segment proc~find_rows->proc~removed_columns proc~row_columns row_columns proc~find_rows->proc~row_columns proc~generate_patch_points->initialize proc~generate_patch_points->compute_crtpb_parameter proc~generate_patch_points->first_call proc~generate_patch_points->integrate proc~get_scales_from_segs->proc~define_problem_size proc~get_scales_from_segs->fill_vector proc~newton_finish->proc~put_x_in_segments proc~newton_finish->solve proc~newton_finish->status proc~newton_finish->destroy proc~newton_finish->initialize proc~newton_finish->proc~get_sparsity_pattern proc~newton_finish->proc~eval_rows kt kt proc~newton_finish->kt proc~newton_func newton_func proc~newton_finish->proc~newton_func proc~read_epoch->none~get jd_to_et jd_to_et proc~read_epoch->jd_to_et julian_date julian_date proc~read_epoch->julian_date proc~removed_columns->proc~define_problem_size 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 calendar_date_to_et calendar_date_to_et proc~update_epoch->calendar_date_to_et et_to_jd et_to_jd proc~update_epoch->et_to_jd julian_date_to_calendar_date julian_date_to_calendar_date proc~update_epoch->julian_date_to_calendar_date proc~add_digits->proc~add_str proc~add_real->proc~add_str proc~add_real->proc~add_digits proc~add_real->proc~reserve proc~scaled scaled proc~add_real->proc~scaled proc~compute_jacobian_stm->proc~define_problem_size proc~compute_jacobian_stm->proc~put_x_in_segments proc~compute_jacobian_stm->proc~get_sparsity_pattern 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 proc~get_char->get proc~get_int->get proc~get_logical->get proc~get_real->get proc~get_real_vec->get 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 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 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->icrf_frame proc~propagate_segment_stm->transform proc~propagate_segment_stm->two_body_rotating_frame proc~propagate_segment_stm->first_call proc~propagate_segment_stm->integrate proc~propagate_segment_stm->proc~identity_stm proc~propagate_segment_stm->proc~set_segment_outputs proc~propagate_segment_stm->proc~earth_moon_frame_matrices proc~ballistic_derivs ballistic_derivs proc~propagate_segment_stm->proc~ballistic_derivs proc~two_prod two_prod proc~scaled->proc~two_prod 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~split split proc~two_prod->proc~split 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

Source Code

    subroutine halo_solver_main(config_file_name,debug,gui,iteration_callback,stop_check,traj_file,epoch_callback)

    use parameters_module
!$  use omp_lib

    implicit none

    character(len=*),intent(in) :: config_file_name  !! the config file to read
    logical,intent(in) :: debug !! for debugging prints
    logical,intent(in),optional :: gui !! run for the GUI (`halo_gui`): the JSON trajectory
                                       !! file is always exported, and the pyvista script
                                       !! is not run. [Default is False]
    procedure(iteration_callback_func),optional :: iteration_callback
        !! if present, it receives the trajectory of each solver iteration
        !! (in memory: no files are written for them). Note: this
        !! propagates all the segments at each iteration.
    procedure(stop_check_func),optional :: stop_check
    procedure(epoch_callback_func),optional :: epoch_callback
        !! if present, it receives the epochs at the constraint points of each
        !! solver iteration (cheap: no propagation)
        !! if present, it is checked at each solver iteration, and the
        !! solver stops if it returns true
    character(len=:),allocatable,intent(out),optional :: traj_file
        !! the JSON trajectory file written for the solution (if any)

    type(my_solver_type),target :: solver  !! an instance of the solver that we will use
                                           !! (target: [[eclipse_optimize]] points to it)
    real(wp),dimension(:),allocatable :: x  !! solver opt vars vector ["forward-backward" formulation]
    real(wp),dimension(:),allocatable :: x_guess  !! the initial guess (for `generate_solution_plots`)
    character(len=:),allocatable :: plot_data_file  !! for `generate_solution_plots`
    integer :: istat_plot  !! exit status of the plot script
    integer :: m  !! number of functions
    real(wp),dimension(:),allocatable :: f  !! function vector (constraint violations)
    real(wp) :: tstart, tend  !! for timing
    real(wp) :: tstart_cpu, tend_cpu  !! for timing
    integer :: istat
    character(len=:),allocatable :: message  !! Text status message from solver
    integer :: n_segs, iseg
    real(wp),dimension(6) :: x_rotating
    character(len=:),allocatable :: mkspk_input, bsp_output, mkspk_setup, pck_output  !! filenames for mkspk
!$  integer :: tid, nthreads

    write(*,'(A)') ''
    write(*,'(A)') ' * HALO start'

!$OMP PARALLEL PRIVATE(NTHREADS, TID)
!$
!$  tid = omp_get_thread_num()
!$
!$  if (tid == 0) then
!$      nthreads = omp_get_num_threads()
!$      write(*,'(A,1X,I3)') ' * Number of OMP threads: ', OMP_get_num_threads()
!$  end if
!$
!$OMP END PARALLEL

    if (debug) then
        write(*,*) ''
        write(*,*) '----------------------'
        write(*,*) 'Initializing...'
        write(*,*) '----------------------'
        write(*,*) ''
    end if

    call solver%init(config_file_name,x)  ! initialize the solver & mission (and generate the initial guess)

    if (present(gui)) then
        if (gui) then
            ! the GUI plots the JSON trajectory file itself
            solver%mission%generate_json_trajectory_file = .true.
            solver%mission%run_pyvista_script = .false.
        end if
    end if
    if (present(iteration_callback)) solver%iteration_callback => iteration_callback
    if (present(stop_check)) solver%stop_check => stop_check  ! (see halo_user_input_check)
    if (present(epoch_callback)) solver%epoch_callback => epoch_callback

    if (solver%mission%generate_plots) &
        call solver%mission%plot('guess', draw_trajectory=.true.)    ! plot the initial guess
    if (solver%mission%generate_guess_and_solution_files) &
        call solver%mission%write_optvars_to_file('guess',x)    ! write guess to a file

    !....debugging....
    ! if (solver%mission%generate_trajectory_files) &
    !     call solver%mission%plot('guess',&
    !             draw_trajectory = .false., &
    !             export_trajectory=solver%mission%generate_trajectory_files)
    !....debugging....

    if (debug) then
        call solver%mission%define_problem_size(m=m)
        allocate(f(m))
        call solver%mission%constraint_violations(x,f)
        write(*,*) ''
        write(*,*) '----------------------'
        write(*,*) 'Initial Guess...'
        write(*,*) '----------------------'
        write(*,*) ''
        write(*,'(A/,*(F30.16/))') 'x:      ', x * solver%mission%xscale + solver%mission%xoffset  ! unscaled values
        write(*,*) ''
        write(*,'(A/,*(F30.16/))') 'f:      ', f    ! scaled values
        write(*,*) ''
    end if

    if (debug) then
        write(*,*) 'INITIAL GUESS:'
        call solver%mission%define_problem_size(n_segs=n_segs)
        do iseg = 1, n_segs
            call solver%mission%segs(iseg)%get_inputs(x0_rotating=x_rotating)
            write(*,'(I5, *(F15.6,1X))') iseg, x_rotating
        end do
    end if

    if (solver%mission%solve) then
        if (debug) then
            write(*,*) ''
            write(*,*) '----------------------'
            write(*,*) 'Solving...'
            write(*,*) '----------------------'
            write(*,*) ''
        end if
        write(*,'(A)') ' * Solving'
        write(*,*) ''

        if (solver%mission%generate_solution_plots) x_guess = x  ! (for the plots)
        call cpu_time(tstart_cpu)
!$      tstart = omp_get_wtime()
        if (solver%mission%sqpopt_solve) then  ! TEMPORARY experiment
            call solver%eclipse_optimize(x)
            message = 'solved with sqpopt (sqpopt_solve)'
        else
        call solver%solve(x)  ! call the solver
        call solver%status(istat=istat, message=message)
        end if
        call cpu_time(tend_cpu)
!$      tend = omp_get_wtime()
        call solver%mission%put_x_in_segments(x) ! populate segs with solution
        if (solver%mission%eclipse_optimization .and. .not. solver%mission%sqpopt_solve) then
            call solver%eclipse_optimize(x)  ! move it out of the Earth's shadow
            call solver%mission%put_x_in_segments(x)
!$          tend = omp_get_wtime()
            call cpu_time(tend_cpu)
            message = message//' (then eclipse optimization)'
        end if
    else if (solver%mission%eclipse_optimization) then
        ! the initial guess is taken as the solution: only the eclipse optimization
        call cpu_time(tstart_cpu)
!$      tstart = omp_get_wtime()
        call solver%mission%put_x_in_segments(x)
        call solver%eclipse_optimize(x)
        call solver%mission%put_x_in_segments(x)
!$      tend = omp_get_wtime()
        call cpu_time(tend_cpu)
        message = 'Not solved (eclipse optimization of the initial guess)'
    else
        call cpu_time(tstart_cpu)
        call cpu_time(tend_cpu)
        message = 'Not solved'
    end if

    if (debug) then
        write(*,*) ''
        write(*,*) '----------------------'
        write(*,*) 'Solution...'
        write(*,*) '----------------------'
        write(*,*) ''
    end if

    write(*,*) ''
    write(*,'(A)') ' * Status: '//message
    write(*,'(A,1x,F10.3,1x,a)') ' * Elapsed cpu_time: ', (tend_cpu-tstart_cpu), 'sec'
!$  write(*,'(A,1x,F10.3,1x,a)') ' * OMP wall time   : ', (tend-tstart), 'sec'

    if (solver%mission%generate_guess_and_solution_files) then
        write(*,'(A)') ' * Generate solution file'
        call solver%mission%write_optvars_to_file('solution',x) ! write solution to a file
    end if

    if (solver%mission%generate_solution_plots .and. allocated(x_guess)) then
        plot_data_file = 'solution_plot_data_'//solver%mission%get_case_name()//'.json'
        write(*,'(A)') ' * Solution plot data: '//plot_data_file
        call solver%mission%write_solution_plot_data(plot_data_file, x_guess, x)
        write(*,'(A)') ' * Solution plots (python/plot_solution.py)'
        call execute_command_line('python ./python/plot_solution.py '//plot_data_file, &
                                  wait=.true., exitstat=istat_plot)
        if (istat_plot /= 0) write(*,'(A)') '   (the plot script failed: it needs Python with numpy and matplotlib)'
    end if

    ! export solution to plot or trajectory file
    if (solver%mission%generate_trajectory_files .or. solver%mission%generate_plots) then
        write(*,'(A)') ' * Export solution trajectory'
        call solver%mission%plot('solution',&
                draw_trajectory=solver%mission%generate_plots, &
                export_trajectory=solver%mission%generate_trajectory_files)
    end if

    if (solver%mission%generate_kernel) then
        if (.not. solver%mission%generate_trajectory_files) then
            error stop 'error: kernel generation requires the trajectory file to be exported'
        else
            write(*,*) '* Generate BSP kernel'

            mkspk_input = 'solution_'//solver%mission%get_case_name()//'.txt'
            bsp_output  = 'solution_'//solver%mission%get_case_name()//'.bsp'
            mkspk_setup = 'mkspk_'//solver%mission%get_case_name()//'.txt'
            pck_output  = 'solution_'//solver%mission%get_case_name()//'.tpc.json'

            !note: have to delete the kernel if it is already there or mkspk will fail.
            call delete_file(bsp_output)

            call generate_mkspk_setup_file(mkspk_setup)
            call generate_pck_file(pck_output)
            call execute_command_line(mkspk_path//' -setup '//mkspk_setup//' -input '//mkspk_input//' -output '//bsp_output)
        end if
    end if

    if (debug) then
        write(*,*) 'SOLUTION:'
        call solver%mission%define_problem_size(n_segs=n_segs)
        do iseg = 1, n_segs
            call solver%mission%segs(iseg)%get_inputs(x0_rotating=x_rotating)
            write(*,'(I5, *(F15.6,1X))') iseg, x_rotating
        end do
    end if

    if (solver%mission%generate_defect_file) then
        write(*,'(A)') ' * Generate defect file'
        call solver%mission%print_constraint_defects('defects_'//&
                                                     solver%mission%get_case_name()//&
                                                     '.csv')
    end if

    if (solver%mission%generate_json_trajectory_file) then
        call solver%mission%export_trajectory_json_file('traj_'//solver%mission%get_case_name())
        if (present(traj_file)) traj_file = 'traj_'//solver%mission%get_case_name()//'.json'
        ! note: halo_gui reads the file name from this line
        write(*,'(A)') ' * Trajectory file: traj_'//solver%mission%get_case_name()//'.json'
    end if

    if (solver%mission%generate_eclipse_files) then
        call solver%mission%generate_eclipse_data('eclipse', &
                                                  filetype = solver%mission%eclipse_filetype)
    end if

    if (solver%mission%run_pyvista_script) then
        write(*,'(A)') ' * Run PyVista script'
        !mkspk_input = 'solution_'//solver%mission%get_case_name()//'.txt'
        mkspk_input = 'traj_'//solver%mission%get_case_name()//'.json'
        call execute_command_line('python ./python/plot_utilities.py '//mkspk_input, wait=.false.)
    end if

    write(*,'(A)') ' * HALO done'
    write(*,'(A)') ''

    end subroutine halo_solver_main