-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBTW.py
executable file
·271 lines (191 loc) · 8.52 KB
/
DBTW.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
#!/usr/bin/env python
# DBTW.py - generate DoS from CG Js read in from file
# Down by the water // My lovely daughter // I took her home
# Import our numeric library
import numpy as np
# Matplotlib
import matplotlib.pyplot as pl
# Sys for arg passing
import sys
import datetime # current date for log files etc.
# from IPython import embed# we do this so we can drop to interactive python for debugging; major Python coolio
# # --> embed() <-- just add this to any point in code, and TADA!
### Matplotlib setup
#Pretty colours; via http://blog.olgabotvinnik.com/post/58941062205/prettyplotlib-painlessly-create-beautiful-matplotlib
try:
import brewer2mpl #'pip install brewer2mpl'
# Get "Set2" colors from ColorBrewer (all colorbrewer scales: http://bl.ocks.org/mbostock/5577023)
colours = brewer2mpl.get_map('Set2', 'qualitative', 8).mpl_colors
except ImportError: #If no brewer2mpl library
#Otherwise, boring built in ones...
colours='brgcmkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk' # Aaah, we fade to grey (fade to grey)
print "Hey - no brewer2mpl (try 'pip install brewer2mpl'). Thus a simple palette."
# Matplotlib - initialise figure
fig=pl.figure()
pl.axes().set_aspect('equal') # Square data .'. square figure please
# Setup size of system to study...
if len(sys.argv) > 1: n = int(sys.argv[1])
else: n=10
# Initialise our Hamiltonian matrix
H = np.zeros ( (n,n) )
if len(sys.argv) > 2: edgesfile = sys.argv[2]
else: edgesfile="test.edges"
positionsfile = sys.argv[3]
# Load off-diagonal elements from text file. Format:-
# (index_i,int) (index_j,int) (value,double) (value,double)
filein_edge=np.loadtxt(edgesfile,
dtype=[('f1', '<u4'), ('f2', '<u4'), ('f3',np.float64),('f4',np.float64)]) #specify datatypes: 4byte int, 4byte int, float64, float64
filein_pos=np.loadtxt(positionsfile,
dtype=[('f5', '<u4'),('f6',np.float64),('f7',np.float64),('f8',np.float64)])
for datum in filein_edge:
#print datum
H[datum[0],datum[1]]=datum[2] # Populate Hamiltonian with off diagonal elements
H[datum[1],datum[0]]=datum[2] # Hermition...
print "Loaded Hamiltonian... "
#pl.title("Off-diagonal elements of Hamiltonian")
#pl.imshow(H,interpolation='nearest', cmap=pl.cm.PuBuGn) # 2D colourmap of Hamiltonian, nearest interpolation.
#pl.colorbar()
#pl.show()
# Fill the diagonal elements with site energy; for tight binding
#np.fill_diagonal(H, -6.0)
for j in range(0,n):
H[j,j]=filein_edge[j][3]
#print H
print "Hamiltonian fully setup, time to solve!"
# OK; here we go - let's solve that TB Hamiltonian!
evals,evecs=np.linalg.eigh(H)
#Calculating centre of electrostatic density
centre_coords = np.zeros(3)
prob = np.zeros(n)
r=np.zeros(n)
distance_charge=np.zeros((n,2))
cum_prob=np.zeros(n)
sorted_charge=np.zeros(n)
polaron_size=np.zeros(n)
num=0.0
for i in range(0,100):
for j in range(0,n):
prob[j]=evecs[j,i]*evecs[j,i]
centre_coords[0]+=filein_pos[j][1]*prob[j]
centre_coords[1]+=filein_pos[j][2]*prob[j]
centre_coords[2]+=filein_pos[j][3]*prob[j]
#Calculating how many molecules polarons localised over
if prob[j]>0.01:num+=1
print num
for j in range(0,n):
r[j]=np.sqrt((centre_coords[0]-filein_pos[j][1])*(centre_coords[0]-filein_pos[j][1])+(centre_coords[1]-filein_pos[j][2])*(centre_coords[1]-filein_pos[j][2])+(centre_coords[2]-filein_pos[j][3])*(centre_coords[2]-filein_pos[j][3]))
# print r
idx = np.argsort(r)
sorted_r = r[idx]
sorted_charge = prob[idx]
cum_prob = np.cumsum(sorted_charge)
# print cum_prob
for j in range (0,n):
if cum_prob[j]>0.95:break
polaron_size[i] = sorted_r[j]
centre_coords = np.zeros(3) # Reset centre_coords
num=0.0 #Rest num
print "Effective size of polaron for eigenvector 0 is", polaron_size[0]
print "Effective size of polaron for eigenvector 5 is", polaron_size[5]
print "Effective size of polaron for eigenvector 10 is", polaron_size[10]
print "Effective size of polaron for eigenvector 50 is", polaron_size[50]
#Calculating electron probabilities
#max_coords = np.zeros(3)
#r=np.zeros(1000)
#for k in range(0,10):
#Finding position of site with maxiumum probability
# for i in range(0,n):
# prob[i]=evecs[i,k]*evecs[i,k]
# max_index=np.argmax(prob)
# max_coords[0]=filein_pos[max_index][1]
# max_coords[1]=filein_pos[max_index][2]
# max_coords[2]=filein_pos[max_index][3]
#print "Prob=", prob[0]
#print "Max index=", max_index
#print "Max coordinates", max_coords
#Calculating an effective radius of the polaron by adding up weighted radii from centre found above
# for j in range(0,n):
# r[k]+=np.sqrt((max_coords[0]-filein_pos[j][1])*(max_coords[0]-filein_pos[j][1])+(max_coords[1]-filein_pos[j][2])*(max_coords[1]-filein_pos[j][2])+(max_coords[2]-filein_pos[j][3])*(max_coords[2]-filein_pos[j][3]))*prob[j]
#print r
#write eigenvalue vs size of polaron to files
#polaron_evals=np.zeros((n,2))
#for i in range (0,n):
# polaron_evals[i,0]=evals[i]
# polaron_evals[i,1]=r[i]
#np.savetxt("Polaron_vs_evals.dat",polaron_evals,delimiter=' ',newline='\n')
#Plot polaron size vs eigenvalue
fig1, ax=pl.subplots()
rects1 = pl.bar(evals[0:20],polaron_size[0:20],0.00001, color='r')
#rects2 = pl.bar(evals[0:20]+0.0001,r[0:20],0.0001, color='b')
pl.title("Size of polaron vs eigenvalue")
pl.xlabel("Eigenvalues")
pl.ylabel("Effective size of polaron")
pl.show()
#print "Eigenvalues", evals
#print "Eigenvectors", evecs
#print "first Eigenvector..."
#print evecs[0]
fig2=pl.figure()
pl.title("DoS by TightBinding")
pl.subplot(311) #3 subplots stacked on top of one another
#Plot Eigenvalues with filled-in Eigenvectors^2 / electron probabilities
pl.subplot(311)
for j in [0,n/2]: #range(0,5): #Number of eigenvalues plotted (electron wavefns)
psi=evecs[:,j]*evecs[:,j]
pl.fill_between(range(n),0,psi, facecolor=colours[j%8])
pl.plot(range(n),evecs[:,j],color=colours[j%8])
pl.ylabel("Occupation")
#pl.ylim((3.8,5.0))
pl.yticks(fontsize=9)
pl.xticks(visible=False)
#Plot cumulative eigenvectors / probability density
pl.subplot(312)
for j in [0,1,2,n/4, n/2]: #range(0,5): #Number of eigenvalues plotted (electron wavefns)
psi=evecs[:,j]*evecs[:,j] # expectation value
pl.fill_between(range(n),0,sorted(psi,reverse=True), facecolor=colours[j%8]) #can't see this anymore on large plots...
psi=sorted(psi,reverse=True) # expectation value, ranked in order (largest first)
psi_sum=[0.0]
for i in range(len(psi)): # should be a nicer way to do this with functional programming!
psi_sum.append(psi_sum[-1]+psi[i])
pl.plot(psi_sum, color=colours[j%8])
pl.plot(y=0.95) # 2 sigma confidence interval? # TODO: why doesn't this work?
pl.ylabel("Cumulative Density")
#pl.ylim((3.8,5.0))
pl.yticks(fontsize=9)
#pl.xticks(visible=False)
#Plot DoS
pl.subplot(313)
pl.hist(evals,100,histtype='stepfilled',color=colours[0])
#pl.ylim(0,80)
pl.ylabel("DoS")
pl.yticks(fontsize=9)
pl.xlim(-6.5,-4.5)
pl.tight_layout(pad=0.3)
pl.show() #Displays plots!
print "Lowest Eigenvalue:\n", evals[0]
fp=open('eigenvector_balls_pymol.py','w')
fp.write("from pymol.cgo import * # get constants \nfrom pymol import cmd \n")
psi=evecs[:,0]*evecs[:,0] # scale everything relative to max density on first eigenvector
for ei,colour in zip( [0,5,10,50] , [(0,0,1),(0,1,0),(1,1,0),(1,0,0)]):
psi=evecs[:,ei]*evecs[:,ei]
maxpsi=max(psi)
fp.write("obj = [\n")
for i in reversed(np.argsort(psi)): #magic list of sorted array indices
# print locations[i]
# print psi[i]
weight=float(psi[i])/maxpsi #on interval [0,1]
fp.write("ALPHA, %f,\n" %(weight))
weight=1
fp.write("COLOR, %f, %f, %f,\n" %(colour[0]*weight , colour[1]*weight, colour[2]*weight))
fp.write("SPHERE, %f, %f, %f, 5.0,\n" %(filein_pos[i][1],filein_pos[i][2],filein_pos[i][3]))
fp.write(" END \n]\ncmd.load_cgo(obj,'EV_%d') \n" %(ei))
#print r
#print "Effective size of polaron is from", np.min(r)
#print "to", np.max(r)
#print "Saving figures...(one moment please)"
now=datetime.datetime.now().strftime("%Y-%m-%d-%Hh%Mm") #String of standardised year-leading time
pl.annotate("%s"%now,xy=(0.75,0.02),xycoords='figure fraction') #Date Stamp in corner
fig1.savefig("%s-polaron.pdf"%now)
fig2.savefig("%s-DBTW.pdf"%now) #Save figures as both PDF and easy viewing PNG (perfect for talks)
#fig.savefig("%s-DBTW.png"%now)
#fig.savefig("%s-LongSnakeMoan.ps"%now) # TODO: check with latest python scripts to see best way to export these for future inclusion in Latex etc.