c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1994  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ##################################################################
c     ##                                                              ##
c     ##  subroutine echarge9  --  smoothed charge energy & analysis  ##
c     ##                                                              ##
c     ##################################################################
c
c
c     "echarge9" calculates the smoothed charge-charge interaction
c     energy; also partitions the energy among the atoms
c
c
      subroutine echarge9
      implicit none
      include 'sizes.i'
      include 'action.i'
      include 'analyz.i'
      include 'atmtyp.i'
      include 'atoms.i'
      include 'charge.i'
      include 'chgpot.i'
      include 'couple.i'
      include 'energi.i'
      include 'group.i'
      include 'inform.i'
      include 'iounit.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,f,fi,fik,r,r2,fgrp
      real*8 xi,yi,zi,xr,yr,zr
      real*8 erf,defterm,time
      logical header,huge,proceed,iuse
c
c
c     zero out the charge interaction energy and partitioning
c
      nec = 0
      ec = 0.0d0
      do i = 1, n
         aec(i) = 0.0d0
      end do
      header = .true.
c
c     zero out the list of atoms to be skipped
c
      do i = 1, n
         skip(i) = 0
      end do
c
c     set conversion factor and switching function coefficients
c
      f = electric / dielec
      call switch ('CHARGE')
c
c     scale deformation time by the diffusion coefficient
c
      if (deform .ne. 0.0d0) then
         time = diffc * deform
         defterm = 0.5d0 / sqrt(time)
      end if
c
c     calculate charge interaction energy term
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
c
c     potential smoothing via the diffusion equation method
c
                  if (deform .ne. 0.0d0)  e = e * erf(defterm*r)
c
c     scale the interaction based on its group membership
c
                  if (use_group)  e = e * fgrp
c
c     increment the charge-charge energy and partition between atoms
c
                  nec = nec + 1
                  ec = ec + e
                  aec(i) = aec(i) + 0.5d0*e
                  aec(k) = aec(k) + 0.5d0*e
c
c     print a warning if the energy of this interaction is large
c
                  huge = (abs(e) .gt. 100.0d0)
                  if (debug .or. (verbose.and.huge)) then
                     if (header) then
                        header = .false.
                        write (iout,10)
   10                   format (/,' Individual Charge-Charge',
     &                             ' Interactions :',
     &                          //,' Type',11x,'Atom Names',
     &                             16x,'Charges',5x,'Distance',
     &                             5x,'Energy',/)
                     end if
                     write (iout,20)  i,name(i),k,name(k),pchg(ii),
     &                                pchg(kk),r,e
   20                format (' Charge   ',i5,'-',a3,1x,i5,'-',
     &                         a3,8x,2f7.2,f10.4,f12.4)
                  end if
               end if
            end if
         end do
      end do
      return
      end
