c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program optirot  --  variable metric torsional optimizer  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "optirot" performs an energy minimization in torsional angle
c     space using an optimally conditioned variable metric method
c
c
      program optirot
      implicit none
      include 'sizes.i'
      include 'files.i'
      include 'iounit.i'
      include 'math.i'
      include 'omega.i'
      include 'scales.i'
      include 'zcoord.i'
      integer i,imin,freeunit
      real*8 optirot1,minimum,grdmin,gnorm,grms
      real*8 xx(maxopt),derivs(maxrot)
      character*60 minfile
      character*80 string
      logical exist
      external optirot1,writeout
c
c
c     set up the molecular mechanics calculation
c
      call initial
      call getint
      call mechanic
      call initrot
c
c     get termination criterion as RMS torsional gradient
c
      grdmin = -1.0d0
      call nextarg (string,exist)
      if (exist)  read (string,*,err=10,end=10)  grdmin
   10 continue
      if (grdmin .le. 0.0d0) then
         write (iout,20)
   20    format (/,' Enter RMS Gradient per Torsion Criterion',
     &              ' [0.01] :  ',$)
         read (input,30)  grdmin
   30    format (f20.0)
      end if
      if (grdmin .eq. 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     set scaling parameter for function and derivative values;
c     use square root of median eigenvalue of typical Hessian
c
      scale = 5.0d0
c
c     scale the coordinates, perform optimization, then unscale
c
      do i = 1, nomega
         xx(i) = dihed(i) * scale
      end do
      call vmetric (nomega,xx,minimum,grdmin,optirot1,writeout)
      do i = 1, nomega
         dihed(i) = xx(i) / scale
         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,40)  minimum,grms,gnorm
   40    format (/,' Final Function Value :',f15.4,
     &           /,' Final RMS Gradient :  ',f15.4,
     &           /,' Final Gradient Norm : ',f15.4)
      else
         write (iout,50)  minimum,grms,gnorm
   50    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 optirot1  --  function & gradient for optimizer  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "optirot1" is a service routine that computes the energy
c     and gradient for optimally conditioned variable metric
c     optimization in torsional angle space
c
c
      function optirot1 (xx,g)
      implicit none
      include 'sizes.i'
      include 'math.i'
      include 'omega.i'
      include 'scales.i'
      include 'zcoord.i'
      integer i
      real*8 optirot1,e,g(maxopt)
      real*8 xx(maxopt),derivs(maxrot)
c
c
c     translate optimization variables into dihedrals
c
      do i = 1, nomega
         dihed(i) = xx(i) / scale
         bet(zline(i)) = dihed(i) * radian
      end do
c
c     get coordinates, then compute energy and gradient
c
      call makexyz
      call gradrot (e,derivs)
      optirot1 = e
c
c     store angle gradients as optimization gradient
c
      do i = 1, nomega
         g(i) = derivs(i) / scale
      end do
      return
      end
