program main
use adaptive

type(ADAPTIVE_MESH), pointer :: parent,base
real, dimension(2) :: rmin 
real, dimension(2) :: rmax 
integer, dimension(2) :: nr 
real, dimension(0:2) :: dr 
integer :: level

real, dimension(:,:,:), target, allocatable :: base_grid
real, dimension(:,:,:,:,:), target, allocatable :: g
! grid function (level,grid_id, time_level,x,y) 
integer finest_level
type(LEVEL_INFO),dimension(maxlevel) :: lp
real t

!HPF$ PROCESSORS P(PX,PY)
!HPF$ DISTRIBUTE base_grid(*,BLOCK,BLOCK) onto P
!HPF$ DISTRIBUTE (*,*,*,BLOCK,BLOCK) onto P :: g

INTERFACE
recursive subroutine propagate(lp,level,base_grid,base,finest_level,g)
use adaptive
use parameters
real, dimension(:,:,:) :: base_grid
real, dimension(:,:,:,:,:) :: g
type(ADAPTIVE_MESH), pointer :: base
integer     :: level,finest_level
type(LEVEL_INFO), dimension(maxlevel) :: lp
!HPF$ PROCESSORS P(PX,PY)
!HPF$ DISTRIBUTE *(*,BLOCK,BLOCK) onto *P :: base_grid
!HPF$ DISTRIBUTE *(*,*,*,BLOCK,BLOCK) onto *P :: g
end subroutine propagate

subroutine regrid(base,base_grid,g,level,finest_level,lp)
use adaptive
use parameters
type(ADAPTIVE_MESH),pointer    :: base
real,dimension(:,:,:)          :: base_grid
real,dimension(:,:,:,:,:),target :: g
integer level,finest_level
type(LEVEL_INFO), dimension(maxlevel) :: lp
!HPF$ PROCESSORS P(PX,PY)
!HPF$ DISTRIBUTE *(*,BLOCK,BLOCK) onto *P :: base_grid
!HPF$ DISTRIBUTE *(*,*,*,BLOCK,BLOCK) onto *P :: g
end subroutine regrid

  
END INTERFACE

call allocate_LEVEL_INFO(lp)
nullify(base)
!allocate base_grid
nullify(parent)
rmin=(/1.0,1.0/)
rmax=(/100.0,100.0/)
nr=(/mx,my/)
level=1
t=0
call allocate_ADAPTIVE_MESH(lp,level,parent,base,t,rmin,rmax,nr)
allocate(base_grid(2,mx,my))

! initialize base grid
 base_grid(1:2,:,:)= 0


! allocate memory for fine grids
allocate(g(maxlevel,ngrds,2,mxf,myf))
level=1
finest_level=1

do i=1,20
write(6,*) '------------------------------------------'
write(6,*) 'coarse step no',i
! ================ regridding =====================================
!time for regridding?
if(mod(i,2)==1 .and. level < maxlevel) then
   call regrid(base,base_grid,g,level,finest_level,lp)
endif  
! ====================================================================
call propagate(lp,level,base_grid,base,finest_level,g) 
enddo

stop
end






