-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMH_DFT.jl
295 lines (175 loc) · 6.65 KB
/
MH_DFT.jl
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
############################################################################################################################
############################################--- Hubbard Model by DFT ---####################################################
############################################################################################################################
############################################################################################################################
####################################################--- Importing ---#######################################################
############################################################################################################################
using LinearAlgebra #Pacote de Algebra linear
using Base.Threads # numbers in binary representation
using LaTeXStrings # For LaTex code
using SpecialFunctions #Special Functions
using Integrals #Integrals
############################################################################################################################
#############################################--- Setting the parameters ---################################################
############################################################################################################################
const U=6.0 #Couloumb repultion
const L=10 #Chain size
v=zeros(L) #Site energy
const Δn=1.E-8 #Convergence parameter
const nj_0=ones(L) #Intial density
const N=Int(L/2) #Number of electrons
const file_="1" # 1 write an article, 0 does not
println("number of threads= ",Threads.nthreads())
v[1]=2
#v[6]=0.7 #Impurity energy
############################################################################################################################
#################################################---- Functions ----########################################################
############################################################################################################################
############################################---- BA fundamental energy ----#####################################################
function integ(U,x)
f=[]
for i in x
push!(f,besselj0(i)*besselj1(i)/(i*(1+exp(U*i/2))))
end
return f
end
function Simp_rule(a,b,values,dx)
s=values[1]+values[length(values)]
for i in 2:1:length(values)-1
if i%2==0
s+=values[i]*4
else
s+=values[i]*2
end
end
return s*dx/3
end
function BA_E0L(U)
a=10^-8
b=10^5
x=LinRange(a,b,10^7)
f_x=integ(U,x)
dx=x[2]-x[1]
return -4*Simp_rule(a,b,f_x,dx)
end
#################################################---- Finding β(U)----#####################################################
function f(x)
return -(2*x/pi)*sin(pi/x)
end
function df(x)
y=x/pi
return (2*cos(1/y)/y-2*sin(1/y))/pi
end
function NM(x0,E0)
rpi2=floor(E0*2/pi)
x0=x0+rpi2*pi/2
xnl=x0
xn=xnl-(f(xnl)-E0)/df(xnl)
N=0
while (abs(xnl-xn)>10^-6)&&(N<10^3)
xnl=xn
xn=xnl-(f(xnl)-E0)/df(xnl)
N+=1
end
return xn
end
function bU(E0)
return NM(0.01,E0)
end
#############################################---- Khon-Sham potential----#####################################################
function Pot_KS(nj,βU,U)
vj=zeros(length(nj))
for i in 1:1:length(nj)
n=nj[i]
if (n>=0)&&(n<=1)
vj[i]+=-2*cos(pi*n/2)-U/2*n-2*cos(n*pi/βU)
elseif (n>1)&&(n<=2)
vj[i]+=2*cos(pi*(2-n)/2)-U/2*n+2*cos((2-n)*pi/βU)-2*βU*U/pi
end
end
return vj
end
#############################################---- Hartree potential----#####################################################
function Pot_H(nj,U)
vj=zeros(length(nj))
for i in 1:1:length(nj)
n=nj[i]
vj[i]+=U*n/2
end
return vj
end
#############################################---- Hamiltonian construction----#####################################################
function mH(L,U,βU,nj_,vj,μ)
mat=zeros(L,L)
if U==0
vKS=zeros(L)
else
vKS=Pot_KS(nj_,βU,U)
end
vH=Pot_H(nj_,U)
for i in 1:1:L-1
mat[i,i+1]=mat[i+1,i]+=-1.0
end
for i in 1:1:L
mat[i,i]+=vKS[i]+vj[i]+vH[i]+μ
end
return mat
end
#############################################---- Eletronic Density----#####################################################
function nj(Vec,N,L)
nj_=zeros(L)
for j in 1:1:L
for i in 1:N
nj_[j]+=abs2(Vec[j,i])*2
end
end
return nj_
end
#############################################---- Fundamental State Energy----#####################################################
function E_fun(nj_,βU,U)
E=0.0
for n in nj_
if (n>=0)&&(n<=1)
E+=sin(pi*n/βU)
elseif (n>1)&&(n<=2)
E+=sin(pi*(2-n)/βU)+U*(n-1)*pi/(-2*βU)
end
end
return -2*βU*E/pi
end
############################################################################################################################
#########################################---- Main Calculation function ----################################################
############################################################################################################################
function SCC(L,N,U,ΔE,v,nj_0)
E0_BA=BA_E0L(U)
βU=bU(E0_BA)
H=mH(L,U,βU,nj_0,v,0.0)
Ener=eigvals(H)
Vec=eigvecs(H)
nj_=nj(Vec,N,L)
E0=E_fun(nj_0,βU,U)/L
E=E_fun(nj_,βU,U)/L
μ=-Ener[N+1]
while sum(abs.(nj_-nj_0))/L>=ΔE
nj_0=(0.95.*nj_0.+0.05.*nj_)
E0=E
H=mH(L,U,βU,nj_0,v,μ)
Ener=eigvals(H)
Vec=eigvecs(H)
nj_=nj(Vec,N,L)
E=E_fun(nj_,βU,U)/L
μ=-Ener[N+1]
println("Step Energy= ",E," ","BA Energy= ",E0_BA," ","DIF.=",abs(E-E0_BA)," μ=",μ)
end
println("Step Energy= ",E," ","BA Energy= ",E0_BA," ","DIF.=",abs(E-E0_BA)," μ=",μ)
return E, nj_
end
@time ener,den=SCC(L,N,U,Δn,v,nj_0)
if file_=="1"
file=open("V2Imp_DFT_MHU"*string(U)*"L"*string(L)*"N"*string(N)*".txt","w") # file's name -> W value, r means random, 1 is the delta's value and 100 is the number of points of time, essemble's size
write(file,"F_Energy"," ",string(ener),"\n")
for i=1:1:L
write(file,string(i)," ",string(den[i]),"\n")
end
close(file)
end