recursive subroutine propagate(lp,level,base_grid,base,finest_level, &
                               g1,g2,g3,g4)
use adaptive
use parameters
real, dimension(:,:,:) :: base_grid
real, dimension(:,:,:,:), target :: g1,g2,g3,g4
type(ADAPTIVE_MESH), pointer :: base
integer     :: level,finest_level
type(LEVEL_INFO), dimension(maxlevel) :: lp

real, dimension(:,:,:), pointer :: gridpointer
type(ADAPTIVE_MESH), pointer :: grid,child,sibling,parent

!HPF$ PROCESSORS P(2,2)
!HPF$ DISTRIBUTE (*,BLOCK,BLOCK) onto P :: base_grid
!HPF$ DISTRIBUTE (*,BLOCK,BLOCK) onto P :: gridpointer
!HPF$ DISTRIBUTE (*,*,BLOCK,BLOCK) onto P :: g1,g2,g3,g4

INTERFACE
  subroutine update(a,t,dr)
  real, dimension(:,:,:) :: a
  real t
  real, dimension(0:2) :: dr
!HPF$ PROCESSORS P(2,2)
!HPF$ DISTRIBUTE *(*,BLOCK,BLOCK) onto *P :: a
  end subroutine update

END INTERFACE

write(6,*) 'PROPAGATE at level',level

if(.not.associated(base)) then
write(6,*) '???'
stop
endif


do istep=1,2
!write(6,*) 'step',istep,' on level',level

! do one step on the current level 
! update all grids on the current level, from 1 to curmax
!   write(6,*) lp%curmax

   if(level == 1) then
     call update(base_grid,base%t,lp(level)%dr)
   else
     grid=>lp(level)%head

     DO WHILE (associated(grid))
     select case(grid%grid_id)
       case(1)
         call update(g1(level,:,:,:),grid%t,lp(level)%dr)
       case(2)
         call update(g2(level,:,:,:),grid%t,lp(level)%dr)
       case(3)
         call update(g3(level,:,:,:),grid%t,lp(level)%dr)
       case(4)
         call update(g4(level,:,:,:),grid%t,lp(level)%dr)
       case default
         write(6,*) 'unexpected grid id'
      end select
      grid=>grid%sibling
      ENDDO ! WHILE
    endif
 
! if there are finer levels, update them ...

     if(level < finest_level) then
     call propagate(lp,level+1,base_grid,base,finest_level,g1,g2,g3,g4)
     endif
enddo

! update coarser level
!if(level>1) then
!grid=>lp(level)%head
!DO WHILE(associated(grid))
! parent=>grid%parent
! grid_id=grid%grid_id
! prnt_id=parent%grid_id
!write(6,*) 'UPDATE:',grid_id,prnt_id
!grid=>grid%sibling
!enddo 
!endif
return
end








