Skip to content

Commit 1bc56c9

Browse files
committed
add script to plot || E || - | exp(lambda_m+1*dt | in loglog plot
1 parent 5a1f55b commit 1bc56c9

File tree

3 files changed

+188
-15
lines changed

3 files changed

+188
-15
lines changed

scripts/pseudo-spectrum/figure_13-16.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from solution_dedalus import solution_dedalus
1313
from solution_linear import solution_linear
1414

15-
from get_matrix import get_upwind, get_centered
15+
from get_matrix import get_upwind, get_centered, get_diffusion
1616
from impeuler import impeuler
1717
from intexact import intexact
1818
from trapezoidal import trapezoidal
@@ -29,9 +29,9 @@
2929
except:
3030
print("No or wrong command line argument provided, creating figure 13. Use 13, 14, 15 or 16 as command line argument.")
3131
figure = 5
32-
assert 13<= figure <= 16, "Figure should be 13, 14, 15 or 16"
32+
assert 13<= figure <= 16 or figure==0, "Figure should be 13, 14, 15 or 16"
3333

34-
if figure==13 or figure==14:
34+
if figure==13 or figure==14 or figure==0:
3535
par = parameter(dedalus = False)
3636
ndof_c = 24
3737
elif figure==15:
@@ -46,9 +46,9 @@
4646
Tend, nslices, maxiter, nfine, ncoarse, tol, epsilon, ndof_f = par.getpar()
4747

4848
if figure==13:
49-
xaxis_f = np.linspace(0.0, 2.0, ndof_f+1)[0:ndof_f]
49+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
5050
dx_f = xaxis_f[1] - xaxis_f[0]
51-
xaxis_c = np.linspace(0.0, 2.0, ndof_c+1)[0:ndof_c]
51+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
5252
dx_c = xaxis_c[1] - xaxis_c[0]
5353
A_f = get_upwind(ndof_f, dx_f)
5454
A_c = get_upwind(ndof_c, dx_c)
@@ -59,9 +59,9 @@
5959
D = A_f*A_f.H - A_f.H*A_f
6060
print("Normality number of the system matrix (this should be zero): %5.3f" % np.linalg.norm(D.todense()))
6161
elif figure==14:
62-
xaxis_f = np.linspace(0.0, 2.0, ndof_f+1)[0:ndof_f]
62+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
6363
dx_f = xaxis_f[1] - xaxis_f[0]
64-
xaxis_c = np.linspace(0.0, 2.0, ndof_c+1)[0:ndof_c]
64+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
6565
dx_c = xaxis_c[1] - xaxis_c[0]
6666
A_f = get_centered(ndof_f, dx_f)
6767
A_c = get_centered(ndof_c, dx_c)
@@ -78,7 +78,20 @@
7878
if figure==15:
7979
filename = 'figure_15.pdf'
8080
elif figure==16:
81-
filename = 'figure_16.pdf'
81+
filename = 'figure_16.pdf'
82+
elif figure==0:
83+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
84+
dx_f = xaxis_f[1] - xaxis_f[0]
85+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
86+
dx_c = xaxis_c[1] - xaxis_c[0]
87+
A_f = get_diffusion(ndof_f, dx_f)
88+
A_c = get_diffusion(ndof_c, dx_c)
89+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
90+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
91+
para = parareal(0.0, Tend, nslices, impeuler, impeuler, nfine, ncoarse, tol, maxiter, u0fine, u0coarse)
92+
filename = 'figure_00.pdf'
93+
D = A_f*A_f.H - A_f.H*A_f
94+
print("Normality number of the system matrix (this should be zero): %5.3f" % np.linalg.norm(D.todense()))
8295
else:
8396
sys.exit("Wrong value for figure")
8497

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import sys
2+
sys.path.append('../../src')
3+
import numpy as np
4+
from numpy import linalg as LA
5+
import scipy.sparse as sp
6+
import scipy.linalg as spla
7+
import scipy.sparse.linalg as linalg
8+
9+
from parareal import parareal
10+
from impeuler import impeuler
11+
from intexact import intexact
12+
from integrator_dedalus import integrator_dedalus
13+
from trapezoidal import trapezoidal
14+
from solution_linear import solution_linear
15+
from solution_dedalus import solution_dedalus
16+
from get_matrix import get_upwind, get_centered, get_diffusion
17+
from parameter import parameter
18+
19+
from pylab import rcParams
20+
import matplotlib.pyplot as plt
21+
from subprocess import call
22+
23+
def ie(z):
24+
return 1.0/(1.0 - z)
25+
26+
def trap(z):
27+
return (1.0 + 0.5*z)/(1.0 - 0.5*z)
28+
29+
try:
30+
figure = int(sys.argv[1]) # 1 generates figure_1, 2 generates figure_2
31+
except:
32+
print("No or wrong command line argument provided, creating figure 5. Use 5, 6, 7 or 8 as command line argument.")
33+
figure = 5
34+
assert 5<= figure <= 8, "Figure should be 5, 6, 7 or 8"
35+
36+
if figure==5 or figure==6:
37+
par = parameter(dedalus = False)
38+
elif figure==7 or figure==8:
39+
par = parameter(dedalus = True)
40+
else:
41+
sys.exit("This should have been caught above")
42+
43+
Tend, nslices, maxiter, nfine, ncoarse, tol, epsilon, ndof_f = par.getpar()
44+
45+
nsteps = [1, 2, 4, 8, 12, 16, 20]
46+
nsteps = [1, 10, 50, 100, 1000, 10000]
47+
48+
ndof_c_v = [16, 24, 30]
49+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
50+
dx_f = xaxis_f[1] - xaxis_f[0]
51+
52+
if figure==5:
53+
A_f = get_upwind(ndof_f, dx_f)
54+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
55+
elif figure==6 or figure==7:
56+
A_f = get_centered(ndof_f, dx_f)
57+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
58+
elif figure==8:
59+
u0fine = solution_dedalus(np.zeros(ndof_f), ndof_f)
60+
else:
61+
sys.exit("This should have been caught above")
62+
63+
norm_l2 = np.zeros((3,np.size(nsteps)))
64+
norm_inf = np.zeros((3,np.size(nsteps)))
65+
dt_f_v = np.zeros((1,np.size(nsteps)))
66+
dt_c_v = np.zeros((1,np.size(nsteps)))
67+
A_f_eig = np.zeros((3,np.size(nsteps)), dtype='complex')
68+
69+
for nn in range(3):
70+
71+
ndof_c = ndof_c_v[nn]
72+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
73+
dx_c = xaxis_c[1] - xaxis_c[0]
74+
75+
### Eigenvalues of A_f
76+
eig_val, eig_vec = LA.eig(A_f.todense())
77+
78+
if figure==5:
79+
A_c = get_upwind(ndof_c, dx_c)
80+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
81+
filename = 'figure_5.pdf'
82+
elif figure==6:
83+
A_c = get_centered(ndof_c, dx_c)
84+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
85+
filename = 'figure_6.pdf'
86+
elif figure==7:
87+
A_c = get_centered(ndof_c, dx_c)
88+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
89+
filename = 'figure_7.pdf'
90+
elif figure==8:
91+
u0coarse = solution_dedalus(np.zeros(ndof_c), ndof_c)
92+
filename = 'figure_8.pdf'
93+
else:
94+
sys.exit("Value of figure should be")
95+
96+
for mm in range(np.size(nsteps)):
97+
98+
if figure==5 or figure==7:
99+
Rz = np.vectorize(ie)
100+
para = parareal(0.0, Tend, nslices, impeuler, impeuler, nsteps[mm], nsteps[mm], tol, maxiter, u0fine, u0coarse)
101+
elif figure==6:
102+
Rz = np.vectorize(trap)
103+
para = parareal(0.0, Tend, nslices, trapezoidal, trapezoidal, nsteps[mm], nsteps[mm], tol, maxiter, u0fine, u0coarse)
104+
elif figure==8:
105+
para = parareal(0.0, Tend, nslices, integrator_dedalus, integrator_dedalus, nsteps[mm], nsteps[mm], tol, maxiter, u0fine, u0coarse)
106+
else:
107+
sys.exit("Value of figure should be")
108+
Pmat, Bmat = para.get_parareal_matrix()
109+
dt_f_v[0,mm] = para.timemesh.slices[0].int_fine.dt
110+
dt_c_v[0,mm] = para.timemesh.slices[0].int_coarse.dt
111+
112+
# Sort according to absolute values of R(z) with z = lambda*dt_f
113+
sort_index = np.argsort(np.abs(Rz(eig_val*dt_f_v[0,mm])))
114+
115+
# Store eigenvalue ndof_c+1. first "truncated" EV
116+
A_f_eig[nn,mm] = np.flip((eig_val[sort_index]))[ndof_c+1]
117+
118+
### Parareal iteration: y^k+1 = Pmat*y^k + Bmat*b
119+
norm_l2[nn,mm] = np.linalg.norm(Pmat.todense(), 2)
120+
121+
122+
rcParams['figure.figsize'] = 2.5, 2.5
123+
fs = 8
124+
ms = 4
125+
fig = plt.figure(1)
126+
#plt.plot(dt_f_v[0,:], norm_l2[0,:], 'bo-', label='m='+str(ndof_c_v[0]), markersize=ms)
127+
#plt.plot(dt_f_v[0,:], norm_l2[1,:], 'rx-', label='m='+str(ndof_c_v[1]), markersize=ms)
128+
#plt.plot(dt_f_v[0,:], norm_l2[2,:], 'cd-', label='m='+str(ndof_c_v[2]), markersize=ms)
129+
#plt.loglog(dt_f_v[0,:], norm_l2[0,:]-np.abs(np.exp(np.multiply(A_f_eig[0,:],dt_f_v[0,:]))), 'bo-', label='m='+str(ndof_c_v[0]), markersize=ms)
130+
#plt.loglog(dt_f_v[0,:], norm_l2[1,:]-np.abs(np.exp(np.multiply(A_f_eig[1,:],dt_f_v[0,:]))), 'rx-', label='m='+str(ndof_c_v[1]), markersize=ms)
131+
print(norm_l2[1,:])
132+
print(A_f_eig[1,:])
133+
print("\n")
134+
print(norm_l2[2,:])
135+
print(A_f_eig[2,:])
136+
137+
plt.loglog(dt_f_v[0,:], norm_l2[2,:]-0.0*np.abs(np.exp(np.multiply(A_f_eig[2,:],dt_f_v[0,:]))), 'cd-', label='m='+str(ndof_c_v[2]), markersize=ms)
138+
#plt.plot(dt_f_v[0,:], 1.0 + 0.0*dt_f_v[0,:], 'k:')
139+
plt.legend(loc='best', bbox_to_anchor=(0.5, 0.5), fontsize=fs, prop={'size':fs-2}, handlelength=3)
140+
plt.xlabel(r'$\delta t = \Delta t$', fontsize=fs)
141+
#plt.ylabel(r'$|| \mathbf{E} ||_2$', fontsize=fs)
142+
plt.ylabel(r'$|| \mathbf{E} ||_2 - | \exp(\lambda_{m+1} \delta t) |$', fontsize=fs)
143+
144+
plt.xlim([0.0, dt_f_v[0,0]])
145+
plt.ylim([1e-2, 1e1])
146+
#plt.xlabel([0, maxiter])
147+
plt.gcf().savefig(filename, bbox_inches='tight')
148+
call(["pdfcrop", filename, filename])
149+
plt.show()

scripts/pseudo-spectrum/figure_9-12.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
except:
2828
print("No or wrong command line argument provided, creating figure 9. Use 9, 10, 11 or 12 as command line argument.")
2929
figure = 5
30-
assert 9<= figure <= 12, "Figure should be 9, 10, 11 or 12"
30+
assert (9<= figure <= 12) or figure==0, "Figure should be 9, 10, 11 or 12"
3131

32-
if figure==9 or figure==10:
32+
if figure==9 or figure==10 or figure==0:
3333
par = parameter(dedalus = False)
3434
ndof_c = 24
3535
elif figure==11:
@@ -44,9 +44,9 @@
4444
Tend, nslices, maxiter, nfine, ncoarse, tol, epsilon, ndof_f = par.getpar()
4545

4646
if figure==9:
47-
xaxis_f = np.linspace(0.0, 2.0, ndof_f+1)[0:ndof_f]
47+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
4848
dx_f = xaxis_f[1] - xaxis_f[0]
49-
xaxis_c = np.linspace(0.0, 2.0, ndof_c+1)[0:ndof_c]
49+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
5050
dx_c = xaxis_c[1] - xaxis_c[0]
5151
A_f = get_upwind(ndof_f, dx_f)
5252
A_c = get_upwind(ndof_c, dx_c)
@@ -57,9 +57,9 @@
5757
D = A_f*A_f.H - A_f.H*A_f
5858
print("Normality number of the system matrix (this should be zero): %5.3f" % np.linalg.norm(D.todense()))
5959
elif figure==10:
60-
xaxis_f = np.linspace(0.0, 2.0, ndof_f+1)[0:ndof_f]
60+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
6161
dx_f = xaxis_f[1] - xaxis_f[0]
62-
xaxis_c = np.linspace(0.0, 2.0, ndof_c+1)[0:ndof_c]
62+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
6363
dx_c = xaxis_c[1] - xaxis_c[0]
6464
A_f = get_centered(ndof_f, dx_f)
6565
A_c = get_centered(ndof_c, dx_c)
@@ -76,7 +76,18 @@
7676
if figure==11:
7777
filename = 'figure_11.pdf'
7878
elif figure==12:
79-
filename = 'figure_12.pdf'
79+
filename = 'figure_12.pdf'
80+
elif figure==0:
81+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
82+
dx_f = xaxis_f[1] - xaxis_f[0]
83+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
84+
dx_c = xaxis_c[1] - xaxis_c[0]
85+
A_f = get_diffusion(ndof_f, dx_f)
86+
A_c = get_diffusion(ndof_c, dx_c)
87+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
88+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
89+
para = parareal(0.0, Tend, nslices, impeuler, impeuler, nfine, ncoarse, tol, maxiter, u0fine, u0coarse)
90+
filename = 'figure_00.pdf'
8091
else:
8192
sys.exit("Wrong value for figure")
8293

0 commit comments

Comments
 (0)