Skip to content

Commit da4bbe3

Browse files
committed
add script to plot solutions
1 parent 1bc56c9 commit da4bbe3

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

scripts/pseudo-spectrum/figure_13-16.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
plt.semilogy(range(1,maxiter+1), defect_l2[0,:], 'bo-', markersize=ms, label=r'$|| e^k ||$')
135135
plt.semilogy(range(1,5), [E_norm**(val-1)*2.0*defect_l2[0,0] for val in range(1,5)], 'b-.', label=r'$|| E ||_2^k$', linewidth=2)
136136
plt.semilogy(range(1,5), [psr**(val-1)*2.0*defect_l2[0,0] for val in range(1,5)], 'r--', label=r'$\sigma_{\epsilon}(E)^k$', linewidth=2)
137-
if figure==15:
137+
if figure==14 or figure==15:
138138
plt.legend(loc='lower right', fontsize=fs, prop={'size':fs-2}, handlelength=3)
139139
else:
140140
plt.legend(loc='upper right', fontsize=fs, prop={'size':fs-2}, handlelength=3)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 integrator_dedalus import integrator_dedalus
11+
12+
from solution_dedalus import solution_dedalus
13+
from solution_linear import solution_linear
14+
15+
from get_matrix import get_upwind, get_centered, get_diffusion
16+
from impeuler import impeuler
17+
from intexact import intexact
18+
from trapezoidal import trapezoidal
19+
20+
from pseudo_spectral_radius import pseudo_spectral_radius
21+
from parameter import parameter
22+
23+
from pylab import rcParams
24+
import matplotlib.pyplot as plt
25+
from subprocess import call
26+
27+
try:
28+
figure = int(sys.argv[1]) # 1 generates figure_1, 2 generates figure_2
29+
except:
30+
print("No or wrong command line argument provided, creating figure 13. Use 13, 14, 15 or 16 as command line argument.")
31+
figure = 5
32+
assert -4<= figure <= -1, "Figure should be -4, -3, -2 or -1"
33+
34+
if figure==-4 or figure==-3:
35+
par = parameter(dedalus = False)
36+
ndof_c = 24
37+
elif figure==-2:
38+
par = parameter(dedalus = True)
39+
ndof_c = 24
40+
elif figure==-1:
41+
par = parameter(dedalus = True)
42+
ndof_c = 30
43+
else:
44+
sys.exit("This should have been caught above")
45+
46+
Tend, nslices, maxiter, nfine, ncoarse, tol, epsilon, ndof_f = par.getpar()
47+
48+
if figure==-4:
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+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
52+
dx_c = xaxis_c[1] - xaxis_c[0]
53+
A_f = get_upwind(ndof_f, dx_f)
54+
A_c = get_upwind(ndof_c, dx_c)
55+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
56+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
57+
para = parareal(0.0, Tend, nslices, impeuler, impeuler, nfine, ncoarse, tol, maxiter, u0fine, u0coarse)
58+
filename = 'figure_minus4.pdf'
59+
mylabel = 'Conf. A'
60+
D = A_f*A_f.H - A_f.H*A_f
61+
print("Normality number of the system matrix (this should be zero): %5.3f" % np.linalg.norm(D.todense()))
62+
elif figure==-3:
63+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
64+
dx_f = xaxis_f[1] - xaxis_f[0]
65+
xaxis_c = np.linspace(0.0, 1.0, ndof_c+1)[0:ndof_c]
66+
dx_c = xaxis_c[1] - xaxis_c[0]
67+
A_f = get_centered(ndof_f, dx_f)
68+
A_c = get_centered(ndof_c, dx_c)
69+
u0fine = solution_linear(np.zeros(ndof_f), A_f)
70+
u0coarse = solution_linear(np.zeros(ndof_c), A_c)
71+
para = parareal(0.0, Tend, nslices, trapezoidal, trapezoidal, nfine, ncoarse, tol, maxiter, u0fine, u0coarse)
72+
filename = 'figure_minus3.pdf'
73+
mylabel = 'Conf. B'
74+
D = A_f*A_f.H - A_f.H*A_f
75+
print("Normality number of the system matrix (this should be zero): %5.3f" % np.linalg.norm(D.todense()))
76+
elif figure==-2 or figure==-1:
77+
xaxis_f = np.linspace(0.0, 1.0, ndof_f+1)[0:ndof_f]
78+
dx_f = xaxis_f[1] - xaxis_f[0]
79+
u0fine = solution_dedalus(np.zeros(ndof_f), ndof_f)
80+
u0coarse = solution_dedalus(np.zeros(ndof_c), ndof_c)
81+
para = parareal(0.0, Tend, nslices, integrator_dedalus, integrator_dedalus, nfine, ncoarse, tol, maxiter, u0fine, u0coarse)
82+
if figure==-2:
83+
filename = 'figure_minus2.pdf'
84+
mylabel = 'Conf. C'
85+
elif figure==-1:
86+
filename = 'figure_minus1.pdf'
87+
mylabel = 'Conf. D'
88+
else:
89+
sys.exit("Wrong value for figure")
90+
91+
Pmat, Bmat = para.get_parareal_matrix()
92+
Fmat = para.timemesh.get_fine_matrix(u0fine)
93+
bvec = np.zeros((ndof_f*(nslices+1),1))
94+
bvec[0:ndof_f,:] = np.reshape(np.sin(2.0*np.pi*xaxis_f), (ndof_f, 1))
95+
u_fine = linalg.spsolve(Fmat, bvec)
96+
y = Bmat@bvec
97+
for k in range(maxiter):
98+
y = Pmat@y + Bmat@bvec
99+
100+
yend = y[ndof_f*nslices:]
101+
102+
rcParams['figure.figsize'] = 2.5, 2.5
103+
fs = 8
104+
ms = 4
105+
fig = plt.figure(1)
106+
plt.plot(xaxis_f, yend, 'b', label=mylabel)
107+
plt.plot(xaxis_f, np.sin(2.0*np.pi*(xaxis_f-Tend)), 'r--', label='Exact')
108+
plt.legend()
109+
plt.xlabel('x')
110+
plt.ylabel('u')
111+
plt.gcf().savefig(filename, bbox_inches='tight')
112+
call(["pdfcrop", filename, filename])
113+
plt.show()
114+
plt.show()

0 commit comments

Comments
 (0)