      SUBROUTINE chkdiv

      IMPLICIT NONE

      INCLUDE 'param.txt'
      INCLUDE 'common.txt'

      REAL diverg,divmax,divbar

c--------------------------------------------------------------------

      divbar = 0.0
      divmax = 0.0

      DO k=1,kmax
         DO j=1,jmax
            DO i=1,imax

               diverg = 
     1  (3.0*rhonew(i,j,k)-4.0*rhold1(i,j,k)+rhold2(i,j,k))/(2.0*dt) *
     1   rp(i)*dr(i)*dphi*dz(k)
     +         +
     2  (ru(i)*0.5*(rhonew(i+1,j,k)+rhonew(i,j,k))*unew(i,j,k) - 
     2   ru(i-1)*0.5*(rhonew(i,j,k)+rhonew(i-1,j,k))*unew(i-1,j,k)) * 
     2   dphi*dz(k)
     +         +
     3  (0.5*(rhonew(i,j+1,k)+rhonew(i,j,k))*vnew(i,j,k) - 
     3   0.5*(rhonew(i,j,k)+rhonew(i,j-1,k))*vnew(i,j-1,k)) * 
     3   dr(i)*dz(k)
     +         +
     4  (0.5*(rhonew(i,j,k+1)+rhonew(i,j,k))*wnew(i,j,k) - 
     4   0.5*(rhonew(i,j,k)+rhonew(i,j,k-1))*wnew(i,j,k-1)) *
     4   rp(i)*dphi*dr(i)

               divbar = divbar + diverg
               diverg = ABS(diverg)
               divmax = MAX( divmax , diverg )


c               if(diverg .gt. 1e-8)then
c                  print*,i,j,k,diverg
c               end if


               
            END DO
         END DO
      END DO

      WRITE(6,100) divbar,divmax
100   FORMAT('Mass loss/gain : Tot = ',e13.6,
     +                      '  Max = ',e13.6)

      RETURN
      END
