{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The conservative Allen-Cahn model for high Reynolds number, two phase flow with large-density and viscosity constrast"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pystencils.session import *\n",
"from lbmpy.session import *\n",
"\n",
"from pystencils.simp import sympy_cse\n",
"from pystencils.boundaries import BoundaryHandling\n",
"\n",
"from lbmpy.phasefield_allen_cahn.contact_angle import ContactAngle\n",
"from lbmpy.phasefield_allen_cahn.force_model import MultiphaseForceModel, CentralMomentMultiphaseForceModel\n",
"from lbmpy.phasefield_allen_cahn.kernel_equations import *\n",
"from lbmpy.phasefield_allen_cahn.parameter_calculation import calculate_parameters_rti\n",
"\n",
"from lbmpy.advanced_streaming import LBMPeriodicityHandling\n",
"from lbmpy.boundaries import NoSlip, LatticeBoltzmannBoundaryHandling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If `pycuda` is installed the simulation automatically runs on GPU"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No pycuda installed\n"
]
}
],
"source": [
"try:\n",
" import pycuda\n",
"except ImportError:\n",
" pycuda = None\n",
" gpu = False\n",
" target = ps.Target.CPU\n",
" print('No pycuda installed')\n",
"\n",
"if pycuda:\n",
" gpu = True\n",
" target = ps.Target.GPU"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The conservative Allen-Cahn model (CACM) for two-phase flow is based on the work of Fakhari et al. (2017) [Improved locality of the phase-field lattice-Boltzmann model for immiscible fluids at high density ratios](http://dx.doi.org/10.1103/PhysRevE.96.053301). The model can be created for two-dimensional problems as well as three-dimensional problems, which have been described by Mitchell et al. (2018) [Development of a three-dimensional\n",
"phase-field lattice Boltzmann method for the study of immiscible fluids at high density ratios](http://dx.doi.org/10.1103/PhysRevE.96.053301). Furthermore, cascaded lattice Boltzmann methods can be combined with the model which was described in [A cascaded phase-field lattice Boltzmann model for the simulation of incompressible, immiscible fluids with high density contrast](http://dx.doi.org/10.1016/j.camwa.2019.08.018)\n",
"\n",
"\n",
"The CACM is suitable for simulating highly complex two phase flow problems with high-density ratios and high Reynolds numbers. In this tutorial, an overview is provided on how to derive the model with lbmpy. For this, the model is defined with two LBM populations. One for the interface tracking, which we call the phase-field LB step and one for recovering the hydrodynamic properties. The latter is called the hydrodynamic LB step."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geometry Setup\n",
"\n",
"First of all, the stencils for the phase-field LB step as well as the stencil for the hydrodynamic LB step are defined. According to the stencils, the simulation can be performed in either 2D- or 3D-space. For 2D simulations, only the D2Q9 stencil is supported. For 3D simulations, the D3Q15, D3Q19 and the D3Q27 stencil are supported. Note here that the cascaded LBM can not be derived for D3Q15 stencils."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"stencil_phase = LBStencil(Stencil.D2Q9)\n",
"stencil_hydro = LBStencil(Stencil.D2Q9)\n",
"assert(len(stencil_phase[0]) == len(stencil_hydro[0]))\n",
"\n",
"dimensions = len(stencil_phase[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Definition of the domain size"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# domain \n",
"L0 = 256\n",
"domain_size = (L0, 4 * L0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Parameter definition\n",
"\n",
"The next step is to calculate all parameters which are needed for the simulation. In this example, a Rayleigh-Taylor instability test case is set up. The parameter calculation for this setup is already implemented in lbmpy and can be used with the dimensionless parameters which describe the problem."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# time step\n",
"timesteps = 8000\n",
"\n",
"# reference time\n",
"reference_time = 4000\n",
"# density of the heavier fluid\n",
"rho_H = 1.0\n",
"\n",
"# calculate the parameters for the RTI\n",
"parameters = calculate_parameters_rti(reference_length=L0,\n",
" reference_time=reference_time,\n",
" density_heavy=rho_H,\n",
" capillary_number=0.44,\n",
" reynolds_number=3000,\n",
" atwood_number=0.998,\n",
" peclet_number=1000,\n",
" density_ratio=1000,\n",
" viscosity_ratio=100)\n",
"# get the parameters\n",
"rho_L = parameters.get(\"density_light\")\n",
"\n",
"mu_H = parameters.get(\"dynamic_viscosity_heavy\")\n",
"mu_L = parameters.get(\"dynamic_viscosity_light\")\n",
"\n",
"tau_H = parameters.get(\"relaxation_time_heavy\")\n",
"tau_L = parameters.get(\"relaxation_time_light\")\n",
"\n",
"sigma = parameters.get(\"surface_tension\")\n",
"M = parameters.get(\"mobility\")\n",
"gravitational_acceleration = parameters.get(\"gravitational_acceleration\")\n",
"\n",
"\n",
"drho3 = (rho_H - rho_L)/3\n",
"# interface thickness\n",
"W = 5\n",
"# coeffcient related to surface tension\n",
"beta = 12.0 * (sigma/W)\n",
"# coeffcient related to surface tension\n",
"kappa = 1.5 * sigma*W\n",
"# relaxation rate allen cahn (h)\n",
"w_c = 1.0/(0.5 + (3.0 * M))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fields\n",
"\n",
"As a next step all fields which are needed get defined. To do so, we create a `datahandling` object. More details about it can be found in the third tutorial of the [pystencils framework]( http://pycodegen.pages.walberla.net/pystencils/). This object holds all fields and manages the kernel runs."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# create a datahandling object\n",
"dh = ps.create_data_handling((domain_size), periodicity=(True, False), parallel=False, default_target=target)\n",
"\n",
"# pdf fields. g is used for hydrodynamics and h for the interface tracking \n",
"g = dh.add_array(\"g\", values_per_cell=len(stencil_hydro))\n",
"dh.fill(\"g\", 0.0, ghost_layers=True)\n",
"h = dh.add_array(\"h\",values_per_cell=len(stencil_phase))\n",
"dh.fill(\"h\", 0.0, ghost_layers=True)\n",
"\n",
"g_tmp = dh.add_array(\"g_tmp\", values_per_cell=len(stencil_hydro))\n",
"dh.fill(\"g_tmp\", 0.0, ghost_layers=True)\n",
"h_tmp = dh.add_array(\"h_tmp\",values_per_cell=len(stencil_phase))\n",
"dh.fill(\"h_tmp\", 0.0, ghost_layers=True)\n",
"\n",
"# velocity field\n",
"u = dh.add_array(\"u\", values_per_cell=dh.dim)\n",
"dh.fill(\"u\", 0.0, ghost_layers=True)\n",
"\n",
"# phase-field\n",
"C = dh.add_array(\"C\")\n",
"dh.fill(\"C\", 0.0, ghost_layers=True)\n",
"C_tmp = dh.add_array(\"C_tmp\")\n",
"dh.fill(\"C_tmp\", 0.0, ghost_layers=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a next step the relaxation time is stated in a symbolic form. It is calculated via interpolation."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# relaxation time and rate\n",
"tau = 0.5 + tau_L + (C.center) * (tau_H - tau_L)\n",
"s8 = 1/(tau)\n",
"\n",
"# density for the whole domain\n",
"rho = rho_L + (C.center) * (rho_H - rho_L)\n",
"\n",
"# body force\n",
"body_force = [0, 0, 0]\n",
"body_force[1] = gravitational_acceleration * rho"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Definition of the lattice Boltzmann methods"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For both LB steps, a weighted orthogonal MRT (WMRT) method is used. It is also possible to change the method to a simpler SRT scheme or a more complicated CLBM scheme. The CLBM scheme can be obtained by commenting in the python snippets in the notebook cells below. Note here that the hydrodynamic LB step is formulated as an incompressible velocity-based LBM. Thus, the velocity terms can not be removed from the equilibrium in the central moment space."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"