
C------------------------------------------------------------------------
C
C           initialize
C
C------------------------------------------------------------------------


      SUBROUTINE  initialize(seed, start)
      INCLUDE  "exp_wolff.h"
      INTEGER   seed, start, i, j
      INTEGER   d1, d2, d3
      LOGICAL*1 error, Log2
      EXTERNAL  Log2

      CALL KXPARA(nddata)
      procnum=nddata(1)
      ori_nproc=nddata(2)

      error=.TRUE.
      DO WHILE(error)
         WRITE(6,20) 'Enter domain size(Max=', Nmax, '):'
 20      FORMAT(2X,A,I4,A)
         READ(5,*)  N
         error = ( .NOT.Log2(N) .OR. (N.LT.1) .OR. (N.GT.Nmax) )
         IF(error) THEN
            WRITE(6,*) '??? Improper input. Try again.'
         ENDIF
      ENDDO

      error=.TRUE.      
      DO WHILE(error)
         WRITE(6,20) 'Enter number of processors(Max=', ori_nproc, '):'
         READ(5,*)  nproc
         error = (.NOT.Log2(nproc) .OR. (nproc.LT.2) .OR.
     *            ( nproc .GT. ori_nproc) .OR. (nproc .GT. N) )
         IF(error) THEN
            WRITE(6,*) '??? Improper input. Try again.'
         ENDIF
      ENDDO
      
      d1=KXGDSP(nproc,1,procdim)
      d2=KXGDIN(1,procdim)
      d3=KXGDCO(procnum,proccoord)

      error=.TRUE.
      DO WHILE(error) 
         WRITE(6,20) 'Enter width of a partition(Max=', N/nproc, '):'
         READ(5,*)   width
         error = ( .NOT.Log2(width) .OR. (width.LT.1) .OR. 
     *             (width .GT. N/nproc) )
         IF(error)  THEN
            WRITE(6,*) '??? Improper input. Try again.'
         ENDIF
      ENDDO   

      WRITE(6,*) 'Enter the number of thermalization sweeps: '
      READ(5,*)  therm_sweeps

      WRITE(6,*) 'Enter number of sweeps per measurements:'
      READ(5,*)  sweeps

      WRITE(6,*) 'Enter number of measurements:'
      READ(5,*)  measurements

      WRITE(6,*) 'Enter Beta value:'
      READ(5,*)  beta

      WRITE(6,*) 'Enter control of initial state
     *            (0=ordered, otherwise random):'
      READ(5,*)  start

      error=.TRUE.
      DO WHILE(error) 
         WRITE(6,*) 'Enter seed number: '
         READ(5,*)  seed
         error = ( seed.LT.0 )
         IF(error) THEN
            WRITE(6,*) '??? Improper input. Try again!'
         ENDIF
      ENDDO

      

C----Get the coords of neighboring processors.
      lpnum=KXGDNO(procnum,0,-1)
      rpnum=KXGDNO(procnum,0, 1)

C----Order the processors
      CALL  processor_order
  
C--- DEBUG!!
c      call kmulti(6)
c      write (6,*) 'procnum=',procnum,'left=',lpnum,'right=',rpnum
c      call kflush(6)
c      call ksingl(6)

      twidth = width * nproc
      ntile  = N / twidth

      Nrow   = N
      Ncol   = width * ntile

      p = 1. - exp( -2. * beta )

      vol        = FLOAT (N*N)


      WRITE(6,*) '*************************************************'
      WRITE(6,*) '           Wolff Cluster (EXPRESS) '
      WRITE(6,*) '-------------------------------------------------'
      WRITE(6,*) '                  input  data     '
      WRITE(6,*) '*************************************************'
      WRITE(6,*) ' Domain size               = ', N, 'x', N, '=', N*N
      WRITE(6,*) ' Number of processors      = ', nproc
      WRITE(6,*) ' Width of each partition   = ', width
      WRITE(6,*) ' Height of each partition  = ', N
      WRITE(6,*) ' Number of tiles           = ', ntile
      WRITE(6,*) ' Width of each title       = ', twidth
      WRITE(6,*) ' Local domain size         = ', Nrow,'x',Ncol,'=',Nrow*Ncol
      WRITE(6,*) ' '
      WRITE(6,*) ' Thermalization sweeps     = ', therm_sweeps
      WRITE(6,*) ' Sweeps per measurements   = ', sweeps
      WRITE(6,*) ' Number of measurments     = ', measurements
      WRITE(6,*) ' Beta value                = ', beta
      WRITE(6,*) ' Wolff probability         = ', p
      WRITE(6,*) ' Starting state            = ', start
      WRITE(6,*) ' Random seed               = ', seed
      WRITE(6,*) '*************************************************'

      RETURN
      END


C--------------------------------------------------------------

C
C    Find the order of processor,  start from node 0
C    then find the right side node of each node 
C

      SUBROUTINE  processor_order
      INCLUDE  "exp_wolff.h"
      INTEGER  i,d

      i=0
      IF(procnum .EQ. 0) THEN
         proc_order(1)=0      !start from node 0
         proc_order(2)=rpnum
      ENDIF

      i=i+1
      d=KXBROD(proc_order,0, nproc*4, IALNOD, 0, i+111)

 77   i=i+1
      IF((procnum .EQ. proc_order(i)) .AND. (procnum .NE. 0)) THEN
         proc_order(i+1)=rpnum
      ENDIF
      d=KXBROD(proc_order,proc_order(i),nproc*4,IALNOD,0,i+111)
      IF(proc_order(i+1) .NE. 0) THEN
         GOTO 77
      ENDIF

      WRITE(6,22) 'Order of processor: ',(proc_order(i), i=1,nproc)
 22   FORMAT(/,2X,A,64(I2,2X))
      WRITE(6,*) ' '

      RETURN
      END

	    
C--------------------------------------------------------------------------

 
C  Initialize the spins with an ordered or random start.
 
	SUBROUTINE init_spins(start)
	INCLUDE "exp_wolff.h"
	INTEGER start
	INTEGER i,j
	REAL ranmar
	
	IF (start.EQ.0) THEN
 
C ------  Ordered (cold) start
	  WRITE(6,*) 'Cold start'
 
	  DO i=0,Nrow-1
             DO j=0,Ncol-1
                spin(i,j) = 1
             ENDDO
	  ENDDO

       ELSE 
 
C ------  Random (hot) start
	  WRITE(6,*) 'Hot start'
 
	  DO i=0,Nrow-1
             DO j=0,Ncol-1
                IF (ranmar() .LT. 0.5) THEN
                   spin(i,j) = 1
                ELSE
                   spin(i,j) = -1
                ENDIF
             ENDDO
	  ENDDO

       ENDIF
	
	
       RETURN 
       END

