c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ################################################################
c     ##                                                            ##
c     ##  program minirot  --  perform LMQN torsional optimization  ##
c     ##                                                            ##
c     ################################################################
c
c
c     "minirot" performs an energy minimization in torsional
c     angle space using a limited memory quasi-Newton method
c
c
      program minirot
      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 minirot1,minimum,grdmin,grms,gnorm
      real*8 xx(maxvar),derivs(maxrot)
      character*60 minfile
      character*80 string
      logical exist
      external minirot1,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     get optimization parameters as scaled dihedral angles
c
      do i = 1, nomega
         xx(i) = dihed(i) * scale
      end do
c
c     make the call to the optimization routine
c
      call lmqn (nomega,xx,minimum,grdmin,minirot1,writeout)
c
c     unscale the final dihedral angle values
c
      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 minirot1  --  energy/gradient for minimizer  ##
c     ##                                                        ##
c     ############################################################
c
c
c     "minirot1" is a service routine that computes the energy
c     and gradient for limited memory quasi-Newton optimization
c     in torsional angle space
c
c
      function minirot1 (xx,g)
      implicit none
      include 'sizes.i'
      include 'math.i'
      include 'omega.i'
      include 'scales.i'
      include 'zcoord.i'
      integer i
      real*8 minirot1,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) / 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)
      minirot1 = e
c
c     store angle gradients as optimization gradient
c
      do i = 1, nomega
         g(i) = derivs(i) / scale
      end do
      return
      end
