Ian Jauslin
summaryrefslogtreecommitdiff
blob: 0bde3aba4f16c30032ab02980cd251612dd95785 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
## Copyright 2021 Ian Jauslin
## 
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
## 
##     http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.

# interpolation
@everywhere mutable struct Easyeq_approx
  bK::Float64
  bL::Float64
end

# compute energy
function easyeq_energy(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,approx)
  # compute gaussian quadrature weights
  weights=gausslegendre(order)

  # compute initial guess from previous rho
  (u,E,err)=easyeq_hatu(easyeq_init_u(a0,order,weights),order,(10.)^minlrho_init,v,maxiter,tolerance,weights,approx)
  for j in 2:nlrho_init
    rho_tmp=10^(minlrho_init+(log10(rho)-minlrho_init)*(j-1)/(nlrho_init-1))
    (u,E,err)=easyeq_hatu(u,order,rho_tmp,v,maxiter,tolerance,weights,approx)
  end

  # print energy
  @printf("% .15e % .15e\n",real(E),err)
end

# compute energy as a function of rho
function easyeq_energy_rho(rhos,order,a0,v,maxiter,tolerance,approx)
  # compute gaussian quadrature weights
  weights=gausslegendre(order)
  # init u
  u=easyeq_init_u(a0,order,weights)

  for j in 1:length(rhos)
    # compute u (init newton with previously computed u)
    (u,E,err)=easyeq_hatu(u,order,rhos[j],v,maxiter,tolerance,weights,approx)

    @printf("% .15e % .15e % .15e\n",rhos[j],real(E),err)

  end
end

# compute u(k)
function easyeq_uk(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,approx)
  weights=gausslegendre(order)

  # compute initial guess from previous rho
  (u,E,err)=easyeq_hatu(easyeq_init_u(a0,order,weights),order,(10.)^minlrho_init,v,maxiter,tolerance,weights,approx)
  for j in 2:nlrho_init
    rho_tmp=10^(minlrho_init+(log10(rho)-minlrho_init)*(j-1)/(nlrho_init-1))
    (u,E,err)=easyeq_hatu(u,order,rho_tmp,v,maxiter,tolerance,weights,approx)
  end

  for i in 1:order
    k=(1-weights[1][i])/(1+weights[1][i])
    @printf("% .15e % .15e\n",k,real(u[i]))
  end
end

# compute u(x)
function easyeq_ux(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,approx)
  weights=gausslegendre(order)

  # compute initial guess from previous rho
  (u,E,err)=easyeq_hatu(easyeq_init_u(a0,order,weights),order,(10.)^minlrho_init,v,maxiter,tolerance,weights,approx)
  for j in 2:nlrho_init
    rho_tmp=10^(minlrho_init+(log10(rho)-minlrho_init)*(j-1)/(nlrho_init-1))
    (u,E,err)=easyeq_hatu(u,order,rho_tmp,v,maxiter,tolerance,weights,approx)
  end

  for i in 1:nx
    x=xmin+(xmax-xmin)*i/nx
    @printf("% .15e % .15e\n",x,real(easyeq_u_x(x,u,weights)))
  end
end

# compute 2u(x)-rho u*u(x)
function easyeq_uux(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,approx)
  weights=gausslegendre(order)

  # compute initial guess from previous rho
  (u,E,err)=easyeq_hatu(easyeq_init_u(a0,order,weights),order,(10.)^minlrho_init,v,maxiter,tolerance,weights,approx)
  for j in 2:nlrho_init
    rho_tmp=10^(minlrho_init+(log10(rho)-minlrho_init)*(j-1)/(nlrho_init-1))
    (u,E,err)=easyeq_hatu(u,order,rho_tmp,v,maxiter,tolerance,weights,approx)
  end

  for i in 1:nx
    x=xmin+(xmax-xmin)*i/nx
    @printf("% .15e % .15e\n",x,real(easyeq_u_x(x,2*u-rho*u.*u,weights)))
  end
end

# condensate fraction
function easyeq_condensate_fraction(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,approx)
  # compute gaussian quadrature weights
  weights=gausslegendre(order)

  # compute initial guess from previous rho
  (u,E,err)=easyeq_hatu(easyeq_init_u(a0,order,weights),order,(10.)^minlrho_init,v,maxiter,tolerance,weights,approx)
  for j in 2:nlrho_init
    rho_tmp=10^(minlrho_init+(log10(rho)-minlrho_init)*(j-1)/(nlrho_init-1))
    (u,E,err)=easyeq_hatu(u,order,rho_tmp,v,maxiter,tolerance,weights,approx)
  end

  # compute eta
  eta=easyeq_eta(u,order,rho,v,maxiter,tolerance,weights,approx)

  # print energy
  @printf("% .15e % .15e\n",eta,err)
end

# condensate fraction as a function of rho
function easyeq_condensate_fraction_rho(rhos,order,a0,v,maxiter,tolerance,approx)
  weights=gausslegendre(order)
  # init u
  u=easyeq_init_u(a0,order,weights)

  for j in 1:length(rhos)
    # compute u (init newton with previously computed u)
    (u,E,err)=easyeq_hatu(u,order,rhos[j],v,maxiter,tolerance,weights,approx)

    # compute eta
    eta=easyeq_eta(u,order,rhos[j],v,maxiter,tolerance,weights,approx)

    @printf("% .15e % .15e % .15e\n",rhos[j],eta,err)
  end
end


# initialize u
@everywhere function easyeq_init_u(a0,order,weights)
  u=zeros(Float64,order)
  for j in 1:order
    # transformed k
    k=(1-weights[1][j])/(1+weights[1][j])
    u[j]=4*pi*a0/k^2
  end

  return u
end

# \hat u(k) computed using Newton
@everywhere function easyeq_hatu(u0,order,rho,v,maxiter,tolerance,weights,approx)
  # initialize V and Eta
  (V,V0)=easyeq_init_v(weights,v)
  (Eta,Eta0)=easyeq_init_H(weights,v)

  # init u
  u=rho*u0

  # iterate
  err=Inf
  for i in 1:maxiter-1
    new=u-inv(easyeq_dXi(u,V,V0,Eta,Eta0,weights,rho,approx))*easyeq_Xi(u,V,V0,Eta,Eta0,weights,rho,approx)

    err=norm(new-u)/norm(u)
    if(err<tolerance)
      u=new
      break
    end
    u=new
  end

  return (u/rho,easyeq_en(u,V0,Eta0,rho,weights)*rho/2,err)
end

# \Eta
@everywhere function easyeq_H(x,t,weights,v)
  return (x>t ? 2*t/x : 2)* integrate_legendre(y->2*pi*((x+t)*y+abs(x-t)*(1-y))*v((x+t)*y+abs(x-t)*(1-y)),0,1,weights)
end

# initialize V
@everywhere function easyeq_init_v(weights,v)
  order=length(weights[1])
  V=Array{Float64}(undef,order)
  V0=v(0)
  for i in 1:order
    k=(1-weights[1][i])/(1+weights[1][i])
    V[i]=v(k)
  end
  return(V,V0)
end

# initialize Eta
@everywhere function easyeq_init_H(weights,v)
  order=length(weights[1])
  Eta=Array{Array{Float64}}(undef,order)
  Eta0=Array{Float64}(undef,order)
  for i in 1:order
    k=(1-weights[1][i])/(1+weights[1][i])
    Eta[i]=Array{Float64}(undef,order)
    for j in 1:order
      y=(weights[1][j]+1)/2
      Eta[i][j]=easyeq_H(k,(1-y)/y,weights,v)
    end
    y=(weights[1][i]+1)/2
    Eta0[i]=easyeq_H(0,(1-y)/y,weights,v)
  end
  return(Eta,Eta0)
end

# Xi(u)
@everywhere function easyeq_Xi(u,V,V0,Eta,Eta0,weights,rho,approx)
  order=length(weights[1])

  # init
  out=zeros(Float64,order)

  # compute E before running the loop
  E=easyeq_en(u,V0,Eta0,rho,weights)

  for i in 1:order
    # k_i
    k=(1-weights[1][i])/(1+weights[1][i])
    # S_i
    S=V[i]-1/(rho*(2*pi)^3)*integrate_legendre_sampled(y->(1-y)/y^3,Eta[i].*u,0,1,weights)

    # A_K,i
    A=0.
    if approx.bK!=0.
      A+=approx.bK*S
    end
    if approx.bK!=1.
      A+=(1-approx.bK)*E
    end

    # T
    if approx.bK==1.
      T=1.
    else
      T=S/A
    end

    # B
    if approx.bK==approx.bL
      B=1.
    else
      B=(approx.bL*S+(1-approx.bL*E))/(approx.bK*S+(1-approx.bK*E))
    end

    # X_i
    X=k^2/(2*A*rho)

    # U_i
    out[i]=u[i]-T/(2*(X+1))*Phi(B*T/(X+1)^2)
  end

  return out
end

# derivative of Xi
@everywhere function easyeq_dXi(u,V,V0,Eta,Eta0,weights,rho,approx)
  order=length(weights[1])

  # init
  out=zeros(Float64,order,order)

  # compute E before the loop
  E=easyeq_en(u,V0,Eta0,rho,weights)

  for i in 1:order
    # k_i
    k=(1-weights[1][i])/(1+weights[1][i])
    # S_i
    S=V[i]-1/(rho*(2*pi)^3)*integrate_legendre_sampled(y->(1-y)/y^3,Eta[i].*u,0,1,weights)

    # A_K,i
    A=0.
    if approx.bK!=0.
      A+=approx.bK*S
    end
    if approx.bK!=1.
      A+=(1-approx.bK)*E
    end

    # T
    if approx.bK==1.
      T=1.
    else
      T=S/A
    end

    # B
    if approx.bK==approx.bL
      B=1.
    else
      B=(approx.bL*S+(1-approx.bL*E))/(approx.bK*S+(1-approx.bK*E))
    end

    # X_i
    X=k^2/(2*A*rho)

    for j in 1:order
      y=(weights[1][j]+1)/2
      dS=-1/rho*(1-y)*Eta[i][j]/(2*(2*pi)^3*y^3)*weights[2][j]
      dE=-1/rho*(1-y)*Eta0[j]/(2*(2*pi)^3*y^3)*weights[2][j]

      # dA
      dA=0.
      if approx.bK!=0.
	dA+=approx.bK*dS
      end
      if approx.bK!=1.
	dA+=(1-approx.bK)*dE
      end

      # dT
      if approx.bK==1.
	dT=0.
      else
	dT=(1-approx.bK)*(E*dS-S*dE)/A^2
      end

      # dB
      if approx.bK==approx.bL
	dB=0.
      else
	dB=(approx.bL*(1-approx.bK)-approx.bK*(1-approx.bL))*(E*dS-S*dE)/(approx.bK*S+(1-approx.bK*E))^2
      end

      dX=-k^2/(2*A^2*rho)*dA

      out[i,j]=(i==j ? 1 : 0)-(dT-T*dX/(X+1))/(2*(X+1))*Phi(B*T/(X+1)^2)-T/(2*(X+1)^3)*(B*dT+T*dB-2*B*T*dX/(X+1))*dPhi(B*T/(X+1)^2)
    end
  end

  return out
end

# derivative of Xi with respect to mu
@everywhere function easyeq_dXidmu(u,V,V0,Eta,Eta0,weights,rho,approx)
  order=length(weights[1])

  # init
  out=zeros(Float64,order)

  # compute E before running the loop
  E=easyeq_en(u,V0,Eta0,rho,weights)

  for i in 1:order
    # k_i
    k=(1-weights[1][i])/(1+weights[1][i])
    # S_i
    S=V[i]-1/(rho*(2*pi)^3)*integrate_legendre_sampled(y->(1-y)/y^3,Eta[i].*u,0,1,weights)

    # A_K,i
    A=0.
    if approx.bK!=0.
      A+=approx.bK*S
    end
    if approx.bK!=1.
      A+=(1-approx.bK)*E
    end

    # T
    if approx.bK==1.
      T=1.
    else
      T=S/A
    end

    # B
    if approx.bK==approx.bL
      B=1.
    else
      B=(approx.bL*S+(1-approx.bL*E))/(approx.bK*S+(1-approx.bK*E))
    end

    # X_i
    X=k^2/(2*A*rho)

    out[i]=T/(2*rho*A*(X+1)^2)*Phi(B*T/(X+1)^2)+B*T^2/(rho*A*(X+1)^4)*dPhi(B*T/(X+1)^2)
  end

  return out
end

# energy
@everywhere function easyeq_en(u,V0,Eta0,rho,weights)
  return V0-1/(rho*(2*pi)^3)*integrate_legendre_sampled(y->(1-y)/y^3,Eta0.*u,0,1,weights)
end

# condensate fraction
@everywhere function easyeq_eta(u,order,rho,v,maxiter,tolerance,weights,approx)
  (V,V0)=easyeq_init_v(weights,v)
  (Eta,Eta0)=easyeq_init_H(weights,v)

  du=-inv(easyeq_dXi(rho*u,V,V0,Eta,Eta0,weights,rho,approx))*easyeq_dXidmu(rho*u,V,V0,Eta,Eta0,weights,rho,approx)

  eta=-1/(2*(2*pi)^3)*integrate_legendre_sampled(y->(1-y)/y^3,Eta0.*du,0,1,weights)

  return eta
end

# inverse Fourier transform
@everywhere function easyeq_u_x(x,u,weights)
  order=length(weights[1])
  out=integrate_legendre_sampled(y->(1-y)/y^3*sin(x*(1-y)/y)/x/(2*pi^2),u,0,1,weights)
  return out
end