-
Notifications
You must be signed in to change notification settings - Fork 1
/
jacosimp.py
290 lines (248 loc) · 14.7 KB
/
jacosimp.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import taichi as ti
import numpy as np
from display import Display
from cgsolver import CGSolver
from bicgsolver import BICGSolver
ti.init(arch=ti.cpu, default_fp=ti.f64)
@ti.data_oriented
class SIMPLESolver:
def __init__(self, lx, ly, nx, ny):
self.lx = lx
self.ly = ly
self.nx = nx
self.ny = ny
self.dx = self.lx / self.nx
self.dy = self.ly / self.ny
self.rho= 1.00
self.mu = 0.01
self.dt = 100000
self.real = ti.f64
self.alpha_p = 0.01
self.alpha_u = 0.01
self.alpha = 0.1
self.u = ti.field(dtype=self.real, shape=(nx+3, ny+2))
self.v = ti.field(dtype=self.real, shape=(nx+2, ny+3))
self.u_mid = ti.field(dtype=self.real, shape=(nx+3, ny+2))
self.v_mid = ti.field(dtype=self.real, shape=(nx+2, ny+3))
self.u0 = ti.field(dtype=self.real, shape=(nx+3, ny+2)) # Previous time step
self.v0 = ti.field(dtype=self.real, shape=(nx+2, ny+3))
self.p = ti.field(dtype=self.real, shape=(nx+2, ny+2))
self.pcor = ti.field(dtype=self.real, shape=(nx+2, ny+2))
self.mdiv = ti.field(dtype=self.real, shape=(nx+2, ny+2))
self.bc = {'w': [0.0, 0.0], 'e': [0.0, 0.0], 'n': [0.0, 0.0], 's': [0.0, 0.0] }
self.ct = ti.field(dtype=self.real, shape=(nx+2, ny+2)) # Cell type
self.coef_u = ti.field(dtype=self.real, shape=(nx+3, ny+2, 5))
self.b_u = ti.field(dtype=self.real, shape=(nx+3, ny+2))
self.coef_v = ti.field(dtype=self.real, shape=(nx+2, ny+3, 5))
self.b_v = ti.field(dtype=self.real, shape=(nx+2, ny+3))
self.coef_p = ti.field(dtype=self.real, shape=(nx+2, ny+2, 5))
self.b_p = ti.field(dtype=self.real, shape=(nx+2, ny+2))
self.disp = Display(self)
def dump_matrix(self, step, msg): # Save u,v,p at step to csv files
for name,val in {'u':self.u, 'v':self.v, 'p':self.p, 'mdiv':self.mdiv, 'pcor':self.pcor}.items():
np.savetxt(f'log/{step:06}-{name}-{msg}.csv', val.to_numpy(), delimiter=',')
def dump_coef(self, step, msg):
np.savetxt(f'log/{step:06}-apu-{msg}.csv', self.coef_u.to_numpy()[:,:,0], delimiter=',')
np.savetxt(f'log/{step:06}-awu-{msg}.csv', self.coef_u.to_numpy()[:,:,1], delimiter=',')
np.savetxt(f'log/{step:06}-aeu-{msg}.csv', self.coef_u.to_numpy()[:,:,2], delimiter=',')
np.savetxt(f'log/{step:06}-anu-{msg}.csv', self.coef_u.to_numpy()[:,:,3], delimiter=',')
np.savetxt(f'log/{step:06}-asu-{msg}.csv', self.coef_u.to_numpy()[:,:,4], delimiter=',')
np.savetxt(f'log/{step:06}-bu -{msg}.csv', self.b_u.to_numpy(), delimiter=',')
np.savetxt(f'log/{step:06}-apv-{msg}.csv', self.coef_v.to_numpy()[:,:,0], delimiter=',')
np.savetxt(f'log/{step:06}-awv-{msg}.csv', self.coef_v.to_numpy()[:,:,1], delimiter=',')
np.savetxt(f'log/{step:06}-aev-{msg}.csv', self.coef_v.to_numpy()[:,:,2], delimiter=',')
np.savetxt(f'log/{step:06}-anv-{msg}.csv', self.coef_v.to_numpy()[:,:,3], delimiter=',')
np.savetxt(f'log/{step:06}-asv-{msg}.csv', self.coef_v.to_numpy()[:,:,4], delimiter=',')
np.savetxt(f'log/{step:06}-bv -{msg}.csv', self.b_v.to_numpy(), delimiter=',')
np.savetxt(f'log/{step:06}-app-{msg}.csv', self.coef_p.to_numpy()[:,:,0], delimiter=',')
np.savetxt(f'log/{step:06}-awp-{msg}.csv', self.coef_p.to_numpy()[:,:,1], delimiter=',')
np.savetxt(f'log/{step:06}-aep-{msg}.csv', self.coef_p.to_numpy()[:,:,2], delimiter=',')
np.savetxt(f'log/{step:06}-anp-{msg}.csv', self.coef_p.to_numpy()[:,:,3], delimiter=',')
np.savetxt(f'log/{step:06}-asp-{msg}.csv', self.coef_p.to_numpy()[:,:,4], delimiter=',')
np.savetxt(f'log/{step:06}-bp -{msg}.csv', self.b_p.to_numpy(), delimiter=',')
@ti.kernel
def compute_coef_u(self):
nx, ny, dx, dy, dt, rho, mu = self.nx, self.ny, self.dx, self.dy, self.dt, self.rho, self.mu
for i,j in ti.ndrange((2,nx+1), (1,ny+1)):
self.coef_u[i,j,1] = -(mu * dy / dx + 0.5 * rho * 0.5 * (self.u[i,j] + self.u[i-1,j]) * dy) # aw
self.coef_u[i,j,2] = -(mu * dy / dx - 0.5 * rho * 0.5 * (self.u[i,j] + self.u[i+1,j]) * dy) # ae
self.coef_u[i,j,3] = -(mu * dx / dy - 0.5 * rho * 0.5 * (self.v[i-1,j+1] + self.v[i,j+1]) * dx)# an
self.coef_u[i,j,4] = -(mu * dx / dy + 0.5 * rho * 0.5 * (self.v[i-1,j] + self.v[i,j]) * dx) # as
self.coef_u[i,j,0] = -(self.coef_u[i,j,1] + self.coef_u[i,j,2] + self.coef_u[i,j,3] +\
self.coef_u[i,j,4]) +\
rho * 0.5 * (self.u[i,j] + self.u[i+1,j]) * dy -\
rho * 0.5 * (self.u[i,j] + self.u[i-1,j]) * dy +\
rho * 0.5 * (self.v[i-1,j+1] + self.v[i,j+1]) * dx -\
rho * 0.5 * (self.v[i-1,j] + self.v[i,j]) * dx +\
rho * dx * dy / dt # ap
self.b_u[i,j] = (self.p[i-1,j] - self.p[i,j]) * dy + rho * dx * dy / dt * self.u0[i, j] # rhs
@ti.kernel
def compute_coef_v(self):
nx, ny, dx, dy, dt, rho, mu = self.nx, self.ny, self.dx, self.dy, self.dt, self.rho, self.mu
for i,j in ti.ndrange((1,nx+1),(2,ny+1)):
self.coef_v[i,j,1] = -(mu * dy / dx + 0.5 * rho * 0.5 * (self.u[i,j] + self.u[i,j-1]) * dy) # aw
self.coef_v[i,j,2] = -(mu * dy / dx - 0.5 * rho * 0.5 * (self.u[i+1,j-1] + self.u[i+1,j]) * dy) # ae
self.coef_v[i,j,3] = -(mu * dx / dy - 0.5 * rho * 0.5 * (self.v[i,j+1] + self.v[i,j]) * dx) # an
self.coef_v[i,j,4] = -(mu * dx / dy + 0.5 * rho * 0.5 * (self.v[i,j-1] + self.v[i,j]) * dx) # as
self.coef_v[i,j,0] = -(self.coef_v[i,j,1] + self.coef_v[i,j,2] + self.coef_v[i,j,3] +\
self.coef_v[i,j,4]) +\
rho * 0.5 * (self.u[i+1,j-1] + self.u[i+1,j]) * dy -\
rho * 0.5 * (self.u[i,j] + self.u[i,j-1]) * dy +\
rho * 0.5 * (self.v[i,j+1] + self.v[i,j]) * dx -\
rho * 0.5 * (self.v[i,j-1] + self.v[i,j]) * dx +\
rho * dx * dy / dt # ap
self.b_v[i,j] = (self.p[i,j-1] - self.p[i,j]) * dx + rho * dx * dy / dt * self.v0[i, j] # rhs
@ti.kernel
def compute_mdiv(self):
nx, ny, dx, dy, rho = self.nx, self.ny, self.dx, self.dy, self.rho
for i,j in ti.ndrange((1,nx+1),(1,ny+1)): # [1,nx], [1,ny]
self.mdiv[i,j] = rho * (self.u[i,j] - self.u[i+1,j]) * dy + rho * (self.v[i,j] - self.v[i,j+1]) * dx
@ti.kernel
def compute_coef_p(self):
nx, ny, dx, dy, rho = self.nx, self.ny, self.dx, self.dy, self.rho
for i,j in ti.ndrange((1,nx+1),(1,ny+1)): # [1,nx], [1,ny]
self.mdiv[i,j] = rho * (self.u[i,j] - self.u[i+1,j]) * dy + rho * (self.v[i,j] - self.v[i,j+1]) * dx
self.b_p[i,j] = self.mdiv[i,j]
self.coef_p[i,j,1] = -rho * dy * dy / self.coef_u[i,j,0] # aw
self.coef_p[i,j,2] = -rho * dy * dy / self.coef_u[i+1,j,0] # ae
self.coef_p[i,j,3] = -rho * dx * dx / self.coef_v[i,j+1,0] # an
self.coef_p[i,j,4] = -rho * dx * dx / self.coef_v[i,j,0] # as
if i == 1:
self.coef_p[i,j,1] = 0.0
if i == nx:
self.coef_p[i,j,2] = 0.0
if j == 1:
self.coef_p[i,j,4] = 0.0
if j == ny:
self.coef_p[i,j,3] = 0.0
self.coef_p[i,j,0] = - (self.coef_p[i,j,1] + self.coef_p[i,j,2] + self.coef_p[i,j,3] + self.coef_p[i,j,4])
#self.coef_p[1,1,1] = 0.0
#self.coef_p[1,1,2] = 0.0
#self.coef_p[1,1,3] = 0.0
#self.coef_p[1,1,4] = 0.0
#self.coef_p[1,1,0] = 1.0
#self.b_p[1,1] = 0.0
@ti.kernel
def set_bc(self):
nx, ny, bc = self.nx, self.ny, self.bc
# u - [nx+3, ny+2] - i E [0,nx+2], j E [0,ny+1]
# v - [nx+2, ny+3] - i E [0,nx+1], j E [0,ny+2]
for j in range(1,ny+1):
# u bc for w
self.b_u[2,j] += - self.coef_u[2,j,1] * bc['w'][0] # b += aw * u_inlet
self.coef_u[2,j,1] = 0.0 # aw = 0
self.u[1,j] = bc['w'][0] # u_inlet
# u bc for e
self.b_u[nx,j] += - self.coef_u[nx,j,2] * bc['e'][0] # b += ae * u_outlet
self.coef_u[nx,j,2] = 0.0 # ae = 0
self.u[nx+1,j] = bc['e'][0] # u_outlet
for i in range(1,nx+1):
# v bc for s
self.b_v[i,2] += - self.coef_v[i,2,4] * bc['s'][0] # b += as * v_inlet
self.coef_v[i,2,4] = 0.0 # as = 0
self.v[i,1] = bc['s'][0] # v_inlet
# v bc for n
self.b_v[i,ny] += - self.coef_v[i,ny,3] * bc['n'][0] # b += an * v_outlet
self.coef_v[i,ny,3] = 0.0 # an = 0
self.v[i,ny+1] = bc['n'][0] # v_outlet
for i in range(2,nx+1):
self.b_u[i,1] += 2 * self.mu * bc['s'][1] * self.dx / self.dy # South sliding wall
self.coef_u[i,1,0] += (self.coef_u[i,1,4] + 2 * self.mu * self.dx / self.dy)
self.coef_u[i,1,4] = 0.0
# ap = ap - as + 2mudx/dy
self.b_u[i,ny] += 2 * self.mu * bc['n'][1] * self.dx / self.dy # North sliding wall
self.coef_u[i,ny,0] += (self.coef_u[i,ny,3] + 2 * self.mu * self.dx / self.dy)
self.coef_u[i,ny,3] = 0.0
# ap = ap - an + 2mudx/dy
for j in range(2,ny+1):
self.b_v[1,j] += 2 * self.mu * bc['w'][1] * self.dy / self.dx # West sliding wall
self.coef_v[1,j,0] += (self.coef_v[1,j,1] + 2 * self.mu * self.dy / self.dx)
self.coef_v[1,j,1] = 0.0
self.b_v[nx,j] += 2 * self.mu * bc['e'][1] * self.dy / self.dx # East sliding wall
self.coef_v[nx,j,0] += (self.coef_v[nx,j,1] + 2 * self.mu * self.dy / self.dx)
self.coef_v[nx,j,2] = 0.0
@ti.kernel
def update_pressure(self):
nx, ny = self.nx, self.ny
for i,j in ti.ndrange((1,nx+1),(1,ny+1)):
self.p[i,j] += self.alpha_p * self.pcor[i,j]
@ti.kernel
def update_velocity(self):
nx, ny, dx, dy = self.nx, self.ny, self.dx, self.dy
for i,j in ti.ndrange((2,nx+1),(1,ny+1)):
#self.u[i,j] += self.alpha_u * (self.pcor[i-1,j] - self.pcor[i,j]) * dy / self.coef_u[i,j,0]
self.u[i,j] += self.alpha * (self.pcor[i-1,j] - self.pcor[i,j]) * dy / self.coef_u[i,j,0]
for i,j in ti.ndrange((1,nx+1),(2,ny+1)):
#self.v[i,j] += self.alpha_u * (self.pcor[i,j-1] - self.pcor[i,j]) * dx / self.coef_v[i,j,0]
self.v[i,j] += self.alpha * (self.pcor[i,j-1] - self.pcor[i,j]) * dx / self.coef_v[i,j,0]
def solve_momentum_eqn(self):
self.compute_coef_u()
self.compute_coef_v()
self.set_bc()
nx, ny, dx, dy = self.nx, self.ny, self.dx, self.dy
for i,j in ti.ndrange((2,nx+1),(1,ny+1)):
self.u_mid[i,j] = (- self.coef_u[i,j,1] * self.u[i-1,j] \
- self.coef_u[i,j,2] * self.u[i+1,j] \
- self.coef_u[i,j,3] * self.u[i,j+1] \
- self.coef_u[i,j,4] * self.u[i,j-1] \
+ self.b_u[i,j] ) / self.coef_u[i,j,0]
self.u[i,j] = (1 - self.alpha_u) * self.u[i,j] + self.alpha_u * self.u_mid[i,j]
for i,j in ti.ndrange((1,nx+1),(2,ny+1)):
self.v_mid[i,j] = (- self.coef_v[i,j,1] * self.v[i-1,j] \
- self.coef_v[i,j,2] * self.v[i+1,j] \
- self.coef_v[i,j,3] * self.v[i,j+1] \
- self.coef_v[i,j,4] * self.v[i,j-1] \
+ self.b_v[i,j] ) / self.coef_v[i,j,0]
self.v[i,j] = (1 - self.alpha_u) * self.v[i,j] + self.alpha_u * self.v_mid[i,j]
def solve_pcorrection_eqn(self):
self.compute_coef_p()
nx, ny = self.nx, self.ny
for i,j in ti.ndrange((1,nx+1),(1,ny+1)):
self.pcor[i,j] = (- self.coef_p[i,j,1] * self.pcor[i-1,j] \
- self.coef_p[i,j,2] * self.pcor[i+1,j] \
- self.coef_p[i,j,3] * self.pcor[i,j+1] \
- self.coef_p[i,j,4] * self.pcor[i,j-1] \
+ self.mdiv[i,j]) / self.coef_p[i,j,0]
@ti.kernel
def init(self):
for i,j in self.u:
self.u[i,j] = 0.0
for i,j in self.v:
self.v[i,j] = 0.0
for i,j in self.p:
self.p[i,j] = 0.0
def solve(self):
self.init()
# self.v0.copy_from(self.v)
# self.u0.copy_from(self.u)
for step in range(60000):
self.solve_momentum_eqn()
#self.solve_momentum_eqn()
#self.solve_momentum_eqn()
#self.solve_momentum_eqn()
#self.solve_momentum_eqn()
#self.compute_mdiv()
#self.dump_matrix(step, 'mfin')
#self.dump_coef(step, 'mfin')
#self.disp.display(f'log/{step:06}-mfin.png')
self.solve_pcorrection_eqn()
self.update_pressure()
self.update_velocity()
self.compute_mdiv()
#self.dump_coef(step, 'corfin')
print(">>> Finished step", step)
if step % 100 == 1:
self.disp.display(f'log/{step:06}-corfin.png')
self.dump_matrix(step, 'corfin')
# Lid-driven Cavity
ssolver = SIMPLESolver(1.0, 1.0, 51, 51) # lx, ly, nx, ny
# Boundary conditions
#ssolver.bc['w'][0] = 1.0 # West Normal velocity
#ssolver.bc['w'][1] = 1.0 # West Tangential velocity
#ssolver.bc['e'][0] = 1.0 # East Normal velocity
# ssolver.bc['e'][1] = 0.0 # East Tangential velocity
# ssolver.bc['n'][0] = 0.0 # North Normal velocity
ssolver.bc['n'][1] = 1.0 # North Tangential velocity
# ssolver.bc['s'][0] = 0.0 # South Normal velocity
# ssolver.bc['s'][1] = 0.0 # South Tangential velocity
ssolver.solve()