Coupling ising.py
From Werner KRAUTH
(Difference between revisions)
| Revision as of 07:53, 22 February 2023 Werner (Talk | contribs) (→Program (in Python3)) ← Previous diff |
Revision as of 11:55, 22 February 2023 Werner (Talk | contribs) Next diff → |
||
| Line 35: | Line 35: | ||
| break | break | ||
| print(T, MeanCoupling / NIter) | print(T, MeanCoupling / NIter) | ||
| - | + | [[Image:IsingCoupling.png|left|50px]] | |
| =Version= | =Version= | ||
| See history for version information. | See history for version information. | ||
| [[Category:Python]] [[Category:Honnef_2015]] [[Category:MOOC_SMAC]] | [[Category:Python]] [[Category:Honnef_2015]] [[Category:MOOC_SMAC]] | ||
Revision as of 11:55, 22 February 2023
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 |
Description
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)
Version
See history for version information.

