Cluster ising.py

From Werner KRAUTH

(Difference between revisions)
Jump to: navigation, search
Revision as of 21:37, 22 September 2015
Werner (Talk | contribs)

← Previous diff
Current revision
Werner (Talk | contribs)

Line 1: Line 1:
-This page presents the program markov_disks_box.py, a Markov-chain algorithm for four disks in a square box of sides 1.+This page presents the Python3 program cluster_ising.py, the Wolff cluster algorithm for the Ising model.
__FORCETOC__ __FORCETOC__
=Description= =Description=
 +The algorithm is more general: it can be used for the Ising and XY models on lattices of arbitrary shapes in any dimensions, and there are versions for particle systems. Our example code however specializes for an LxL periodic square lattice in two dimensions that is initialized with random spins, and then is updated nsteps times.
=Program= =Program=
- 
- import random 
-  
- L = [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]] 
- sigma = 0.15 
- sigma_sq = sigma ** 2 
- delta = 0.1 
- n_steps = 1000 
- for steps in range(n_steps): 
- a = random.choice(L) 
- b = [a[0] + random.uniform(-delta, delta), a[1] + random.uniform(-delta, delta)] 
- min_dist = min((b[0] - c[0]) ** 2 + (b[1] - c[1]) ** 2 for c in L if c != a) 
- box_cond = min(b[0], b[1]) < sigma or max(b[0], b[1]) > 1.0 - sigma 
- if not (box_cond or min_dist < 4.0 * sigma ** 2): 
- a[:] = b 
- print L 
- 
-=Version= 
-See history for version information. 
- 
-[[Category:Python]] 
- 
Line 53: Line 32:
for j in Cluster: for j in Cluster:
S[j] *= -1 S[j] *= -1
 +
 +=Version=
 +See history for version information.
 +
 +[[Category:Python]] [[Category:Oxford_2024]] [[Category:MOOC_SMAC]]

Current revision

This page presents the Python3 program cluster_ising.py, the Wolff cluster algorithm for the Ising model.


Contents

Description

The algorithm is more general: it can be used for the Ising and XY models on lattices of arbitrary shapes in any dimensions, and there are versions for particle systems. Our example code however specializes for an LxL periodic square lattice in two dimensions that is initialized with random spins, and then is updated nsteps times.

Program

import random, math

L = 100
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)}
T = 2.5
p  = 1.0 - math.exp(-2.0 / T)
nsteps = 10000
S = [random.choice([1, -1]) for k in range(N)]
for step in range(nsteps):
    k = random.randint(0, N - 1)
    Pocket, Cluster = [k], [k]
    while Pocket != []:
        j = random.choice(Pocket)
        for l in nbr[j]:
            if S[l] == S[j] and l not in Cluster \
                   and random.uniform(0.0, 1.0) < p:
                Pocket.append(l)
                Cluster.append(l)
        Pocket.remove(j)
    for j in Cluster:
        S[j] *= -1

Version

See history for version information.

Personal tools