Skip to content

Commit 4fc13ee

Browse files
author
Daniel Ruprecht
committed
added dirk method to run_standard for comparison against fwsw-SDC
1 parent 70ca0c9 commit 4fc13ee

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

examples/acoustic_1d_imex/dirk.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import numpy as np
2+
import math
3+
import scipy.sparse.linalg as LA
4+
import scipy.sparse as sp
5+
6+
class dirk:
7+
8+
def __init__(self, M, order):
9+
10+
assert np.shape(M)[0]==np.shape(M)[1], "Matrix M must be quadratic"
11+
self.Ndof = np.shape(M)[0]
12+
self.M = M
13+
self.order = order
14+
15+
assert self.order in [2,22,3,4], 'Order must be 2,22,3,4'
16+
17+
if (self.order==2):
18+
self.nstages = 1
19+
self.A = np.zeros((1,1))
20+
self.A[0,0] = 0.5
21+
self.tau = [0.5]
22+
self.b = [1.0]
23+
24+
if (self.order==22):
25+
self.nstages = 2
26+
self.A = np.zeros((2,2))
27+
self.A[0,0] = 1.0/3.0
28+
self.A[1,0] = 1.0/2.0
29+
self.A[1,1] = 1.0/2.0
30+
31+
self.tau = np.zeros(2)
32+
self.tau[0] = 1.0/3.0
33+
self.tau[1] = 1.0
34+
35+
self.b = np.zeros(2)
36+
self.b[0] = 3.0/4.0
37+
self.b[1] = 1.0/4.0
38+
39+
40+
if (self.order==3):
41+
self.nstages = 2
42+
self.A = np.zeros((2,2))
43+
self.A[0,0] = 0.5 + 1.0/( 2.0*math.sqrt(3.0) )
44+
self.A[1,0] = -1.0/math.sqrt(3.0)
45+
self.A[1,1] = self.A[0,0]
46+
47+
self.tau = np.zeros(2)
48+
self.tau[0] = 0.5 + 1.0/( 2.0*math.sqrt(3.0) )
49+
self.tau[1] = 0.5 - 1.0/( 2.0*math.sqrt(3.0) )
50+
51+
self.b = np.zeros(2)
52+
self.b[0] = 0.5
53+
self.b[1] = 0.5
54+
55+
if (self.order==4):
56+
self.nstages = 3
57+
alpha = 2.0*math.cos(math.pi/18.0)/math.sqrt(3.0)
58+
59+
self.A = np.zeros((3,3))
60+
self.A[0,0] = (1.0 + alpha)/2.0
61+
self.A[1,0] = -alpha/2.0
62+
self.A[1,1] = self.A[0,0]
63+
self.A[2,0] = (1.0 + alpha)
64+
self.A[2,1] = -(1.0 + 2.0*alpha)
65+
self.A[2,2] = self.A[0,0]
66+
67+
self.tau = np.zeros(3)
68+
self.tau[0] = (1.0 + alpha)/2.0
69+
self.tau[1] = 1.0/2.0
70+
self.tau[2] = (1.0 - alpha)/2.0
71+
72+
self.b = np.zeros(3)
73+
self.b[0] = 1.0/(6.0*alpha*alpha)
74+
self.b[1] = 1.0 - 1.0/(3.0*alpha*alpha)
75+
self.b[2] = 1.0/(6.0*alpha*alpha)
76+
77+
self.stages = np.zeros((self.nstages,self.Ndof))
78+
79+
def timestep(self, u0, dt):
80+
81+
uend = u0
82+
for i in range(0,self.nstages):
83+
84+
b = u0
85+
86+
# Compute right hand side for this stage's implicit step
87+
for j in range(0,i):
88+
b = b + self.A[i,j]*dt*self.f(self.stages[j,:])
89+
90+
# Implicit solve for current stage
91+
self.stages[i,:] = self.f_solve( b, dt*self.A[i,i] )
92+
93+
# Add contribution of current stage to final value
94+
uend = uend + self.b[i]*dt*self.f(self.stages[i,:])
95+
96+
return uend
97+
98+
#
99+
# Returns f(u) = c*u
100+
#
101+
def f(self,u):
102+
return self.M.dot(u)
103+
104+
105+
#
106+
# Solves (Id - alpha*c)*u = b for u
107+
#
108+
def f_solve(self, b, alpha):
109+
L = sp.eye(self.Ndof) - alpha*self.M
110+
return LA.spsolve(L, b)

examples/acoustic_1d_imex/run_standard.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from matplotlib import pyplot as plt
66
from buildWave1DMatrix import getWave1DMatrix, getWave1DAdvectionMatrix
7+
from dirk import dirk
78

89
sigma_0 = 0.1
910
k = 7.0*2*np.pi
@@ -30,6 +31,8 @@ def u(x,t, multiscale):
3031
Id, A = getWave1DMatrix(nvars, dx, ['periodic','periodic'], ['periodic','periodic'])
3132
A = -cs*A
3233

34+
dirk = dirk( (A+Dx), 4)
35+
3336
M_ieuler = Id - dt*(A + Dx)
3437

3538
M_bdf = Id - (2.0/3.0)*dt*(A + Dx)
@@ -38,11 +41,11 @@ def u(x,t, multiscale):
3841
M_trap = Id - alpha*dt*(A+Dx)
3942
B_trap = Id + (1-alpha)*dt*(A+Dx)
4043

41-
u0, p0 = u(mesh, 0.0, 1.0)
42-
y0_ie = np.concatenate( (u0, p0) )
43-
y0_tp = y0_ie
44-
45-
y0_bdf = y0_ie
44+
u0, p0 = u(mesh, 0.0, 1.0)
45+
y0_ie = np.concatenate( (u0, p0) )
46+
y0_tp = y0_ie
47+
y0_bdf = y0_ie
48+
y0_dirk = y0_ie
4649

4750
fig = plt.figure(figsize=(8,8))
4851

@@ -62,15 +65,20 @@ def u(x,t, multiscale):
6265
b_bdf = (4.0/3.0)*y0_bdf - (1.0/3.0)*ym1_bdf
6366
ynew_bdf = LA.spsolve(M_bdf, b_bdf)
6467

65-
unew_ie, pnew_ie = np.split(ynew_ie, 2)
66-
unew_tp, pnew_tp = np.split(ynew_tp, 2)
68+
# DIRK scheme
69+
ynew_dirk = dirk.timestep(y0_dirk, dt)
70+
71+
unew_ie, pnew_ie = np.split(ynew_ie, 2)
72+
unew_tp, pnew_tp = np.split(ynew_tp, 2)
6773
unew_bdf, pnew_bdf = np.split(ynew_bdf, 2)
74+
unew_dirk, pnew_dirk = np.split(ynew_dirk, 2)
6875
uex, pex = u(mesh, float(i+1)*dt, 0.0)
6976

7077
if i==Nsteps-1:
7178
fig.gca().clear()
7279
plt.plot(mesh, pnew_bdf, 'b', label='BDF-2')
73-
plt.plot(mesh, pnew_tp, 'r', label='Trapezoidal')
80+
#plt.plot(mesh, pnew_tp, 'r', label='Trapezoidal')
81+
plt.plot(mesh, pnew_dirk, 'r', label='DIRK')
7482
plt.plot(mesh, pex, 'k', label='Slow Mode')
7583
fig.gca().set_xlim([0, 1.0])
7684
fig.gca().set_ylim([-0.5, 1.1])
@@ -89,6 +97,8 @@ def u(x,t, multiscale):
8997
y0_tp = ynew_tp
9098
ym1_bdf = y0_bdf
9199
y0_bdf = ynew_bdf
100+
y0_dirk = ynew_dirk
101+
92102
plt.show()
93103
#lt.gcf().savefig('final.pdf', bbox_inches='tight')
94104
#os.system('ffmpeg -r 25 -i images/standard-%03d.jpeg -vcodec libx264 -crf 25 movie.avi')

0 commit comments

Comments
 (0)