c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ##################################################################
c     ##                                                              ##
c     ##  subroutine gradient  --  find energy & gradient components  ##
c     ##                                                              ##
c     ##################################################################
c
c
c     "gradient" calls subroutines to calculate the potential energy
c     and first derivatives with respect to Cartesian coordinates
c
c
      subroutine gradient (energy,derivs)
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'bath.i'
      include 'bound.i'
      include 'cutoff.i'
      include 'deriv.i'
      include 'energi.i'
      include 'inter.i'
      include 'potent.i'
      include 'virial.i'
      include 'warp.i'
      integer i,j
      real*8 energy,derivs(3,maxatm)
c
c
c     zero out each of the potential energy components
c
      eb = 0.0d0
      ea = 0.0d0
      eba = 0.0d0
      eub = 0.0d0
      eaa = 0.0d0
      eopb = 0.0d0
      eid = 0.0d0
      eit = 0.0d0
      et = 0.0d0
      ebt = 0.0d0
      ett = 0.0d0
      ev = 0.0d0
      e14 = 0.0d0
      ec = 0.0d0
      ecd = 0.0d0
      ed = 0.0d0
      em = 0.0d0
      ep = 0.0d0
      er = 0.0d0
      es = 0.0d0
      eg = 0.0d0
      ex = 0.0d0
c
c     zero out each of the first derivative components
c
      do i = 1, n
         do j = 1, 3
            deb(j,i) = 0.0d0
            dea(j,i) = 0.0d0
            deba(j,i) = 0.0d0
            deub(j,i) = 0.0d0
            deaa(j,i) = 0.0d0
            deopb(j,i) = 0.0d0
            deid(j,i) = 0.0d0
            deit(j,i) = 0.0d0
            det(j,i) = 0.0d0
            debt(j,i) = 0.0d0
            dett(j,i) = 0.0d0
            dev(j,i) = 0.0d0
            de14(j,i) = 0.0d0
            dec(j,i) = 0.0d0
            decd(j,i) = 0.0d0
            ded(j,i) = 0.0d0
            dem(j,i) = 0.0d0
            dep(j,i) = 0.0d0
            der(j,i) = 0.0d0
            des(j,i) = 0.0d0
            deg(j,i) = 0.0d0
            dex(j,i) = 0.0d0
         end do
      end do
c
c     maintain any periodic boundary conditions
c
      if (use_bounds)  call bounds
c
c     zero out the virial and the intermolecular energy
c
      if (isobaric) then
         virx = 0.0d0
         viry = 0.0d0
         virz = 0.0d0
      end if
      einter = 0.0d0
c
c     alter bond and torsion constants for pisystem
c
      if (use_orbit)  call piscf
c
c     call the local geometry energy and gradient routines
c
      if (use_bond)  call ebond1
      if (use_angle)  call eangle1
      if (use_strbnd)  call estrbnd1
      if (use_urey)  call eurey1
      if (use_angang)  call eangang1
      if (use_opbend)  call eopbend1
      if (use_improp)  call eimprop1
      if (use_imptor)  call eimptor1
      if (use_tors)  call etors1
      if (use_strtor)  call estrtor1
c     if (use_tortor)  call etortor1
c
c     call the van der Waals energy and gradient routines
c
      if (use_lights) then
         if (use_buck)  call ebuck5
         if (use_lj)  call elj5
         if (use_hal)  call ehal5
      else
         if (use_buck)  call ebuck1
         if (use_lj)  call elj1
         if (use_hal)  call ehal1
      end if
      if (use_gauss)  call egauss1
c
c     call the electrostatic energy and gradient routines
c
      if (use_charge) then
         if (use_deform) then
            call echarge7
         else if (use_lights) then
            call echarge5
         else
            call echarge1
         end if
      end if
      if (use_chgdpl)  call echgdpl1
      if (use_dipole)  call edipole1
      if (use_mpole .or. use_polar)  call empole1
      if (use_rxnfld)  call erxnfld1
c
c     call any miscellaneous energy and gradient routines
c
      if (use_solv)  call esolv1
      if (use_geom)  call egeom1
      if (use_extra)  call extra1
c
c     sum up to get the total energy and first derivatives
c
      energy = eb + ea + eba + eub + eaa + eopb + eid
     &            + eit + et + ebt + ett + ev + e14 + ec
     &            + ecd + ed + em + ep + er + es + eg + ex
      do i = 1, n
         do j = 1, 3
            derivs(j,i) = deb(j,i) + dea(j,i) + deba(j,i)
     &                       + deub(j,i) + deaa(j,i) + deopb(j,i)
     &                       + deid(j,i) + deit(j,i) + det(j,i)
     &                       + debt(j,i) + dett(j,i) + dev(j,i)
     &                       + de14(j,i) + dec(j,i) + decd(j,i)
     &                       + ded(j,i) + dem(j,i) + dep(j,i)
     &                       + der(j,i) + des(j,i) + deg(j,i)
     &                       + dex(j,i)
         end do
      end do
c      write (*,*) "Dd", ea,eb,ev
      return
      end
