Ising coupling.py
From Werner KRAUTH
Context
This page is part of my 2024 Beg Rohu Lectures on "The second Markov chain revolution" at the Summer School "Concepts and Methods of Statistical Physics" (3 - 15 June 2024).
The present program illustrates the coupling and the phenomenon of monotone coupling in Markov-chain sampling in the example of the two-dimensional Ising model on an LxL square lattice with periodic boundary conditions. We start, at time t=0 with two configurations, one called SLow (all spins equal to -1) and another one, called SHigh (all spins equal to +1), and runs the same Markov chain on both of them until they resulting configurations coincide.
Python program
import math import random L = 64; N = L * L nbr = {i : ((i // L) * L + (i + 1) % L, (i + L) % N, (i // L) * L + (i - 1) % L, (i - L) % N) \ for i in range(N)} beta = 0.4 # beta = 0.4407 is critical temperature SLow = [-1 for site in range(N)] SHigh = [1 for site in range(N)] iter = 0 while True: iter += 1 k = random.randint(0, N - 1) Upsilon = random.uniform(0.0, 1.0) hLow = sum(SLow[nn] for nn in nbr[k]) hHigh = sum(SHigh[nn] for nn in nbr[k]) SLow[k] = -1 if Upsilon < 1.0 / (1.0 + math.exp(-2.0 * beta * hLow)): SLow[k] = 1 SHigh[k] = -1 if Upsilon < 1.0 / (1.0 + math.exp(-2.0 * beta * hHigh)): SHigh[k] = 1 if SHigh == SLow: print(iter / N) print(SLow, SHigh) break