halo_grad Subroutine

public subroutine halo_grad(me, x, g)

Compute the gradient of the solver function (Jacobian matrix). Dense version.

Arguments

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

Calls

proc~~halo_grad~~CallsGraph proc~halo_grad halo_grad cache cache proc~halo_grad->cache compute_jacobian_dense compute_jacobian_dense proc~halo_grad->compute_jacobian_dense compute_jacobian_stm compute_jacobian_stm proc~halo_grad->compute_jacobian_stm get_sparsity_pattern get_sparsity_pattern proc~halo_grad->get_sparsity_pattern proc~jacobian_is_cached jacobian_is_cached proc~halo_grad->proc~jacobian_is_cached uncache uncache proc~halo_grad->uncache

Source Code

    subroutine halo_grad(me,x,g)

    implicit none

    class(nlesolver_type),intent(inout) :: me
    real(wp),dimension(:),intent(in)     :: x
    real(wp),dimension(:,:),intent(out)  :: g

    real(wp),dimension(:,:),allocatable :: jac !! the jacobian matrix returned by `numdiff`
        ! ...note: need to modify so it doesn't
        !          have to return an allocatable array
    real(wp),dimension(:),allocatable :: jac_sparse !! for `jacobian_mode=2`
    integer,dimension(:),allocatable :: irow, icol  !! for `jacobian_mode=2`

    integer :: i  !! seg number counter

    select type (me)
    class is (my_solver_type)

        ! first let's cache all the segment data:
        do i=1,size(me%mission%segs)
            call me%mission%segs(i)%cache()
        end do

        if (jacobian_is_cached(me%mission,x)) then
            jac_sparse = me%mission%jac_cache  ! (from the function evaluation at this x)
        else if (me%mission%jacobian_mode==2) then
            ! analytic (sparse), scattered into the dense matrix:
            call me%mission%compute_jacobian_stm(x,jac_sparse)
        end if
        if (me%mission%jacobian_mode==2) then
            call me%mission%get_sparsity_pattern(irow,icol)
            g = zero
            do i = 1, size(irow)
                g(irow(i),icol(i)) = jac_sparse(i)
            end do
        else
            ! use numdiff to compute the jacobian matrix (dense version)
            call me%mission%compute_jacobian_dense(x,jac)
            g = jac
        end if

        ! restore data just in case:
        do i=1,size(me%mission%segs)
            call me%mission%segs(i)%uncache()
        end do

    class default
        error stop 'invalid class in halo_grad'
    end select

    end subroutine halo_grad