[1]:
from lbmpy.session import *

Tutorial 04: The Cumulant Lattice Boltzmann Method in lbmpy

A) Principles of the centered cumulant collision operator

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

\[\begin{align} M( \mathbf{X} ) = \sum_i f_i \exp \left( \mathbf{c}_i \cdot \mathbf{X} \right) \end{align}\]

The raw moments \(m_{\alpha \beta \gamma}\) can be expressed as its derivatives, evaluated at zero:

\[\begin{align} m_{\alpha \beta \gamma} = \left. \frac{\partial^{\alpha}}{\partial X^{\alpha}} \frac{\partial^{\beta}}{\partial Y^{\beta}} \frac{\partial^{\gamma}}{\partial Z^{\gamma}} M(X, Y, Z) \right\vert_{\mathbf{X}=0} \end{align}\]

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:

\[\begin{split}\begin{align} C(\mathbf{X}) :=& \, \log ( M(\mathbf{X}) ) \\ c_{\alpha \beta \gamma} =& \, \left. \frac{\partial^{\alpha}}{\partial X^{\alpha}} \frac{\partial^{\beta}}{\partial Y^{\beta}} \frac{\partial^{\gamma}}{\partial Z^{\gamma}} C(X, Y, Z) \right\vert_{\mathbf{X}=0} \end{align}\end{split}\]

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. All cumulants of orders 2 and 3 are equal to their corresponding central moments, up to the density \(\rho\) as a proportionality factor.

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

\[\begin{align} C( \mathbf{X} ) = \mathbf{X} \cdot \mathbf{u} + \log( K( \mathbf{X} ) ). \end{align}\]

Derivatives of \(C\) can thus be expressed in terms of derivatives of \(K\), directly yielding equations of the cumulants in terms of central moments.

In the cumulant lattice Boltzmann method, forces are applied symmetrically in central moment space.

B) Method Creation

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.

Relaxation of Monomial Cumulants

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.

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.

[2]:
lbm_config = LBMConfig(method=Method.MONOMIAL_CUMULANT, relaxation_rate=sp.Symbol('omega_v'), compressible=True)
method_monomial = create_lb_method(lbm_config=lbm_config)
method_monomial
[2]:
Cumulant-Based Method Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
Continuous Hydrodynamic Maxwellian Equilibrium $f (\rho, \left( u_{0}, \ u_{1}\right), \left( v_{0}, \ v_{1}\right)) = \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}$
Compressible: ✓ Deviation Only: ✗ Order: 2
Relaxation Info
Cumulant Eq. Value Relaxation Rate
$1$ $\rho \log{\left(\rho \right)}$ $0.0$
$x$ $\rho u_{0}$ $0.0$
$y$ $\rho u_{1}$ $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.0$
$x y^{2}$ $0$ $1.0$
$x^{2} y^{2}$ $0$ $1.0$

Relaxation of Polynomial Cumulants

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.

[3]:
lbm_config = LBMConfig(method=Method.CUMULANT, relaxation_rate=sp.Symbol('omega_v'), compressible=True)
method_polynomial = create_lb_method(lbm_config=lbm_config)
method_polynomial
[3]:
Cumulant-Based Method Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
Continuous Hydrodynamic Maxwellian Equilibrium $f (\rho, \left( u_{0}, \ u_{1}\right), \left( v_{0}, \ v_{1}\right)) = \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}$
Compressible: ✓ Deviation Only: ✗ Order: 2
Relaxation Info
Cumulant Eq. Value Relaxation Rate
$1$ $\rho \log{\left(\rho \right)}$ $0.0$
$x$ $\rho u_{0}$ $0.0$
$y$ $\rho u_{1}$ $0.0$
$x y$ $0$ $\omega_{v}$
$x^{2} - y^{2}$ $0$ $\omega_{v}$
$x^{2} + y^{2}$ $\frac{2 \rho}{3}$ $1.0$
$x^{2} y$ $0$ $1.0$
$x y^{2}$ $0$ $1.0$
$x^{2} y^{2}$ $0$ $1.0$

C) Exemplary simulation: flow around sphere

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.

[4]:
from lbmpy.relaxationrates import relaxation_rate_from_lattice_viscosity
from lbmpy.macroscopic_value_kernels import pdf_initialization_assignments

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.

From these definitions, we can calculate the relaxation rate omega for the lattice Boltzmann method.

[5]:
reference_length = 30
maximal_velocity = 0.05
reynolds_number = 100000
kinematic_vicosity = (reference_length * maximal_velocity) / reynolds_number
initial_velocity=(maximal_velocity, 0)

omega = relaxation_rate_from_lattice_viscosity(kinematic_vicosity)

As a next step, we define the domain size of our set up.

[6]:
stencil = LBStencil(Stencil.D2Q9)
domain_size = (reference_length * 12, reference_length * 4)
dim = len(domain_size)

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

[7]:
dh = ps.create_data_handling(domain_size=domain_size, periodicity=(False, False))

src = dh.add_array('src', values_per_cell=len(stencil), alignment=True)
dh.fill('src', 0.0, ghost_layers=True)
dst = dh.add_array('dst', values_per_cell=len(stencil), alignment=True)
dh.fill('dst', 0.0, ghost_layers=True)

velField = dh.add_array('velField', values_per_cell=dh.dim, alignment=True)
dh.fill('velField', 0.0, ghost_layers=True)

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.

[8]:
lbm_config = LBMConfig(stencil=Stencil.D2Q9, method=Method.CUMULANT, relaxation_rate=omega,
                       compressible=True,
                       output={'velocity': velField}, kernel_type='stream_pull_collide')

method = create_lb_method(lbm_config=lbm_config)
method
[8]:
Cumulant-Based Method Stencil: D2Q9 Zero-Centered Storage: ✓ Force Model: None
Continuous Hydrodynamic Maxwellian Equilibrium $f (\rho, \left( u_{0}, \ u_{1}\right), \left( v_{0}, \ v_{1}\right)) = \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}$
Compressible: ✓ Deviation Only: ✗ Order: 2
Relaxation Info
Cumulant Eq. Value Relaxation Rate
$1$ $\rho \log{\left(\rho \right)}$ $0.0$
$x$ $\rho u_{0}$ $0.0$
$y$ $\rho u_{1}$ $0.0$
$x y$ $0$ $1.99982001619854$
$x^{2} - y^{2}$ $0$ $1.99982001619854$
$x^{2} + y^{2}$ $\frac{2 \rho}{3}$ $1.0$
$x^{2} y$ $0$ $1.0$
$x y^{2}$ $0$ $1.0$
$x^{2} y^{2}$ $0$ $1.0$

Initialization with equilibrium

[9]:
init = pdf_initialization_assignments(method, 1.0, initial_velocity, src.center_vector)

ast_init = ps.create_kernel(init, target=dh.default_target)
kernel_init = ast_init.compile()

dh.run_kernel(kernel_init)

Definition of the update rule

[10]:
lbm_optimisation = LBMOptimisation(symbolic_field=src, symbolic_temporary_field=dst)
update = create_lb_update_rule(lb_method=method,
                               lbm_config=lbm_config,
                               lbm_optimisation=lbm_optimisation)

ast_kernel = ps.create_kernel(update, target=dh.default_target, cpu_openmp=True)
kernel = ast_kernel.compile()

Definition of the boundary set up

[11]:
def set_sphere(x, y, *_):
    mid = (domain_size[0] // 3, domain_size[1] // 2)
    radius = reference_length // 2
    return (x-mid[0])**2 + (y-mid[1])**2 < radius**2
[12]:
bh = LatticeBoltzmannBoundaryHandling(method, dh, 'src', name="bh")

inflow = UBB(initial_velocity)
outflow = ExtrapolationOutflow(stencil[4], method)
wall = NoSlip("wall")

bh.set_boundary(inflow, slice_from_direction('W', dim))
bh.set_boundary(outflow, slice_from_direction('E', dim))
for direction in ('N', 'S'):
    bh.set_boundary(wall, slice_from_direction(direction, dim))

bh.set_boundary(NoSlip("obstacle"), mask_callback=set_sphere)

plt.figure(dpi=200)
plt.boundary_handling(bh)
../_images/notebooks_04_tutorial_cumulant_LBM_22_0.png
[13]:
def timeloop(timeSteps):
    for i in range(timeSteps):
        bh()
        dh.run_kernel(kernel)
        dh.swap("src", "dst")

Run the simulation

[14]:
mask = np.fromfunction(set_sphere, (domain_size[0], domain_size[1], len(domain_size)))
if 'is_test_run' not in globals():
    timeloop(50000)  # initial steps

    def run():
        timeloop(100)
        return np.ma.array(dh.gather_array('velField'), mask=mask)

    animation = plt.vector_field_magnitude_animation(run, frames=600, rescale=True)
    set_display_mode('video')
    res = display_animation(animation)
else:
    timeloop(10)
    res = None
res
[14]: