c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program newtrot  --  perform TNCG torsional optimization  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "newtrot" performs an energy minimization in torsional angle
c     space using a truncated Newton conjugate gradient method
c
c
      program newtrot
      implicit none
      include 'sizes.i'
      include 'files.i'
      include 'iounit.i'
      include 'math.i'
      include 'omega.i'
      include 'zcoord.i'
      integer i,imin,freeunit
      real*8 newtrot1,minimum,grdmin,gnorm,grms
      real*8 xx(maxvar),derivs(maxrot)
      character*1 answer
      character*6 mode,method
      character*60 minfile
      character*80 string
      logical exist
      external newtrot1,newtrot2,writeout
c
c
c     set up the molecular mechanics calculation
c
      call initial
      call getint
      call mechanic
      call initrot
c
c     get the type of optimization algorithm to use
c
      mode = 'auto  '
      call nextarg (answer,exist)
      if (.not. exist) then
         answer = 'A'
         write (iout,10)  answer
   10    format (/,' Choose Automatic, Newton, TNCG or DTNCG',
     &              ' Method [',a1,'] :  ',$)
         read (input,20)  answer
   20    format (a1)
      end if
      call upcase (answer)
      if (answer .eq. 'A')  mode = 'auto  '
      if (answer .eq. 'N')  mode = 'newton'
      if (answer .eq. 'T')  mode = 'tncg  '
      if (answer .eq. 'D')  mode = 'dtncg '
c
c     get the type of linear equation preconditioning to use
c
      method = 'diag  '
      call nextarg (answer,exist)
      if (.not. exist) then
         answer = 'D'
         write (iout,30)  answer
   30    format (/,' Precondition via Auto/None/Diag/',
     &              'SSOR/ICCG [',a1,'] :  ',$)
         read (input,40)  answer
   40    format (a1)
      end if
      call upcase (answer)
      if (answer .eq. 'A')  method = 'auto  '
      if (answer .eq. 'N')  method = 'none  '
      if (answer .eq. 'D')  method = 'diag  '
      if (answer .eq. 'S')  method = 'ssor  '
      if (answer .eq. 'I')  method = 'iccg  '
c
c     get termination criterion as RMS torsional gradient
c
      grdmin = -1.0d0
      call nextarg (string,exist)
      if (exist)  read (string,*,err=50,end=50)  grdmin
   50 continue
      if (grdmin .le. 0.0d0) then
         write (iout,60)
   60    format (/,' Enter RMS Gradient per Torsion Criterion',
     &              ' [0.01] :  ',$)
         read (input,70)  grdmin
   70    format (f20.0)
      end if
      if (grdmin .le. 0.0d0)  grdmin = 0.01d0
c
c     write out a copy of coordinates for later update
c
      imin = freeunit ()
      minfile = filename(1:leng)//'.int'
      call version (minfile,'new')
      open (unit=imin,file=minfile,status='new')
      call prtint (imin)
      close (unit=imin)
      outfile = minfile
c
c     translate the initial coordinates
c
      do i = 1, nomega
         xx(i) = dihed(i)
      end do
c
c     make the call to the optimization routine
c
      call tncg (mode,method,nomega,xx,minimum,grdmin,
     &               newtrot1,newtrot2,writeout)
c
c     untranslate the final coordinates
c
      do i = 1, nomega
         dihed(i) = xx(i)
         bet(zline(i)) = dihed(i) * radian
      end do
c
c     write out final function value and gradient
c
      call gradrot (minimum,derivs)
      gnorm = 0.0d0
      do i = 1, nomega
         gnorm = gnorm + derivs(i)**2
      end do
      gnorm = sqrt(gnorm)
      grms = gnorm / sqrt(dble(nomega))
      if (grms .gt. 0.0001d0) then
         write (iout,90)  minimum,grms,gnorm
   80    format (/,' Final Function Value :',f15.4,
     &           /,' Final RMS Gradient :  ',f15.4,
     &           /,' Final Gradient Norm : ',f15.4)
      else
         write (iout,90)  minimum,grms,gnorm
   90    format (/,' Final Function Value :',f15.4,
     &           /,' Final RMS Gradient :  ',d15.4,
     &           /,' Final Gradient Norm : ',d15.4)
      end if
c
c     write the final coordinates into a file
c
      imin = freeunit ()
      open (unit=imin,file=minfile,status='old')
      rewind (unit=imin)
      call prtint (imin)
      close (unit=imin)
      end
c
c
c     #########################################################
c     ##                                                     ##
c     ##  function newtrot1  --  energy/gradient for newton  ##
c     ##                                                     ##
c     #########################################################
c
c
c     "newtrot1" is a service routine that computes the energy
c     and gradient for truncated Newton conjugate gradient
c     optimization in torsional angle space
c
c
      function newtrot1 (xx,g)
      implicit none
      include 'sizes.i'
      include 'math.i'
      include 'omega.i'
      include 'zcoord.i'
      integer i
      real*8 newtrot1,e,derivs(maxrot)
      real*8 xx(maxvar),g(maxvar)
c
c
c     translate optimization variables into dihedrals
c
      do i = 1, nomega
         dihed(i) = xx(i)
         bet(zline(i)) = dihed(i) * radian
      end do
c
c     get coordinates, then compute energy and gradient
c
      call makexyz
      call gradrot (e,derivs)
      newtrot1 = e
c
c     store angle gradients as optimization gradient
c
      do i = 1, nomega
         g(i) = derivs(i)
      end do
      return
      end
c
c
c     ##########################################################
c     ##                                                      ##
c     ##  subroutine newtrot2  --  Hessian matrix for newton  ##
c     ##                                                      ##
c     ##########################################################
c
c
c     "newtrot2" is a service routine that computes the sparse
c     matrix Hessian elements for truncated Newton optimization
c     in torsional angle space
c
c
      subroutine newtrot2 (mode,xx,h,h_init,h_stop,h_index,h_diag)
      implicit none
      include 'sizes.i'
      include 'hescut.i'
      include 'math.i'
      include 'omega.i'
      include 'zcoord.i'
      integer i,j,ihess
      integer h_init(maxvar),h_stop(maxvar)
      integer h_index(maxhess)
      real*8 xx(maxvar),h_diag(maxvar)
      real*8 h(maxhess)
      real*8 hrot(maxrot,maxrot)
      character*4 mode
      save hrot
c
c
c     translate optimization parameters and compute
c     Cartesian coordinates from internal coordinates
c
      if (mode .eq. 'none')  return
      do i = 1, nomega
         dihed(i) = xx(i)
         bet(zline(i)) = dihed(i) * radian
      end do
c
c     compute the desired portion of the Hessian
c
      call makexyz
      call hessrot (mode,hrot)
c
c     store the large elements in sparse matrix format
c
      if (mode .eq. 'full') then
         ihess = 0
         do i = 1, nomega
            h_diag(i) = hrot(i,i)
            h_init(i) = ihess + 1
            do j = i+1, nomega
               if (abs(hrot(j,i)) .ge. hesscut) then
                  ihess = ihess + 1
                  h_index(ihess) = j
                  h(ihess) = hrot(j,i)
               end if
            end do
            h_stop(i) = ihess
         end do
c
c     store only the Hessian matrix diagonal
c
      else if (mode .eq. 'diag') then
         do i = 1, nomega
            h_diag(i) = hrot(i,i)
         end do
      end if
      return
      end
