subroutine boundaries(var,nx,ny,nz)
!---------------------------------------------------
! set boundary conditions after grid update
!---------------------------------------------------

use types
real(SP), dimension(:,:,:)   :: var
integer(I4B)                 :: nx,ny,nz,boundtype

!     boundtype = 1 : periodic boundary conditions
!     boundtype = 2 : 0 boundary conditions (W3d)

      boundtype = 1

      if(boundtype .eq. 1) then

!     FACES
   
      write(6,*) 'nx=' , nx, 'ny=',ny, 'nz=',nz

      var(1,:,:) = var(nx-1,:,:)
      var(:,1,:) = var(:,ny-1,:)
      var(:,:,1) = var(:,:,nz-1)
      var(nx,:,:) = var(2,:,:)
      var(:,ny,:) = var(:,2,:)
      var(:,:,nz) = var(:,:,2)

!     X-LEGS
      var(:,1,1) = var(:,ny-1,nz-1)
      var(:,ny,1) = var(:,2,nz-1)
      var(:,1,nz) = var(:,ny-1,2)
      var(:,ny,nz) = var(:,2,2)
!     Y-LEGS
      var(1,:,1) = var(nx-1,:,nz-1)
      var(nx,:,1) = var(2,:,nz-1)
      var(1,:,nz) = var(nx-1,:,2)
      var(nx,:,nz) = var(2,:,2)
!     Z-LEGS
      var(1,1,:) = var(nx-1,ny-1,:)
      var(nx,1,:) = var(2,ny-1,:)
      var(1,ny,:) = var(nx-1,2,:)
      var(nx,ny,:) = var(2,2,:)

!     CUBE CORNERS
      var(1,1,1) = var(nx-1,ny-1,nz-1)
      var(1,1,nz) = var(nx-1,ny-1,2)
      var(1,ny,1) = var(nx-1,2,nz-1)
      var(1,ny,nz) = var(nx-1,2,2)
      var(nx,1,1) = var(2,ny-1,nz-1)
      var(nx,1,nz) = var(2,ny-1,2)
      var(nx,ny,1) = var(2,2,nz-1)
      var(nx,ny,nz) = var(2,2,2)
   
      endif
      if(boundtype .eq. 2) then

      var(1,:,:) = 0.0
      var(:,1,:) = 0.0
      var(:,:,1) = 0.0
      var(nx,:,:) = 0.0
      var(:,ny,:) = 0.0
      var(:,:,nz) = 0.0

!     X-LEGS
      var(:,1,1) = 0.0
      var(:,ny,1) = 0.0
      var(:,1,nz) = 0.0
      var(:,ny,nz) = 0.0
!     Y-LEGS
      var(1,:,1) = 0.0
      var(nx,:,1) = 0.0
      var(1,:,nz) = 0.0
      var(nx,:,nz) = 0.0
!     Z-LEGS
      var(1,1,:) = 0.0
      var(nx,1,:) = 0.0
      var(1,ny,:) = 0.0
      var(nx,ny,:) = 0.0

!     CUBE CORNERS
      var(1,1,1) = 0.0
      var(1,1,nz) = 0.0
      var(1,ny,1) = 0.0
      var(1,ny,nz) = 0.0
      var(nx,1,1) = 0.0
      var(nx,1,nz) = 0.0
      var(nx,ny,1) = 0.0
      var(nx,ny,nz) = 0.0

      endif
   

return
end
