Skip to content

Commit a1007b7

Browse files
committed
fix bug
1 parent 4b39dad commit a1007b7

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

‎montecarlo2d.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@ def __init__(self, args):
3131
self.L = args.size
3232
self.eqstep = args.eqstep
3333
self.mcstep = args.mcstep
34-
35-
self.n1 = 1.0 / (self.mcstep * self.L ** 2)
36-
self.n2 = 1.0 / (self.mcstep ** 2 * self.L ** 2)
34+
self.area = self.L ** 2
3735

3836
def _init_spin(self):
3937
return 2 * np.random.randint(2, size=(self.L, self.L)) - 1
4038

4139
# Calculate energy using neighbors
4240
def _calc_energy(self, spin):
4341
R = np.roll(spin, 1, axis=0) + np.roll(spin, -1, axis=0) + np.roll(spin, 1, axis=1) + np.roll(spin, -1, axis=1)
44-
return np.sum(-R * spin) / 4
42+
return np.sum(-R * spin) / (4 * self.area)
4543

4644
def _calc_magnetization(self, spin):
47-
return np.sum(spin)
45+
return np.sum(spin) / self.area
4846

4947
def simulate(self, beta):
5048
E1, M1, E2, M2 = 0, 0, 0, 0
@@ -58,9 +56,9 @@ def simulate(self, beta):
5856
_metropolis(spin, self.L, beta)
5957
E = self._calc_energy(spin)
6058
M = self._calc_magnetization(spin)
61-
E1 += E
62-
M1 += M
63-
E2 += self.n1 * E ** 2
64-
M2 += self.n1 * M ** 2
59+
E1 += E / self.mcstep
60+
M1 += M / self.mcstep
61+
E2 += E ** 2 / self.mcstep
62+
M2 += M ** 2 / self.mcstep
6563

66-
return self.n1 * E1, self.n1 * M1, (E2 - self.n2 * E1 * E1) * beta ** 2, (M2 - self.n2 * M1 * M1) * beta
64+
return E1, M1, (E2 - E1 * E1) * beta ** 2, (M2 - M1 * M1) * beta

‎montecarlo3d.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def __init__(self, args):
3434
self.L = args.size
3535
self.eqstep = args.eqstep
3636
self.mcstep = args.mcstep
37-
38-
self.n1 = 1.0 / (self.mcstep * self.L ** 3)
39-
self.n2 = 1.0 / (self.mcstep ** 2 * self.L ** 3)
37+
self.volume = self.L ** 3
4038

4139
def _init_spin(self):
4240
return 2 * np.random.randint(2, size=(self.L, self.L, self.L)) - 1
@@ -46,10 +44,10 @@ def _calc_energy(self, spin):
4644
R = np.roll(spin, 1, axis=0) + np.roll(spin, -1, axis=0) \
4745
+ np.roll(spin, 1, axis=1) + np.roll(spin, -1, axis=1) \
4846
+ np.roll(spin, 1, axis=2) + np.roll(spin, -1, axis=2)
49-
return np.sum(-R * spin) / 6
47+
return np.sum(-R * spin) / (6 * self.volume)
5048

5149
def _calc_magnetization(self, spin):
52-
return np.sum(spin)
50+
return np.sum(spin) / self.volume
5351

5452
# Monte Carlo CPU version
5553
def simulate(self, beta):
@@ -64,9 +62,9 @@ def simulate(self, beta):
6462
_metropolis3d(spin, self.L, beta)
6563
E = self._calc_energy(spin)
6664
M = self._calc_magnetization(spin)
67-
E1 += E
68-
M1 += M
69-
E2 += self.n1 * E ** 2
70-
M2 += self.n1 * M ** 2
65+
E1 += E / self.mcstep
66+
M1 += M / self.mcstep
67+
E2 += E ** 2 / self.mcstep
68+
M2 += M ** 2 / self.mcstep
7169

72-
return self.n1 * E1, self.n1 * M1, (E2 - self.n2 * E1 * E1) * beta ** 2, (M2 - self.n2 * M1 * M1) * beta
70+
return E1, M1, (E2 - E1 * E1) * beta ** 2, (M2 - M1 * M1) * beta

0 commit comments

Comments
 (0)