-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlbm_chapter5_6_D2Q5a.py
145 lines (136 loc) · 5.63 KB
/
lbm_chapter5_6_D2Q5a.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
import taichi as ti
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import matplotlib.cm as cm
ti.init(arch=ti.cpu)
@ti.data_oriented
class lbm_solver:
def __init__(self): # total steps to run
self.m = 201
self.n = 201
self.xl = 1.
self.yl = 1.
self.dx = self.xl/(self.m-1.)
self.dy = self.yl/(self.n-1.)
self.w = ti.field(dtype=ti.f32, shape=5)
self.e = ti.field(dtype=ti.i32, shape=(5, 2))
self.c2 = 1./3.
self.rho = ti.field(dtype=ti.f32, shape=(self.m,self.n))
self.f = ti.Vector.field(5, dtype=ti.f32, shape=(self.m,self.n))
self.f_old = ti.Vector.field(5, dtype=ti.f32, shape=(self.m,self.n))
self.flux = ti.field(dtype=ti.f32, shape=self.m)
self.fluxq = ti.field(dtype=ti.f32, shape=self.m)
self.Tm = ti.field(dtype=ti.f32, shape=self.m)
self.x = ti.field(dtype=ti.f32, shape=self.m)
self.y = ti.field(dtype=ti.f32, shape=self.n)
self.Z = ti.field(dtype=ti.f32, shape=(self.m,self.n))
self.alpha = 0.25
self.omega = 1./(3.*self.alpha+0.5)
self.twall = 1.0
self.nstep = 10000
for i in range(1, self.m):
self.x[i] = self.x[i-1]+self.dx
arr = np.array([1./3., 1./6., 1./6., 1./6., 1./6.], dtype=np.float32)
self.w.from_numpy(arr)
arr = np.array([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], dtype=np.int32)
self.e.from_numpy(arr)
@ti.kernel
def init(self):
pass
@ti.func
def feq(self,i,j,k):
return self.w[k] * self.rho[i,j]
@ti.kernel
def collision(self):
for i, j in ti.ndrange((0, self.m), (0, self.n)):
for k in ti.static(range(5)):
self.f_old[i,j][k] = (1-self.omega)*self.f[i,j][k]+self.omega*self.feq(i,j,k)
@ti.kernel
def stream(self):
# Streaming
for i, j in ti.ndrange((0, self.m), (0, self.n)):
for k in ti.static(range(5)):
ip = i - self.e[k, 0]
jp = j - self.e[k, 1]
if 0<=ip<self.m and 0<=jp<self.n:
self.f[i,j][k] = self.f_old[ip,jp][k]
# for i, j in ti.ndrange((0, self.m-1), (0, self.n)):
# self.f[self.m-i-1,j][1] = self.f_old[self.m-i-2,j][1]
# self.f[i,j][2] = self.f_old[i+1,j][2]
# for i, j in ti.ndrange((0, self.m), (0, self.n-1)):
# self.f[i,self.n-j-1][3] = self.f_old[i,self.n-j-2][3]
# self.f[i,j][4] = self.f_old[i,j+1][4]
@ti.kernel
def boundary(self):
# Boundary conditions
for j in ti.ndrange(self.n):
self.f[0,j][1] = self.twall - self.f[0,j][0] - self.f[0,j][2]- self.f[0,j][3]- self.f[0,j][4]
self.f[self.m-1,j][3] = - self.f[self.m-1,j][0] - self.f[self.m-1,j][1]- self.f[self.m-1,j][2]- self.f[self.m-1,j][4]
for i in ti.ndrange(self.m):
self.f[i,0][3] = self.f[i,1][3]
self.f[i,self.n-1][4] = -self.f[i,self.n-1][0]-self.f[i,self.n-1][1]-self.f[i,self.n-1][2]-self.f[i,self.n-1][3]
@ti.kernel
def update(self):
for i, j in ti.ndrange((0, self.m), (0, self.n)):
self.rho[i,j] = self.f[i,j][0] + self.f[i,j][1] + self.f[i,j][2]+ self.f[i,j][3]+ self.f[i,j][4]
# self.flux[i] = self.tk[i]*self.omega[i]*(self.f[i][1]-self.f[i][2])/self.c2
for i in range(self.m):
self.Tm[i] = self.rho[i,(self.n-1)/2]
@ti.kernel
def post(self):
for i, j in ti.ndrange((0, self.m), (0, self.n)):
self.Z[j,i] = self.rho[i,j]
# for i in range(self.m-1):
# self.fluxq[i] = self.tk[i]*(self.rho[i] - self.rho[i+1])
# self.fluxq[self.m-1] = self.fluxq[self.m-2]
def plot(self):
plt.subplot(1, 2, 1)
plt.plot(self.x.to_numpy(), self.Tm.to_numpy())
plt.title("Temperature")
plt.xlabel("X")
plt.ylabel("T")
plt.subplot(1, 2, 2)
X, TM = np.meshgrid(self.x.to_numpy(), self.Tm.to_numpy())
a = plt.contourf(X, TM, self.Z.to_numpy(), 10, cmap=plt.cm.Spectral)
b = plt.contour(X, TM, self.Z.to_numpy(), 10, colors='black', linewidths=1, linestyles='solid')
plt.colorbar(a, ticks=[i for i in np.linspace(0,1,10)])
plt.clabel(b, inline=True, fontsize=10)
# plt.plot(self.x.to_numpy(), self.flux.to_numpy(), 'x')
# plt.plot(self.x.to_numpy(), self.fluxq.to_numpy(), 'o')
# plt.title("fluxq")
# plt.xlabel("X")
# plt.ylabel("T")
plt.show()
@ti.pyfunc
def gui_plot(self):
rho = self.rho.to_numpy()
rho_img = cm.plasma(rho)
self.gui.set_image(rho_img)
pos = np.stack((self.x.to_numpy(), (self.Tm.to_numpy())), axis=-1)
self.gui.circles(pos, color=0x000000, radius=3)
# pos = np.stack((self.x.to_numpy()/200+0.5,
# self.flux.to_numpy()/1), axis=-1)
# self.gui.circles(pos, color=0x00FFFF, radius=5)
# pos = np.stack((self.x.to_numpy()/200+0.5,
# self.fluxq.to_numpy()/1), axis=-1)
# self.gui.circles(pos, color=0x000000, radius=5)
self.gui.show()
def solve(self):
self.init()
self.gui = ti.GUI('Diffusion equation D1Q2', res=(
201, 201), background_color=0xFFFFFF)
self.gui.fps_limit = 500
for k in tqdm(range(self.nstep)):
self.collision()
self.stream()
self.boundary()
self.update()
self.post()
self.gui_plot()
self.plot()
if __name__ == '__main__':
lbm = lbm_solver()
lbm.solve()
print(lbm.Tm)
print(lbm.rho)