-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMetropolis_functions.py
406 lines (343 loc) · 16.4 KB
/
Metropolis_functions.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import numpy as np
import matplotlib.pyplot as plt
from numba import jit # sudo pip3 install numba
import io
import imageio # sudo pip3 install imageio
from Observables_functions import get_energy_per_spin_per_lattice, get_energy, get_magnetisation_squared
"""
This file contains functions for Metropolis algorithm computation for the 2D XY-model using NumPy and Numba.
"""
@jit(nopython=True)
def rand_2D_indices(L):
"""
Generates row index and column index ranging from 0 to L-1 in a uniformly random manner.
Parameters
----------
L: int
Lattice size where the total number of spins is given by L×L.
Returns
-------
i: int
Uniformly generated random number ranging from 0 to L-1. It is used to index the lattice.
j: int
Uniformly generated random number ranging from 0 to L-1. It is used to index the lattice.
"""
i, j = np.random.randint(low=0, high=L, size=2)
return i, j
@jit(nopython=True)
def get_energy_difference_with_trial_state(J, L, i, j, new_phi, lattice):
"""
Get the energy difference between the trial state and the current state.
Parameters
----------
J: float
Coupling constant. It must follow that J>0 such that we are studying the ferromagnetic XY-model.
L: int
Lattice size where the total number of spins is given by L×L.
i: int
Uniformly generated random number ranging from 0 to L-1. It is used to index the lattice.
j: int
Uniformly generated random number ranging from 0 to L-1. It is used to index the lattice.
new_phi: np.ndarray (float)
A numpy array containing N uniformly generated numbers ranging from -π to π, where N = relaxation_time * T_n.
lattice: np.ndarray (float)
The input lattice containing L×L spins.
Returns
-------
dE: float
Energy difference between the new and current lattice such that dE = E_new - E_current.
"""
old = np.cos( lattice[i , j] - lattice[(i+1)%L , j] ) \
+ np.cos( lattice[i , j] - lattice[(i-1)%L , j] ) \
+ np.cos( lattice[i , j] - lattice[i , (j+1)%L] ) \
+ np.cos( lattice[i , j] - lattice[i , (j-1)%L] )
new = np.cos( new_phi - lattice[(i+1)%L , j] ) \
+ np.cos( new_phi - lattice[(i-1)%L , j] ) \
+ np.cos( new_phi - lattice[i , (j+1)%L] ) \
+ np.cos( new_phi - lattice[i , (j-1)%L] )
dE = -J * (new - old)
return dE
@jit(nopython=True)
def Metropolis_single_iteration(J, L, lattice, T):
"""
A single iteration of the Metropolis algorithm. It mutates the numpy array feed into this function.
Parameters
----------
J: float
Coupling constant. It must follow that J>0 such that we are studying the ferromagnetic XY-model.
L: int
Lattice size where the total number of spins is given by L×L.
lattice: np.ndarray (float)
The input lattice containing L×L spins.
T: float
The temperature of the system in units of kB.
Returns
-------
None
"""
i, j = rand_2D_indices(L)
new_phi = (2 * np.pi) * np.random.rand() - np.pi
dE = get_energy_difference_with_trial_state(J, L, i, j, new_phi, lattice)
if (dE <= 0):
lattice[i, j] = new_phi
else:
r = np.random.rand()
W = np.exp(-1 / T * dE) # T is in units of kB
if (r < W):
lattice[i, j] = new_phi
@jit(nopython=True)
def store_lattice_for_plotting(save_for_plot, total_counter, plot_at_Nth_index, lattices_plot, lattice, index_history, T):
"""
Stores lattice into array for plotting.
"""
if save_for_plot and (total_counter in plot_at_Nth_index):
lattices_plot[np.where(
plot_at_Nth_index == total_counter)[0][0], :, :] = lattice
index_history[np.where(
plot_at_Nth_index == total_counter)[0][0]] = T
@jit(nopython=True)
def Metropolis(J, L, relaxation_time, extra_time, lattice,
T_init, T_final, T_n, plot_at_Nth_index, save_for_plot=False):
"""
Applies the XY-model Metropolis evolution algorithm at temperature T_init to a given input lattice of size L×L, where each of the spins takes on value ranging from -π to π.
After relaxation_time number of time-steps, the temperature of the system changes until we run for the final time for T_final. This is why the function is labelled slow quench.
Parameters
----------
J: float
Coupling constant. It must follow that J>0 such that we are studying the ferromagnetic XY-model.
L: int
Lattice size where the total number of spins is given by L×L.
relaxation_time: int
The number of Metropolis evolution time-steps one applies to the lattice for a given temperature T before changing the temperature.
extra_time: int
The number of Metropolis evolution time-steps one applies ot the lattice after the relaxation_time.
lattice: np.ndarray (float)
The input lattice containing L×L spins.
T_init: float
The initial temperature of the system in units of kB (Boltzmann constant).
T_final: float
The final temperature of the system in units of kB.
T_n: int
The number of points between T_init and T_final, inclusive.
plot_at_Nth_index: np.ndarray (int)
Specifies at which Metropolis evolution time-steps to save the lattice snapshot for plotting purposes.
save_for_plot: bool
Specifies whether to store the lattices for plotting purposes.
Returns
-------
ave_M2: np.ndarray (float)
The normalised magnetisation squared.
ave_E: np.ndarray (float)
The mean of energy.
ave_E2:
The mean of energy squared.
lattices_plot: np.ndarray (float)
A numpy array of shape (len(plot_at_Nth_index),L,L). It stores the snapshot of the lattice at time-steps specified by plot_at_Nth_index.
This is used for plotting the evolution of the lattice.
index_history: np.ndarray (float)
A numpy array of shape (len(plot_at_Nth_index),). It stores the index of the system at time-steps specified by plot_at_Nth_index.
This is used for plotting the evolution of the lattice.
"""
T_array = np.linspace(T_init, T_final, T_n) # In units of kB
total_counter = 0
reduced_interval = int(L**2/2)
reduced_extra_time = int( np.ceil(extra_time/reduced_interval) )
ave_E = np.zeros((T_n,reduced_extra_time))
ave_M2 = np.zeros((T_n,reduced_extra_time))
ave_E2 = np.zeros((T_n,reduced_extra_time))
if save_for_plot:
lattices_plot = np.zeros((len(plot_at_Nth_index), L, L))
index_history = np.zeros(len(plot_at_Nth_index), dtype=np.int32)
for a in range(T_n):
j = 0
for b in range(relaxation_time):
Metropolis_single_iteration(J, L, lattice, T_array[a])
#store_lattice_for_plotting(save_for_plot, total_counter, plot_at_Nth_index, lattices_plot, lattice, index_history, a)
# total_counter += 1
for c in range(extra_time):
Metropolis_single_iteration(J, L, lattice, T_array[a])
if not(c==0) and (c%reduced_interval==0 or c==extra_time-1):
E = get_energy(J, L, lattice)
ave_E2[a,j] = E**2
ave_E[a,j] = E
ave_M2[a,j] = get_magnetisation_squared(lattice)
j = j + 1
store_lattice_for_plotting(save_for_plot, total_counter, plot_at_Nth_index, lattices_plot, lattice, index_history, a)
total_counter += 1
ave_M2 = ave_M2/(L**4)
#Cv = (ave_E2-ave_E)
return ave_M2, ave_E, ave_E2, lattices_plot, index_history
def creategif(J, L, T, Tc, ave_M2, Cv, plot_at_Nth_index, lattices_plot, index_history, filename, plot_mode):
"""
Creates an animated .gif of the evolution of the lattice in θ∈[-π,π) space. No temporary files are created as we are utilising RAM.
Parameters
----------
J: float
Coupling constant. It must follow that J>0 such that we are studying the ferromagnetic XY-model.
L: int
Lattice size where the total number of spins is given by L×L.
T: np.ndarray (float)
A numpy array of shape (len(plot_at_Nth_index),). It stores the temperature of the system at time-steps specified by plot_at_Nth_index.
Tc: float
The critical temperature.
ave_M2: np.ndarray (float)
The normalised magnetisation squared.
Cv: np.ndarray (float)
The specific heat capacity.
plot_at_Nth_index: np.ndarray (int)
Specifies at which Metropolis evolution time-steps to save the lattice snapshot for plotting purposes.
lattices_plot: np.ndarray (float)
A numpy array of shape (len(plot_at_Nth_index),L,L). It stores the snapshot of the lattice at time-steps specified by plot_at_Nth_index.
index_history: np.ndarray (float)
A numpy array of shape (len(plot_at_Nth_index),). It stores the index of the system at time-steps specified by plot_at_Nth_index.
This is used for plotting the evolution of the lattice.
filename: string
Specifies the filename of the .gif to be saved.
plot_mode: string
Specifies the type of animated .gif plot to be produced. The possible options are:
1.) phase_space_noarrows
2.) phase_space_arrows
3.) energy_space_arrows
4.) phase_and_energy_spaces_arrows
5.) phase_and_energy_spaces_noarrows
6.) phase_and_energy_spaces_noarrows_and_observables
Returns
-------
None
"""
assert plot_mode in ['phase_space_noarrows','phase_space_arrows','energy_space_arrows','phase_and_energy_spaces_arrows','phase_and_energy_spaces_noarrows','phase_and_energy_spaces_noarrows_and_observables']
X, Y = np.mgrid[0:L, 0:L]
with imageio.get_writer(filename, mode='I') as writer:
for counter, index in enumerate(index_history):
print(
f"Creating gif... {np.round((counter+1)/len(plot_at_Nth_index)*100,2)}%"
)
if plot_mode == 'phase_space_noarrows':
plt.imshow(lattices_plot[counter, :, :], cmap='hsv')
plt.clim(-np.pi, np.pi)
plt.colorbar(ticks=[-np.pi, 0, np.pi])
elif plot_mode == 'phase_space_arrows':
U, V = np.cos(lattices_plot[counter, :, :].T), np.sin(
lattices_plot[counter, :, :].T)
plt.quiver(X,
Y,
U,
V,
edgecolor='k',
facecolor='None',
linewidth=.5)
plt.imshow(lattices_plot[counter, :, :], cmap='hsv')
plt.clim(-np.pi, np.pi)
plt.colorbar(ticks=[-np.pi, 0, np.pi])
elif plot_mode == 'energy_space_arrows':
U, V = np.cos(lattices_plot[counter, :, :].T), np.sin(
lattices_plot[counter, :, :].T)
E = get_energy_per_spin_per_lattice(
J, lattices_plot[counter, :, :])
plt.quiver(X,
Y,
U,
V,
edgecolor='k',
facecolor='None',
linewidth=.5)
plt.imshow(E, cmap='YlOrRd')
plt.clim(-4, 0)
plt.colorbar(ticks=[-4, -2, 0])
elif plot_mode == 'phase_and_energy_spaces_arrows':
U, V = np.cos(lattices_plot[counter, :, :].T), np.sin(
lattices_plot[counter, :, :].T)
E = get_energy_per_spin_per_lattice(
J, lattices_plot[counter, :, :])
fig = plt.figure(figsize=(10, 3.7))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.quiver(X,
Y,
U,
V,
edgecolor='k',
facecolor='None',
linewidth=.5)
im1 = ax1.imshow(lattices_plot[counter, :, :],
vmin=-np.pi,
vmax=np.pi,
cmap='hsv')
fig.colorbar(im1, ticks=[-3.14, 0, 3.14], ax=ax1)
ax1.set_title("Phase space, $θ\in(-\pi,\pi)$")
ax2.quiver(X,
Y,
U,
V,
edgecolor='k',
facecolor='None',
linewidth=.5)
im2 = ax2.imshow(E, vmin=-4, vmax=0, cmap='YlOrRd')
fig.colorbar(im2, ticks=[-4, -2, 0], ax=ax2)
ax2.set_title("Energy, $E$")
elif plot_mode == 'phase_and_energy_spaces_noarrows':
E = get_energy_per_spin_per_lattice(
J, lattices_plot[counter, :, :])
fig = plt.figure(figsize=(10, 3.7))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
im1 = ax1.imshow(lattices_plot[counter, :, :],
vmin=-np.pi,
vmax=np.pi,
cmap='hsv')
fig.colorbar(im1, ticks=[-3.14, 0, 3.14], ax=ax1)
ax1.set_title("Phase space, $θ\in(-\pi,\pi)$")
im2 = ax2.imshow(E, vmin=-4, vmax=0, cmap='YlOrRd')
fig.colorbar(im2, ticks=[-4, -2, 0], ax=ax2)
ax2.set_title("Energy, $E$")
elif plot_mode == 'phase_and_energy_spaces_noarrows_and_observables':
E = get_energy_per_spin_per_lattice(
J, lattices_plot[counter, :, :])
fig = plt.figure(figsize=(10, 8))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
im1 = ax1.imshow(lattices_plot[counter, :, :],
vmin=-np.pi,
vmax=np.pi,
cmap='hsv')
fig.colorbar(im1, ticks=[-3.14, 0, 3.14], ax=ax1)
ax1.set_title("Phase space, $θ\in(-\pi,\pi)$")
im2 = ax2.imshow(E, vmin=-4, vmax=0, cmap='YlOrRd')
fig.colorbar(im2, ticks=[-4, -2, 0], ax=ax2)
ax2.set_title("Energy, $E$")
ax3.plot(T/Tc, ave_M2, 'r-')
ax3.plot( T[index]/Tc , ave_M2[index] , 'ro', markersize=15)
ax3.set_xlabel("$T/T_c$")
ax3.set_ylabel("$\langle M^2\\rangle/N^2$")
ax3.set_ylim([0,1])
ax3.set_xlim([T[0]/Tc,T[-1]/Tc])
ax3.grid()
ax3.set_title("Magnetisation squared, $\langle M^2\\rangle/N^2$")
ax4.plot( T/Tc , Cv/((L**2*T**2)) , 'g-')
ax4.plot( T[index]/Tc , Cv[index]/((L**2*T[index]**2)) , 'go', markersize=15)
ax4.set_xlabel("$T/T_c$")
ax4.set_ylabel("$c/k_B$")
ax4.set_ylim([0,2])
ax4.set_xlim([T[0]/Tc,T[-1]/Tc])
ax4.grid()
ax4.set_title("Specific heat capacity per spin, $c/k_B$")
if (plot_mode != 'phase_and_energy_spaces_arrows') and (plot_mode != 'phase_and_energy_spaces_noarrows') and (plot_mode != 'phase_and_energy_spaces_noarrows_and_observables'):
plt.title(
f"Run #{plot_at_Nth_index[counter]}. $J$={J}. $T/T_c$={np.round(T[index]/Tc,2)}. $L$={L}."
)
else:
fig.suptitle(
f"Run #{plot_at_Nth_index[counter]}. $J$={J}. $T/T_c$={np.round(T[index]/Tc,2)}. $L$={L}."
)
buf = io.BytesIO()
plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
buf.seek(0)
image = imageio.imread(buf)
writer.append_data(image)
buf.close()
plt.cla()
plt.clf()
plt.close('all')
print(f"Gif created.")