Skip to content

Commit e244a0a

Browse files
author
Daniel Ruprecht
committed
order parameter now correctly passed down to buildFDMatrix; upwind operators available for order 1 to 5; buildFDMatrix still needs modification to support more than order 4
1 parent 7229d67 commit e244a0a

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

examples/boussinesq_2d_imex/build2DFDMatrix.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
import scipy.sparse as sp
55
from buildFDMatrix import getMatrix, getUpwindMatrix, getBCLeft, getBCRight
66

7-
def get2DUpwindMatrix(N, dx):
7+
#
8+
#
9+
#
10+
def get2DUpwindMatrix(N, dx, order):
811

9-
Dx = getUpwindMatrix( N[0], dx)
12+
Dx = getUpwindMatrix( N[0], dx, order)
1013
return sp.kron( Dx, sp.eye(N[1]), format="csr" )
1114

15+
#
16+
#
17+
#
1218
def get2DMesh(N, x_b, z_b, bc_hor, bc_ver):
1319
assert np.size(N)==2, 'N needs to be an array with two entries: N[0]=Nx and N[1]=Nz'
1420
assert np.size(x_b)==2, 'x_b needs to be an array with two entries: x_b[0] = left boundary, x_b[1] = right boundary'
@@ -39,12 +45,16 @@ def get2DMesh(N, x_b, z_b, bc_hor, bc_ver):
3945
xx, zz = np.meshgrid(x,z,indexing="ij")
4046
return xx, zz, h
4147

42-
def get2DMatrix(N, h, bc_hor, bc_ver):
48+
#
49+
#
50+
#
51+
def get2DMatrix(N, h, bc_hor, bc_ver, order):
52+
4353
assert np.size(N)==2, 'N needs to be an array with two entries: N[0]=Nx and N[1]=Nz'
4454
assert np.size(h)==2, 'h needs to be an array with two entries: h[0]=dx and h[1]=dz'
4555

46-
Ax = getMatrix( N[0], h[0], bc_hor[0], bc_hor[1])
47-
Az = getMatrix( N[1], h[1], bc_ver[0], bc_ver[1])
56+
Ax = getMatrix( N[0], h[0], bc_hor[0], bc_hor[1], order)
57+
Az = getMatrix( N[1], h[1], bc_ver[0], bc_ver[1], order)
4858

4959
Dx = sp.kron( Ax, sp.eye(N[1]), format="csr")
5060
Dz = sp.kron( sp.eye(N[0]), Az, format="csr")
@@ -83,4 +93,4 @@ def getBCVertical( value, N, dz, bc_ver):
8393
bu = getBCRight( value[1], N[1], dz, bc_ver[1])
8494
bu = np.kron( np.ones(N[0]), bu)
8595

86-
return bd, bu
96+
return bd, bu

examples/boussinesq_2d_imex/buildBoussinesq2DMatrix.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def getBoussinesq2DUpwindMatrix(N, dx, u_adv, order):
66

7-
Dx = get2DUpwindMatrix(N, dx)
7+
Dx = get2DUpwindMatrix(N, dx, order)
88

99
# Note: In the equations it is u_t + u_adv* D_x u = ... so in order to comply with the form u_t = M u,
1010
# add a minus sign in front of u_adv
@@ -19,10 +19,12 @@ def getBoussinesq2DUpwindMatrix(N, dx, u_adv, order):
1919
return sp.csc_matrix(M)
2020

2121
def getBoussinesq2DMatrix(N, h, bc_hor, bc_ver, c_s, Nfreq, order):
22-
Dx_u, Dz_u = get2DMatrix(N, h, bc_hor[0], bc_ver[0])
23-
Dx_w, Dz_w = get2DMatrix(N, h, bc_hor[1], bc_ver[1])
24-
Dx_b, Dz_b = get2DMatrix(N, h, bc_hor[2], bc_ver[2])
25-
Dx_p, Dz_p = get2DMatrix(N, h, bc_hor[3], bc_ver[3])
22+
23+
Dx_u, Dz_u = get2DMatrix(N, h, bc_hor[0], bc_ver[0], order)
24+
Dx_w, Dz_w = get2DMatrix(N, h, bc_hor[1], bc_ver[1], order)
25+
Dx_b, Dz_b = get2DMatrix(N, h, bc_hor[2], bc_ver[2], order)
26+
Dx_p, Dz_p = get2DMatrix(N, h, bc_hor[3], bc_ver[3], order)
27+
2628
Id_N = sp.eye(N[0]*N[1])
2729

2830
Zero = np.zeros((N[0]*N[1],N[0]*N[1]))
@@ -53,4 +55,4 @@ def getBoussinesqBCHorizontal(value, N, dx, bc_hor):
5355
return b_left, b_right
5456

5557
def getBoussinesqBCVertical():
56-
return 0.0
58+
return 0.0

examples/boussinesq_2d_imex/buildFDMatrix.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
import numpy as np
22
import scipy.sparse as sp
33
import scipy.linalg as la
4+
import sys
5+
46
# Only for periodic BC because we have advection only in x direction
5-
def getUpwindMatrix(N, dx):
6-
7-
#stencil = [-1.0, 1.0]
8-
#zero_pos = 2
9-
#coeff = 1.0
10-
11-
#stencil = [1.0, -4.0, 3.0]
12-
#coeff = 1.0/2.0
13-
#zero_pos = 3
14-
15-
#stencil = [1.0, -6.0, 3.0, 2.0]
16-
#coeff = 1.0/6.0
17-
#zero_pos = 3
7+
def getUpwindMatrix(N, dx, order):
188

19-
#stencil = [-5.0, 30.0, -90.0, 50.0, 15.0]
20-
#coeff = 1.0/60.0
21-
#zero_pos = 4
9+
if order==1:
10+
stencil = [-1.0, 1.0]
11+
zero_pos = 2
12+
coeff = 1.0
2213

23-
stencil = [3.0, -20.0, 60.0, -120.0, 65.0, 12.0]
24-
coeff = 1.0/60.0
25-
zero_pos = 5
14+
elif order==2:
15+
stencil = [1.0, -4.0, 3.0]
16+
coeff = 1.0/2.0
17+
zero_pos = 3
18+
19+
elif order==3:
20+
stencil = [1.0, -6.0, 3.0, 2.0]
21+
coeff = 1.0/6.0
22+
zero_pos = 3
23+
24+
elif order==4:
25+
stencil = [-5.0, 30.0, -90.0, 50.0, 15.0]
26+
coeff = 1.0/60.0
27+
zero_pos = 4
2628

29+
elif order==5:
30+
stencil = [3.0, -20.0, 60.0, -120.0, 65.0, 12.0]
31+
coeff = 1.0/60.0
32+
zero_pos = 5
33+
else:
34+
sys.exit("Order "+str(order)+" not implemented.")
35+
2736
first_col = np.zeros(N)
2837

2938
# Because we need to specific first column (not row) in circulant, flip stencil array
@@ -34,13 +43,14 @@ def getUpwindMatrix(N, dx):
3443

3544
return sp.csc_matrix( coeff*(1.0/dx)*la.circulant(first_col) )
3645

37-
def getMatrix(N, dx, bc_left, bc_right):
46+
def getMatrix(N, dx, bc_left, bc_right, order):
3847
stencil = [1.0, -8.0, 0.0, 8.0, -1.0]
3948
range = [ -2, -1, 0, 1, 2]
4049
A = sp.diags(stencil, range, shape=(N,N))
4150
A = sp.lil_matrix(A)
4251

4352
assert bc_left in ['periodic','neumann','dirichlet'], "Unknown type of BC"
53+
4454
if bc_left in ['periodic']:
4555
assert bc_right in ['periodic'], "Periodic BC can only be selected for both sides simultaneously"
4656

examples/boussinesq_2d_imex/playground.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
# setup parameters "in time"
4242
t0 = 0
43-
Tend = 3000
44-
Nsteps = 500
43+
#Tend = 3000
44+
#Nsteps = 500
4545
Tend = 30
4646
Nsteps = 5
4747
dt = Tend/float(Nsteps)
@@ -54,7 +54,7 @@
5454
pparams['Nfreq'] = 0.01
5555
pparams['x_bounds'] = [(-150.0, 150.0)]
5656
pparams['z_bounds'] = [( 0.0, 10.0)]
57-
pparams['order'] = [0] # [fine_level, coarse_level]
57+
pparams['order'] = [1] # [fine_level, coarse_level]
5858
pparams['gmres_maxiter'] = [50]
5959
pparams['gmres_restart'] = 20
6060
pparams['gmres_tol'] = 1e-6
@@ -105,4 +105,4 @@
105105

106106
#extract_stats = grep_stats(stats,iter=-1,type='residual')
107107
#sortedlist_stats = sort_stats(extract_stats,sortby='step')
108-
#print(extract_stats,sortedlist_stats)
108+
#print(extract_stats,sortedlist_stats)

0 commit comments

Comments
 (0)