-
Notifications
You must be signed in to change notification settings - Fork 1
/
wave_equation_1d.jl
60 lines (46 loc) · 1.37 KB
/
wave_equation_1d.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Discretize the acoustic wave equation in 1D using staggered SBP operators.
Weakly impose a homogeneous boundary condition on each boundary.
"""
using SparseArrays
using LinearAlgebra
using Test
import sbp
# Number of grid points
nx = 100
"""
Load staggered grid operators and grids
xp denotes the regular grid with equidistantly spaced grid points, and xm
denotes the cell-centered grid, including boundary points.
"""
ops = sbp.OP2019
desc = ops.load_description()
xp, xm, h, Dp, Dm, Hp, Hm, Pp, Pm = ops.build_operators(desc, nx)
"""
Write the discretization as dq/dt = A * q, q = [v, p], where
A = [ 0, Dp,
Dm, 0],
v is stored on the xp grid, and p is stored on the xm grid.
"""
rows = [length(xp), length(xm)]
cols = [length(xp), length(xm)]
A = sbp.Sparse.block_matrix(rows, cols)
A12 = Dp
A21 = Dm
A = sbp.Sparse.block_matrix_insert(A, rows, rows, 1, 2, A12)
A = sbp.Sparse.block_matrix_insert(A, rows, rows, 2, 1, A21)
"""
Impose the boundary condition p = 0 on each boundary weakly in an energy
conserving manner.
"""
Bp = sbp.Staggered.boundary_matrix_p(nx)
Hpi = spdiagm(0 => 1 ./diag(Hp))
SAT = -Hpi*Bp
A = sbp.Sparse.block_matrix_insert(A, rows, rows, 1, 2, A12 + SAT)
"""
Check that the discretization is energy conservative to machine precision by
eigenvalue analysis
"""
lam = real(eigvals(Matrix(A)))
@test minimum(lam) > -1e-13
@test maximum(lam) < 1e-13