Rep:EnergyAndMagnetisation.py:
Appearance
import numpy as np
class IsingLattice:
E = 0.0
E2 = 0.0
M = 0.0
M2 = 0.0
n_cycles = 0
def __init__(self, n_rows, n_cols):
self.n_rows = n_rows
self.n_cols = n_cols
self.lattice = np.random.choice([-1,1], size=(n_rows, n_cols))
def energy(self):
"Return the total energy of the current lattice configuration."
energy = 0.0
sum_sisj = []
for i in range(self.n_rows):
for j in range(self.n_cols):
if i < self.n_rows -1:
sum_sisj.append(self.lattice[i,j] * self.lattice[i+1,j])
else:
sum_sisj.append(self.lattice[i,j] * self.lattice[0,j])
if i > 0:
sum_sisj.append(self.lattice[i,j] * self.lattice[i-1,j])
else:
sum_sisj.append(self.lattice[i,j] * self.lattice[self.n_rows -1,j])
if j < self.n_cols -1:
sum_sisj.append(self.lattice[i,j] * self.lattice[i,j+1])
else:
sum_sisj.append(self.lattice[i,j] * self.lattice[i,0])
if j > 0:
sum_sisj.append(self.lattice[i,j] * self.lattice[i,j-1])
else:
sum_sisj.append(self.lattice[i,j] * self.lattice[i,self.n_cols -1])
sum_sum_sisj = sum(sum_sisj)
E = -0.5 * 1.0 * sum_sum_sisj
return E
def magnetisation(self):
"Return the total magnetisation of the current lattice configuration."
magnetisation_list = []
for i in range(self.n_rows):
for j in range(self.n_cols):
magnetisation_list.append(self.lattice[i,j])
magnetisation = sum (magnetisation_list)
return magnetisation