Markov ising.py

From Werner KRAUTH

Revision as of 21:51, 22 September 2015; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

This page presents the program markov_ising.py, a Markov-chain algorithm for the Ising model on an LXL square lattice in two dimensions.

Contents

Description

Program

import random, math

L = 16
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)}
nsteps = 1000000
T = 2.0
beta = 1.0 / T
S = [random.choice([1, -1]) for k in range(N)]
for step in range(nsteps):
    k = random.randint(0, N - 1)
    delta_E = 2.0 * S[k] * sum(S[nn] for nn in nbr[k])
    if random.uniform(0.0, 1.0) < math.exp(-beta * delta_E):
        S[k] *= -1
print S, sum(S)

Version

See history for version information.

Personal tools