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
|
using FastGaussQuadrature
using Printf
using LinearAlgebra
include("tools.jl")
include("integration.jl")
include("simpleq.jl")
include("simpleq-newton.jl")
include("iteration.jl")
function main()
## defaults
tolerance=1e-14
order=100
maxiter=21
rho=1e-6
e=1e-4
d=3
v=v_exp3d
a0=a0_exp3d
# plot range when plotting in x
xmin=0
xmax=100
nx=100
# read cli arguments
(params,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=="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=="xmin"
xmin=parse(Float64,rhs)
elseif lhs=="xmax"
xmax=parse(Float64,rhs)
elseif lhs=="nx"
nx=parse(Int64,rhs)
else
print(stderr,"unrecognized parameter '",lhs,"'.\n")
exit(-1)
end
end
end
## run command
# e(rho)
if command=="energy_rho"
energy_rho(order,a0,d,v,maxiter,tolerance)
# d^2(rho*e(rho))
elseif command=="convexity"
ddrhoe(order,a0,d,v,maxiter,tolerance)
# u_n(x)
elseif command=="iteration"
iteration_ux(order,e,a0,d,v,maxiter)
else
print(stderr,"unrecognized command '",command,"'.\n")
exit(-1)
end
end
# read cli arguments
function read_args(ARGS)
# flag
flag=""
# output strings
params=""
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"
else
print_usage()
exit(-1)
end
end
# arguments that do not start with a dash
else
if flag=="params"
params=arg
else
command=arg
end
# reset flag
flag=""
end
end
if command==""
print_usage()
exit(-1)
end
return (params,command)
end
# usage
function print_usage()
print(stderr,"usage: simpleq [-p params] <command>\n")
end
# exponential potential in 3 dimensions
function v_exp3d(k)
return 8*pi/(1+k^2)^2
end
a0_exp3d=1.254356410591064819505409291110046864031181245635821944528
# compute energy as a function of rho
function energy_rho(order,a0,d,v,maxiter,tolerance)
minlrho=-6
maxlrho=2
nlrho=100
for j in 0:nlrho-1
rho=10^(minlrho+(maxlrho-minlrho)*j/nlrho)
# linear spacing
#rho=10.0^minlrho+(10.0^maxlrho-10.0^minlrho)*j/nlrho
(u,E)=hatu_newton(order,rho,a0,d,v,maxiter,tolerance)
@printf("% .8e % .8e\n",rho,real(E))
end
end
# compute \partial^2(e\rho) as a function of rho
function ddrhoe(order,a0,d,v,maxiter,tolerance)
minlrho=-6
maxlrho=2
nlrho=100
for j in 0:nlrho-1
rho=10^(minlrho+(maxlrho-minlrho)*j/nlrho)
# interval
drho=rho*1.01
(u,E)=hatu_newton(order,rho,a0,d,v,maxiter,tolerance)
(up,Ep)=hatu_newton(order,rho+drho,a0,d,v,maxiter,tolerance)
(um,Em)=hatu_newton(order,rho-drho,a0,d,v,maxiter,tolerance)
@printf("% .8e % .8e\n",rho,real((rho+drho)*Ep+(rho-drho)*Em-2*rho*E)/drho^2)
end
end
# compute \int u_n(x) at every step
function iteration_ux(order,e,a0,d,v,maxiter)
(u,rho)=hatun_iteration(e,order,d,v,maxiter)
weights=gausslegendre(order)
intun=0.
for n in 2:maxiter+1
# compute \hat u_n(0)=1/(2*rho_n)+rho_{n-1}/2*\hat u_{n-1}(0)^2
intun=real(1/(2*rho[n])+rho[n-1]/2*intun^2)
@printf("%2d % .15e\n",n-1,abs(intun-1/rho[maxiter+1]))
end
end
main()
|