Compute the gradient of the solver function (Jacobian matrix). Dense version.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(nlesolver_type), | intent(inout) | :: | me | |||
| real(kind=wp), | intent(in), | dimension(:) | :: | x | ||
| real(kind=wp), | intent(out), | dimension(:,:) | :: | g |
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