{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from lbmpy.session import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 04: The Cumulant Lattice Boltzmann Method in lbmpy\n", "\n", "## A) Principles of the centered cumulant collision operator\n", "\n", "Recently, an advanced Lattice Boltzmann collision operator based on relaxation in cumulant space has gained popularity. Similar to moments, cumulants are statistical quantities inherent to a probability distribution. A significant advantage of the cumulants is that they are statistically independent by construction. Moments can be defined by using the so-called moment generating function, which for the discrete particle distribution of the LB equation is stated as\n", "\n", "\n", "\\begin{align}\n", " M( \\mathbf{X} ) = \n", " \\sum_i f_i \n", " \\exp \\left(\n", " \\mathbf{c}_i \\cdot \\mathbf{X}\n", " \\right)\n", "\\end{align}\n", "\n", "\n", "The raw moments $m_{\\alpha \\beta \\gamma}$ can be expressed as its derivatives, evaluated at zero:\n", "\n", "\n", "\\begin{align}\n", " m_{\\alpha \\beta \\gamma} \n", " = \n", " \\left.\n", " \\frac{\\partial^{\\alpha}}{\\partial X^{\\alpha}}\n", " \\frac{\\partial^{\\beta}}{\\partial Y^{\\beta}}\n", " \\frac{\\partial^{\\gamma}}{\\partial Z^{\\gamma}}\n", " M(X, Y, Z) \n", " \\right\\vert_{\\mathbf{X}=0}\n", "\\end{align}\n", "\n", "\n", "The cumulant-generating function is defined as the natural logarithm of this moment-generating function, and the cumulants $c_{\\alpha \\beta \\gamma}$ are defined as its derivatives evaluated at zero:\n", "\n", "\n", "\\begin{align}\n", " C(\\mathbf{X}) :=& \\, \\log ( M(\\mathbf{X}) ) \\\\\n", " c_{\\alpha \\beta \\gamma} \n", " =& \\,\n", " \\left.\n", " \\frac{\\partial^{\\alpha}}{\\partial X^{\\alpha}}\n", " \\frac{\\partial^{\\beta}}{\\partial Y^{\\beta}}\n", " \\frac{\\partial^{\\gamma}}{\\partial Z^{\\gamma}}\n", " C(X, Y, Z) \n", " \\right\\vert_{\\mathbf{X}=0}\n", "\\end{align}\n", "\n", "\n", "Other than with moments, there is no straightforward way to express cumulants in terms of the populations. However, their generating functions can be related to allowing the computation of cumulants from both raw and central moments, computed from populations. In lbmpy, the transformations from populations to cumulants and back are implemented using central moments as intermediaries. This is done for two primary reasons:\n", " 1. All cumulants of orders 2 and 3 are equal to their corresponding central moments, up to the density $\\rho$ as a proportionality factor.\n", " 2. The conserved modes of the first order, which correspond to momentum, are relaxed in central moment space to allow for a more efficient implicit forcing scheme.\n", "\n", "The central moment-generating function $K$ can be related to the moment-generating function through $K(\\mathbf{X}) = \\exp( - \\mathbf{X} \\cdot \\mathbf{u} ) M(\\mathbf{X})$. It is possible to recombine the equation with the definition of the cumulant-generating function\n", "\n", "\n", "\\begin{align}\n", " C( \\mathbf{X} ) = \\mathbf{X} \\cdot \\mathbf{u} + \\log( K( \\mathbf{X} ) ).\n", "\\end{align}\n", "\n", "\n", "Derivatives of $C$ can thus be expressed in terms of derivatives of $K$, directly yielding equations of the cumulants in terms of central moments.\n", "\n", "In the cumulant collision operator, forces can be applied through a simple forcing scheme which in lbmpy is called *implicit forcing*. Ony the force contributions to the first-order (momentum) modes are considered. When the first-order central moments are taken in the frame of reference shifted by $F/2$, they trail behind zero by just that much. Forcing can be applied by first adding half the force *before* collision, and then adding the remaining half *after* collision. Due to this, the first-order central moments entering the cumulant equations are always zero. By applying the force already in central-moment space, the cumulant equations are simplified significantly as no forces need to be taken into account. The pre- and post-collision momentum central moments take the form:\n", "\n", "\n", "\\begin{align}\n", " \\kappa_{100} &= - \\frac{F_x}{2}, \\\\ \n", " \\kappa^{\\ast}_{100} &= \\kappa_{100} + F_x = \\frac{F_x}{2}\n", "\\end{align}\n", "\n", "\n", "In total, through forcing, the first central moments change sign. This is equivalent to relaxation with a relaxation rate $\\omega = 2$. For this reason, lbmpy's implementation of the cumulant LBM calculates the collision of the momentum modes in central moment space, and the default force model overrides their relaxation rate by setting it to 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## B) Method Creation\n", "\n", "Using the create_lb_method interface, creation of a cumulant-based lattice Boltzmann method in lbmpy is straightforward. Cumulants can either be relaxed in their raw (monomial forms) or in polynomial combinations. Both variants are available as predefined setups. **Attention:** Cumulant-based methods are only available for the *compressible* equilibrium.\n", "\n", "### Relaxation of Monomial Cumulants\n", "\n", "Monomial cumulant relaxation is available through the method=\"monomial_cumulant\" parameter setting. This variant requires fewer transformation steps and is a little faster than polynomial relaxation, but it does not allow separation of bulk and shear viscosity. Default monomial cumulant sets are available for the D2Q9, D3Q19 and D3Q27 stencils. It is also possible to define a custom set of monomial cumulants.\n", "\n", "When creating a monomial cumulant method, one option is to specify only a single relaxation rate which will then be assigned to all cumulants related to the shear viscosity. In this case, all other non-conserved cumulants will be relaxed to equilibrium. Alternatively, individual relaxation rates for all non-conserved cumulants can be specified. The conserved cumulants are set to zero by default to save computational cost. They can be adjusted with set_zeroth_moment_relaxation_rate, set_first_moment_relaxation_rate and set_conserved_moments_relaxation_rate." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Cumulant-Based Method\n", " Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Continuous Hydrodynamic Maxwellian Equilibrium\n", " \n", " $f (\\rho, \\left( u_{0}, \\ u_{1}\\right), \\left( v_{0}, \\ v_{1}\\right)) \n", " = \\frac{3 \\rho e^{- \\frac{3 \\left(- u_{0} + v_{0}\\right)^{2}}{2} - \\frac{3 \\left(- u_{1} + v_{1}\\right)^{2}}{2}}}{2 \\pi}$\n", " Compressible: ✓ Deviation Only: ✗ Order: ∞
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "
Relaxation Info
CumulantEq. Value Relaxation Rate
$1 (central moment)$$\\rho$$0$
$x (central moment)$$0$$0$
$y (central moment)$$0$$0$
$x^{2}$$\\frac{\\rho}{3}$$\\omega_{v}$
$y^{2}$$\\frac{\\rho}{3}$$\\omega_{v}$
$x y$$0$$\\omega_{v}$
$x^{2} y$$0$$1$
$x y^{2}$$0$$1$
$x^{2} y^{2}$$0$$1$
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lbm_config = LBMConfig(method=Method.MONOMIAL_CUMULANT, relaxation_rate=sp.Symbol('omega_v'), compressible=True)\n", "method_monomial = create_lb_method(lbm_config=lbm_config)\n", "method_monomial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Relaxation of Polynomial Cumulants\n", "\n", "By setting method=\"cumulant\", a set of default polynomial cumulants is chosen to be relaxed. Those cumulants are taken from literature and assembled into groups selected to enforce rotational invariance (see: lbmpy.methods.centeredcumulant.centered_cumulants). Default polynomial groups are available for the D2Q9, D3Q19 and D3Q27 stencils. As before it is possible to specify only a single relaxation rate assigned to the moments governing the shear viscosity. All other relaxation rates are then automatically set to one." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Cumulant-Based Method\n", " Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Continuous Hydrodynamic Maxwellian Equilibrium\n", " \n", " $f (\\rho, \\left( u_{0}, \\ u_{1}\\right), \\left( v_{0}, \\ v_{1}\\right)) \n", " = \\frac{3 \\rho e^{- \\frac{3 \\left(- u_{0} + v_{0}\\right)^{2}}{2} - \\frac{3 \\left(- u_{1} + v_{1}\\right)^{2}}{2}}}{2 \\pi}$\n", " Compressible: ✓ Deviation Only: ✗ Order: ∞
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "
Relaxation Info
CumulantEq. Value Relaxation Rate
$1 (central moment)$$\\rho$$0$
$x (central moment)$$0$$0$
$y (central moment)$$0$$0$
$x y$$0$$\\omega_{v}$
$x^{2} - y^{2}$$0$$\\omega_{v}$
$x^{2} + y^{2}$$\\frac{2 \\rho}{3}$$1$
$x^{2} y$$0$$1$
$x y^{2}$$0$$1$
$x^{2} y^{2}$$0$$1$
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lbm_config = LBMConfig(method=Method.CUMULANT, relaxation_rate=sp.Symbol('omega_v'), compressible=True)\n", "method_polynomial = create_lb_method(lbm_config=lbm_config)\n", "method_polynomial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Central Moments and Forcing\n", "\n", "The conserved modes are marked with the note *(central moment)* in the table above. It highlights the fact that these modes are relaxed in central moment space, other than cumulant space. As described in section A, this is done to enable the implicit forcing scheme. When a force is specified, the momentum modes' relaxation rates are overridden by the force model. In the following cell, a symbolic force is specified. Further, a full list of relaxation rates is passed to allow the specification of bulk viscosity." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Cumulant-Based Method\n", " Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: CenteredCumulantForceModel
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Continuous Hydrodynamic Maxwellian Equilibrium\n", " \n", " $f (\\rho, \\left( u_{0}, \\ u_{1}\\right), \\left( v_{0}, \\ v_{1}\\right)) \n", " = \\frac{3 \\rho e^{- \\frac{3 \\left(- u_{0} + v_{0}\\right)^{2}}{2} - \\frac{3 \\left(- u_{1} + v_{1}\\right)^{2}}{2}}}{2 \\pi}$\n", " Compressible: ✓ Deviation Only: ✗ Order: ∞
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "
Relaxation Info
CumulantEq. Value Relaxation Rate
$1 (central moment)$$\\rho$$0$
$x (central moment)$$0$$2 (overridden by force model)$
$y (central moment)$$0$$2 (overridden by force model)$
$x y$$0$$\\omega_{shear}$
$x^{2} - y^{2}$$0$$\\omega_{shear}$
$x^{2} + y^{2}$$\\frac{2 \\rho}{3}$$\\omega_{bulk}$
$x^{2} y$$0$$\\omega_{3}$
$x y^{2}$$0$$\\omega_{3}$
$x^{2} y^{2}$$0$$\\omega_{4}$
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lbm_config = LBMConfig(method=Method.CUMULANT, force=sp.symbols('F_:3'), compressible=True,\n", " relaxation_rates= [sp.Symbol('omega_shear'),\n", " sp.Symbol('omega_bulk'),\n", " sp.Symbol('omega_3'),\n", " sp.Symbol('omega_4')])\n", "method_with_force = create_lb_method(lbm_config=lbm_config)\n", "method_with_force" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C) Exemplary simulation: flow around sphere\n", "\n", "To end this tutorial, we show an example simulation of a channel flow with a spherical obstacle. This example is shown in 2D with the D2Q9 stencil but can be adapted easily for a 3D simulation." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from lbmpy.relaxationrates import relaxation_rate_from_lattice_viscosity\n", "from lbmpy.macroscopic_value_kernels import pdf_initialization_assignments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To define the channel flow with dimensionless parameters, we first define the reference length in lattice cells. The reference length will be the diameter of the spherical obstacle. Furthermore, we define a maximal velocity which will be set for the inflow later. The Reynolds number is set relatively high to 100000, which will cause a turbulent flow in the channel.\n", "\n", "From these definitions, we can calculate the relaxation rate omega for the lattice Boltzmann method." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "reference_length = 30\n", "maximal_velocity = 0.05\n", "reynolds_number = 100000\n", "kinematic_vicosity = (reference_length * maximal_velocity) / reynolds_number\n", "initial_velocity=(maximal_velocity, 0)\n", "\n", "omega = relaxation_rate_from_lattice_viscosity(kinematic_vicosity)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a next step, we define the domain size of our set up." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "stencil = LBStencil(Stencil.D2Q9)\n", "domain_size = (reference_length * 12, reference_length * 4)\n", "dim = len(domain_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the data for the simulation is allocated. We allocate a field src for the PDFs and field dst used as a temporary field to implement the two grid pull pattern. Additionally, we allocate a velocity field velField" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "dh = ps.create_data_handling(domain_size=domain_size, periodicity=(False, False))\n", "\n", "src = dh.add_array('src', values_per_cell=len(stencil), alignment=True)\n", "dh.fill('src', 0.0, ghost_layers=True)\n", "dst = dh.add_array('dst', values_per_cell=len(stencil), alignment=True)\n", "dh.fill('dst', 0.0, ghost_layers=True)\n", "\n", "velField = dh.add_array('velField', values_per_cell=dh.dim, alignment=True)\n", "dh.fill('velField', 0.0, ghost_layers=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We choose a cumulant lattice Boltzmann method, as described above. Here the second-order cumulants are relaxed with the relaxation rate calculated above. All higher-order cumulants are relaxed with one, which means we set them to the equilibrium." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Cumulant-Based Method\n", " Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 \n", " Continuous Hydrodynamic Maxwellian Equilibrium\n", " \n", " $f (\\rho, \\left( u_{0}, \\ u_{1}\\right), \\left( v_{0}, \\ v_{1}\\right)) \n", " = \\frac{3 \\rho e^{- \\frac{3 \\left(- u_{0} + v_{0}\\right)^{2}}{2} - \\frac{3 \\left(- u_{1} + v_{1}\\right)^{2}}{2}}}{2 \\pi}$\n", " Compressible: ✓ Deviation Only: ✗ Order: ∞
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", "
Relaxation Info
CumulantEq. Value Relaxation Rate
$1 (central moment)$$\\rho$$0$
$x (central moment)$$0$$0$
$y (central moment)$$0$$0$
$x y$$0$$1.99982001619854$
$x^{2} - y^{2}$$0$$1.99982001619854$
$x^{2} + y^{2}$$\\frac{2 \\rho}{3}$$1$
$x^{2} y$$0$$1$
$x y^{2}$$0$$1$
$x^{2} y^{2}$$0$$1$