get_x_from_json_file Subroutine

public subroutine get_x_from_json_file(me, x)

Read the JSON solution file and put the x vector in the mission. Can be used to restart a solution from a previous run (e.g., with different settings, as long as the fundamental problem isn't changed).

Note

No error checking here to make sure the file is consistent with the current mission!

Type Bound

mission_type

Arguments

Type IntentOptional Attributes Name
class(mission_type), intent(inout) :: me
real(kind=wp), intent(out), dimension(:), allocatable :: x

scaled x vector


Calls

proc~~get_x_from_json_file~~CallsGraph proc~get_x_from_json_file mission_type%get_x_from_json_file 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

Called by

proc~~get_x_from_json_file~~CalledByGraph proc~get_x_from_json_file mission_type%get_x_from_json_file proc~halo_solver_main halo_solver_main proc~halo_solver_main->proc~get_x_from_json_file

Source Code

    subroutine get_x_from_json_file(me,x)

    class(mission_type),intent(inout) :: me
    real(wp),dimension(:),intent(out),allocatable :: x !! scaled `x` vector

    real(wp),dimension(:),allocatable :: xscale !! scale factors
    type(json_core) :: json
    type(json_file) :: f
    type(json_value),pointer :: xvec, p_element
    integer :: n_children, i
    logical :: found

    if (allocated(me%initial_guess_from_file)) then

        ! read this file:
        ! {
        ! "xvec": [
        !   {
        !     "i": 1,
        !     "label": "SEG1 T0 (day)",
        !     "value": -0.96866230153337847E-3,
        !     "scale": 0.1E+1
        !   },
        !   ...

        call f%load(me%initial_guess_from_file)
        call f%get('xvec',xvec,found)
        if (.not. found) error stop 'invalid JSON solution file'
        call json%info(xvec,n_children=n_children) ! get size of x
        allocate(x(n_children))
        allocate(xscale(n_children))
        !TODO: should verify size of x compared to current problem.
        !      really should check that all the vars are the same
        ! get each element:
        do i = 1, n_children
            call json%get_child(xvec, i, p_element, found)
            call json%get(p_element,'value',x(i), found)
            if (.not. found) error stop 'could not find value in json file'
            call json%get(p_element,'scale',xscale(i), found)
            if (.not. found) error stop 'could not find scale in json file'
        end do
        x = x / xscale  ! return scaled vector
    else
        error stop 'error: the initial_guess_from_file has not been initialized'
    end if

    end subroutine get_x_from_json_file