*...:....1....:....2....:....3....:....4....:....5....:....6....:....7....:....8
*    
*     simlat.f
*
*     Copyright (C) 1992.
*
*     Marie Davidian and A. Ronald Gallant 
*     P.O. Box 5513, Raleigh NC 27650-5513, USA   
*
*     Permission to use, copy, modify, and distribute this
*     software and its documentation for any purpose and
*     without fee is hereby granted, provided that the
*     above copyright notice appear in all copies and that
*     both that copyright notice and this permission notice
*     appear in supporting documentation.
*
*     This software is provided "as is" without any
*     expressed or implied warranty.
*
*...:....1....:....2....:....3....:....4....:....5....:....6....:....7....:....8
      subroutine simlat()
      implicit real*8 (a-h,o-z)
      implicit integer*4 (i-n)
      save

      include "global.f"

      real*8 V,W,work,u

      integer*4 jseed,i,ii,jj

      integer*4 mV,mwork

      parameter (mV=200,mwork=500)

      dimension V(mV),W(mV*mV),work(mwork),u(mM)

*     Error check.

      if (ntermz.gt.mV) then
        write(3,3901) ntermz
        stop
      end if

      open(unit=9,file=outfil,status='unknown',form='formatted')

*     If mwork is too small, whx should catch it.

      call whx(V,W,x,
     &  theta(iaA),theta(iaB0),theta(iaB),theta(iaR),
     &  M,L,ntermz,ntermx,Kz,Kx,work,mwork)

      jseed=iseed

      do i=1,npt

        call sampz(jseed,u,V,W,M,ntermz,ntermx)

        do jj=1,M
          z(jj)=0.d0
        end do

        do jj=1,M
        do ii=1,jj
          z(ii)=z(ii)+theta(ibR+jj*(jj-1)/2+ii)*u(jj)
        end do
        end do

        do ii=1,M
          write(9,9001) z(ii)
        end do

      end do

      close(9)

      return
3901  format(1x,'error, simlat, increase mV to at least',i5)
9001  format(e30.18)
      end
