import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [8, 8]
class Ising(object):
def __init__(self, n, m):
self.n = n
self.m = m
self.state = np.random.choice([-1, 1], n*m).reshape((n, m))
def plot(self):
fig, ax = plt.subplots()
ax.set_aspect(1)
xy = np.array([[a, b] for a in range(self.n) for b in range(self.m)])
fills = ['black' if (s == 1) else 'white' for s in self.state.reshape((self.n*self.m,))]
ax.scatter(xy[:,0], xy[:,1], c=fills, marker="8", s=5)
def update(self, T):
x = np.random.randint(self.n)
y = np.random.randint(self.m)
s = self.state[x, y]
mismatches = 0
for dx, dy in [(-1, 0), (+1, 0), (0, -1), (0, +1)]:
xp = ((x + dx) % self.n)
yp = ((y + dy) % self.m)
if self.state[xp, yp] * s == -1:
mismatches += 1
dE = 4 - 2 * mismatches
if dE < 0 or np.random.uniform() < np.exp(-dE / T):
self.state[x, y] *= -1
ising = Ising(100, 100)
for _ in range(100000):
ising.update(0.01)
ising.plot()
isi = Ising(400, 400)
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()
for _ in range(100000):
isi.update(0.01)
isi.plot()