NumMeth-22: LBM fertig

This commit is contained in:
2023-01-12 07:41:43 +01:00
parent 847a1c68e2
commit a0b91f392d
2 changed files with 74 additions and 7 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

+74 -7
View File
@@ -6417,6 +6417,8 @@ To give a rough idea of how Eq. \eqref{eq:lbm9} propagates the occupation number
\section{Numerical implementation} \section{Numerical implementation}
\definecolor{mylgrey}{rgb}{0.8,0.8,0.8} \definecolor{mylgrey}{rgb}{0.8,0.8,0.8}
\definecolor{mygreen}{rgb}{0.1,0.1,0.9}
\definecolor{mygray}{rgb}{0.4,0.4,0.4}
\lstset{ \lstset{
language=Python, language=Python,
@@ -6452,17 +6454,16 @@ To give a rough idea of how Eq. \eqref{eq:lbm9} propagates the occupation number
The quantity that specifies the state of our simulation are the occupation num- The quantity that specifies the state of our simulation are the occupation num-
bers $f_i(\vec{x}_j )$. We represent these as a single contiguous \texttt{numpy} array called \texttt{f\_ikl}. Note that the suffixes in our specific naming scheme indicate the number of array bers $f_i(\vec{x}_j )$. We represent these as a single contiguous \texttt{numpy} array called \texttt{f\_ikl}. Note that the suffixes in our specific naming scheme indicate the number of array
dimensions and their function. In the present case, \texttt{ikl} stands for three array dimensions: \texttt{i} is a direction (array dimension of size 9), \texttt{k} is the lattice position in x-direction, and \texttt{l} (size $n_x$) the lattice position in y-direction (size $n_y$). Below we also encounter \texttt{c}, which denotes a Cartesian array dimension (size 2). This naming convention eases readability of the code and translation of formulas containing lin- dimensions and their function. In the present case, \texttt{ikl} stands for three array dimensions: \texttt{i} is a direction (array dimension of size 9), \texttt{k} is the lattice position in x-direction, and \texttt{l} (size $n_x$) the lattice position in y-direction (size $n_y$). Below we also encounter \texttt{c}, which denotes a Cartesian array dimension (size 2). This naming convention eases readability of the code and translation of formulas containing lin-
ear algebra into \texttt{numpy} operations and is borrowed from the GPAW code (Mortensen ear algebra into \texttt{numpy} operations and is borrowed from the GPAW code. Note that because numpy’s default storage order is row-major, the array \texttt{f\_ikl} is stored in what is commonly called the structure of
et al., 2005; Enkovaara et al., 2010). Note that because numpy’s default storage order arrays (SoA) storage order.
is row-major, the array \texttt{f\_ikl} is stored in what is commonly called the structure of
arrays (SoA) storage order (Obrecht et al., 2011; Qi, 2017).
The streaming part can be cast into the form listed in Code Listing 1, which The streaming part can be cast into the form listed in Code Listing 1, which
uses the numpy.roll function and automatically takes care of periodic boundary uses the numpy.roll function and automatically takes care of periodic boundary
conditions. numpy.roll rolls the data on the lattice into the direction specified conditions. numpy.roll rolls the data on the lattice into the direction specified
by axis in the code listing by a distance given by c_ic[i ] . Note that c_ic[i ] by axis in the code listing by a distance given by \texttt{c\_ic[i]}. Note that \texttt{c\_ic[i]}
is a one-dimensional array of length 2 that specifies the Cartesian coordinates of is a one-dimensional array of length 2 that specifies the Cartesian coordinates of
direction i. Taking the example of the f_ikl array, a roll operation on occupation direction i. Taking the example of the \texttt{f\_ikl} array, a roll operation on occupation
number 1 and axis 0 with unit distance will assign f1,k,l → f1,k+1,l. number 1 and axis 0 with unit distance will assign $f_{1,k,l} \to f_{1,k+1,l}$.
\begin{lstlisting}[frame=single] \begin{lstlisting}[frame=single]
import numpy as np import numpy as np
@@ -6472,6 +6473,72 @@ def stream(f_ikl):
f_ikl[i] = np.roll(f_ikl[i], c_ic[i], axis=(0, 1)) f_ikl[i] = np.roll(f_ikl[i], c_ic[i], axis=(0, 1))
\end{lstlisting} \end{lstlisting}
The collision part of Eq. \eqref{eq:lbm5} is also straightforward to implement. To translate
an expression like Eq. \eqref{eq:lbm9} into vectorized numpy code, it can be useful to write it
down in Einstein notation. Einstein notation can be cast directly in Python code
using the \texttt{numpy.einsum} function. Code Listing 2 shows a naive but straightforward
implementation of the collision step, split into the computation of the equilibrium
distribution function in equilibrium and the final collision step in collide, where
omega is the relaxation parameter $\omega$ of Eq. \eqref{eq:lbm5}.
\begin{lstlisting}[frame=single]
import numpy as np
w_i = np.array([4/9, 1/9, 1/9, 1/9, 1/9, 1/36, 1/36, 1/36, 1/36])
def equilibrium(rho_kl , u_ckl):
cu_ikl = np.dot(u_ckl.T, c_ic.T).T
uu_kl = np.sum(u_ckl ** 2, axis=0)
return (w_i*(rho_kl*(1 + 3*cu_ikl + 9/2*cu_ikl ** 2 - 3/2*uu_kl )).T).T
def collide(f_ikl , omega):
rho_kl = np.sum(f_ikl , axis=0)
u_ckl = np.dot(f_ikl.T, c_ic ).T/rho_kl
f_ikl += omega*(equilibrium(rho_kl , u_ckl) - f_ikl)
return rho_kl , u_ckl
\end{lstlisting}
It is worth pointing out that the collision implementation of Code Listing 2 does not
lead to optimal performance. The Python code can be further optimized by unrolling
all multiplications with \texttt{c\_ic} and eliminating the terms that vanish due to zeros
in \texttt{c\_ic}. This implementation is our optimized reference Python implementation.
\section{Lid-driven cavity}
The lid-driven cavity is frequently used to test fluid dynamics simulation programs.
Given a quadratic box with a sliding lid, as shown in Fig. \ref{fig:lbm3}a, we use equilibrium
initial conditions, Eq. \eqref{eq:lbm10}, for the discrete BGK-Boltzmann transport equations.
The initial values were chosen as $\rho = 1.0$ and $u = 0$ at time $t = 0$.
\begin{figure}
\begin{center}
\pgfimage[width=.8\textwidth]{fig_chapter9/lbm3}
\end{center}
\caption{Lid-driven cavity. (a) The system consists of a quadratic box with
hard walls drawn in black and a lid that slides to the right with a prescribed
velocity, drawn in red. The lattice points all lie inside the box and the walls
lie half way between boundary lattice points and (virtual) solid lattice points
outside the box. (b) Streamlines in the steady state for a domain of 300 × 300
lattice points and $\omega = 1.7$, corresponding to Reynolds number 1000, given the
typical velocity of the sliding lid as $u_\text{lid} = 0.1$. The figures is visualized using
the streamplot function of the matplotlib6library. The lattice is resampled
into a 30 × 30 lattice before determining the streamlines.}
\label{fig:lbm3}
\end{figure}
The lid moves with a given velocity $u_\text{lid}$ in direction 1, i. e. to the east. We apply
bounce-back boundary conditions on the black boundaries and the prescribed wall
velocity boundary conditions on the red wall. The full boundary conditions can be
written as
\begin{align}
f_i = f_{i^*} - 6 \omega_i \rho_\text{Wall} \vec{c}_i\cdot \vec{u}_\text{lid}
\end{align}
where $i^*$ indicates the direction before bounce-back and $\rho_\text{Wall}$ is the fluid density at the wall. Note that we apply bounce back boundary conditions to all parallel domains,
but we introduce ghost buffers only for domain boundaries in interior domains, not
for the outer boundaries of edge domains. Using this approach, we do not need
special treatment of boundary or interior domains.
We calculated the velocity field in the steady state. Fig. \ref{fig:lbm3}b shows the streamlines of the flow field ater 1 million time steps for a lattice of $300\times 300$ lattice points with $\omega=1.7$ and the velocity of the lid chosen as $u_\text{lid} = 0.1$. The sliding lid induces a large vortex rotating clock wise. The two lower corners show smalls vortices rotating in the opposite direction.
\bibliographystyle{natdin} \bibliographystyle{natdin}
\bibliography{./literatur.bib} \bibliography{./literatur.bib}