c===========================================================
c     Tests routine for displaying 2d spin lattice using 
c     SGI-specific routine 'grlat2d'. 
c===========================================================
      program      tlat2d

      implicit     none
      integer      iargc,    i4arg

      integer      maxn
      parameter  ( maxn = 1 000 )
      real*8       a(maxn*maxn)

      integer      n,        nsteps

      logical      ltrace
      parameter  ( ltrace = .true. )

      integer      npar
      parameter  ( npar = 1 )
      real*8       vpar(1)

      integer      j

      if( iargc() .ne. 2 ) goto 900
      n      = i4arg(1,-1)
      nsteps = i4arg(2,-1)
      if( n .le. 0  .or.  nsteps .le. 0 ) goto 900
      if( ltrace ) then
         write(0,*) 'tlat2d: n: ', n, '  nsteps: ', nsteps
      end if
   
      do j = 1 , nsteps
c-----------------------------------------------------------
c        'dmrandspin' defines a 2d array of random "spins":
c        having values +1.0d0 or -1.0d0.
c-----------------------------------------------------------
         call dmrandspin(a,n,n)
c-----------------------------------------------------------
c        First call of this routine automatically opens 
c        graphics window when running on console of SGI.
c-----------------------------------------------------------
         call grlat2d(a,n,n,vpar,npar)
      end do
c-----------------------------------------------------------
c    Call with nx (or ny) < 0, will cause graphics display
c    to be "paused" until the escape key is depressed 
c    (while the cursor is in the graphics window)
c-----------------------------------------------------------
      call grlat2d(a,-n,-n,vpar,npar)

      stop

900   continue
         write(0,*) 'usage: tlat2d <n> <nsteps>'
      stop

      end

c===========================================================
c     Generates 2d random lattice with values +/- 1.0
c===========================================================
      subroutine dmrandspin(a,d1,d2)
         implicit       none
         real*8         rand
         integer        d1,    d2
         real*8         a(d1,d2)
         real*8         val
         integer        i,     j
         do j = 1 , d2
            do i = 1 , d1
               val = rand()
               if( val .ge. 0.5d0 ) then
                  a(i,j) = -1.0d0
               else
                  a(i,j) = +1.0d0
               end if
            end do
         end do
         return
      end
