Python Thermal lattice Boltzmann model ignoring source term -- python code help please

AI Thread Summary
The discussion centers on implementing a Lattice Boltzmann Model (LBM) for simulating phase change in a thermal system using a D2Q5 lattice structure. The model addresses a one-dimensional, single-phase phase change scenario with specified boundary conditions, including constant temperature on the left wall and adiabatic conditions on the right. Key parameters include the number of nodes in the x and y directions, heat diffusion coefficient, and the number of time steps for simulation.The code initializes temperature and enthalpy matrices, along with a liquid fraction matrix to represent the phase state. The main loop consists of collision and streaming processes, where the distribution function is updated based on equilibrium calculations and boundary conditions. The model calculates temperature, enthalpy, and liquid fraction iteratively until convergence is achieved, monitoring errors to ensure accuracy. A division by zero error is noted in the code, indicating a potential issue in the phase change boundary detection logic.
Gwen
Messages
1
Reaction score
1
LBM model for phase change- relevant equations found here. Also here.

Code:
#Thermal LBM
#solves 1D 1 phase phase-change
#D2Q5 Lattice

nx=100                                   # the number of nodes in x direction lattice direction
ny=5                                    # the number of nodes in y direction lattice direction
alpha=.5/3                     # heat diffusion coefficient                                   # the dimension of the problem
mstep=1000                               # the number of time step
tau=3.*alpha+0.5

Tleft=0.0                                 # left wall temperature
Tright=1.0                                # right wall temperature
k=5 # k=0,1,2,3,4,5,6,8,9

x=numpy.linspace(0,1,nx+1) #start at zero, end at 1, fill with nx+1 even spaced intervals
y=numpy.linspace(0,1,ny+1)
t=np.zeros(mstep)
s=np.zeros(mstep)
w=numpy.ones(k)                              # witghting factor
T=numpy.ones((ny+1,nx+1) )         # Temperature matrix
f= numpy.ones((k, ny+1,nx+1))                # distribution function

Hl=1
Hs=0.5
H=numpy.ones((ny+1,nx+1) )                   # Enthalpy matrix
Fl=numpy.ones((ny+1,nx+1) )                  # Liquid fraction matrix (Fl=1 for liquid, Fl=0 for solid)

##================ Initial boundary condition
w[0]=1./3. #0.0
w[1:5]=1./6. #1./4.

##================== Initial value

T[0:ny+1,0:nx+1]=1.0   #temperature in the whole region (including bottom wall)
T[0:ny+1,0]=0        #temperature on the left wall
T[0:ny+1,nx]=1.0       #temperature one node in from the right wall
T[ny,1:nx]=1.0         #temp one node in from the top wall (and one node in from left and right sides)

for i in range(nx+1):
    for j in range(ny+1):
        for l in range (k): #k=0,1,2,3,4    
            f[l,j,i]=w[l]*T[j,i]
  
##   Main loop  : comprised two parts :collision and streaming
##=====================
for n in range(mstep) :
    t[n]=n  #track the time
    time=t[n]

    epsilon=1e-8
    error=1
    Fl_old=Fl
    while error>epsilon:
        Fl_old_iter=Fl
        T_old_iter=T

# collision process
# ==========================
        for i in range(nx+1):
            for j in range(ny+1):
                    for l in range (k):
                        feq=w[l]*T[j,i]  
                        f[l,j,i]=(1.-1/tau)*f[l,j,i]+(1/tau)*feq-w[l]*(Fl[j,i]-Fl_old[j,i])
 
 #streaming process
# ==========================
        for i in range(nx):
            for j in range(ny,0,-1):  #backwards from top to bottom
                f[2,j,i]=f[2,j-1,i]

        for i in range(nx,0,-1):   #backwards from right to left
            for j in range(ny,0,-1):  #backwards from top to bottom
                f[1,j,i]=f[1,j,i-1]

        for i in range(nx,0,-1):   #backwards from right to left
            for j in range(0,ny):     #forward from bottom to second-to-top lattice node
                f[4,j,i]=f[4,j+1,i]

        for i in range(0,nx):      #forward from left to second-to-right lattice node
            for j in range(0,ny):     #forward from bottom to second-to-top lattice node
                f[3,j,i]=f[3,j,i+1]

# Boundary conditions
#  =============================
        for j in range(0,ny+1) :               #left Boundary. Dirichlet boundary condition: constant temperature.
            f[1,j,0]=( Tleft*(w[1]+w[3]) )-f[3,j,0]

        for j in range(0,ny+1):                #right Boundary. adiabatic
            f[3,j,nx]=f[1,j,nx]

        for i in range(0,nx+1):                # bottom and top Boundary
            f[4,ny,i]=f[2,ny,i]                  #adiabatic
 #================================ #calculate temperature
        for i in range(nx+1):
            for j in range(ny+1):
                sum=0.0
                for l in range (k):
                    sum=sum+f[l,j,i]
                T[j,i]=sum
        T[0:ny+1,0]=Tleft           #Dirichlet BC      
        T[0:ny+1,nx]=T[0:ny+1,nx]   #adiabatic BC        
        T[ny,1:nx]=T[ny-1,1:nx]     #adiabatic BC        
        T[0,1:nx]=T[1,1:nx]         #adiabatic BC       
#==============================   #calculate nodal enthalpy and liquid fraction
        for i in range(nx+1):
            for j in range(ny+1):
                H[j,i]=0.5*T[j,i]+0.5*Fl[j,i]
        for i in range(nx+1):
            for j in range(ny+1):
                if H[j,i]<=Hs:
                    Fl[j,i]=0
                elif H[j,i]>Hs and H[j,i] < Hl:
                    Fl[j,i]=(H[j,i]-Hs)/(Hl-Hs)
                else:
                    Fl[j,i]=1
#==============================   #convergence? If no, go back
        for i in range(nx+1):
            for j in range(ny+1):
                error_Fl=abs(np.max(np.max((Fl[j,i]-Fl_old_iter[j,i])/Fl[j,i])))
                error_T=abs(np.max(np.max((T[j,i]-T_old_iter[j,i])/T[j,i])))
                error=np.max([error_Fl, error_T])      
#find position of phase change boundary (where Fl<=0.5)  
    Fl_col=Fl[3,:]<=0.5
    max = Fl_col[0]
    index = 0
    for i in range(1,len(Fl_col)):
        if Fl_col[I] >= 0.5:
            max = Fl_col
            index = i[/I][/I]
 
        s[n]=index/nx   
#==============================[/I][/I]
 
Last edited by a moderator:
Technology news on Phys.org
I get a division by zero error at line 119
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
2K
Replies
4
Views
5K
Replies
34
Views
3K
Replies
1
Views
4K
Replies
8
Views
6K
Replies
2
Views
3K
Back
Top