c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ############################################################
c     ##                                                        ##
c     ##  subroutine temper  --  maintain constant temperature  ##
c     ##                                                        ##
c     ############################################################
c
c
c     "temper" maintains a constant desired temperature by scaling
c     the velocities via coupling to an external temperature bath
c
c
      subroutine temper (dt,temp)
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'bath.i'
      include 'moldyn.i'
      include 'usage.i'
      integer i,j
      real*8  temp,dt,scale
c
c
c     find the scale factor to maintain constant temperature
c
      if (temp .eq. 0.0d0)  temp = 0.1d0
      scale = sqrt(1.0d0 + (dt/tautemp)*(kelvin/temp-1.0d0))
c
c     couple to external temperature bath via velocity scaling
c
      do i = 1, n
         if (use(i)) then
            do j = 1, 3
               v(j,i) = scale * v(j,i)
            end do
         end if
      end do
      return
      end
