      module amr
   
      use amr_param 
      use refinement_control

       type LEVEL_INFO
         type(ADAPTIVE_MESH),pointer :: ml
         type(CLUSTER),pointer       :: clhead
         real, dimension(3)        :: ldr
         real                      :: ldt
         integer                     :: lrgint
         integer                     :: lsteps
         integer                     :: lbwx
         integer                     :: lbwy
         integer                     :: lbwz
         real                      :: rcxshp
         real                      :: rcyshp
         real                      :: rczshp
         real                      :: rcrhtm
         integer                     :: nclusters
       end type LEVEL_INFO

c    ml    : pointer to first grid on the level
c    ldr   : spatial mesh spacing on the level
c    ldt   : temporal mash spacing on the level 
c    lrgint: regrid every lrgint steps on the level
c    lsteps: total # of time steps taken on this level
c    lbw   : regriding buffer width

      contains

      subroutine initialize_level_info(lp)
      type(LEVEL_INFO),dimension(maxlev) :: lp 
      real dr1

       do i=1,maxlev
        nullify(lp(i)%ml)
        nullify(lp(i)%clhead)
        lp(i)%lsteps = 0
        lp(i)%nclusters=0
       enddo

       dr1=1.0d0 0

       lp%rcxshp = rcxshp
       lp%rcyshp = rcxshp
       lp%rczshp = rcxshp
       lp%lrgint = rcrgin
       lp%rcrhtm = rcrhtm

       lp(1)%rcxshp = 0
       lp(1)%rcyshp = 0
       lp(1)%rczshp = 0
       lp(1)%rcrhtm = 0

       lp%ldr(1)=0.0
       lp%ldr(2)=0.0
       lp%ldr(3)=0.0
       lp%ldt=0.0
       lp%lbwx=0
       lp%lbwy=0
       lp%lbwz=0

       lp(1)%ldr = dr1
       lp(1)%ldt=lp(1)%ldr(1) * dtbydr


       if(maxlev.ge.2) then
          do lev = 2, maxlev
            lp(lev)%ldr(1)=lp(lev-1)%ldr(1)/lp(lev)%rcxshp
            lp(lev)%ldr(2)=lp(lev-1)%ldr(2)/lp(lev)%rcxshp
            lp(lev)%ldr(3)=lp(lev-1)%ldr(3)/lp(lev)%rcxshp
            lp(lev)%ldt=lp(lev-1)%ldt/lp(lev)%rcrhtm
            lp(lev)%lrgint=lp(lev)%lrgint
            lp(lev)%lbwx=lp(lev)%rcxshp * bufwid
            lp(lev)%lbwy=lp(lev)%rcyshp * bufwid
            lp(lev)%lbwz=lp(lev)%rczshp * bufwid
          enddo
       endif

      return
      end subroutine initialize_level_info


      subroutine clear_cluster_structure(lp)
      type(LEVEL_INFO),array(maxlev)  :: lp
 
      type(CLUSTER),pointer           :: cls,c 
      integer                         :: lev 

       do lev=1,maxlev

  20   continue
       if(.not.associated(lp(lev)%clhead)) cycle

       cls=>lp(lev)%clhead

       do i=1,lp(lev)%nclusters-1
       c=>cls%clnext
       deallocate(cls)
       cls=>c
       enddo  

       deallocate(c)
       nullify(lp(lev)%clhead)

       lp(lev)%nclusters=0
       enddo

       return
       end subroutine clear_cluster_structure



      end module amr










