c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  subroutine mdrest  --  stop translation & rotation of CM  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "mdrest" computes and then removes any translational
c     or rotational kinetic energy of the center of mass
c
c
      subroutine mdrest
      implicit none
      include 'sizes.i'
      include 'atmtyp.i'
      include 'atoms.i'
      include 'bound.i'
      include 'inform.i'
      include 'iounit.i'
      include 'moldyn.i'
      include 'units.i'
      integer i,j
      real*8 etrans,erot
      real*8 weight,total
      real*8 xcm,ycm,zcm
      real*8 xx,xy,xz,yy,yz,zz
      real*8 xdiff,ydiff,zdiff
      real*8 mang(3),vang(3)
      real*8 vcm(3),tensor(3,3)
c
c
c     compute the linear velocity of the center of mass
c
      total = 0.0d0
      do j = 1, 3
         vcm(j) = 0.0d0
      end do
      do i = 1, n
         weight = mass(i)
         total = total + weight
         do j = 1, 3
            vcm(j) = vcm(j) + v(j,i)*weight
         end do
      end do
c
c     compute translational kinetic energy of center of mass
c
      etrans = 0.0d0
      do j = 1, 3
         vcm(j) = vcm(j) / total
         etrans = etrans + vcm(j)**2
      end do
      etrans = 0.5d0 * etrans * total / convert
c
c     eliminate any translation of the center of mass
c
      do i = 1, n
         do j = 1, 3
            v(j,i) = v(j,i) - vcm(j)
         end do
      end do
c
c     print the linear velocity of the center of mass
c
      if (verbose) then
         write (iout,10)  (vcm(i),i=1,3),etrans
   10    format (/,' Linear Velocity of CM : ',3d12.2,
     &           /,' Translational Kinetic Energy :',f20.4,
     &              ' Kcal/mole')
      end if
c
c     compute the angular momentum about the center of mass
c
      if (.not. use_bounds) then
         xcm = 0.0d0
         ycm = 0.0d0
         zcm = 0.0d0
         do j = 1, 3
            mang(j) = 0.0d0
         end do
         do i = 1, n
            weight = mass(i)
            xcm = xcm + x(i)*weight
            ycm = ycm + y(i)*weight
            zcm = zcm + z(i)*weight
            mang(1) = mang(1) + (y(i)*v(3,i)-z(i)*v(2,i))*weight
            mang(2) = mang(2) + (z(i)*v(1,i)-x(i)*v(3,i))*weight
            mang(3) = mang(3) + (x(i)*v(2,i)-y(i)*v(1,i))*weight
         end do
         xcm = xcm / total
         ycm = ycm / total
         zcm = zcm / total
         mang(1) = mang(1) - (ycm*vcm(3)-zcm*vcm(2))*total
         mang(2) = mang(2) - (zcm*vcm(1)-xcm*vcm(3))*total
         mang(3) = mang(3) - (xcm*vcm(2)-ycm*vcm(1))*total
c
c     calculate and then invert the inertial tensor
c
         xx = 0.0d0
         xy = 0.0d0
         xz = 0.0d0
         yy = 0.0d0
         yz = 0.0d0
         zz = 0.0d0
         do i = 1, n
            weight = mass(i)
            xdiff = x(i) - xcm
            ydiff = y(i) - ycm
            zdiff = z(i) - zcm
            xx = xx + xdiff*xdiff*weight
            xy = xy + xdiff*ydiff*weight
            xz = xz + xdiff*zdiff*weight
            yy = yy + ydiff*ydiff*weight
            yz = yz + ydiff*zdiff*weight
            zz = zz + zdiff*zdiff*weight
         end do
         tensor(1,1) = yy + zz
         tensor(2,1) = -xy
         tensor(3,1) = -xz
         tensor(1,2) = -xy
         tensor(2,2) = xx + zz
         tensor(3,2) = -yz
         tensor(1,3) = -xz
         tensor(2,3) = -yz
         tensor(3,3) = xx + yy
         call invert (3,3,tensor)
c
c     compute angular velocity and rotational kinetic energy
c
         erot = 0.0d0
         do i = 1, 3
            vang(i) = 0.0d0
            do j = 1, 3
               vang(i) = vang(i) + tensor(i,j)*mang(j)
            end do
            erot = erot + vang(i)*mang(i)
         end do
         erot = 0.5d0 * erot / convert
c
c     eliminate any rotation about the center of mass
c
         do i = 1, n
            xdiff = x(i) - xcm
            ydiff = y(i) - ycm
            zdiff = z(i) - zcm
            v(1,i) = v(1,i) - vang(2)*zdiff + vang(3)*ydiff
            v(2,i) = v(2,i) - vang(3)*xdiff + vang(1)*zdiff
            v(3,i) = v(3,i) - vang(1)*ydiff + vang(2)*xdiff
         end do
c
c     print the angular velocity about the center of mass
c
         if (verbose) then
            write (iout,20)  (vang(i),i=1,3),erot
   20       format (/,' Angular Velocity of CM :',3d12.2,
     &              /,' Rotational Kinetic Energy :',3x,f20.4,
     &                 ' Kcal/mole')
         end if
      end if
      return
      end
