-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_acoustics_2d.py
165 lines (128 loc) · 5.69 KB
/
linear_acoustics_2d.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pylab as pl
from GenericFVUtils import *
rho0_ = 10.
K0_ = 1.
c0_ = np.sqrt(K0_/rho0_)
def maxAbsEig(hcl):
return max( \
max( np.max(abs(hcl.params.u0 - hcl.params.c0)), np.max(abs(hcl.params.u0 + hcl.params.c0)) )/hcl.dx \
, \
max( np.max(abs(hcl.params.v0 - hcl.params.c0)), np.max(abs(hcl.params.v0 + hcl.params.c0)) )/hcl.dy \
)
def numFluxX_HLL2(self, U, dt, dx):
sL = self.params.u0 - self.params.c0
sR = self.params.u0 + self.params.c0
mask_plus = sL > 0
mask_minus = sR < 0
mask_middle = -mask_plus & -mask_minus
#mask_middleL = middle & self.params.u0<=0
#mask_middleR = middle & self.params.u0>0
F0W = self.params.u0*U[0].uW + self.params.K0*U[1].uW
F1W = 1./self.params.rho0*U[0].uW + self.params.u0*U[1].uW
F2W = 0.*self.params.u0*U[2].uW
F0E = self.params.u0*U[0].uE + self.params.K0*U[1].uE
F1E = 1./self.params.rho0*U[0].uE + self.params.u0*U[1].uE
F2E = self.params.u0*U[2].uE
F0 = np.empty_like(U[0].uW)
F1 = np.empty_like(U[1].uW)
F2 = np.empty_like(U[2].uW)
[F0[mask_plus], F1[mask_plus], F2[mask_plus]] = [F0W[mask_plus], F1W[mask_plus], F2W[mask_plus]]
[F0[mask_minus], F1[mask_minus], F2[mask_minus]] = [F0E[mask_minus], F1E[mask_minus], F2E[mask_minus]]
[F0[mask_middle], F1[mask_middle], F2[mask_middle]] = [
(sR[mask_middle]*F0W[mask_middle] - sL[mask_middle]*F0E[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[0].uE[mask_middle] - U[0].uW[mask_middle]) )/(2.*self.params.c0),
(sR[mask_middle]*F1W[mask_middle] - sL[mask_middle]*F1E[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[1].uE[mask_middle] - U[1].uW[mask_middle]) )/(2.*self.params.c0),
(sR[mask_middle]*F2W[mask_middle] - sL[mask_middle]*F2E[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[2].uE[mask_middle] - U[2].uW[mask_middle]) )/(2.*self.params.c0)
]
return [F0, F1, F2]
def numFluxY_HLL2(self, U, dt, dy):
sL = self.params.v0 - self.params.c0
sR = self.params.v0 + self.params.c0
mask_plus = sL > 0
mask_minus = sR < 0
mask_middle = -mask_plus & -mask_minus
#mask_middleL = middle & self.params.v0<=0
#mask_middleR = middle & self.params.v0>0
F0S = self.params.v0*U[0].uS + self.params.K0*U[2].uS
F1S = self.params.v0*U[1].uS
F2S = 1./self.params.rho0*U[0].uS + self.params.v0*U[2].uS
F0N = self.params.v0*U[0].uN + self.params.K0*U[2].uN
F1N = self.params.v0*U[1].uN
F2N = 1./self.params.rho0*U[0].uN + self.params.v0*U[2].uN
F0 = np.empty_like(U[0].uS)
F1 = np.empty_like(U[1].uS)
F2 = np.empty_like(U[2].uS)
[F0[mask_plus], F1[mask_plus], F2[mask_plus]] = [F0S[mask_plus], F1S[mask_plus], F2S[mask_plus]]
[F0[mask_minus], F1[mask_minus], F2[mask_minus]] = [F0S[mask_minus], F1S[mask_minus], F2S[mask_minus]]
[F0[mask_middle], F1[mask_middle], F2[mask_middle]] = [
(sR[mask_middle]*F0S[mask_middle] - sL[mask_middle]*F0N[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[0].uN[mask_middle] - U[0].uS[mask_middle]) )/(2.*self.params.c0),
(sR[mask_middle]*F1S[mask_middle] - sL[mask_middle]*F1N[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[1].uN[mask_middle] - U[1].uS[mask_middle]) )/(2.*self.params.c0),
(sR[mask_middle]*F2S[mask_middle] - sL[mask_middle]*F2N[mask_middle] + sL[mask_middle]*sR[mask_middle]*(U[2].uN[mask_middle] - U[2].uS[mask_middle]) )/(2.*self.params.c0)
]
return [F0, F1, F2]
def boundaryCondFunW(t, dx, y):
# square pulse
u = 0.*y
if t<1.0:
#u = 0.25
u[(y>.45) & (y<.55)] = np.sin(2*2*np.pi*t)
return [0.*y, u, 0.*y]
def initialCondFun(xv, yv):
#uinit = .1*np.ones((ny, nx))
#uinit[np.sqrt((xv-.25)**2 + (yv-.25)**2)<.125] = 1.
u0_init = np.zeros_like(xv)
u1_init = np.zeros_like(xv)
u2_init = np.zeros_like(xv)
return [u0_init, u1_init, u2_init]
def linear(nx=100, ny=100 ,Tmax=1., order=1, limiter='minmod', method='HLL2'):
dim = 2
# generate instance of class
hcl = HyperbolicConsLawNumSolver(dim, order, limiter, True)
#set numerical Flux
if method=='HLL2':
numFluxX = numFluxX_HLL2
numFluxY = numFluxY_HLL2
else:
numFluxX = numFluxX_HLL3
numFluxY = numFluxY_HLL3
hcl.setNumericalFluxFuns(numFluxX, numFluxY, maxAbsEig)
# set boundary conditions
boundaryCondFunN = "Neumann"
boundaryCondFunS = "Neumann"
#boundaryCondFunW = "Neumann"
boundaryCondFunE = "Neumann"
hcl.setBoundaryCond(boundaryCondFunE, boundaryCondFunW, boundaryCondFunN, boundaryCondFunS)
# set initial state
xCc = np.linspace(0.+.5/nx,1.-.5/nx,nx) # cell centers
yCc = np.linspace(0.+.5/ny,1.-.5/ny,ny) # cell centers
xv, yv = np.meshgrid(xCc, yCc)
hcl.setUinit(initialCondFun(xv, yv), nx, ny, xCc, yCc)
# set flux parameters
xCi = np.linspace(1.+.5,1.+-.5,nx+1) # cell interface
yCi = np.linspace(1.+.5,1.+-.5,ny) # cell interface
xvi, yvi = np.meshgrid(xCi, yCi)
u0_ = 0*xvi
#u0_ = np.ones((ny, nx+1))
#u0_[xvi<0] = -1
xCi = np.linspace(1.+.5,1.+-.5,nx) # cell interface
yCi = np.linspace(1.+.5,1.+-.5,ny+1) # cell interface
xvi, yvi = np.meshgrid(xCi, yCi)
v0_ = 0*yvi
#v0_ = np.ones((ny+1, nx))
#v0_[yvi<0] = -1
hcl.setFluxAndSourceParams(rho0 = rho0_, K0 = K0_, c0 = c0_, u0 = u0_, v0 = v0_)
hcl.selfCheck()
# apply explicit time stepping
t = 0.
# flux is linear, i.e., eigenvalues are independent of time
eig = maxAbsEig(hcl)
CFL = 0.49
dt = 1.*CFL/eig
while t<Tmax:
if t+dt>Tmax:
dt=Tmax-t
t = hcl.timeStepExplicit(t, dt)
#plot result
pl.title('linear acoustics 2d')
pl.ion()
pl.pcolor(xv, yv, hcl.getU(0), cmap='RdBu')
return hcl