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


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

	  accept = 0

	  DO i=1,side
	  DO j=1,side

	    current_spin = spin(i,j)
 
C --------  Sum neighboring spins.
	    spin_sum = spin(i,pos(j)) + spin(i,neg(j)) +
     &                 spin(pos(i),j) + spin(neg(i),j)

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
	RETURN 
	END


