-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDEexample.jl
More file actions
388 lines (317 loc) · 10.8 KB
/
Copy pathSDEexample.jl
File metadata and controls
388 lines (317 loc) · 10.8 KB
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
@everywhere using DriftDiffusionPoissonSystems
@everywhere using Sobol
@everywhere include("LPcubature.jl")
@everywhere function eval_integrand(x,y,realization)
#(sum(realization)-length(realization)/2)*sin(x.^2).*sin(y.^2)
hcat(mean(exp(-vec(realization)*vec(x.^2+y.^2)'),1))
end
@everywhere function integral(x,y)
#(sum(realization)-length(realization)/2)*sin(x.^2).*sin(y.^2)
(1-exp(-(x.^2+y.^2)))./(x.^2+y.^2)
end
@everywhere function multisym_expectation(n,d)
m = 1
nodes_sym, weights_sym = return_cubature_formula_new(n,m,d)
A = [x->1;x->0;x->0;x->1]
bddata=[1 2 3 4;'D' 'D' 'D' 'D'; (x,y)->1 (x,y)->1 (x,y)->1 (x,y)->1]
mesh = read_mesh("mesh_s_p05.msh")
F = [(x,y,u)->eval_integrand(x,y,nodes_sym[i,:]) for i in 1:size(nodes_sym)[1]]
eval_tmp = pmap((j) -> solve_semlin_poisson(mesh,A,bddata,F[j],(x,y,u)->0),1:length(F))
sum(eval_tmp.*weights_sym)
end
@everywhere function qmc_expectation(n,N)
m = 1
S = SobolSeq(n)
p = hcat([next(S) for i = 1:N]...)'
A = [x->1;x->0;x->0;x->1]
bddata=[1 2 3 4;'D' 'D' 'D' 'D'; (x,y)->1 (x,y)->1 (x,y)->1 (x,y)->1]
mesh = read_mesh("mesh_s_p05.msh")
F = [(x,y,u)->eval_integrand(x,y,p[i,:]) for i in 1:N]
eval_tmp = pmap((j) -> solve_semlin_poisson(mesh,A,bddata,F[j],(x,y,u)->0),1:length(F))
mean(eval_tmp)
end
@everywhere function vmc_expectation(n,N)
m = 1
p = hcat([rand(n) for i = 1:N]...)'
A = [x->1;x->0;x->0;x->1]
bddata=[1 2 3 4;'D' 'D' 'D' 'D'; (x,y)->1 (x,y)->1 (x,y)->1 (x,y)->1]
mesh = read_mesh("mesh_s_p05.msh")
F = [(x,y,u)->eval_integrand(x,y,p[i,:]) for i in 1:N]
eval_tmp = pmap((j) -> solve_semlin_poisson(mesh,A,bddata,F[j],(x,y,u)->0),1:length(F))
mean(eval_tmp)
end
function exact()
A = [x->1;x->0;x->0;x->1]
bddata=[1 2 3 4;'D' 'D' 'D' 'D'; (x,y)->1 (x,y)->1 (x,y)->1 (x,y)->1]
mesh = read_mesh("mesh_s_p05.msh")
f = (x,y,u) -> (1-exp(x.^2+y.^2))./(x.^2+y.^2)
vec(solve_semlin_poisson(mesh,A,bddata,(x,y,u)->integral(x,y),(x,y,u)->0))
end
function test(n,d,N)
mesh = read_mesh("mesh_s_p05.msh")
mulsym = multisym_expectation(n,d)
qmc = qmc_expectation(n,N)
ex = exact()
calculate_norm(mesh,(ex-mulsym).^2), calculate_norm(mesh,(ex-qmc).^2)
end
function plotit(n,d,N)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
mulsym = multisym_expectation(n,d)
qmc = qmc_expectation(n,N)
ex = exact()
clf()
figure(1)
surf(X,Y,abs(vec(ex)-vec(mulsym)),cmap="jet")
figure(2)
surf(X,Y,abs(vec(ex)-vec(qmc)),cmap="jet")
sqrt(calculate_norm(mesh,(ex-mulsym).^2)), sqrt(calculate_norm(mesh,(ex-qmc).^2))
end
function calc_sym(n,d)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
mulsym = multisym_expectation(n,d)
ex = exact()
sqrt(calculate_norm(mesh,(ex-mulsym).^2)), mulsym
end
function calc_qmc(n,N)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
qmc = qmc_expectation(n,N)
ex = exact()
sqrt(calculate_norm(mesh,(ex-qmc).^2)), qmc
end
function calc_vmc(n,N)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
vmc = vmc_expectation(n,N)
ex = exact()
sqrt(calculate_norm(mesh,(ex-vmc).^2)), vmc
end
function calc_qmc_fromto(n,N1,N2,N)
m = 1
S = SobolSeq(n)
p = (hcat([next(S) for i = 1:N]...)')[N1:N2]
A = [x->1;x->0;x->0;x->1]
bddata=[1 2 3 4;'D' 'D' 'D' 'D'; (x,y)->1 (x,y)->1 (x,y)->1 (x,y)->1]
mesh = read_mesh("mesh_s_p05.msh")
F = [(x,y,u)->eval_integrand(x,y,p[i,:]) for i in 1:(N2-N1)]
eval_tmp = pmap((j) -> solve_semlin_poisson(mesh,A,bddata,F[j],(x,y,u)->0),1:length(F))
mean(eval_tmp)
end
function calculate_norm(mesh,u)
a1 = vec(mesh.elements[1,:])
a2 = vec(mesh.elements[2,:])
a3 = vec(mesh.elements[3,:])
q1 = mesh.nodes[:,a1]
q2 = mesh.nodes[:,a2]
q3 = mesh.nodes[:,a3]
uu = q2-q3
vv = q3-q1
ww = q1-q2
ar = (uu[1,:].*vv[2,:]-uu[2,:].*vv[1,:])./2
sol=((u[a1]+u[a2]+u[a3])./3)
sol2=ones(size(sol,1),1)
norm=sqrt(sum(abs(ar'.*sol.^2)))
return norm
end
#=
sym_sol = []; sym_err = [];
for d in [3:2:11;]
err, sol = calc_sym(15,d)
push!(sym_sol,sol)
push!(sym_err,err)
end
push!(sym_sol, vec(exact()))
qmc_err = []; qmc_sol = [];
for N in [10;100;1000;]
# for N in [10000;]
err, sol = calc_qmc(15,N)
push!(qmc_sol, sol)
push!(qmc_err, err)
end
push!(qmc_sol, vec(exact()))
=#
#=
sym_err = []; sym_sol = [];
for d in [3:2:11;]
err, sol = calc_sym(15,d)
push!(sym_sol,sol)
push!(sym_err,err)
end
push!(sym_sol, vec(exact()))
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
fig = figure()
for i in 1:2
for j in 1:3
ax = fig[:add_subplot](3,2,i+2*(j-1), projection = "3d")
Z = vec(sym_sol[i+2*(j-1)])
if i+2*(j-1) != 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(sym_sol[end]) - vec(sym_sol[i+2*(j-1)]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
end
savefig("sde")
=#
function plot_sol(sol, err, ex, name)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
fig = figure(name,figsize=(20,12),dpi=80,facecolor="w",edgecolor="k")
for i in 1:2
for j in 1:3
ax = fig[:add_subplot](3,2,i+2*(j-1), projection = "3d")
Z = vec(ex)
if i+2*(j-1) != 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(ex) - vec(sol[i+2*(j-1)]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
end
savefig(name)
end
function create_plots(n)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
sym_err = []; sym_sol = [];
dd = [3:2:11;]
for d in dd
tmp_err, tmp_sol = calc_sym(n,d)
push!(sym_sol,tmp_sol)
push!(sym_err,tmp_err)
end
ex = exact()
sol = sym_sol
fig = figure("Sym",figsize=(20,12),dpi=80,facecolor="w",edgecolor="k")
for i in 1:2
for j in 1:3
ax = fig[:add_subplot](3,2,i+2*(j-1), projection = "3d")
Z = vec(ex)
if i+2*(j-1) != 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(ex) - vec(sol[i+2*(j-1)]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
d = convert(Int64,dd[i+2*(j-1)])
title("d = $d")
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
title("Exact solution")
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
end
savefig("sym.svg")
qmc_err = []; qmc_sol = [];
NN = [10;100;200;500;1000]
for N in NN
tmp_err, tmp_sol = calc_qmc(n,N)
push!(qmc_sol, tmp_sol)
push!(qmc_err, tmp_err)
end
sol = qmc_sol
fig = figure("qMC",figsize=(20,12),dpi=80,facecolor="w",edgecolor="k")
for i in 1:2
for j in 1:3
ax = fig[:add_subplot](3,2,i+2*(j-1), projection = "3d")
Z = vec(ex)
if i+2*(j-1) != 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(ex) - vec(sol[i+2*(j-1)]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
N = convert(Int64,NN[i+2*(j-1)])
title("N = $N")
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
title("Exact solution")
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
end
savefig("qmc.svg")
writedlm("qmc_sol.txt",qmc_sol)
writedlm("qmc_err.txt",qmc_err)
writedlm("sym_sol.txt",sym_sol)
writedlm("sym_err.txt",sym_err)
end
function plotdaplots(fig_x,fig_y)
mesh = read_mesh("mesh_s_p05.msh")
X = vec(mesh.nodes[1,:]); Y = vec(mesh.nodes[2,:])
ex = exact()
NN = [10;100;200;500;1000]
dd = [3:2:11;]
sol = readdlm("sym_sol.txt")
fig = figure("Sym",figsize=(fig_x,fig_y),dpi=80,facecolor="w",edgecolor="k")
for i in 1:6
ax = fig[:add_subplot](6,1,i, projection = "3d")
Z = vec(ex)
if i!= 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(ex) - vec(sol[i,:]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
d = convert(Int64,dd[i])
title("d = $d")
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
title("Exact expectation")
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
savefig("fsc_sde_sym.pdf")
sol = readdlm("qmc_sol.txt")
fig = figure("qMC",figsize=(fig_x,fig_y),dpi=80,facecolor="w",edgecolor="k")
for i in 1:6
ax = fig[:add_subplot](6,1,i, projection = "3d")
Z = vec(ex)
if i != 6
f = matplotlib[:ticker][:FormatStrFormatter]("%1.1f") # Define format of tick labels
ax[:zaxis][:set_major_formatter](f) # Set format of tick labels
tmp = abs(vec(ex) - vec(sol[i,:]))
exponent = floor(Int64,maximum(log10(tmp)))
Z = tmp*10^(-exponent)
Mx = matplotlib[:ticker][:MultipleLocator](ceil(maximum(Z)/3)) # Define interval of major ticks
ax[:zaxis][:set_major_locator](Mx) # Set interval of major ticks
N = convert(Int64,NN[i])
title("N = $N")
zlabel("e-$(exponent)",rotation = 90)
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
else
title("Exact expectation")
ax[:plot_trisurf](X,Y,Z,cmap=ColorMap("jet"), alpha=0.7, linewidth=0.25)
end
end
savefig("fsc_sde_qmc.pdf")
end