subroutine aptcums (pcum, nbins, np, frstr, nb, nerr)

ccbeg.
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c                             SUBROUTINE APTCUMS
c
c     call aptcums (pcum, nbins, np, frstr, nb, nerr)
c
c     Version:  aptcums  Updated    1990 July 20 14:00.
c               aptcums  Originated 1990 July 20 14:00.
c
c     Author:   Arthur L. Edwards, LLNL, L-298, Telephone (925) 422-4123.
c
c
c     Purpose:  To randomly sample np bin numbers nb from nbins bins, each
c               with cumulative relative probability pcum(n) of selecting bins
c               1 through n.
c               Any fraction frstr of sampling may be striated, from 0.0 to 1.0.
c               Flag nerr indicates any input error.
c
c     WARNING:  STRIATED SAMPLING INTRODUCES CORRELATIONS BETWEEN SAMPLE INDICES
c               AND BIN INDICES.  USE ONLY WHEN SUCH CORRELATIONS ACCEPTABLE.
c
c     Note:     Use subroutine aptcump to find the cumulative probability
c               density function for a histogram or piecewise linear probability
c               density function.
c
c     Timing:   For a test problem with 8 bins, 1000 samples, the cpu time was
c               3300 microseconds unstriated, 3900 microseconds striated.
c
c     Input:    pcum, nbins, np, frstr.
c
c     Output:   nb, nerr.
c
c     Glossary:
c
c     frstr     Input    The fraction (from 0.0 to 1.0) of samples to striate
c                          over the bins.  Remaining samples will be selected
c                          randomly over the bins.
c
c     nb        Output   The randomly sampled bin numbers.  Size np.
c
c     nbins     Input    The number of bins.
c
c     nerr      Output   Indicates an input error, if not 0.
c                          1 if nbins is not positive.
c                          2 if np is not positive.
c                          3 if frstr is less than 0.0 or more than 1.0.
c
c     np        Input    Size of array nb.  Number of samples.
c
c     pcum      Input    For bin n, pcum(n) is the relative (unnormalized)
c                          cumulative probability of selecting any bin from
c                          1 to n, inclusive.  The normalized probabilities
c                          could be found, if desired, by dividing each pcum(n)
c                          value by pcum(nbins).  Size nbins.
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ccend.

c.... Dimensioned arguments.

c---- Cumulative probability, bins 1 to n.
      dimension pcum    (1)
c---- Index of sampled bin.
      dimension nb      (1)

c.... Local variables.

c---- Index in internal array.
      common /laptcums/ n
c---- First index of subset of data.
      common /laptcums/ n1
c---- Last index of subset of data.
      common /laptcums/ n2
c---- Index in external array.
      common /laptcums/ nn
c---- Size of current subset of data.
      common /laptcums/ ns
c---- Number of striated samples.
      common /laptcums/ nstrd
c---- Number of unstriated samples.
      common /laptcums/ nunst
c---- Random numbers in range 0.0 to 1.0.
      common /laptcums/ ranfp   (64)
c---- Striated random number, 0.0 to 1.0.
      common /laptcums/ ranst
cbugc***DEBUG begins.
cbugc---- Fraction of samples in bin.
cbug      common /laptcums/ fb
cbugc---- Index in loop over bins.
cbug      common /laptcums/ nbin
cbug 9901 format (/ 'aptcums sampling from cumulative PDF.' /
cbug     &  'np=',i5,' frstr=',1pe22.14 /
cbug     &  (i3,' pcum=',1pe22.14))
cbug      write (3, 9901) np, frstr, (n, pcum(n), n = 1, nbins)
cbugc***DEBUG ends.

c.... initialize.

      nerr = 0

c.... Test for input errors.

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

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

      if ((frstr .lt. 0.0) .or. (frstr .gt. 1.0)) then
        nerr = 3
        go to 210
      endif

c.... Find the number of striated and unstriated samples.

      nstrd = frstr * np + 0.5
      nunst = np - nstrd

c.... See if any unstriated samples are needed.

c---- Need unstriated samples.
      if (nunst .gt. 0) then

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

        n1 = 1
        n2 = min (nunst, 64)

c....   Loop over the subset of samples.

c---- Loop over subset of samples.
  110   ns = n2 - n1 + 1

c....   Generate the needed random numbers.

c---- Loop over samples.
        do 120 n = 1, ns
          ranfp(n) = ranf( )
c---- End of loop over samples.
  120   continue

c....   Randomly sample bin numbers.

c---- Loop over samples.
        do 130 n = 1, ns

          nn     = n + n1 - 1
          nb(nn) = luf (ranfp(n) * pcum(nbins), pcum, nbins - 1)

c---- End of loop over samples.
  130   continue

c....   See if all subsets of samples are done.

c---- Do another subset of data.
        if (n2 .lt. nunst) then
          n1 = n2 + 1
          n2 = min (nunst, n1 + 63)
c---- End of loop over subset of samples.
          go to 110
        endif

c---- Tested nunst.
      endif

c.... See if any striated samples are needed.

c---- Need striated samples.
      if (nstrd .gt. 0) then

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

        n1 = 1 + nunst
        n2 = min (np, n1 + 63)

c....   Loop over the subset of samples.

c---- Loop over subset of samples.
  140   ns = n2 - n1 + 1

c....   Generate the needed random numbers.

c---- Loop over samples.
        do 150 n = 1, ns
          ranfp(n) = ranf( )
c---- End of loop over samples.
  150   continue

c....   Randomly sample bin numbers.

c---- Loop over samples.
        do 160 n = 1, ns

          nn     = n + n1 - 1
          ranst  = (nn - nunst - ranfp(n)) / nstrd
          nb(nn) = luf (ranst * pcum(nbins), pcum, nbins - 1)

c---- End of loop over samples.
  160   continue

c....   See if all subsets of samples are done.

c---- Do another subset of data.
        if (n2 .lt. np) then
          n1 = n2 + 1
          n2 = min (np, n1 + 63)
c---- End of loop over subset of samples.
          go to 140
        endif

c---- Tested nstrd.
      endif
cbugc***DEBUG begins.
cbug 9902 format (/ 'aptcums results:' /
cbug     &  '  nunst=',i5,' nstrd=',i5)
cbug 9903 format (5('  n=',i5,' nb=',i3))
cbug      write ( 3, 9902) nunst, nstrd
cbug      write ( 3, 9903) (n, nb(n), n = 1, np)
cbug
cbug 9904 format (/ '  summary by bins:')
cbug 9905 format (i5,' samples=',i5,' fraction=',1pe22.14)
cbug      write ( 3, 9904)
cbugc---- Loop over bins.
cbug      do 190 nbin = 1, nbins
cbug        ns = 0
cbugc---- Loop over samples.
cbug        do 185 n = 1, np
cbug          if (nb(n) .eq. nbin) then
cbug            ns = ns + 1
cbug          endif
cbugc---- End of loop over samples.
cbug  185   continue
cbug        fb = ns / (np + 1.e-99)
cbug        write ( 3, 9905) nbin, ns, fb
cbugc---- End of loop over bins.
cbug  190 continue
cbugc***DEBUG ends.

  210 return

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

UCRL-WEB-209832