export_trajectory_json_file Subroutine

public subroutine export_trajectory_json_file(me, filename, only_first_rev)

Export the trajectory JSON file.

Note

It is assumed that all the data is present in the segments needed to propagate.

Type Bound

mission_type

Arguments

Type IntentOptional Attributes Name
class(mission_type), intent(inout) :: me
character(len=*), intent(in) :: filename

plot file name [without extension]

logical, intent(in), optional :: only_first_rev

to only do the first rev [Default is False]


Calls

proc~~export_trajectory_json_file~~CallsGraph proc~export_trajectory_json_file mission_type%export_trajectory_json_file 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~propagate_segment segment%propagate_segment proc~export_trajectory_json_file->proc~propagate_segment proc~segment_trajectory_json segment_trajectory_json proc~export_trajectory_json_file->proc~segment_trajectory_json add add proc~export_rp_ra_json_file->add destroy destroy proc~export_rp_ra_json_file->destroy evaluate evaluate proc~export_rp_ra_json_file->evaluate initialize initialize proc~export_rp_ra_json_file->initialize solve solve proc~export_rp_ra_json_file->solve 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~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~add_digits add_digits proc~add_int->proc~add_digits proc~add_real_array->proc~add_str proc~add_real_list text_buffer%add_real_list proc~add_real_array->proc~add_real_list proc~reserve text_buffer%reserve proc~add_real_array->proc~reserve proc~add_str->proc~reserve proc~add_digits->proc~add_str proc~add_real_list->proc~add_str proc~add_real_list->proc~reserve proc~add_real text_buffer%add_real proc~add_real_list->proc~add_real 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~two_prod two_prod proc~scaled->proc~two_prod proc~split split proc~two_prod->proc~split

Called by

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

Source Code

    subroutine export_trajectory_json_file(me,filename,only_first_rev)

    implicit none

    class(mission_type),intent(inout) :: me
    character(len=*),intent(in) :: filename !! plot file name [without extension]
    logical,intent(in),optional :: only_first_rev  !! to only do the first rev [Default is False]

    integer,parameter :: block_size = 512 !! number of segments formatted at a time (in parallel)

    integer :: iseg  !! segment number counter
    integer :: nsegs_to_plot !! number of segments to plot
    real(wp),dimension(:),allocatable :: rdot, rmag
    logical :: accumulate_rdot !! to generate the rdot/rp/ra file
    real(wp),dimension(:),allocatable :: cumulative_et,cumulative_rdot,cumulative_r
    type(text_buffer),dimension(:),allocatable :: buf !! the JSON text of a block of segments
    integer :: iunit, istat, i1, i2

    write(*,'(A)') ' * Export the trajectory JSON file.'

    ! optional arguments:
    nsegs_to_plot = size(me%segs) ! default export all the segments
    if (present(only_first_rev)) then
        if (only_first_rev) nsegs_to_plot = 8 ! only the first rev (8 segments)
    end if
    accumulate_rdot = me%generate_rp_ra_file

    do iseg = 1, nsegs_to_plot
        call destroy_traj(iseg)
    end do

    !====================================
    ! now propagate the segments:
!$OMP PARALLEL DO    !...FIRSTPRIVATE(me)
    do iseg = 1, nsegs_to_plot
        call me%segs(iseg)%propagate(mode=2)  ! [export points]
    end do
!$OMP END PARALLEL DO
    !====================================

    if (accumulate_rdot) then
        allocate(cumulative_et(0))
        allocate(cumulative_rdot(0))
        allocate(cumulative_r(0))
        do iseg = 1, nsegs_to_plot
            associate (seg => me%segs(iseg))
                call compute_rdot_vecs(seg%traj_inertial%x,seg%traj_inertial%y,seg%traj_inertial%z,&
                                       seg%traj_inertial%vx,seg%traj_inertial%vy,seg%traj_inertial%vz,&
                                       rmag, rdot)
                call append_traj_to_arrays(seg)
            end associate
        end do
        call me%export_rp_ra_json_file(cumulative_et, cumulative_r, cumulative_rdot, filename)
    end if

    ! write the JSON file directly (an array of segments). json-fortran is not used here,
    ! because building and printing the JSON structure is slow for large files (millions of
    ! numbers). the segments are formatted in parallel, a block at a time, and written in order.
    open(newunit=iunit, file=trim(filename)//'.json', access='stream', form='unformatted', &
         status='replace', iostat=istat)
    if (istat/=0) error stop 'error opening trajectory JSON file.'
    write(iunit) '{'//new_line('a')//'  "segs": ['//new_line('a')
    allocate(buf(min(block_size, nsegs_to_plot)))
    do i1 = 1, nsegs_to_plot, block_size
        i2 = min(i1 + block_size - 1, nsegs_to_plot)
!$OMP PARALLEL DO
        do iseg = i1, i2
            call segment_trajectory_json(me%segs(iseg), iseg, iseg==nsegs_to_plot, buf(iseg-i1+1))
        end do
!$OMP END PARALLEL DO
        do iseg = i1, i2
            associate (b => buf(iseg-i1+1))
                write(iunit) b%s(1:b%n)
            end associate
        end do
    end do
    write(iunit) '  ]'//new_line('a')//'}'//new_line('a')
    close(iunit)

    do iseg = 1, nsegs_to_plot
        call destroy_traj(iseg)
    end do

    contains

        subroutine destroy_traj(iseg)
            integer,intent(in) :: iseg !! segment number
            call me%segs(iseg)%traj_inertial%destroy()
            call me%segs(iseg)%traj_rotating%destroy()
            call me%segs(iseg)%traj_se_rotating%destroy()
        end subroutine destroy_traj

        subroutine append_traj_to_arrays(seg)
            type(segment),intent(in) :: seg

            integer :: n, i

            n = size(seg%traj_inertial%et)
            if (seg%traj_inertial%et(1)<=seg%traj_inertial%et(2)) then
                ! forward propagated segment
                cumulative_et   = [cumulative_et,   seg%traj_inertial%et(1:n-1)]
                cumulative_rdot = [cumulative_rdot, rdot(1:n-1)]
                cumulative_r    = [cumulative_r,    rmag(1:n-1)]
            else
                ! some of the segments are propagated backwards, so we have to reverse them
                ! [note this is very inefficient... need to fix this]  -TODO
                cumulative_et   = [cumulative_et,   seg%traj_inertial%et(n:2:-1)]
                cumulative_rdot = [cumulative_rdot, rdot(n:2:-1)]
                cumulative_r    = [cumulative_r,    rmag(n:2:-1)]
            end if

        end subroutine append_traj_to_arrays

    end subroutine export_trajectory_json_file