compute_jacobian_stm Subroutine

public subroutine compute_jacobian_stm(me, x, jac)

Compute the (scaled) sparse Jacobian analytically (jacobian_mode=2): each segment is propagated once with its state transition matrix (in parallel, see propagate_segment_stm), and the blocks are assembled.

Constraint block i (rows 6i-5:6i) is (rotating frame). Segment 2i-1 starts at node i, and segment 2i at node i+1; each segment's final epoch is set by the epoch of one of these two nodes (see put_x_in_segments). So block row i has nonzeros only in the 14 columns of nodes i and i+1.

The values are returned in the order of the sparsity pattern (get_sparsity_pattern).

Type Bound

mission_type

Arguments

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

opt var vector (scaled)

real(kind=wp), intent(out), dimension(:), allocatable :: jac

the nonzero Jacobian elements (scaled)


Calls

proc~~compute_jacobian_stm~~CallsGraph proc~compute_jacobian_stm mission_type%compute_jacobian_stm proc~define_problem_size mission_type%define_problem_size proc~compute_jacobian_stm->proc~define_problem_size proc~earth_moon_frame_matrices earth_moon_frame_matrices proc~compute_jacobian_stm->proc~earth_moon_frame_matrices proc~get_sparsity_pattern mission_type%get_sparsity_pattern proc~compute_jacobian_stm->proc~get_sparsity_pattern proc~propagate_segment_stm segment%propagate_segment_stm proc~compute_jacobian_stm->proc~propagate_segment_stm proc~put_x_in_segments mission_type%put_x_in_segments proc~compute_jacobian_stm->proc~put_x_in_segments proc~removed_columns mission_type%removed_columns proc~compute_jacobian_stm->proc~removed_columns 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~get_sparsity_pattern->proc~define_problem_size proc~get_sparsity_pattern->proc~removed_columns proc~propagate_segment_stm->proc~earth_moon_frame_matrices first_call first_call proc~propagate_segment_stm->first_call icrf_frame icrf_frame proc~propagate_segment_stm->icrf_frame integrate integrate proc~propagate_segment_stm->integrate proc~ballistic_derivs ballistic_derivs proc~propagate_segment_stm->proc~ballistic_derivs proc~identity_stm identity_stm proc~propagate_segment_stm->proc~identity_stm proc~set_segment_outputs segment%set_segment_outputs proc~propagate_segment_stm->proc~set_segment_outputs transform transform proc~propagate_segment_stm->transform two_body_rotating_frame two_body_rotating_frame proc~propagate_segment_stm->two_body_rotating_frame 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~removed_columns->proc~define_problem_size 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~set_segment_inputs->icrf_frame proc~set_segment_inputs->transform proc~set_segment_inputs->two_body_rotating_frame 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~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

Called by

proc~~compute_jacobian_stm~~CalledByGraph proc~compute_jacobian_stm mission_type%compute_jacobian_stm proc~equilibrate equilibrate proc~equilibrate->proc~compute_jacobian_stm proc~equilibrate_scales mission_type%equilibrate_scales proc~equilibrate_scales->proc~compute_jacobian_stm proc~eclipse_optimize eclipse_optimize proc~eclipse_optimize->proc~equilibrate proc~initialize_the_mission mission_type%initialize_the_mission proc~initialize_the_mission->proc~equilibrate_scales interface~eclipse_optimize my_solver_type%eclipse_optimize interface~eclipse_optimize->proc~eclipse_optimize proc~initialize_the_solver my_solver_type%initialize_the_solver proc~initialize_the_solver->proc~initialize_the_mission proc~halo_solver_main halo_solver_main proc~halo_solver_main->interface~eclipse_optimize proc~halo_solver_main->proc~initialize_the_solver

Source Code

    subroutine compute_jacobian_stm(me,x,jac)

    implicit none

    class(mission_type),intent(inout) :: me
    real(wp),dimension(:),intent(in) :: x  !! opt var vector (scaled)
    real(wp),dimension(:),allocatable,intent(out) :: jac  !! the nonzero Jacobian elements (scaled)

    ! for each of the 8 segments of a rev: its start node, and the node whose epoch sets its final epoch
    ! (nodes of a rev: 1 periapsis, 2 quarter, 3 apoapsis, 4 three-quarter, 5 next periapsis)
    integer,dimension(8),parameter :: start_node = [1,2,2,3,3,4,4,5]
    integer,dimension(8),parameter :: tf_node    = [1,1,3,3,3,3,5,5]

    real(wp),dimension(:,:,:),allocatable :: blk  !! (6,14,n_blocks) the nonzero blocks [unscaled]
    integer,dimension(:),allocatable :: irow, icol  !! sparsity pattern
    integer,dimension(:),allocatable :: keep  !! the full-problem column of each column
    integer,dimension(:),allocatable :: removed  !! the full-problem columns that are removed
    logical,dimension(:),allocatable :: is_removed
    real(wp),dimension(6) :: drdot  !! d(rdot)/d(initial rotating-frame state)
    real(wp),dimension(6,6) :: n_0
    real(wp),dimension(3) :: r, v
    real(wp) :: sgn, rmag
    integer :: i, k, iseg, irev, l, ib, js, ks, cs, ck, n_segs, n_full, m, row, fc, node, cc, rr

    call me%define_problem_size(n_segs=n_segs, m=m)
    call me%define_problem_size(n=n_full, full_problem=.true.)

    ! set the segment inputs, and propagate them all with their STMs:
    call me%put_x_in_segments(x)
    !$OMP PARALLEL DO
    do i = 1, n_segs
        call me%segs(i)%propagate_stm()
    end do
    !$OMP END PARALLEL DO

    ! assemble the blocks (unscaled; times in days):
    allocate(blk(6,14,4*me%n_revs))
    blk = zero
    do iseg = 1, n_segs
        irev = (iseg-1)/8           ! 0-based rev
        l    = iseg - 8*irev        ! segment in the rev (1-8)
        ib   = (iseg+1)/2           ! constraint block
        sgn  = merge(one, -one, mod(iseg,2)==0)
        js   = 4*irev + start_node(l)  ! start node
        ks   = 4*irev + tf_node(l)     ! node that sets the final epoch
        cs   = (js-ib)*7               ! column offset of the start node in the block (0 or 7)
        ck   = (ks-ib)*7               ! column offset of the final-epoch node
        associate (seg => me%segs(iseg))
            blk(:,cs+2:cs+7,ib) = blk(:,cs+2:cs+7,ib) + sgn*seg%dxf_dx0
            blk(:,cs+1,ib)      = blk(:,cs+1,ib)      + sgn*seg%dxf_dt0*day2sec
            blk(:,ck+1,ib)      = blk(:,ck+1,ib)      + sgn*seg%dxf_dtf*day2sec
        end associate
    end do

    ! the optional rdot=0 constraint (a function of the first inertial state, which
    ! is N(t0) times the rotating-frame state). [only the state columns are in the pattern]
    drdot = zero
    if (me%constrain_initial_rdot) then
        r = me%segs(1)%data%x0(1:3)
        v = me%segs(1)%data%x0(4:6)
        rmag = norm2(r)
        call earth_moon_frame_matrices(me%segs(1), me%segs(1)%data%et_ref + me%segs(1)%data%t0*day2sec, n=n_0)
        drdot = matmul([v/rmag - dot_product(r,v)*r/rmag**3, r/rmag], n_0)
    end if

    ! map each column of the problem to its full-problem column:
    removed = me%removed_columns()
    allocate(is_removed(n_full))
    is_removed = .false.
    if (size(removed)>0) is_removed(removed) = .true.
    allocate(keep(n_full - count(is_removed)))  ! (a loop: no large temporaries on the stack)
    k = 0
    do i = 1, n_full
        if (.not. is_removed(i)) then
            k = k + 1
            keep(k) = i
        end if
    end do

    ! fill the values in the sparsity pattern order, and scale them:
    call me%get_sparsity_pattern(irow,icol)
    allocate(jac(size(irow)))
    do k = 1, size(irow)
        row = irow(k)
        fc  = keep(icol(k))
        if (me%constrain_initial_rdot .and. row==m) then
            jac(k) = drdot(fc-1)  ! (columns 2-7: the first node's state)
        else
            ib   = (row-1)/6 + 1
            rr   = row - 6*(ib-1)
            node = (fc-1)/7 + 1
            cc   = fc - 7*(node-1)
            jac(k) = blk(rr, (node-ib)*7 + cc, ib)
        end if
        jac(k) = jac(k) * me%xscale(icol(k)) / me%fscale(row)
    end do

    end subroutine compute_jacobian_stm