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 | Intent | Optional | 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) |
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