Skip to content

Models

jax_lab.core.models.BGKSim

Bases: LBMBase

BGK simulation class.

This class implements the Bhatnagar-Gross-Krook (BGK) approximation for the collision step in the Lattice Boltzmann Method.

Source code in jax_lab/core/models.py
class BGKSim(LBMBase):
    """
    BGK simulation class.

    This class implements the Bhatnagar-Gross-Krook (BGK) approximation for the collision step in the Lattice Boltzmann Method.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @partial(jit, static_argnums=(0,))
    def collision(self, f):
        """
        BGK collision step for lattice.

        The collision step is where the main physics of the LBM is applied. In the BGK approximation,
        the distribution function is relaxed towards the equilibrium distribution function.
        """
        f = self.precision_policy.cast_to_compute(f)
        rho, u = self.update_macroscopic(f)
        feq = self.equilibrium(rho, u, cast_output=False)
        fneq = f - feq
        fout = f - self.omega * fneq
        if self.force is not None:
            fout = self.apply_force(fout, feq, rho, u)
        return self.precision_policy.cast_to_output(fout)

collision

collision(f)

BGK collision step for lattice.

The collision step is where the main physics of the LBM is applied. In the BGK approximation, the distribution function is relaxed towards the equilibrium distribution function.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision(self, f):
    """
    BGK collision step for lattice.

    The collision step is where the main physics of the LBM is applied. In the BGK approximation,
    the distribution function is relaxed towards the equilibrium distribution function.
    """
    f = self.precision_policy.cast_to_compute(f)
    rho, u = self.update_macroscopic(f)
    feq = self.equilibrium(rho, u, cast_output=False)
    fneq = f - feq
    fout = f - self.omega * fneq
    if self.force is not None:
        fout = self.apply_force(fout, feq, rho, u)
    return self.precision_policy.cast_to_output(fout)

jax_lab.core.models.KBCSim

Bases: LBMBase

KBC simulation class.

This class implements the Karlin-Bösch-Chikatamarla (KBC) model for the collision step in the Lattice Boltzmann Method.

Source code in jax_lab/core/models.py
class KBCSim(LBMBase):
    """
    KBC simulation class.

    This class implements the Karlin-Bösch-Chikatamarla (KBC) model for the collision step in the Lattice Boltzmann Method.
    """

    def __init__(self, **kwargs):
        if kwargs.get("lattice").name != "D3Q27" and kwargs.get("nz") > 0:
            raise ValueError("KBC collision operator in 3D must only be used with D3Q27 lattice.")
        super().__init__(**kwargs)

    @partial(jit, static_argnums=(0,))
    def collision(self, f):
        """
        KBC collision step for lattice.
        """
        f = self.precision_policy.cast_to_compute(f)
        tiny = 1e-32
        beta = self.omega * 0.5
        rho, u = self.update_macroscopic(f)
        feq = self.equilibrium(rho, u, cast_output=False)
        fneq = f - feq
        if self.dim == 2:
            deltaS = self.fdecompose_shear_d2q9(fneq) * rho / 4.0
        else:
            deltaS = self.fdecompose_shear_d3q27(fneq) * rho
        deltaH = fneq - deltaS
        invBeta = 1.0 / beta
        gamma = invBeta - (2.0 - invBeta) * self.entropic_scalar_product(deltaS, deltaH, feq) / (
            tiny + self.entropic_scalar_product(deltaH, deltaH, feq)
        )

        fout = f - beta * (2.0 * deltaS + gamma[..., None] * deltaH)

        # add external force
        if self.force is not None:
            fout = self.apply_force(fout, feq, rho, u)
        return self.precision_policy.cast_to_output(fout)

    @partial(jit, static_argnums=(0,))
    def collision_modified(self, f):
        """
        Alternative KBC collision step for lattice.
        Note:
        At low Reynolds number the orignal KBC collision above produces inaccurate results because
        it does not check for the entropy increase/decrease. The KBC stabalizations should only be
        applied in principle to cells whose entropy decrease after a regular BGK collision. This is
        the case in most cells at higher Reynolds numbers and hence a check may not be needed.
        Overall the following alternative collision is more reliable and may replace the original
        implementation. The issue at the moment is that it is about 60-80% slower than the above method.
        """
        f = self.precision_policy.cast_to_compute(f)
        tiny = 1e-32
        beta = self.omega * 0.5
        rho, u = self.update_macroscopic(f)
        feq = self.equilibrium(rho, u, castOutput=False)

        # Alternative KBC: only stabalizes for voxels whose entropy decreases after BGK collision.
        f_bgk = f - self.omega * (f - feq)
        H_fin = jnp.sum(f * jnp.log(f / self.w), axis=-1, keepdims=True)
        H_fout = jnp.sum(f_bgk * jnp.log(f_bgk / self.w), axis=-1, keepdims=True)

        # the rest is identical to collision_deprecated
        fneq = f - feq
        if self.dim == 2:
            deltaS = self.fdecompose_shear_d2q9(fneq) * rho / 4.0
        else:
            deltaS = self.fdecompose_shear_d3q27(fneq) * rho
        deltaH = fneq - deltaS
        invBeta = 1.0 / beta
        gamma = invBeta - (2.0 - invBeta) * self.entropic_scalar_product(deltaS, deltaH, feq) / (
            tiny + self.entropic_scalar_product(deltaH, deltaH, feq)
        )

        f_kbc = f - beta * (2.0 * deltaS + gamma[..., None] * deltaH)
        fout = jnp.where(H_fout > H_fin, f_kbc, f_bgk)

        # add external force
        if self.force is not None:
            fout = self.apply_force(fout, feq, rho, u)
        return self.precision_policy.cast_to_output(fout)

    @partial(jit, static_argnums=(0,), inline=True)
    def entropic_scalar_product(self, x, y, feq):
        """
        Compute the entropic scalar product of x and y to approximate gamma in KBC.

        Returns
        -------
        jax.numpy.array
            Entropic scalar product of x, y, and feq.
        """
        return jnp.sum(x * y / feq, axis=-1)

    @partial(jit, static_argnums=(0,), inline=True)
    def fdecompose_shear_d2q9(self, fneq):
        """
        Decompose fneq into shear components for D2Q9 lattice.

        Parameters
        ----------
        fneq (jax.numpy.array): Non-equilibrium distribution function.

        Returns
        -------
        jax.numpy.array
            Shear components of fneq.
        """
        Pi = self.momentum_flux(fneq)
        N = Pi[..., 0] - Pi[..., 2]
        s = jnp.zeros_like(fneq)
        s = s.at[..., 6].set(N)
        s = s.at[..., 3].set(N)
        s = s.at[..., 2].set(-N)
        s = s.at[..., 1].set(-N)
        s = s.at[..., 8].set(Pi[..., 1])
        s = s.at[..., 4].set(-Pi[..., 1])
        s = s.at[..., 5].set(-Pi[..., 1])
        s = s.at[..., 7].set(Pi[..., 1])

        return s

    @partial(jit, static_argnums=(0,), inline=True)
    def fdecompose_shear_d3q27(self, fneq):
        """
        Decompose fneq into shear components for D3Q27 lattice.

        Parameters
        ----------
        fneq (jax.numpy.ndarray): Non-equilibrium distribution function.

        Returns
        -------
        jax.numpy.ndarray
            Shear components of fneq.
        """
        # if self.grid.dim == 3:
        #     diagonal    = (0, 3, 5)
        #     offdiagonal = (1, 2, 4)
        # elif self.grid.dim == 2:
        #     diagonal    = (0, 2)
        #     offdiagonal = (1,)

        # c=
        # array([[0, 0, 0],-----0
        #        [0, 0, -1],----1
        #        [0, 0, 1],-----2
        #        [0, -1, 0],----3
        #        [0, -1, -1],---4
        #        [0, -1, 1],----5
        #        [0, 1, 0],-----6
        #        [0, 1, -1],----7
        #        [0, 1, 1],-----8
        #        [-1, 0, 0],----9
        #        [-1, 0, -1],--10
        #        [-1, 0, 1],---11
        #        [-1, -1, 0],--12
        #        [-1, -1, -1],-13
        #        [-1, -1, 1],--14
        #        [-1, 1, 0],---15
        #        [-1, 1, -1],--16
        #        [-1, 1, 1],---17
        #        [1, 0, 0],----18
        #        [1, 0, -1],---19
        #        [1, 0, 1],----20
        #        [1, -1, 0],---21
        #        [1, -1, -1],--22
        #        [1, -1, 1],---23
        #        [1, 1, 0],----24
        #        [1, 1, -1],---25
        #        [1, 1, 1]])---26
        Pi = self.momentum_flux(fneq)
        Nxz = Pi[..., 0] - Pi[..., 5]
        Nyz = Pi[..., 3] - Pi[..., 5]

        # For c = (i, 0, 0), c = (0, j, 0) and c = (0, 0, k)
        s = jnp.zeros_like(fneq)
        s = s.at[..., 9].set((2.0 * Nxz - Nyz) / 6.0)
        s = s.at[..., 18].set((2.0 * Nxz - Nyz) / 6.0)
        s = s.at[..., 3].set((-Nxz + 2.0 * Nyz) / 6.0)
        s = s.at[..., 6].set((-Nxz + 2.0 * Nyz) / 6.0)
        s = s.at[..., 1].set((-Nxz - Nyz) / 6.0)
        s = s.at[..., 2].set((-Nxz - Nyz) / 6.0)

        # For c = (i, j, 0)
        s = s.at[..., 12].set(Pi[..., 1] / 4.0)
        s = s.at[..., 24].set(Pi[..., 1] / 4.0)
        s = s.at[..., 21].set(-Pi[..., 1] / 4.0)
        s = s.at[..., 15].set(-Pi[..., 1] / 4.0)

        # For c = (i, 0, k)
        s = s.at[..., 10].set(Pi[..., 2] / 4.0)
        s = s.at[..., 20].set(Pi[..., 2] / 4.0)
        s = s.at[..., 19].set(-Pi[..., 2] / 4.0)
        s = s.at[..., 11].set(-Pi[..., 2] / 4.0)

        # For c = (0, j, k)
        s = s.at[..., 8].set(Pi[..., 4] / 4.0)
        s = s.at[..., 4].set(Pi[..., 4] / 4.0)
        s = s.at[..., 7].set(-Pi[..., 4] / 4.0)
        s = s.at[..., 5].set(-Pi[..., 4] / 4.0)

        return s

collision

collision(f)

KBC collision step for lattice.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision(self, f):
    """
    KBC collision step for lattice.
    """
    f = self.precision_policy.cast_to_compute(f)
    tiny = 1e-32
    beta = self.omega * 0.5
    rho, u = self.update_macroscopic(f)
    feq = self.equilibrium(rho, u, cast_output=False)
    fneq = f - feq
    if self.dim == 2:
        deltaS = self.fdecompose_shear_d2q9(fneq) * rho / 4.0
    else:
        deltaS = self.fdecompose_shear_d3q27(fneq) * rho
    deltaH = fneq - deltaS
    invBeta = 1.0 / beta
    gamma = invBeta - (2.0 - invBeta) * self.entropic_scalar_product(deltaS, deltaH, feq) / (
        tiny + self.entropic_scalar_product(deltaH, deltaH, feq)
    )

    fout = f - beta * (2.0 * deltaS + gamma[..., None] * deltaH)

    # add external force
    if self.force is not None:
        fout = self.apply_force(fout, feq, rho, u)
    return self.precision_policy.cast_to_output(fout)

collision_modified

collision_modified(f)

Alternative KBC collision step for lattice. Note: At low Reynolds number the orignal KBC collision above produces inaccurate results because it does not check for the entropy increase/decrease. The KBC stabalizations should only be applied in principle to cells whose entropy decrease after a regular BGK collision. This is the case in most cells at higher Reynolds numbers and hence a check may not be needed. Overall the following alternative collision is more reliable and may replace the original implementation. The issue at the moment is that it is about 60-80% slower than the above method.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision_modified(self, f):
    """
    Alternative KBC collision step for lattice.
    Note:
    At low Reynolds number the orignal KBC collision above produces inaccurate results because
    it does not check for the entropy increase/decrease. The KBC stabalizations should only be
    applied in principle to cells whose entropy decrease after a regular BGK collision. This is
    the case in most cells at higher Reynolds numbers and hence a check may not be needed.
    Overall the following alternative collision is more reliable and may replace the original
    implementation. The issue at the moment is that it is about 60-80% slower than the above method.
    """
    f = self.precision_policy.cast_to_compute(f)
    tiny = 1e-32
    beta = self.omega * 0.5
    rho, u = self.update_macroscopic(f)
    feq = self.equilibrium(rho, u, castOutput=False)

    # Alternative KBC: only stabalizes for voxels whose entropy decreases after BGK collision.
    f_bgk = f - self.omega * (f - feq)
    H_fin = jnp.sum(f * jnp.log(f / self.w), axis=-1, keepdims=True)
    H_fout = jnp.sum(f_bgk * jnp.log(f_bgk / self.w), axis=-1, keepdims=True)

    # the rest is identical to collision_deprecated
    fneq = f - feq
    if self.dim == 2:
        deltaS = self.fdecompose_shear_d2q9(fneq) * rho / 4.0
    else:
        deltaS = self.fdecompose_shear_d3q27(fneq) * rho
    deltaH = fneq - deltaS
    invBeta = 1.0 / beta
    gamma = invBeta - (2.0 - invBeta) * self.entropic_scalar_product(deltaS, deltaH, feq) / (
        tiny + self.entropic_scalar_product(deltaH, deltaH, feq)
    )

    f_kbc = f - beta * (2.0 * deltaS + gamma[..., None] * deltaH)
    fout = jnp.where(H_fout > H_fin, f_kbc, f_bgk)

    # add external force
    if self.force is not None:
        fout = self.apply_force(fout, feq, rho, u)
    return self.precision_policy.cast_to_output(fout)

entropic_scalar_product

entropic_scalar_product(x, y, feq)

Compute the entropic scalar product of x and y to approximate gamma in KBC.

Returns

jax.numpy.array Entropic scalar product of x, y, and feq.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def entropic_scalar_product(self, x, y, feq):
    """
    Compute the entropic scalar product of x and y to approximate gamma in KBC.

    Returns
    -------
    jax.numpy.array
        Entropic scalar product of x, y, and feq.
    """
    return jnp.sum(x * y / feq, axis=-1)

fdecompose_shear_d2q9

fdecompose_shear_d2q9(fneq)

Decompose fneq into shear components for D2Q9 lattice.

Parameters

fneq (jax.numpy.array): Non-equilibrium distribution function.

Returns

jax.numpy.array Shear components of fneq.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def fdecompose_shear_d2q9(self, fneq):
    """
    Decompose fneq into shear components for D2Q9 lattice.

    Parameters
    ----------
    fneq (jax.numpy.array): Non-equilibrium distribution function.

    Returns
    -------
    jax.numpy.array
        Shear components of fneq.
    """
    Pi = self.momentum_flux(fneq)
    N = Pi[..., 0] - Pi[..., 2]
    s = jnp.zeros_like(fneq)
    s = s.at[..., 6].set(N)
    s = s.at[..., 3].set(N)
    s = s.at[..., 2].set(-N)
    s = s.at[..., 1].set(-N)
    s = s.at[..., 8].set(Pi[..., 1])
    s = s.at[..., 4].set(-Pi[..., 1])
    s = s.at[..., 5].set(-Pi[..., 1])
    s = s.at[..., 7].set(Pi[..., 1])

    return s

fdecompose_shear_d3q27

fdecompose_shear_d3q27(fneq)

Decompose fneq into shear components for D3Q27 lattice.

Parameters

fneq (jax.numpy.ndarray): Non-equilibrium distribution function.

Returns

jax.numpy.ndarray Shear components of fneq.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def fdecompose_shear_d3q27(self, fneq):
    """
    Decompose fneq into shear components for D3Q27 lattice.

    Parameters
    ----------
    fneq (jax.numpy.ndarray): Non-equilibrium distribution function.

    Returns
    -------
    jax.numpy.ndarray
        Shear components of fneq.
    """
    # if self.grid.dim == 3:
    #     diagonal    = (0, 3, 5)
    #     offdiagonal = (1, 2, 4)
    # elif self.grid.dim == 2:
    #     diagonal    = (0, 2)
    #     offdiagonal = (1,)

    # c=
    # array([[0, 0, 0],-----0
    #        [0, 0, -1],----1
    #        [0, 0, 1],-----2
    #        [0, -1, 0],----3
    #        [0, -1, -1],---4
    #        [0, -1, 1],----5
    #        [0, 1, 0],-----6
    #        [0, 1, -1],----7
    #        [0, 1, 1],-----8
    #        [-1, 0, 0],----9
    #        [-1, 0, -1],--10
    #        [-1, 0, 1],---11
    #        [-1, -1, 0],--12
    #        [-1, -1, -1],-13
    #        [-1, -1, 1],--14
    #        [-1, 1, 0],---15
    #        [-1, 1, -1],--16
    #        [-1, 1, 1],---17
    #        [1, 0, 0],----18
    #        [1, 0, -1],---19
    #        [1, 0, 1],----20
    #        [1, -1, 0],---21
    #        [1, -1, -1],--22
    #        [1, -1, 1],---23
    #        [1, 1, 0],----24
    #        [1, 1, -1],---25
    #        [1, 1, 1]])---26
    Pi = self.momentum_flux(fneq)
    Nxz = Pi[..., 0] - Pi[..., 5]
    Nyz = Pi[..., 3] - Pi[..., 5]

    # For c = (i, 0, 0), c = (0, j, 0) and c = (0, 0, k)
    s = jnp.zeros_like(fneq)
    s = s.at[..., 9].set((2.0 * Nxz - Nyz) / 6.0)
    s = s.at[..., 18].set((2.0 * Nxz - Nyz) / 6.0)
    s = s.at[..., 3].set((-Nxz + 2.0 * Nyz) / 6.0)
    s = s.at[..., 6].set((-Nxz + 2.0 * Nyz) / 6.0)
    s = s.at[..., 1].set((-Nxz - Nyz) / 6.0)
    s = s.at[..., 2].set((-Nxz - Nyz) / 6.0)

    # For c = (i, j, 0)
    s = s.at[..., 12].set(Pi[..., 1] / 4.0)
    s = s.at[..., 24].set(Pi[..., 1] / 4.0)
    s = s.at[..., 21].set(-Pi[..., 1] / 4.0)
    s = s.at[..., 15].set(-Pi[..., 1] / 4.0)

    # For c = (i, 0, k)
    s = s.at[..., 10].set(Pi[..., 2] / 4.0)
    s = s.at[..., 20].set(Pi[..., 2] / 4.0)
    s = s.at[..., 19].set(-Pi[..., 2] / 4.0)
    s = s.at[..., 11].set(-Pi[..., 2] / 4.0)

    # For c = (0, j, k)
    s = s.at[..., 8].set(Pi[..., 4] / 4.0)
    s = s.at[..., 4].set(Pi[..., 4] / 4.0)
    s = s.at[..., 7].set(-Pi[..., 4] / 4.0)
    s = s.at[..., 5].set(-Pi[..., 4] / 4.0)

    return s

jax_lab.core.models.AdvectionDiffusionBGK

Bases: LBMBase

Advection Diffusion Model based on the BGK model.

Source code in jax_lab/core/models.py
class AdvectionDiffusionBGK(LBMBase):
    """
    Advection Diffusion Model based on the BGK model.
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.vel = kwargs.get("vel", None)
        if self.vel is None:
            raise ValueError("Velocity must be specified for AdvectionDiffusionBGK.")

    @partial(jit, static_argnums=(0,))
    def collision(self, f):
        """
        BGK collision step for lattice.
        """
        f = self.precision_policy.cast_to_compute(f)
        rho = jnp.sum(f, axis=-1, keepdims=True)
        feq = self.equilibrium(rho, self.vel, cast_output=False)
        fneq = f - feq
        fout = f - self.omega * fneq
        return self.precision_policy.cast_to_output(fout)

collision

collision(f)

BGK collision step for lattice.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision(self, f):
    """
    BGK collision step for lattice.
    """
    f = self.precision_policy.cast_to_compute(f)
    rho = jnp.sum(f, axis=-1, keepdims=True)
    feq = self.equilibrium(rho, self.vel, cast_output=False)
    fneq = f - feq
    fout = f - self.omega * fneq
    return self.precision_policy.cast_to_output(fout)

jax_lab.core.models.MRTSim

Bases: LBMBase

Multi-relaxation time model.

Source code in jax_lab/core/models.py
class MRTSim(LBMBase):
    """
    Multi-relaxation time model.
    """

    def __init__(self, **kwargs):
        kwargs.update({"omega": 1.0})
        super().__init__(**kwargs)
        self.s_rho = kwargs.get("s_rho")
        self.s_e = kwargs.get("s_e")
        self.s_eta = kwargs.get("s_eta")
        self.s_j = kwargs.get("s_j")
        self.s_q = kwargs.get("s_q")
        self.s_v = self.omega
        self.M_inv = jnp.array(
            np.transpose(np.linalg.inv(kwargs.get("M"))),
            dtype=self.precision_policy.compute_dtype,
        )
        self.M = jnp.array(np.transpose(kwargs.get("M")), dtype=self.precision_policy.compute_dtype)
        if isinstance(self.lattice, LatticeD2Q9):
            self.S = jnp.array(
                np.diag([self.s_rho, self.s_e, self.s_eta, self.s_j, self.s_q, self.s_j, self.s_q, self.s_v, self.s_v]),
                dtype=self.precision_policy.compute_dtype,
            )
        elif isinstance(self.lattice, LatticeD3Q19):
            self.s_pi = kwargs.get("s_pi")
            self.s_m = kwargs.get("s_m")
            self.S = jnp.array(
                np.diag([
                    self.s_rho,
                    self.s_e,
                    self.s_eta,
                    self.s_j,
                    self.s_q,
                    self.s_j,
                    self.s_q,
                    self.s_j,
                    self.s_q,
                    self.s_v,
                    self.s_pi,
                    self.s_v,
                    self.s_pi,
                    self.s_v,
                    self.s_v,
                    self.s_v,
                    self.s_m,
                    self.s_m,
                    self.s_m,
                ]),
                dtype=self.precision_policy.compute_dtype,
            )
        else:
            NotImplementedError(f"Lattice type {self.lattice.name} has not been implemented")

        # Fused collision matrix K = M @ S @ M_inv, replacing the three separate moment-space matrix multiplies
        # (f @ M, relax, @ M_inv) in collision() with symbolic, sparse-coefficient columns compiled from K's
        # nonzero entries - see collision() and MultiphaseMRT.collision (same fusion identity, without a
        # surface-tension term since this is the single-phase model).
        collision_matrix = np.asarray(jnp.dot(jnp.dot(self.M, self.S), self.M_inv))
        columns = []
        for output_direction in range(self.lattice.q):
            columns.append(
                tuple(
                    (input_direction, np.float32(collision_matrix[input_direction, output_direction]))
                    for input_direction in range(self.lattice.q)
                    if not np.isclose(collision_matrix[input_direction, output_direction], 0.0, atol=1e-7)
                )
            )
        self.collision_matrix = jnp.array(collision_matrix, dtype=self.precision_policy.compute_dtype)
        self.collision_terms = tuple(columns)

    @partial(jit, static_argnums=(0,))
    def collision(self, f):
        """
        MRT collision step for lattice, using a symbolic (sparse-coefficient) fused collision matrix instead of
        three separate moment-space matrix multiplies. See MultiphaseMRT.collision for the fusion identity.
        """
        f = self.precision_policy.cast_to_compute(f)
        rho, u = self.update_macroscopic(f)
        feq = self.equilibrium(rho, u)
        difference = f - feq

        outputs = []
        for output_direction, terms in enumerate(self.collision_terms):
            relaxed = sum(difference[..., input_direction] * coefficient for input_direction, coefficient in terms)
            outputs.append(f[..., output_direction] - relaxed)
        fout = jnp.stack(outputs, axis=-1)

        if self.force is not None:
            fout = fout + self._compute_force_delta_feq(rho, u)
        return self.precision_policy.cast_to_output(fout)

    @partial(jit, static_argnums=(0,), inline=True)
    def _compute_force_delta_feq(self, rho, u):
        """
        Real-space compact EDM difference delta_feq = feq(rho, u + du) - feq(rho, u), computed directly from
        cu, dcu and delta_usqr instead of building a full feq_force array via equilibrium(). Shared by
        collision (added directly, real space) and apply_force (transformed into moment space with M, for
        callers using the unfused, moment-space collision path).

        Parameters
        ----------
        rho (jax.numpy.ndarray): Density field.

        u (jax.numpy.ndarray): Velocity field.

        Returns
        -------
        (jax.numpy.ndarray): Real-space delta_feq.
        """
        du = self.get_force()
        c = jnp.array(self.c, dtype=self.precision_policy.compute_dtype)
        cu = 3.0 * jnp.dot(u, c)
        dcu = 3.0 * jnp.dot(du, c)
        delta_usqr = 1.5 * (2.0 * jnp.sum(u * du, axis=-1, keepdims=True) + jnp.sum(jnp.square(du), axis=-1, keepdims=True))
        return rho * self.w * (dcu * (1.0 + cu + 0.5 * dcu) - delta_usqr)

    @partial(jit, static_argnums=(0,), inline=True)
    def apply_force(self, m, meq, rho, u):
        """
        add force based on exact-difference method due to Kupershtokh, in moment space.

        LBMBase.apply_force subtracts feq in population space, which does not apply here since m/meq are
        moment-space quantities. Instead, the real-space delta_feq (see _compute_force_delta_feq) is
        transformed into moment space with M: since M is linear, dot(delta_feq, M) ==
        dot(feq(rho, u + du), M) - dot(feq(rho, u), M), an exact substitute for subtracting a moment-space
        equilibrium from a population-space one. Provided for callers using the unfused, moment-space collision
        path (e.g. Thermal.apply_force_thermal, when the wrapped fluid_solver is an MRTSim); collision() itself
        adds delta_feq directly in real space instead.

        Parameters
        ----------
        m (jax.numpy.ndarray): Post-collision moments.

        meq (jax.numpy.ndarray): Equilibrium moments. Unused - kept for interface compatibility, since the
            compact difference formula only needs rho, u and the force.

        rho (jax.numpy.ndarray): Density field.

        u (jax.numpy.ndarray): Velocity field.

        Returns
        -------
        (jax.numpy.ndarray): Post-collision moments with the force applied.

        References
        ----------
        1. Kupershtokh, A. (2004). New method of incorporating a body force term into the lattice Boltzmann
        equation. In Proceedings of the 5th International EHD Workshop (pp. 241-246). University of Poitiers.
        """
        delta_feq = self._compute_force_delta_feq(rho, u)
        return m + jnp.dot(delta_feq, self.M)

apply_force

apply_force(m, meq, rho, u)

add force based on exact-difference method due to Kupershtokh, in moment space.

LBMBase.apply_force subtracts feq in population space, which does not apply here since m/meq are moment-space quantities. Instead, the real-space delta_feq (see _compute_force_delta_feq) is transformed into moment space with M: since M is linear, dot(delta_feq, M) == dot(feq(rho, u + du), M) - dot(feq(rho, u), M), an exact substitute for subtracting a moment-space equilibrium from a population-space one. Provided for callers using the unfused, moment-space collision path (e.g. Thermal.apply_force_thermal, when the wrapped fluid_solver is an MRTSim); collision() itself adds delta_feq directly in real space instead.

Parameters

m (jax.numpy.ndarray): Post-collision moments.

meq (jax.numpy.ndarray): Equilibrium moments. Unused - kept for interface compatibility, since the compact difference formula only needs rho, u and the force.

rho (jax.numpy.ndarray): Density field.

u (jax.numpy.ndarray): Velocity field.

Returns

(jax.numpy.ndarray): Post-collision moments with the force applied.

References
  1. Kupershtokh, A. (2004). New method of incorporating a body force term into the lattice Boltzmann equation. In Proceedings of the 5th International EHD Workshop (pp. 241-246). University of Poitiers.
Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def apply_force(self, m, meq, rho, u):
    """
    add force based on exact-difference method due to Kupershtokh, in moment space.

    LBMBase.apply_force subtracts feq in population space, which does not apply here since m/meq are
    moment-space quantities. Instead, the real-space delta_feq (see _compute_force_delta_feq) is
    transformed into moment space with M: since M is linear, dot(delta_feq, M) ==
    dot(feq(rho, u + du), M) - dot(feq(rho, u), M), an exact substitute for subtracting a moment-space
    equilibrium from a population-space one. Provided for callers using the unfused, moment-space collision
    path (e.g. Thermal.apply_force_thermal, when the wrapped fluid_solver is an MRTSim); collision() itself
    adds delta_feq directly in real space instead.

    Parameters
    ----------
    m (jax.numpy.ndarray): Post-collision moments.

    meq (jax.numpy.ndarray): Equilibrium moments. Unused - kept for interface compatibility, since the
        compact difference formula only needs rho, u and the force.

    rho (jax.numpy.ndarray): Density field.

    u (jax.numpy.ndarray): Velocity field.

    Returns
    -------
    (jax.numpy.ndarray): Post-collision moments with the force applied.

    References
    ----------
    1. Kupershtokh, A. (2004). New method of incorporating a body force term into the lattice Boltzmann
    equation. In Proceedings of the 5th International EHD Workshop (pp. 241-246). University of Poitiers.
    """
    delta_feq = self._compute_force_delta_feq(rho, u)
    return m + jnp.dot(delta_feq, self.M)

collision

collision(f)

MRT collision step for lattice, using a symbolic (sparse-coefficient) fused collision matrix instead of three separate moment-space matrix multiplies. See MultiphaseMRT.collision for the fusion identity.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision(self, f):
    """
    MRT collision step for lattice, using a symbolic (sparse-coefficient) fused collision matrix instead of
    three separate moment-space matrix multiplies. See MultiphaseMRT.collision for the fusion identity.
    """
    f = self.precision_policy.cast_to_compute(f)
    rho, u = self.update_macroscopic(f)
    feq = self.equilibrium(rho, u)
    difference = f - feq

    outputs = []
    for output_direction, terms in enumerate(self.collision_terms):
        relaxed = sum(difference[..., input_direction] * coefficient for input_direction, coefficient in terms)
        outputs.append(f[..., output_direction] - relaxed)
    fout = jnp.stack(outputs, axis=-1)

    if self.force is not None:
        fout = fout + self._compute_force_delta_feq(rho, u)
    return self.precision_policy.cast_to_output(fout)

jax_lab.core.models.CLBMSim

Bases: LBMBase

Central moment (cascaded) collision model.

Source code in jax_lab/core/models.py
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
class CLBMSim(LBMBase):
    """
    Central moment (cascaded) collision model.
    """

    def __init__(self, **kwargs):
        kwargs.update({"omega": 1.0})
        super().__init__(**kwargs)
        self.M_inv = jnp.array(
            np.transpose(np.linalg.inv(kwargs.get("M"))),
            dtype=self.precision_policy.compute_dtype,
        )
        self.M = jnp.array(np.transpose(kwargs.get("M")), dtype=self.precision_policy.compute_dtype)
        self.s_0 = kwargs.get("s_0")
        self.s_1 = kwargs.get("s_1")
        self.s_b = kwargs.get("s_b")
        self.s_2 = kwargs.get("s_2")
        self.s_3 = kwargs.get("s_3")
        self.s_4 = kwargs.get("s_4")
        self.s_v = self.omega
        if isinstance(self.lattice, LatticeD2Q9):
            self.S = jnp.array(
                np.diag([self.s_0, self.s_1, self.s_1, self.s_b, self.s_2, self.s_2, self.s_3, self.s_3, self.s_4]),
                dtype=self.precision_policy.compute_dtype,
            )
        elif isinstance(self.lattice, LatticeD3Q19):
            self.s_plus = (self.s_b + 2 * self.s_2) / 3
            self.s_minus = (self.s_b - self.s_2) / 3

            S = np.diag([
                self.s_0,
                self.s_1,
                self.s_1,
                self.s_1,
                self.s_v,
                self.s_v,
                self.s_v,
                self.s_plus,
                self.s_plus,
                self.s_plus,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_4,
                self.s_4,
                self.s_4,
            ])
            S[7, 8] = self.s_minus
            S[7, 9] = self.s_minus
            S[8, 7] = self.s_minus
            S[8, 9] = self.s_minus
            S[9, 7] = self.s_minus
            S[9, 8] = self.s_minus
            self.S = jnp.array(S, dtype=self.precision_policy.compute_dtype)

        elif isinstance(self.lattice, LatticeD3Q27):
            self.s_plus = (self.s_b + 2 * self.s_2) / 3
            self.s_minus = (self.s_b - self.s_2) / 3
            self.s_3b = kwargs.get("s_3b")
            self.s_4b = kwargs.get("s_4b")
            self.s_5 = kwargs.get("s_5")
            self.s_6 = kwargs.get("s_6")

            S = np.diag([
                self.s_0,
                self.s_1,
                self.s_1,
                self.s_1,
                self.s_v,
                self.s_v,
                self.s_v,
                self.s_plus,
                self.s_plus,
                self.s_plus,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3,
                self.s_3b,
                self.s_4,
                self.s_4,
                self.s_4,
                self.s_4b,
                self.s_4b,
                self.s_4b,
                self.s_5,
                self.s_5,
                self.s_5,
                self.s_6,
            ])
            S[7, 8] = self.s_minus
            S[7, 9] = self.s_minus
            S[8, 7] = self.s_minus
            S[8, 9] = self.s_minus
            S[9, 7] = self.s_minus
            S[9, 8] = self.s_minus
            self.S = jnp.array(S, dtype=self.precision_policy.compute_dtype)

    @partial(jit, static_argnums=(0,), inline=True)
    def macroscopic_velocity(self, f, rho):
        """
        macroscopic_velocity computes the velocity and incorporates forces into velocity for Exact Difference Method (EDM) (used for SRT and MRT collision) models
        and the consistent forcing scheme developed by LinLin Fei et. al (for Cascaded LBM). This is used for post-processing only and not for equilibrium distribution computation.

        Parameters
        ----------
        f (jax.numpy.ndarray): Distribution arrays.

        rho (jax.numpy.ndarray): Density fields.

        Returns
        -------
        u: jax.numpy.ndarray
            Velocity fields.
        """
        # rho_tree = map(lambda f: jnp.sum(f, axis=-1, keepdims=True), f_tree)
        c = jnp.array(self.c, dtype=self.precision_policy.compute_dtype).T
        u = jnp.dot(f, c) / rho
        if self.force is not None:
            return u + 0.5 * self.force / rho
        else:
            return u

    @partial(jit, static_argnums=(0,))
    def compute_central_moment(self, m, u):
        if isinstance(self.lattice, LatticeD2Q9):

            def shift(m, u):
                ux = u[..., 0]
                uy = u[..., 1]
                usq = ux**2 + uy**2
                udiff = ux**2 - uy**2
                T = jnp.zeros_like(m)
                T = T.at[..., 0].set(m[..., 0])
                T = T.at[..., 1].set(-ux * m[..., 0] + m[..., 1])
                T = T.at[..., 2].set(-uy * m[..., 0] + m[..., 2])
                T = T.at[..., 3].set(usq * m[..., 0] - 2 * ux * m[..., 1] - 2 * uy * m[..., 2] + m[..., 3])
                T = T.at[..., 4].set(udiff * m[..., 0] - 2 * ux * m[..., 1] + 2 * uy * m[..., 2] + m[..., 4])
                T = T.at[..., 5].set(ux * uy * m[..., 0] - uy * m[..., 1] - ux * m[..., 2] + m[..., 5])
                T = T.at[..., 6].set(
                    -(ux**2) * uy * m[..., 0]
                    + 2 * ux * uy * m[..., 1]
                    + ux**2 * m[..., 2]
                    - 0.5 * uy * m[..., 3]
                    - 0.5 * uy * m[..., 4]
                    - 2 * ux * m[..., 5]
                    + m[..., 6]
                )
                T = T.at[..., 7].set(
                    -(uy**2) * ux * m[..., 0]
                    + uy**2 * m[..., 1]
                    + 2 * ux * uy * m[..., 2]
                    - 0.5 * ux * m[..., 3]
                    + 0.5 * ux * m[..., 4]
                    - 2 * uy * m[..., 5]
                    + m[..., 7]
                )
                T = T.at[..., 8].set(
                    (uy**2 * ux**2) * m[..., 0]
                    - 2 * ux * uy**2 * m[..., 1]
                    - 2 * uy * ux**2 * m[..., 2]
                    + 0.5 * usq * m[..., 3]
                    - 0.5 * udiff * m[..., 4]
                    + 4 * ux * uy * m[..., 5]
                    - 2 * uy * m[..., 6]
                    - 2 * ux * m[..., 7]
                    + m[..., 8]
                )
                return T

            return shift(m, u)

        elif isinstance(self.lattice, LatticeD3Q19):

            def shift(m, u):
                ux = u[..., 0]
                uy = u[..., 1]
                uz = u[..., 2]
                T = jnp.zeros_like(m)
                T = T.at[..., 0].set(m[..., 0])
                T = T.at[..., 1].set(-ux * m[..., 0] + m[..., 1])
                T = T.at[..., 2].set(-uy * m[..., 0] + m[..., 2])
                T = T.at[..., 3].set(-uz * m[..., 0] + m[..., 3])
                T = T.at[..., 4].set(ux * uy * m[..., 0] - uy * m[..., 1] - ux * m[..., 2] + m[..., 4])
                T = T.at[..., 5].set(ux * uz * m[..., 0] - uz * m[..., 1] - ux * m[..., 3] + m[..., 5])
                T = T.at[..., 6].set(uy * uz * m[..., 0] - uz * m[..., 2] - uy * m[..., 3] + m[..., 6])
                T = T.at[..., 7].set((ux**2) * m[..., 0] - 2 * ux * m[..., 1] + m[..., 7])
                T = T.at[..., 8].set((uy**2) * m[..., 0] - 2 * uy * m[..., 2] + m[..., 8])
                T = T.at[..., 9].set((uz**2) * m[..., 0] - 2 * uz * m[..., 3] + m[..., 9])
                T = T.at[..., 10].set(
                    -ux * (uy**2) * m[..., 0] + (uy**2) * m[..., 1] + 2 * ux * uy * m[..., 2] - 2 * uy * m[..., 4] - ux * m[..., 8] + m[..., 10]
                )
                T = T.at[..., 11].set(
                    -ux * (uz**2) * m[..., 0] + (uz**2) * m[..., 1] + 2 * ux * uz * m[..., 3] - 2 * uz * m[..., 5] - ux * m[..., 9] + m[..., 11]
                )
                T = T.at[..., 12].set(
                    -(ux**2) * uy * m[..., 0] + 2 * ux * uy * m[..., 1] + (ux**2) * m[..., 2] - 2 * ux * m[..., 4] - uy * m[..., 7] + m[..., 12]
                )
                T = T.at[..., 13].set(
                    -(ux**2) * uz * m[..., 0] + 2 * ux * uz * m[..., 1] + (ux**2) * m[..., 3] - 2 * ux * m[..., 5] - uz * m[..., 7] + m[..., 13]
                )
                T = T.at[..., 14].set(
                    -uy * (uz**2) * m[..., 0] + (uz**2) * m[..., 2] + 2 * uy * uz * m[..., 3] - 2 * uz * m[..., 6] - uy * m[..., 9] + m[..., 14]
                )
                T = T.at[..., 15].set(
                    -(uy**2) * uz * m[..., 0] + 2 * uy * uz * m[..., 2] + (uy**2) * m[..., 3] - 2 * uy * m[..., 6] - uz * m[..., 8] + m[..., 15]
                )
                T = T.at[..., 16].set(
                    (ux**2) * (uy**2) * m[..., 0]
                    - 2 * ux * (uy**2) * m[..., 1]
                    - 2 * uy * (ux**2) * m[..., 2]
                    + 4 * ux * uy * m[..., 4]
                    + (uy**2) * m[..., 7]
                    + (ux**2) * m[..., 8]
                    - 2 * ux * m[..., 10]
                    - 2 * uy * m[..., 12]
                    + m[..., 16]
                )
                T = T.at[..., 17].set(
                    (ux**2) * (uz**2) * m[..., 0]
                    - 2 * ux * (uz**2) * m[..., 1]
                    - 2 * uz * (ux**2) * m[..., 3]
                    + 4 * ux * uz * m[..., 5]
                    + (uz**2) * m[..., 7]
                    + (ux**2) * m[..., 9]
                    - 2 * ux * m[..., 11]
                    - 2 * uz * m[..., 13]
                    + m[..., 17]
                )
                T = T.at[..., 18].set(
                    (uy**2) * (uz**2) * m[..., 0]
                    - 2 * uy * (uz**2) * m[..., 2]
                    - 2 * uz * (uy**2) * m[..., 3]
                    + 4 * uy * uz * m[..., 6]
                    + (uz**2) * m[..., 8]
                    + (uy**2) * m[..., 9]
                    - 2 * uy * m[..., 14]
                    - 2 * uz * m[..., 15]
                    + m[..., 18]
                )
                return T

            return shift(m, u)

        elif isinstance(self.lattice, LatticeD3Q27):

            def shift(m, u):
                ux = u[..., 0]
                uy = u[..., 1]
                uz = u[..., 2]
                T = jnp.zeros_like(m)
                T = T.at[..., 0].set(m[..., 0])
                T = T.at[..., 1].set(m[..., 1] - m[..., 0] * ux)
                T = T.at[..., 2].set(m[..., 2] - m[..., 0] * uy)
                T = T.at[..., 3].set(m[..., 3] - m[..., 0] * uz)
                T = T.at[..., 4].set(m[..., 4] - m[..., 2] * ux - m[..., 1] * uy + m[..., 0] * ux * uy)
                T = T.at[..., 5].set(m[..., 5] - m[..., 3] * ux - m[..., 1] * uz + m[..., 0] * ux * uz)
                T = T.at[..., 6].set(m[..., 6] - m[..., 3] * uy - m[..., 2] * uz + m[..., 0] * uy * uz)
                T = T.at[..., 7].set(m[..., 0] * ux**2 - 2 * m[..., 1] * ux + m[..., 7])
                T = T.at[..., 8].set(m[..., 0] * uy**2 - 2 * m[..., 2] * uy + m[..., 8])
                T = T.at[..., 9].set(m[..., 0] * uz**2 - 2 * m[..., 3] * uz + m[..., 9])
                T = T.at[..., 10].set(
                    m[..., 10] - m[..., 8] * ux - 2 * m[..., 4] * uy + m[..., 1] * uy**2 - m[..., 0] * ux * uy**2 + 2 * m[..., 2] * ux * uy
                )
                T = T.at[..., 11].set(
                    m[..., 11] - m[..., 9] * ux - 2 * m[..., 5] * uz + m[..., 1] * uz**2 - m[..., 0] * ux * uz**2 + 2 * m[..., 3] * ux * uz
                )
                T = T.at[..., 12].set(
                    m[..., 12] - 2 * m[..., 4] * ux - m[..., 7] * uy + m[..., 2] * ux**2 - m[..., 0] * ux**2 * uy + 2 * m[..., 1] * ux * uy
                )
                T = T.at[..., 13].set(
                    m[..., 13] - 2 * m[..., 5] * ux - m[..., 7] * uz + m[..., 3] * ux**2 - m[..., 0] * ux**2 * uz + 2 * m[..., 1] * ux * uz
                )
                T = T.at[..., 14].set(
                    m[..., 14] - m[..., 9] * uy - 2 * m[..., 6] * uz + m[..., 2] * uz**2 - m[..., 0] * uy * uz**2 + 2 * m[..., 3] * uy * uz
                )
                T = T.at[..., 15].set(
                    m[..., 15] - 2 * m[..., 6] * uy - m[..., 8] * uz + m[..., 3] * uy**2 - m[..., 0] * uy**2 * uz + 2 * m[..., 2] * uy * uz
                )
                T = T.at[..., 16].set(
                    m[..., 16]
                    - m[..., 6] * ux
                    - m[..., 5] * uy
                    - m[..., 4] * uz
                    + m[..., 3] * ux * uy
                    + m[..., 2] * ux * uz
                    + m[..., 1] * uy * uz
                    - m[..., 0] * ux * uy * uz
                )
                T = T.at[..., 17].set(
                    m[..., 0] * ux**2 * uy**2
                    - 2 * m[..., 2] * ux**2 * uy
                    + m[..., 8] * ux**2
                    - 2 * m[..., 1] * ux * uy**2
                    + 4 * m[..., 4] * ux * uy
                    - 2 * m[..., 10] * ux
                    + m[..., 7] * uy**2
                    - 2 * m[..., 12] * uy
                    + m[..., 17]
                )
                T = T.at[..., 18].set(
                    m[..., 0] * ux**2 * uz**2
                    - 2 * m[..., 3] * ux**2 * uz
                    + m[..., 9] * ux**2
                    - 2 * m[..., 1] * ux * uz**2
                    + 4 * m[..., 5] * ux * uz
                    - 2 * m[..., 11] * ux
                    + m[..., 7] * uz**2
                    - 2 * m[..., 13] * uz
                    + m[..., 18]
                )
                T = T.at[..., 19].set(
                    m[..., 0] * uy**2 * uz**2
                    - 2 * m[..., 3] * uy**2 * uz
                    + m[..., 9] * uy**2
                    - 2 * m[..., 2] * uy * uz**2
                    + 4 * m[..., 6] * uy * uz
                    - 2 * m[..., 14] * uy
                    + m[..., 8] * uz**2
                    - 2 * m[..., 15] * uz
                    + m[..., 19]
                )
                T = T.at[..., 20].set(
                    m[..., 20]
                    - 2 * m[..., 16] * ux
                    - m[..., 13] * uy
                    - m[..., 12] * uz
                    + m[..., 6] * ux**2
                    - m[..., 3] * ux**2 * uy
                    - m[..., 2] * ux**2 * uz
                    + 2 * m[..., 5] * ux * uy
                    + 2 * m[..., 4] * ux * uz
                    + m[..., 7] * uy * uz
                    - 2 * m[..., 1] * ux * uy * uz
                    + m[..., 0] * ux**2 * uy * uz
                )
                T = T.at[..., 21].set(
                    m[..., 21]
                    - m[..., 15] * ux
                    - 2 * m[..., 16] * uy
                    - m[..., 10] * uz
                    + m[..., 5] * uy**2
                    - m[..., 3] * ux * uy**2
                    - m[..., 1] * uy**2 * uz
                    + 2 * m[..., 6] * ux * uy
                    + m[..., 8] * ux * uz
                    + 2 * m[..., 4] * uy * uz
                    - 2 * m[..., 2] * ux * uy * uz
                    + m[..., 0] * ux * uy**2 * uz
                )
                T = T.at[..., 22].set(
                    m[..., 22]
                    - m[..., 14] * ux
                    - m[..., 11] * uy
                    - 2 * m[..., 16] * uz
                    + m[..., 4] * uz**2
                    - m[..., 2] * ux * uz**2
                    - m[..., 1] * uy * uz**2
                    + m[..., 9] * ux * uy
                    + 2 * m[..., 6] * ux * uz
                    + 2 * m[..., 5] * uy * uz
                    - 2 * m[..., 3] * ux * uy * uz
                    + m[..., 0] * ux * uy * uz**2
                )
                T = T.at[..., 23].set(
                    m[..., 23]
                    - m[..., 19] * ux
                    - 2 * m[..., 22] * uy
                    - 2 * m[..., 21] * uz
                    + m[..., 11] * uy**2
                    + m[..., 10] * uz**2
                    - m[..., 9] * ux * uy**2
                    - m[..., 8] * ux * uz**2
                    - 2 * m[..., 4] * uy * uz**2
                    - 2 * m[..., 5] * uy**2 * uz
                    + m[..., 1] * uy**2 * uz**2
                    + 2 * m[..., 14] * ux * uy
                    + 2 * m[..., 15] * ux * uz
                    + 4 * m[..., 16] * uy * uz
                    - 4 * m[..., 6] * ux * uy * uz
                    + 2 * m[..., 2] * ux * uy * uz**2
                    + 2 * m[..., 3] * ux * uy**2 * uz
                    - m[..., 0] * ux * uy**2 * uz**2
                )
                T = T.at[..., 24].set(
                    m[..., 24]
                    - 2 * m[..., 22] * ux
                    - m[..., 18] * uy
                    - 2 * m[..., 20] * uz
                    + m[..., 14] * ux**2
                    + m[..., 12] * uz**2
                    - m[..., 9] * ux**2 * uy
                    - 2 * m[..., 4] * ux * uz**2
                    - 2 * m[..., 6] * ux**2 * uz
                    - m[..., 7] * uy * uz**2
                    + m[..., 2] * ux**2 * uz**2
                    + 2 * m[..., 11] * ux * uy
                    + 4 * m[..., 16] * ux * uz
                    + 2 * m[..., 13] * uy * uz
                    - 4 * m[..., 5] * ux * uy * uz
                    + 2 * m[..., 1] * ux * uy * uz**2
                    + 2 * m[..., 3] * ux**2 * uy * uz
                    - m[..., 0] * ux**2 * uy * uz**2
                )
                T = T.at[..., 25].set(
                    m[..., 25]
                    - 2 * m[..., 21] * ux
                    - 2 * m[..., 20] * uy
                    - m[..., 17] * uz
                    + m[..., 15] * ux**2
                    + m[..., 13] * uy**2
                    - 2 * m[..., 5] * ux * uy**2
                    - 2 * m[..., 6] * ux**2 * uy
                    - m[..., 8] * ux**2 * uz
                    - m[..., 7] * uy**2 * uz
                    + m[..., 3] * ux**2 * uy**2
                    + 4 * m[..., 16] * ux * uy
                    + 2 * m[..., 10] * ux * uz
                    + 2 * m[..., 12] * uy * uz
                    - 4 * m[..., 4] * ux * uy * uz
                    + 2 * m[..., 1] * ux * uy**2 * uz
                    + 2 * m[..., 2] * ux**2 * uy * uz
                    - m[..., 0] * ux**2 * uy**2 * uz
                )
                T = T.at[..., 26].set(
                    m[..., 0] * ux**2 * uy**2 * uz**2
                    - 2 * m[..., 3] * ux**2 * uy**2 * uz
                    + m[..., 9] * ux**2 * uy**2
                    - 2 * m[..., 2] * ux**2 * uy * uz**2
                    + 4 * m[..., 6] * ux**2 * uy * uz
                    - 2 * m[..., 14] * ux**2 * uy
                    + m[..., 8] * ux**2 * uz**2
                    - 2 * m[..., 15] * ux**2 * uz
                    + m[..., 19] * ux**2
                    - 2 * m[..., 1] * ux * uy**2 * uz**2
                    + 4 * m[..., 5] * ux * uy**2 * uz
                    - 2 * m[..., 11] * ux * uy**2
                    + 4 * m[..., 4] * ux * uy * uz**2
                    - 8 * m[..., 16] * ux * uy * uz
                    + 4 * m[..., 22] * ux * uy
                    - 2 * m[..., 10] * ux * uz**2
                    + 4 * m[..., 21] * ux * uz
                    - 2 * m[..., 23] * ux
                    + m[..., 7] * uy**2 * uz**2
                    - 2 * m[..., 13] * uy**2 * uz
                    + m[..., 18] * uy**2
                    - 2 * m[..., 12] * uy * uz**2
                    + 4 * m[..., 20] * uy * uz
                    - 2 * m[..., 24] * uy
                    + m[..., 17] * uz**2
                    - 2 * m[..., 25] * uz
                    + m[..., 26]
                )

                return T

            return shift(m, u)

    @partial(jit, static_argnums=(0,))
    def compute_central_moment_inverse(self, T, u):
        if isinstance(self.lattice, LatticeD2Q9):

            def shift_inverse(T, u):
                ux = u[..., 0]
                uy = u[..., 1]
                usq = ux**2 + uy**2
                udiff = ux**2 - uy**2
                m = jnp.zeros_like(T)
                m = m.at[..., 0].set(T[..., 0])
                m = m.at[..., 1].set(ux * T[..., 0] + T[..., 1])
                m = m.at[..., 2].set(uy * T[..., 0] + T[..., 2])
                m = m.at[..., 3].set(usq * T[..., 0] + 2 * ux * T[..., 1] + 2 * uy * T[..., 2] + T[..., 3])
                m = m.at[..., 4].set(udiff * T[..., 0] + 2 * ux * T[..., 1] - 2 * uy * T[..., 2] + T[..., 4])
                m = m.at[..., 5].set(ux * uy * T[..., 0] + uy * T[..., 1] + ux * T[..., 2] + T[..., 5])
                m = m.at[..., 6].set(
                    (ux**2) * uy * T[..., 0]
                    + 2 * ux * uy * T[..., 1]
                    + ux**2 * T[..., 2]
                    + 0.5 * uy * T[..., 3]
                    + 0.5 * uy * T[..., 4]
                    + 2 * ux * T[..., 5]
                    + T[..., 6]
                )
                m = m.at[..., 7].set(
                    (uy**2) * ux * T[..., 0]
                    + uy**2 * T[..., 1]
                    + 2 * ux * uy * T[..., 2]
                    + 0.5 * ux * T[..., 3]
                    - 0.5 * ux * T[..., 4]
                    + 2 * uy * T[..., 5]
                    + T[..., 7]
                )
                m = m.at[..., 8].set(
                    (uy**2 * ux**2) * T[..., 0]
                    + 2 * ux * uy**2 * T[..., 1]
                    + 2 * uy * ux**2 * T[..., 2]
                    + 0.5 * usq * T[..., 3]
                    - 0.5 * udiff * T[..., 4]
                    + 4 * ux * uy * T[..., 5]
                    + 2 * uy * T[..., 6]
                    + 2 * ux * T[..., 7]
                    + T[..., 8]
                )
                return m

            return shift_inverse(T, u)

        elif isinstance(self.lattice, LatticeD3Q19):

            def shift_inverse(T, u):
                ux = u[..., 0]
                uy = u[..., 1]
                uz = u[..., 2]
                m = jnp.zeros_like(T)
                m = m.at[..., 0].set(T[..., 0])
                m = m.at[..., 1].set(ux * T[..., 0] + T[..., 1])
                m = m.at[..., 2].set(uy * T[..., 0] + T[..., 2])
                m = m.at[..., 3].set(uz * T[..., 0] + T[..., 3])
                m = m.at[..., 4].set(ux * uy * T[..., 0] + uy * T[..., 1] + ux * T[..., 2] + T[..., 4])
                m = m.at[..., 5].set(ux * uz * T[..., 0] + uz * T[..., 1] + ux * T[..., 3] + T[..., 5])
                m = m.at[..., 6].set(uy * uz * T[..., 0] + uz * T[..., 2] + uy * T[..., 3] + T[..., 6])
                m = m.at[..., 7].set((ux**2) * T[..., 0] + 2 * ux * T[..., 1] + T[..., 7])
                m = m.at[..., 8].set((uy**2) * T[..., 0] + 2 * uy * T[..., 2] + T[..., 8])
                m = m.at[..., 9].set((uz**2) * T[..., 0] + 2 * uz * T[..., 3] + T[..., 9])
                m = m.at[..., 10].set(
                    ux * (uy**2) * T[..., 0] + (uy**2) * T[..., 1] + 2 * ux * uy * T[..., 2] + 2 * uy * T[..., 4] + ux * T[..., 8] + T[..., 10]
                )
                m = m.at[..., 11].set(
                    ux * (uz**2) * T[..., 0] + (uz**2) * T[..., 1] + 2 * ux * uz * T[..., 3] + 2 * uz * T[..., 5] + ux * T[..., 9] + T[..., 11]
                )
                m = m.at[..., 12].set(
                    (ux**2) * uy * T[..., 0] + 2 * ux * uy * T[..., 1] + (ux**2) * T[..., 2] + 2 * ux * T[..., 4] + uy * T[..., 7] + T[..., 12]
                )
                m = m.at[..., 13].set(
                    (ux**2) * uz * T[..., 0] + 2 * ux * uz * T[..., 1] + (ux**2) * T[..., 3] + 2 * ux * T[..., 5] + uz * T[..., 7] + T[..., 13]
                )
                m = m.at[..., 14].set(
                    uy * (uz**2) * T[..., 0] + (uz**2) * T[..., 2] + 2 * uy * uz * T[..., 3] + 2 * uz * T[..., 6] + uy * T[..., 9] + T[..., 14]
                )
                m = m.at[..., 15].set(
                    (uy**2) * uz * T[..., 0] + 2 * uy * uz * T[..., 2] + (uy**2) * T[..., 3] + 2 * uy * T[..., 6] + uz * T[..., 8] + T[..., 15]
                )
                m = m.at[..., 16].set(
                    (ux**2) * (uy**2) * T[..., 0]
                    + 2 * ux * (uy**2) * T[..., 1]
                    + 2 * uy * (ux**2) * T[..., 2]
                    + 4 * ux * uy * T[..., 4]
                    + (uy**2) * T[..., 7]
                    + (ux**2) * T[..., 8]
                    + 2 * ux * T[..., 10]
                    + 2 * uy * T[..., 12]
                    + T[..., 16]
                )
                m = m.at[..., 17].set(
                    (ux**2) * (uz**2) * T[..., 0]
                    + 2 * ux * (uz**2) * T[..., 1]
                    + 2 * uz * (ux**2) * T[..., 3]
                    + 4 * ux * uz * T[..., 5]
                    + (uz**2) * T[..., 7]
                    + (ux**2) * T[..., 9]
                    + 2 * ux * T[..., 11]
                    + 2 * uz * T[..., 13]
                    + T[..., 17]
                )
                m = m.at[..., 18].set(
                    (uy**2) * (uz**2) * T[..., 0]
                    + 2 * uy * (uz**2) * T[..., 2]
                    + 2 * uz * (uy**2) * T[..., 3]
                    + 4 * uy * uz * T[..., 6]
                    + (uz**2) * T[..., 8]
                    + (uy**2) * T[..., 9]
                    + 2 * uy * T[..., 14]
                    + 2 * uz * T[..., 15]
                    + T[..., 18]
                )
                return m

            return shift_inverse(T, u)

        elif isinstance(self.lattice, LatticeD3Q27):

            def shift_inverse(T, u):
                ux = u[..., 0]
                uy = u[..., 1]
                uz = u[..., 2]
                m = jnp.zeros_like(T)
                m = m.at[..., 0].set(T[..., 0])
                m = m.at[..., 1].set(T[..., 1] + T[..., 0] * ux)
                m = m.at[..., 2].set(T[..., 2] + T[..., 0] * uy)
                m = m.at[..., 3].set(T[..., 3] + T[..., 0] * uz)
                m = m.at[..., 4].set(T[..., 4] + T[..., 2] * ux + T[..., 1] * uy + T[..., 0] * ux * uy)
                m = m.at[..., 5].set(T[..., 5] + T[..., 3] * ux + T[..., 1] * uz + T[..., 0] * ux * uz)
                m = m.at[..., 6].set(T[..., 6] + T[..., 3] * uy + T[..., 2] * uz + T[..., 0] * uy * uz)
                m = m.at[..., 7].set(T[..., 0] * ux**2 + 2 * T[..., 1] * ux + T[..., 7])
                m = m.at[..., 8].set(T[..., 0] * uy**2 + 2 * T[..., 2] * uy + T[..., 8])
                m = m.at[..., 9].set(T[..., 0] * uz**2 + 2 * T[..., 3] * uz + T[..., 9])
                m = m.at[..., 10].set(
                    T[..., 10] + T[..., 8] * ux + 2 * T[..., 4] * uy + T[..., 1] * uy**2 + T[..., 0] * ux * uy**2 + 2 * T[..., 2] * ux * uy
                )
                m = m.at[..., 11].set(
                    T[..., 11] + T[..., 9] * ux + 2 * T[..., 5] * uz + T[..., 1] * uz**2 + T[..., 0] * ux * uz**2 + 2 * T[..., 3] * ux * uz
                )
                m = m.at[..., 12].set(
                    T[..., 12] + 2 * T[..., 4] * ux + T[..., 7] * uy + T[..., 2] * ux**2 + T[..., 0] * ux**2 * uy + 2 * T[..., 1] * ux * uy
                )
                m = m.at[..., 13].set(
                    T[..., 13] + 2 * T[..., 5] * ux + T[..., 7] * uz + T[..., 3] * ux**2 + T[..., 0] * ux**2 * uz + 2 * T[..., 1] * ux * uz
                )
                m = m.at[..., 14].set(
                    T[..., 14] + T[..., 9] * uy + 2 * T[..., 6] * uz + T[..., 2] * uz**2 + T[..., 0] * uy * uz**2 + 2 * T[..., 3] * uy * uz
                )
                m = m.at[..., 15].set(
                    T[..., 15] + 2 * T[..., 6] * uy + T[..., 8] * uz + T[..., 3] * uy**2 + T[..., 0] * uy**2 * uz + 2 * T[..., 2] * uy * uz
                )
                m = m.at[..., 16].set(
                    T[..., 16]
                    + T[..., 6] * ux
                    + T[..., 5] * uy
                    + T[..., 4] * uz
                    + T[..., 3] * ux * uy
                    + T[..., 2] * ux * uz
                    + T[..., 1] * uy * uz
                    + T[..., 0] * ux * uy * uz
                )
                m = m.at[..., 17].set(
                    T[..., 0] * ux**2 * uy**2
                    + 2 * T[..., 2] * ux**2 * uy
                    + T[..., 8] * ux**2
                    + 2 * T[..., 1] * ux * uy**2
                    + 4 * T[..., 4] * ux * uy
                    + 2 * T[..., 10] * ux
                    + T[..., 7] * uy**2
                    + 2 * T[..., 12] * uy
                    + T[..., 17]
                )
                m = m.at[..., 18].set(
                    T[..., 0] * ux**2 * uz**2
                    + 2 * T[..., 3] * ux**2 * uz
                    + T[..., 9] * ux**2
                    + 2 * T[..., 1] * ux * uz**2
                    + 4 * T[..., 5] * ux * uz
                    + 2 * T[..., 11] * ux
                    + T[..., 7] * uz**2
                    + 2 * T[..., 13] * uz
                    + T[..., 18]
                )
                m = m.at[..., 19].set(
                    T[..., 0] * uy**2 * uz**2
                    + 2 * T[..., 3] * uy**2 * uz
                    + T[..., 9] * uy**2
                    + 2 * T[..., 2] * uy * uz**2
                    + 4 * T[..., 6] * uy * uz
                    + 2 * T[..., 14] * uy
                    + T[..., 8] * uz**2
                    + 2 * T[..., 15] * uz
                    + T[..., 19]
                )
                m = m.at[..., 20].set(
                    T[..., 20]
                    + 2 * T[..., 16] * ux
                    + T[..., 13] * uy
                    + T[..., 12] * uz
                    + T[..., 6] * ux**2
                    + T[..., 3] * ux**2 * uy
                    + T[..., 2] * ux**2 * uz
                    + 2 * T[..., 5] * ux * uy
                    + 2 * T[..., 4] * ux * uz
                    + T[..., 7] * uy * uz
                    + 2 * T[..., 1] * ux * uy * uz
                    + T[..., 0] * ux**2 * uy * uz
                )
                m = m.at[..., 21].set(
                    T[..., 21]
                    + T[..., 15] * ux
                    + 2 * T[..., 16] * uy
                    + T[..., 10] * uz
                    + T[..., 5] * uy**2
                    + T[..., 3] * ux * uy**2
                    + T[..., 1] * uy**2 * uz
                    + 2 * T[..., 6] * ux * uy
                    + T[..., 8] * ux * uz
                    + 2 * T[..., 4] * uy * uz
                    + 2 * T[..., 2] * ux * uy * uz
                    + T[..., 0] * ux * uy**2 * uz
                )
                m = m.at[..., 22].set(
                    T[..., 22]
                    + T[..., 14] * ux
                    + T[..., 11] * uy
                    + 2 * T[..., 16] * uz
                    + T[..., 4] * uz**2
                    + T[..., 2] * ux * uz**2
                    + T[..., 1] * uy * uz**2
                    + T[..., 9] * ux * uy
                    + 2 * T[..., 6] * ux * uz
                    + 2 * T[..., 5] * uy * uz
                    + 2 * T[..., 3] * ux * uy * uz
                    + T[..., 0] * ux * uy * uz**2
                )
                m = m.at[..., 23].set(
                    T[..., 23]
                    + T[..., 19] * ux
                    + 2 * T[..., 22] * uy
                    + 2 * T[..., 21] * uz
                    + T[..., 11] * uy**2
                    + T[..., 10] * uz**2
                    + T[..., 9] * ux * uy**2
                    + T[..., 8] * ux * uz**2
                    + 2 * T[..., 4] * uy * uz**2
                    + 2 * T[..., 5] * uy**2 * uz
                    + T[..., 1] * uy**2 * uz**2
                    + 2 * T[..., 14] * ux * uy
                    + 2 * T[..., 15] * ux * uz
                    + 4 * T[..., 16] * uy * uz
                    + 4 * T[..., 6] * ux * uy * uz
                    + 2 * T[..., 2] * ux * uy * uz**2
                    + 2 * T[..., 3] * ux * uy**2 * uz
                    + T[..., 0] * ux * uy**2 * uz**2
                )
                m = m.at[..., 24].set(
                    T[..., 24]
                    + 2 * T[..., 22] * ux
                    + T[..., 18] * uy
                    + 2 * T[..., 20] * uz
                    + T[..., 14] * ux**2
                    + T[..., 12] * uz**2
                    + T[..., 9] * ux**2 * uy
                    + 2 * T[..., 4] * ux * uz**2
                    + 2 * T[..., 6] * ux**2 * uz
                    + T[..., 7] * uy * uz**2
                    + T[..., 2] * ux**2 * uz**2
                    + 2 * T[..., 11] * ux * uy
                    + 4 * T[..., 16] * ux * uz
                    + 2 * T[..., 13] * uy * uz
                    + 4 * T[..., 5] * ux * uy * uz
                    + 2 * T[..., 1] * ux * uy * uz**2
                    + 2 * T[..., 3] * ux**2 * uy * uz
                    + T[..., 0] * ux**2 * uy * uz**2
                )
                m = m.at[..., 25].set(
                    T[..., 25]
                    + 2 * T[..., 21] * ux
                    + 2 * T[..., 20] * uy
                    + T[..., 17] * uz
                    + T[..., 15] * ux**2
                    + T[..., 13] * uy**2
                    + 2 * T[..., 5] * ux * uy**2
                    + 2 * T[..., 6] * ux**2 * uy
                    + T[..., 8] * ux**2 * uz
                    + T[..., 7] * uy**2 * uz
                    + T[..., 3] * ux**2 * uy**2
                    + 4 * T[..., 16] * ux * uy
                    + 2 * T[..., 10] * ux * uz
                    + 2 * T[..., 12] * uy * uz
                    + 4 * T[..., 4] * ux * uy * uz
                    + 2 * T[..., 1] * ux * uy**2 * uz
                    + 2 * T[..., 2] * ux**2 * uy * uz
                    + T[..., 0] * ux**2 * uy**2 * uz
                )
                m = m.at[..., 26].set(
                    T[..., 0] * ux**2 * uy**2 * uz**2
                    + 2 * T[..., 3] * ux**2 * uy**2 * uz
                    + T[..., 9] * ux**2 * uy**2
                    + 2 * T[..., 2] * ux**2 * uy * uz**2
                    + 4 * T[..., 6] * ux**2 * uy * uz
                    + 2 * T[..., 14] * ux**2 * uy
                    + T[..., 8] * ux**2 * uz**2
                    + 2 * T[..., 15] * ux**2 * uz
                    + T[..., 19] * ux**2
                    + 2 * T[..., 1] * ux * uy**2 * uz**2
                    + 4 * T[..., 5] * ux * uy**2 * uz
                    + 2 * T[..., 11] * ux * uy**2
                    + 4 * T[..., 4] * ux * uy * uz**2
                    + 8 * T[..., 16] * ux * uy * uz
                    + 4 * T[..., 22] * ux * uy
                    + 2 * T[..., 10] * ux * uz**2
                    + 4 * T[..., 21] * ux * uz
                    + 2 * T[..., 23] * ux
                    + T[..., 7] * uy**2 * uz**2
                    + 2 * T[..., 13] * uy**2 * uz
                    + T[..., 18] * uy**2
                    + 2 * T[..., 12] * uy * uz**2
                    + 4 * T[..., 20] * uy * uz
                    + 2 * T[..., 24] * uy
                    + T[..., 17] * uz**2
                    + 2 * T[..., 25] * uz
                    + T[..., 26]
                )
                return m

            return shift_inverse(T, u)

    @partial(jit, static_argnums=(0,))
    def compute_eq_central_moments(self, rho):
        """
        Calculate the central moments of the equilibrium distribution.

        Parameters
        ----------
        rho (jax.numpy.ndarray): Density field.

        Returns
        -------
        T_eq : jax.numpy.ndarray
            Central moments of the equilibrium distribution.
        """

        if isinstance(self.lattice, LatticeD2Q9):
            T_eq = jnp.zeros((self.nx, self.ny, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            T_eq = T_eq.at[..., 0].set(rho[..., 0])
            T_eq = T_eq.at[..., 3].set(2 * rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs**4)

            return T_eq

        elif isinstance(self.lattice, LatticeD3Q19):
            T_eq = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            T_eq = T_eq.at[..., 0].set(rho[..., 0])
            T_eq = T_eq.at[..., 7].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 9].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 16].set(rho[..., 0] * self.lattice.cs**4)
            T_eq = T_eq.at[..., 17].set(rho[..., 0] * self.lattice.cs**4)
            T_eq = T_eq.at[..., 18].set(rho[..., 0] * self.lattice.cs**4)

            return T_eq

        elif isinstance(self.lattice, LatticeD3Q27):
            T_eq = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            T_eq = T_eq.at[..., 0].set(rho[..., 0])
            T_eq = T_eq.at[..., 7].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 9].set(rho[..., 0] * self.lattice.cs2)
            T_eq = T_eq.at[..., 17].set(rho[..., 0] * self.lattice.cs**4)
            T_eq = T_eq.at[..., 18].set(rho[..., 0] * self.lattice.cs**4)
            T_eq = T_eq.at[..., 19].set(rho[..., 0] * self.lattice.cs**4)
            T_eq = T_eq.at[..., 26].set(rho[..., 0] * self.lattice.cs**6)

            return T_eq

    @partial(jit, static_argnums=(0,))
    def compute_force_central_moments(self, F):
        """
        Calculate the central moments of the force distribution. Includes modification to accurately replicate mechanical stability conditions.

        Parameters
        ----------
        F (pytree of jax.numpy.ndarray): Force field.

        Returns
        -------
        C : pytree of jax.numpy.ndarray
            Central moments of the force distribution.
        """

        if isinstance(self.lattice, LatticeD2Q9):
            C = jnp.zeros((self.nx, self.ny, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            Fx = F[..., 0]
            Fy = F[..., 1]
            C = C.at[..., 1].set(Fx)
            C = C.at[..., 2].set(Fy)
            C = C.at[..., 6].set(Fy * self.lattice.cs2)
            C = C.at[..., 7].set(Fx * self.lattice.cs2)

            return C
        elif isinstance(self.lattice, LatticeD3Q19):
            C = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            Fx = F[..., 0]
            Fy = F[..., 1]
            Fz = F[..., 2]
            C = C.at[..., 1].set(Fx)
            C = C.at[..., 2].set(Fy)
            C = C.at[..., 3].set(Fz)
            C = C.at[..., 10].set(Fx * self.lattice.cs2)
            C = C.at[..., 11].set(Fx * self.lattice.cs2)
            C = C.at[..., 12].set(Fy * self.lattice.cs2)
            C = C.at[..., 13].set(Fz * self.lattice.cs2)
            C = C.at[..., 14].set(Fy * self.lattice.cs2)
            C = C.at[..., 15].set(Fz * self.lattice.cs2)

            return C
        elif isinstance(self.lattice, LatticeD3Q27):
            C = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
            Fx = F[..., 0]
            Fy = F[..., 1]
            Fz = F[..., 2]
            C = C.at[..., 1].set(Fx)
            C = C.at[..., 2].set(Fy)
            C = C.at[..., 3].set(Fz)
            C = C.at[..., 10].set(Fx * self.lattice.cs2)
            C = C.at[..., 11].set(Fx * self.lattice.cs2)
            C = C.at[..., 12].set(Fy * self.lattice.cs2)
            C = C.at[..., 13].set(Fz * self.lattice.cs2)
            C = C.at[..., 14].set(Fy * self.lattice.cs2)
            C = C.at[..., 15].set(Fz * self.lattice.cs2)
            C = C.at[..., 23].set(Fx * self.lattice.cs**4)
            C = C.at[..., 24].set(Fy * self.lattice.cs**4)
            C = C.at[..., 25].set(Fz * self.lattice.cs**4)

            return C

    @partial(jit, static_argnums=(0,), inline=True)
    def apply_force(self, Tdash, rho, u):
        """
        Modified version of the apply_force defined in LBMBase to account for modified force.

        Parameters
        ----------
            Tdash (jax.numpy.ndarray): Central moments post-collision distribution functions.

            rho (jax.numpy.ndarray): Density field.

            u (jax.numpy.ndarray): Velocity field.

        Returns
        -------
            f_postcollision (jax.numpy.ndarray): Post-collision distribution functions with the force applied.
        """
        F = self.get_force()
        if F is None:
            F = jnp.zeros_like(u)
        C = self.compute_force_central_moments(F)
        Tf = jnp.dot(C, jnp.eye(self.lattice.q) - 0.5 * self.S)
        return Tdash + Tf

    @partial(jit, static_argnums=(0,))
    def collision(self, fin):
        """
        Cascaded LBM collision step for lattice.
        """
        fin = self.precision_policy.cast_to_compute(fin)
        rho, _ = self.update_macroscopic(fin)
        u = self.macroscopic_velocity(fin, rho)
        T = jnp.dot(fin, self.M)
        Tdash = self.compute_central_moment(T, u)
        Tdash_eq = self.compute_eq_central_moments(rho)
        Tout = jnp.dot(Tdash, jnp.eye(self.lattice.q) - self.S) + jnp.dot(Tdash_eq, self.S)
        Tout = self.apply_force(Tout, rho, u)
        Tout = self.compute_central_moment_inverse(Tout, u)
        fout = jnp.dot(T, self.M_inv)
        return self.precision_policy.cast_to_output(fout)

apply_force

apply_force(Tdash, rho, u)

Modified version of the apply_force defined in LBMBase to account for modified force.

Parameters
1
2
3
4
5
Tdash (jax.numpy.ndarray): Central moments post-collision distribution functions.

rho (jax.numpy.ndarray): Density field.

u (jax.numpy.ndarray): Velocity field.
Returns
1
f_postcollision (jax.numpy.ndarray): Post-collision distribution functions with the force applied.
Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def apply_force(self, Tdash, rho, u):
    """
    Modified version of the apply_force defined in LBMBase to account for modified force.

    Parameters
    ----------
        Tdash (jax.numpy.ndarray): Central moments post-collision distribution functions.

        rho (jax.numpy.ndarray): Density field.

        u (jax.numpy.ndarray): Velocity field.

    Returns
    -------
        f_postcollision (jax.numpy.ndarray): Post-collision distribution functions with the force applied.
    """
    F = self.get_force()
    if F is None:
        F = jnp.zeros_like(u)
    C = self.compute_force_central_moments(F)
    Tf = jnp.dot(C, jnp.eye(self.lattice.q) - 0.5 * self.S)
    return Tdash + Tf

collision

collision(fin)

Cascaded LBM collision step for lattice.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def collision(self, fin):
    """
    Cascaded LBM collision step for lattice.
    """
    fin = self.precision_policy.cast_to_compute(fin)
    rho, _ = self.update_macroscopic(fin)
    u = self.macroscopic_velocity(fin, rho)
    T = jnp.dot(fin, self.M)
    Tdash = self.compute_central_moment(T, u)
    Tdash_eq = self.compute_eq_central_moments(rho)
    Tout = jnp.dot(Tdash, jnp.eye(self.lattice.q) - self.S) + jnp.dot(Tdash_eq, self.S)
    Tout = self.apply_force(Tout, rho, u)
    Tout = self.compute_central_moment_inverse(Tout, u)
    fout = jnp.dot(T, self.M_inv)
    return self.precision_policy.cast_to_output(fout)

compute_eq_central_moments

compute_eq_central_moments(rho)

Calculate the central moments of the equilibrium distribution.

Parameters

rho (jax.numpy.ndarray): Density field.

Returns

T_eq : jax.numpy.ndarray Central moments of the equilibrium distribution.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def compute_eq_central_moments(self, rho):
    """
    Calculate the central moments of the equilibrium distribution.

    Parameters
    ----------
    rho (jax.numpy.ndarray): Density field.

    Returns
    -------
    T_eq : jax.numpy.ndarray
        Central moments of the equilibrium distribution.
    """

    if isinstance(self.lattice, LatticeD2Q9):
        T_eq = jnp.zeros((self.nx, self.ny, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        T_eq = T_eq.at[..., 0].set(rho[..., 0])
        T_eq = T_eq.at[..., 3].set(2 * rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs**4)

        return T_eq

    elif isinstance(self.lattice, LatticeD3Q19):
        T_eq = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        T_eq = T_eq.at[..., 0].set(rho[..., 0])
        T_eq = T_eq.at[..., 7].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 9].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 16].set(rho[..., 0] * self.lattice.cs**4)
        T_eq = T_eq.at[..., 17].set(rho[..., 0] * self.lattice.cs**4)
        T_eq = T_eq.at[..., 18].set(rho[..., 0] * self.lattice.cs**4)

        return T_eq

    elif isinstance(self.lattice, LatticeD3Q27):
        T_eq = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        T_eq = T_eq.at[..., 0].set(rho[..., 0])
        T_eq = T_eq.at[..., 7].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 8].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 9].set(rho[..., 0] * self.lattice.cs2)
        T_eq = T_eq.at[..., 17].set(rho[..., 0] * self.lattice.cs**4)
        T_eq = T_eq.at[..., 18].set(rho[..., 0] * self.lattice.cs**4)
        T_eq = T_eq.at[..., 19].set(rho[..., 0] * self.lattice.cs**4)
        T_eq = T_eq.at[..., 26].set(rho[..., 0] * self.lattice.cs**6)

        return T_eq

compute_force_central_moments

compute_force_central_moments(F)

Calculate the central moments of the force distribution. Includes modification to accurately replicate mechanical stability conditions.

Parameters

F (pytree of jax.numpy.ndarray): Force field.

Returns

C : pytree of jax.numpy.ndarray Central moments of the force distribution.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,))
def compute_force_central_moments(self, F):
    """
    Calculate the central moments of the force distribution. Includes modification to accurately replicate mechanical stability conditions.

    Parameters
    ----------
    F (pytree of jax.numpy.ndarray): Force field.

    Returns
    -------
    C : pytree of jax.numpy.ndarray
        Central moments of the force distribution.
    """

    if isinstance(self.lattice, LatticeD2Q9):
        C = jnp.zeros((self.nx, self.ny, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        Fx = F[..., 0]
        Fy = F[..., 1]
        C = C.at[..., 1].set(Fx)
        C = C.at[..., 2].set(Fy)
        C = C.at[..., 6].set(Fy * self.lattice.cs2)
        C = C.at[..., 7].set(Fx * self.lattice.cs2)

        return C
    elif isinstance(self.lattice, LatticeD3Q19):
        C = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        Fx = F[..., 0]
        Fy = F[..., 1]
        Fz = F[..., 2]
        C = C.at[..., 1].set(Fx)
        C = C.at[..., 2].set(Fy)
        C = C.at[..., 3].set(Fz)
        C = C.at[..., 10].set(Fx * self.lattice.cs2)
        C = C.at[..., 11].set(Fx * self.lattice.cs2)
        C = C.at[..., 12].set(Fy * self.lattice.cs2)
        C = C.at[..., 13].set(Fz * self.lattice.cs2)
        C = C.at[..., 14].set(Fy * self.lattice.cs2)
        C = C.at[..., 15].set(Fz * self.lattice.cs2)

        return C
    elif isinstance(self.lattice, LatticeD3Q27):
        C = jnp.zeros((self.nx, self.ny, self.nz, self.lattice.q), dtype=self.precision_policy.compute_dtype)
        Fx = F[..., 0]
        Fy = F[..., 1]
        Fz = F[..., 2]
        C = C.at[..., 1].set(Fx)
        C = C.at[..., 2].set(Fy)
        C = C.at[..., 3].set(Fz)
        C = C.at[..., 10].set(Fx * self.lattice.cs2)
        C = C.at[..., 11].set(Fx * self.lattice.cs2)
        C = C.at[..., 12].set(Fy * self.lattice.cs2)
        C = C.at[..., 13].set(Fz * self.lattice.cs2)
        C = C.at[..., 14].set(Fy * self.lattice.cs2)
        C = C.at[..., 15].set(Fz * self.lattice.cs2)
        C = C.at[..., 23].set(Fx * self.lattice.cs**4)
        C = C.at[..., 24].set(Fy * self.lattice.cs**4)
        C = C.at[..., 25].set(Fz * self.lattice.cs**4)

        return C

macroscopic_velocity

macroscopic_velocity(f, rho)

macroscopic_velocity computes the velocity and incorporates forces into velocity for Exact Difference Method (EDM) (used for SRT and MRT collision) models and the consistent forcing scheme developed by LinLin Fei et. al (for Cascaded LBM). This is used for post-processing only and not for equilibrium distribution computation.

Parameters

f (jax.numpy.ndarray): Distribution arrays.

rho (jax.numpy.ndarray): Density fields.

Returns

u: jax.numpy.ndarray Velocity fields.

Source code in jax_lab/core/models.py
@partial(jit, static_argnums=(0,), inline=True)
def macroscopic_velocity(self, f, rho):
    """
    macroscopic_velocity computes the velocity and incorporates forces into velocity for Exact Difference Method (EDM) (used for SRT and MRT collision) models
    and the consistent forcing scheme developed by LinLin Fei et. al (for Cascaded LBM). This is used for post-processing only and not for equilibrium distribution computation.

    Parameters
    ----------
    f (jax.numpy.ndarray): Distribution arrays.

    rho (jax.numpy.ndarray): Density fields.

    Returns
    -------
    u: jax.numpy.ndarray
        Velocity fields.
    """
    # rho_tree = map(lambda f: jnp.sum(f, axis=-1, keepdims=True), f_tree)
    c = jnp.array(self.c, dtype=self.precision_policy.compute_dtype).T
    u = jnp.dot(f, c) / rho
    if self.force is not None:
        return u + 0.5 * self.force / rho
    else:
        return u