      subroutine gradnads(m,n,ncentr,icontr,inddyy,icentr,g,coord,
     &                    charge,density,gradient,zeta,twozetaa,
     &                    twozetab,px,py,pz,base,pxax,pyay,pzaz,axbx,
     &                    ayby,azbz,pxcx,pycy,pzcz,twozeta,u,ss0,ss1,
     &                    ss2,ss3,ps0,ps1,ps2,ds0,ds1,fs0,c,c1,pscond,
     &                    dscond,fscond,dpcond,grad)

c  this subroutine written by alain st-amant of the department of
c  pharmaceutical chemistry, university of california, san francisco.
c  all rights reserved.  this is part of the DeFT project.

      implicit real*8(a-h,o-z)

      dimension icontr(*),inddyy(m,*),icentr(m,*)

      dimension g(*),coord(3,*),charge(*),density(*),gradient(3,*),
     &          zeta(*),twozetaa(*),twozetab(*),px(*),py(*),pz(*),
     &          base(*),pxax(*),pyay(*),pzaz(*),axbx(*),ayby(*),azbz(*),
     &          pxcx(*),pycy(*),pzcz(*),twozeta(*),u(*),ss0(*),ss1(*),
     &          ss2(*),ss3(*),ps0(n,*),ps1(n,*),ps2(n,*),ds0(n,*),
     &          ds1(n,*),fs0(n,*),c(*),c1(n,*),pscond(m,*),dscond(m,*),
     &          fscond(m,*),dpcond(m,*),grad(m,*)

      data zero,two,three,four,pi/0.0d0,2.0d0,
     &                            3.0d0,4.0d0,3.1415926535898d0/

      do 1001 i=1,n
 1001 twozeta(i)=two*zeta(i)

      do 1002 nn=1,ncentr

      ccx=coord(1,nn)
      ccy=coord(2,nn)
      ccz=coord(3,nn)

      twopichrg=two*pi*charge(nn)

      do 1003 i=1,n
      pxcx(i)=px(i)-ccx
      pycy(i)=py(i)-ccy
      pzcz(i)=pz(i)-ccz
 1003 u(i)=zeta(i)*(pxcx(i)*pxcx(i)+pycy(i)*pycy(i)+pzcz(i)*pzcz(i))

      do 1004 i=1,n
      call augg(4,u(i),g)
      ss0(i)=base(i)*g(1)*twopichrg/zeta(i)
      ss1(i)=base(i)*g(2)*twopichrg/zeta(i)
      ss2(i)=base(i)*g(3)*twopichrg/zeta(i)
 1004 ss3(i)=base(i)*g(4)*twopichrg/zeta(i)

      do 1005 i=1,n
      ps0(i,1)=pxax(i)*ss0(i)-pxcx(i)*ss1(i)
      ps0(i,2)=pyay(i)*ss0(i)-pycy(i)*ss1(i)
 1005 ps0(i,3)=pzaz(i)*ss0(i)-pzcz(i)*ss1(i)

      do 1006 i=1,n
      ps1(i,1)=pxax(i)*ss1(i)-pxcx(i)*ss2(i)
      ps1(i,2)=pyay(i)*ss1(i)-pycy(i)*ss2(i)
 1006 ps1(i,3)=pzaz(i)*ss1(i)-pzcz(i)*ss2(i)

      do 1007 i=1,n
      ps2(i,1)=pxax(i)*ss2(i)-pxcx(i)*ss3(i)
      ps2(i,2)=pyay(i)*ss2(i)-pycy(i)*ss3(i)
 1007 ps2(i,3)=pzaz(i)*ss2(i)-pzcz(i)*ss3(i)

      do 1008 i=1,n
 1008 c(i)=(ss0(i)-ss1(i))/twozeta(i)

      do 1009 i=1,n
      ds0(i,1)=pxax(i)*ps0(i,1)-pxcx(i)*ps1(i,1)+c(i)
      ds0(i,2)=pyay(i)*ps0(i,1)-pycy(i)*ps1(i,1)
      ds0(i,3)=pzaz(i)*ps0(i,1)-pzcz(i)*ps1(i,1)
      ds0(i,4)=pyay(i)*ps0(i,2)-pycy(i)*ps1(i,2)+c(i)
      ds0(i,5)=pzaz(i)*ps0(i,2)-pzcz(i)*ps1(i,2)
 1009 ds0(i,6)=pzaz(i)*ps0(i,3)-pzcz(i)*ps1(i,3)+c(i)

      do 1010 i=1,n
 1010 c(i)=(ss1(i)-ss2(i))/twozeta(i)

      do 1011 i=1,n
      ds1(i,1)=pxax(i)*ps1(i,1)-pxcx(i)*ps2(i,1)+c(i)
      ds1(i,2)=pyay(i)*ps1(i,1)-pycy(i)*ps2(i,1)
      ds1(i,3)=pzaz(i)*ps1(i,1)-pzcz(i)*ps2(i,1)
      ds1(i,4)=pyay(i)*ps1(i,2)-pycy(i)*ps2(i,2)+c(i)
      ds1(i,5)=pzaz(i)*ps1(i,2)-pzcz(i)*ps2(i,2)
 1011 ds1(i,6)=pzaz(i)*ps1(i,3)-pzcz(i)*ps2(i,3)+c(i)

      do 1012 j=1,3
      do 1012 i=1,n
 1012 c1(i,j)=(ps0(i,j)-ps1(i,j))/twozeta(i)

      do 1013 i=1,n
      fs0(i,01)=pxax(i)*ds0(i,1)-pxcx(i)*ds1(i,1)+c1(i,1)*two
      fs0(i,02)=pyay(i)*ds0(i,1)-pycy(i)*ds1(i,1)
      fs0(i,03)=pzaz(i)*ds0(i,1)-pzcz(i)*ds1(i,1)
      fs0(i,04)=pyay(i)*ds0(i,2)-pycy(i)*ds1(i,2)+c1(i,1)
      fs0(i,05)=pzaz(i)*ds0(i,2)-pzcz(i)*ds1(i,2)
      fs0(i,06)=pzaz(i)*ds0(i,3)-pzcz(i)*ds1(i,3)+c1(i,1)
      fs0(i,07)=pyay(i)*ds0(i,4)-pycy(i)*ds1(i,4)+c1(i,2)*two
      fs0(i,08)=pzaz(i)*ds0(i,4)-pzcz(i)*ds1(i,4)
      fs0(i,09)=pzaz(i)*ds0(i,5)-pzcz(i)*ds1(i,5)+c1(i,2)
 1013 fs0(i,10)=pzaz(i)*ds0(i,6)-pzcz(i)*ds1(i,6)+c1(i,3)*two

      do 1014 j=1,3
      do 1014 i=1,m
 1014 pscond(i,j)=zero

      do 1015 j=1,3
      do 1015 i=1,n
 1015 pscond(icontr(i),j)=pscond(icontr(i),j)-ps0(i,j)

      do 1016 i=1,m
      grad(i,1)=pscond(i,1)*density(inddyy(i,1))*two
      grad(i,2)=pscond(i,2)*density(inddyy(i,2))
      grad(i,3)=pscond(i,3)*density(inddyy(i,3))
      grad(i,4)=pscond(i,1)*density(inddyy(i,2))
      grad(i,5)=pscond(i,2)*density(inddyy(i,4))*two
      grad(i,6)=pscond(i,3)*density(inddyy(i,5))
      grad(i,7)=pscond(i,1)*density(inddyy(i,3))
      grad(i,8)=pscond(i,2)*density(inddyy(i,5))
 1016 grad(i,9)=pscond(i,3)*density(inddyy(i,6))*two

      do 1017 k=1,3
      do 1017 j=1,3
      do 1017 i=1,m
 1017 gradient(k,nn)=gradient(k,nn)+grad(i,3*(k-1)+j)

      do 1018 k=1,3
      do 1018 j=1,3
      do 1018 i=1,m
 1018 gradient(k,icentr(i,1))=gradient(k,icentr(i,1))-grad(i,3*(k-1)+j)

      do 1019 j=1,10
      do 1019 i=1,m
 1019 fscond(i,j)=zero

      do 1020 j=1,10
      do 1020 i=1,n
 1020 fscond(icontr(i),j)=fscond(icontr(i),j)+fs0(i,j)*twozetaa(i)

      do 1021 i=1,m
      grad(i,01)=fscond(i,01)*density(inddyy(i,1))
      grad(i,02)=fscond(i,02)*density(inddyy(i,2))
      grad(i,03)=fscond(i,03)*density(inddyy(i,3))
      grad(i,04)=fscond(i,04)*density(inddyy(i,4))
      grad(i,05)=fscond(i,05)*density(inddyy(i,5))
      grad(i,06)=fscond(i,06)*density(inddyy(i,6))
      grad(i,07)=fscond(i,02)*density(inddyy(i,1))
      grad(i,08)=fscond(i,04)*density(inddyy(i,2))
      grad(i,09)=fscond(i,05)*density(inddyy(i,3))
      grad(i,10)=fscond(i,07)*density(inddyy(i,4))
      grad(i,11)=fscond(i,08)*density(inddyy(i,5))
      grad(i,12)=fscond(i,09)*density(inddyy(i,6))
      grad(i,13)=fscond(i,03)*density(inddyy(i,1))
      grad(i,14)=fscond(i,05)*density(inddyy(i,2))
      grad(i,15)=fscond(i,06)*density(inddyy(i,3))
      grad(i,16)=fscond(i,08)*density(inddyy(i,4))
      grad(i,17)=fscond(i,09)*density(inddyy(i,5))
 1021 grad(i,18)=fscond(i,10)*density(inddyy(i,6))

      do 1022 k=1,3
      do 1022 j=1,6
      do 1022 i=1,m
 1022 gradient(k,nn)=gradient(k,nn)+grad(i,6*(k-1)+j)

      do 1023 k=1,3
      do 1023 j=1,6
      do 1023 i=1,m
 1023 gradient(k,icentr(i,1))=gradient(k,icentr(i,1))-grad(i,6*(k-1)+j)

      do 1024 j=1,6
      do 1024 i=1,m
 1024 dscond(i,j)=zero

      do 1025 j=1,10
      do 1025 i=1,m
 1025 fscond(i,j)=zero

      do 1026 j=1,6
      do 1026 i=1,n
 1026 dscond(icontr(i),j)=dscond(icontr(i),j)+ds0(i,j)*twozetab(i)

      do 1027 j=1,10
      do 1027 i=1,n
 1027 fscond(icontr(i),j)=fscond(icontr(i),j)+fs0(i,j)*twozetab(i)

      do 1028 i=1,m
      dpcond(i,01)=axbx(i)*dscond(i,1)+fscond(i,01)
      dpcond(i,02)=ayby(i)*dscond(i,1)+fscond(i,02)
      dpcond(i,03)=azbz(i)*dscond(i,1)+fscond(i,03)
      dpcond(i,04)=axbx(i)*dscond(i,2)+fscond(i,02)
      dpcond(i,05)=ayby(i)*dscond(i,2)+fscond(i,04)
      dpcond(i,06)=azbz(i)*dscond(i,2)+fscond(i,05)
      dpcond(i,07)=axbx(i)*dscond(i,3)+fscond(i,03)
      dpcond(i,08)=ayby(i)*dscond(i,3)+fscond(i,05)
      dpcond(i,09)=azbz(i)*dscond(i,3)+fscond(i,06)
      dpcond(i,10)=axbx(i)*dscond(i,4)+fscond(i,04)
      dpcond(i,11)=ayby(i)*dscond(i,4)+fscond(i,07)
      dpcond(i,12)=azbz(i)*dscond(i,4)+fscond(i,08)
      dpcond(i,13)=axbx(i)*dscond(i,5)+fscond(i,05)
      dpcond(i,14)=ayby(i)*dscond(i,5)+fscond(i,08)
      dpcond(i,15)=azbz(i)*dscond(i,5)+fscond(i,09)
      dpcond(i,16)=axbx(i)*dscond(i,6)+fscond(i,06)
      dpcond(i,17)=ayby(i)*dscond(i,6)+fscond(i,09)
 1028 dpcond(i,18)=azbz(i)*dscond(i,6)+fscond(i,10)

      do 1029 i=1,m
      grad(i,01)=dpcond(i,01)*density(inddyy(i,1))
      grad(i,02)=dpcond(i,04)*density(inddyy(i,2))
      grad(i,03)=dpcond(i,07)*density(inddyy(i,3))
      grad(i,04)=dpcond(i,10)*density(inddyy(i,4))
      grad(i,05)=dpcond(i,13)*density(inddyy(i,5))
      grad(i,06)=dpcond(i,16)*density(inddyy(i,6))
      grad(i,07)=dpcond(i,02)*density(inddyy(i,1))
      grad(i,08)=dpcond(i,05)*density(inddyy(i,2))
      grad(i,09)=dpcond(i,08)*density(inddyy(i,3))
      grad(i,10)=dpcond(i,11)*density(inddyy(i,4))
      grad(i,11)=dpcond(i,14)*density(inddyy(i,5))
      grad(i,12)=dpcond(i,17)*density(inddyy(i,6))
      grad(i,13)=dpcond(i,03)*density(inddyy(i,1))
      grad(i,14)=dpcond(i,06)*density(inddyy(i,2))
      grad(i,15)=dpcond(i,09)*density(inddyy(i,3))
      grad(i,16)=dpcond(i,12)*density(inddyy(i,4))
      grad(i,17)=dpcond(i,15)*density(inddyy(i,5))
 1029 grad(i,18)=dpcond(i,18)*density(inddyy(i,6))

      do 1030 k=1,3
      do 1030 j=1,6
      do 1030 i=1,m
 1030 gradient(k,nn)=gradient(k,nn)+grad(i,6*(k-1)+j)

      do 1031 k=1,3
      do 1031 j=1,6
      do 1031 i=1,m
 1031 gradient(k,icentr(i,2))=gradient(k,icentr(i,2))-grad(i,6*(k-1)+j)

 1002 continue

      return
      end
