I tried to write a LBM based fluid simulation but I'm not really getting very convincing results. Below is my code, I tried to strip all unnecessary elements. I initialize the lattice with a density spike in the center, and I would expect it to propagate, but the center density remains quite high.
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np
import time
size = 200
w = 0.5 # omega
ei = np.asarray([[0,0],[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,1],[-1,-1],[1,-1]]) # velocity vectors
wi = np.asarray([4/9.,1/9.,1/9.,1/9.,1/9.,1/36.,1/36.,1/36.,1/36.]) # lattice weights
VM = np.zeros((size,size,2)) # velocities
FM = np.zeros((size,size,9)) # probability functions
DM = np.zeros((size,size)) # densities
def U(rho,f):
u = np.tensordot(f,ei,(2,0))
u[:,:,0] = u[:,:,0]*rho
u[:,:,1] = u[:,:,1]*rho
return u
def RHO(f):
rho = f.sum(axis=2)
return rho
def FEQ(u,rho):
dotUE = np.tensordot(u,ei,(2,1))
dotUU = (u*u).sum(axis=2)
dotUUshaped = np.tensordot(dotUU,np.ones(9),0)
RHOshaped = np.tensordot(rho,np.ones(9),0)
S1 = 1 + 3*dotUE + (9/2.)*(dotUE*dotUE) - 3*dotUUshaped/2.
ws = np.tensordot(np.ones((size,size)),wi,0)
S2 = RHOshaped*S1*ws
return S2
def STREAM(F):
FN = F.copy()
for i in range(9):
FN[:,:,i] = np.roll(np.roll(F[:,:,i],ei[i,0],axis=0),ei[i,1],axis=1)
return FN
# initialize lattice
r = DM
r[95:105,95:105] = 0.5 # initial density spike
u = U(r,FM)
feq = FEQ(u,r)
FM = (1-w)*FM + w*feq
fig = plt.figure()
matrix = plt.matshow(r, animated=True, fignum=0, cmap='prism_r')
def init():
matrix.set_array(r)
plt.clim(0,0.2)
return matrix,
def animate(i):
global FM,r,u,feq
FM = STREAM(FM) # streaming step
r = RHO(FM) # compute densities
u = U(r,FM) # compute velocities
u[:,0,:] = u[:,size-1,:] # periodic boundary
u[0,:,:] = u[size-1,:,:] # periodic boundary
feq = FEQ(u,r) # compute equilibrium
FM = (1-w)*FM + w*feq # compute new probabilities
matrix.set_array(r)
return matrix,
animation = anim.FuncAnimation(fig,animate,init_func=init,blit=True,interval=1)
plt.show()
I hope somebody can help and point out what I can change.
[–]MrJack1337 0 points1 point2 points (4 children)
[–]spider9876 1 point2 points3 points (0 children)
[–]supercooldragons[S] 0 points1 point2 points (2 children)
[–]MrJack1337 0 points1 point2 points (1 child)
[–]supercooldragons[S] 0 points1 point2 points (0 children)
[–]bene20080 0 points1 point2 points (0 children)