c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  subroutine ebond1  --  bond stretch energy & derivatives  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "ebond1" calculates the bond stretching energy and
c     first derivatives with respect to Cartesian coordinates
c
c
      subroutine ebond1
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'bath.i'
      include 'bndpot.i'
      include 'bond.i'
      include 'deriv.i'
      include 'energi.i'
      include 'group.i'
      include 'usage.i'
      include 'virial.i'
      integer i,k,ibond
      real*8 e,ideal,force
c jcc real*8 expterm,bde,fgrp
      real*8 expterm,fgrp,bondde,disoc,alpha,alpha2
c jcc
      real*8 dt,dt2,deddt
      real*8 de,dedx,dedy,dedz
      real*8 xr,yr,zr,rik,rik2
      logical proceed
      logical usebde
c
c
c     zero out the bond energy and first derivatives
c
      eb = 0.0d0
      do i = 1, n
         deb(1,i) = 0.0d0
         deb(2,i) = 0.0d0
         deb(3,i) = 0.0d0
      end do
c
c     calculate the bond stretch energy and first derivatives
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
               deddt = 2.0d0 * alpha * disoc * (1.0d0-expterm) * 
     &                 expterm
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)
               deddt = 2.0d0 * bndunit * force * dt
     &                    * (1.0d0+1.5d0*cbnd*dt+2.0d0*qbnd*dt2)
            endif
c
c     scale the interaction based on its group membership
c
            if (use_group) then
               e = e * fgrp
               deddt = deddt * fgrp
            end if
c
c     compute chain rule terms needed for derivatives
c
            if (rik .eq. 0.0d0) then
               de = 0.0d0
            else
               de = deddt / rik
            end if
            dedx = de * xr
            dedy = de * yr
            dedz = de * zr
c
c     increment the total bond energy and first derivatives
c
            eb = eb + e
            deb(1,i) = deb(1,i) + dedx
            deb(2,i) = deb(2,i) + dedy
            deb(3,i) = deb(3,i) + dedz
            deb(1,k) = deb(1,k) - dedx
            deb(2,k) = deb(2,k) - dedy
            deb(3,k) = deb(3,k) - dedz
c
c     increment the virial for use in pressure computation
c
            if (isobaric) then
               virx = virx + xr*dedx
               viry = viry + yr*dedy
               virz = virz + zr*dedz
            end if
         end if
      end do
      return
      end
