C
C       Metropolis Monte Carlo update of the Ising model
C      (Metropolis et al., J. Chem. Phys. 21 (1953) 1087).
C
C    Based on the code in the book "Statistical Field Theory", 
C    G. Parisi, (Addison-Wesley, 1988).
C
C    For the Ising model:
C      current_energy = - current_spin * spin_sum
C          new_energy = - new_spin * spin_sum 
C                     = current_spin * spin_sum
C    Thus
C         energy_diff = new_energy - current_energy 
C                     = 2 * current_spin * spin_sum
C
C    As long as isiz and jsiz are both >=2, we don't have to do a
C    black/white checkerboard update, since sites being updated 
C    simultaneously on different processors will not be neighbors.
C


	SUBROUTINE metupdate(iterations)
	INCLUDE "common.h"
	INTEGER iterations
	INTEGER n, i,j, current_spin,spin_sum,energy_diff
	INTEGER get_spin,spin1,spin2,spin3,spin4
	REAL ranmar
	
	DO n=1,iterations

	  accept = 0

	  DO i=1,isiz
	  DO j=1,jsiz

	    current_spin = spin(i,j)
 
C --------  Get neighboring spins (may need to get values from
C --------  neighboring processors, which may have been changed).
            spin1 = get_spin(i-1,j)
            spin2 = get_spin(i+1,j)
            spin3 = get_spin(i,j-1)
            spin4 = get_spin(i,j+1)

C --------  Sum neighboring spins.
	    spin_sum = spin1 + spin2 + spin3 + spin4

C --------  Metropolis accept/reject step.
	    energy_diff = 2 * current_spin * spin_sum
	    IF ( ( energy_diff.LE.0 ) .OR.
     &           ( EXP(-beta*energy_diff).GT.ranmar() ) ) THEN
	      spin(i,j) = -current_spin
	      accept = accept + 1
	    ENDIF

	  ENDDO
	  ENDDO

	ENDDO

C ----  Pass edges to neighboring processors.
        CALL spin_edges

	RETURN 
	END


