c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1993  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     ###############################################################
c     ##                                                           ##
c     ##  subroutine kurey  --  Urey-Bradley parameter assignment  ##
c     ##                                                           ##
c     ###############################################################
c
c
c     "kurey" assigns the force constants and ideal distances
c     for the Urey-Bradley 1-3 interactions; also processes any
c     new or changed parameter values
c
c
      subroutine kurey
      implicit none
      include 'sizes.i'
      include 'angle.i'
      include 'atmtyp.i'
      include 'atoms.i'
      include 'inform.i'
      include 'iounit.i'
      include 'keys.i'
      include 'kurybr.i'
      include 'potent.i'
      include 'urey.i'
      integer i,j,size,next
      integer ia,ib,ic
      integer ita,itb,itc
      real*8 bb,tt
      character*3 pa,pb,pc
      character*9 pt
      character*20 keyword
      character*80 record,string
      logical header
c
c
c     process keywords containing Urey-Bradley parameters
c
      header = .true.
      do i = 1, nkey
         next = 1
         record = keyline(i)
         call gettext (record,keyword,next)
         call upcase (keyword)
         if (keyword(1:9) .eq. 'UREYBRAD ') then
            ia = 0
            ib = 0
            ic = 0
            bb = 0.0d0
            tt = 0.0d0
            string = record(next:80)
            read (string,*,err=10,end=10)  ia,ib,ic,bb,tt
   10       continue
            if (header) then
               header = .false.
               write (iout,20)
   20          format (/,' Additional Urey-Bradley Parameters :',
     &                 //,5x,'Atom Classes',8x,'K(UB)',6x,'Distance',/)
            end if
            write (iout,30)  ia,ib,ic,bb,tt
   30       format (4x,3i4,2x,2f12.3)
            size = 3
            call numeral (ia,pa,size)
            call numeral (ib,pb,size)
            call numeral (ic,pc,size)
            if (ia .le. ic) then
               pt = pa//pb//pc
            else
               pt = pc//pb//pa
            end if
            do j = 1, maxnu
               if (ku(j).eq.'         ' .or. ku(j).eq.pt) then
                  ku(j) = pt
                  ucon(j) = bb
                  dst13(j) = tt
                  goto 50
               end if
            end do
            write (iout,40)
   40       format (/,' KUREY  --  Too many Urey-Bradley',
     &                 ' Interaction Parameters')
            abort = .true.
   50       continue
         end if
      end do
c
c     loop over all angles assigning the necessary constants
c
      nurey = 0
      do i = 1, nangle
         ia = iang(1,i)
         ib = iang(2,i)
         ic = iang(3,i)
         ita = class(ia)
         itb = class(ib)
         itc = class(ic)
         size = 3
         call numeral (ita,pa,size)
         call numeral (itb,pb,size)
         call numeral (itc,pc,size)
         if (ita .le. itc) then
            pt = pa//pb//pc
         else
            pt = pc//pb//pa
         end if
c
c     search for Urey-Bradley parameters for current angle
c
         do j = 1, maxnu
            if (ku(j) .eq. pt) then
               nurey = nurey + 1
               iury(1,nurey) = ia
               iury(2,nurey) = ic
               uk(nurey) = ucon(j)
               ul(nurey) = dst13(j)
               goto 60
            end if
         end do
   60    continue
      end do
c
c     if no Urey-Bradley terms are used, turn off the potential
c
      if (nurey .eq. 0)  use_urey = .false.
      return
      end
