c
c
c     ###################################################
c     ##  COPYRIGHT (C)  1990  by  Jay William Ponder  ##
c     ##              All Rights Reserved              ##
c     ###################################################
c
c     #############################################################
c     ##                                                         ##
c     ##  subroutine makexyz  --  convert internal to Cartesian  ##
c     ##                                                         ##
c     #############################################################
c
c
c     "makexyz" generates a complete set of Cartesian coordinates
c     for a full structure from the internal coordinate values
c
c
      subroutine makexyz
      implicit none
      include 'sizes.i'
      include 'atoms.i'
      include 'math.i'
      include 'zcoord.i'
      integer i,k,i1,i2,i3,chiral
      real*8 bond,angle1,angle2,sign
c
c
c     first atom is placed at the origin
c
      if (n .ge. 1) then
         x(1) = 0.0d0
         y(1) = 0.0d0
         z(1) = 0.0d0
      end if
c
c     second atom is placed along the z-axis
c
      if (n .ge. 2) then
         x(2) = 0.0d0
         y(2) = 0.0d0
         z(2) = blen(2)
      end if
c
c     third atom is placed in the x,z-plane
c
      if (n .ge. 3) then
         x(3) = blen(3) * sin(alph(3)/radian)
         y(3) = 0.0d0
         if (iz(1,3) .eq. 1) then
            z(3) = blen(3) * cos(alph(3)/radian)
         else
            z(3) = z(2) - blen(3)*cos(alph(3)/radian)
         end if
      end if
c
c     as long as atoms remain linear with the first
c     two atoms, keep placing them along the z-axis
c
      i = 3
      if (n .gt. 3) then
         dowhile (x(i) .eq. 0.0d0)
            i = i + 1
            i1 = iz(1,i)
            i2 = iz(2,i)
            if (z(i1) .gt. z(i2)) then
               sign = 1.0d0
            else
               sign = -1.0d0
            end if
            x(i) = blen(i) * sin(alph(i)/radian)
            y(i) = 0.0d0
            z(i) = z(i1) - sign*blen(i)*cos(alph(i)/radian)
         end do
      end if
c
c     loop over each atom in turn, finding its coordinates
c
      k = i + 1
      do i = k, n
         i1 = iz(1,i)
         i2 = iz(2,i)
         i3 = iz(3,i)
         chiral = iz(4,i)
         bond = blen(i)
         angle1 = alph(i)
         angle2 = bet(i)
         call xyzatm (i,i1,bond,i2,angle1,i3,angle2,chiral)
      end do
      return
      end
