Skip to content

Commit 8252b51

Browse files
Daniel RuprechtDaniel Ruprecht
authored andcommitted
problem specification for convergence test
1 parent 6eb8066 commit 8252b51

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
r"""
2+
One-dimensional IMEX acoustic-advection
3+
=========================
4+
5+
Integrate the linear 1D acoustic-advection problem:
6+
7+
.. math::
8+
u_t + U u_x + c p_x & = 0 \\
9+
p_t + U p_x + c u_x & = 0.
10+
11+
"""
12+
13+
import numpy as np
14+
import scipy.sparse as sp
15+
import scipy.sparse.linalg as LA
16+
17+
from pySDC.Problem import ptype
18+
from pySDC.datatype_classes.mesh import mesh, rhs_imex_mesh
19+
20+
from buildWave1DMatrix import getWave1DMatrix, getWave1DAdvectionMatrix
21+
22+
def u_initial(x, k):
23+
return np.sin(k*2.0*np.pi*x)
24+
25+
class acoustic_1d_imex(ptype):
26+
"""
27+
Example implementing the forced 1D heat equation with Dirichlet-0 BC in [0,1]
28+
29+
Attributes:
30+
solver: Sharpclaw solver
31+
state: Sharclaw state
32+
domain: Sharpclaw domain
33+
"""
34+
35+
def __init__(self, cparams, dtype_u, dtype_f):
36+
"""
37+
Initialization routine
38+
39+
Args:
40+
cparams: custom parameters for the example
41+
dtype_u: particle data type (will be passed parent class)
42+
dtype_f: acceleration data type (will be passed parent class)
43+
"""
44+
45+
# these parameters will be used later, so assert their existence
46+
assert 'nvars' in cparams
47+
assert 'cs' in cparams
48+
assert 'cadv' in cparams
49+
assert 'order_adv' in cparams
50+
assert 'waveno' in cparams
51+
52+
# add parameters as attributes for further reference
53+
for k,v in cparams.items():
54+
setattr(self,k,v)
55+
56+
# invoke super init, passing number of dofs, dtype_u and dtype_f
57+
super(acoustic_1d_imex,self).__init__(self.nvars,dtype_u,dtype_f)
58+
59+
self.mesh = np.linspace(0.0, 1.0, self.nvars[1], endpoint=False)
60+
self.dx = self.mesh[1] - self.mesh[0]
61+
62+
self.Dx = -self.cadv*getWave1DAdvectionMatrix(self.nvars[1], self.dx, self.order_adv)
63+
self.Id, A = getWave1DMatrix(self.nvars[1], self.dx, ['periodic','periodic'], ['periodic','periodic'])
64+
self.A = -self.cs*A
65+
66+
def solve_system(self,rhs,factor,u0,t):
67+
"""
68+
Simple linear solver for (I-dtA)u = rhs
69+
70+
Args:
71+
rhs: right-hand side for the nonlinear system
72+
factor: abbrev. for the node-to-node stepsize (or any other factor required)
73+
u0: initial guess for the iterative solver (not used here so far)
74+
t: current time (e.g. for time-dependent BCs)
75+
76+
Returns:
77+
solution as mesh
78+
"""
79+
80+
M = self.Id - factor*self.A
81+
82+
b = np.concatenate( (rhs.values[0,:], rhs.values[1,:]) )
83+
84+
sol = LA.spsolve(M, b)
85+
86+
me = mesh(self.nvars)
87+
me.values[0,:], me.values[1,:] = np.split(sol, 2)
88+
89+
return me
90+
91+
92+
def __eval_fexpl(self,u,t):
93+
"""
94+
Helper routine to evaluate the explicit part of the RHS
95+
96+
Args:
97+
u: current values (not used here)
98+
t: current time
99+
100+
Returns:
101+
explicit part of RHS
102+
"""
103+
104+
105+
b = np.concatenate( (u.values[0,:], u.values[1,:]) )
106+
sol = self.Dx.dot(b)
107+
108+
fexpl = mesh(self.nvars)
109+
fexpl.values[0,:], fexpl.values[1,:] = np.split(sol, 2)
110+
111+
return fexpl
112+
113+
114+
def __eval_fimpl(self,u,t):
115+
"""
116+
Helper routine to evaluate the implicit part of the RHS
117+
118+
Args:
119+
u: current values
120+
t: current time (not used here)
121+
122+
Returns:
123+
implicit part of RHS
124+
"""
125+
126+
b = np.concatenate( (u.values[0,:], u.values[1,:]) )
127+
sol = self.A.dot(b)
128+
129+
fimpl = mesh(self.nvars,val=0)
130+
fimpl.values[0,:], fimpl.values[1,:] = np.split(sol, 2)
131+
132+
return fimpl
133+
134+
135+
def eval_f(self,u,t):
136+
"""
137+
Routine to evaluate both parts of the RHS
138+
139+
Args:
140+
u: current values
141+
t: current time
142+
143+
Returns:
144+
the RHS divided into two parts
145+
"""
146+
147+
f = rhs_imex_mesh(self.nvars)
148+
f.impl = self.__eval_fimpl(u,t)
149+
f.expl = self.__eval_fexpl(u,t)
150+
return f
151+
152+
def u_exact(self,t):
153+
"""
154+
Routine to compute the exact solution at time t
155+
156+
Args:
157+
t: current time
158+
159+
Returns:
160+
exact solution
161+
"""
162+
163+
me = mesh(self.nvars)
164+
me.values[0,:] = 0.5*u_initial( self.mesh - (self.cadv + self.cs)*t , self.waveno) - 0.5*u_initial( self.mesh - (self.cadv - self.cs)*t , self.waveno)
165+
me.values[1,:] = 0.5*u_initial( self.mesh - (self.cadv + self.cs)*t , self.waveno) + 0.5*u_initial( self.mesh - (self.cadv - self.cs)*t , self.waveno)
166+
return me
167+
168+

0 commit comments

Comments
 (0)