Ian Jauslin
summaryrefslogtreecommitdiff
blob: 382fe6bd28ebd7603e8de5f18d37a9b61a05094d (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
## 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.

using Distributed
@everywhere using FastGaussQuadrature
@everywhere using LinearAlgebra
@everywhere using Polynomials
@everywhere using Printf
@everywhere using SpecialFunctions
include("chebyshev.jl")
include("integration.jl")
include("interpolation.jl")
include("tools.jl")
include("potentials.jl")
include("print.jl")
include("easyeq.jl")
include("simpleq-Kv.jl")
include("simpleq-iteration.jl")
include("simpleq-hardcore.jl")
include("anyeq.jl")
function main()
  ## defaults

  # values of rho,e, when needed
  rho=1e-6
  e=1e-4

  # incrementally initialize Newton algorithm
  nlrho_init=1

  # potential
  v=k->v_exp(k,1.)
  a0=a0_exp(1.)
  # arguments of the potential
  v_param_a=1.
  v_param_b=1.
  v_param_c=1.
  v_param_e=1.

  # plot range when plotting in rho
  minlrho=-6
  maxlrho=2
  nlrho=100
  rhos=Array{Float64}(undef,0)
  # plot range when plotting in e
  minle=-6
  maxle=2
  nle=100
  es=Array{Float64}(undef,0)
  # plot range when plotting in x
  xmin=0
  xmax=100
  nx=100

  # cutoffs
  tolerance=1e-11
  order=100
  maxiter=21

  # for anyeq
  # P
  P=11
  # N
  N=12
  # number of splines
  J=10

  # starting rho from which to incrementally initialize Newton algorithm
  # default must be set after reading rho, if not set explicitly
  minlrho_init=nothing

  # Hann window for Fourier transforms
  windowL=1e3

  # approximation for easyeq
  # bK,bL
  easyeq_simpleq_approx=Easyeq_approx(0.,0.)
  easyeq_medeq_approx=Easyeq_approx(1.,1.)
  easyeq_approx=easyeq_simpleq_approx

  # approximation for anyeq
  # aK,bK,gK,aL1,bL1,aL2,bL2,gL2,aL3,bL3,gl3
  anyeq_simpleq_approx=Anyeq_approx(0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,0.)
  anyeq_bigeq_approx=Anyeq_approx(1.,1.,1.,1.,1.,1.,1.,1.,0.,0.,0.)
  anyeq_fulleq_approx=Anyeq_approx(1.,1.,1.,1.,1.,1.,1.,1.,1.,0.,1.)
  anyeq_medeq_approx=Anyeq_approx(0.,1.,1.,0.,1.,0.,0.,0.,0.,0.,0.)
  anyeq_compleq_approx=Anyeq_approx(1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.)
  anyeq_approx=anyeq_bigeq_approx

  # read cli arguments
  (params,potential,method,savefile,command)=read_args(ARGS)

  # read params
  if params!=""
    for param in split(params,";")
      terms=split(param,"=")
      if length(terms)!=2
	print(stderr,"error: could not read parameter '",param,"'.\n")
	exit(-1)
      end
      lhs=terms[1]
      rhs=terms[2]
      if lhs=="rho"
	rho=parse(Float64,rhs)
      elseif lhs=="minlrho_init"
	minlrho_init=parse(Float64,rhs)
      elseif lhs=="nlrho_init"
	nlrho_init=parse(Int64,rhs)
      elseif lhs=="e"
	e=parse(Float64,rhs)
      elseif lhs=="tolerance"
	tolerance=parse(Float64,rhs)
      elseif lhs=="order"
	order=parse(Int64,rhs)
      elseif lhs=="maxiter"
	maxiter=parse(Int64,rhs)
      elseif lhs=="rhos"
	rhos=parse_list(rhs)
      elseif lhs=="minlrho"
	minlrho=parse(Float64,rhs)
      elseif lhs=="maxlrho"
	maxlrho=parse(Float64,rhs)
      elseif lhs=="nlrho"
	nlrho=parse(Int64,rhs)
      elseif lhs=="es"
	es=parse_list(rhs)
      elseif lhs=="minle"
	minle=parse(Float64,rhs)
      elseif lhs=="maxle"
	maxle=parse(Float64,rhs)
      elseif lhs=="nle"
	nle=parse(Int64,rhs)
      elseif lhs=="xmin"
	xmin=parse(Float64,rhs)
      elseif lhs=="xmax"
	xmax=parse(Float64,rhs)
      elseif lhs=="nx"
	nx=parse(Int64,rhs)
      elseif lhs=="P"
	P=parse(Int64,rhs)
      elseif lhs=="N"
	N=parse(Int64,rhs)
      elseif lhs=="J"
	J=parse(Int64,rhs)
      elseif lhs=="window_L"
	windowL=parse(Float64,rhs)
      elseif lhs=="aK"
	anyeq_approx.aK=parse(Float64,rhs)
      elseif lhs=="bK"
	anyeq_approx.bK=parse(Float64,rhs)
	easyeq_approx.bK=parse(Float64,rhs)
      elseif lhs=="gK"
	anyeq_approx.gK=parse(Float64,rhs)
      elseif lhs=="aL1"
	anyeq_approx.aL1=parse(Float64,rhs)
      elseif lhs=="bL"
	easyeq_approx.bL=parse(Float64,rhs)
      elseif lhs=="bL1"
	anyeq_approx.bL1=parse(Float64,rhs)
      elseif lhs=="aL2"
	anyeq_approx.aL2=parse(Float64,rhs)
      elseif lhs=="bL2"
	anyeq_approx.bL2=parse(Float64,rhs)
      elseif lhs=="gL2"
	anyeq_approx.gL2=parse(Float64,rhs)
      elseif lhs=="aL3"
	anyeq_approx.aL3=parse(Float64,rhs)
      elseif lhs=="bL3"
	anyeq_approx.bL3=parse(Float64,rhs)
      elseif lhs=="gL3"
	anyeq_approx.gL3=parse(Float64,rhs)
      elseif lhs=="v_a"
	v_param_a=parse(Float64,rhs)
      elseif lhs=="v_b"
	v_param_b=parse(Float64,rhs)
      elseif lhs=="v_c"
	v_param_c=parse(Float64,rhs)
      elseif lhs=="v_e"
	v_param_e=parse(Float64,rhs)
      elseif lhs=="eq"
	if rhs=="simpleq"
	  easyeq_approx=easyeq_simpleq_approx
	  anyeq_approx=anyeq_simpleq_approx
	elseif rhs=="medeq"
	  easyeq_approx=easyeq_medeq_approx
	  anyeq_approx=anyeq_medeq_approx
	elseif rhs=="bigeq"
	  anyeq_approx=anyeq_bigeq_approx
	elseif rhs=="fulleq"
	  anyeq_approx=anyeq_fulleq_approx
	elseif rhs=="compleq"
	  anyeq_approx=anyeq_compleq_approx
	else
	    print(stderr,"error: unrecognized equation: ",rhs,"\n")
	    exit(-1)
	end
      else
	print(stderr,"unrecognized parameter '",lhs,"'.\n")
	exit(-1)
      end
    end
  end

  ## read potential
  if potential=="exp"
    v=k->v_exp(k,v_param_a)
    a0=a0_exp(v_param_a)
  elseif potential=="expcry"
    v=k->v_expcry(k,v_param_a,v_param_b)
    a0=a0_expcry(v_param_a,v_param_b)
  elseif potential=="npt"
    v=k->v_npt(k)
    a0=a0_npt()
  elseif potential=="alg"
    v=v_alg
    a0=a0_alg
  elseif potential=="algwell"
    v=v_algwell
    a0=a0_algwell
  elseif potential=="exact"
    v=k->v_exact(k,v_param_b,v_param_c,v_param_e)
    a0=a0_exact(v_param_b,v_param_c,v_param_e)
  elseif potential=="tent"
    v=k->v_tent(k,v_param_a,v_param_b)
    a0=a0_tent(v_param_a,v_param_b)
  else
    print(stderr,"unrecognized potential '",potential,"'.\n'")
    exit(-1)
  end

  ## set parameters
  # rhos
  if length(rhos)==0
    rhos=Array{Float64}(undef,nlrho)
    for j in 0:nlrho-1
      rhos[j+1]=(nlrho==1 ? 10^minlrho : 10^(minlrho+(maxlrho-minlrho)/(nlrho-1)*j))
    end
  end
  # es
  if length(es)==0
    es=Array{Float64}(undef,nle)
    for j in 0:nle-1
      es[j+1]=(nle==1 ? 10^minle : 10^(minle+(maxle-minle)/(nle-1)*j))
    end
  end

  # default minlrho_init
  if (minlrho_init==nothing)
    minlrho_init=log10(rho)
  end

  # splines
  taus=Array{Float64}(undef,J+1)
  for j in 0:J
    taus[j+1]=-1+2*j/J
  end

  ## run command
  if method=="easyeq"
    if command=="energy"
      easyeq_energy(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,easyeq_approx)
    # e(rho)
    elseif command=="energy_rho"
      easyeq_energy_rho(rhos,order,a0,v,maxiter,tolerance,easyeq_approx)
    # u(k)
    elseif command=="uk"
      easyeq_uk(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,easyeq_approx)
    # u(x)
    elseif command=="ux"
      easyeq_ux(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,easyeq_approx)
    elseif command=="uux"
      easyeq_uux(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,easyeq_approx)
    # condensate fraction
    elseif command=="condensate_fraction"
      easyeq_condensate_fraction(minlrho_init,nlrho_init,order,rho,a0,v,maxiter,tolerance,easyeq_approx)
    elseif command=="condensate_fraction_rho"
      easyeq_condensate_fraction_rho(rhos,order,a0,v,maxiter,tolerance,easyeq_approx)
    else
      print(stderr,"unrecognized command '",command,"'.\n")
      exit(-1)
    end
  elseif method=="simpleq-hardcore"
    if command=="energy_rho"
      simpleq_hardcore_energy_rho(rhos,taus,P,N,J,maxiter,tolerance)
    elseif command=="ux"
      simpleq_hardcore_ux(rho,taus,P,N,J,maxiter,tolerance)
    elseif command=="condensate_fraction_rho"
      simpleq_hardcore_condensate_fraction_rho(rhos,taus,P,N,J,maxiter,tolerance)
    end
  elseif method=="simpleq-iteration"
    # u_n(x) using iteration
    if command=="u"
      simpleq_iteration_ux(order,e,v,maxiter,xmin,xmax,nx)
    # rho(e) using iteration
    elseif command=="rho_e"
      simpleq_iteration_rho_e(es,order,v,maxiter)
    else
      print(stderr,"unrecognized command '",command,"'.\n")
      exit(-1)
    end
  elseif method=="anyeq"
    if command=="save_Abar"
      anyeq_save_Abar(taus,P,N,J,v,anyeq_approx)
    elseif command=="energy"
      anyeq_energy(rho,minlrho_init,nlrho_init,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    # e(rho)
    elseif command=="energy_rho"
      anyeq_energy_rho(rhos,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    elseif command=="energy_rho_init_prevrho"
      anyeq_energy_rho_init_prevrho(rhos,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    elseif command=="energy_rho_init_nextrho"
      anyeq_energy_rho_init_nextrho(rhos,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    # u(j)
    elseif command=="uk"
      anyeq_uk(minlrho_init,nlrho_init,taus,P,N,J,rho,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    # u(j)
    elseif command=="ux"
      anyeq_ux(minlrho_init,nlrho_init,taus,P,N,J,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,anyeq_approx,savefile)
    # condensate fraction
    elseif command=="condensate_fraction"
      anyeq_condensate_fraction(rho,minlrho_init,nlrho_init,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    elseif command=="condensate_fraction_rho"
      anyeq_condensate_fraction_rho(rhos,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    # momentum distribution
    elseif command=="momentum_distribution"
      anyeq_momentum_distribution(rho,minlrho_init,nlrho_init,taus,P,N,J,a0,v,maxiter,tolerance,anyeq_approx,savefile)
    elseif command=="2pt"
      anyeq_2pt_correlation(minlrho_init,nlrho_init,taus,P,N,J,windowL,rho,a0,v,maxiter,tolerance,xmin,xmax,nx,anyeq_approx,savefile)
    else
      print(stderr,"unrecognized command: '",command,"'\n")
      exit(-1)
    end
  elseif method=="simpleq-Kv"
    if command=="2pt"
      simpleq_Kv_2pt(minlrho_init,nlrho_init,taus,P,N,J,rho,a0,v,maxiter,tolerance,xmin,xmax,nx)
    elseif command=="condensate_fraction_rho"
      simpleq_Kv_condensate_fraction(rhos,taus,P,N,J,a0,v,maxiter,tolerance)
    elseif command=="Kv"
      simpleq_Kv_Kv(minlrho_init,nlrho_init,taus,P,N,J,rho,a0,v,maxiter,tolerance,xmin,xmax,nx)
    else
      print(stderr,"unrecognized command: '",command,"'\n")
      exit(-1)
    end
  else
    print(stderr,"unrecognized method: '",method,"'\n")
    exit(-1)
  end

end

# parse a comma separated list as an array of Float64
function parse_list(str)
  elems=split(str,",")
  out=Array{Float64}(undef,length(elems))
  for i in 1:length(elems)
    out[i]=parse(Float64,elems[i])
  end
  return out
end

# read cli arguments
function read_args(ARGS)
  # flag
  flag=""

  # output strings
  params=""
  # default potential
  potential="exp"
  # default method
  method="easyeq"
  
  savefile=""
  command=""

  # loop over arguments
  for arg in ARGS
    # argument that starts with a dash
    if arg[1]=='-'
      # go through characters after dash
      for char in arg[2:length(arg)]
	
	# set params
	if char=='p'
	  # raise flag
	  flag="params"
	elseif char=='U'
	  # raise flag
	  flag="potential"
	elseif char=='M'
	  # raise flag
	  flag="method"
	elseif char=='s'
	  # raise flag
	  flag="savefile"
	else
	  print_usage()
	  exit(-1)
	end
      end
    # arguments that do not start with a dash
    else
      if flag=="params"
	params=arg
      elseif flag=="potential"
	potential=arg
      elseif flag=="method"
	method=arg
      elseif flag=="savefile"
	savefile=arg
      else
	command=arg
      end
      # reset flag
      flag=""
    end
  end

  if command==""
    print_usage()
    exit(-1)
  end

  return (params,potential,method,savefile,command)
end

# usage
function print_usage()
  print(stderr,"usage:  simplesolv [-p params] [-U potential] [-M method] [-s savefile] <command>\n")
end

main()