Skip to content

Commit 0af9fbc

Browse files
author
Daniel Ruprecht
committed
new function to run bdf2, implicit euler and trapezoidal rule for comparison; fixed wave length in fwsw sdc initial data
1 parent d5e5260 commit 0af9fbc

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

examples/acoustic_1d_imex/ProblemClass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def u_exact(self,t):
161161
"""
162162

163163
sigma_0 = 0.1
164-
k = 7.2*np.pi
164+
k = 7.0*2.0*np.pi
165165
x_0 = 0.75
166166
x_1 = 0.25
167167

examples/acoustic_1d_imex/playground.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from pySDC.Stats import grep_stats, sort_stats
1414

1515
# Sharpclaw imports
16-
from clawpack import pyclaw
17-
from clawpack import riemann
16+
#from clawpack import pyclaw
17+
#from clawpack import riemann
1818
from matplotlib import pyplot as plt
1919

2020

@@ -40,7 +40,7 @@
4040
# This comes as read-in for the problem class
4141
pparams = {}
4242
pparams['nvars'] = [(2,512)]
43-
pparams['cadv'] = 0.0
43+
pparams['cadv'] = 0.1
4444
pparams['cs'] = 1.0
4545
pparams['order_adv'] = 5
4646

@@ -83,13 +83,14 @@
8383
x_0 = 0.75
8484

8585
#plt.plot(P.mesh, uex.values[0,:], '+', color='b', label='u (exact)')
86-
#plt.plot(P.mesh, uend.values[0,:], '-', color='b', label='u (SDC)')
86+
plt.plot(P.mesh, uend.values[1,:], '-', color='b', label='SDC')
8787
#plt.plot(P.mesh, uex.values[1,:], '+', color='r', label='p (exact)')
88-
plt.plot(P.mesh, uend.values[1,:], '-', color='b', linewidth=2.0, label='p (SDC)')
88+
#plt.plot(P.mesh, uend.values[1,:], '-', color='b', linewidth=2.0, label='p (SDC)')
8989
p_slow = np.exp(-np.square(P.mesh-x_0)/(sigma_0*sigma_0))
90-
plt.plot(P.mesh, p_slow, '+', color='r', markersize=4, label='slow mode')
90+
#plt.plot(P.mesh, p_slow, '-', color='r', markersize=4, label='slow mode')
9191
plt.legend(loc=2)
9292
plt.xlim([0, 1])
9393
plt.ylim([-0.1, 1.1])
94+
fig.gca().grid()
9495
#plt.show()
9596
plt.gcf().savefig('fwsw-sdc-K'+str(sparams['maxiter'])+'-M'+str(description['num_nodes'])+'.pdf', bbox_inches='tight')
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import numpy as np
2+
import scipy.sparse as sp
3+
import scipy.sparse.linalg as LA
4+
from matplotlib import pyplot as plt
5+
from buildWave1DMatrix import getWave1DMatrix, getWave1DAdvectionMatrix
6+
7+
sigma_0 = 0.1
8+
k = 7.0*2*np.pi
9+
x_0 = 0.75
10+
x_1 = 0.25
11+
multi_scale = 1.0
12+
13+
nvars = 512
14+
cs = 1.0
15+
cadv = 0.0
16+
order = 4
17+
18+
def u(x,t):
19+
u0 = np.exp(-np.square( np.mod( mesh- cs*t, 1.0 ) -x_0 )/(sigma_0*sigma_0)) + multi_scale*np.exp(-np.square( np.mod( mesh -cs*t, 1.0 ) -x_1 )/(sigma_0*sigma_0))*np.cos(k*( np.mod( mesh-cs*t, 1.0 ))/sigma_0)
20+
p0 = u0
21+
return u0, p0
22+
23+
Tend = 3.0
24+
Nsteps = 154
25+
dt = Tend/float(Nsteps)
26+
27+
mesh = np.linspace(0.0, 1.0, nvars, endpoint=False)
28+
dx = mesh[1] - mesh[0]
29+
Dx = -cadv*getWave1DAdvectionMatrix(nvars, dx, order)
30+
Id, A = getWave1DMatrix(nvars, dx, ['periodic','periodic'], ['periodic','periodic'])
31+
A = -cs*A
32+
33+
M_ieuler = Id - dt*(A + Dx)
34+
35+
M_bdf = Id - (2.0/3.0)*dt*(A + Dx)
36+
37+
alpha = 0.5
38+
M_trap = Id - alpha*dt*(A+Dx)
39+
B_trap = Id + (1-alpha)*dt*(A+Dx)
40+
41+
u0, p0 = u(mesh, 0.0)
42+
y0_ie = np.concatenate( (u0, p0) )
43+
y0_tp = y0_ie
44+
45+
y0_bdf = y0_ie
46+
47+
fig = plt.figure(figsize=(8,8))
48+
49+
for i in range(0,Nsteps):
50+
51+
# implicit Euler step
52+
ynew_ie = LA.spsolve(M_ieuler, y0_ie)
53+
54+
# trapezoidal rule step
55+
b_tp = B_trap.dot(y0_tp)
56+
ynew_tp = LA.spsolve(M_trap, b_tp)
57+
58+
# BDF-2 scheme
59+
if i==0:
60+
ynew_bdf = LA.spsolve(M_ieuler, y0_bdf)
61+
else:
62+
b_bdf = (4.0/3.0)*y0_bdf - (1.0/3.0)*ym1_bdf
63+
ynew_bdf = LA.spsolve(M_bdf, b_bdf)
64+
65+
unew_ie, pnew_ie = np.split(ynew_ie, 2)
66+
unew_tp, pnew_tp = np.split(ynew_tp, 2)
67+
unew_bdf, pnew_bdf = np.split(ynew_bdf, 2)
68+
uex, pex = u(mesh, float(i+1)*dt)
69+
70+
fig.gca().clear()
71+
#plt.plot(mesh, pnew_bdf, 'b', label='BDF-2')
72+
plt.plot(mesh, pnew_tp, 'r', label='Trapezoidal')
73+
plt.plot(mesh, pex, 'k', label='Exact')
74+
fig.gca().set_xlim([0, 1.0])
75+
fig.gca().set_ylim([-0.5, 1.1])
76+
fig.gca().legend(loc=3)
77+
fig.gca().grid()
78+
plt.draw()
79+
plt.pause(0.0001)
80+
#if i==0:
81+
# plt.gcf().savefig('initial.pdf', bbox_inches='tight')
82+
y0_ie = ynew_ie
83+
y0_tp = ynew_tp
84+
ym1_bdf = y0_bdf
85+
y0_bdf = ynew_bdf
86+
#plt.show()
87+
#plt.gcf().savefig('final.pdf', bbox_inches='tight')

0 commit comments

Comments
 (0)