fcn Subroutine

subroutine fcn(me, x, f, istat)

function for the SA algorithm.

Arguments

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

[period, et_ref]

real(kind=wp), intent(out) :: f
integer, intent(out) :: istat

Calls

proc~~fcn~~CallsGraph proc~fcn halo_optimizer::fcn destroy destroy proc~fcn->destroy get get proc~fcn->get get_case_name get_case_name proc~fcn->get_case_name halo_solver_main halo_solver_main proc~fcn->halo_solver_main load load proc~fcn->load read_config_file read_config_file proc~fcn->read_config_file remove remove proc~fcn->remove update update proc~fcn->update

Source Code

        subroutine fcn(me, x, f, istat)

            !! function for the SA algorithm.

            class(simulated_annealing_type),intent(inout) :: me
            real(wp), dimension(:), intent(in) :: x !! [period, et_ref]
            real(wp), intent(out) :: f
            integer,intent(out) :: istat

            integer,save :: istep = 0 !! number of function evaluations

            character(len=5) :: istep_str
            character(len=:),allocatable :: script !! script to run
            character(len=:),allocatable :: step_config !! config file for this step
            character(len=:),allocatable :: eclipse_file !! eclipse file
            type(json_file) :: json !! for loading the eclipse json file
            real(wp),dimension(:),allocatable :: et_vec !! ephemeris time of eclipse violations
            type(my_solver_type) :: solver !! just so we can read the base config and generate the case name

            istep = istep + 1
            write(istep_str,'(I5)') istep

            ! create the script
            ! read the original script and modify with the inputs, then save the new one
            step_config = './STEP='//trim(adjustl(istep_str))//'.json'
            call config%json%update('period', x(1), found)
            call config%json%update('et_ref', x(2), found)
            ! make sure the output settings are right:
            call config%json%update('initial_guess_from_file', '', found)
            call config%json%update('generate_plots',                    .false., found)
            call config%json%update('generate_trajectory_files',         .false., found)
            call config%json%update('generate_guess_and_solution_files', .false., found)
            call config%json%update('generate_kernel',                   .false., found)
            call config%json%update('generate_defect_file',              .false., found)
            call config%json%update('generate_json_trajectory_file',     .false., found)
            call config%json%update('run_pyvista_script',                .false., found)
            call config%json%update('generate_eclipse_files', .true., found)  ! only need these
            call config%json%update('eclipse_filetype', 2, found)             !
            call config%json%remove('year')
            call config%json%remove('month')
            call config%json%remove('day')
            call config%json%remove('hour')
            call config%json%remove('minute')
            call config%json%remove('sec')
            call config%json%print(step_config)

            ! have to read the config so that get_case_name will return the right string:
            write(*,*) 'reading base config file: '//base_config_file
            call solver%read_config_file(step_config)

            ! the env var is used by the script to indicate the config file to use:
            !call execute_command_line('export HALO_CONFIG_FILE='//step_config//'; '//base_script_file)
            ! new way: call the solver as a function:
            call halo_solver_main(step_config,debug=.false.)

            ! read the eclipse function output file
            eclipse_file = './eclipse_'//solver%mission%get_case_name()//'.json'
            call json%load(eclipse_file)
            call json%get('et', et_vec, found)
            if (.not. found) error stop 'error reading eclipse file. et not found.'
            call json%destroy()

            ! f is just the count the 1-hr epochs where the phi is < 0
            !TODO:: could use unique() to remove duplicate et's. just count the unique ets (can be zero)
            f = real(size(et_vec), wp)

            istat = 0 ! no problems

            write(*,*) '======================'
            write(*,*) ' Latest f: ', f
            write(*,*) '======================'
            write(iunit,'(A20,1X,2(E30.16,1X),I8)') step_config, x, int(f)

        end subroutine fcn