c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     #############################################################
c     ##                                                         ##
c     ##  function energy  --  evaluates energy terms and total  ##
c     ##                                                         ##
c     #############################################################
c
c
c     "energy" calls the subroutines to calculate the potential
c     energy terms and sums up to form the total energy
c
c
      function energy ()
      implicit none
      include 'sizes.i'
      include 'bound.i'
      include 'cutoff.i'
      include 'energi.i'
      include 'potent.i'
      include 'warp.i'
      real*8 energy
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     maintain any periodic boundary conditions
c
      if (use_bounds)  call bounds
c
c     alter bond and torsion constants for pisystem
c
      if (use_orbit)  call piscf
c
c     call the local geometry energy component routines
c
      if (use_bond)  call ebond
      if (use_angle)  call eangle
      if (use_strbnd)  call estrbnd
      if (use_urey)  call eurey
      if (use_angang)  call eangang
      if (use_opbend)  call eopbend
      if (use_improp)  call eimprop
      if (use_imptor)  call eimptor
      if (use_tors)  call etors
      if (use_strtor)  call estrtor
c     if (use_tortor)  call etortor
c
c     call the van der Waals energy component routines
c
      if (use_lights) then
         if (use_buck)  call ebuck4
         if (use_lj)  call elj4
         if (use_hal)  call ehal4
      else
         if (use_buck)  call ebuck
         if (use_lj)  call elj
         if (use_hal)  call ehal
      end if
      if (use_gauss)  call egauss
c
c     call the electrostatic energy component routines
c
      if (use_charge) then
         if (use_deform) then
            call echarge6
         else if (use_lights) then
            call echarge4
         else
            call echarge
         end if
      end if
      if (use_chgdpl)  call echgdpl
      if (use_dipole)  call edipole
      if (use_mpole .or. use_polar)  call empole
      if (use_rxnfld)  call erxnfld
c
c     call any miscellaneous energy component routines
c
      if (use_solv)  call esolv
      if (use_geom)  call egeom
      if (use_extra)  call extra
c
c     sum up to give the total potential energy
c
      energy = eb + ea + eba + eub + eaa + eopb + eid
     &            + eit + et + ebt + ett + ev + e14 + ec
     &            + ecd + ed + em + ep + er + es + eg + ex
      return
      end
