In [107]:
import numpy as np
import matplotlib.pyplot as plt
In [56]:
plt.rcParams['figure.figsize'] = [8, 8]
In [81]:
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
In [105]:
ising = Ising(100, 100)
for _ in range(100000):
    ising.update(0.01)
ising.plot()
In [94]:
isi = Ising(400, 400)
In [95]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [96]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [97]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [98]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [99]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [101]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [102]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [103]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()
In [104]:
for _ in range(100000):
    isi.update(0.01)
isi.plot()