c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###########################################################
c     ##                                                       ##
c     ##  subroutine pressure  --  maintain constant pressure  ##
c     ##                                                       ##
c     ###########################################################
c
c
c     "pressure" uses the internal virial to find the pressure
c     in a periodic box and maintains a constant desired pressure
c     by scaling the coordinates via coupling to an external
c     constant pressure bath
c
c
      subroutine pressure (dt,pres,vol,e_kin)
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'bath.i'
      include 'boxes.i'
      include 'units.i'
      include 'usage.i'
      include 'virial.i'
      integer i
      real*8 dt,e_kin,pres,vol
      real*8 virial,third,scale
c
c
c     find the volume of the periodic box
c
      if (orthogonal) then
         vol = xbox * ybox * zbox
      else if (monoclinic) then
         vol = beta_sin * xbox * ybox * zbox
      else if (triclinic) then
         vol = (gamma_sin*gamma_term) * xbox * ybox * zbox
      else if (octahedron) then
         vol = 0.5d0 * xbox * ybox * zbox
      end if
c
c     compute the value of the internal virial and pressure
c
      virial = virx + viry + virz
      pres = (2.0d0*e_kin - virial) / (3.0d0*vol)
      pres = pres * prescon
c
c     find the scale factor to maintain constant pressure
c
      third = 1.0d0 / 3.0d0
      scale = (1.0d0 + (dt*compress/taupres)*(pres-atmsph))**third
c
c     alter the size of the periodic box
c
      xbox = scale * xbox
      ybox = scale * ybox
      zbox = scale * zbox
      xbox2 = 0.5d0 * xbox
      ybox2 = 0.5d0 * ybox
      zbox2 = 0.5d0 * zbox
      if (octahedron)  box34 = 0.75d0 * xbox
c
c     couple to external pressure bath via atom scaling
c
      do i = 1, n
         if (use(i)) then
            x(i) = scale * x(i)
            y(i) = scale * y(i)
            z(i) = scale * z(i)
         end if
      end do
      return
      end
