Skip to content

Commit f024efb

Browse files
author
Daniel Ruprecht
committed
script to plot plane wave in time generated by parareal, fine and coarse method
1 parent 9d4d027 commit f024efb

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

scripts/plot_updates.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sys
2+
sys.path.append('../src')
3+
4+
from parareal import parareal
5+
from impeuler import impeuler
6+
from intexact import intexact
7+
from solution_linear import solution_linear
8+
import numpy as np
9+
from pylab import rcParams
10+
import matplotlib.pyplot as plt
11+
from subprocess import call
12+
import time
13+
14+
if __name__ == "__main__":
15+
16+
Nx = 200
17+
x = np.linspace(0,20,Nx+1,endpoint=False)
18+
x = x[0:Nx]
19+
20+
Nk = 4
21+
k_vec = np.linspace(0, np.pi, Nk+1, endpoint=False)
22+
k_vec = k_vec[1:]
23+
# Select a wave number
24+
k_ind = 3
25+
k = k_vec[k_ind]
26+
27+
Tend = 16.0
28+
nslices = 16
29+
U_speed = 1.0
30+
nu = 0.0
31+
ncoarse = 1
32+
nfine = 1
33+
34+
symb = -(1j*U_speed*k + nu*k**2)
35+
u0_val = np.array([[1.0]], dtype='complex')
36+
u0 = solution_linear(u0_val, np.array([[symb]],dtype='complex'))
37+
para = parareal(0.0, Tend, nslices, intexact, impeuler, nfine, ncoarse, 0.0, 0, u0)
38+
39+
stab_coarse = para.timemesh.slices[0].get_coarse_update_matrix(u0)
40+
stab_coarse = stab_coarse**nslices
41+
stab_ex = np.exp(-1j*U_speed*k*Tend)*np.exp(-nu*k**2*Tend)
42+
43+
y_start = np.exp(1j*k*x)
44+
y_coarse = (stab_coarse[0,0]*y_start).real
45+
y_ex = (stab_ex*y_start).real
46+
47+
fs = 8
48+
# rcParams['figure.figsize'] = 2.5, 2.5
49+
rcParams['figure.figsize'] = 7.5, 7.5
50+
51+
fig = plt.figure()
52+
y_old = 0.0*x
53+
for k in range(3,4):
54+
stab_para = para.get_parareal_stab_function(k)
55+
y_new = (stab_para[0,0]*y_start).real
56+
update = y_new - y_old
57+
plt.plot(x, update, 'b')
58+
plt.plot(x, y_ex, 'r')
59+
plt.title(('k = %2i' % k), fontsize=fs)
60+
plt.xlim([x[0], x[-1]])
61+
#plt.ylim([-1, 1])
62+
plt.show()
63+
#plt.show(block=False)
64+
#plt.pause(1)
65+
#fig.clear()
66+
y_old = y_new

scripts/plot_wave_time.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import sys
2+
sys.path.append('../src')
3+
4+
from parareal import parareal
5+
from impeuler import impeuler
6+
from intexact import intexact
7+
from trapezoidal import trapezoidal
8+
from special_integrator import special_integrator
9+
from solution_linear import solution_linear
10+
import numpy as np
11+
import scipy.sparse as sparse
12+
import math
13+
14+
from pylab import rcParams
15+
import matplotlib.pyplot as plt
16+
from matplotlib.patches import Polygon
17+
from subprocess import call
18+
import sympy
19+
from pylab import rcParams
20+
21+
def solve_omega(R):
22+
return 1j*( np.log(abs(R)) + 1j*np.angle(R) )
23+
24+
def findroots(R, n):
25+
assert abs(n - float(int(n)))<1e-14, "n must be an integer or a float equal to an integer"
26+
p = np.zeros(int(n)+1, dtype='complex')
27+
p[-1] = -R
28+
p[0] = 1.0
29+
return np.roots(p)
30+
31+
def normalise(R, T, target):
32+
roots = findroots(R, T)
33+
for x in roots:
34+
assert abs(x**T-R)<1e-5, ("Element in roots not a proper root: err=%5.3e" % abs(x**T-R))
35+
minind = np.argmin(abs(np.angle(roots) - target))
36+
return roots[minind]
37+
38+
if __name__ == "__main__":
39+
40+
41+
Tend = 16.0
42+
nslices = int(Tend) # Make sure each time slice has length 1
43+
U_speed = 1.0
44+
nu = 0.0
45+
ncoarse = 5
46+
nfine = 10
47+
taxis = np.linspace(0.0, Tend, nfine*nslices)
48+
niter_v = [3]
49+
50+
k_vec = np.linspace(0.0, np.pi, 6, endpoint=False)
51+
k_vec = k_vec[1:]
52+
waveno = k_vec[-1]
53+
54+
symb = -(1j*U_speed*waveno + nu*waveno**2)
55+
symb_coarse = symb
56+
# symb_coarse = -(1.0/dx)*(1.0 - np.exp(-1j*waveno*dx))
57+
58+
# Solution objects define the problem
59+
u0_val = np.array([[1.0]], dtype='complex')
60+
u0 = solution_linear(u0_val, np.array([[symb]],dtype='complex'))
61+
ucoarse = solution_linear(u0_val, np.array([[symb_coarse]],dtype='complex'))
62+
63+
para = parareal(0.0, Tend, nslices, intexact, trapezoidal, nfine, ncoarse, 0.0, niter_v[0], u0)
64+
65+
# get update matrix for imp Euler over one slice
66+
stab_fine = para.timemesh.slices[0].get_fine_update_matrix(u0)
67+
stab_coarse = para.timemesh.slices[0].get_coarse_update_matrix(ucoarse)
68+
stab_ex = np.exp(symb)
69+
70+
rcParams['figure.figsize'] = 7.5, 7.5
71+
fs = 8
72+
fig = plt.figure()
73+
74+
for k in range(0,nslices):
75+
# for k in range(0,2):
76+
plt.clf()
77+
78+
stab_para_n0 = para.get_parareal_stab_function(k, ucoarse=ucoarse)
79+
stab_para_np1 = para.get_parareal_stab_function(k+1, ucoarse=ucoarse)
80+
81+
stab_para_norm_n0 = normalise(stab_para_n0[0,0], Tend, np.angle(stab_ex))
82+
stab_para_norm_np1 = normalise(stab_para_np1[0,0], Tend, np.angle(stab_ex))
83+
84+
sol_fine = solve_omega(stab_fine[0,0])
85+
sol_ex = solve_omega(stab_ex)
86+
sol_coarse = solve_omega(stab_coarse[0,0])
87+
sol_para_n0 = solve_omega(stab_para_norm_n0)
88+
sol_para_np1 = solve_omega(stab_para_norm_np1)
89+
90+
y_fine = np.exp(-1j*sol_fine*taxis)
91+
y_ex = np.exp(-1j*sol_ex*taxis)
92+
y_coarse = np.exp(-1j*sol_coarse*taxis)
93+
y_para_n0 = np.exp(-1j*sol_para_n0*taxis)
94+
y_para_np1 = np.exp(-1j*sol_para_np1*taxis)
95+
96+
update = y_para_np1 - y_para_n0
97+
98+
99+
plt.plot(taxis, y_fine.real, 'b')
100+
# plt.plot(taxis, y_ex.real, 'g')
101+
plt.plot(taxis, y_para_n0.real, 'k')
102+
# plt.plot(taxis, (y_fine - y_para_n0).real, 'k')
103+
#plt.plot(taxis, (y_fine - y_coarse).real, 'r')
104+
# plt.plot(taxis, update.real, 'r')
105+
plt.ylim([-2, 2])
106+
if k<nslices-1:
107+
plt.show(block=False)
108+
else:
109+
plt.show()
110+
plt.pause(2)
111+

0 commit comments

Comments
 (0)