forked from ghwatson/casini_corners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.py
293 lines (233 loc) · 9.58 KB
/
calculate.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
'''
Created on May 31, 2013
@author: ghwatson
'''
from __future__ import division
from scipy import linalg
import scipy as sp
from sympy.mpmath import mpf, extraprec, cos, log, pi, isinf, isnan
import sympy.mpmath
from maple import MapleLink
import multiprocessing
import bresenham
GUARD_START = 100 # Remembers the guard bits required to make integrals converge
def correlations(polygon, maple_dir, precision, correlators=None, verbose=False):
'''
Given a point cloud defined by polygon, this function will find the X and P
matrices of the phi and pi field correlations of free bosons on a square
2D lattice.
:param polygon: a list of points on the lattice ex: [[1,2],[5,8],[3,4]]
:param maple_dir: directory to the commandline maple
:param precision: precision with which to perform the integration
:param correlators: precomputed correlators to avoid recomputation
:param verbose: the user can turn standard output on and off.
'''
sympy.mpmath.mp.dps = precision
# Find all displacement vectors.
dist_vects = [abs(p1-p2) for p1 in polygon for p2 in polygon]
# The correlators depend symmetrically on i,j. To optimize calculations,
# trim down to the effectively unique displacement vectors.
dist_vects_ordered = [sp.array([p[1],p[0]]) if p[0] > p[1] else p for p in dist_vects]
unique_ij = list(set(tuple(p) for p in dist_vects_ordered))
# Allocate X and P.
n = len(polygon)
X = sp.zeros((n,n))
P = sp.zeros((n,n))
# Get all the unique correlators.
for [i,j] in unique_ij:
if correlators is not None and (i,j) in correlators.keys():
phi_corr, pi_corr = correlators[(i,j)]
else:
phi_corr, pi_corr = correlators_ij(i,j,maple_dir,precision)
if verbose == True:
print "Calculated integrals for i,j = {0}:".format([i,j])
print "phi: {0}".format(phi_corr)
print "pi: {0}".format(pi_corr)
correlators[(i,j)] = [phi_corr, pi_corr]
# Fill out X,P.
for n1,p1 in enumerate(polygon):
for n2,p2 in enumerate(polygon):
[i,j] = abs(p1-p2)
if i > j:
i,j = j,i
X[n1,n2],P[n1,n2] = correlators[(i,j)]
if correlators is not None:
return X,P,correlators
else:
return X,P
# Used in the below function as a worker in multiprocessing code. This is the
# kernel that gets parallelized.
def worker(data):
# Unpack data.
i,j,verbose,maple_dir,precision = data
phi_corr, pi_corr = correlators_ij(i,j,maple_dir,precision)
if verbose == True:
print "Calculated integrals for i,j = {0}:".format([i,j])
print "phi: {0}".format(phi_corr)
print "pi: {0}".format(pi_corr)
return [i,j,phi_corr,pi_corr]
def correlations_multicore(polygon, maple_dir, precision, correlators=None, verbose=False):
'''
A variant of the above correlations function which will distribute the
integrals across many subprocesses to utilize multiple cores.
'''
sympy.mpmath.mp.dps = precision
# Find all displacement vectors.
dist_vects = [abs(p1-p2) for p1 in polygon for p2 in polygon]
# The correlators depend symmetrically on i,j. To optimize calculations,
# trim down to the effectively unique displacement vectors.
dist_vects_ordered = [sp.array([p[1],p[0]]) if p[0] > p[1] else p for p in dist_vects]
unique_ij = list(set(tuple(p) for p in dist_vects_ordered))
# Allocate X and P.
n = len(polygon)
X = sp.zeros((n,n))
P = sp.zeros((n,n))
# Record which correlators need to be computed.
pool_inputs = []
for [i,j] in unique_ij:
if correlators is not None and (i,j) in correlators.keys():
phi_corr, pi_corr = correlators[(i,j)]
correlators[(i,j)] = [phi_corr, pi_corr]
else:
# Aggregate jobs for the pool.
pool_inputs.append([i,j,verbose,maple_dir,precision])
# Calculate correlators using a pool of workers.
pool_size = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=pool_size)
pool_outputs = pool.map(worker,pool_inputs)
pool.close()
pool.join()
for i,j,phi,pi in pool_outputs:
correlators[(i,j)] = [phi,pi]
# Fill out X,P.
for n1,p1 in enumerate(polygon):
for n2,p2 in enumerate(polygon):
[i,j] = abs(p1-p2)
if i > j:
i,j = j,i
X[n1,n2],P[n1,n2] = correlators[(i,j)]
if correlators is not None:
return X,P,correlators
else:
return X,P
def correlators_ij(i, j, maple_dir, precision):
'''
Calculate the integral cos(ix)cos(jy)/sqrt(2(1-cos(x))+2(1-cos(y))) for x,y = -Pi..Pi.
This is done by computing the inner integral symbolically, and the outer numerically.
The integral is done on a quarter quadrant since it is symmetric.
:param i: lattice index i
:param j: lattice index j
:param maple_dir: the directory to the commandline maple.
:param precision: the arithmetic precision (for numerical integration)
'''
# Set the mpmath precision.
sympy.mpmath.mp.dps = precision
global GUARD_START
maple_link = MapleLink(maple_dir,precision)
# Quicker if the higher frequency occurs in numerically solved outer
# integral.
if i>j:
i,j = j,i
# Perform the inner integrals.
phi_str = "cos({0}*x)/sqrt(2*(1-cos(x))+2*(1-cos(y)))".format(int(i))
phi_integ_str = "simplify(int({0},x=0..Pi) assuming y >= 0);".format(phi_str)
pi_str = "cos({0}*x)*sqrt(2*(1-cos(x))+2*(1-cos(y)))".format(int(i))
pi_integ_str = "simplify(int({0},x=0..Pi) assuming y >= 0);".format(pi_str)
inner_pi_str = maple_link.query(pi_integ_str)
maple_link.parse(inner_pi_str)
pi_stack = maple_link.get_parsed_stack()
inner_phi_str = maple_link.query(phi_integ_str)
maple_link.parse(inner_phi_str)
phi_stack = maple_link.get_parsed_stack()
def phi_inner_integral(y):
vardict = {'y':y}
out = maple_link.eval_stack(phi_stack, vardict)
return out*cos(int(j)*y)
def pi_inner_integral(y):
vardict = {'y':y}
out = maple_link.eval_stack(pi_stack, vardict)
return out*cos(int(j)*y)
# Perform the outer integrals.
def int_fail(integ):
return (isnan(integ) or isinf(integ))
# This loop will increase the bits of precision in the integral calculations
# if they don't converge (low enough precision results in INF as a result).
# The global is used to remember the precision that works.
for guard_bits in xrange(GUARD_START,1000,100):
phi_integ = sympy.mpmath.quad(extraprec(guard_bits)(phi_inner_integral),[0,pi])
if int_fail(phi_integ):
continue
pi_integ = sympy.mpmath.quad(extraprec(guard_bits)(pi_inner_integral),[0,pi])
if int_fail(pi_integ):
continue
GUARD_START = guard_bits
break
else:
raise ValueError("Hit guard bit tolerance of 1000!") #TODO: make this an input if needed
# Prefactors
phi_integ *= mpf('1')/(2*pi**2)
pi_integ *= mpf('1')/(2*pi**2)
return phi_integ,pi_integ
def entropy(X, P, n, precision, truncate, verbose=False):
'''
Using the correlator matrices, find the nth entropy.
:param X: spatial correlator matrix (sympy)
:param P: momentum correlator matrix (sympy)
:param n: Renyi index
:param precision: arithmetic precision
:param verbose: runtime output flag
'''
XP = sp.dot(X,P)
# DEBUGGING: Saving the correlator matrices.
# prec = sympy.mpmath.mp.dps
# sp.savetxt("xmat.txt", X, fmt='%.{0}f'.format(prec),delimiter=",")
# sp.savetxt("pmat.txt",P, fmt='%.{0}f'.format(prec),delimiter=",")
# Scipy eigenvalues
sqrt_eigs = sp.sqrt(linalg.eigvals(XP))
# Check that the eigenvalues are well-defined.
to_remove = []
for i, eig in enumerate(sqrt_eigs):
if eig.real <= 0.5:
if truncate:
# Remove the eigenvalue
to_remove.append(i)
else:
raise ValueError("At least one of the eigenvalues of sqrt(XP) is below 0.5! \n eig = {0}".format(eig))
if eig.imag != 0:
print "Warning: got an imaginary eigvalue component: " + str(eig.imag)
sqrt_eigs = sp.delete(sqrt_eigs,to_remove)
# Chop off imaginary component.
sqrt_eigs = sqrt_eigs.real
# Calculate entropy.
S_n = 0
if n == 1:
for vk in sqrt_eigs:
S_n += ((vk + 0.5)*log(vk + 0.5) - (vk - 0.5)*log(vk - 0.5))
else:
for vk in sqrt_eigs:
S_n += log((vk + 0.5)**n - (vk - 0.5)**n)
S_n *= 1./(n-1)
if verbose==True:
print "Calculated entropy of {0}".format(S_n)
S_n = float(S_n)
return S_n
def square_lattice(L):
'''
Generates an array of points making up a square LxL lattice.
:param L: the dimension of the square lattice.
'''
x = sp.linspace(0,L,L+1)
coord_arrays = sp.meshgrid(x,x)
polygon = (sp.dstack(coord_arrays))
polygon = sp.reshape(polygon,((L+1)**2,2))
return polygon
def circle_lattice(L):
'''
Generates an array of points making up a rasterized disc with integer
radius L.
:param L: the radius of the disk
'''
coord_tuples = bresenham.raster_disk(0,0,L)
unique_tuples = set(coord_tuples)
coords = [sp.array(i) for i in unique_tuples]
return coords