Skip to content
Snippets Groups Projects
Commit ddea6da8 authored by Markus Holzer's avatar Markus Holzer
Browse files

Added more documentation

parent 7c1a5ae9
No related branches found
No related tags found
1 merge request!93Dataclasses for method creation
Pipeline #34321 failed
......@@ -16,10 +16,10 @@ It even comes with an integrated Chapman Enskog analysis based on sympy!
Common test scenarios can be set up quickly:
```python
from lbmpy.scenarios import create_channel
from pystencils import Target
from lbmpy.session import *
ch = create_channel(domain_size=(300, 100, 100), force=1e-7, method="trt",
ch = create_channel(domain_size=(300, 100, 100), force=1e-7, method=Method.TRT,
equilibrium_order=2, compressible=True,
relaxation_rates=[1.97, 1.6], optimization={'target': Target.GPU})
```
......
%% Cell type:code id: tags:
``` python
from lbmpy.session import *
```
%% Cell type:markdown id: tags:
# Tutorial 03: Defining LB methods in *lbmpy*
## A) General Form
%% Cell type:markdown id: tags:
The lattice Boltzmann equation in its most general form is:
$$f_q(\mathbf{x} + \mathbf{c}_q \delta t, t+\delta t) = K\left( f_q(\mathbf{x}, t) \right)$$
with a discrete velocity set $\mathbf{c}_q$ (stencil) and a generic collision operator $K$.
So a lattice Boltzmann method can be fully defined by picking a stencil and a collision operator.
The collision operator $K$ has the following structure:
- Transformation of particle distribution function $f$ into collision space. This transformation has to be invertible and may be nonlinear.
- The collision operation is an convex combination of the pdf representation in collision space $c$ and some equilibrium vector $c^{(eq)}$. This equilibrium can also be defined in physical space, then $c^{(eq)} = C( f^{(eq)} ) $. The convex combination is done elementwise using a diagonal matrix $S$ where the diagonal entries are the relaxation rates.
- After collision, the collided state $c'$ is transformed back into physical space
![](../img/collision.svg)
The full collision operator is:
$$K(f) = C^{-1}\left( (I-S)C(f) + SC(f^{(eq}) \right)$$
or
$$K(f) = C^{-1}\left( C(f) - S (C(f) - C(f^{(eq})) \right)$$
%% Cell type:markdown id: tags:
## B) Moment-based relaxation
The most commonly used LBM collision operator is the multi relaxation time (MRT) collision.
In MRT methods the collision space is spanned by moments of the distribution function. This is a very natural approach, since the pdf moments are the quantities that go into the Chapman Enskog analysis that is used to show that LB methods can solve the Navier Stokes equations. Also the lower order moments correspond to the macroscopic quantities of interest (density/pressure, velocity, shear rates, heat flux). Furthermore the transformation to collision space is linear in this case, simplifying the collision equations:
$$K(f) = C^{-1}\left( C(f) - S (C(f) - C(f^{(eq})) \right)$$
$$K(f) = f - \underbrace{ C^{-1}SC}_{A}(f - f^{(eq)})$$
in *lbmpy* the following formulation is used, since it is more natural to define the equilibrium in moment-space instead of physical space:
$$K(f) = f - C^{-1}S(Cf - c^{(eq)})$$
%% Cell type:markdown id: tags:
### Use a pre-defined method
Lets create a moment-based method in *lbmpy* and see how the moment transformation $C$ and the relaxation rates that comprise the diagonal matrix $S$ can be defined. We start with a function that creates a basic MRT model.
Don't use this for real simulations, there orthogonalized MRT methods should be used, as discussed in the next section.
%% Cell type:code id: tags:
``` python
from lbmpy.creationfunctions import create_lb_method
lbm_config = LBMConfig(stencil=Stencil.D2Q9, method=Method.MRT_RAW)
method = create_lb_method(lbm_config=lbm_config)
# check also method='srt', 'trt', 'mrt'
method
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d442c0fa0>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49aed671c0>
%% Cell type:markdown id: tags:
The first column labeled "Moment" defines the collision space and thus the transformation matrix $C$.
The remaining columns specify the equilibrium vector in moment space $c^{(eq)}$ and the corresponding relaxation rate.
Each row of the "Moment" column defines one row of $C$. In the next cells this matrix and the discrete velocity set (stencil) of our method are shown. Check for example the second last row of the table $x^2 y$: In the corresponding second last row of the moment matrix $C$ where each column stands for a lattice velocity (for ordering visualized stencil below) and each entry is the expression $x^2 y$ where $x$ and $y$ are the components of the lattice velocity.
In general the transformation matrix $C_{iq}$ is defined as;
$$c_i = C_{iq} f_q = \sum_q m_i(c_q)$$
where $m_i(c_q)$ is the $i$'th moment polynomial where $x$ and $y$ are substituted with the components of the $q$'th lattice velocity
%% Cell type:code id: tags:
``` python
# Transformation matrix C
method.moment_matrix
```
%% Output
$\displaystyle \left[\begin{matrix}1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\0 & 0 & 0 & -1 & 1 & -1 & 1 & -1 & 1\\0 & 1 & -1 & 0 & 0 & 1 & 1 & -1 & -1\\0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 & 1\\0 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 1\\0 & 0 & 0 & 0 & 0 & -1 & 1 & 1 & -1\\0 & 0 & 0 & 0 & 0 & 1 & 1 & -1 & -1\\0 & 0 & 0 & 0 & 0 & -1 & 1 & -1 & 1\\0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 & 1\end{matrix}\right]$
⎡1 1 1 1 1 1 1 1 1 ⎤
⎢ ⎥
⎢0 0 0 -1 1 -1 1 -1 1 ⎥
⎢ ⎥
⎢0 1 -1 0 0 1 1 -1 -1⎥
⎢ ⎥
⎢0 0 0 1 1 1 1 1 1 ⎥
⎢ ⎥
⎢0 1 1 0 0 1 1 1 1 ⎥
⎢ ⎥
⎢0 0 0 0 0 -1 1 1 -1⎥
⎢ ⎥
⎢0 0 0 0 0 1 1 -1 -1⎥
⎢ ⎥
⎢0 0 0 0 0 -1 1 -1 1 ⎥
⎢ ⎥
⎣0 0 0 0 0 1 1 1 1 ⎦
%% Cell type:code id: tags:
``` python
method.stencil.plot()
```
%% Output
%% Cell type:code id: tags:
``` python
method.stencil
```
%% Output
<lbmpy.stencils.LBStencil at 0x7f0d442f2a90>
<lbmpy.stencils.LBStencil at 0x7f49aedd27c0>
%% Cell type:markdown id: tags:
### Orthogonal MRTs
For a real MRT method, the moments should be orthogonalized.
One can either orthogonalize using the standard scalar product or a scalar product that is weighted with the lattice weights. If unsure, use the weighted version.
The next cell shows how to get both orthogonalized MRT versions in lbmpy.
%% Cell type:code id: tags:
``` python
lbm_config = LBMConfig(stencil=Stencil.D2Q9, method=Method.MRT, weighted=True)
weighted_ortho_mrt = create_lb_method(lbm_config=lbm_config)
weighted_ortho_mrt
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d39ecaee0>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49aea33850>
%% Cell type:code id: tags:
``` python
lbm_config = LBMConfig(stencil=Stencil.D2Q9, method=Method.MRT, weighted=False)
ortho_mrt = create_lb_method(lbm_config=lbm_config)
ortho_mrt
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d39f23760>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49aeb64fa0>
%% Cell type:markdown id: tags:
One can check if a method is orthogonalized:
%% Cell type:code id: tags:
``` python
ortho_mrt.is_orthogonal, weighted_ortho_mrt.is_weighted_orthogonal
```
%% Output
(True, True)
%% Cell type:markdown id: tags:
### Central moment lattice Boltzmann methods
%% Cell type:markdown id: tags:
Another popular method is the cascaded lattice Boltzmann method. The cascaded LBM increases the numerical stability by shifting the collision step to the central moment space. Thus it is applied in the non-moving frame and achieves a better Galilean invariance. Typically the central moment collision operator is derived for compressible LB methods, and a higher-order equilibrium is used. Although incompressible LB methods with a second-order equilibrium can be derived with lbmpy, it violates the Galilean invariance and thus reduces the advantages of the method.
%% Cell type:code id: tags:
``` python
lbm_config = LBMConfig(stencil=Stencil.D2Q9, method=Method.CENTRAL_MOMENT, equilibrium_order=4,
compressible=True)
central_moment_method = create_lb_method(lbm_config)
central_moment_method
```
%% Output
<lbmpy.methods.momentbased.centralmomentbasedmethod.CentralMomentBasedLbMethod at 0x7f0d39c84d90>
<lbmpy.methods.momentbased.centralmomentbasedmethod.CentralMomentBasedLbMethod at 0x7f49aeadb0a0>
%% Cell type:markdown id: tags:
The shift to the central moment space is done by applying a so-called shift matrix. Usually, this introduces a high numerical overhead. This problem is solved with lbmpy because each transformation stage can be specifically optimised individually. Therefore, it is possible to derive a CLBM with only a little numerical overhead.
%% Cell type:code id: tags:
``` python
central_moment_method.shift_matrix
```
%% Output
$\displaystyle \left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\- u_{0} & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\- u_{1} & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\u_{0} u_{1} & - u_{1} & - u_{0} & 1 & 0 & 0 & 0 & 0 & 0\\u_{0}^{2} - u_{1}^{2} & - 2 u_{0} & 2 u_{1} & 0 & 1 & 0 & 0 & 0 & 0\\u_{0}^{2} + u_{1}^{2} & - 2 u_{0} & - 2 u_{1} & 0 & 0 & 1 & 0 & 0 & 0\\- u_{0}^{2} u_{1} & 2 u_{0} u_{1} & u_{0}^{2} & - 2 u_{0} & - \frac{u_{1}}{2} & - \frac{u_{1}}{2} & 1 & 0 & 0\\- u_{0} u_{1}^{2} & u_{1}^{2} & 2 u_{0} u_{1} & - 2 u_{1} & \frac{u_{0}}{2} & - \frac{u_{0}}{2} & 0 & 1 & 0\\u_{0}^{2} u_{1}^{2} & - 2 u_{0} u_{1}^{2} & - 2 u_{0}^{2} u_{1} & 4 u_{0} u_{1} & - \frac{u_{0}^{2}}{2} + \frac{u_{1}^{2}}{2} & \frac{u_{0}^{2}}{2} + \frac{u_{1}^{2}}{2} & - 2 u_{1} & - 2 u_{0} & 1\end{matrix}\right]$
⎡ 1 0 0 0 0 0 0 0
⎢ -u₀ 1 0 0 0 0 0 0
⎢ -u₁ 0 1 0 0 0 0 0
⎢ u₀⋅u₁ -u₁ -u₀ 1 0 0 0 0
⎢ 2 2
⎢u₀ - u₁ -2⋅u₀ 2⋅u₁ 0 1 0 0 0
⎢ 2 2
⎢u₀ + u₁ -2⋅u₀ -2⋅u₁ 0 0 1 0 0
⎢ 2 2 -u₁ -u₁
⎢ -u₀ ⋅u₁ 2⋅u₀⋅u₁ u₀ -2⋅u₀ ──── ──── 1 0
⎢ 2 2
⎢ 2 2 u₀ -u₀
⎢ -u₀⋅u₁ u₁ 2⋅u₀⋅u₁ -2⋅u₁ ── ──── 0 1
⎢ 2 2
⎢ 2 2 2 2
⎢ 2 2 2 2 u₀ u₁ u₀ u₁
⎢ u₀ ⋅u₁ -2⋅u₀⋅u₁ -2⋅u₀ ⋅u₁ 4⋅u₀⋅u₁ - ─── + ─── ─── + ─── -2⋅u₁ -2⋅u
⎣ 2 2 2 2
0⎤
0⎥
0⎥
0⎥
0⎥
0⎥
0⎥
0⎥
₀ 1⎥
%% Cell type:markdown id: tags:
### Define custom MRT method
To choose custom values for the left moment column one can pass a nested list of moments.
Moments that should be relaxed with the same paramter are grouped together.
*lbmpy* also comes with a few templates for this list taken from literature:
%% Cell type:code id: tags:
``` python
from lbmpy.methods import mrt_orthogonal_modes_literature
from lbmpy.stencils import get_stencil
from lbmpy.moments import MOMENT_SYMBOLS
x, y, z = MOMENT_SYMBOLS
moments = mrt_orthogonal_modes_literature(LBStencil(Stencil.D2Q9), is_weighted=True)
moments
```
%% Output
$\displaystyle \left[ \left[ 1\right], \ \left[ x, \ y\right], \ \left[ 3 x^{2} + 3 y^{2} - 2, \ x^{2} - y^{2}, \ x y\right], \ \left[ x \left(3 x^{2} + 3 y^{2} - 4\right), \ y \left(3 x^{2} + 3 y^{2} - 4\right)\right], \ \left[ - 15 x^{2} - 15 y^{2} + 9 \left(x^{2} + y^{2}\right)^{2} + 2\right]\right]$
⎢ ⎡ 2 2 2 2 ⎤ ⎡ ⎛ 2 2 ⎞ ⎛ 2
⎣[1], [x, y], ⎣3⋅x + 3⋅y - 2, x - y , x⋅y⎦, ⎣x⋅⎝3⋅x + 3⋅y - 4⎠, y⋅⎝3⋅x +
⎡ 2 ⎤⎤
2 ⎞⎤ ⎢ 2 2 ⎛ 2 2⎞ ⎥⎥
3⋅y - 4⎠⎦, ⎣- 15⋅x - 15⋅y + 9⋅⎝x + y ⎠ + 2⎦⎦
%% Cell type:markdown id: tags:
This nested moment list can be passed to `create_lb_method`:
%% Cell type:code id: tags:
``` python
create_lb_method(LBMConfig(stencil=Stencil.D2Q9, method=Method.MRT, nested_moments=moments))
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d39cceac0>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49ae9359d0>
%% Cell type:markdown id: tags:
If one needs to also specify custom equilibrium moments the following approach can be used
%% Cell type:code id: tags:
``` python
rho = sp.symbols("rho")
u = sp.symbols("u_:3")
omega = sp.symbols("omega_:4")
method_table = [
# Conserved moments
(1, rho, 0 ),
(x, u[0], 0 ),
(y, u[1], 0 ),
# Shear moments
(x*y, u[0]*u[1], omega[0]),
(x**2-y**2, u[0]**2 - u[1]**2, omega[0]),
(x**2+y**2, 2*rho/3 + u[0]**2 + u[1]**2, omega[1]),
# Higher order
(x * y**2, u[0]/3, omega[2]),
(x**2 * y, u[1]/3, omega[2]),
(x**2 * y**2, rho/9 + u[0]**2/3 + u[1]**2/3, omega[3]),
]
method = create_generic_mrt(LBStencil(Stencil.D2Q9), method_table)
method
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d39bf1c10>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49ae8e9c10>
%% Cell type:markdown id: tags:
Instead of manually defining all entries in the method table, *lbmpy* has functions to fill the table according to a specific pattern. For example:
- for a full stencil (D2Q9, D3Q27) there exist exactly 9 or 27 linearly independent moments. These can either be taken as they are, or orthogonalized using Gram-Schmidt, weighted Gram-Schmidt or a Hermite approach
- equilibrium values can be computed from the standard discrete equilibrium of order 1,2 or 3. Alternatively they can also be computed as continuous moments of a Maxwellian distribution
One option is to start with one of *lbmpy*'s built-in methods and modify it with `create_lb_method_from_existing`.
In the next cell we fix the fourth order relaxation rate to a constant, by writing a function that defines how to alter each row of the collision table. This is for demonstration only, of course we could have done it right away when passing in the collision table.
%% Cell type:code id: tags:
``` python
def modification_func(moment, eq, rate):
if rate == omega[3]:
return moment, eq, 1.0
return moment, eq, rate
method = create_lb_method_from_existing(method, modification_func)
method
```
%% Output
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f0d39bfbc70>
<lbmpy.methods.momentbased.momentbasedmethod.MomentBasedLbMethod at 0x7f49ae8ca190>
%% Cell type:markdown id: tags:
Our customized method can be directly passed into one of the scenarios. We can for example set up a channel flow with it. Since we used symbols as relaxation rates, we have to pass them in as `kernel_params`.
%% Cell type:code id: tags:
``` python
ch = create_channel(domain_size=(100, 30), lb_method=method, u_max=0.05,
kernel_params={'omega_0': 1.8, 'omega_1': 1.4, 'omega_2': 1.5})
ch.run(500)
plt.figure(dpi=200)
plt.vector_field(ch.velocity[:, :]);
```
%% Output
%% Cell type:markdown id: tags:
#### Bonus: Automatic analysis
Above we have created a non-orthogonal MRT, where the shear viscosity and bulk viscosity can be independently controlled. For moment-based methods, *lbmpy* also offers an automatic Chapman Enskog analysis that can find the relation between viscosity and relaxation rate(s):
%% Cell type:code id: tags:
``` python
from lbmpy.chapman_enskog import ChapmanEnskogAnalysis
analysis = ChapmanEnskogAnalysis(method)
analysis.get_dynamic_viscosity()
```
%% Output
$\displaystyle - \frac{\omega_{0} - 2}{6 \omega_{0}}$
-(ω₀ - 2)
──────────
6⋅ω₀
%% Cell type:code id: tags:
``` python
analysis.get_bulk_viscosity()
```
%% Output
$\displaystyle - \frac{1}{9} - \frac{1}{3 \omega_{1}} + \frac{5}{9 \omega_{0}}$
1 1 5
- ─ - ──── + ────
9 3⋅ω₁ 9⋅ω₀
......
......@@ -9,9 +9,11 @@ Method parameters
General:
- ``stencil='D2Q9'``: stencil name e.g. 'D2Q9', 'D3Q19'. See :func:`pystencils.stencils.get_stencil` for details
- ``method='srt'``: name of lattice Boltzmann method. This determines the selection and relaxation pattern of
moments/cumulants, i.e. which moment/cumulant basis is chosen, and which of the basis vectors are relaxed together
- ``stencil=Stencil.D2Q9``: All stencils are defined in :class: `lbmpy.enums.Stencil`.
From that :class:`lbmpy.stencils.LBStenil` class will be created.
- ``method=Method.SRT``: name of lattice Boltzmann method. Defined by :class: `lbmpy.enums.Method`.
This determines the selection and relaxation pattern of moments/cumulants, i.e. which moment/cumulant
basis is chosen, and which of the basis vectors are relaxed together
- ``srt``: single relaxation time (:func:`lbmpy.methods.create_srt`)
- ``trt``: two relaxation time, first relaxation rate is for even moments and determines the viscosity (as in SRT),
......@@ -46,10 +48,7 @@ General:
compressible.
- ``equilibrium_order=2``: order in velocity, at which the equilibrium moment/cumulant approximation is
truncated. Order 2 is sufficient to approximate Navier-Stokes
- ``force_model=None``: possible values: ``None``, ``'simple'``, ``'luo'``, ``'guo'``/``'schiller'``,
``'buick'``/``'silva'``, ``'edm'``/``'kupershtokh'``, ``'he'``, ``'shanchen'``, ``'cumulant'``,
or an instance of a class implementing the same methods as the classes in :mod:`lbmpy.forcemodels`.
For details, see :mod:`lbmpy.forcemodels`
- ``force_model=None``: possibilities are defined in :class: `lbmpy.enums.ForceModel`
- ``force=(0,0,0)``: either constant force or a symbolic expression depending on field value
- ``maxwellian_moments=True``: way to compute equilibrium moments/cumulants, if False the standard
discretized LBM equilibrium is used, otherwise the equilibrium moments are computed from the continuous Maxwellian.
......@@ -182,6 +181,11 @@ Kernel functions are created in three steps:
The function :func:`create_lb_function` runs the whole pipeline, the other functions in this module
execute this pipeline only up to a certain step. Each function optionally also takes the result of the previous step.
Each of those functions is configured with three data classes. One to define the lattice Boltzmann method itself.
This class is called `LBMConfig`. The second one determines optimisations which are specific to the LBM. Optimisations
like the common supexpression elimination. The config class is called `LBMOptimisation`. The third
data class determines hardware optimisation. This can be found in the pystencils module as `CreateKernelConfig`.
With this class for example the target of the generated code is specified.
For example, to modify the AST one can run::
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment