c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###########################################################
c     ##                                                       ##
c     ##  subroutine ebond  --  bond stretch potential energy  ##
c     ##                                                       ##
c     ###########################################################
c
c
c     "ebond" calculates the bond stretching energy
c
c
      subroutine ebond
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'bndpot.i'
      include 'bond.i'
      include 'energi.i'
      include 'group.i'
      include 'usage.i'
      integer i,k,ibond
      real*8 e,ideal,force
c jcc real*8 expterm,bde
      real*8 expterm,bondde,disoc,alpha,alpha2
c jcc
      real*8 dt,dt2,fgrp
      real*8 xr,yr,zr,rik,rik2
      logical proceed
      logical usebde
c
c
c     zero out the bond stretching energy
c
      eb = 0.0d0
c
c     calculate the bond stretching energy term
c
      do ibond = 1, nbond
         i = ibnd(1,ibond)
         k = ibnd(2,ibond)
         ideal = bl(ibond)
         force = bk(ibond)
c jcc
         bondde = bde(ibond)
         usebde=.false.
         if (bondde.gt.0.0d0) usebde=.true.
c
c     decide whether to compute the current interaction
c
         proceed = .true.
         if (use_group)  call groups (proceed,fgrp,2,i,k,0,0,0)
         if (proceed)  proceed = (use(i) .or. use(k))
c
c     compute the value of the bond length deviation
c
         if (proceed) then
            xr = x(i) - x(k)
            yr = y(i) - y(k)
            zr = z(i) - z(k)
            rik2 = xr*xr + yr*yr + zr*zr
            rik = sqrt(rik2)
            dt = rik - ideal
c
c     Morse potential; energy = BDE * (1 - e**(-alpha*dt))**2)
c     with alpha = sqrt(ForceConst/BDE) 
c     and BDE = Bond Dissociation Energy 
c     If BDE is not given alpha = 2 
c
c jcc       if (bndtyp .eq. 'MORSE') then
            if ((bndtyp .eq. 'MORSE').or.(usebde)) then
               if (usebde) then
                  disoc = bondde * bdeunit
                  alpha = sqrt(force*bndunit/disoc)
               else
                  alpha = 2.d0
                  alpha2 = alpha*alpha
                  disoc = bndunit * force / alpha2
               endif
               expterm = exp(-alpha*dt)
               e = disoc * (1.0d0-expterm)**2
c
c     Taylor series expansion of Morse potential through
c     the fourth power of the bond length deviation
c
            else
               dt2 = dt * dt
               e = bndunit * force * dt2 * (1.0d0+cbnd*dt+qbnd*dt2)
            endif
c
c     scale the interaction based on its group membership
c
            if (use_group)  e = e * fgrp
c
c     increment the total bond stretching energy
c
            eb = eb + e
         end if
      end do
      return
      end
