c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  subroutine echarge7  --  smoothed charge energy & derivs  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "echarge7" calculates the smoothed charge-charge interaction
c     energy and first derivatives with respect to Cartesian coordinates
c    
c
c
      subroutine echarge7
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'charge.i'
      include 'chgpot.i'
      include 'couple.i'
      include 'deriv.i'
      include 'energi.i'
      include 'group.i'
      include 'math.i'
      include 'shunt.i'
      include 'units.i'
      include 'usage.i'
      include 'warp.i'
      integer i,j,k,skip(maxatm)
      integer ii,kk,in,kn
      real*8 e,de,f,fi,fik,r,r2,fgrp
      real*8 xi,yi,zi,xr,yr,zr
      real*8 dedx,dedy,dedz
      real*8 erf,erfterm
      real*8 expcut,expterm
      real*8 defterm,time,time2
      logical proceed,iuse
c
c
c     zero out the charge interaction energy and derivatives
c
      ec = 0.0d0
      do i = 1, n
         dec(1,i) = 0.0d0
         dec(2,i) = 0.0d0
         dec(3,i) = 0.0d0
      end do
      if (nion .eq. 0)  return
c
c     zero out the list of atoms to be skipped
c
      do i = 1, n
         skip(i) = 0
      end do
c
c     use conversion factor that puts energy in desired units
c
      f = electric / dielec
c
c     set cutoff distances and switching function coefficients
c
      call switch ('CHARGE')
c
c     scale deformation time by the diffusion coefficient
c
      if (deform .ne. 0.0d0) then
         expcut = -50.0d0
         time = diffc * deform
         time2 = sqrt(time)
         defterm = 0.5d0 / time2
      end if
c
c     compute charge interaction energy and first derivatives
c
      do ii = 1, nion-1
         i = iion(ii)
         in = jion(ii)
         iuse = (use(i))
         skip(in) = i
         do j = 1, n12(in)
            skip(i12(j,in)) = i * chg12use
         end do
         do j = 1, n13(in)
            skip(i13(j,in)) = i * chg13use
         end do
         do j = 1, n14(in)
            skip(i14(j,in)) = i * chg14use
         end do
         xi = x(i)
         yi = y(i)
         zi = z(i)
         fi = f * pchg(ii)
         do kk = ii+1, nion
            k = iion(kk)
            kn = jion(kk)
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 = (iuse .or. use(k))
            if (proceed)  proceed = (skip(kn) .ne. i)
c
c     compute the energy contribution for this interaction
c
            if (proceed) then
               xr = xi - x(k)
               yr = yi - y(k)
               zr = zi - z(k)
               r2 = xr*xr + yr*yr + zr*zr
               if (r2 .le. off2) then
                  r = sqrt(r2)
                  fik = fi * pchg(kk)
                  if (skip(k) .eq. -i)  fik = fik / chgscale
                  e = fik / r
                  de = -fik / r2
c
c     potential smoothing via the diffusion equation method
c
                  if (deform .ne. 0.0d0) then
                     erfterm = erf(defterm*r)
                     expterm = -0.25d0 * r2 / time
                     if (expterm .gt. expcut) then
                        expterm = exp(expterm) / (sqrtpi*time2)
                     else
                        expterm = 0.0d0
                     end if
                     e = e * erfterm
                     de = de*erfterm + fik*expterm/r
                  end if
c
c     scale the interaction based on its group membership
c
                  if (use_group) then
                     e = e * fgrp
                     de = de * fgrp
                  end if
c
c     form the chain rule terms for derivative expressions
c
                  de = de / r
                  dedx = de * xr
                  dedy = de * yr
                  dedz = de * zr
c
c     increment the overall energy and derivative expressions
c
                  ec = ec + e
                  dec(1,i) = dec(1,i) + dedx
                  dec(2,i) = dec(2,i) + dedy
                  dec(3,i) = dec(3,i) + dedz
                  dec(1,k) = dec(1,k) - dedx
                  dec(2,k) = dec(2,k) - dedy
                  dec(3,k) = dec(3,k) - dedz
               end if
            end if
         end do
      end do
      return
      end
