# D2Q9 Single Relexation Time (SRT)

This tutorial will try to teach you how to make and use lattice Boltzmann (LB) models within the TCLB environment. First a brief overview will be given of the discrete lattice Boltzmann equation taking advantage of the Bhatnagar-Gross-Krook (BGK) collision operator (also known as single relaxation time operator) will be given, then the code required to simulate Poiseuille flow will be developed.

## The SRT-lattice Boltzmann Equation

The discrete form of the Boltzmann transport equation can be expressed as:

This describes the evolution of particle distribution functions along discrete velocities, where the right hand side is often referred to as the collision operation and the evaluation to the left hand side, the streaming operation. Note here that we are going to work in lattice units during the description of the model in which we assume $\Delta x = \Delta t = 1$ For the D2Q9 model, this assumption allows us to express the 9 discrete directions as,

Additionally, we define the equilibrium distribution function found by an expansion of a Maxwellian distribution as,

where,

### Boundary Conditions and Force Implementation

For this specific example, we are looking at a flow through a channel. We can utilise periodic conditions along the x-axis but have to implement no-slip wall conditions to limit domain along y-axis. To do this with LBM, the bounceback method is used. Particle distribution functions that stream into a node flagged as a "wall" are reversed, or "bounced-back" in the opposite direction Therefore, in general for a D2Q9 lattice we obtain:

where ' indicates the post-bounce-back direction.

To drive the fluid in the Poiseuille flow example, a body force will be applied. For this we consider a body force G acting in each time increment resulting in a momentum change $\Delta\rho u = G$ . To incorporate this, we modify the equilibrium velocity,

This approach ultimately acts to relax each particle distribution function towards an equilibrium momentum that has included the time-incremental change due to the applied body force. To obtain the bulk fluid velocity, the before and after collision momentum is averaged giving,

and with this we now have all we the dynamics required to implement the specified example.

## Model Creation in TCLB

As in the previous tutorial, we want to set up a folder named d2q9srt in ~TCLB/models/tutorial/ and create the generic file structure (conf.mk, Dynamics.c, Dynamics.R).

To start off the model, we look to add the nine distribution functions for the nine discrete velocities. This is implemented in the Dynamics.R file, but instead of adding each as its own field we look to stream the functions in the process of calling them at each node. To do this we instead add them as Densities:

AddDensity(name="f", dx=0, dy=0  )


Notice that the dx and dy coordinates correspond with the c matrix previously given. At this stage, it helps to assess what other values will be needed to initialise and run the LBM. To start off the method, a fluid density and initial velocities must be specified, then to perform the collision operation the relaxation time and the magnitude of the applied body force need to be defined . Additionally, from this simulation we want to be able to interrogate the macroscopic fluid velocity and density so these must be added as Quantities. For this, we define (in Dynamics.R):

AddQuantity( name="U",unit="m/s", vector=TRUE )

AddSetting( name="omega", comment='inverse of relaxation time')


From the above, we have all the variables we need to implement the LBM. The first step is to initialise the lattice over the required domain, this is incorporated as part of the dynamics that are occurring in the model and must be incorporated into the Dynamics.c file within the Init() function.

CudaDeviceFunction void Init(){
real_t u = {Velocity, 0.0};
real_t d = Density;
SetEquilibrium(d,u);
}


Notice here that we are calling on the function SetEquilibrium() within which we want to calculate the equilibrium distribution for the given density and velocity fields.

CudaDeviceFunction void SetEquilibrium(real_t d, real_t u)
{
f = ( 2. + ( -u*u - u*u )*3. )*d*2./9.;
f = ( 2. + ( -u*u + ( 1 + u )*u*2. )*3. )*d/18.;
f = ( 2. + ( -u*u + ( 1 + u )*u*2. )*3. )*d/18.;
f = ( 2. + ( -u*u + ( -1 + u )*u*2. )*3. )*d/18.;
f = ( 2. + ( -u*u + ( -1 + u )*u*2. )*3. )*d/18.;
f = ( 1. + ( ( 1 + u )*u + ( 1 + u + u*3. )*u )*3. )*d/36.;
f = ( 1. + ( ( 1 + u )*u + ( -1 + u - u*3. )*u )*3. )*d/36.;
f = ( 1. + ( ( -1 + u )*u + ( -1 + u + u*3. )*u )*3. )*d/36.;
f = ( 1. + ( ( -1 + u )*u + ( 1 + u - u*3. )*u )*3. )*d/36.;
}


With the equilibrium function taken care of, we can now look at updating our initialised lattice. For this, the streaming operation is taken care of with the specification of Densities that we made. The collision operation however needs to be implemented and bounce-back for nodes flagged as walls. To do this, we define the run() function to describe what happens at each node at each timestep.

CudaDeviceFunction void Run() {
// This defines the dynamics that we run at each node in the domain.
switch (NodeType & NODE_BOUNDARY) {
case NODE_Wall:
BounceBack();
break;
}
if ((NodeType & NODE_BOUNDARY) == NODE_BGK)
{
CollisionBGK();
}
}


Don't forget to declare new node types in Dynamics.R.

Additionally, in all Dynamics.c files the Color() function is required. (even with ./configure --disable-graphics).

CudaDeviceFunction float2 Color() {
float2 ret;
ret.x = 0;
ret.y = 1;
return ret;
}


The switch function described can also be used for the implementation of velocity/pressure boundaries, but these will not be discussed in this tutorial. As can be seen above, we need to define functions that describe both the BounceBack() and CollisionBGK() operations. The bounce-back operation occurs as described in the theory component of this tutorial where distribution functions are reversed:

CudaDeviceFunction void BounceBack() {
// Method to reverse distribution functions along the bounding nodes.
real_t uf;
uf = f;
f = f;
f = uf;
uf = f;
f = f;
f = uf;
uf = f;
f = f;
f = uf;
uf = f;
f = f;
f = uf;
}


The BGK collision, from the left hand side of the discrete Boltzmann equation:

CudaDeviceFunction void CollisionBGK() {
// Here we perform a single relaxation time collision operation.
// We save memory here by using a single dummy variable
real_t u, d, f_temp;
d = getRho();
// pu* = pu + rG
u = (( f-f-f+f-f+f )/d + GravitationX/omega );
u = ((-f-f+f+f-f+f )/d + GravitationY/omega );
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
f_temp = f;
SetEquilibrium(d, u); //stores equilibrium distribution in f-f
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
f = f_temp - omega*(f_temp-f);
}


With all the dynamics described, all that is left to do now is calculate the macroscopic variables using Get functions to correspond with the Quantities stated in Dynamics.R.The macroscopic density is the sum of all the distribution functions and the momentum is the sum of the distribution functions multiplied by the discrete velocity directions.

CudaDeviceFunction real_t getRho() {
// This function defines the macroscopic density at the current node.
return f+f+f+f+f+f+f+f+f;
}

// This function defines the macroscopic velocity at the current node.
real_t d = f+f+f+f+f+f+f+f+f;
vector_t u;
// pv = pu + G/2
u.x = (( f-f-f+f-f+f )/d + GravitationX*0.5 );
u.y = ((-f-f+f+f-f+f )/d + GravitationY*0.5 );
u.z = 0;
return u;
}


## Setting up a Simulation

In order validate this example, we want to specify the relation between physical and lattice units. For this, we need provide the relations for the spatial and time dimensions. The most natural way to do this is to give the physical dx size and the actual fluid viscosity. This information is stored at the start of the xml input file under the parent heading of <Units>, for this you want to first create a new file by the name of d2q9_poiseuille.xml:

<?xml version="1.0"?>
<CLBConfig version="2.0" output="output/">
<Units>
<Params size="0.0005m" gauge="1"/>
<Params nu="1e-5m2/s" gauge="0.1666666666"/>
</Units>


The reason for this, is it gives not only a view of how to implement physical values in TCLB but gives a straightforward method to calculate the expected maximum physical velocity. We do this through a comparison of the maximum velocity which in Poiseuille is given by:

The setup of the test simulation domain is arbitrary and different values can be tested from those shown here. A channel of 0.02m length and 0.0095m height is chosen with an applied body force of 0.000311634m/s^2. As stated above one lattice spacing is equal to 0.0005m so the total size of the simulation domain will be 0.02 x (0.0095+2*0.0005) m. This is defined as the geometry of the system in the xml set up file noting that the in-built <Channel> function is used to specify the bottom and top nodes as walls:

<Geometry nx="0.02m" ny="0.0105m">
<BGK><Box/></BGK>
<Channel/>
</Wall>
</Geometry>


Here we can see that the collision method specified for the cells in the entire domain is BGK to align with the Run function. Following on from the geometry of the domain, the model specific settings (defined in Dynamics.R) need to be defined. For this we take a fluid of density 1000kg/m^3 and choose a relaxation time of the value 1.

<Model>
<Params Velocity="0.0"/>
<Params omega="1.0"/>
<Params GravitationX="0.000311634m/s2"/>
<Params Density="1000kg/m3"/>
</Model>


The final step is to output the desired quantities both at the start (so we can be sure that we have initialised the lattice correctly) and after the final solution step.

    <VTK/>
<VTK Iterations="50000"/>
<Solve Iterations="50000"/>
</CLBConfig>


With the model and set-up files created, we can now look to make and run d2q9srt. To do this first enter the TCLB directory and call the run file along with the input file location, after this, the analysis can be performed in Paraview:

make d2q9srt
CLB/d2q9srt/main d2q9_poiseuille.xml
paraview output/d2q9_poiseuille_VTK_P00_..pvti


In case the calculations are run on CPU, an mpi run can also be initiated e.g:

mpirun -np 4 CLB/d2q9srt/main d2q9_poiseuille.xml


From the analytical equation given before, we expect a maximum velocity of 0.000351562m/s, while in the results it is found to be 0.00035293m/s showing an error of 0.389%.
{"ft_lang_label":"__label__en","ft_lang_prob":0.72035056,"math_prob":0.9828334,"size":11849,"snap":"2019-51-2020-05","text_gpt3_token_len":3526,"char_repetition_ratio":0.115660615,"word_repetition_ratio":0.04769648,"special_character_ratio":0.316398,"punctuation_ratio":0.12453145,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99725246,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-07T22:41:07Z\",\"WARC-Record-ID\":\"<urn:uuid:9270ced8-9687-463b-95b4-1c6336e7bc44>\",\"Content-Length\":\"162302\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3934cc85-6948-49c5-9ecd-ccbb2ac5760e>\",\"WARC-Concurrent-To\":\"<urn:uuid:1a0169fe-010c-41e0-9a2d-5ffefcdec520>\",\"WARC-IP-Address\":\"104.248.63.231\",\"WARC-Target-URI\":\"https://tclb-docs.netlify.com/tutorials/model-development/2.-d2q9-single-relexation-time/\",\"WARC-Payload-Digest\":\"sha1:BRMPAWMZVIB5EVLQAF327ECZ2N34SFBV\",\"WARC-Block-Digest\":\"sha1:RVX2CNDN37NSBAIHRD4QVJFM6Z3F467C\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540502120.37_warc_CC-MAIN-20191207210620-20191207234620-00033.warc.gz\"}"}