Skip to content

Commit 28d1172

Browse files
author
Daniel Ruprecht
committed
removed run_standard; everything is now included in runmultiscale script
1 parent 4fc13ee commit 28d1172

File tree

3 files changed

+208
-122
lines changed

3 files changed

+208
-122
lines changed

examples/acoustic_1d_imex/run_standard.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

examples/acoustic_1d_imex/runmultiscale.py

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
from pySDC import Log
1313
from pySDC.Stats import grep_stats, sort_stats
1414

15-
# Sharpclaw imports
16-
#from clawpack import pyclaw
17-
#from clawpack import riemann
15+
from standard_integrators import bdf2, dirk, trapezoidal
16+
1817
from matplotlib import pyplot as plt
18+
from pylab import rcParams
1919

20+
fs = 8
2021

2122
if __name__ == "__main__":
2223

@@ -55,8 +56,11 @@
5556
description['problem_params'] = pparams
5657
description['dtype_u'] = mesh
5758
description['dtype_f'] = rhs_imex_mesh
58-
description['collocation_class'] = collclass.CollGaussLobatto
59-
description['num_nodes'] = 3
59+
description['collocation_class'] = collclass.CollGaussRadau_Right
60+
if sparams['maxiter']==2:
61+
description['num_nodes'] = 2
62+
else:
63+
description['num_nodes'] = 3
6064
description['sweeper_class'] = imex_1st_order
6165
description['level_params'] = lparams
6266
description['hook_class'] = plot_solution
@@ -73,25 +77,64 @@
7377
# call main function to get things done...
7478
uend,stats = mp.run_pfasst(MS,u0=uinit,t0=t0,dt=dt,Tend=Tend)
7579

76-
# compute exact solution and compare
77-
uex = P.u_exact(Tend)
80+
# instantiate standard integrators to be run for comparison
81+
trap = trapezoidal( P.A+P.Dx, 0.5 )
82+
bdf2 = bdf2( P.A+P.Dx)
83+
dirk = dirk( P.A+P.Dx, sparams['maxiter'])
84+
85+
y0_tp = np.concatenate( (uinit.values[0,:], uinit.values[1,:]) )
86+
y0_bdf = y0_tp
87+
y0_dirk = y0_tp
88+
89+
# Perform 154 time steps with standard integrators
90+
for i in range(0,154):
91+
92+
# trapezoidal rule step
93+
ynew_tp = trap.timestep(y0_tp, dt)
94+
95+
# BDF-2 scheme
96+
if i==0:
97+
ynew_bdf = bdf2.firsttimestep( y0_bdf, dt)
98+
ym1_bdf = y0_bdf
99+
else:
100+
ynew_bdf = bdf2.timestep( y0_bdf, ym1_bdf, dt)
78101

79-
fig = plt.figure(figsize=(8,8))
102+
# DIRK scheme
103+
ynew_dirk = dirk.timestep(y0_dirk, dt)
104+
105+
y0_tp = ynew_tp
106+
ym1_bdf = y0_bdf
107+
y0_bdf = ynew_bdf
108+
y0_dirk = ynew_dirk
109+
110+
# Finished running standard integrators
111+
unew_tp, pnew_tp = np.split(ynew_tp, 2)
112+
unew_bdf, pnew_bdf = np.split(ynew_bdf, 2)
113+
unew_dirk, pnew_dirk = np.split(ynew_dirk, 2)
114+
115+
rcParams['figure.figsize'] = 5, 2.5
116+
fig = plt.figure()
80117

81118
sigma_0 = 0.1
82119
k = 7.0*2.0*np.pi
83120
x_0 = 0.75
84121
x_1 = 0.25
85122

86-
#plt.plot(P.mesh, uex.values[0,:], '+', color='b', label='u (exact)')
87-
plt.plot(P.mesh, uend.values[1,:], '-', color='b', label='SDC')
123+
plt.plot(P.mesh, pnew_tp, '-', color='c', label='Trapezoidal')
124+
plt.plot(P.mesh, uend.values[1,:], '-', color='b', label='SDC('+str(sparams['maxiter'])+')')
125+
plt.plot(P.mesh, pnew_bdf, '-', color='r', label='BDF-2')
126+
plt.plot(P.mesh, pnew_dirk, color='g', label='DIRK('+str(dirk.order)+')')
88127
#plt.plot(P.mesh, uex.values[1,:], '+', color='r', label='p (exact)')
89128
#plt.plot(P.mesh, uend.values[1,:], '-', color='b', linewidth=2.0, label='p (SDC)')
90-
p_slow = np.exp(-np.square(P.mesh-x_0-pparams['cadv']*Tend)/(sigma_0*sigma_0))
91-
plt.plot(P.mesh, p_slow, '-', color='r', markersize=4, label='slow mode')
92-
plt.legend(loc=2)
93-
plt.xlim([0, 1])
94-
plt.ylim([-0.1, 1.1])
129+
130+
p_slow = np.exp(-np.square( np.mod( P.mesh-pparams['cadv']*Tend, 1.0 ) -x_0 )/(sigma_0*sigma_0))
131+
plt.plot(P.mesh, p_slow, '+', color='k', markersize=fs-2, label='Slow mode', markevery=10)
132+
plt.xlabel('x', fontsize=fs, labelpad=0)
133+
plt.ylabel('Pressure', fontsize=fs, labelpad=0)
134+
fig.gca().set_xlim([0, 1.0])
135+
fig.gca().set_ylim([-0.5, 1.1])
136+
fig.gca().tick_params(axis='both', labelsize=fs)
137+
plt.legend(loc='upper left', fontsize=fs, prop={'size':fs})
95138
fig.gca().grid()
96-
plt.show()
97-
#plt.gcf().savefig('fwsw-sdc-K'+str(sparams['maxiter'])+'-M'+str(description['num_nodes'])+'.pdf', bbox_inches='tight')
139+
#plt.show()
140+
plt.gcf().savefig('fwsw-sdc-K'+str(sparams['maxiter'])+'-M'+str(description['num_nodes'])+'.pdf', bbox_inches='tight')
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import numpy as np
2+
import math
3+
import scipy.sparse.linalg as LA
4+
import scipy.sparse as sp
5+
6+
#
7+
# Trapezoidal rule
8+
#
9+
class trapezoidal:
10+
11+
def __init__(self, M, alpha=0.5):
12+
assert np.shape(M)[0]==np.shape(M)[1], "Matrix M must be quadratic"
13+
self.Ndof = np.shape(M)[0]
14+
self.M = M
15+
self.alpha = alpha
16+
17+
def timestep(self, u0, dt):
18+
M_trap = sp.eye(self.Ndof) - self.alpha*dt*self.M
19+
B_trap = sp.eye(self.Ndof) + (1.0-self.alpha)*dt*self.M
20+
b = B_trap.dot(u0)
21+
return LA.spsolve(M_trap, b)
22+
#
23+
# A BDF-2 implicit two-step method
24+
#
25+
class bdf2:
26+
27+
def __init__(self, M):
28+
assert np.shape(M)[0]==np.shape(M)[1], "Matrix M must be quadratic"
29+
self.Ndof = np.shape(M)[0]
30+
self.M = M
31+
32+
def firsttimestep(self, u0, dt):
33+
b = u0
34+
L = sp.eye(self.Ndof) - dt*self.M
35+
return LA.spsolve(L, b)
36+
37+
def timestep(self, u0, um1, dt):
38+
b = (4.0/3.0)*u0 - (1.0/3.0)*um1
39+
L = sp.eye(self.Ndof) - (2.0/3.0)*dt*self.M
40+
return LA.spsolve(L, b)
41+
#
42+
# A diagonally implicit Runge-Kutta method of order 2, 3 or 4
43+
#
44+
class dirk:
45+
46+
def __init__(self, M, order):
47+
48+
assert np.shape(M)[0]==np.shape(M)[1], "Matrix M must be quadratic"
49+
self.Ndof = np.shape(M)[0]
50+
self.M = M
51+
self.order = order
52+
53+
assert self.order in [2,22,3,4], 'Order must be 2,22,3,4'
54+
55+
if (self.order==2):
56+
self.nstages = 1
57+
self.A = np.zeros((1,1))
58+
self.A[0,0] = 0.5
59+
self.tau = [0.5]
60+
self.b = [1.0]
61+
62+
if (self.order==22):
63+
self.nstages = 2
64+
self.A = np.zeros((2,2))
65+
self.A[0,0] = 1.0/3.0
66+
self.A[1,0] = 1.0/2.0
67+
self.A[1,1] = 1.0/2.0
68+
69+
self.tau = np.zeros(2)
70+
self.tau[0] = 1.0/3.0
71+
self.tau[1] = 1.0
72+
73+
self.b = np.zeros(2)
74+
self.b[0] = 3.0/4.0
75+
self.b[1] = 1.0/4.0
76+
77+
78+
if (self.order==3):
79+
self.nstages = 2
80+
self.A = np.zeros((2,2))
81+
self.A[0,0] = 0.5 + 1.0/( 2.0*math.sqrt(3.0) )
82+
self.A[1,0] = -1.0/math.sqrt(3.0)
83+
self.A[1,1] = self.A[0,0]
84+
85+
self.tau = np.zeros(2)
86+
self.tau[0] = 0.5 + 1.0/( 2.0*math.sqrt(3.0) )
87+
self.tau[1] = 0.5 - 1.0/( 2.0*math.sqrt(3.0) )
88+
89+
self.b = np.zeros(2)
90+
self.b[0] = 0.5
91+
self.b[1] = 0.5
92+
93+
if (self.order==4):
94+
self.nstages = 3
95+
alpha = 2.0*math.cos(math.pi/18.0)/math.sqrt(3.0)
96+
97+
self.A = np.zeros((3,3))
98+
self.A[0,0] = (1.0 + alpha)/2.0
99+
self.A[1,0] = -alpha/2.0
100+
self.A[1,1] = self.A[0,0]
101+
self.A[2,0] = (1.0 + alpha)
102+
self.A[2,1] = -(1.0 + 2.0*alpha)
103+
self.A[2,2] = self.A[0,0]
104+
105+
self.tau = np.zeros(3)
106+
self.tau[0] = (1.0 + alpha)/2.0
107+
self.tau[1] = 1.0/2.0
108+
self.tau[2] = (1.0 - alpha)/2.0
109+
110+
self.b = np.zeros(3)
111+
self.b[0] = 1.0/(6.0*alpha*alpha)
112+
self.b[1] = 1.0 - 1.0/(3.0*alpha*alpha)
113+
self.b[2] = 1.0/(6.0*alpha*alpha)
114+
115+
self.stages = np.zeros((self.nstages,self.Ndof))
116+
117+
def timestep(self, u0, dt):
118+
119+
uend = u0
120+
for i in range(0,self.nstages):
121+
122+
b = u0
123+
124+
# Compute right hand side for this stage's implicit step
125+
for j in range(0,i):
126+
b = b + self.A[i,j]*dt*self.f(self.stages[j,:])
127+
128+
# Implicit solve for current stage
129+
self.stages[i,:] = self.f_solve( b, dt*self.A[i,i] )
130+
131+
# Add contribution of current stage to final value
132+
uend = uend + self.b[i]*dt*self.f(self.stages[i,:])
133+
134+
return uend
135+
136+
#
137+
# Returns f(u) = c*u
138+
#
139+
def f(self,u):
140+
return self.M.dot(u)
141+
142+
143+
#
144+
# Solves (Id - alpha*c)*u = b for u
145+
#
146+
def f_solve(self, b, alpha):
147+
L = sp.eye(self.Ndof) - alpha*self.M
148+
return LA.spsolve(L, b)

0 commit comments

Comments
 (0)