Coupling ising.py
From Werner KRAUTH
Revision as of 11:56, 22 February 2023; view current revision
←Older revision | Newer revision→
←Older revision | Newer revision→
This page presents the program coupling_ising.py, a heat-bath algorithm for the Ising model on an LxL square lattice in two dimensions, run for two configurations at a time. The algorithm illustrates the coupling phenomenon.
Contents |
[edit]
Description
[edit]
Program (in Python3)
import random, math
L = 7
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)}
NIter = 100
for TT in range(20, 40):
T = TT / 10
beta = 1.0 / T
MeanCoupling = 0
for iter in range(NIter):
S1 = [-1] * N
S2 = [1] * N
step = 0
while True:
step += 1
k = random.randint(0, N - 1)
Upsilon = random.uniform(0.0, 1.0)
h1 = sum(S1[nn] for nn in nbr[k])
S1[k] = -1
if Upsilon < 1.0 / (1.0 + math.exp(-2.0 * beta * h1)): S1[k] = 1
h2 = sum(S2[nn] for nn in nbr[k])
S2[k] = -1
if Upsilon < 1.0 / (1.0 + math.exp(-2.0 * beta * h2)): S2[k] = 1
if S1 == S2:
MeanCoupling += step
break
print(T, MeanCoupling / NIter)
[edit]
Output
A slightly modified graphics version of this program produces the following output:
[edit]
Version
See history for version information.

