























































































































 


































































































































































 


















































































































































































































 






























































 










 








































































































































































































































 










                                
                                

                                
                                

                                
                                

                                

























































































subroutine prolongate_3d_cc_real8_tvd ( &
     src, srcipadext, srcjpadext, srckpadext, srciext, srcjext, srckext, &
     dst, dstipadext, dstjpadext, dstkpadext, dstiext, dstjext, dstkext, &
     srcbbox, dstbbox, regbbox)

  implicit none

  REAL*8, parameter :: one=1, fourth=one/4

  integer srcipadext, srcjpadext, srckpadext
  integer srciext, srcjext, srckext
  REAL*8 src(srcipadext,srcjpadext,srckpadext)
  integer dstipadext, dstjpadext, dstkpadext
  integer dstiext, dstjext, dstkext
  REAL*8 dst(dstipadext,dstjpadext,dstkpadext)
  
  
  
  integer srcbbox(3,3), dstbbox(3,3), regbbox(3,3)

  character*1000 msg

  integer offsetlo, offsethi

  integer regiext, regjext, regkext

  integer dstifac, dstjfac, dstkfac

  integer srcioff, srcjoff, srckoff
  integer dstioff, dstjoff, dstkoff

  integer i, j, k
  integer i0, j0, k0
  integer fi, fj, fk
  integer d

  REAL*8 :: dlo, dup
  REAL*8 :: slopex, slopey, slopez

  do d=1,3
     if (srcbbox(d,3).eq.0 .or. dstbbox(d,3).eq.0 &
          .or. regbbox(d,3).eq.0) then
        call CCTK_Warn(0,97,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: stride is zero")
     end if
     if (srcbbox(d,3).le.regbbox(d,3) &
          .or. dstbbox(d,3).ne.regbbox(d,3)) then
        call CCTK_Warn(0,101,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: strides disagree")
     end if
     if (mod(srcbbox(d,3), dstbbox(d,3)).ne.0) then
        call CCTK_Warn(0,104,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: destination strides are not integer mul&
  &tiples of the source strides")
     end if





     if (regbbox(d,1).gt.regbbox(d,2)) then
        
        call CCTK_Warn(0,113,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: region extent is empty")
     end if
     regkext = (regbbox(d,2) - regbbox(d,1)) / regbbox(d,3) + 1
     dstkfac = srcbbox(d,3) / dstbbox(d,3)
     srckoff = (regbbox(d,1) - srcbbox(d,1)) / dstbbox(d,3)
     offsetlo = regbbox(d,3)
     if (mod(srckoff + 0, dstkfac).eq.0) then
        offsetlo = 0
        if (regkext.gt.1) then
           offsetlo = regbbox(d,3)
        end if
     end if
     offsethi = regbbox(d,3)
     if (mod(srckoff + regkext-1, dstkfac).eq.0) then
        offsethi = 0
        if (regkext.gt.1) then
           offsethi = regbbox(d,3)
        end if
     end if
     if (regbbox(d,1)-offsetlo.lt.srcbbox(d,1) &
          .or. regbbox(d,2)+offsethi.gt.srcbbox(d,2) &
          .or. regbbox(d,1).lt.dstbbox(d,1) &
          .or. regbbox(d,2).gt.dstbbox(d,2)) then
        call CCTK_Warn(0,136,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: region extent is not contained in array&
  & extent")
     end if
  end do

  if (srciext.ne.(srcbbox(1,2)-srcbbox(1,1))/srcbbox(1,3)+1 &
       .or. srcjext.ne.(srcbbox(2,2)-srcbbox(2,1))/srcbbox(2,3)+1 &
       .or. srckext.ne.(srcbbox(3,2)-srcbbox(3,1))/srcbbox(3,3)+1 &
       .or. dstiext.ne.(dstbbox(1,2)-dstbbox(1,1))/dstbbox(1,3)+1 &
       .or. dstjext.ne.(dstbbox(2,2)-dstbbox(2,1))/dstbbox(2,3)+1 &
       .or. dstkext.ne.(dstbbox(3,2)-dstbbox(3,1))/dstbbox(3,3)+1) then
     call CCTK_Warn(0,146,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: array sizes don't agree with bounding boxe&
  &s")
  end if

  if (any(srcbbox(:,3) / dstbbox(:,3) /= 2)) then
     call CCTK_Warn(0,150,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: refinement factor is not 2")
  end if

  if (any(mod(regbbox(:,3), 2) /= 0)) then
     call CCTK_Warn(0,154,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: region stride is not a multiple of 2")
  end if

  regiext = (regbbox(1,2) - regbbox(1,1)) / regbbox(1,3) + 1
  regjext = (regbbox(2,2) - regbbox(2,1)) / regbbox(2,3) + 1
  regkext = (regbbox(3,2) - regbbox(3,1)) / regbbox(3,3) + 1

  dstifac = 2 
  dstjfac = 2 
  dstkfac = 2 

  srcioff = (regbbox(1,1) - srcbbox(1,1) + regbbox(1,3) / 2) / dstbbox(1,3)
  srcjoff = (regbbox(2,1) - srcbbox(2,1) + regbbox(1,3) / 2) / dstbbox(2,3)
  srckoff = (regbbox(3,1) - srcbbox(3,1) + regbbox(1,3) / 2) / dstbbox(3,3)

  dstioff = (regbbox(1,1) - dstbbox(1,1)) / dstbbox(1,3)
  dstjoff = (regbbox(2,1) - dstbbox(2,1)) / dstbbox(2,3)
  dstkoff = (regbbox(3,1) - dstbbox(3,1)) / dstbbox(3,3)

  if (srcioff<0 .or. srcjoff<0 .or. srckoff<0) then
     call CCTK_Warn(0,174,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: source array offset is negative")
  end if

  if (dstioff<0 .or. dstjoff<0 .or. dstkoff<0) then
     call CCTK_Warn(0,178,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", "Internal error: destination array offset is negative")
  end if

  

  do k = 0, regkext-1
     k0 = (srckoff + k) / dstkfac
     fk = mod(srckoff + k, dstkfac)

     do j = 0, regjext-1
        j0 = (srcjoff + j) / dstjfac
        fj = mod(srcjoff + j, dstjfac)

        do i = 0, regiext-1
           i0 = (srcioff + i) / dstifac
           fi = mod(srcioff + i, dstifac)

           
           
           
           
           
           
           

           if ((i0  )<1 .or. (i0  )>( srciext) .or. (j0  )<1 .or. (j0  )>(srcjext) .or. (k0  )<1 .or. (k0  )>(srckext)) then
  write (msg, '(a, " array index out of bounds: shape is (",i4,",",i4,",",i4,"), index is (",i4,",",i4,",",i4,")")') ( "src"), ( sr&
  &ciext),(srcjext),(srckext), (i0  ),(j0  ),(k0  )
  call CCTK_Warn(0,203,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", msg)
  end if
           if ((i0+2)<1 .or. (i0+2)>( srciext) .or. (j0+2)<1 .or. (j0+2)>(srcjext) .or. (k0+2)<1 .or. (k0+2)>(srckext)) then
  write (msg, '(a, " array index out of bounds: shape is (",i4,",",i4,",",i4,"), index is (",i4,",",i4,",",i4,")")') ( "src"), ( sr&
  &ciext),(srcjext),(srckext), (i0+2),(j0+2),(k0+2)
  call CCTK_Warn(0,204,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", msg)
  end if

           dlo = src(i0+1,j0+1,k0+1) - src(i0+0,j0+1,k0+1)
           dup = src(i0+2,j0+1,k0+1) - src(i0+1,j0+1,k0+1)
           slopex = (2*fi-1) * fourth * minmod(dlo,dup)
           
           dlo = src(i0+1,j0+1,k0+1) - src(i0+1,j0+0,k0+1)
           dup = src(i0+1,j0+2,k0+1) - src(i0+1,j0+1,k0+1)
           slopey = (2*fj-1) * fourth * minmod(dlo,dup)
           
           dlo = src(i0+1,j0+1,k0+1) - src(i0+1,j0+1,k0+0)
           dup = src(i0+1,j0+1,k0+2) - src(i0+1,j0+1,k0+1)
           slopez = (2*fk-1) * fourth * minmod(dlo,dup)
           
           if ((dstioff+i+1)<1 .or. (dstioff+i+1)>( dstiext) .or. ( dstjoff+j+1)<1 .or. ( dstjoff+j+1)>(dstjext) .or. ( dstkoff+k+1&
  &)<1 .or. ( dstkoff+k+1)>(dstkext)) then
  write (msg, '(a, " array index out of bounds: shape is (",i4,",",i4,",",i4,"), index is (",i4,",",i4,",",i4,")")') ( "dst"), ( ds&
  &tiext),(dstjext),(dstkext), (dstioff+i+1),( dstjoff+j+1),( dstkoff+k+1)
  call CCTK_Warn(0,218,"prolongate_3d_cc_real8_tvd.F90","CarpetLib", msg)
  end if
           
           dst (dstioff+i+1, dstjoff+j+1, dstkoff+k+1) = &
                src(i0+1,j0+1,k0+1) + slopex + slopey + slopez
           
        end do
     end do
  end do
  
contains
  
  function minmod(a, b)
    
    REAL*8 minmod
    REAL*8 a, b
    
    if (a * b < 0) then
       minmod = 0
    else if (abs(a) < abs(b)) then
       minmod = a
    else
       minmod = b
    end if
    
  end function minmod
  
end subroutine prolongate_3d_cc_real8_tvd
