BHL Notes

MY LEARNING TRAJECTORY

0%

OpenFOAM Tutorial - Lid Driven Cavity

Lid-driven cavity flow

The lid-driven cavity flow tutorial is the easiest and most well documented tutorial on OpenFOAM. A incompressible flow in a two-dimensional square domain is shown in figure below. The top wall moves in the $x$-direction at a speed of 1 m/s while the other 3 are stationary. Initially, the flow will be assumed laminar and will be solved on a uniform and non mesh using the icoFoam solver for laminar, isothermal, incompressible flow. Then, the effect of increased mesh resolution and mesh grading towards the walls will be investigated.

Geometry of the lid driven cavity
Geometry of the lid driven cavity


Setting up the case

Running directory

As a first step, we copy the cavity case directory to the running directory.

cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity <your running directory>

Mesh generation

OpenFOAM solves the case in 3 dimensions by default but can be instructed to solve in 2 dimensions by specifying a ‘special’ empty boundary condition on boundaries normal to the (3rd) dimension for which no solution is required. The mesh generator, blockMesh, generates meshes from a description specified in an input dictionary, blockMeshDict located in the system (or constant/polyMesh) directory for a given case. The blockMeshDict entries for this case are as follows:

blockMeshDict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/\*--------------------------------\*- C++ -\*----------------------------------\*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\\*---------------------------------------------------------------------------\*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* //

convertToMeters 0.1; // scaled to the real problem dimensions(deafault is meter)

vertices
(
(0 0 0) // vertices 0
(1 0 0) // vertices 1
(1 1 0) // vertices 2
(0 1 0) // vertices 3
(0 0 0.1) // vertices 4
(1 0 0.1) // vertices 5
(1 1 0.1) // vertices 6
(0 1 0.1) // vertices 7
);

// defines the blocks using the vertex indices and the cell spacing in 3 directions
blocks
(

hex (0 1 2 3 4 5 6 7) (20 20 1) simpleGrading (1 1 1)
);

edges
(
);

// defines the boundary patches
boundary
(
movingWall // patch name
{
type wall; // patch type
faces
(
(3 7 6 2) // define patch surface by vertex indices
);
}
fixedWalls
{
type wall;
faces
(
(0 4 7 3)
(2 6 5 1)
(1 5 4 0)
);
}
frontAndBack
{
type empty;
faces
(
(0 3 2 1)
(4 5 6 7)
);
}
);

mergePatchPairs
(
);

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* //

The mesh is generated by running blockMesh on this blockMeshDict file. From within the case directory, this is done, simply by typing in the terminal: blockMesh

Boundary and initial conditions

The initial fields set up for this case is set up to start at time t = 0 s, so the initial field data is stored in a 0 sub-directory of the cavity directory. The 0 sub-directory contains 2 files, p and U, one for each of the pressure (p) and velocity (U) fields whose initial values and boundary conditions must be set. Let us look into the file p:

file p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/\*--------------------------------\*- C++ -\*----------------------------------\*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\\*---------------------------------------------------------------------------\*/
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
object p;
}
// \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* //

dimensions \[0 2 -2 0 0 0 0\];

internalField uniform 0;

boundaryField
{
movingWall
{
type zeroGradient;
}

fixedWalls
{
type zeroGradient;
}

frontAndBack
{
type empty;
}
}

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* //

  • dimensions specifies the dimensions of the field, here kinematic pressure, i.e. $ m^2s^{-2}$ (see here for more information);
  • internalField the internal field data which can be uniform, described by a single value; or nonuniform, where all the values of the field must be specified (see here for more information);
  • boundaryField the boundary field data that includes boundary conditions and data for all the boundary patches.

The similar configurations can be found in the velocity field in the 0/U file.

Physical properties

For an icoFoam case, the only property that must be specified is the kinematic viscosity($\nu$) which is stored from the transportProperties dictionary. We can check that the kinematic viscosity is set correctly by opening the transportProperties dictionary to view/edit its entries. This case will be run with a Reynolds number of 10, where the Reynolds number is defined as:

$$\eqalign{ \mathbf{Re} = \frac{d \vert U \vert}{\nu} }$$

where $d$ and $U$ are the characteristic length and velocity respectively, and and $\nu$ is the kinematic viscosity we are going to define. Here in our case $d$ = 0.1 m, $U$ = 1 m/s, so that for $\mathbf{Re}$ = 10, $\nu$ = 0.01 $m^2s^{-1}$. Hence, the correct file entry for kinematic viscosity should be specified below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/\*--------------------------------\*- C++ -\*----------------------------------\*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\\*---------------------------------------------------------------------------\*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "constant";
object transportProperties;
}
// \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* //

nu \[0 2 -1 0 0 0 0\] 0.01;

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* //

Control of the simulation

Setting data relating to the control of time and reading and writing of the solution data are read in from the controlDict dictionary.

controlDict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/\*--------------------------------\*- C++ -\*----------------------------------\*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\\*---------------------------------------------------------------------------\*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "system";
object controlDict;
}
// \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* \* //

application icoFoam;

startFrom startTime;

startTime 0;

stopAt endTime;

endTime 0.5;

deltaT 0.005;

writeControl timeStep;

writeInterval 20;

purgeWrite 0;

writeFormat ascii;

writePrecision 6;

writeCompression off;

timeFormat general;

timePrecision 6;

runTimeModifiable true;

// \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* //

Running an application

Viewing the mesh

It is convenient to keep ParaView open while running other commands from the terminal, we will launch it in the background using the & operator by typing paraFoam & to run paraView at current directory. Alternatively, it can be launched from another directory location with an optional -case argument giving the case directory. paraFoam -case <your directory (relative path)> & The geometry and the wireframe can be obtained in ParaView . The figure below shows the configuration of uniform mesh distribution.

Uniform mesh
Uniform mesh

Some modifications can be done in the blockMeshDict, if the non uniform mesh distribution is desired. We list some examples as follow:

  • simpleGrading The simple description specifies uniform expansions in the local $x_1$, $x_2$ and $x_3$ directions respectively with only 3 expansion ratios. The ratio is that of the width of the end cell $\delta_e$ along one edge of a block to the width of the start cell $\delta_s$ along that edge.

    simpleGrading (1 2 3) // cell expansion ratios in 3 directions

non uniform mesh - simpleGrading
Non uniform mesh - simpleGrading

  • edgeGrading This gives us the way to specified the expension ratio of the mesh on each edge directly.

    edgeGrading (1 1 1 1 2 2 2 2 3 3 3 3)
    

This means the ratio of cell widths along edges 0-3 is 1, along edges 4-7 is 2 and along 8-11 is 3 ( the number and direction of the edge in a block can be seen here).

Non uniform mesh - edgeGrading
Non uniform mesh - edgeGrading

  • Multi-grading of a block In our case, we might require finer cell for the mesh near the walls. OpenFOAM v2.4+ includes multi-grading functionality that can divide a block in an given direction and apply different grading within each division. It is specified by replacing any single value expansion ratio in the grading specification of the block. A example configuration for our cavity case is shown as follow.
    simpleGrading (     
        // x1-direction expansion ratio
        (
            (0.2 0.3 4)    // 20% y-dir, 30% cells, expansion = 4
            (0.6 0.4 1)    // 60% y-dir, 40% cells, expansion = 1
            (0.2 0.3 0.25) // 20% y-dir, 30% cells, expansion = 0.25 (1/4)
        )
        // x2-direction expansion ratio
        (
            (0.2 0.3 4)    // 20% y-dir, 30% cells, expansion = 4
            (0.6 0.4 1)    // 60% y-dir, 40% cells, expansion = 1
            (0.2 0.3 0.25) // 20% y-dir, 30% cells, expansion = 0.25 (1/4)
        )

        // x3-direction expansion ratio
            1                  
    )

Non uniform mesh - multi-grading
Non uniform mesh - multi-grading


Running

The icoFoam solver is executed either by entering the case directory and typing icoFoam at the command prompt, or with the optional -case argument giving the case directory. icoFoam -case <your running directory>

pressure contour and the velocity vector
Pressure contour and the velocity vector


  • Reference:
  1. CFD Direct
  2. OpenFOAM for beginners: Hands-on training
  3. OpenFOAM tutorial
  4. OpenFOAM step by step tutorial