c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  program testrot  --  derivative test; torsional version  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "testrot" computes and compares the analytical and numerical
c     gradient vectors of the potential energy function with respect
c     to rotatable torsional angles
c
c
      program testrot
      implicit none
      include 'sizes.i'
      include 'domega.i'
      include 'energi.i'
      include 'iounit.i'
      include 'math.i'
      include 'omega.i'
      include 'zcoord.i'
      integer i
      real*8 delta,delta0,eps,e,e0,energy
      real*8 derivs(maxrot),nderiv(maxrot)
      real*8 eb0,ea0,eba0,eub0,eaa0,eopb0,eid0
      real*8 eit0,et0,ebt0,ett0,ev0,e140,ec0
      real*8 ecd0,ed0,em0,ep0,er0,es0,eg0,ex0
      character*80 string
      logical exist,query
c
c
c     set up the molecular mechanics calculation
c
      call initial
      call getint
      call mechanic
      call initrot
c
c     get the stepsize for numerical gradient calculation
c
      delta = -1.0d0
      delta0 = 1.0d-3
      query = .true.
      call nextarg (string,exist)
      if (exist) then
         read (string,*,err=10,end=10)  delta
         query = .false.
      end if
   10 continue
      if (query) then
         write (iout,20)  delta0
   20    format (/,' Enter Numerical Derivative Stepsize [',d7.1,
     &              ' Deg] :  ',$)
         read (input,30)  delta
   30    format (f20.0)
      end if
      if (delta .le. 0.0d0)  delta = delta0
      eps = -delta / radian
c
c     make call to get analytical derivatives
c
      call gradrot (e0,derivs)
      write (iout,40)  e0
   40 format (//,' Total Potential Energy :',f17.4,' Kcal/mole')
      write (iout,50)
   50 format (/,' Energy',9x,'EB',10x,'EA',9x,'EBA',9x,'EUB',
     &           9x,'EAA',8x,'EOPB',
     &        /,' Terms',9x,'EID',9x,'EIT',10x,'ET',9x,'EBT',
     &           9x,'ETT',9x,'E14',
     &        /,16x,'EV',10x,'EC',9x,'ECD',10x,'ED',10x,'EM',
     &           10x,'EP',
     &        /,16x,'ER',10x,'ES',10x,'EG',10x,'EX')
      write (iout,60)  eb,ea,eba,eub,eaa,eopb,eid,eit,et,ebt,
     &                 ett,ev,e14,ec,ecd,ed,em,ep,er,es,eg,ex
   60 format (/,6x,6f12.4,/,6x,6f12.4,/,6x,6f12.4,/,6x,4f12.4)
      write (iout,70)
   70 format (/,' Energy Derivatives with Respect to Torsions :')
      write (iout,80)
   80 format (/,' Atom 1',7x,'d EB',8x,'d EA',7x,'d EBA',
     &           7x,'d EUB',7x,'d EAA',6x,'d EOPB',
     &        /,' Atom 2',6x,'d EID',7x,'d EIT',8x,'d ET',
     &           7x,'d EBT',7x,'d ETT',7x,'d E14',
     &        /,'  Type',8x,'d EV',8x,'d EC',7x,'d ECD',
     &           8x,'d ED',8x,'d EM',8x,'d EP',
     &        /,14x,'d ER',8x,'d ES',8x,'d EG',8x,'d EX')
c
c     loop over all dihedrals to get numerical derivatives
c
      do i = 1, nomega
         bet(zline(i)) = bet(zline(i)) + delta/2.0d0
         call makexyz
         e0 = energy ()
         eb0 = eb
         ea0 = ea
         eba0 = eba
         eub0 = eub
         eaa0 = eaa
         eopb0 = eopb
         eid0 = eid
         eit0 = eit
         et0 = et
         ebt0 = ebt
         ett0 = ett
         e140 = e14
         ev0 = ev
         ec0 = ec
         ecd0 = ecd
         ed0 = ed
         em0 = em
         ep0 = ep
         er0 = er
         es0 = es
         eg0 = eg
         ex0 = ex
         bet(zline(i)) = bet(zline(i)) - delta
         call makexyz
         e = energy ()
         bet(zline(i)) = bet(zline(i)) + delta/2.0d0
         nderiv(i) = (e-e0)/eps
         write (iout,90)  iomega(2,i),teb(i),tea(i),teba(i),teub(i),
     &                    teaa(i),teopb(i),iomega(1,i),teid(i),teit(i),
     &                    tet(i),tebt(i),tett(i),te14(i),tev(i),tec(i),
     &                    tecd(i),ted(i),tem(i),tep(i),ter(i),tes(i),
     &                    teg(i),tex(i)
   90    format (/,i6,6f12.4,/,i6,6f12.4,/,' Anlyt',6f12.4,/,6x,4f12.4)
         write (iout,100)  iomega(2,i),(eb-eb0)/eps,(ea-ea0)/eps,
     &                    (eba-eba0)/eps,(eub-eub0)/eps,(eaa-eaa0)/eps,
     &                    (eopb-eopb0)/eps,iomega(1,i),(eid-eid0)/eps,
     &                    (eit-eit0)/eps,(et-et0)/eps,(ebt-ebt0)/eps,
     &                    (ett-ett0)/eps,(e14-e140)/eps,(ev-ev0)/eps,
     &                    (ec-ec0)/eps,(ecd-ecd0)/eps,(ed-ed0)/eps,
     &                    (em-em0)/eps,(ep-ep0)/eps,(er-er0)/eps,
     &                    (es-es0)/eps,(eg-eg0)/eps,(ex-ex0)/eps
  100    format (/,i6,6f12.4,/,i6,6f12.4,/,' Numer',6f12.4,/,6x,4f12.4)
      end do
c
c     print comparison of analytical and numerical derivatives
c
      write (iout,110)
  110 format (//,5x,'Torsion',16x,'Anlyt Deriv',4x,'Numer Deriv',/)
      do i = 1, nomega
         write (iout,120)  iomega(2,i),iomega(1,i),derivs(i),nderiv(i)
  120    format (1x,i5,'-',i5,10x,2f15.4)
      end do
      end
