subroutine aptcirp (ax, ay, az, bx, by, bz, cx, cy, cz, np, tol,
     &                    px, py, pz, ux, uy, uz, rad, nerr)

ccbeg.
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c                             SUBROUTINE APTCIRP
c
c     call aptcirp (ax, ay, az, bx, by, bz, cx, cy, cz, np, tol,
c    &              px, py, pz, ux, uy, uz, rad, nerr)
c
c     Version:  aptcirp  Updated    1993 June 20 17:20.
c               aptcirp  Originated 1993 April 27, 14:40.
c
c     Author:   Arthur L. Edwards, LLNL, L-298, Telephone (925) 422-4123.
c
c
c     Purpose:  For each of the np sets of 3 points a = (ax, ay, az),
c               b = (bx, by, bz) and c = (cx, cy, cz), to find the center
c               p = (px, py, pz), unit normal vector u = (ux, uy, uz) and
c               radius rad of the circle through points (a, b, c).
c               Flag nerr indicates any input error, if not zero.
c
c     Method:   Point "p" is in the planes bisecting vectors "ab" and "ac", and
c               is coplanar with points (a, b, c):
c                 (p - 0.5 * (b + a)) dot (b - a) = 0
c                 (p - 0.5 * (c + a)) dot (c - a) = 0
c                 (p - a) dot ((b - a) X (c - a)) = 0
c               These 3 equations are solved for px, py and pz.
c
c     Input:    ax, ay, az, bx, by, bz, cx, cy, cz, np, tol.
c
c     Output:   px, py, pz, ux, uy, uz, rad, nerr.
c
c     Calls: aptvpln, aptvdis, aptvsum, aptvdot, aptsolv, aptvuna 
c               
c
c     History:  1994 June 20.  Changed output vector "u" = (ux, uy, uz) to
c               a unit vector, as originally intended.
c
c     Glossary:
c
c     ax,ay,az  Input    The x, y, z coordinates of point "a".  Size np.
c
c     bx,by,bz  Input    The x, y, z coordinates of point "b".  Size np.
c
c     cx,cy,cz  Input    The x, y, z coordinates of point "c".  Size np.
c
c     nerr      Output   Indicates an input error, if not 0.
c                          1 if np is not positive.
c
c     np        Input    The number of sets of points (a, b, c) for which the
c                          circle is to be found.  Must be positive.
c
c     px,py,pz  Output   The x, y, z coordinates of the center of the circle.
c                          Each 1.e99 if the points (a, b, c) are colinear or
c                          congruent.  Size np.
c
c     rad       Output   The radius of the circle, larger than 1.e99 if the
c                          points (a, b, c) are colinear or congruent.  Size np.
c
c     tol       Input    Numerical tolerance limit.
c                          On Cray computers, recommend 1.e-5 to 1.e-11.
c
c     ux,uy,uz  Output   The x, y, z components of unit vector "u", normal to
c                          the plane of the circle.  Each zero if points
c                          (a, b, c) are colinear or congruent.  Size np.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ccend.

c.... Dimensioned arguments.

c---- Coordinate x of input point "a".
      dimension ax      (1)
c---- Coordinate y of input point "a".
      dimension ay      (1)
c---- Coordinate z of input point "a".
      dimension az      (1)
c---- Coordinate x of input point "b".
      dimension bx      (1)
c---- Coordinate y of input point "b".
      dimension by      (1)
c---- Coordinate z of input point "b".
      dimension bz      (1)
c---- Coordinate x of input point "c".
      dimension cx      (1)
c---- Coordinate y of input point "c".
      dimension cy      (1)
c---- Coordinate z of input point "c".
      dimension cz      (1)
c---- Coordinate x of center of circle "p".
      dimension px      (1)
c---- Coordinate y of center of circle "p".
      dimension py      (1)
c---- Coordinate z of center of circle "p".
      dimension pz      (1)
c---- Radius of circle.
      dimension rad     (1)
c---- Component x of normal vector "u".
      dimension ux      (1)
c---- Component y of normal vector "u".
      dimension uy      (1)
c---- Component z of normal vector "u".
      dimension uz      (1)

c.... Local variables.  Floating point.

c---- Coefficients of equations for "p".
      common /laptcirp/ a       (3,3)
c---- Dot product of "ab" and "abmid".
      common /laptcirp/ abdot   (64)
c---- Length of vector "abmid".
      common /laptcirp/ abmidl  (64)
c---- Component x of vector "abmid".
      common /laptcirp/ abmidx  (64)
c---- Component y of vector "abmid".
      common /laptcirp/ abmidy  (64)
c---- Component z of vector "abmid".
      common /laptcirp/ abmidz  (64)
c---- Length of vector "ab".
      common /laptcirp/ abvlen  (64)
c---- Component x of vector "ab".
      common /laptcirp/ abx     (64)
c---- Component y of vector "ab".
      common /laptcirp/ aby     (64)
c---- Component z of vector "ab".
      common /laptcirp/ abz     (64)
c---- Dot product of "ac" and "acmid".
      common /laptcirp/ acdot   (64)
c---- Length of vector "acmid".
      common /laptcirp/ acmidl  (64)
c---- Component x of vector "acmid".
      common /laptcirp/ acmidx  (64)
c---- Component y of vector "acmid".
      common /laptcirp/ acmidy  (64)
c---- Component z of vector "acmid".
      common /laptcirp/ acmidz  (64)
c---- Length of vector "ac".
      common /laptcirp/ acvlen  (64)
c---- Component x of vector "ac".
      common /laptcirp/ acx     (64)
c---- Component y of vector "ac".
      common /laptcirp/ acy     (64)
c---- Component z of vector "ac".
      common /laptcirp/ acz     (64)
c---- Dot product of "a" and "u".
      common /laptcirp/ audot   (64)
c---- Inverse of matrix a.
      common /laptcirp/ b       (3,3)
c---- A big number.
      common /laptcirp/ big
c---- Solution for "p".
      common /laptcirp/ q       (3)
c---- Solution for "p" (check).
      common /laptcirp/ qm      (3)
c---- Residuals for "p" (check).
      common /laptcirp/ qr      (3)
c---- Right side of equations for "p".
      common /laptcirp/ s       (3)
c---- Right side of equations for "p" (check).
      common /laptcirp/ sm      (3)
c---- Residuals of right side of equations for "p" (check).
      common /laptcirp/ sr      (3)
c---- Working memory for aptsolv.
      common /laptcirp/ w       (3,4)

c.... Local variables.  Integers.

c---- Index in arrays.
      common /laptcirp/ n
c---- First index of subset of data.
      common /laptcirp/ n1
c---- Last index of subset of data.
      common /laptcirp/ n2
c---- Index in external array.
      common /laptcirp/ nn
c---- Size of current subset of data.
      common /laptcirp/ ns
c---- Twice area of triangle (a, b, c).
      common /laptcirp/ vlen    (64)
cbugc***DEBUG begins.
cbug 9901 format (/ 'aptcirp finding circle through points:' /
cbug     &  (i3,' ax,ay,az=',1p3e22.14 /
cbug     &  '    bx,by,bz=',1p3e22.14 /
cbug     &  '    cx,cy,cz=',1p3e22.14))
cbug      write ( 3, 9901) (n, ax(n), ay(n), az(n), bx(n), by(n), bz(n),
cbug     &  cx(n), cy(n), cz(n), n = 1, np)
cbugc***DEBUG ends.

c.... Initialize.

      big = 1.e99

      nerr = 0

c.... Test for input errors.

      if (np .le. 0) then
        nerr = 1
        go to 210
      endif

c.... Set up the indices of the first subset of data.

      n1 = 1
      n2 = min (np, 64)

c.... Loop over subsets of data.

  110 ns = n2 - n1 + 1

c.... Find the vectors normal to the plane of each circle.

      call aptvpln (ax(n1), ay(n1), az(n1), bx(n1), by(n1), bz(n1),
     &              cx(n1), cy(n1), cz(n1), ns, tol,
     &              ux(n1), uy(n1), uz(n1), vlen, nerr)

c.... Find the difference vectors "ab" and "ac".

      call aptvdis (ax(n1), ay(n1), az(n1), bx(n1), by(n1), bz(n1), ns,
     &              tol, abx, aby, abz, abvlen, nerr)

      call aptvdis (ax(n1), ay(n1), az(n1), cx(n1), cy(n1), cz(n1), ns,
     &              tol, acx, acy, acz, acvlen, nerr)

c.... Find the midpoint vectors "abmid" and "acmid".

      call aptvsum (0, 0.5, ax(n1), ay(n1), az(n1),
     &                 0.5, bx(n1), by(n1), bz(n1), ns, tol,
     &              abmidx, abmidy, abmidz, abmidl, nerr)

      call aptvsum (0, 0.5, ax(n1), ay(n1), az(n1),
     &                 0.5, cx(n1), cy(n1), cz(n1), ns, tol,
     &              acmidx, acmidy, acmidz, acmidl, nerr)

c.... Find the vector dot products ab.abmid, ac.acmid and a.u.

      call aptvdot (abx, aby, abz, abmidx, abmidy, abmidz, ns, tol,
     &              abdot, nerr)

      call aptvdot (acx, acy, acz, acmidx, acmidy, acmidz, ns, tol,
     &              acdot, nerr)

      call aptvdot (ax(n1), ay(n1), az(n1), ux(n1), uy(n1), uz(n1), ns,
     &              tol, audot, nerr)

c.... Find the center of each circle.

      do 120 n = 1, ns

        nn = n1 + n - 1

        a(1,1) = abx(n)
        a(1,2) = aby(n)
        a(1,3) = abz(n)
        a(2,1) = acx(n)
        a(2,2) = acy(n)
        a(2,3) = acz(n)
        a(3,1) = ux (nn)
        a(3,2) = uy (nn)
        a(3,3) = uz (nn)
        s(1)   = abdot(n)
        s(2)   = acdot(n)
        s(3)   = audot(n)

        call aptsolv (a, s, 3, w, 3, tol, q, b, qm, qr, sm, sr, nerr)

        px(nn) = q(1)
        py(nn) = q(2)
        pz(nn) = q(3)

c....   Test for colinear or congruent points (a, b, c).

        if (nerr .eq. 1) then
          px(nn) = big
          py(nn) = big
          pz(nn) = big
          nerr   = 0
        endif

  120 continue

c.... Find the radius of each circle.

      call aptvdis (ax(n1), ay(n1), az(n1), px(n1), py(n1), pz(n1), ns,
     &              tol, abx, aby, abz, rad(n1), nerr)

c.... Find the unit normal vectors.

      call aptvuna (ux(n1), uy(n1), uz(n1), ns, tol, vlen, nerr)

c.... See if all data subsets are done.

c---- Do another subset of data.
      if (n2 .lt. np) then
        n1 = n2 + 1
        n2 = min (np, n1 + 63)
        go to 110
      endif
cbugc***DEBUG begins.
cbug 9902 format (/ 'aptcirp results:' /
cbug     &  (i3,'  px,py,pz=',1p3e22.14 /
cbug     &  '     ux,uy,uz=',1p3e22.14 /
cbug     &  '     rad=     ',1pe22.14))
cbug      write ( 3, 9902) (n, px(n), py(n), pz(n),
cbug     &  ux(n), uy(n), uz(n), rad(n), n = 1, np)
cbugc***DEBUG ends.

  210 return

c.... End of subroutine aptcirp.      (+1 line.)
      end

UCRL-WEB-209832