-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransferMatrix2DIsing
172 lines (134 loc) · 4.35 KB
/
TransferMatrix2DIsing
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
import numpy
import itertools
import scipy.sparse.linalg
import math
import heapq
import numpy as np
from irlb import *
import time
# ALGORITHM TO PERMUTE WITHOUT REPETIONS
class unique_element:
def __init__(self,value,occurrences):
self.value = value
self.occurrences = occurrences
def perm_unique(elements):
eset=set(elements)
listunique = [unique_element(i,elements.count(i)) for i in eset]
u=len(elements)
return perm_unique_helper(listunique,[0]*u,u-1)
def perm_unique_helper(listunique,result_list,d):
if d < 0:
yield tuple(result_list)
else:
for i in listunique:
if i.occurrences > 0:
result_list[d]=i.value
i.occurrences-=1
for g in perm_unique_helper(listunique,result_list,d-1):
yield g
i.occurrences+=1
class TransferMatrix:
######### working with the Transfer matrix
def __init__(self,N,J):
self.N = N
self.J = J
def compute(self,N,J):
self.N = N
self.J = J
lista = list() # defining a list
lista2 = list() # defining a list
for i in range(1,self.N+1):
lista.append(1)
for j in range(1,self.N):
lista[self.N-j] = -1
a = list(perm_unique(lista))# making the permutations with the class (permutation without repetition)
lista2.append(a)
# removing extra brackets
lista2 = list(itertools.chain(*lista2))
#lista2.reverse()
################## just adding the missing states #################
add1 = []
add2 = []
for i in range(1,self.N+1):
add1.append(1)
add2.append(-1)
add1 = tuple(add1)
add2 = tuple(add2)
lista2.insert(0,tuple(add1))
lista2.insert(len(lista2),tuple(add2))
tst = []
for i in range(0,len(lista2)):
tst.append(lista2[i])
lista2 = list(itertools.chain(tst)) # just removing the extra unneccessery brackets
####################
Nu = 2**(self.N)
p = numpy.zeros(Nu*Nu).reshape((Nu,Nu))
for i in range(0,Nu):
for j in range(0,Nu):
sum1 = 0
for k in range(0,self.N):
sum1 = sum1 + self.J*(lista2[i][k]*lista2[j][k])
for l in range(0,N):
if l==N-1:
k = 0
else:
k = l + 1
sum1 = sum1 + self.J*(lista2[i][l]*lista2[i][k])/2
sum1 = sum1 + self.J*(lista2[j][l]*lista2[j][k])/2
p[i][j] = np.float64(math.exp(sum1))
return p
"""
N = 10
J = 1
T = TransferMatrix(N,J).compute(N,J)
vals = numpy.linalg.eigvals(T)
#print vals
#print T
#print "----- -------- ------ ----"
T = T.reshape(2**(N),2**(N))
#print T
nu = 2
tic1 = time.clock()
S = numpy.linalg.eigvals(T)
toc1 = time.clock()
tic2 = time.clock()
X = irlb(T,nu)
toc2 = time.clock()
print 'vreme za prvi:',toc1-tic1,'s'
print 'vreme za drugi:',toc2-tic2,'s'
print(X[1])
print(max(S))
print max(X[1])
"""
nu=2
for k in range(0,50):
print k
s = 0.2 + k*0.01
c = ''.join([str('koji'),str(s),str('.txt')])
f = open(c,'w')
print "N/","/Largest EigenValues","/Free Energy", "-----J = ",s
for i in range(0,13):
N = i+1
print 'gde sam:',N
J = s
tic1 = time.clock()
T = TransferMatrix(N,J).compute(N,J)
toc1 = time.clock()
print 'vreme za transfer matricu:',toc1-tic1,'s'
#vals, vecs = scipy.sparse.linalg.eigsh(T,k=2**(N)-1)
#vals = numpy.linalg.eigvals(T)
T = T.reshape(2**(N),2**(N))
tic2 = time.clock()
X = irlb(T,nu)
toc2 = time.clock()
print 'vreme za dijagonalizaciju:',toc2-tic2,'s'
print(X[1])
print 'samo jedna vrednost:',max(X[1])
eigenvalues = np.float64(math.log(max(X[1]))/N)
free_en = np.float64(-eigenvalues)
print N,eigenvalues,free_en
ww = ' '.join([str(N),str(free_en)])
f.write(str(ww))
f.write('\n')
f.close()
###########################################