program driver
use parameters
use hpfdata
use adaptive
use types       

type(ADAPTIVE_MESH),pointer     :: mesh,parent
integer, dimension(3)           :: nr
real, dimension(3)              :: gmin,gmax,gd
character(256)                  :: grid_name
real(SP) t
integer (I4B) level,grid_id

real(SP) t1(NPROC), t2(NPROC)
!HPF$ PROCESSORS TPROC(NPROC)
!HPF$ DISTRIBUTE t1(BLOCK) onto TPROC
!HPF$ DISTRIBUTE t2(BLOCK) onto TPROC

INTERFACE
subroutine initialize_grid_functions(t)
use types
use adaptive
use hpfdata
real(SP)                         :: t 
end subroutine initialize_grid_functions

subroutine grid_step(level,t)
use types
use parameters
use adaptive
real(SP)        :: t
integer(I4B)    :: level
end subroutine grid_step

      extrinsic(hpf_local) subroutine timer(return_time, initial_time)
      use types
      real(SP), dimension(:), intent(in)  :: initial_time
      real(SP), dimension(:), intent(out) :: return_time
!HPF$ DISTRIBUTE *(block) :: initial_time
!HPF$ ALIGN return_time with initial_time
      end subroutine timer

END INTERFACE

call timer(t2,t1)

nullify(mesh)
nullify(parent)
call allocate_LEVEL_INFO

!initialize base grid
grid_name='test1'
gmin=1.0
gmax(1)=100.0
gmax(2)=100.0
gmax(3)=1.0
gd=1.0
level=1
t=0
nr(1)=int((gmax(1)-gmin(1)+gd(1))/gd(1) + 0.5)
nr(2)=int((gmax(2)-gmin(2)+gd(2))/gd(2) + 0.5)
nr(3)=int((gmax(3)-gmin(3)+gd(3))/gd(3) + 0.5)
write(6,*) nr

call allocate_ADAPTIVE_MESH(level,parent,mesh,t,gmin,gmax,nr)
write(6,*) mesh%grid_id

call initialize_grid_functions(t)

!write(6,*) glev1(1,1,1,:,:,:)

grid_name='test2'
gmin(1)=2.0
gmin(2)=2.0
gmin(3)=1.0
gmax(1)=4.0
gmax(2)=6.0
gmax(3)=1.0
gd  =0.5
level=2

nr(1)=int((gmax(1)-gmin(1)+gd(1))/gd(1) + 0.5)
nr(2)=int((gmax(2)-gmin(2)+gd(2))/gd(2) + 0.5)
nr(3)=int((gmax(3)-gmin(3)+gd(3))/gd(3) + 0.5)
gfrb(1,1,:)=1
gflb(1,1,:)=1


parent=>mesh
call allocate_ADAPTIVE_MESH(level,parent,mesh,t,gmin,gmax,nr)
write(6,*) mesh%grid_id

grid_name='test2'
gmin(1)=5.0
gmin(2)=7.0
gmin(3)=1.0
gmax(1)=9.0
gmax(2)=9.0
gmax(3)=1.0
gd  =0.5
level=2
gfrb(level,mesh%grid_id,:)=0
gflb(level,mesh%grid_id,:)=0

nr(1)=int((gmax(1)-gmin(1)+gd(1))/gd(1) + 0.5)
nr(2)=int((gmax(2)-gmin(2)+gd(2))/gd(2) + 0.5)
nr(3)=int((gmax(3)-gmin(3)+gd(3))/gd(3) + 0.5)
call allocate_ADAPTIVE_MESH(level,parent,mesh,t,gmin,gmax,nr)
write(6,*) mesh%grid_id
gfrb(level,mesh%grid_id,:)=0
gflb(level,mesh%grid_id,:)=0

do i=1,maxlevel
if(associated(lp(i)%head)) then
   mesh=>lp(i)%head
   DO WHILE (associated(mesh))
    write(6,*) mesh%rmin
    mesh=>mesh%sibling
   ENDDO
endif
enddo

level=1
do nsteps=1,10
call grid_step(level,t)
enddo

call timer(t1,t2)
print *,'time',t1,' seconds'

stop
end





