c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1997  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     #############################################################
c     ##                                                         ##
c     ##  function bndangle  --  bond angle between three atoms  ##
c     ##                                                         ##
c     #############################################################
c
c
c     "bndangle" finds the value of the bond angle defined
c     by three input atoms
c
c
      function bndangle (ia,ib,ic)
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'math.i'
      integer ia,ib,ic
      real*8 bndangle
      real*8 xab,yab,zab
      real*8 xcb,ycb,zcb
      real*8 rab2,rcb2
      real*8 dot,cosine
c
c
c     set default value in case atoms are degenerate
c
      bndangle = 0.0d0
c
c     compute the value in degrees of the bond angle
c
      xab = x(ia) - x(ib)
      yab = y(ia) - y(ib)
      zab = z(ia) - z(ib)
      rab2 = xab*xab + yab*yab + zab*zab
      xcb = x(ic) - x(ib)
      ycb = y(ic) - y(ib)
      zcb = z(ic) - z(ib)
      rcb2 = xcb*xcb + ycb*ycb + zcb*zcb
      if (rab2.ne.0.0d0 .and. rcb2.ne.0.0d0) then
         dot = xab*xcb + yab*ycb + zab*zcb
         cosine = dot / sqrt(rab2*rcb2)
         cosine = min(1.0d0,max(-1.0d0,cosine))
         bndangle = radian * acos(cosine)
      end if
      return
      end
