@@ -29,6 +29,27 @@ def _metropolis3d(spin, L, beta):
2929 spin [x , y , z ] = s
3030
3131
32+ # Test energy
33+ @numba .jit
34+ def _calc_energy (self , spin ):
35+ energy = 0
36+ for x in range (len (spin )):
37+ for y in range (len (spin )):
38+ for z in range (len (spin )):
39+ s = spin [x , y , z ]
40+ # Sum of the spins of nearest neighbors
41+ xpp = (x + 1 ) if (x + 1 ) < L else 0
42+ ypp = (y + 1 ) if (y + 1 ) < L else 0
43+ zpp = (z + 1 ) if (z + 1 ) < L else 0
44+ xnn = (x - 1 ) if (x - 1 ) >= 0 else (L - 1 )
45+ ynn = (y - 1 ) if (y - 1 ) >= 0 else (L - 1 )
46+ znn = (z - 1 ) if (z - 1 ) >= 0 else (L - 1 )
47+ R = spin [xpp , y , z ] + spin [x , ypp , z ] + spin [x , y , zpp ] \
48+ + spin [xnn , y , z ] + spin [x , ynn , z ] + spin [x , y , znn ]
49+ energy += - R * s
50+ return energy / 6
51+
52+
3253class MonteCarlo3D :
3354 def __init__ (self , args ):
3455 self .L = args .size
@@ -42,11 +63,11 @@ def _init_spin(self):
4263 return 2 * np .random .randint (2 , size = (self .L , self .L , self .L )) - 1
4364
4465 # Calculate energy using neighbors
45- def _calc_energy (self , spin ):
46- R = np .roll (spin , 1 , axis = 0 ) + np .roll (spin , - 1 , axis = 0 ) \
47- + np .roll (spin , 1 , axis = 1 ) + np .roll (spin , - 1 , axis = 1 ) \
48- + np .roll (spin , 1 , axis = 2 ) + np .roll (spin , - 1 , axis = 2 )
49- return np .sum (- R * spin ) / 6
66+ # def _calc_energy(self, spin):
67+ # R = np.roll(spin, 1, axis=0) + np.roll(spin, -1, axis=0) \
68+ # + np.roll(spin, 1, axis=1) + np.roll(spin, -1, axis=1) \
69+ # + np.roll(spin, 1, axis=2) + np.roll(spin, -1, axis=2)
70+ # return np.sum(-R * spin) / 6
5071
5172 def _calc_magnetization (self , spin ):
5273 return np .sum (spin )
@@ -62,7 +83,7 @@ def simulate(self, beta):
6283 # Monte Carlo steps
6384 for _ in range (self .mcstep ):
6485 _metropolis3d (spin , self .L , beta )
65- E = self . _calc_energy (spin )
86+ E = _calc_energy (spin )
6687 M = self ._calc_magnetization (spin )
6788 E1 += E
6889 M1 += M
0 commit comments