# LBM边界初始化效率问题

``````@ti.kernel
def apply_bc2(self):
for jbc in ti.ndrange((self.ini_lv, self.ny)):
self.apply_bc_bound(0, jbc, 1, jbc, self.bc_type[0], 0) # inlet
self.apply_bc_bound(self.nx-1, jbc, self.nx-2, jbc, self.bc_type[2], 2) # outflow
for ibc in ti.ndrange(self.nx):
self.apply_bc_bound(ibc, 0, ibc, 1, self.bc_type[1], 1) # bottom
self.apply_bc_bound(ibc, self.ny-1, ibc, self.ny-2, self.bc_type[3], 3) # top
for inb, jnb in ti.ndrange(self.nx, self.ny):
for k in ti.ndrange(9):
ibc = self.e[k, 0]+inb
jbc = self.e[k, 1]+jnb
self.rho[ibc, jbc] = self.rho[inb, jnb]
self.u[ibc,jbc][0] = self.u[ibc,jbc][1] = 0
self.bound(ibc, jbc, inb, jnb, 0)
self.rho[ibc, jbc] = self.rho[inb, jnb]
self.u[ibc,jbc][0] = self.u[ibc,jbc][1] = 0
self.bound(ibc, jbc, inb, jnb, 2) # 动网格表面
else: self.u[inb,jnb][0] = self.u[inb,jnb][1] = 0

@ti.func
def bound(self, ibc, jbc, inb, jnb, type):
if type == 0:
for k in ti.static(range(9)):
self.f_old[ibc, jbc][k] = self.f_eq(ibc, jbc, k) + \
self.f_old[inb, jnb][k] - self.f_eq(inb, jnb, k)
elif type == 1: # 镜面反射
for k in ti.static(range(9)):
a = [0, 3, 2, 1, 4, 7, 8, 5, 6]
self.f_old[ibc, jbc][k] = self.f_eq(ibc, jbc, k) + \
self.f_old[inb, jnb][a[k]] - self.f_eq(inb, jnb, a[k])
elif type == 2: # 动网格表面
self.u[ibc, jbc][0] = self.u[ibc, jbc][1] = 0
for k in ti.static(range(9)):
self.f_old[ibc, jbc][k] = self.f_eq(ibc, jbc, k) + \
self.f_old[inb, jnb][k] - self.f_eq(inb, jnb, k) + 2*self.w[k]*self.rho[inb, jnb]*3*self.dz[inb]

@ti.func
def apply_bc_bound(self, ibc, jbc, inb, jnb, type, dr):
self.rho[ibc, jbc] = self.rho[inb, jnb]
if type == 0: # 狄利克雷边界
self.u[ibc, jbc][0] = self.bc_value[dr, 0]
self.u[ibc, jbc][1] = self.bc_value[dr, 1]
self.bound(ibc, jbc, inb, jnb, 0)
elif type == 1: # 黎曼边界
self.u[ibc, jbc][0] = self.u[inb, jnb][0]
self.u[ibc, jbc][1] = self.u[inb, jnb][1]
self.bound(ibc, jbc, inb, jnb, 0)
elif type == 2: # 对数流入口
self.u[ibc, jbc][0] = self.cal_in_u()[jbc]
self.u[ibc, jbc][1] = self.u[inb, jnb][1]
self.bound(ibc, jbc, inb, jnb, 0)
elif type == 3: # 镜面边界
self.u[ibc, jbc][0] = self.u[inb, jnb][0]
self.u[ibc, jbc][1] = self.bc_value[dr, 1]
self.bound(ibc, jbc, inb, jnb, 1)
``````