-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_4MMC.py
290 lines (232 loc) · 8.4 KB
/
3_4MMC.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
from numpy import linalg as LA
from numpy.linalg import matrix_power
import time
import datetime
import sys
import os
import random
import math
import scipy as sp
import scipy.linalg
from scipy.linalg import expm
from matplotlib.pyplot import *
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import pyplot
startTime = time.time()
print ("STARTED:" , datetime.datetime.now().strftime("%d %B %Y %H:%M:%S"))
if len(sys.argv) < 8:
print("Usage: python", str(sys.argv[0]), "READ-IN? " "SAVE-or-NOT? " "NCOL " "NITER " "g " "c " "kappa ")
sys.exit(1)
READIN = int(sys.argv[1])
SAVE = int(sys.argv[2])
NCOL = int(sys.argv[3])
Niters_sim = int(sys.argv[4])
g = float(sys.argv[5])
c = float(sys.argv[6])
kappa = float(sys.argv[7])
if READIN not in [0,1]:
print ("Wrong input for READIN")
sys.exit(1)
if SAVE not in [0,1]:
print ("Wrong input for SAVE")
sys.exit(1)
NMAT = 4
dt = 1e-4
nsteps = int(1e-2/dt)
GAP = 1
X = np.zeros((NMAT, NCOL, NCOL), dtype=complex)
mom_X = np.zeros((NMAT, NCOL, NCOL), dtype=complex)
f_X = np.zeros((NMAT, NCOL, NCOL), dtype=complex)
X_bak = np.zeros((NMAT, NCOL, NCOL), dtype=complex)
expDH = []
if NMAT == 3:
trXsq, trYsq, trZsq = [], [], []
if NMAT == 4:
trXsq, trYsq, trZsq, trWsq = [], [], [], []
if NMAT > 4:
print ("This is not supported & checked yet! Lower NMAT or modify the code (not difficult!) or contact the author")
sys.exit(1)
print ("Matrix chain with %2.0f matrices" %(NMAT))
print ("NCOL = " "%3.0f "","" and g = %4.2f,c = %4.2f,kappa = %4.2f"%(NCOL, g, c, kappa))
print ("----------------------------------------------------")
def dagger(a):
return np.transpose(a).conj()
def box_muller():
PI = 2.0*math.asin(1.0);
r = random.uniform(0,1)
s = random.uniform(0,1)
p = np.sqrt(-2.0*np.log(r)) * math.sin(2.0*PI*s)
q = np.sqrt(-2.0*np.log(r)) * math.cos(2.0*PI*s)
return p,q
def comm(A,B):
return np.dot(A,B) - np.dot(B,A)
def copy_fields(b):
for j in range(NMAT):
X_bak[j] = b[j]
return X_bak
def rejected_go_back_old_fields(a):
for j in range(NMAT):
X[j] = a[j]
return X
def refresh_mom():
for j in range (NMAT):
mom_X[j] = random_hermitian()
return mom_X
def random_hermitian():
tmp = np.zeros((NCOL, NCOL), dtype=complex)
for i in range (NCOL):
for j in range (i+1, NCOL):
r1, r2 = box_muller()
tmp[i][j] = complex(r1, r2)/math.sqrt(2)
tmp[j][i] = complex(r1, -r2)/math.sqrt(2)
for i in range (NCOL):
r1, r2 = box_muller()
tmp[i][i] = complex(r1, 0.0)
return tmp
def makeH(tmp):
tmp2 = 0.50*(tmp+dagger(tmp)) - (0.50*np.trace(tmp+dagger(tmp))*np.eye(NCOL))/NCOL
for i in range (NCOL):
tmp2[i][i] = complex(tmp[i][i].real,0.0)
if np.allclose(tmp2, dagger(tmp2)) == False:
print ("WARNING: Couldn't make hermitian.")
return tmp2
def hamil(X,mom_X):
ham = potential(X)
for j in range (NMAT):
ham += 0.50 * np.trace(np.dot(mom_X[j],mom_X[j])).real
return ham
def potential(X):
s1 = 0.0
for i in range (NMAT):
s1 += 0.50 * np.trace(np.dot(X[i],X[i]))
s1 += (g/4.0)* np.trace((matrix_power(X[i], 4)))
if i == NMAT-1:
pre = kappa
else:
pre = c
s1 -= pre*np.trace(np.dot(X[i],X[(i+1)%NMAT]))
return (s1*NCOL).real
def force(X):
f_X = np.zeros((NMAT, NCOL, NCOL), dtype=complex)
for i in range (NMAT):
if i == NMAT-1:
pre1 = kappa
pre2 = c
elif i == 0:
pre1 = c
pre2 = kappa
else:
pre1 = pre2 = c
f_X[i] = (X[i]+(g*(matrix_power(X[i], 3))) - (pre1*X[(i+1)%NMAT]) - (pre2*X[(i-1+NMAT)%NMAT]))*NCOL
for j in range(NMAT):
if np.allclose(f_X[j], dagger(f_X[j])) == False:
f_X[j] = makeH(f_X[j])
return f_X
def leapfrog(X,dt):
mom_X = refresh_mom()
ham_init = hamil(X,mom_X)
for j in range(NMAT):
X[j] = X[j] + (mom_X[j] * dt * 0.50)
for j in range(NMAT):
for i in range(1, nsteps):
f_X = force(X)
mom_X[j] = mom_X[j] - (f_X[j]*dt)
X[j] = X[j] + (mom_X[j]*dt)
f_X = force(X)
for j in range(NMAT):
mom_X[j] = mom_X[j] - (f_X[j] * dt)
X[j] = X[j] + (mom_X[j] * dt * 0.50)
ham_final = hamil(X,mom_X)
return X, ham_init, ham_final
def update(X, acc_count):
X_bak = copy_fields(X)
X, start, end = leapfrog(X, dt)
change = end - start
if np.exp(-change) < random.uniform(0,1):
X = rejected_go_back_old_fields(X_bak)
print(("REJECT: deltaH = " "%10.7f " " startH = " "%10.7f" " endH = " "%10.7f" % (change, start, end)))
else:
print(("ACCEPT: deltaH = " "%10.7f " "startH = " "%10.7f" " endH = " "%10.7f" % (change, start, end)))
acc_count += 1
if NMAT >= 3:
tmp0 = np.trace(np.dot(X[0],X[0])).real
tmp00 = np.trace(np.dot(np.dot(X[0],X[0]),np.dot(X[0],X[0]))).real
trXsq.append(tmp0/NCOL)
tmp1 = np.trace(np.dot(X[1],X[1])).real
tmp11 = np.trace(np.dot(np.dot(X[1],X[1]),np.dot(X[1],X[1]))).real
trYsq.append(tmp1/NCOL)
tmp2 = np.trace(np.dot(X[2],X[2])).real
tmp22 = np.trace(np.dot(np.dot(X[2],X[2]),np.dot(X[2],X[2]))).real
trZsq.append(tmp2/NCOL)
if NMAT == 4:
tmp3 = np.trace(np.dot(X[3],X[3])).real
tmp33 = np.trace(np.dot(np.dot(X[3],X[3]),np.dot(X[3],X[3]))).real
trWsq.append(tmp3/NCOL)
if MDTU%GAP == 0:
if NMAT == 3:
f3.write("%4.8f \t %4.8f \t %4.8f \n" %(tmp0/NCOL, tmp1/NCOL, tmp2/NCOL))
f4.write("%4.8f \t %4.8f \t %4.8f \n" %(tmp00/NCOL, tmp11/NCOL, tmp22/NCOL))
if NMAT == 4:
f3.write("%4.8f \t %4.8f \t %4.8f \t %4.8f \n" %(tmp0/NCOL, tmp1/NCOL, tmp2/NCOL, tmp3/NCOL))
f4.write("%4.8f \t %4.8f \t %4.8f \t %4.8f \n" %(tmp00/NCOL, tmp11/NCOL, tmp22/NCOL, tmp33/NCOL))
return X, acc_count
if __name__ == '__main__':
if READIN == 0:
print ("Loading fresh configuration")
for i in range (NMAT):
X[i] = 0.0
if READIN == 1:
name_f = "config_{}MM_N{}_g{}.npy".format(NMAT, NCOL, g)
if os.path.isfile(name_f) == True:
print ("Reading old configuration file:", name_f)
A = np.load(name_f)
for i in range (NMAT):
for j in range (NCOL):
for k in range (NCOL):
X[i][j][k] = A[i][j][k]
for j in range(NMAT):
if np.allclose(X[j], dagger(X[j])) == False:
print ("Input configuration 'X' not hermitian, ", LA.norm(X[j] - dagger(X[j])), "making it so")
X[j] = makeH(X[j])
else:
print ("Configuration not found, loaded fresh")
for i in range (NMAT):
X[i] = 0.0
f3 = open('t2_N%s_D%s_g%s.txt' %(NCOL,kappa,g), 'w')
f4 = open('t4_N%s_D%s_g%s.txt' %(NCOL,kappa,g), 'w')
acc_count = 0
for MDTU in range (1, Niters_sim+1):
X,acc_count = update(X,acc_count)
if MDTU%10 == 0 and SAVE == 1:
name_f = "config_{}MM_N{}_g{}.npy".format(NMAT, NCOL, g)
print ("Saving configuration file: ", name_f)
np.save(name_f, X)
f3.close()
f4.close()
if acc_count/Niters_sim < 0.50:
print("WARNING: Acceptance rate is below 50%")
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.grid(which='major', axis='y', linestyle='--')
MDTU = np.linspace(0, int(Niters_sim/GAP), int(Niters_sim/GAP), endpoint=True)
plt.ylabel(r'$\langle \rm{Tr}(X_{1,2,3}^{2})/N \rangle$')
plt.xlabel('Time units')
if NMAT == 3:
plot(MDTU, trXsq, 'red')
plot(MDTU, trYsq, 'teal')
plot(MDTU, trZsq, 'blue')
if NMAT == 4:
plot(MDTU, trXsq, 'red')
plot(MDTU, trYsq, 'teal')
plot(MDTU, trZsq, 'blue')
plot(MDTU, trWsq, 'green')
outname = "%sMM_N%s_k%s" %(NMAT, NCOL, kappa)
plt.savefig(outname+'.pdf')
print ("Fraction % of MDTU accepted = ", (acc_count/Niters_sim)*100)
print ("COMPLETED:" , datetime.datetime.now().strftime("%d %B %Y %H:%M:%S"))
endTime = time.time()
print ("Running time:", round(endTime - startTime, 2), "seconds")