

























































































































 


































































































































































 






















































































































































































 






















































 










 








































































































































































































































 










                                
                                

                                
                                

                                
                                

                                





































module pointwise2

  implicit none
  private
  public calc_2position
  public calc_2offsets
  public get_2scalar
  public get_2vector
  public get_2tensor
  public set_2scalar
  public set_2vector
  public set_2tensor
  public get_2scalarderivs
  public get_2vectorderivs
  public get_2tensorderivs
  public get_2scalarderivs2
  public get_2vectorderivs2
  public get_2tensorderivs2
  public get_2scalarderivs3
  public get_2vectorderivs3
  public get_2tensorderivs3
contains




  subroutine get_2derivs (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    integer :: order1
    integer :: i
    order1 = 2
    if (present(order)) order1 = order
    select case (order1)
    case (2)
       do i=1,2
          f(i) = (a(pos+off(i)) - a(pos-off(i))) / (2*dx(i))
       end do
    case (4)
       do i=1,2
          f(i) = (- a(pos+2*off(i)) + 8*a(pos+off(i)) - 8*a(pos-off(i)) + a(pos-2*off(i))) / (12*dx(i))
       end do
    case default
       call CCTK_Warn(0,36,"pointwise2.F90","TGRtensor", "Unsupported finite differencing order")
    end select
  end subroutine get_2derivs
  subroutine get_2derivs2 (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    integer :: order1
    integer :: i
    order1 = 2
    if (present(order)) order1 = order
    select case (order1)
    case (2)
       do i=1,2
          f(i,i) = (a(pos+off(i)) - 2*a(pos) + a(pos-off(i))) / dx(i)**2
       end do
       f(1,2) = (  a(pos-off(1)-off(2)) - a(pos+off(1)-off(2)) &
            &    - a(pos-off(1)+off(2)) + a(pos+off(1)+off(2))) / (4*dx(1)*dx(2))
       f(2,1) = f(1,2)
    case (4)
       do i=1,2
          f(i,i) = (- a(pos-2*off(i)) + 16*a(pos-off(i)) - 30*a(pos) + 16*a(pos+off(i)) - a(pos+2*off(i))) / (12*dx(i)**2)
       end do
       f(1,2) = (    a(pos+2*off(1)+2*off(2)) -  8*a(pos+off(1)+2*off(2)) +  8*a(pos-off(1)+2*off(2)) -   a(pos-2*off(1)+2*off(2)) &
            &    - 8*a(pos+2*off(1)+  off(2)) + 64*a(pos+off(1)+  off(2)) - 64*a(pos-off(1)+  off(2)) + 8*a(pos-2*off(1)+  off(2)) &
            &    + 8*a(pos+2*off(1)-  off(2)) - 64*a(pos+off(1)-  off(2)) + 64*a(pos-off(1)-  off(2)) - 8*a(pos-2*off(1)-  off(2)) &
            &    -   a(pos+2*off(1)-2*off(2)) +  8*a(pos+off(1)-2*off(2)) -  8*a(pos-off(1)-2*off(2)) +   a(pos-2*off(1)-2*off(2)))&
  & / (144*dx(1)*dx(2))
       f(2,1) = f(1,2)
    case default
       call CCTK_Warn(0,67,"pointwise2.F90","TGRtensor", "Unsupported finite differencing order")
    end select
  end subroutine get_2derivs2
  subroutine get_2derivs3 (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    integer :: order1
    order1 = 2
    if (present(order)) order1 = order
    select case (order1)
    case (2)
       f(1,1,1) = (a(pos+2*off(1)) - 2*a(pos+off(1)) + 2*a(pos-off(1)) - a(pos-2*off(1))) / (2*dx(1)**3)
       f(2,1,1) = (a(pos+off(1)+off(2)) - 2*a(pos+off(2)) + a(pos-off(1)+off(2)) - a(pos+off(1)-off(2)) + 2*a(pos-off(2)) - a(pos-o&
  &ff(1)-off(2))) / (2*dx(1)*dx(1)*dx(2))
       f(1,2,1) = f(2,1,1)
       f(2,2,1) = (a(pos+off(1)+off(2)) - 2*a(pos+off(1)) + a(pos+off(1)-off(2)) - a(pos-off(1)+off(2)) + 2*a(pos-off(1)) - a(pos-o&
  &ff(1)-off(2))) / (2*dx(1)*dx(2)*dx(2))
       f(:,1,2) = f(:,2,1)
       f(1,2,2) = f(2,2,1)
       f(2,2,2) = (a(pos+2*off(2)) - 2*a(pos+off(2)) + 2*a(pos-off(2)) - a(pos-2*off(2))) / (2*dx(2)**3)
    case (4)
       
       f(1,1,1) = (a(pos-3*off(1)) - 8*a(pos-2*off(1)) + 13*a(pos-off(1)) - 13*a(pos+off(1)) + 8*a(pos+2*off(1)) - a(pos+3*off(1)))&
  & / (8*dx(1)**3)
       
       
       f(2,1,1) = (+   a(pos-2*off(1)-2*off(2)) -  16*a(pos-off(1)-2*off(2)) +  30*a(pos-2*off(2)) -  16*a(pos+off(1)-2*off(2)) +  &
  & a(pos+2*off(1)-2*off(2)) &
            &      - 8*a(pos-2*off(1)-  off(2)) + 128*a(pos-off(1)-  off(2)) - 240*a(pos-  off(2)) + 128*a(pos+off(1)-  off(2)) - 8&
  &*a(pos+2*off(1)-  off(2)) &
            &      + 8*a(pos-2*off(1)+  off(2)) - 128*a(pos-off(1)+  off(2)) + 240*a(pos+  off(2)) - 128*a(pos+off(1)+  off(2)) + 8&
  &*a(pos+2*off(1)+  off(2)) &
            &      -   a(pos-2*off(1)+2*off(2)) +  16*a(pos-off(1)+2*off(2)) -  30*a(pos+2*off(2)) +  16*a(pos+off(1)+2*off(2)) -  &
  & a(pos+2*off(1)+2*off(2))) / (144*dx(1)**2*dx(2))
       f(1,2,1) = f(2,1,1)
       f(2,2,1) = (+   a(pos-2*off(2)-2*off(1)) -  16*a(pos-off(2)-2*off(1)) +  30*a(pos-2*off(1)) -  16*a(pos+off(2)-2*off(1)) +  &
  & a(pos+2*off(2)-2*off(1)) &
            &      - 8*a(pos-2*off(2)-  off(1)) + 128*a(pos-off(2)-  off(1)) - 240*a(pos-  off(1)) + 128*a(pos+off(2)-  off(1)) - 8&
  &*a(pos+2*off(2)-  off(1)) &
            &      + 8*a(pos-2*off(2)+  off(1)) - 128*a(pos-off(2)+  off(1)) + 240*a(pos+  off(1)) - 128*a(pos+off(2)+  off(1)) + 8&
  &*a(pos+2*off(2)+  off(1)) &
            &      -   a(pos-2*off(2)+2*off(1)) +  16*a(pos-off(2)+2*off(1)) -  30*a(pos+2*off(1)) +  16*a(pos+off(2)+2*off(1)) -  &
  & a(pos+2*off(2)+2*off(1))) / (144*dx(2)**2*dx(1))
       f(:,1,2) = f(:,2,1)
       f(1,2,2) = f(2,2,1)
       f(2,2,2) = (a(pos-3*off(2)) - 8*a(pos-2*off(2)) + 13*a(pos-off(2)) - 13*a(pos+off(2)) + 8*a(pos+2*off(2)) - a(pos+3*off(2)))&
  & / (8*dx(2)**3)
    case default
       call CCTK_Warn(0,106,"pointwise2.F90","TGRtensor", "Unsupported finite differencing order")
    end select
  end subroutine get_2derivs3

  subroutine calc_2position (shape, i,j, pos)
    integer, intent(in)  :: shape(2)
    integer, intent(in)  :: i,j
    integer, intent(out) :: pos
    pos = 1 + i-1 + shape(1) * (j-1)
  end subroutine calc_2position
  subroutine calc_2offsets (shape, off)
    integer, intent(in)  :: shape(2)
    integer, intent(out) :: off(2)
    off(1) = 1
    off(2) = shape(1)
  end subroutine calc_2offsets
  subroutine get_2scalar (a, f, pos)
    REAL*8, intent(in)  :: a(*)
    REAL*8, intent(out) :: f
    integer,   intent(in)  :: pos
    f = a(pos)
  end subroutine get_2scalar
  subroutine get_2vector (ax,ay, f, pos)
    REAL*8, intent(in)  :: ax(*),ay(*)
    REAL*8, intent(out) :: f(2)
    integer,   intent(in)  :: pos
    f(1) = ax(pos)
    f(2) = ay(pos)
  end subroutine get_2vector
  subroutine get_2tensor (axx,axy,ayy, f, pos)
    REAL*8, intent(in)  :: axx(*),axy(*),ayy(*)
    REAL*8, intent(out) :: f(2,2)
    integer,   intent(in)  :: pos
    f(1,1) = axx(pos)
    f(1,2) = axy(pos)
    f(2,1) = axy(pos)
    f(2,2) = ayy(pos)
  end subroutine get_2tensor
  subroutine set_2scalar (f, a, pos)
    REAL*8, intent(in) :: f
    REAL*8             :: a(*)
    integer,   intent(in) :: pos
    a(pos) = f
  end subroutine set_2scalar
  subroutine set_2vector (f, ax,ay, pos)
    REAL*8, intent(in) :: f(2)
    REAL*8             :: ax(*),ay(*)
    integer,   intent(in) :: pos
    ax(pos) = f(1)
    ay(pos) = f(2)
  end subroutine set_2vector
  subroutine set_2tensor (f, axx,axy,ayy, pos)
    REAL*8, intent(in) :: f(2,2)
    REAL*8             :: axx(*),axy(*),ayy(*)
    integer,   intent(in) :: pos
    axx(pos) = f(1,1)
    axy(pos) = f(1,2)
    ayy(pos) = f(2,2)
  end subroutine set_2tensor
  subroutine get_2scalarderivs (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    call get_2derivs (a, f, pos, off, dx, order)
  end subroutine get_2scalarderivs
  subroutine get_2vectorderivs (ax,ay, f, pos, off, dx, order)
    REAL*8, intent(in)            :: ax(*),ay(*)
    REAL*8, intent(out)           :: f(2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fx(2),fy(2)
    call get_2derivs (ax, fx, pos, off, dx, order)
    call get_2derivs (ay, fy, pos, off, dx, order)
    f(1,:) = fx
    f(2,:) = fy
  end subroutine get_2vectorderivs
  subroutine get_2tensorderivs (axx,axy,ayy, f, pos, off, dx, order)
    REAL*8, intent(in)            :: axx(*),axy(*),ayy(*)
    REAL*8, intent(out)           :: f(2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fxx(2),fxy(2),fyy(2)
    call get_2derivs (axx, fxx, pos, off, dx, order)
    call get_2derivs (axy, fxy, pos, off, dx, order)
    call get_2derivs (ayy, fyy, pos, off, dx, order)
    f(1,1,:) = fxx
    f(1,2,:) = fxy
    f(2,1,:) = fxy
    f(2,2,:) = fyy
  end subroutine get_2tensorderivs
  subroutine get_2scalarderivs2 (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    call get_2derivs2 (a, f, pos, off, dx, order)
  end subroutine get_2scalarderivs2
  subroutine get_2vectorderivs2 (ax,ay, f, pos, off, dx, order)
    REAL*8, intent(in)            :: ax(*),ay(*)
    REAL*8, intent(out)           :: f(2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fx(2,2),fy(2,2)
    call get_2derivs2 (ax, fx, pos, off, dx, order)
    call get_2derivs2 (ay, fy, pos, off, dx, order)
    f(1,:,:) = fx
    f(2,:,:) = fy
  end subroutine get_2vectorderivs2
  subroutine get_2tensorderivs2 (axx,axy,ayy, f, pos, off, dx, order)
    REAL*8, intent(in)            :: axx(*),axy(*),ayy(*)
    REAL*8, intent(out)           :: f(2,2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fxx(2,2),fxy(2,2),fyy(2,2)
    call get_2derivs2 (axx, fxx, pos, off, dx, order)
    call get_2derivs2 (axy, fxy, pos, off, dx, order)
    call get_2derivs2 (ayy, fyy, pos, off, dx, order)
    f(1,1,:,:) = fxx
    f(1,2,:,:) = fxy
    f(2,1,:,:) = fxy
    f(2,2,:,:) = fyy
  end subroutine get_2tensorderivs2
  subroutine get_2scalarderivs3 (a, f, pos, off, dx, order)
    REAL*8, intent(in)            :: a(*)
    REAL*8, intent(out)           :: f(2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    call get_2derivs3 (a, f, pos, off, dx, order)
  end subroutine get_2scalarderivs3
  subroutine get_2vectorderivs3 (ax,ay, f, pos, off, dx, order)
    REAL*8, intent(in)            :: ax(*),ay(*)
    REAL*8, intent(out)           :: f(2,2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fx(2,2,2),fy(2,2,2)
    call get_2derivs3 (ax, fx, pos, off, dx, order)
    call get_2derivs3 (ay, fy, pos, off, dx, order)
    f(1,:,:,:) = fx
    f(2,:,:,:) = fy
  end subroutine get_2vectorderivs3
  subroutine get_2tensorderivs3 (axx,axy,ayy, f, pos, off, dx, order)
    REAL*8, intent(in)            :: axx(*),axy(*),ayy(*)
    REAL*8, intent(out)           :: f(2,2,2,2,2)
    integer,   intent(in)            :: pos, off(2)
    REAL*8, intent(in)            :: dx(2)
    integer,   intent(in),  optional :: order
    REAL*8 :: fxx(2,2,2),fxy(2,2,2),fyy(2,2,2)
    call get_2derivs3 (axx, fxx, pos, off, dx, order)
    call get_2derivs3 (axy, fxy, pos, off, dx, order)
    call get_2derivs3 (ayy, fyy, pos, off, dx, order)
    f(1,1,:,:,:) = fxx
    f(1,2,:,:,:) = fxy
    f(2,1,:,:,:) = fxy
    f(2,2,:,:,:) = fyy
  end subroutine get_2tensorderivs3
end module pointwise2
