module JU_xJU_yJU_z6

    use common, only : wp
  implicit none

contains

  !> 6th order accurate summation-by-parts finite difference operation of the elastic wave equation on a curvilinear grid
  !> Compute partial derivatives for velocities u1, u2, u3 in non-conservative form
  !> Compute partial derivatives for stress u4, u5, u6, u7, u8, u9 in conservative form

  subroutine JJU_x6(x, y, z, F, G, X_x, Ju_x)

    !=================================================================================================
    !
        use datatypes, only: block_fields, block_grid_t

    implicit none

    integer, intent(in) :: x, y, z                      ! indices in the tranformed cordinate
    type(block_fields), intent(in):: F
    type(block_grid_t), intent(in) :: G
    real(kind = wp), dimension(:), intent(out) :: Ju_x            ! metric coefficients

    real(kind = wp), dimension(:,:,:,:), allocatable, intent(in) :: X_x   ! the grid
    real(kind = wp), dimension(:), allocatable, save :: Jq_xU, Jr_xU, Js_xU            ! work array
    real(kind = wp) :: hx, hy, hz                      ! spatial steps discertizing the unit cube
    integer :: n, nx, ny, nz

    nx = G%C%nq
    ny = G%C%nr
    nz = G%C%ns

    n = size(F%F, 4)

    hx = G%hq
    hy = G%hr
    hz = G%hs

    ! work arrays:
    if (.not. allocated(Jq_xU)) allocate(Jq_xU(n))
    if (.not. allocated(Jr_xU)) allocate(Jr_xU(n))
    if (.not. allocated(Js_xU)) allocate(Js_xU(n))

    !Ju_x = 0.0_wp*Ju_x

    Jq_xU = 0.0_wp*Jq_xU

    ! Compute: (Jq_xU)_q or (Jq_yU)_q or (Jq_zU)_q for stresses
    ! Compute: q_xU_q or q_yU_q or q_zU_q for displacements
    !=========================================================================================================================
    ! Interior approximation: Standard central finite difference approximation

!print *, 'FD'
    if ((x .ge. 7) .and. (x .le. nx-6)) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(-1.0_wp/60.0_wp*F%F(x-3, y, z, 1:3)&
            + 0.15_wp*F%F(x-2, y, z, 1:3)&
            - 0.75_wp*F%F(x-1, y, z, 1:3)&
            + 0.75_wp*F%F(x+1, y, z, 1:3)&
            - 0.15_wp*F%F(x+2, y, z, 1:3)&
            + 1.0_wp/60.0_wp*F%F(x+3, y, z, 1:3))

       Jq_xU(4:n) = -1.0_wp/60.0_wp*F%F(x-3, y, z, 4:n)*G%J(x-3, y, z)*X_x(x-3, y, z, 1) &
            + 0.15_wp*F%F(x-2, y, z, 4:n)*G%J(x-2, y, z)*X_x(x-2, y, z, 1) &
            - 0.75_wp*F%F(x-1, y, z, 4:n)*G%J(x-1, y, z)*X_x(x-1, y, z, 1) &
            + 0.75_wp*F%F(x+1, y, z, 4:n)*G%J(x+1, y, z)*X_x(x+1, y, z, 1) &
            - 0.15_wp*F%F(x+2, y, z, 4:n)*G%J(x+2, y, z)*X_x(x+2, y, z, 1) &
            + 1.0_wp/60.0_wp*F%F(x+3, y, z, 4:n)*G%J(x+3, y, z)*X_x(x+3, y, z, 1)
    end if

    ! Special onesided treatments of boundaries

    !=========================================================================================================================
    ! Left boundary closure in x-directyion
    if (x .eq. 1) then
       Jq_xU(1:3) =  X_x(1, y, z, 1)*(0.036577936277544_wp*F%F(6, y, z, 1:3) &
            + 0.104488069284042_wp*F%F(5, y, z, 1:3) &
            - 0.450398306578272_wp*F%F(4, y, z, 1:3) &
            - 0.141512858744873_wp*F%F(3, y, z, 1:3) &
            + 2.033378678700676_wp*F%F(2, y, z, 1:3) &
            - 1.582533518939116_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) =  0.036577936277544_wp*F%F(6, y, z, 4:n)*G%J(6, y, z)*X_x(6, y, z, 1) &
            + 0.104488069284042_wp*F%F(5, y, z, 4:n)*G%J(5, y, z)*X_x(5, y, z, 1) &
            - 0.450398306578272_wp*F%F(4, y, z, 4:n)*G%J(4, y, z)*X_x(4, y, z, 1) &
            - 0.141512858744873_wp*F%F(3, y, z, 4:n)*G%J(3, y, z)*X_x(3, y, z, 1) &
            + 2.033378678700676_wp*F%F(2, y, z, 4:n)*G%J(2, y, z)*X_x(2, y, z, 1) &
            - 1.582533518939116_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    if (x .eq. 2) then
       Jq_xU(1:3) = X_x(2, y, z, 1)*(-0.014903449191300_wp*F%F(6, y, z, 1:3) &
            - 0.069112065532624_wp*F%F(5, y, z, 1:3) &
            + 0.258816087376832_wp*F%F(4, y, z, 1:3) &
            + 0.287258622978251_wp*F%F(3, y, z, 1:3) &
            - 0.462059195631158_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) = -0.014903449191300_wp*F%F(6, y, z, 4:n)*G%J(6, y, z)*X_x(6, y, z, 1) &
            - 0.069112065532624_wp*F%F(5, y, z, 4:n)*G%J(5, y, z)*X_x(5, y, z, 1) &
            + 0.258816087376832_wp*F%F(4, y, z, 4:n)*G%J(4, y, z)*X_x(4, y, z, 1) &
            + 0.287258622978251_wp*F%F(3, y, z, 4:n)*G%J(3, y, z)*X_x(3, y, z, 1) &
            - 0.462059195631158_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    if (x .eq. 3) then
       Jq_xU(1:3) =X_x(3, y, z, 1)*(-0.018129342917256_wp*F%F(6, y, z, 1:3) &
            - 0.022902190275815_wp*F%F(5, y, z, 1:3) &
            + 0.606235523609147_wp*F%F(4, y, z, 1:3) &
            - 0.636451095137907_wp*F%F(2, y, z, 1:3) &
            + 0.071247104721830_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) = -0.018129342917256_wp*F%F(6, y, z, 4:n)*G%J(6, y, z)*X_x(6, y, z, 1) &
            - 0.022902190275815_wp*F%F(5, y, z, 4:n)*G%J(5, y, z)*X_x(5, y, z, 1) &
            + 0.606235523609147_wp*F%F(4, y, z, 4:n)*G%J(4, y, z)*X_x(4, y, z, 1) &
            - 0.636451095137907_wp*F%F(2, y, z, 4:n)*G%J(2, y, z)*X_x(2, y, z, 1) &
            + 0.071247104721830_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    if (x .eq. 4) then
       Jq_xU(1:3) =   X_x(4, y, z, 1)*(0.013435342414630*F%F(7, y, z, 1:3) &
            - 0.051642265516119_wp*F%F(6, y, z, 1:3)&
            + 0.520262285050482_wp*F%F(5, y, z, 1:3)&
            - 0.306681191361148_wp*F%F(3, y, z, 1:3)&
            - 0.290087484386815_wp*F%F(2, y, z, 1:3)&
            + 0.114713313798970_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) =  0.013435342414630*F%F(7, y, z, 4:n)*G%J(7, y, z)*X_x(7, y, z, 1) &
            - 0.051642265516119_wp*F%F(6, y, z, 4:n)*G%J(6, y, z)*X_x(6, y, z, 1) &
            + 0.520262285050482_wp*F%F(5, y, z, 4:n)*G%J(5, y, z)*X_x(5, y, z, 1) &
            - 0.306681191361148_wp*F%F(3, y, z, 4:n)*G%J(3, y, z)*X_x(3, y, z, 1) &
            - 0.290087484386815_wp*F%F(2, y, z, 4:n)*G%J(2, y, z)*X_x(2, y, z, 1) &
            + 0.114713313798970_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    if (x .eq. 5) then
       Jq_xU(1:3) = X_x(5, y, z, 1)*(0.018281071473911_wp*F%F(8, y, z, 1:3)&
            - 0.164529643265203_wp*F%F(7, y, z, 1:3) &
            + 0.769199413962647_wp*F%F(6, y, z, 1:3)&
            - 0.707905442575989_wp*F%F(4, y, z, 1:3)&
            + 0.015764336127392_wp*F%F(3, y, z, 1:3)&
            + 0.105400944933782_wp*F%F(2, y, z, 1:3)&
            - 0.036210680656541_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) = 0.018281071473911_wp*F%F(8, y, z, 4:n)*G%J(8, y, z)*X_x(8, y, z, 1) &
            - 0.164529643265203_wp*F%F(7, y, z, 4:n)*G%J(7, y, z)*X_x(7, y, z, 1) &
            + 0.769199413962647_wp*F%F(6, y, z, 4:n)*G%J(6, y, z)*X_x(6, y, z, 1) &
            - 0.707905442575989_wp*F%F(4, y, z, 4:n)*G%J(4, y, z)*X_x(4, y, z, 1) &
            + 0.015764336127392_wp*F%F(3, y, z, 4:n)*G%J(3, y, z)*X_x(3, y, z, 1) &
            + 0.105400944933782_wp*F%F(2, y, z, 4:n)*G%J(2, y, z)*X_x(2, y, z, 1) &
            - 0.036210680656541_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    if (x .eq. 6) then
       Jq_xU(1:3) = X_x(6, y, z, 1)*(0.016437980868017_wp*F%F(9, y, z, 1:3)&
            - 0.147941827812150_wp*F%F(8, y, z, 1:3) &
            + 0.739709139060752_wp*F%F(7, y, z, 1:3)&
            - 0.691649024426814_wp*F%F(5, y, z, 1:3)&
            + 0.063183694641876_wp*F%F(4, y, z, 1:3)&
            + 0.011220896474665_wp*F%F(3, y, z, 1:3)&
            + 0.020437334208704_wp*F%F(2, y, z, 1:3)&
            - 0.011398193015050_wp*F%F(1, y, z, 1:3))

       Jq_xU(4:n) = 0.016437980868017_wp*F%F(9, y, z, 4:n)*G%J(9, y, z)*X_x(9, y, z, 1) &
            - 0.147941827812150_wp*F%F(8, y, z, 4:n)*G%J(8, y, z)*X_x(8, y, z, 1) &
            + 0.739709139060752_wp*F%F(7, y, z, 4:n)*G%J(7, y, z)*X_x(7, y, z, 1) &
            - 0.691649024426814_wp*F%F(5, y, z, 4:n)*G%J(5, y, z)*X_x(5, y, z, 1) &
            + 0.063183694641876_wp*F%F(4, y, z, 4:n)*G%J(4, y, z)*X_x(4, y, z, 1) &
            + 0.011220896474665_wp*F%F(3, y, z, 4:n)*G%J(3, y, z)*X_x(3, y, z, 1) &
            + 0.020437334208704_wp*F%F(2, y, z, 4:n)*G%J(2, y, z)*X_x(2, y, z, 1) &
            - 0.011398193015050_wp*F%F(1, y, z, 4:n)*G%J(1, y, z)*X_x(1, y, z, 1)

    end if

    !=========================================================================================================================
    ! Right boundary closure in x-directyion
    if (x .eq. nx-5) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(-0.016437980868017_wp*F%F(nx-8, y, z, 1:3)&
            + 0.147941827812150_wp*F%F(nx-7, y, z, 1:3)&
            - 0.739709139060752_wp*F%F(nx-6, y, z, 1:3)&
            + 0.691649024426814_wp*F%F(nx-4, y, z, 1:3)&
            - 0.063183694641876_wp*F%F(nx-3, y, z, 1:3)&
            - 0.011220896474665_wp*F%F(nx-2, y, z, 1:3)&
            - 0.020437334208704_wp*F%F(nx-1, y, z, 1:3)&
            + 0.011398193015050_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = - 0.016437980868017_wp*F%F(nx-8, y, z, 4:n)*G%J(nx-8, y, z)*X_x(nx-8, y, z, 1) &
            + 0.147941827812150_wp*F%F(nx-7, y, z, 4:n)*G%J(nx-7, y, z)*X_x(nx-7, y, z, 1) &
            - 0.739709139060752_wp*F%F(nx-6, y, z, 4:n)*G%J(nx-6, y, z)*X_x(nx-6, y, z, 1) &
            + 0.691649024426814_wp*F%F(nx-4, y, z, 4:n)*G%J(nx-4, y, z)*X_x(nx-4, y, z, 1) &
            - 0.063183694641876_wp*F%F(nx-3, y, z, 4:n)*G%J(nx-3, y, z)*X_x(nx-3, y, z, 1) &
            - 0.011220896474665_wp*F%F(nx-2, y, z, 4:n)*G%J(nx-2, y, z)*X_x(nx-2, y, z, 1) &
            - 0.020437334208704_wp*F%F(nx-1, y, z, 4:n)*G%J(nx-1, y, z)*X_x(nx-1, y, z, 1) &
            + 0.011398193015050_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)

    end if

    if (x .eq. nx-4) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(-0.018281071473911_wp*F%F(nx-7, y, z, 1:3)&
            + 0.164529643265203_wp*F%F(nx-6, y, z, 1:3)&
            - 0.769199413962647_wp*F%F(nx-5, y, z, 1:3)&
            + 0.707905442575989_wp*F%F(nx-3, y, z, 1:3)&
            - 0.015764336127392_wp*F%F(nx-2, y, z, 1:3)&
            - 0.105400944933782_wp*F%F(nx-1, y, z, 1:3)&
            + 0.036210680656541_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = - 0.018281071473911_wp*F%F(nx-7, y, z, 4:n)*G%J(nx-7, y, z)*X_x(nx-7, y, z, 1) &
            + 0.164529643265203_wp*F%F(nx-6, y, z, 4:n)*G%J(nx-6, y, z)*X_x(nx-6, y, z, 1) &
            - 0.769199413962647_wp*F%F(nx-5, y, z, 4:n)*G%J(nx-5, y, z)*X_x(nx-5, y, z, 1) &
            + 0.707905442575989_wp*F%F(nx-3, y, z, 4:n)*G%J(nx-3, y, z)*X_x(nx-3, y, z, 1) &
            - 0.015764336127392_wp*F%F(nx-2, y, z, 4:n)*G%J(nx-2, y, z)*X_x(nx-2, y, z, 1) &
            - 0.105400944933782_wp*F%F(nx-1, y, z, 4:n)*G%J(nx-1, y, z)*X_x(nx-1, y, z, 1) &
            + 0.036210680656541_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)

    end if

    if (x .eq. nx-3) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(-0.013435342414630*F%F(nx-6, y, z, 1:3)&
            + 0.051642265516119_wp*F%F(nx-5, y, z, 1:3)&
            - 0.520262285050482_wp*F%F(nx-4, y, z, 1:3)&
            + 0.306681191361148_wp*F%F(nx-2, y, z, 1:3)&
            + 0.290087484386815_wp*F%F(nx-1, y, z, 1:3)&
            - 0.114713313798970_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = - 0.013435342414630*F%F(nx-6, y, z, 4:n)*G%J(nx-6, y, z)*X_x(nx-6, y, z, 1) &
            + 0.051642265516119_wp*F%F(nx-5, y, z, 4:n)*G%J(nx-5, y, z)*X_x(nx-5, y, z, 1) &
            - 0.520262285050482_wp*F%F(nx-4, y, z, 4:n)*G%J(nx-4, y, z)*X_x(nx-4, y, z, 1) &
            + 0.306681191361148_wp*F%F(nx-2, y, z, 4:n)*G%J(nx-2, y, z)*X_x(nx-2, y, z, 1) &
            + 0.290087484386815_wp*F%F(nx-1, y, z, 4:n)*G%J(nx-1, y, z)*X_x(nx-1, y, z, 1) &
            - 0.114713313798970_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)

    end if

    if (x .eq. nx-2) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(0.018129342917256_wp*F%F(nx-5, y, z, 1:3)&
            + 0.022902190275815_wp*F%F(nx-4, y, z, 1:3)&
            - 0.606235523609147_wp*F%F(nx-3, y, z, 1:3)&
            + 0.636451095137907_wp*F%F(nx-1, y, z, 1:3) &
            - 0.071247104721830_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = 0.018129342917256_wp*F%F(nx-5, y, z, 4:n)*G%J(nx-5, y, z)*X_x(nx-5, y, z, 1) &
            + 0.022902190275815_wp*F%F(nx-4, y, z, 4:n)*G%J(nx-4, y, z)*X_x(nx-4, y, z, 1) &
            - 0.606235523609147_wp*F%F(nx-3, y, z, 4:n)*G%J(nx-3, y, z)*X_x(nx-3, y, z, 1) &
            + 0.636451095137907_wp*F%F(nx-1, y, z, 4:n)*G%J(nx-1, y, z)*X_x(nx-1, y, z, 1) &
            - 0.071247104721830_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)
    end if

    if (x .eq. nx-1) then
       Jq_xU(1:3) = X_x(x, y, z, 1)*(0.014903449191300_wp*F%F(nx-5, y, z, 1:3)&
            + 0.069112065532624_wp*F%F(nx-4, y, z, 1:3)&
            - 0.258816087376832_wp*F%F(nx-3, y, z, 1:3)&
            - 0.287258622978251_wp*F%F(nx-2, y, z, 1:3)&
            + 0.462059195631158_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = 0.014903449191300_wp*F%F(nx-5, y, z, 4:n)*G%J(nx-5, y, z)*X_x(nx-5, y, z, 1) &
            + 0.069112065532624_wp*F%F(nx-4, y, z, 4:n)*G%J(nx-4, y, z)*X_x(nx-4, y, z, 1) &
            - 0.258816087376832_wp*F%F(nx-3, y, z, 4:n)*G%J(nx-3, y, z)*X_x(nx-3, y, z, 1) &
            - 0.287258622978251_wp*F%F(nx-2, y, z, 4:n)*G%J(nx-2, y, z)*X_x(nx-2, y, z, 1) &
            + 0.462059195631158_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)
    end if

    if (x .eq. nx) then
       Jq_xU(1:3) =  X_x(x, y, z, 1)*(-0.036577936277544_wp*F%F(nx-5, y, z, 1:3)&
            - 0.104488069284042_wp*F%F(nx-4, y, z, 1:3)&
            + 0.450398306578272_wp*F%F(nx-3, y, z, 1:3)&
            + 0.141512858744873_wp*F%F(nx-2, y, z, 1:3)&
            - 2.033378678700676_wp*F%F(nx-1, y, z, 1:3)&
            + 1.582533518939116_wp*F%F(nx, y, z, 1:3))

       Jq_xU(4:n) = - 0.036577936277544_wp*F%F(nx-5, y, z, 4:n)*G%J(nx-5, y, z)*X_x(nx-5, y, z, 1) &
            - 0.104488069284042_wp*F%F(nx-4, y, z, 4:n)*G%J(nx-4, y, z)*X_x(nx-4, y, z, 1) &
            + 0.450398306578272_wp*F%F(nx-3, y, z, 4:n)*G%J(nx-3, y, z)*X_x(nx-3, y, z, 1) &
            + 0.141512858744873_wp*F%F(nx-2, y, z, 4:n)*G%J(nx-2, y, z)*X_x(nx-2, y, z, 1) &
            - 2.033378678700676_wp*F%F(nx-1, y, z, 4:n)*G%J(nx-1, y, z)*X_x(nx-1, y, z, 1) &
            + 1.582533518939116_wp*F%F(nx, y, z, 4:n)*G%J(nx, y, z)*X_x(nx, y, z, 1)

    end if

    !=================================================================

    Jr_xU = 0.0_wp*Jr_xU

    !implicit none

    if (y .eq. 1) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(0.036577936277544_wp*F%F(x, 6, z, 1:3)&
            + 0.104488069284042_wp*F%F(x, 5, z, 1:3)&
            - 0.450398306578272_wp*F%F(x, 4, z, 1:3)&
            - 0.141512858744873_wp*F%F(x, 3, z, 1:3)&
            + 2.033378678700676_wp*F%F(x, 2, z, 1:3)&
            - 1.582533518939116_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) =  0.036577936277544_wp*F%F(x, 6, z, 4:n)*G%J(x, 6, z)*X_x(x, 6, z, 2) &
            + 0.104488069284042_wp*F%F(x, 5, z, 4:n)*G%J(x, 5, z)*X_x(x, 5, z, 2) &
            - 0.450398306578272_wp*F%F(x, 4, z, 4:n)*G%J(x, 4, z)*X_x(x, 4, z, 2) &
            - 0.141512858744873_wp*F%F(x, 3, z, 4:n)*G%J(x, 3, z)*X_x(x, 3, z, 2) &
            + 2.033378678700676_wp*F%F(x, 2, z, 4:n)*G%J(x, 2, z)*X_x(x, 2, z, 2) &
            - 1.582533518939116_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq. 2) then
       Jr_xU(1:3) =X_x(x, y, z, 2)*(-0.014903449191300_wp*F%F(x, 6, z, 1:3)&
            - 0.069112065532624_wp*F%F(x, 5, z, 1:3)&
            + 0.258816087376832_wp*F%F(x, 4, z, 1:3)&
            + 0.287258622978251_wp*F%F(x, 3, z, 1:3)&
            - 0.462059195631158_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) = -0.014903449191300_wp*F%F(x, 6, z, 4:n)*G%J(x, 6, z)*X_x(x, 6, z, 2) &
            - 0.069112065532624_wp*F%F(x, 5, z, 4:n)*G%J(x, 5, z)*X_x(x, 5, z, 2) &
            + 0.258816087376832_wp*F%F(x, 4, z, 4:n)*G%J(x, 4, z)*X_x(x, 4, z, 2) &
            + 0.287258622978251_wp*F%F(x, 3, z, 4:n)*G%J(x, 3, z)*X_x(x, 3, z, 2) &
            - 0.462059195631158_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq. 3) then
       Jr_xU(1:3) =  X_x(x, y, z, 2)*(-0.018129342917256_wp*F%F(x, 6, z, 1:3)&
            - 0.022902190275815_wp*F%F(x, 5, z, 1:3)&
            + 0.606235523609147_wp*F%F(x, 4, z, 1:3)&
            - 0.636451095137907_wp*F%F(x, 2, z, 1:3)&
            + 0.071247104721830_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) =  -0.018129342917256_wp*F%F(x, 6, z, 4:n)*G%J(x, 6, z)*X_x(x, 6, z, 2) &
            - 0.022902190275815_wp*F%F(x, 5, z, 4:n)*G%J(x, 5, z)*X_x(x, 5, z, 2) &
            + 0.606235523609147_wp*F%F(x, 4, z, 4:n)*G%J(x, 4, z)*X_x(x, 4, z, 2) &
            - 0.636451095137907_wp*F%F(x, 2, z, 4:n)*G%J(x, 2, z)*X_x(x, 2, z, 2) &
            + 0.071247104721830_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq. 4) then
       Jr_xU(1:3) =   X_x(x, y, z, 2)*(0.013435342414630*F%F(x, 7, z, 1:3)&
            - 0.051642265516119_wp*F%F(x, 6, z, 1:3)&
            + 0.520262285050482_wp*F%F(x, 5, z, 1:3)&
            - 0.306681191361148_wp*F%F(x, 3, z, 1:3)&
            - 0.290087484386815_wp*F%F(x, 2, z, 1:3)&
            + 0.114713313798970_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) =   0.013435342414630*F%F(x, 7, z, 4:n)*G%J(x, 7, z)*X_x(x, 7, z, 2) &
            - 0.051642265516119_wp*F%F(x, 6, z, 4:n)*G%J(x, 6, z)*X_x(x, 6, z, 2) &
            + 0.520262285050482_wp*F%F(x, 5, z, 4:n)*G%J(x, 5, z)*X_x(x, 5, z, 2) &
            - 0.306681191361148_wp*F%F(x, 3, z, 4:n)*G%J(x, 3, z)*X_x(x, 3, z, 2) &
            - 0.290087484386815_wp*F%F(x, 2, z, 4:n)*G%J(x, 2, z)*X_x(x, 2, z, 2) &
            + 0.114713313798970_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq. 5) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(0.018281071473911_wp*F%F(x, 8, z, 1:3)&
            - 0.164529643265203_wp*F%F(x, 7, z, 1:3) &
            + 0.769199413962647_wp*F%F(x, 6, z, 1:3) &
            - 0.707905442575989_wp*F%F(x, 4, z, 1:3) &
            + 0.015764336127392_wp*F%F(x, 3, z, 1:3) &
            + 0.105400944933782_wp*F%F(x, 2, z, 1:3) &
            - 0.036210680656541_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) = 0.018281071473911_wp*F%F(x, 8, z, 4:n)*G%J(x, 8, z)*X_x(x, 8, z, 2) &
            - 0.164529643265203_wp*F%F(x, 7, z, 4:n)*G%J(x, 7, z)*X_x(x, 7, z, 2) &
            + 0.769199413962647_wp*F%F(x, 6, z, 4:n)*G%J(x, 6, z)*X_x(x, 6, z, 2) &
            - 0.707905442575989_wp*F%F(x, 4, z, 4:n)*G%J(x, 4, z)*X_x(x, 4, z, 2) &
            + 0.015764336127392_wp*F%F(x, 3, z, 4:n)*G%J(x, 3, z)*X_x(x, 3, z, 2) &
            + 0.105400944933782_wp*F%F(x, 2, z, 4:n)*G%J(x, 2, z)*X_x(x, 2, z, 2) &
            - 0.036210680656541_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq. 6) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(0.016437980868017_wp*F%F(x, 9, z, 1:3)&
            - 0.147941827812150_wp*F%F(x, 8, z, 1:3)&
            + 0.739709139060752_wp*F%F(x, 7, z, 1:3)&
            - 0.691649024426814_wp*F%F(x, 5, z, 1:3)&
            + 0.063183694641876_wp*F%F(x, 4, z, 1:3)&
            + 0.011220896474665_wp*F%F(x, 3, z, 1:3)&
            + 0.020437334208704_wp*F%F(x, 2, z, 1:3)&
            - 0.011398193015050_wp*F%F(x, 1, z, 1:3))

       Jr_xU(4:n) = 0.016437980868017_wp*F%F(x, 9, z, 4:n)*G%J(x, 9, z)*X_x(x, 9, z, 2) &
            - 0.147941827812150_wp*F%F(x, 8, z, 4:n)*G%J(x, 8, z)*X_x(x, 8, z, 2) &
            + 0.739709139060752_wp*F%F(x, 7, z, 4:n)*G%J(x, 7, z)*X_x(x, 7, z, 2) &
            - 0.691649024426814_wp*F%F(x, 5, z, 4:n)*G%J(x, 5, z)*X_x(x, 5, z, 2) &
            + 0.063183694641876_wp*F%F(x, 4, z, 4:n)*G%J(x, 4, z)*X_x(x, 4, z, 2) &
            + 0.011220896474665_wp*F%F(x, 3, z, 4:n)*G%J(x, 3, z)*X_x(x, 3, z, 2) &
            + 0.020437334208704_wp*F%F(x, 2, z, 4:n)*G%J(x, 2, z)*X_x(x, 2, z, 2) &
            - 0.011398193015050_wp*F%F(x, 1, z, 4:n)*G%J(x, 1, z)*X_x(x, 1, z, 2)

    end if

    if (y .eq.  ny-5) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(-0.016437980868017_wp*F%F(x, ny-8, z, 1:3) &
            + 0.147941827812150_wp*F%F(x, ny-7, z, 1:3) &
            - 0.739709139060752_wp*F%F(x, ny-6, z, 1:3) &
            + 0.691649024426814_wp*F%F(x, ny-4, z, 1:3) &
            - 0.063183694641876_wp*F%F(x, ny-3, z, 1:3) &
            - 0.011220896474665_wp*F%F(x, ny-2, z, 1:3) &
            - 0.020437334208704_wp*F%F(x, ny-1, z, 1:3) &
            + 0.011398193015050_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n) = -0.016437980868017_wp*F%F(x, ny-8, z, 4:n)*G%J(x, ny-8, z)*X_x(x, ny-8, z, 2) &
            + 0.147941827812150_wp*F%F(x, ny-7, z, 4:n)*G%J(x, ny-7, z)*X_x(x, ny-7, z, 2) &
            - 0.739709139060752_wp*F%F(x, ny-6, z, 4:n)*G%J(x, ny-6, z)*X_x(x, ny-6, z, 2) &
            + 0.691649024426814_wp*F%F(x, ny-4, z, 4:n)*G%J(x, ny-4, z)*X_x(x, ny-4, z, 2) &
            - 0.063183694641876_wp*F%F(x, ny-3, z, 4:n)*G%J(x, ny-3, z)*X_x(x, ny-3, z, 2) &
            - 0.011220896474665_wp*F%F(x, ny-2, z, 4:n)*G%J(x, ny-2, z)*X_x(x, ny-2, z, 2) &
            - 0.020437334208704_wp*F%F(x, ny-1, z, 4:n)*G%J(x, ny-1, z)*X_x(x, ny-1, z, 2) &
            + 0.011398193015050_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)

    end if

    if (y .eq.  ny-4) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(-0.018281071473911_wp*F%F(x, ny-7, z, 1:3)&
            + 0.164529643265203_wp*F%F(x, ny-6, z, 1:3)&
            - 0.769199413962647_wp*F%F(x, ny-5, z, 1:3)&
            + 0.707905442575989_wp*F%F(x, ny-3, z, 1:3)&
            - 0.015764336127392_wp*F%F(x, ny-2, z, 1:3)&
            - 0.105400944933782_wp*F%F(x, ny-1, z, 1:3)&
            + 0.036210680656541_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n) = - 0.018281071473911_wp*F%F(x, ny-7, z, 4:n)*G%J(x, ny-7, z)*X_x(x, ny-7, z, 2) &
            + 0.164529643265203_wp*F%F(x, ny-6, z, 4:n)*G%J(x, ny-6, z)*X_x(x, ny-6, z, 2) &
            - 0.769199413962647_wp*F%F(x, ny-5, z, 4:n)*G%J(x, ny-5, z)*X_x(x, ny-5, z, 2) &
            + 0.707905442575989_wp*F%F(x, ny-3, z, 4:n)*G%J(x, ny-3, z)*X_x(x, ny-3, z, 2) &
            - 0.015764336127392_wp*F%F(x, ny-2, z, 4:n)*G%J(x, ny-2, z)*X_x(x, ny-2, z, 2) &
            - 0.105400944933782_wp*F%F(x, ny-1, z, 4:n)*G%J(x, ny-1, z)*X_x(x, ny-1, z, 2) &
            + 0.036210680656541_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)

    end if

    if (y .eq.  ny-3) then

       Jr_xU(1:3) = X_x(x, y, z, 2)*(-0.013435342414630*F%F(x, ny-6, z, 1:3)&
            + 0.051642265516119_wp*F%F(x, ny-5, z, 1:3)&
            - 0.520262285050482_wp*F%F(x, ny-4, z, 1:3)&
            + 0.306681191361148_wp*F%F(x, ny-2, z, 1:3)&
            + 0.290087484386815_wp*F%F(x, ny-1, z, 1:3)&
            - 0.114713313798970_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n) = - 0.013435342414630*F%F(x, ny-6, z, 4:n)*G%J(x, ny-6, z)*X_x(x, ny-6, z, 2) &
            + 0.051642265516119_wp*F%F(x, ny-5, z, 4:n)*G%J(x, ny-5, z)*X_x(x, ny-5, z, 2) &
            - 0.520262285050482_wp*F%F(x, ny-4, z, 4:n)*G%J(x, ny-4, z)*X_x(x, ny-4, z, 2) &
            + 0.306681191361148_wp*F%F(x, ny-2, z, 4:n)*G%J(x, ny-2, z)*X_x(x, ny-2, z, 2) &
            + 0.290087484386815_wp*F%F(x, ny-1, z, 4:n)*G%J(x, ny-1, z)*X_x(x, ny-1, z, 2) &
            - 0.114713313798970_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)

    end if

    if (y .eq.  ny-2) then

       Jr_xU(1:3) = X_x(x, y, z, 2)*(0.018129342917256_wp*F%F(x, ny-5, z, 1:3)&
            + 0.022902190275815_wp*F%F(x, ny-4, z, 1:3)&
            - 0.606235523609147_wp*F%F(x, ny-3, z, 1:3)&
            + 0.636451095137907_wp*F%F(x, ny-1, z, 1:3)&
            - 0.071247104721830_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n) = 0.018129342917256_wp*F%F(x, ny-5, z, 4:n)*G%J(x, ny-5, z)*X_x(x, ny-5, z, 2) &
            + 0.022902190275815_wp*F%F(x, ny-4, z, 4:n)*G%J(x, ny-4, z)*X_x(x, ny-4, z, 2) &
            - 0.606235523609147_wp*F%F(x, ny-3, z, 4:n)*G%J(x, ny-3, z)*X_x(x, ny-3, z, 2) &
            + 0.636451095137907_wp*F%F(x, ny-1, z, 4:n)*G%J(x, ny-1, z)*X_x(x, ny-1, z, 2) &
            - 0.071247104721830_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)

    end if

    if (y .eq.  ny-1) then
       Jr_xU(1:3) = X_x(x, y, z, 2)*(0.014903449191300_wp*F%F(x, ny-5, z, 1:3)&
            + 0.069112065532624_wp*F%F(x, ny-4, z, 1:3)&
            - 0.258816087376832_wp*F%F(x, ny-3, z, 1:3)&
            - 0.287258622978251_wp*F%F(x, ny-2, z, 1:3)&
            + 0.462059195631158_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n) = 0.014903449191300_wp*F%F(x, ny-5, z, 4:n)*G%J(x, ny-5, z)*X_x(x, ny-5, z, 2) &
            + 0.069112065532624_wp*F%F(x, ny-4, z, 4:n)*G%J(x, ny-4, z)*X_x(x, ny-4, z, 2) &
            - 0.258816087376832_wp*F%F(x, ny-3, z, 4:n)*G%J(x, ny-3, z)*X_x(x, ny-3, z, 2) &
            - 0.287258622978251_wp*F%F(x, ny-2, z, 4:n)*G%J(x, ny-2, z)*X_x(x, ny-2, z, 2) &
            + 0.462059195631158_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)
    end if

    if (y .eq.  ny) then
       Jr_xU(1:3)  = X_x(x, y, z, 2)*(- 0.036577936277544_wp*F%F(x, ny-5, z, 1:3)&
            - 0.104488069284042_wp*F%F(x, ny-4, z, 1:3)&
            + 0.450398306578272_wp*F%F(x, ny-3, z, 1:3)&
            + 0.141512858744873_wp*F%F(x, ny-2, z, 1:3)&
            - 2.033378678700676_wp*F%F(x, ny-1, z, 1:3)&
            + 1.582533518939116_wp*F%F(x, ny, z, 1:3))

       Jr_xU(4:n)  =  - 0.036577936277544_wp*F%F(x, ny-5, z, 4:n)*G%J(x, ny-5, z)*X_x(x, ny-5, z, 2) &
            - 0.104488069284042_wp*F%F(x, ny-4, z, 4:n)*G%J(x, ny-4, z)*X_x(x, ny-4, z, 2) &
            + 0.450398306578272_wp*F%F(x, ny-3, z, 4:n)*G%J(x, ny-3, z)*X_x(x, ny-3, z, 2) &
            + 0.141512858744873_wp*F%F(x, ny-2, z, 4:n)*G%J(x, ny-2, z)*X_x(x, ny-2, z, 2) &
            - 2.033378678700676_wp*F%F(x, ny-1, z, 4:n)*G%J(x, ny-1, z)*X_x(x, ny-1, z, 2) &
            + 1.582533518939116_wp*F%F(x, ny, z, 4:n)*G%J(x, ny, z)*X_x(x, ny, z, 2)

    end if

    if ((y .ge. 7) .and. (y .le. ny-6)) then

       Jr_xU(1:3) = X_x(x, y, z, 2)*(-1.0_wp/60.0_wp*F%F(x, y-3, z, 1:3)&
            + 0.15_wp*F%F(x, y-2, z, 1:3)&
            - 0.75_wp*F%F(x, y-1, z, 1:3)&
            + 0.75_wp*F%F(x, y+1, z, 1:3)&
            - 0.15_wp*F%F(x, y+2, z, 1:3)&
            + 1.0_wp/60.0_wp*F%F(x, y+3, z, 1:3))

       Jr_xU(4:n) =   -1.0_wp/60.0_wp*F%F(x, y-3, z, 4:n)*G%J(x, y-3, z)*X_x(x, y-3, z, 2) &
            + 0.15_wp*F%F(x, y-2, z, 4:n)*G%J(x, y-2, z)*X_x(x, y-2, z, 2) &
            - 0.75_wp*F%F(x, y-1, z, 4:n)*G%J(x, y-1, z)*X_x(x, y-1, z, 2) &
            + 0.75_wp*F%F(x, y+1, z, 4:n)*G%J(x, y+1, z)*X_x(x, y+1, z, 2) &
            - 0.15_wp*F%F(x, y+2, z, 4:n)*G%J(x, y+2, z)*X_x(x, y+2, z, 2) &
            + 1.0_wp/60.0_wp*F%F(x, y+3, z, 4:n)*G%J(x, y+3, z)*X_x(x, y+3, z, 2)
    end if

    !======================================================================

    Js_xU = 0.0_wp*Js_xU

    if (z .eq. 1) then
        Js_xU(1:3) = X_x(x,y,z,3)*(0.036577936277544_wp*F%F(x, y, 6, 1:3)&
            + 0.104488069284042_wp*F%F(x, y, 5, 1:3)&
            - 0.450398306578272_wp*F%F(x, y, 4, 1:3)&
            - 0.141512858744873_wp*F%F(x, y, 3, 1:3)&
            + 2.033378678700676_wp*F%F(x, y, 2, 1:3)&
            - 1.582533518939116_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n) =  0.036577936277544_wp*F%F(x, y, 6, 4:n)*G%J(x, y, 6)*X_x(x, y, 6, 3) &
            + 0.104488069284042_wp*F%F(x, y, 5, 4:n)*G%J(x, y, 5)*X_x(x, y, 5, 3) &
            - 0.450398306578272_wp*F%F(x, y, 4, 4:n)*G%J(x, y, 4)*X_x(x, y, 4, 3) &
            - 0.141512858744873_wp*F%F(x, y, 3, 4:n)*G%J(x, y, 3)*X_x(x, y, 3, 3) &
            + 2.033378678700676_wp*F%F(x, y, 2, 4:n)*G%J(x, y, 2)*X_x(x, y, 2, 3) &
            - 1.582533518939116_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)
    end if

    if (z .eq.  2) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(-0.014903449191300_wp*F%F(x, y, 6, 1:3)&
            - 0.069112065532624_wp*F%F(x, y, 5, 1:3)&
            + 0.258816087376832_wp*F%F(x, y, 4, 1:3)&
            + 0.287258622978251_wp*F%F(x, y, 3, 1:3)&
            - 0.462059195631158_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n) = -0.014903449191300_wp*F%F(x, y, 6, 4:n)*G%J(x, y, 6)*X_x(x, y, 6, 3) &
            - 0.069112065532624_wp*F%F(x, y, 5, 4:n)*G%J(x, y, 5)*X_x(x, y, 5, 3) &
            + 0.258816087376832_wp*F%F(x, y, 4, 4:n)*G%J(x, y, 4)*X_x(x, y, 4, 3) &
            + 0.287258622978251_wp*F%F(x, y, 3, 4:n)*G%J(x, y, 3)*X_x(x, y, 3, 3) &
            - 0.462059195631158_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)

    end if

    if (z .eq.  3) then
       Js_xU(1:3)  = X_x(x, y, z, 3)*(-0.018129342917256_wp*F%F(x, y, 6, 1:3)&
            - 0.022902190275815_wp*F%F(x, y, 5, 1:3)&
            + 0.606235523609147_wp*F%F(x, y, 4, 1:3)&
            - 0.636451095137907_wp*F%F(x, y, 2, 1:3)&
            + 0.071247104721830_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n)  = -0.018129342917256_wp*F%F(x, y, 6, 4:n)*G%J(x, y, 6)*X_x(x, y, 6, 3) &
            - 0.022902190275815_wp*F%F(x, y, 5, 4:n)*G%J(x, y, 5)*X_x(x, y, 5, 3) &
            + 0.606235523609147_wp*F%F(x, y, 4, 4:n)*G%J(x, y, 4)*X_x(x, y, 4, 3) &
            - 0.636451095137907_wp*F%F(x, y, 2, 4:n)*G%J(x, y, 2)*X_x(x, y, 2, 3) &
            + 0.071247104721830_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)

    end if

    if (z .eq.  4) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(0.013435342414630*F%F(x, y, 7, 1:3)&
            - 0.051642265516119_wp*F%F(x, y, 6, 1:3)&
            + 0.520262285050482_wp*F%F(x, y, 5, 1:3)&
            - 0.306681191361148_wp*F%F(x, y, 3, 1:3)&
            - 0.290087484386815_wp*F%F(x, y, 2, 1:3)&
            + 0.114713313798970_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n) =   0.013435342414630*F%F(x, y, 7, 4:n)*G%J(x, y, 7)*X_x(x, y, 7, 3) &
            - 0.051642265516119_wp*F%F(x, y, 6, 4:n)*G%J(x, y, 6)*X_x(x, y, 6, 3) &
            + 0.520262285050482_wp*F%F(x, y, 5, 4:n)*G%J(x, y, 5)*X_x(x, y, 5, 3) &
            - 0.306681191361148_wp*F%F(x, y, 3, 4:n)*G%J(x, y, 3)*X_x(x, y, 3, 3) &
            - 0.290087484386815_wp*F%F(x, y, 2, 4:n)*G%J(x, y, 2)*X_x(x, y, 2, 3) &
            + 0.114713313798970_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)

    end if

    if (z .eq.  5) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(0.018281071473911_wp*F%F(x, y, 8, 1:3)&
            - 0.164529643265203_wp*F%F(x, y, 7, 1:3)&
            + 0.769199413962647_wp*F%F(x, y, 6, 1:3)&
            - 0.707905442575989_wp*F%F(x, y, 4, 1:3)&
            + 0.015764336127392_wp*F%F(x, y, 3, 1:3)&
            + 0.105400944933782_wp*F%F(x, y, 2, 1:3)&
            - 0.036210680656541_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n) =  0.018281071473911_wp*F%F(x, y, 8, 4:n)*G%J(x, y, 8)*X_x(x, y, 8, 3) &
            - 0.164529643265203_wp*F%F(x, y, 7, 4:n)*G%J(x, y, 7)*X_x(x, y, 7, 3) &
            + 0.769199413962647_wp*F%F(x, y, 6, 4:n)*G%J(x, y, 6)*X_x(x, y, 6, 3) &
            - 0.707905442575989_wp*F%F(x, y, 4, 4:n)*G%J(x, y, 4)*X_x(x, y, 4, 3) &
            + 0.015764336127392_wp*F%F(x, y, 3, 4:n)*G%J(x, y, 3)*X_x(x, y, 3, 3) &
            + 0.105400944933782_wp*F%F(x, y, 2, 4:n)*G%J(x, y, 2)*X_x(x, y, 2, 3) &
            - 0.036210680656541_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)

    end if

    if (z .eq.  6) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(0.016437980868017_wp*F%F(x, y, 9, 1:3)&
            - 0.147941827812150_wp*F%F(x, y, 8, 1:3)&
            + 0.739709139060752_wp*F%F(x, y, 7, 1:3)&
            - 0.691649024426814_wp*F%F(x, y, 5, 1:3)&
            + 0.063183694641876_wp*F%F(x, y, 4, 1:3)&
            + 0.011220896474665_wp*F%F(x, y, 3, 1:3)&
            + 0.020437334208704_wp*F%F(x, y, 2, 1:3)&
            - 0.011398193015050_wp*F%F(x, y, 1, 1:3))

       Js_xU(4:n) =  0.016437980868017_wp*F%F(x, y, 9, 4:n)*G%J(x, y, 9)*X_x(x, y, 9, 3) &
            - 0.147941827812150_wp*F%F(x, y, 8, 4:n)*G%J(x, y, 8)*X_x(x, y, 8, 3) &
            + 0.739709139060752_wp*F%F(x, y, 7, 4:n)*G%J(x, y, 7)*X_x(x, y, 7, 3) &
            - 0.691649024426814_wp*F%F(x, y, 5, 4:n)*G%J(x, y, 5)*X_x(x, y, 5, 3) &
            + 0.063183694641876_wp*F%F(x, y, 4, 4:n)*G%J(x, y, 4)*X_x(x, y, 4, 3) &
            + 0.011220896474665_wp*F%F(x, y, 3, 4:n)*G%J(x, y, 3)*X_x(x, y, 3, 3) &
            + 0.020437334208704_wp*F%F(x, y, 2, 4:n)*G%J(x, y, 2)*X_x(x, y, 2, 3) &
            - 0.011398193015050_wp*F%F(x, y, 1, 4:n)*G%J(x, y, 1)*X_x(x, y, 1, 3)

    end if

    if (z .eq.   nz-5) then
       Js_xU(1:3) =  X_x(x, y, z, 3)*(-0.016437980868017_wp*F%F(x, y, nz-8, 1:3)&
            + 0.147941827812150_wp*F%F(x, y, nz-7, 1:3)&
            - 0.739709139060752_wp*F%F(x, y, nz-6, 1:3)&
            + 0.691649024426814_wp*F%F(x, y, nz-4, 1:3)&
            - 0.063183694641876_wp*F%F(x, y, nz-3, 1:3)&
            - 0.011220896474665_wp*F%F(x, y, nz-2, 1:3)&
            - 0.020437334208704_wp*F%F(x, y, nz-1, 1:3)&
            + 0.011398193015050_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n) = -0.016437980868017_wp*F%F(x, y, nz-8, 4:n)*G%J(x, y, nz-8)*X_x(x, y, nz-8, 3) &
            + 0.147941827812150_wp*F%F(x, y, nz-7, 4:n)*G%J(x, y, nz-7)*X_x(x, y, nz-7, 3) &
            - 0.739709139060752_wp*F%F(x, y, nz-6, 4:n)*G%J(x, y, nz-6)*X_x(x, y, nz-6, 3) &
            + 0.691649024426814_wp*F%F(x, y, nz-4, 4:n)*G%J(x, y, nz-4)*X_x(x, y, nz-4, 3) &
            - 0.063183694641876_wp*F%F(x, y, nz-3, 4:n)*G%J(x, y, nz-3)*X_x(x, y, nz-3, 3) &
            - 0.011220896474665_wp*F%F(x, y, nz-2, 4:n)*G%J(x, y, nz-2)*X_x(x, y, nz-2, 3) &
            - 0.020437334208704_wp*F%F(x, y, nz-1, 4:n)*G%J(x, y, nz-1)*X_x(x, y, nz-1, 3) &
            + 0.011398193015050_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if (z .eq. nz-4) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(-0.018281071473911_wp*F%F(x, y, nz-7, 1:3)&
            + 0.164529643265203_wp*F%F(x, y, nz-6, 1:3)&
            - 0.769199413962647_wp*F%F(x, y, nz-5, 1:3)&
            + 0.707905442575989_wp*F%F(x, y, nz-3, 1:3)&
            - 0.015764336127392_wp*F%F(x, y, nz-2, 1:3)&
            - 0.105400944933782_wp*F%F(x, y, nz-1, 1:3)&
            + 0.036210680656541_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n) = -0.018281071473911_wp*F%F(x, y, nz-7, 4:n)*G%J(x, y, nz-7)*X_x(x, y, nz-7, 3) &
            + 0.164529643265203_wp*F%F(x, y, nz-6, 4:n)*G%J(x, y, nz-6)*X_x(x, y, nz-6, 3) &
            - 0.769199413962647_wp*F%F(x, y, nz-5, 4:n)*G%J(x, y, nz-5)*X_x(x, y, nz-5, 3) &
            + 0.707905442575989_wp*F%F(x, y, nz-3, 4:n)*G%J(x, y, nz-3)*X_x(x, y, nz-3, 3) &
            - 0.015764336127392_wp*F%F(x, y, nz-2, 4:n)*G%J(x, y, nz-2)*X_x(x, y, nz-2, 3) &
            - 0.105400944933782_wp*F%F(x, y, nz-1, 4:n)*G%J(x, y, nz-1)*X_x(x, y, nz-1, 3) &
            + 0.036210680656541_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if (z .eq.   nz-3) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(-0.013435342414630*F%F(x, y, nz-6, 1:3)&
            + 0.051642265516119_wp*F%F(x, y, nz-5, 1:3)&
            - 0.520262285050482_wp*F%F(x, y, nz-4, 1:3)&
            + 0.306681191361148_wp*F%F(x, y, nz-2, 1:3)&
            + 0.290087484386815_wp*F%F(x, y, nz-1, 1:3)&
            - 0.114713313798970_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n) =  - 0.013435342414630*F%F(x, y, nz-6, 4:n)*G%J(x, y, nz-6)*X_x(x, y, nz-6, 3) &
            + 0.051642265516119_wp*F%F(x, y, nz-5, 4:n)*G%J(x, y, nz-5)*X_x(x, y, nz-5, 3) &
            - 0.520262285050482_wp*F%F(x, y, nz-4, 4:n)*G%J(x, y, nz-4)*X_x(x, y, nz-4, 3) &
            + 0.306681191361148_wp*F%F(x, y, nz-2, 4:n)*G%J(x, y, nz-2)*X_x(x, y, nz-2, 3) &
            + 0.290087484386815_wp*F%F(x, y, nz-1, 4:n)*G%J(x, y, nz-1)*X_x(x, y, nz-1, 3) &
            - 0.114713313798970_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if (z .eq.   nz-2) then

       Js_xU(1:3) = X_x(x, y, z, 3)*(0.018129342917256_wp*F%F(x, y, nz-5, 1:3)&
            + 0.022902190275815_wp*F%F(x, y, nz-4, 1:3)&
            - 0.606235523609147_wp*F%F(x, y, nz-3, 1:3)&
            + 0.636451095137907_wp*F%F(x, y, nz-1, 1:3)&
            - 0.071247104721830_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n) = 0.018129342917256_wp*F%F(x, y, nz-5, 4:n)*G%J(x, y, nz-5)*X_x(x, y, nz-5, 3) &
            + 0.022902190275815_wp*F%F(x, y, nz-4, 4:n)*G%J(x, y, nz-4)*X_x(x, y, nz-4, 3) &
            - 0.606235523609147_wp*F%F(x, y, nz-3, 4:n)*G%J(x, y, nz-3)*X_x(x, y, nz-3, 3) &
            + 0.636451095137907_wp*F%F(x, y, nz-1, 4:n)*G%J(x, y, nz-1)*X_x(x, y, nz-1, 3) &
            - 0.071247104721830_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if (z .eq.   nz-1) then

       Js_xU(1:3) = X_x(x, y, z, 3)*(0.014903449191300_wp*F%F(x, y, nz-5, 1:3)&
            + 0.069112065532624_wp*F%F(x, y, nz-4, 1:3)&
            - 0.258816087376832_wp*F%F(x, y, nz-3, 1:3)&
            - 0.287258622978251_wp*F%F(x, y, nz-2, 1:3)&
            + 0.462059195631158_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n) = 0.014903449191300_wp*F%F(x, y, nz-5, 4:n)*G%J(x, y, nz-5)*X_x(x, y, nz-5, 3) &
            + 0.069112065532624_wp*F%F(x, y, nz-4, 4:n)*G%J(x, y, nz-4)*X_x(x, y, nz-4, 3) &
            - 0.258816087376832_wp*F%F(x, y, nz-3, 4:n)*G%J(x, y, nz-3)*X_x(x, y, nz-3, 3) &
            - 0.287258622978251_wp*F%F(x, y, nz-2, 4:n)*G%J(x, y, nz-2)*X_x(x, y, nz-2, 3) &
            + 0.462059195631158_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if (z .eq.   nz) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(-0.036577936277544_wp*F%F(x, y, nz-5, 1:3)&
            - 0.104488069284042_wp*F%F(x, y, nz-4, 1:3)&
            + 0.450398306578272_wp*F%F(x, y, nz-3, 1:3)&
            + 0.141512858744873_wp*F%F(x, y, nz-2, 1:3)&
            - 2.033378678700676_wp*F%F(x, y, nz-1, 1:3)&
            + 1.582533518939116_wp*F%F(x, y, nz, 1:3))

       Js_xU(4:n)   = -0.036577936277544_wp*F%F(x, y, nz-5, 4:n)*G%J(x, y, nz-5)*X_x(x, y, nz-5, 3) &
            - 0.104488069284042_wp*F%F(x, y, nz-4, 4:n)*G%J(x, y, nz-4)*X_x(x, y, nz-4, 3) &
            + 0.450398306578272_wp*F%F(x, y, nz-3, 4:n)*G%J(x, y, nz-3)*X_x(x, y, nz-3, 3) &
            + 0.141512858744873_wp*F%F(x, y, nz-2, 4:n)*G%J(x, y, nz-2)*X_x(x, y, nz-2, 3) &
            - 2.033378678700676_wp*F%F(x, y, nz-1, 4:n)*G%J(x, y, nz-1)*X_x(x, y, nz-1, 3) &
            + 1.582533518939116_wp*F%F(x, y, nz, 4:n)*G%J(x, y, nz)*X_x(x, y, nz, 3)

    end if

    if ((z .ge. 7) .and. (z .le. nz-6)) then
       Js_xU(1:3) = X_x(x, y, z, 3)*(-1.0_wp/60.0_wp*F%F(x, y, z-3, 1:3)&
            + 0.15_wp*F%F(x, y, z-2, 1:3)&
            - 0.75_wp*F%F(x, y, z-1, 1:3)&
            + 0.75_wp*F%F(x, y, z+1, 1:3)&
            - 0.15_wp*F%F(x, y, z+2, 1:3)&
            + 1.0_wp/60.0_wp*F%F(x, y, z+3, 1:3))

       Js_xU(4:n) =  -1.0_wp/60.0_wp*F%F(x, y, z-3, 4:n)*G%J(x, y, z-3)*X_x(x, y, z-3, 3) &
            + 0.15_wp*F%F(x, y, z-2, 4:n)*G%J(x, y, z-2)*X_x(x, y, z-2, 3) &
            - 0.75_wp*F%F(x, y, z-1, 4:n)*G%J(x, y, z-1)*X_x(x, y, z-1, 3) &
            + 0.75_wp*F%F(x, y, z+1, 4:n)*G%J(x, y, z+1)*X_x(x, y, z+1, 3) &
            - 0.15_wp*F%F(x, y, z+2, 4:n)*G%J(x, y, z+2)*X_x(x, y, z+2, 3) &
            + 1.0_wp/60.0_wp*F%F(x, y, z+3, 4:n)*G%J(x, y, z+3)*X_x(x, y, z+3, 3)

    end if

    Ju_x(1:n) = (1.0_wp/hx)*Jq_xU(1:n) + (1.0_wp/hy)*Jr_xU(1:n) + (1.0_wp/hz)*Js_xU(1:n)

    !print *, 'cartisian boundary'

  end subroutine JJU_x6

  !> 6th order accurate summation-by-parts finite difference operation of the elastic wave equation  on a curvilinear grid
  !> Compute partial derivatives for displacements u1, u2, u3 in non-conservative form
  !> Compute partial derivatives for stress u4, u5, u6, u7, u8, u9 in conservative form
  !> Form the RHS for the interior

  subroutine JJU_x6_interior(F, G, M, type_of_mesh)
    use datatypes, only: block_type, block_fields, block_grid_t, block_material

    implicit none

    !=================================================================================================
    !
    !type(block_fields), intent(inout):: F
    type(block_type), intent(inout):: F
    type(block_grid_t), intent(in) :: G
    type(block_material), intent(in) :: M

    character(len=64), intent(in) :: type_of_mesh

    integer :: mx, my, mz, px, py, pz, nx, ny, nz, n
    real(kind = wp) :: hx, hy, hz                           ! spatial steps discertizing the unit cube

    integer :: x,y,z,s, iz, fz, iy, fy, ix, fx

    real(kind = wp),parameter :: c(1:3) = (/ 0.75_wp,-0.15_wp,1.0_wp/60.0_wp /) ! FD coefficients
    real(kind = wp),dimension(1:3) :: cx,cy,cz
    real(kind = wp),dimension(1:9) :: DFx,DFy,DFz
    real(kind = wp) :: a1,a2,a3,a4

    mx = G%C%mq
    my = G%C%mr
    mz = G%C%ms
    px = G%C%pq
    py = G%C%pr
    pz = G%C%ps
    nx = G%C%nq
    ny = G%C%nr
    nz = G%C%ns

    n = size(F%F%F, 4)

    hx = G%hq
    hy = G%hr
    hz = G%hs

    cx = c/hx; cy = c/hy; cz = c/hz

!!$    iz = mz
!!$    fz = pz
!!$    if (mz == 1) iz = mz + 6
!!$    if (pz == nz) fz = pz - 6
!!$
!!$    iy = my
!!$    fy = py
!!$    if (my == 1) iy= my + 6
!!$    if (py == ny) fy = py - 6
!!$
!!$    ix = mx
!!$    fx = px
!!$    if (mx == 1 ) ix = mx + 6
!!$    if (px == nx) fx = px - 6
!!$
!!$    if ((mz == 1) .and. (F%PMLB(5)%pml .EQV. .TRUE.)) iz = mz + max(F%PMLB(5)%N_pml, 6)
!!$    if ((pz == nz) .and. (F%PMLB(6)%pml .EQV. .TRUE.)) fz = pz - max(F%PMLB(6)%N_pml, 6)
!!$
!!$    if ((my == 1) .and. (F%PMLB(3)%pml .EQV. .TRUE.)) iy = my + max(F%PMLB(3)%N_pml, 6)
!!$    if ((py == ny) .and. (F%PMLB(4)%pml .EQV. .TRUE.)) fy = py - max(F%PMLB(4)%N_pml, 6)
!!$
!!$    if ((mx == 1) .and. (F%PMLB(1)%pml .EQV. .TRUE.)) ix = mx + max(F%PMLB(1)%N_pml, 6)
!!$    if ((px == nx) .and. (F%PMLB(2)%pml .EQV. .TRUE.)) fx = px - max(F%PMLB(2)%N_pml, 6)

    ix = 6
    iy = 6
    iz = 6

    fx = 6
    fy = 6
    fz = 6

    if (F%PMLB(5)%pml .EQV. .TRUE.) iz = max(F%PMLB(5)%N_pml, 6)
    if (F%PMLB(6)%pml .EQV. .TRUE.) fz = max(F%PMLB(6)%N_pml, 6)

    if (F%PMLB(3)%pml .EQV. .TRUE.) iy = max(F%PMLB(3)%N_pml, 6)
    if (F%PMLB(4)%pml .EQV. .TRUE.) fy = max(F%PMLB(4)%N_pml, 6)

    if (F%PMLB(1)%pml .EQV. .TRUE.) ix = max(F%PMLB(1)%N_pml, 6)
    if (F%PMLB(2)%pml .EQV. .TRUE.) fx = max(F%PMLB(2)%N_pml, 6)

    select case(type_of_mesh)

    case('curvilinear') ! locked or welded interface

       do z = mz, pz
          do y = my, py
             do x = mx, px

                 ! cycle if an interior point
             if (((iz+1 > z) .or. (z > nz - fz)) .or. &
                  ((iy+1 > y) .or. (y > ny - fy)) .or. &
                  ((ix+1 > x) .or. (x > nx - fx))) cycle

                DFx = 0.0_wp; DFy = 0.0_wp; DFz = 0.0_wp

                do s = 1,3

                   DFx(1:3) = DFx(1:3) &
                        + G%metricx(x, y, z, 1)*cx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricx(x, y, z, 2)*cy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricx(x, y, z, 3)*cz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFx(4:9) = DFx(4:9) &
                   + cx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricx(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricx(x-s, y  , z  , 1))) &
                   + cy(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricx(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricx(x  , y-s, z  , 2))) &
                   + cz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricx(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricx(x  , y  , z-s, 3)))
                   !==========================================================

                   DFy(1:3) = DFy(1:3) &
                        + G%metricy(x, y, z, 1)*cx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricy(x, y, z, 2)*cy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricy(x, y, z, 3)*cz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFy(4:9) = DFy(4:9) &
                   + cx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricy(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricy(x-s, y  , z  , 1))) &
                   + cy(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricy(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricy(x  , y-s, z  , 2))) &
                   + cz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricy(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricy(x  , y  , z-s, 3)))
                   !==========================================================

                   DFz(1:3) = DFz(1:3) &
                        + G%metricz(x, y, z, 1)*cx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricz(x, y, z, 2)*cy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricz(x, y, z, 3)*cz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFz(4:9) = DFz(4:9) &
                   + cx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricz(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricz(x-s, y  , z  , 1))) &
                   + cy(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricz(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricz(x  , y-s, z  , 2))) &
                   + cz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricz(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricz(x  , y  , z-s, 3)))

                end do

                a1 = 1.0_wp/(M%M(x, y, z, 3)*G%J(x,y,z)) ! 1/(rho*J)
                a2 = (2.0_wp*M%M(x, y, z, 2) + M%M(x, y, z, 1)) ! (lambda+2*mu)
                a3 = M%M(x, y, z, 1) ! lambda
                a4 = M%M(x, y, z, 2) ! mu

                F%F%DF(x, y, z, 1) = F%F%DF(x,y,z,1) + a1*(DFx(4) + DFy(7) + DFz(8))
                F%F%DF(x, y, z, 2) = F%F%DF(x,y,z,2) + a1*(DFx(7) + DFy(5) + DFz(9))
                F%F%DF(x, y, z, 3) = F%F%DF(x,y,z,3) + a1*(DFx(8) + DFy(9) + DFz(6))

                F%F%DF(x, y, z, 4) = F%F%DF(x,y,z,4) + a2*DFx(1) + a3*(DFy(2) + DFz(3))
                F%F%DF(x, y, z, 5) = F%F%DF(x,y,z,5) + a2*DFy(2) + a3*(DFx(1) + DFz(3))
                F%F%DF(x, y, z, 6) = F%F%DF(x,y,z,6) + a2*DFz(3) + a3*(DFx(1) + DFy(2))

                F%F%DF(x, y, z, 7) = F%F%DF(x,y,z,7) + a4*(DFy(1) + DFx(2))
                F%F%DF(x, y, z, 8) = F%F%DF(x,y,z,8) + a4*(DFz(1) + DFx(3))
                F%F%DF(x, y, z, 9) = F%F%DF(x,y,z,9) + a4*(DFz(2) + DFy(3))

             end do
          end do
       end do

    case('cartesian') ! carseian mesh

       do z = mz, pz
          do y = my, py
             do x = mx, px

                if (((iz+1 > z) .or. (z > nz - fz)) .or. &
                  ((iy+1 > y) .or. (y > ny - fy)) .or. &
                  ((ix+1 > x) .or. (x > nx - fx))) cycle

                DFx = 0.0_wp; DFy = 0.0_wp; DFz = 0.0_wp

                do s = 1,3

                   DFx(:) = DFx(:) &
                        &+G%metricx(x, y, z, 1)*cx(s)*(F%F%F(x+s, y  , z  , :) - F%F%F(x-s, y  , z  , :))
                   !==========================================================

                   DFy(:) = DFy(:) &
                        &+G%metricy(x, y, z, 2)*cy(s)*(F%F%F(x  , y+s, z , :) - F%F%F(x  , y-s, z  , :))
                   !==========================================================

                   DFz(:) = DFz(:) &
                        &+G%metricz(x, y, z, 3)*cz(s)*(F%F%F(x  , y  , z+s, :) - F%F%F(x  , y  , z-s, :))
                end do

                a1 = 1.0_wp/(M%M(x, y, z, 3)) ! 1/(rho*J)

                a2 = (2.0_wp*M%M(x, y, z, 2) + M%M(x, y, z, 1)) ! (lambda+2*mu)

                a3 = M%M(x, y, z, 1) ! lambda

                a4 = M%M(x, y, z, 2) ! mu

               ! Update time derivatives
                F%F%DF(x, y, z, 1) = F%F%DF(x,y,z,1) + a1*(DFx(4) + DFy(7) + DFz(8))
                F%F%DF(x, y, z, 2) = F%F%DF(x,y,z,2) + a1*(DFx(7) + DFy(5) + DFz(9))
                F%F%DF(x, y, z, 3) = F%F%DF(x,y,z,3) + a1*(DFx(8) + DFy(9) + DFz(6))

                F%F%DF(x, y, z, 4) = F%F%DF(x,y,z,4) + a2*DFx(1) + a3*(DFy(2) + DFz(3))
                F%F%DF(x, y, z, 5) = F%F%DF(x,y,z,5) + a2*DFy(2) + a3*(DFx(1) + DFz(3))
                F%F%DF(x, y, z, 6) = F%F%DF(x,y,z,6) + a2*DFz(3) + a3*(DFx(1) + DFy(2))

                F%F%DF(x, y, z, 7) = F%F%DF(x,y,z,7) + a4*(DFy(1) + DFx(2))
                F%F%DF(x, y, z, 8) = F%F%DF(x,y,z,8) + a4*(DFz(1) + DFx(3))
                F%F%DF(x, y, z, 9) = F%F%DF(x,y,z,9) + a4*(DFz(2) + DFy(3))

             end do
          end do
       end do

    end select

  end subroutine JJU_x6_interior

  subroutine JJU_x6_interior_upwind(F, G, M, type_of_mesh)
    use datatypes, only: block_type, block_fields, block_grid_t, block_material

    implicit none

    !=================================================================================================
    !
    !type(block_fields), intent(inout):: F
    type(block_type), intent(inout):: F
    type(block_grid_t), intent(in) :: G
    type(block_material), intent(in) :: M

    character(len=64), intent(in) :: type_of_mesh

    integer :: mx, my, mz, px, py, pz, nx, ny, nz, n
    real(kind = wp) :: hx, hy, hz                           ! spatial steps discertizing the unit cube

    integer :: x,y,z,s, iz, fz, iy, fy, ix, fx

    real(kind = wp),parameter :: cf(1:6) = (/  0.05_wp,  -0.5_wp, -1.0_wp/3.00_wp, 1.0_wp, -0.25_wp, 1.0_wp/30.0_wp /) ! forward  FD coefficients
    real(kind = wp),parameter :: cb(1:6) = (/ -1.0_wp/30.0_wp, 0.25_wp, -1.0_wp, 1.0_wp/3.0_wp, 0.5_wp, -0.05_wp /) ! backward FD coefficients
    real(kind = wp),dimension(1:6) :: cfx,cfy,cfz, cbx,cby,cbz
    real(kind = wp),dimension(1:9) :: DFx,DFy,DFz
    real(kind = wp) :: a1,a2,a3,a4

    mx = G%C%mq
    my = G%C%mr
    mz = G%C%ms
    px = G%C%pq
    py = G%C%pr
    pz = G%C%ps
    nx = G%C%nq
    ny = G%C%nr
    nz = G%C%ns

    n = size(F%F%F, 4)

    hx = G%hq
    hy = G%hr
    hz = G%hs

    cfx = cf/hx; cfy = cf/hy; cfz = cf/hz
    cbx = cb/hx; cby = cb/hy; cbz = cb/hz

!!$    iz = mz
!!$    fz = pz
!!$    if (mz == 1) iz = mz + 6
!!$    if (pz == nz) fz = pz - 6
!!$
!!$    iy = my
!!$    fy = py
!!$    if (my == 1) iy= my + 6
!!$    if (py == ny) fy = py - 6
!!$
!!$    ix = mx
!!$    fx = px
!!$    if (mx == 1 ) ix = mx + 6
!!$    if (px == nx) fx = px - 6
!!$
!!$    if ((mz == 1) .and. (F%PMLB(5)%pml .EQV. .TRUE.)) iz = mz + max(F%PMLB(5)%N_pml, 6)
!!$    if ((pz == nz) .and. (F%PMLB(6)%pml .EQV. .TRUE.)) fz = pz - max(F%PMLB(6)%N_pml, 6)
!!$
!!$    if ((my == 1) .and. (F%PMLB(3)%pml .EQV. .TRUE.)) iy = my + max(F%PMLB(3)%N_pml, 6)
!!$    if ((py == ny) .and. (F%PMLB(4)%pml .EQV. .TRUE.)) fy = py - max(F%PMLB(4)%N_pml, 6)
!!$
!!$    if ((mx == 1) .and. (F%PMLB(1)%pml .EQV. .TRUE.)) ix = mx + max(F%PMLB(1)%N_pml, 6)
!!$    if ((px == nx) .and. (F%PMLB(2)%pml .EQV. .TRUE.)) fx = px - max(F%PMLB(2)%N_pml, 6)

    ix = 6
    iy = 6
    iz = 6

    fx = 6
    fy = 6
    fz = 6

    if (F%PMLB(5)%pml .EQV. .TRUE.) iz = max(F%PMLB(5)%N_pml, 6)
    if (F%PMLB(6)%pml .EQV. .TRUE.) fz = max(F%PMLB(6)%N_pml, 6)

    if (F%PMLB(3)%pml .EQV. .TRUE.) iy = max(F%PMLB(3)%N_pml, 6)
    if (F%PMLB(4)%pml .EQV. .TRUE.) fy = max(F%PMLB(4)%N_pml, 6)

    if (F%PMLB(1)%pml .EQV. .TRUE.) ix = max(F%PMLB(1)%N_pml, 6)
    if (F%PMLB(2)%pml .EQV. .TRUE.) fx = max(F%PMLB(2)%N_pml, 6)

    select case(type_of_mesh)

    case('curvilinear') ! locked or welded interface

       do z = mz, pz
          do y = my, py
             do x = mx, px

                 ! cycle if an interior point
             if (((iz+1 > z) .or. (z > nz - fz)) .or. &
                  ((iy+1 > y) .or. (y > ny - fy)) .or. &
                  ((ix+1 > x) .or. (x > nx - fx))) cycle

                DFx = 0.0_wp; DFy = 0.0_wp; DFz = 0.0_wp

                do s = 1,3

                   DFx(1:3) = DFx(1:3) &
                        + G%metricx(x, y, z, 1)*cfx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricx(x, y, z, 2)*cfy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricx(x, y, z, 3)*cfz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFx(4:9) = DFx(4:9) &
                   + cbx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricx(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricx(x-s, y  , z  , 1))) &
                   + cby(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricx(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricx(x  , y-s, z  , 2))) &
                   + cbz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricx(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricx(x  , y  , z-s, 3)))
                   !==========================================================

                   DFy(1:3) = DFy(1:3) &
                        + G%metricy(x, y, z, 1)*cfx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricy(x, y, z, 2)*cfy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricy(x, y, z, 3)*cfz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFy(4:9) = DFy(4:9) &
                   + cbx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricy(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricy(x-s, y  , z  , 1))) &
                   + cby(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricy(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricy(x  , y-s, z  , 2))) &
                   + cbz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricy(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricy(x  , y  , z-s, 3)))
                   !==========================================================

                   DFz(1:3) = DFz(1:3) &
                        + G%metricz(x, y, z, 1)*cfx(s)*(F%F%F(x+s, y  , z  , 1:3) - F%F%F(x-s, y  , z  , 1:3)) &
                        + G%metricz(x, y, z, 2)*cfy(s)*(F%F%F(x  , y+s, z  , 1:3) - F%F%F(x  , y-s, z  , 1:3)) &
                        + G%metricz(x, y, z, 3)*cfz(s)*(F%F%F(x  , y  , z+s, 1:3) - F%F%F(x  , y  , z-s, 1:3))

                   DFz(4:9) = DFz(4:9) &
                   + cbx(s)*( &
                        F%F%F(x+s, y  , z  , 4:9)*(G%J(x+s, y  , z  )*G%metricz(x+s, y  , z  , 1))- &
                        F%F%F(x-s, y  , z  , 4:9)*(G%J(x-s, y  , z  )*G%metricz(x-s, y  , z  , 1))) &
                   + cby(s)*( &
                        F%F%F(x  , y+s, z  , 4:9)*(G%J(x  , y+s, z  )*G%metricz(x  , y+s, z  , 2))- &
                        F%F%F(x  , y-s, z  , 4:9)*(G%J(x  , y-s, z  )*G%metricz(x  , y-s, z  , 2))) &
                   + cbz(s)*( &
                        F%F%F(x  , y  , z+s, 4:9)*(G%J(x  , y  , z+s)*G%metricz(x  , y  , z+s, 3))- &
                        F%F%F(x  , y  , z-s, 4:9)*(G%J(x  , y  , z-s)*G%metricz(x  , y  , z-s, 3)))

                end do

                a1 = 1.0_wp/(M%M(x, y, z, 3)*G%J(x,y,z)) ! 1/(rho*J)
                a2 = (2.0_wp*M%M(x, y, z, 2) + M%M(x, y, z, 1)) ! (lambda+2*mu)
                a3 = M%M(x, y, z, 1) ! lambda
                a4 = M%M(x, y, z, 2) ! mu

                F%F%DF(x, y, z, 1) = F%F%DF(x,y,z,1) + a1*(DFx(4) + DFy(7) + DFz(8))
                F%F%DF(x, y, z, 2) = F%F%DF(x,y,z,2) + a1*(DFx(7) + DFy(5) + DFz(9))
                F%F%DF(x, y, z, 3) = F%F%DF(x,y,z,3) + a1*(DFx(8) + DFy(9) + DFz(6))

                F%F%DF(x, y, z, 4) = F%F%DF(x,y,z,4) + a2*DFx(1) + a3*(DFy(2) + DFz(3))
                F%F%DF(x, y, z, 5) = F%F%DF(x,y,z,5) + a2*DFy(2) + a3*(DFx(1) + DFz(3))
                F%F%DF(x, y, z, 6) = F%F%DF(x,y,z,6) + a2*DFz(3) + a3*(DFx(1) + DFy(2))

                F%F%DF(x, y, z, 7) = F%F%DF(x,y,z,7) + a4*(DFy(1) + DFx(2))
                F%F%DF(x, y, z, 8) = F%F%DF(x,y,z,8) + a4*(DFz(1) + DFx(3))
                F%F%DF(x, y, z, 9) = F%F%DF(x,y,z,9) + a4*(DFz(2) + DFy(3))

             end do
          end do
       end do

    case('cartesian') ! carteian mesh

     !print *, 'interior'

       do z = mz, pz
          do y = my, py
             do x = mx, px

                if (((iz+1 > z) .or. (z > nz - fz)) .or. &
                  ((iy+1 > y) .or. (y > ny - fy)) .or. &
                  ((ix+1 > x) .or. (x > nx - fx))) cycle

                DFx = 0.0_wp; DFy = 0.0_wp; DFz = 0.0_wp

                do s = -2,3

                   DFx(1:3) = DFx(1:3) &
                        &+G%metricx(x, y, z, 1)*cfx(s+3)*(F%F%F(x+s, y  , z  , 1:3) ) ! forward difference for velocity field

                   DFx(4:9) =DFx(4:9) &

                        &+G%metricx(x, y, z, 1)*cbx(s+3)*(F%F%F(x+s-1, y  , z  , 4:9) ) ! backwad difference for stress field
                   !==========================================================

                   DFy(1:3) = DFy(1:3) &
                        &+G%metricy(x, y, z, 2)*cfy(s+3)*(F%F%F(x  , y+s, z , 1:3) )  ! forward difference for velocity field

                   DFy(4:9) = DFy(4:9) &

                        &+G%metricy(x, y, z, 2)*cby(s+3)*(F%F%F(x  , y+s-1, z , 4:9) ) ! backward difference for velocity field
                   !==========================================================

                   DFz(1:3) = DFz(1:3) &
                        &+G%metricz(x, y, z, 3)*cfz(s+3)*(F%F%F(x  , y  , z+s, 1:3))   ! forward difference for velocity field

                   DFz(4:9) = DFz(4:9) &

                        &+G%metricz(x, y, z, 3)*cbz(s+3)*(F%F%F(x  , y  , z+s-1, 4:9))   ! backward difference for velocity field

                end do

                a1 = 1.0_wp/(M%M(x, y, z, 3)) ! 1/(rho*J)

                a2 = (2.0_wp*M%M(x, y, z, 2) + M%M(x, y, z, 1)) ! (lambda+2*mu)

                a3 = M%M(x, y, z, 1) ! lambda

                a4 = M%M(x, y, z, 2) ! mu

                F%F%DF(x, y, z, 1) = F%F%DF(x,y,z,1) + a1*(DFx(4) + DFy(7) + DFz(8))
                F%F%DF(x, y, z, 2) = F%F%DF(x,y,z,2) + a1*(DFx(7) + DFy(5) + DFz(9))
                F%F%DF(x, y, z, 3) = F%F%DF(x,y,z,3) + a1*(DFx(8) + DFy(9) + DFz(6))

                F%F%DF(x, y, z, 4) = F%F%DF(x,y,z,4) + a2*DFx(1) + a3*(DFy(2) + DFz(3))
                F%F%DF(x, y, z, 5) = F%F%DF(x,y,z,5) + a2*DFy(2) + a3*(DFx(1) + DFz(3))
                F%F%DF(x, y, z, 6) = F%F%DF(x,y,z,6) + a2*DFz(3) + a3*(DFx(1) + DFy(2))

                F%F%DF(x, y, z, 7) = F%F%DF(x,y,z,7) + a4*(DFy(1) + DFx(2))
                F%F%DF(x, y, z, 8) = F%F%DF(x,y,z,8) + a4*(DFz(1) + DFx(3))
                F%F%DF(x, y, z, 9) = F%F%DF(x,y,z,9) + a4*(DFz(2) + DFy(3))

             end do
          end do
       end do

    end select

  end subroutine JJU_x6_interior_upwind

  subroutine JJU_x6_upwind(x, y, z, F, G, X_x, Ju_x)

     !=================================================================================================
     !
           use datatypes, only: block_fields, block_grid_t

     implicit none

     integer, intent(in) :: x, y, z                      ! indices in the tranformed cordinate
     type(block_fields), intent(in):: F
     type(block_grid_t), intent(in) :: G
     real(kind = wp), dimension(:), intent(out) :: Ju_x            ! metric coefficients

     real(kind = wp), dimension(:,:,:,:), allocatable, intent(in) :: X_x   ! the grid
     real(kind = wp), dimension(:), allocatable, save :: Jq_xU, Jr_xU, Js_xU            ! work array
     real(kind = wp) :: hx, hy, hz                      ! spatial steps discertizing the unit cube
     integer :: n, nx, ny, nz

     nx = G%C%nq
     ny = G%C%nr
     nz = G%C%ns

     n = size(F%F, 4)

     hx = G%hq
     hy = G%hr
     hz = G%hs

     ! work arrays:
     if (.not. allocated(Jq_xU)) allocate(Jq_xU(n))
     if (.not. allocated(Jr_xU)) allocate(Jr_xU(n))
     if (.not. allocated(Js_xU)) allocate(Js_xU(n))

     !Ju_x = 0.0_wp*Ju_x

     Jq_xU = 0.0_wp*Jq_xU
     ! Jr_xU = 0.0_wp*Jr_xU
     ! Js_xU = 0.0_wp*Js_xU

     ! Compute: (Jq_xU)_q or (Jq_yU)_q or (Jq_zU)_q for stresses
     ! Compute: q_xU_q or q_yU_q or q_zU_q for displacements
     !=========================================================================================================================
     ! Interior approximation: Standard central finite difference approximation
     !print *, 'metric ',  X_x(x,y,z,1), X_x(x,y,z,2),  X_x(x,y,z,3)

     if ((x .ge. 5) .and. (x .le. nx-4)) then
        ! forward FD
        Jq_xU(1:3) = X_x(x, y, z, 1)*(0.05_wp*F%F(x-2, y, z, 1:3)&
             - 0.5_wp*F%F(x-1, y, z, 1:3)&
             - 1.0_wp/3.0_wp*F%F(x, y, z, 1:3)&
             + 1.0_wp*F%F(x+1, y, z, 1:3)&
             - 0.25_wp*F%F(x+2, y, z, 1:3)&
             + 1.0_wp/30.0_wp*F%F(x+3, y, z, 1:3))
             ! 1/float(20) * u_old[i -2] - 1/float(2) * u_old[i-1] - 1/float(3) * u_old[i] + u_old[i+1 ] - 1/float(4) *u_old[i+2] + 1/float(30) * u_old[i+3]

       ! backward FD

        Jq_xU(4:9) = X_x(x, y, z, 1) * (-1.0_wp/30.0_wp*F%F(x-3, y, z, 4:n)&
              + 0.25_wp*F%F(x-2, y, z, 4:n)&
              - 1.0_wp*F%F(x-1, y, z, 4:n)&
              + 1.0_wp/3.0_wp*F%F(x, y, z, 4:n)&
              + 0.5_wp*F%F(x+1, y, z, 4:n)&
              - 0.05_wp*F%F(x+2, y, z, 4:n))
              ! -1/float(30) * u_old[i -3] + 1/float(4)* u_old[i-2] - u_old[i -1]  +1/float(3) * u_old[i] + 1/float(2) *u_old[i+1] - 1/float(20) * u_old[i+2]

              !real(kind = wp),parameter :: cf(1:6) = (/  0.05_wp,  -0.5_wp, -1.0_wp/3.00_wp, 1.0_wp, -0.25_wp, 1.0_wp/30.0_wp /) ! forward  FD coefficients
             ! real(kind = wp),parameter :: cb(1:6) = (/ -1.0_wp/30.0_wp, 0.25_wp, -1.0_wp, 1.0_wp/3.0_wp, 0.5_wp, -0.05_wp /) ! backward FD coefficients

     end if

     ! Special onesided treatments of boundaries

     !=========================================================================================================================
     ! ! Left boundary closure in x-directyion
     if (x .eq. 1) then

        ! Use forward FD for velocity fields
        Jq_xU(1:3) = X_x(x, y, z, 1)*(- 0.041832669322709_wp*F%F(4, y, z, 1:3) &
          - 0.374501992031873_wp*F%F(3, y, z, 1:3) &
          + 1.874501992031873_wp*F%F(2, y, z, 1:3) &
          - 1.458167330677291_wp*F%F(1, y, z, 1:3))
          ! -1.458167330677291 * u_old[0] + 1.874501992031873 * u_old[1] - 0.374501992031873 *u_old[2] - 0.041832669322709 * u_old[3]

        ! Use backward FD for velocity fields
        Jq_xU(4:9) =  X_x(x, y, z, 1)*(-0.089641434262948_wp*F%F(4, y, z, 4:n) &
             - 0.231075697211155_wp*F%F(3, y, z, 4:n) &
             + 1.731075697211155_wp*F%F(2, y, z, 4:n) &
             - 1.410358565737052_wp*F%F(1, y, z, 4:n))
             ! -1.410358565737052*u_old[0] + 1.731075697211155*u_old[1]   -0.231075697211155*u_old[2]-0.089641434262948*u_old[3]

     end if

     if (x .eq. 2) then

        ! Use forward FD for velocity fields
        Jq_xU(1:3) = X_x(x, y, z, 1)*(0.026755852842809_wp*F%F(5, y, z, 1:3) &
             - 0.095875139353400_wp*F%F(4, y, z, 1:3) &
             + 0.627090301003345_wp*F%F(3, y, z, 1:3) &
             - 0.073578595317726_wp*F%F(2, y, z, 1:3) &
             - 0.484392419175028_wp*F%F(1, y, z, 1:3))
             !- 0.484392419175028 * u_old[0] - 0.073578595317726  *u_old[1] + 0.627090301003345 *u_old[2] - 0.095875139353400 * u_old[3] + 0.026755852842809 * u_old[4]

        Jq_xU(4:9) = X_x(x, y, z, 1)*(0.024526198439242_wp*F%F(4, y, z, 4:n) &
             + 0.426421404682274_wp*F%F(3, y, z, 4:n) &
             + 0.073578595317726_wp*F%F(2, y, z, 4:n) &
             - 0.524526198439242_wp*F%F(1, y, z, 4:n))
             ! -0.524526198439242 * u_old[0] + 0.073578595317726  * u_old[1]  + 0.426421404682274 * u_old[2] + 0.024526198439242 * u_old[3]

     end if

     if (x .eq. 3) then
        Jq_xU(1:3) =X_x(x, y, z, 1)*(0.037914691943128_wp*F%F(6, y, z, 1:3) &
             - 0.284360189573460_wp*F%F(5, y, z, 1:3) &
             + 1.033965244865719_wp*F%F(4, y, z, 1:3) &
             - 0.274881516587678_wp*F%F(3, y, z, 1:3) &
             - 0.604265402843602_wp*F%F(2, y, z, 1:3) &
             + 0.091627172195893_wp*F%F(1, y, z, 1:3))
             !0.091627172195893  * u_old[0] - 0.604265402843602  * u_old[1] - 0.274881516587678  * u_old[2] + 1.033965244865719 * u_old[3] - 0.284360189573460 * u_old[4]
             ! + 0.037914691943128  * u_old[5]\

     ! Backward FD for stress fields
        Jq_xU(4:9) = X_x(x, y, z, 1)*(-0.056872037914692_wp*F%F(5, y, z, 4:n) &
             + 0.522116903633491_wp*F%F(4, y, z, 4:n) &
             + 0.274881516587678_wp*F%F(3, y, z, 4:n) &
             - 0.888625592417062_wp*F%F(2, y, z, 4:n) &
             + 0.148499210110585_wp*F%F(1, y, z, 4:n))
             ! 0.148499210110585 * u_old[0]  - 0.888625592417062 *u_old[1]  + 0.274881516587678 * u_old[2] + 0.522116903633491*u_old[3] - 0.056872037914692 * u_old[4]
     end if

     if (x .eq. 4) then
        Jq_xU(1:3) =   X_x(x, y, z, 1)*(0.032476319350474_wp*F%F(7, y, z, 1:3) &
             - 0.243572395128552_wp*F%F(6, y, z, 1:3)&
             + 0.974289580514208_wp*F%F(5, y, z, 1:3)&
             - 0.316644113667118_wp*F%F(4, y, z, 1:3)&
             - 0.447225981055480_wp*F%F(3, y, z, 1:3)&
             - 0.029769959404601_wp*F%F(2, y, z, 1:3)&
             + 0.030446549391069_wp*F%F(1, y, z, 1:3))
             ! 0.030446549391069 * u_old[0] - 0.029769959404601 *u_old[1] - 0.447225981055480 *u_old[2] - 0.316644113667118 *u_old[3] +  0.974289580514208  * u_old[4]
             ! - 0.243572395128552 * u_old[5]  + 0.032476319350474 * u_old[6]

        Jq_xU(4:9) = X_x(x, y, z, 1)*(-0.048714479025710_wp*F%F(6, y, z, 4:n) &
             + 0.487144790257104_wp*F%F(5, y, z, 4:n)&
             + 0.316644113667118_wp*F%F(4, y, z, 4:n)&
             - 0.885656292286874_wp*F%F(3, y, z, 4:n) &
             + 0.116373477672530_wp*F%F(2, y, z, 4:n) &
             + 0.014208389715832_wp*F%F(1, y, z, 4:n))
             ! 0.014208389715832* u_old[0] + 0.116373477672530 * u_old[1] - 0.885656292286874 * u_old[2] + 0.316644113667118 * u_old[3]
             ! + 0.487144790257104 * u_old[4] - 0.048714479025710  *u_old[5]

     end if

     !=========================================================================================================================
     ! Right boundary closure in x-directyion

     if (x .eq. nx-3) then

         ! Forward FD for velocity fields
        Jq_xU(1:3) = X_x(x, y, z, 1)*(0.048714479025710_wp*F%F(nx-5, y, z, 1:3)&
             - 0.487144790257104_wp*F%F(nx-4, y, z, 1:3)&
             - 0.316644113667118_wp*F%F(nx-3, y, z, 1:3)&
             + 0.885656292286874_wp*F%F(nx-2, y, z, 1:3)&
             - 0.116373477672530_wp*F%F(nx-1, y, z, 1:3)&
             - 0.014208389715832_wp*F%F(nx,   y, z, 1:3))
             !  0.048714479025710 * u_old[m-5]  - 0.487144790257104  *u_old[m-4] - 0.316644113667118 *u_old[m-3] + 0.885656292286874 * u_old[m-2]
             ! - 0.116373477672530 * u_old[m-1]  - 0.014208389715832 *u_old[m]

         ! Backward FD for stress fields
        Jq_xU(4:9) = X_x(x, y, z, 1)*(-0.032476319350474_wp*F%F(nx-6, y, z, 4:n) &
             + 0.243572395128552_wp*F%F(nx-5, y, z, 4:n)&
             - 0.974289580514208_wp*F%F(nx-4, y, z, 4:n)&
             + 0.316644113667118_wp*F%F(nx-3, y, z, 4:n) &
             + 0.447225981055480_wp*F%F(nx-2, y, z, 4:n) &
             + 0.029769959404601_wp*F%F(nx-1, y, z, 4:n)&
             - 0.030446549391069_wp*F%F(nx,   y, z, 4:n))
             ! -0.032476319350474  *u_old[m-6] + 0.243572395128552  * u_old[m-5] -0.974289580514208 *u_old[m-4] +
             ! 0.316644113667118 * u_old[m-3] + 0.447225981055480 * u_old[m-2]  + 0.029769959404601 *u_old[m-1] - 0.030446549391069 * u_old[m]

     end if

     if (x .eq. nx-2) then
        Jq_xU(1:3) = X_x(x, y, z, 1)*(0.056872037914692_wp*F%F(nx-4, y, z, 1:3)&
             - 0.522116903633491_wp*F%F(nx-3, y, z, 1:3)&
             - 0.274881516587678_wp*F%F(nx-2, y, z, 1:3)&
             + 0.888625592417062_wp*F%F(nx-1, y, z, 1:3)&
             - 0.148499210110585_wp*F%F(nx,   y, z, 1:3))
             ! 0.056872037914692 * u_old[m-4] - 0.522116903633491 * u_old[m-3] - 0.274881516587678 * u_old[m-2] + 0.888625592417062* u_old[m-1] - 0.148499210110585 * u_old[m]

        ! Backward FD for stress fields
         Jq_xU(4:9) = X_x(x, y, z, 1)*(-0.037914691943128_wp*F%F(nx-5, y, z, 4:n) &
               + 0.284360189573460_wp*F%F(nx-4, y, z, 4:n)&
               - 1.033965244865719_wp*F%F(nx-3, y, z, 4:n)&
               + 0.274881516587678_wp*F%F(nx-2, y, z, 4:n) &
               + 0.604265402843602_wp*F%F(nx-1, y, z, 4:n) &
               - 0.091627172195893_wp*F%F(nx,   y, z, 4:n))
               ! -0.037914691943128  * u_old[m-5] + 0.284360189573460 * u_old[m-4] -1.033965244865719 * u_old[m-3]
               ! + 0.274881516587678  * u_old[m-2] + 0.604265402843602 * u_old[m-1] - 0.091627172195893 * u_old[m]

     end if

     if (x .eq. nx-1) then
        Jq_xU(1:3) = X_x(x, y, z, 1)*(-0.024526198439242_wp*F%F(nx-3, y, z, 1:3)&
             - 0.426421404682274_wp*F%F(nx-2, y, z, 1:3)&
             - 0.073578595317726_wp*F%F(nx-1, y, z, 1:3) &
             + 0.524526198439242_wp*F%F(nx,   y, z, 1:3))
             ! - 0.024526198439242 * u_old[m-3] - 0.426421404682274 * u_old[m-2] - 0.073578595317726 *u_old[m-1] + 0.524526198439242 * u_old[m]

          ! Backward FD for stress fields
        Jq_xU(4:9) = X_x(x, y, z, 1)*(-0.026755852842809_wp*F%F(nx-4, y, z, 4:n) &
             + 0.095875139353400_wp*F%F(nx-3, y,z, 4:n)&
             - 0.627090301003345_wp*F%F(nx-2, y, z, 4:n)&
             + 0.073578595317726_wp*F%F(nx-1, y, z, 4:n) &
             + 0.484392419175028_wp*F%F(nx,   y, z, 4:n))
             !  -0.026755852842809  * u_old[m-4] + 0.095875139353400* u_old[m-3] -0.627090301003345  * u_old[m-2] + 0.073578595317726 * u_old[m-1] + 0.484392419175028 * u_old[m]

     end if

     if (x .eq. nx) then
        Jq_xU(1:3) =  X_x(x, y, z, 1)*(0.089641434262948_wp*F%F(nx-3, y, z, 1:3)&
             + 0.231075697211155_wp*F%F(nx-2, y, z, 1:3)&
             - 1.731075697211155_wp*F%F(nx-1, y, z, 1:3)&
             + 1.410358565737052_wp*F%F(nx,   y, z, 1:3))
             ! 0.089641434262948 * u_old[m-3] + 0.231075697211155* u_old[m-2] - 1.731075697211155 *u_old[m-1] +1.410358565737052 * u_old[m]

     ! Backward FD for stress fields
        Jq_xU(4:9) = X_x(x, y, z, 1)*(0.041832669322709_wp*F%F(nx-3, y,z, 4:n)&
             + 0.374501992031873_wp*F%F(nx-2, y, z, 4:n)&
             - 1.874501992031873_wp*F%F(nx-1, y, z, 4:n) &
             + 1.458167330677291_wp*F%F(nx,   y, z, 4:n))
             ! 0.041832669322709 * u_old[m-3] +0.374501992031873 *u_old[m-2] -1.874501992031873 * u_old[m-1] + 1.458167330677291 * u_old[m]

     end if

     !=================================================================

     Jr_xU = 0.0_wp*Jr_xU

     !implicit none

     if ((y .ge. 5) .and. (y .le. ny-4)) then

          Jr_xU(1:3) = X_x(x, y, z, 2)*(0.05_wp*F%F(x, y-2, z, 1:3)&
               - 0.5_wp*F%F(x, y-1, z, 1:3)&
               - 1.0_wp/3.0_wp*F%F(x, y, z, 1:3)&
               + 1.0_wp*F%F(x, y+1, z, 1:3)&
               - 0.25_wp*F%F(x, y+2, z, 1:3)&
               + 1.0_wp/30.0_wp*F%F(x, y+3, z, 1:3))
               ! 1/float(20) * u_old[i -2] - 1/float(2) * u_old[i-1] - 1/float(3) * u_old[i] + u_old[i+1 ] - 1/float(4) *u_old[i+2] + 1/float(30) * u_old[i+3]

          Jr_xU(4:9) = X_x(x, y, z, 2) * (-1.0_wp/30.0_wp*F%F(x, y-3, z, 4:n)&
               + 0.25_wp*F%F(x, y-2, z, 4:n)&
               - 1.0_wp*F%F(x, y-1, z, 4:n)&
               + 1.0_wp/3.0_wp*F%F(x, y, z, 4:n)&
               + 0.5_wp*F%F(x, y+1, z, 4:n)&
               - 0.05_wp*F%F(x, y+2, z, 4:n))
               !-1/float(30) * u_old[i -3] + 1/float(4)* u_old[i-2] - u_old[i -1] +1/float(3) * u_old[i] + 1/float(2) *u_old[i+1] - 1/float(20) * u_old[i+2]

       end if

     if (y .eq. 1) then

        Jr_xU(1:3) = X_x(x, y, z, 2)*(- 0.041832669322709_wp*F%F(x, 4, z, 1:3)&
             - 0.374501992031873_wp*F%F(x, 3, z, 1:3)&
             + 1.874501992031873_wp*F%F(x, 2, z, 1:3)&
             - 1.458167330677291_wp*F%F(x, 1, z, 1:3))
             !-1.458167330677291 * u_old[0] + 1.874501992031873 * u_old[1] - 0.374501992031873 *u_old[2] - 0.041832669322709 * u_old[3]

     ! Backward FD for stress fields
        Jr_xU(4:9) =  X_x(x, y, z, 2)*(-0.089641434262948_wp*F%F(x, 4, z, 4:n) &
             - 0.231075697211155_wp*F%F(x, 3, z, 4:n) &
             + 1.731075697211155_wp*F%F(x, 2, z, 4:n) &
             - 1.410358565737052_wp*F%F(x, 1, z, 4:n))
             ! -1.410358565737052*u_old[0] + 1.731075697211155*u_old[1]  -0.231075697211155*u_old[2]-0.089641434262948*u_old[3]

     end if

     if (y .eq. 2) then

        Jr_xU(1:3) = X_x(x, y, z, 2)*( 0.026755852842809_wp*F%F(x, 5, z, 1:3)&
             - 0.095875139353400_wp*F%F(x, 4, z, 1:3)&
             + 0.627090301003345_wp*F%F(x, 3, z, 1:3)&
             - 0.073578595317726_wp*F%F(x, 2, z, 1:3)&
             - 0.484392419175028_wp*F%F(x, 1, z, 1:3))
             !- 0.484392419175028 * u_old[0] - 0.073578595317726  *u_old[1] + 0.627090301003345 *u_old[2] - 0.095875139353400 * u_old[3] + 0.026755852842809 * u_old[4]

        Jr_xU(4:9) = X_x(x, y, z, 2)*(0.024526198439242_wp*F%F(x, 4, z, 4:n) &
             + 0.426421404682274_wp*F%F(x, 3, z, 4:n) &
             + 0.073578595317726_wp*F%F(x, 2, z, 4:n) &
             - 0.524526198439242_wp*F%F(x, 1, z, 4:n))
              ! -0.524526198439242 * u_old[0] + 0.073578595317726  * u_old[1] + 0.426421404682274* u_old[2] + 0.024526198439242 * u_old[3]

     end if

     if (y .eq. 3) then

          Jr_xU(1:3) = X_x(x, y, z, 2)*(0.037914691943128_wp*F%F(x, 6, z, 1:3)&
          - 0.284360189573460_wp*F%F(x, 5, z, 1:3)&
          + 1.033965244865719_wp*F%F(x, 4, z, 1:3)&
          - 0.274881516587678_wp*F%F(x, 3, z, 1:3)&
          - 0.604265402843602_wp*F%F(x, 2, z, 1:3)&
          + 0.091627172195893_wp*F%F(x, 1, z, 1:3))
          !0.091627172195893  * u_old[0] - 0.604265402843602  * u_old[1] - 0.274881516587678  * u_old[2] + 1.033965244865719 * u_old[3]
          !  - 0.284360189573460 * u_old[4] + 0.037914691943128  * u_old[5]\

     ! Backward FD for stress fields
          Jr_xU(4:9) = X_x(x, y, z, 2)*(-0.056872037914692_wp*F%F(x, 5, z, 4:n) &
            + 0.522116903633491_wp*F%F(x, 4, z, 4:n)&
            + 0.274881516587678_wp*F%F(x, 3, z, 4:n) &
            - 0.888625592417062_wp*F%F(x, 2, z, 4:n) &
            + 0.148499210110585_wp*F%F(x, 1, z, 4:n))
            ! 0.148499210110585 * u_old[0]  - 0.888625592417062 *u_old[1] + 0.274881516587678 * u_old[2] + 0.522116903633491*u_old[3] - 0.056872037914692 * u_old[4]

     end if

     if (y .eq. 4) then
        Jr_xU(1:3) =  X_x(x, y, z, 2)*(0.032476319350474_wp*F%F(x, 7, z, 1:3)&
             - 0.243572395128552_wp*F%F(x, 6, z, 1:3)&
             + 0.974289580514208_wp*F%F(x, 5, z, 1:3)&
             - 0.316644113667118_wp*F%F(x, 4, z, 1:3)&
             - 0.447225981055480_wp*F%F(x, 3, z, 1:3)&
             - 0.029769959404601_wp*F%F(x, 2, z, 1:3)&
             + 0.030446549391069_wp*F%F(x, 1, z, 1:3))
             ! 0.030446549391069 * u_old[0] - 0.029769959404601 *u_old[1] - 0.447225981055480 *u_old[2] - 0.316644113667118 *u_old[3]
             ! +  0.974289580514208  * u_old[4] - 0.243572395128552 * u_old[5] + 0.032476319350474 * u_old[6]

        Jr_xU(4:9) = X_x(x, y, z, 2)*(-0.048714479025710_wp*F%F(x, 6, z, 4:n) &
             + 0.487144790257104_wp*F%F(x, 5, z, 4:n)&
             + 0.316644113667118_wp*F%F(x, 4, z, 4:n)&
             - 0.885656292286874_wp*F%F(x, 3, z, 4:n) &
             + 0.116373477672530_wp*F%F(x, 2, z, 4:n) &
             + 0.014208389715832_wp*F%F(x, 1, z, 4:n))
             ! 0.014208389715832* u_old[0] + 0.116373477672530 * u_old[1]  - 0.885656292286874 * u_old[2] + 0.316644113667118 * u_old[3]
             !  + 0.487144790257104 * u_old[4] - 0.048714479025710  *u_old[5]

     end if

     if (y .eq. ny-3) then
         Jr_xU(1:3) = X_x(x, y, z, 2)*(0.048714479025710_wp*F%F(x, ny-5, z, 4:n) &
               - 0.487144790257104_wp*F%F(x, ny-4, z, 4:n)&
               - 0.316644113667118_wp*F%F(x, ny-3, z, 4:n)&
               + 0.885656292286874_wp*F%F(x, ny-2, z, 4:n) &
               - 0.116373477672530_wp*F%F(x, ny-1, z, 4:n) &
               - 0.014208389715832_wp*F%F(x, ny,   z, 4:n))
               ! 0.048714479025710 * u_old[m-5]  - 0.487144790257104  *u_old[m-4] - 0.316644113667118 *u_old[m-3]
               ! + 0.885656292286874 * u_old[m-2] - 0.116373477672530 * u_old[m-1]  - 0.014208389715832 *u_old[m]

     ! Backward FD for stress fields
         Jr_xU(4:9) = X_x(x, y, z, 2)*(-0.032476319350474_wp*F%F(x, ny-6, z, 4:n) &
             + 0.243572395128552_wp*F%F(x, ny-5, z, 4:n)&
             - 0.974289580514208_wp*F%F(x, ny-4, z, 4:n)&
             + 0.316644113667118_wp*F%F(x, ny-3, z, 4:n) &
             + 0.447225981055480_wp*F%F(x, ny-2, z, 4:n) &
             + 0.029769959404601_wp*F%F(x, ny-1, z, 4:n)&
             - 0.030446549391069_wp*F%F(x, ny,   z, 4:n))
             ! -0.032476319350474  *u_old[m-6] + 0.243572395128552  * u_old[m-5] -0.974289580514208 *u_old[m-4] + 0.316644113667118 * u_old[m-3]
             !  + 0.447225981055480 * u_old[m-2]  + 0.029769959404601 *u_old[m-1]  - 0.030446549391069 * u_old[m]

     end if

     if (y .eq.  ny-2) then
        Jr_xU(1:3) = X_x(x, y, z, 2)*(0.056872037914692_wp*F%F(x, ny-4, z, 1:3)&
             - 0.522116903633491_wp*F%F(x, ny-3, z, 1:3)&
             - 0.274881516587678_wp*F%F(x, ny-2, z, 1:3)&
             + 0.888625592417062_wp*F%F(x, ny-1, z, 1:3)&
             - 0.148499210110585_wp*F%F(x, ny,   z, 1:3))
             ! 0.056872037914692 * u_old[m-4] - 0.522116903633491 * u_old[m-3] - 0.274881516587678 * u_old[m-2] + 0.888625592417062* u_old[m-1] - 0.148499210110585 * u_old[m]

        ! Backward FD for stress fields
        Jr_xU(4:9) = X_x(x, y, z, 2)*(-0.037914691943128_wp*F%F(x, ny-5, z, 4:n) &
             + 0.284360189573460_wp*F%F(x, ny-4, z, 4:n)&
             - 1.033965244865719_wp*F%F(x, ny-3, z, 4:n)&
             + 0.274881516587678_wp*F%F(x, ny-2, z, 4:n) &
             + 0.604265402843602_wp*F%F(x, ny-1, z, 4:n) &
             - 0.091627172195893_wp*F%F(x, ny,   z, 4:n))
             !  -0.037914691943128  * u_old[m-5] + 0.284360189573460 * u_old[m-4] -1.033965244865719 * u_old[m-3] + 0.274881516587678  * u_old[m-2]
             ! + 0.604265402843602 * u_old[m-1] - 0.091627172195893 * u_old[m]

     end if

     if (y .eq.  ny-1) then

        Jr_xU(1:3) = X_x(x, y, z, 2)*(- 0.024526198439242_wp*F%F(x, ny-3, z, 1:3)&
             - 0.426421404682274_wp*F%F(x, ny-2, z, 1:3)&
             - 0.073578595317726_wp*F%F(x, ny-1, z, 1:3)&
             + 0.524526198439242_wp*F%F(x, ny,   z, 1:3))
             ! - 0.024526198439242 * u_old[m-3] - 0.426421404682274 * u_old[m-2] - 0.073578595317726 *u_old[m-1] + 0.524526198439242 * u_old[m]

               ! Backward FD for stress fields
        Jr_xU(4:9) = X_x(x, y, z, 2)*(-0.026755852842809_wp*F%F(x, ny-4, z, 4:n) &
             + 0.095875139353400_wp*F%F(x, ny-3, z, 4:n) &
             - 0.627090301003345_wp*F%F(x, ny-2, z, 4:n) &
             + 0.073578595317726_wp*F%F(x, ny-1, z, 4:n) &
             + 0.484392419175028_wp*F%F(x, ny,   z, 4:n))
             ! -0.026755852842809  * u_old[m-4] + 0.095875139353400* u_old[m-3] -0.627090301003345  * u_old[m-2] + 0.073578595317726 * u_old[m-1] + 0.484392419175028 * u_old[m]

     end if

     if (y .eq.  ny) then

        Jr_xU(1:3) = X_x(x, y, z, 2)*(0.089641434262948_wp*F%F(x, ny-3, z, 1:3)&
             + 0.231075697211155_wp*F%F(x, ny-2, z, 1:3)&
             - 1.731075697211155_wp*F%F(x, ny-1, z, 1:3)&
             + 1.410358565737052_wp*F%F(x, ny,   z, 1:3))
             ! 0.089641434262948 * u_old[m-3] + 0.231075697211155* u_old[m-2] - 1.731075697211155 *u_old[m-1] +1.410358565737052 * u_old[m]

            ! Backward FD for stress fields
        Jr_xU(4:9) = X_x(x, y, z, 2)*(0.041832669322709_wp*F%F(x, ny-3, z, 4:n)&
             + 0.374501992031873_wp*F%F(x, ny-2, z, 4:n) &
             - 1.874501992031873_wp*F%F(x, ny-1, z, 4:n) &
             + 1.458167330677291_wp*F%F(x, ny,   z, 4:n))
             ! 0.041832669322709 * u_old[m-3] + 0.374501992031873 *u_old[m-2] -1.874501992031873 * u_old[m-1] + 1.458167330677291 * u_old[m]

     end if

     !======================================================================

     Js_xU = 0.0_wp*Js_xU

     if ((z .ge. 5) .and. (z .le. nz-4)) then

          Js_xU(1:3) = X_x(x, y, z, 3)*(0.05_wp*F%F(x, y, z-2, 1:3)&
               - 0.5_wp*F%F(x, y, z-1, 1:3)&
               - 1.0_wp/3.0_wp*F%F(x, y, z, 1:3)&
               + 1.0_wp*F%F(x, y, z+1, 1:3)&
               - 0.25_wp*F%F(x, y, z+2, 1:3)&
               + 1.0_wp/30.0_wp*F%F(x, y, z+3, 1:3))
               !  1/float(20) * u_old[i -2] - 1/float(2) * u_old[i-1] - 1/float(3) * u_old[i] + u_old[i+1 ] - 1/float(4) *u_old[i+2] + 1/float(30) * u_old[i+3]

          Js_xU(4:9) = X_x(x, y, z, 3) * (-1.0_wp/30.0_wp*F%F(x, y, z-3, 4:n)&
               + 0.25_wp*F%F(x, y, z-2, 4:n)&
               - 1.0_wp*F%F(x, y, z-1, 4:n)&
               + 1.0_wp/3.0_wp*F%F(x, y, z, 4:n)&
               + 0.5_wp*F%F(x, y, z+1, 4:n)&
               - 0.05_wp*F%F(x, y, z+2, 4:n))
               ! -1/float(30) * u_old[i -3] + 1/float(4)* u_old[i-2] - u_old[i -1]  +1/float(3) * u_old[i] + 1/float(2) *u_old[i+1] - 1/float(20) * u_old[i+2]

       end if

     if (z .eq.  1) then

        Js_xU(1:3) =  X_x(x, y, z, 3)*(- 0.041832669322709_wp*F%F(x, y, 4, 1:3)&
             - 0.374501992031873_wp*F%F(x, y, 3, 1:3)&
             + 1.874501992031873_wp*F%F(x, y, 2, 1:3)&
             - 1.458167330677291_wp*F%F(x, y, 1, 1:3))
             ! -1.458167330677291 * u_old[0] + 1.874501992031873 * u_old[1] - 0.374501992031873 *u_old[2] - 0.041832669322709 * u_old[3]

       ! Backward FD for stress fields
         Js_xU(4:9) =  X_x(x, y, z, 3)*(-0.089641434262948_wp*F%F(x, y, 4, 4:n) &
             - 0.231075697211155_wp*F%F(x, y, 3, 4:n) &
             + 1.731075697211155_wp*F%F(x, y, 2, 4:n) &
             - 1.410358565737052_wp*F%F(x, y, 1, 4:n))
             ! -1.410358565737052*u_old[0] + 1.731075697211155*u_old[1]  -0.231075697211155*u_old[2]-0.089641434262948*u_old[3]
     end if

     if (z .eq.  2) then
        Js_xU(1:3) = X_x(x, y, z, 3)*(0.026755852842809_wp*F%F(x, y, 5, 1:3)&
             - 0.095875139353400_wp*F%F(x, y, 4, 1:3)&
             + 0.627090301003345_wp*F%F(x, y, 3, 1:3)&
             - 0.073578595317726_wp*F%F(x, y, 2, 1:3)&
             - 0.484392419175028_wp*F%F(x, y, 1, 1:3))
             !  - 0.484392419175028 * u_old[0] - 0.073578595317726  *u_old[1] + 0.627090301003345 *u_old[2] - 0.095875139353400 * u_old[3] + 0.026755852842809 * u_old[4]

        Js_xU(4:9) = X_x(x, y, z, 3)*(0.024526198439242_wp*F%F(x, y, 4, 4:n) &
             + 0.426421404682274_wp*F%F(x, y, 3, 4:n) &
             + 0.073578595317726_wp*F%F(x, y, 2, 4:n) &
             - 0.524526198439242_wp*F%F(x, y, 1, 4:n))
             ! -0.524526198439242 * u_old[0] + 0.073578595317726  * u_old[1] + 0.426421404682274* u_old[2] + 0.024526198439242 * u_old[3]
     end if

     if (z .eq.  3) then
        Js_xU(1:3)  = X_x(x, y, z, 3)*(0.037914691943128_wp*F%F(x, y, 6, 1:3)&
             - 0.284360189573460_wp*F%F(x, y, 5, 1:3)&
             + 1.033965244865719_wp*F%F(x, y, 4, 1:3)&
             - 0.274881516587678_wp*F%F(x, y, 3, 1:3)&
             - 0.604265402843602_wp*F%F(x, y, 2, 1:3)&
             + 0.091627172195893_wp*F%F(x, y, 1, 1:3))
             ! 0.091627172195893  * u_old[0] - 0.604265402843602  * u_old[1] - 0.274881516587678  * u_old[2] + 1.033965244865719 * u_old[3]
             ! - 0.284360189573460 * u_old[4] + 0.037914691943128  * u_old[5]\

           ! Backward FD for stress fields
         Js_xU(4:9) = X_x(x, y, z, 3)*(-0.056872037914692_wp*F%F(x,y, 5, 4:n) &
             + 0.522116903633491_wp*F%F(x, y, 4, 4:n) &
             + 0.274881516587678_wp*F%F(x, y, 3, 4:n) &
             - 0.888625592417062_wp*F%F(x, y, 2, 4:n) &
             + 0.148499210110585_wp*F%F(x, y, 1, 4:n))
             ! 0.148499210110585 * u_old[0]  - 0.888625592417062 *u_old[1] + 0.274881516587678 * u_old[2] + 0.522116903633491*u_old[3] - 0.056872037914692 * u_old[4]

     end if

     if (z .eq.  4) then
        Js_xU(1:3) = X_x(x, y, z, 3)*(0.032476319350474*F%F(x, y, 7, 1:3)&
             - 0.243572395128552_wp*F%F(x, y, 6, 1:3)&
             + 0.974289580514208_wp*F%F(x, y, 5, 1:3)&
             - 0.316644113667118_wp*F%F(x, y, 4, 1:3)&
             - 0.447225981055480_wp*F%F(x, y, 3, 1:3)&
             - 0.029769959404601_wp*F%F(x, y, 2, 1:3)&
             + 0.030446549391069_wp*F%F(x, y, 1, 1:3))
             ! 0.030446549391069 * u_old[0] - 0.029769959404601 *u_old[1] - 0.447225981055480 *u_old[2] - 0.316644113667118 *u_old[3]
             !  +  0.974289580514208  * u_old[4] - 0.243572395128552 * u_old[5] + 0.032476319350474 * u_old[6]

        Js_xU(4:9) = X_x(x, y, z, 3)*(-0.048714479025710_wp*F%F(x, y, 6, 4:n) &
             + 0.487144790257104_wp*F%F(x, y, 5, 4:n)&
             + 0.316644113667118_wp*F%F(x, y, 4, 4:n)&
             - 0.885656292286874_wp*F%F(x, y, 3, 4:n) &
             + 0.116373477672530_wp*F%F(x, y, 2, 4:n) &
             + 0.014208389715832_wp*F%F(x, y, 1, 4:n))
             ! 0.014208389715832* u_old[0] + 0.116373477672530 * u_old[1] - 0.885656292286874 * u_old[2] + 0.316644113667118 * u_old[3]
             ! + 0.487144790257104 * u_old[4] - 0.048714479025710  *u_old[5]

     end if

     if (z .eq.   nz-3) then
        Js_xU(1:3) =  X_x(x, y, z, 3)*(0.048714479025710_wp*F%F(x, y, nz-5, 1:3)&
             - 0.487144790257104_wp*F%F(x, y, nz-4, 1:3)&
             - 0.316644113667118_wp*F%F(x, y, nz-3, 1:3)&
             + 0.885656292286874_wp*F%F(x, y, nz-2, 1:3)&
             - 0.116373477672530_wp*F%F(x, y, nz-1, 1:3)&
             - 0.014208389715832_wp*F%F(x, y, nz,   1:3))
             ! 0.048714479025710 * u_old[m-5]  - 0.487144790257104  *u_old[m-4] - 0.316644113667118 *u_old[m-3]  + 0.885656292286874 * u_old[m-2]
             !  - 0.116373477672530 * u_old[m-1]  - 0.014208389715832 *u_old[m]

             ! Backward FD for stress fields
        Js_xU(4:9) = X_x(x, y, z, 3)*(-0.032476319350474_wp*F%F(x, y, nz-6,4:n) &
             + 0.243572395128552_wp*F%F(x, y, nz-5, 4:n)&
             - 0.974289580514208_wp*F%F(x, y, nz-4, 4:n)&
             + 0.316644113667118_wp*F%F(x, y, nz-3, 4:n) &
             + 0.447225981055480_wp*F%F(x, y, nz-2, 4:n) &
             + 0.029769959404601_wp*F%F(x, y, nz-1, 4:n)&
             - 0.030446549391069_wp*F%F(x, y, nz,   4:n))
             ! -0.032476319350474  *u_old[m-6] + 0.243572395128552  * u_old[m-5] -0.974289580514208 *u_old[m-4] + 0.316644113667118 * u_old[m-3]
             ! + 0.447225981055480 * u_old[m-2]  + 0.029769959404601 *u_old[m-1] - 0.030446549391069 * u_old[m]

     end if

     if (z .eq. nz-2) then

        Js_xU(1:3) = X_x(x, y, z, 3)*(0.056872037914692_wp*F%F(x, y, nz-4, 1:3)&
             - 0.522116903633491_wp*F%F(x, y, nz-3, 1:3)&
             - 0.274881516587678_wp*F%F(x, y, nz-2, 1:3)&
             + 0.888625592417062_wp*F%F(x, y, nz-1, 1:3)&
             - 0.148499210110585_wp*F%F(x, y, nz,   1:3))
             ! 0.056872037914692 * u_old[m-4] - 0.522116903633491 * u_old[m-3] - 0.274881516587678 * u_old[m-2] + 0.888625592417062* u_old[m-1] - 0.148499210110585 * u_old[m]

               ! Backward FD for stress fields
        Js_xU(4:9) = X_x(x, y, z, 3)*(-0.037914691943128_wp*F%F(x, y, nz-5, 4:n) &
             + 0.284360189573460_wp*F%F(x, y, nz-4, 4:n)&
             - 1.033965244865719_wp*F%F(x, y, nz-3, 4:n)&
             + 0.274881516587678_wp*F%F(x, y, nz-2, 4:n) &
             + 0.604265402843602_wp*F%F(x, y, nz-1, 4:n) &
             - 0.091627172195893_wp*F%F(x, y, nz,   4:n))
             !-0.037914691943128  * u_old[m-5] + 0.284360189573460 * u_old[m-4] -1.033965244865719 * u_old[m-3] + 0.274881516587678  * u_old[m-2]
             !  + 0.604265402843602 * u_old[m-1] - 0.091627172195893 * u_old[m]

     end if

     if (z .eq.   nz-1) then
        Js_xU(1:3) = X_x(x, y, z, 3)*(-0.024526198439242_wp*F%F(x, y, nz-3, 1:3)&
             - 0.426421404682274_wp*F%F(x, y, nz-2, 1:3)&
             - 0.073578595317726_wp*F%F(x, y, nz-1, 1:3)&
             + 0.524526198439242_wp*F%F(x, y, nz,   1:3))
             ! - 0.024526198439242 * u_old[m-3] - 0.426421404682274 * u_old[m-2] - 0.073578595317726 *u_old[m-1] + 0.524526198439242 * u_old[m]

                 ! Backward FD for stress fields
        Js_xU(4:9) = X_x(x, y, z, 3)*(-0.026755852842809_wp*F%F(x, y, nz-4, 4:n) &
             + 0.095875139353400_wp*F%F(x, y, nz-3, 4:n)&
             - 0.627090301003345_wp*F%F(x, y, nz-2, 4:n)&
             + 0.073578595317726_wp*F%F(x, y, nz-1, 4:n) &
             + 0.484392419175028_wp*F%F(x, y, nz,   4:n))
             !  -0.026755852842809  * u_old[m-4] + 0.095875139353400* u_old[m-3] -0.627090301003345  * u_old[m-2] + 0.073578595317726 * u_old[m-1] + 0.484392419175028 * u_old[m]

     end if

     if (z .eq.   nz) then

        Js_xU(1:3) = X_x(x, y, z, 3)*(0.089641434262948_wp*F%F(x, y, nz-3, 1:3)&
             + 0.231075697211155_wp*F%F(x, y, nz-2, 1:3)&
             - 1.731075697211155_wp*F%F(x, y, nz-1, 1:3)&
             + 1.410358565737052_wp*F%F(x, y, nz,   1:3))
             !  0.089641434262948 * u_old[m-3] + 0.231075697211155* u_old[m-2] - 1.731075697211155 *u_old[m-1] +1.410358565737052 * u_old[m]

        ! Backward FD for stress fields
        Js_xU(4:9) = X_x(x, y, z, 3)*(0.041832669322709_wp*F%F(x, y, nz-3, 4:n)&
             + 0.374501992031873_wp*F%F(x, y, nz-2, 4:n)&
             - 1.874501992031873_wp*F%F(x, y, nz-1, 4:n) &
             + 1.458167330677291_wp*F%F(x, y, nz,   4:n))
             ! 0.041832669322709 * u_old[m-3] +0.374501992031873 *u_old[m-2]  -1.874501992031873 * u_old[m-1] + 1.458167330677291 * u_old[m]

     end if

     Ju_x(1:n) = (1.0_wp/hx)*Jq_xU(1:n) + (1.0_wp/hy)*Jr_xU(1:n) + (1.0_wp/hz)*Js_xU(1:n)

   end subroutine JJU_x6_upwind

end module  JU_xJU_yJU_z6


program fmath
    use fmathmod
    implicit none
    real :: a, b
    real :: s
    real :: d

    print *, 'Please enter first number: '
    read (*, *) a
    print *, 'Please enter second number: '
    read (*, *) b
    call fsum(a, b, s)
    call fdiff(a, b, d)
    print *, 'Sum is: ', s
    print *, 'Difference is: ', d

end program fmath


c non perfectly nested parallel loop
      program hpftest65
      integer i, j
      integer n
      parameter (n=100)
      real a(n), b(n,n), tmp
chpf$ processors p(2,2)
chpf$ template t(n,n)
chpf$ align b(i,j) with t(i,j)
chpf$ distribute t(block,block) onto p
      print *, 'hpftest65 running'
      do i=1, n
         a(i)=111.0/i
      enddo
chpf$ independent(i)
      do i=1, n
         tmp = a(i)+7.5*i
chpf$ independent(j)
         do j=1, n
            b(j,i) = tmp + 17.3*j
         enddo
      enddo
      print *, ((b(i,j), i=45, 55), j=45, 55)
      end


*> \brief \b SGET06
*
*  =========== DOCUMENTATION ===========
*
* Online html documentation available at
*            http://www.netlib.org/lapack/explore-html/
*
*  Definition:
*  ===========
*
*       REAL             FUNCTION SGET06( RCOND, RCONDC )
*
*       .. Scalar Arguments ..
*       REAL               RCOND, RCONDC
*       ..
*
*
*> \par Purpose:
*  =============
*>
*> \verbatim
*>
*> SGET06 computes a test ratio to compare two values for RCOND.
*> \endverbatim
*
*  Arguments:
*  ==========
*
*> \param[in] RCOND
*> \verbatim
*>          RCOND is REAL
*>          The estimate of the reciprocal of the condition number of A,
*>          as computed by SGECON.
*> \endverbatim
*>
*> \param[in] RCONDC
*> \verbatim
*>          RCONDC is REAL
*>          The reciprocal of the condition number of A, computed as
*>          ( 1/norm(A) ) / norm(inv(A)).
*> \endverbatim
*
*  Authors:
*  ========
*
*> \author Univ. of Tennessee
*> \author Univ. of California Berkeley
*> \author Univ. of Colorado Denver
*> \author NAG Ltd.
*
*> \date December 2016
*
*> \ingroup single_lin
*
*  =====================================================================
      REAL             FUNCTION SGET06( RCOND, RCONDC )
*
*  -- LAPACK test routine (version 3.7.0) --
*  -- LAPACK is a software package provided by Univ. of Tennessee,    --
*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
*     December 2016
*
*     .. Scalar Arguments ..
      REAL               RCOND, RCONDC
*     ..
*
*  =====================================================================
*
*     .. Parameters ..
      REAL               ZERO, ONE
      PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0 )
*     ..
*     .. Local Scalars ..
      REAL               EPS, RAT
*     ..
*     .. External Functions ..
      REAL               SLAMCH
      EXTERNAL           SLAMCH
*     ..
*     .. Intrinsic Functions ..
      INTRINSIC          MAX, MIN
*     ..
*     .. Executable Statements ..
*
      EPS = SLAMCH( 'Epsilon' )
      IF( RCOND.GT.ZERO ) THEN
         IF( RCONDC.GT.ZERO ) THEN
            RAT = MAX( RCOND, RCONDC ) / MIN( RCOND, RCONDC ) -
     $            ( ONE-EPS )
         ELSE
            RAT = RCOND / EPS
         END IF
      ELSE
         IF( RCONDC.GT.ZERO ) THEN
            RAT = RCONDC / EPS
         ELSE
            RAT = ZERO
         END IF
      END IF
      SGET06 = RAT
      RETURN
*
*     End of SGET06
*
      END


! Check that lbound() and ubound() work correctly for assumed shapes.
! { dg-do run }
program main
  integer, dimension (40, 80) :: a = 1
  call test (a)
contains
  subroutine test (b)
    integer, dimension (11:, -8:), target :: b
    integer, dimension (:, :), pointer :: ptr

    if (lbound (b, 1) .ne. 11) STOP 1
    if (ubound (b, 1) .ne. 50) STOP 2
    if (lbound (b, 2) .ne. -8) STOP 3
    if (ubound (b, 2) .ne. 71) STOP 4

    if (lbound (b (:, :), 1) .ne. 1) STOP 5
    if (ubound (b (:, :), 1) .ne. 40) STOP 6
    if (lbound (b (:, :), 2) .ne. 1) STOP 7
    if (ubound (b (:, :), 2) .ne. 80) STOP 8

    if (lbound (b (20:30:3, 40), 1) .ne. 1) STOP 9
    if (ubound (b (20:30:3, 40), 1) .ne. 4) STOP 10

    ptr => b
    if (lbound (ptr, 1) .ne. 11) STOP 11
    if (ubound (ptr, 1) .ne. 50) STOP 12
    if (lbound (ptr, 2) .ne. -8) STOP 13
    if (ubound (ptr, 2) .ne. 71) STOP 14
  end subroutine test
end program main