subroutine advance(t,finlev,icstep)
use gridfunctions
real t
integer istep,i2step,i3step,icstep
integer, parameter :: nsteps=2
integer levels,curlev,finlev
integer ipoint
real t2,t3
type(ADAPTIVE_MESH),pointer :: grid,gridbck

INTERFACE
subroutine update(curlev,nx,ny,nz,p)
use hpfprocessors
use param
integer :: curlev,nx,ny,nz
real, dimension(:,:,:,:) :: p

!HPF$ DISTRIBUTE p(*,BLOCK,*,*) onto PROC
end subroutine update

subroutine boundaries(t,curlev,istep,nx,ibound,id,p,pp)
use hpfprocessors
use param
real t
integer curlev,nx,id,istep
integer, dimension(3,2) :: ibound
real, dimension(:,:,:,:) :: p
real, dimension(:,:,:,:) :: pp

!HPF$ DISTRIBUTE p(*,BLOCK,*,*)
!HPF$ DISTRIBUTE pp(*,BLOCK,*,*)
end subroutine boundaries

subroutine inject(level,id,p,pp)
use hpfprocessors
use param
integer level,id
real, dimension(:,:,:,:) :: p
real, dimension(:,:,:,:) :: pp
!HPF$ DISTRIBUTE p(*,BLOCK,*,*)
!HPF$ DISTRIBUTE pp(*,BLOCK,*,*)
end subroutine inject

END INTERFACE

!if(icstep > 12) stop
!if(mod(icstep,2) == 1) call regrid(t,finlev)
call regrid(t,finlev)

do istep=1,nsteps


curlev=1
!p=>g1
nx=lp(curlev)%grid%nr(1)
ny=lp(curlev)%grid%nr(2)
nz=lp(curlev)%grid%nr(3)
!write(6,*) 'update level',curlev,nx,ny,nz
call update(curlev,nx,ny,nz,g1)
write(6,*) g1(1,3,1:4,1), 'base grid (11)'
t=t+lp(curlev)%dt
id=1
call base_bound(t,curlev,nx,lp(1)%grid%ibound,id)
write(6,*) g1(1,3,1:4,1), 'with bond (11)'
!pp=>g1
if(finlev > 1) then
  t2=t-lp(curlev)%dt
  curlev=2
write(6,*) 'start processing level',curlev
  do i2step=1,nsteps
write(6,*) 'i2step=',i2step,' on level',curlev   
   t2=t2+lp(curlev)%dt
   if(allocated(g21)) then
    grid=>lp(curlev)%grid 
!    p=>g21
    nx=lp(curlev)%grid%nr(1)
    ny=lp(curlev)%grid%nr(2)
    nz=lp(curlev)%grid%nr(3)
it=(lp(2)%grid%rmin(2)-lp(1)%grid%rmin(2))+1
write(6,*) g1(2,3,it:it+3,1), 'shifted (11)'
write(6,*) g21(1,3,1:8:2,1), '21: init'
    call update(curlev,nx,ny,nz,g21)
write(6,*) g21(1,3,1:8:2,1), '21 updated'
    id=1
    call boundaries(t2,curlev,i2step,nx,lp(2)%grid%ibound,id,g21,g1)
it=(grid%rmin(2)-grid%parent%rmin(2))*rho+1
write(6,*) g21(1,3,1:8:2,1), '21 bound'

do kk=1,4
k1=(kk-1)*2+1
k2=k1+1
write(6,*) g21(1,3,k1:k2,1)
enddo

write(6,*) 'offset used',it
    if(associated(grid%child)) then
       gridbck=>grid
       grid=>grid%child
       t3=t2-lp(curlev)%dt
       curlev=curlev+1      
write(6,*) 'start processing level',curlev
           do i3step=1,nsteps
write(6,*) 'i3step=',i3step   
           t3=t3+lp(curlev)%dt
           if(allocated(g31)) then
!              p=>g31
              nx=lp(curlev)%grid%nr(1)
              ny=lp(curlev)%grid%nr(2)
              nz=lp(curlev)%grid%nr(3)
write(6,*) g31(1,3,1:16:4,1), '31: init'

do kk=1,4
k1=(kk-1)*4+1
k2=k1+3
write(6,*) g31(1,3,k1:k2,1)
enddo

              call update(curlev,nx,ny,nz,g31)
              id=1
write(6,*) g31(1,3,1:16:4,1), '31: updated'
              call boundaries(t3,curlev,i3step,nx,lp(3)%grid%ibound,id,g31,g21)
write(6,*) g31(1,3,1:16:4,1), '31: bound'


do kk=1,4
k1=(kk-1)*4+1
k2=k1+3
write(6,*) g31(1,3,k1:k2,1)
enddo
           endif

       if(allocated(g31)) g31(2,:,:,:)=g31(1,:,:,:)
       enddo  ! i3step   
write(6,*) 'i3step do exited'
it=(grid%rmin(2)-grid%parent%rmin(2))*rho+1
!write(6,*) g31(1,3,1:8:2,1), '31'
!write(6,*) g21(1,3,it:it+3,1), '21'
!if(t > 23.0) then
!  write(6,*) 'offset used',it
!  stop
!endif
!write(6,*) 'inject 31->21'
write(6,*) g31(1,3,1:16:4,1), '31: inject'
        call inject(curlev,id,g31,g21)
write(6,*) g21(1,3,it:it+6:2,1), '21injected'
       grid=>gridbck
       curlev=curlev-1  
     endif  !associated(grid%child)
   endif  !g21 
   if(allocated(g22)) then
!     p=>g22
    grid=>lp(curlev)%grid
    grid=>grid%neighbor
    nx=grid%nr(1)
    ny=grid%nr(2)
    nz=grid%nr(3)
    call update(curlev,nx,ny,nz,g22)
    id=2
    call boundaries(t2,curlev,i2step,nx,grid%ibound,id,g22,g1) 
    if(associated(grid%child)) then
       gridbck=>grid
       grid=>grid%child
       t3=t2-lp(curlev)%dt
       curlev=curlev+1      
       do i3step=1,nsteps
           t3=t3+lp(curlev)%dt
           if(allocated(g32)) then
!              p=>g32
              nx=lp(curlev)%grid%nr(1)
              ny=lp(curlev)%grid%nr(2)
              nz=lp(curlev)%grid%nr(3)
              call update(curlev,nx,ny,nz,g32)
              id=1
              call boundaries(t3,curlev,i3step,nx,lp(3)%grid%ibound,id,g32,g22)
           endif
       if(allocated(g32)) g32(2,:,:,:)=g32(1,:,:,:)
       enddo  ! i3step   
        call inject(curlev,id,g32,g22)
       grid=>gridbck
       curlev=curlev-1  
     endif  !associated(grid%child)
   endif 
   if(allocated(g23)) then
!     p=>g23
    grid=>lp(curlev)%grid
    grid=>grid%neighbor 
    nx=grid%nr(1)
    ny=grid%nr(2)
    nz=grid%nr(3)
    call update(curlev,nx,ny,nz,g23)
    id=3
    call boundaries(t2,curlev,i2step,nx,grid%ibound,id,g23,g1) 
    if(associated(grid%child)) then
       gridbck=>grid
       grid=>grid%child
       t3=t2-lp(curlev)%dt
       curlev=curlev+1      
       do i3step=1,nsteps
           t3=t3+lp(curlev)%dt
           if(allocated(g33)) then
!              p=>g33
              nx=lp(curlev)%grid%nr(1)
              ny=lp(curlev)%grid%nr(2)
              nz=lp(curlev)%grid%nr(3)
              call update(curlev,nx,ny,nz,g33)
              id=1
              call boundaries(t3,curlev,i3step,nx,lp(3)%grid%ibound,id,g33,g23)
           endif
       if(allocated(g33)) g33(2,:,:,:)=g33(1,:,:,:)
       enddo  ! i3step   
        call inject(curlev,id,g33,g23)
       grid=>gridbck
       curlev=curlev-1  
     endif  !associated(grid%child)
   endif

  if(allocated(g21)) g21(2,:,:,:)=g21(1,:,:,:)
  if(allocated(g22)) g22(2,:,:,:)=g22(1,:,:,:)
  if(allocated(g23)) g23(2,:,:,:)=g23(1,:,:,:)
  enddo !i2step
write(6,*) 'loop i2step exited'

   if(allocated(g21)) then
!    p=>g21
!    pp=>g1
    id=1
it=(lp(2)%grid%rmin(2)-lp(1)%grid%rmin(2))*rho+1
write(6,*) g1(1,3,it:it+3,1), 'TEST IT'
    call inject(curlev,id,g21,g1)
write(6,*) g1(1,3,it:it+3,1), '11 injected'
    endif

   if(allocated(g22)) then
!    p=>g22
!    pp=>g1
    id=2
    call inject(curlev,id,g22,g1)
    endif

   if(allocated(g23)) then
!    p=>g23
!    pp=>g1
    id=3
    call inject(curlev,id,g23,g1)
    endif

endif !finlev>1
g1(2,:,:,:)=g1(1,:,:,:)
call dumpdata(finlev)
write(6,*) g1(2,1,1:4,1)
write(6,*) 'end of istep=',istep,'  +++++++++++++++++++++++++++++++++++++'
enddo ! istep

return
end
