forked from bdhammel/ising-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathising.py
197 lines (159 loc) · 4.98 KB
/
ising.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as manimation
from tqdm import tqdm
import click
class IsingLattice:
def __init__(self, temperature, initial_state, size):
self.size = size
self.T = temperature
self.system = self._build_system(initial_state)
@property
def sqr_size(self):
return (self.size, self.size)
def _build_system(self, initial_state):
"""Build the system
Build either a randomly distributed system or a homogeneous system (for
watching the deterioration of magnetization
Parameters
----------
initial_state : str: "r" or other
Initial state of the lattice. currently only random ("r") initial
state, or uniformly magnetized, is supported
"""
if initial_state == 'r':
system = np.random.choice([-1, 1], self.sqr_size)
elif initial_state == 'u':
system = np.ones(self.sqr_size)
else:
raise ValueError(
"Initial State must be 'r', random, or 'u', uniform"
)
return system
def _bc(self, i):
"""Apply periodic boundary condition
Check if a lattice site coordinate falls out of bounds. If it does,
apply periodic boundary condition
Assumes lattice is square
Parameters
----------
i : int
lattice site coordinate
Return
------
int
corrected lattice site coordinate
"""
if i >= self.size:
return 0
if i < 0:
return self.size - 1
else:
return i
def energy(self, N, M):
"""Calculate the energy of spin interaction at a given lattice site
i.e. the interaction of a Spin at lattice site n,m with its 4 neighbors
- S_n,m*(S_n+1,m + Sn-1,m + S_n,m-1, + S_n,m+1)
Parameters
----------
N : int
lattice site coordinate
M : int
lattice site coordinate
Return
------
float
energy of the site
"""
return -2*self.system[N, M]*(
self.system[self._bc(N - 1), M] + self.system[self._bc(N + 1), M]
+ self.system[N, self._bc(M - 1)] + self.system[N, self._bc(M + 1)]
)
@property
def internal_energy(self):
e = 0
E = 0
E_2 = 0
for i in range(self.size):
for j in range(self.size):
e = self.energy(i, j)
E += e
E_2 += e**2
U = (1./self.size**2)*E
U_2 = (1./self.size**2)*E_2
return U, U_2
@property
def heat_capacity(self):
U, U_2 = self.internal_energy
return U_2 - U**2
@property
def magnetization(self):
"""Find the overall magnetization of the system
"""
return np.abs(np.sum(self.system)/self.size**2)
def run(lattice, epochs, video=True):
"""Run the simulation
"""
FFMpegWriter = manimation.writers['ffmpeg']
writer = FFMpegWriter(fps=10)
fig = plt.figure()
with writer.saving(fig, "ising.mp4", 100):
for epoch in tqdm(range(epochs)):
# Randomly select a site on the lattice
N, M = np.random.randint(0, lattice.size, 2)
# Calculate energy of a flipped spin
E = -1*lattice.energy(N, M)
# "Roll the dice" to see if the spin is flipped
if E <= 0.:
lattice.system[N, M] *= -1
elif np.exp(-E/lattice.T) > np.random.rand():
lattice.system[N, M] *= -1
if video and epoch % (epochs//75) == 0:
img = plt.imshow(
lattice.system, interpolation='nearest', cmap='jet'
)
writer.grab_frame()
img.remove()
plt.close('all')
@click.command()
@click.option(
'--temperature', '-t',
default=0.5,
show_default=True,
help='temperature of the system'
)
@click.option(
'--initial-state', '-i',
default='r',
type=click.Choice(['r', 'u'], case_sensitive=False),
show_default=True,
help='(R)andom or (U)niform initial state of the system'
)
@click.option(
'--size', '-s',
default=100,
show_default=True,
help='Number of sites, M, in the MxM lattice'
)
@click.option(
'--epochs', '-e',
default=1_000_000,
type=int,
show_default=True,
help='Number of iterations to run the simulation for'
)
@click.option(
'--video',
is_flag=True,
help='Record a video of the simulation progression'
)
def main(temperature, initial_state, size, epochs, video):
lattice = IsingLattice(
temperature=temperature, initial_state=initial_state, size=size
)
run(lattice, epochs, video)
print(f"{'Net Magnetization [%]:':.<25}{lattice.magnetization:.2f}")
print(f"{'Heat Capacity [AU]:':.<25}{lattice.heat_capacity:.2f}")
if __name__ == "__main__":
plt.ion()
main()