First CFD Case

2. First Simulation by OpenFOAM

a. Problem definition

The first problem we work on is the well known ''Cavity Problem'' (see for exapmle). Here, the lid (top wall) of a square model moves by $ U=1~\mathrm{m/s} $ to the right side. Accordingly, the fluid (water in this case) moves with the lid owing to the no-slip condition.

There are some items on the page. Do not miss them

Figure: Cavity Problem definition

You can find the documentation of this problem in doc folder in OpenFOAM installation directory. Open a terminal window by ctrl+t and run the command below in the terminal

evince -i 19 /opt/openfoam10/doc/Guides/OpenFOAMUserGuide-A4.pdf &

to open the ''Tutorials'' on page 19. Then you can close the terminal window.


b. Pre-processing

We can find this study in the tutorials directory of our OpenFOAM installation. Copy it in lecture02 folder.

cd ~/OpenFOAM/lecture02/
cp -r /opt/openfoam10/tutorials/incompressible/icoFoam/cavity/cavity .

''blockMesh'' is a mesh generation tool for creating simple block meshes in OpenFOAM. The cavity problem geometry and mesh is set in system/blockMeshDict file. This file generates the geometry below.

There are some items on the page. Do not miss them

Figure: Cavity Problem computational domain

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.1;

vertices
(
    (0 0 0)
    (1 0 0)
    (1 1 0)
    (0 1 0)
    (0 0 0.1)
    (1 0 0.1)
    (1 1 0.1)
    (0 1 0.1)
);

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

boundary
(
    movingWall
    {
        type wall;
        faces
        (
            (3 7 6 2)
        );
    }
    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)
        );
    }
);


// ************************************************************************* //

The mesh can be generated by running

blockMesh

command in the cavity folder. Then, you can visualize the mesh running

paraFoam

Figure: ParaView visualization of the blockMesh of Cavity Problem

We can find the mesh files, such as

~/OpenFOAM/lecture02/cavity/constant/polyMesh
                                     ├── boundary
                                     ├── faces
                                     ├── neighbour
                                     ├── owner
                                     └── points

c. Solver setup

0 (zero) directory

In setting up the solver, the first step is to define the boundary and initial conditions (BCs and ICs). These are specified within the 0 folder in the working directory.

~/OpenFOAM/lecture02/cavity/0
                            ├── p
                            └── U

Here the p and U are the condition files for pressure and velocity.

Besides the pressure field $ p $ and velocity field $ U $, which are almost always present, the contents of the 0 directory can vary significantly depending on the physics of the problem being solved and the specific solver being used. Here are some common examples:


In the pressure file below, dimensions [0 2 -2 0 0 0 0] part is important. These numbers are

Dimension Represents Numbers
Mass kilograms (kg) 0
Length meters (m) 2
Time seconds (s) -2
Temperature kelvin (K) 0
Quantity of Substance mole (mol) 0
Electric Current ampere (A) 0
Luminous Intensity candela (cd) 0

Therefore, the unit is $ \mathrm{m^2/s^2} $. However, actual unit of the pressure is

$$ \mathrm{Pa} = \frac{\mathrm{N}}{\mathrm{m^2}} = \frac{\mathrm{kg}}{\mathrm{m~s}^2}. $$

In OpenFOAM, the pressure $ \mathrm{p} $ is defined actually as ''pressure per unit density''. Density unit is $ \mathrm{kg/m^3} $. So the

$$ \dfrac{\mathrm{m^2}}{\mathrm{s^2}} = \dfrac{\frac{\mathrm{kg}}{\mathrm{m~s}^2}}{\frac{\mathrm{kg}}{\mathrm{m^3}}} . $$

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;           // The file format is ASCII, meaning it's human-readable
    class       volScalarField;  // This file describes a scalar field variable (as opposed to a vector field)
    object      p;               // The name of the field defined in this file, which is pressure 'p'
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 2 -2 0 0 0 0]; // The dimensions of pressure in terms of basic dimensions (mass, length, time, etc.)

internalField   uniform 0;        // Sets a uniform initial condition for pressure throughout the domain, which is zero in this case

boundaryField
{
    movingWall
    {
        type            zeroGradient; // The pressure gradient at the boundary is zero, meaning no change in pressure across this boundary
    }

    fixedWalls
    {
        type            zeroGradient; // Same as above, no pressure gradient across the boundary
    }

    frontAndBack
    {
        type            empty;        // Specifies there's no domain in this direction, used in 2D simulations
    }
}


// ************************************************************************ //

In the velocity file below, the unit is $ \mathrm{m/s} $ as it is mentioned above.

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;           // File format: ascii (human-readable) or binary
    class       volVectorField;  // Means it's a vectoral field variable defined over the volume of the mesh
    object      U;               // Object name: 'U' typically represents the velocity field in OpenFOAM
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions      [0 1 -1 0 0 0 0]; 

internalField   uniform (0 0 0);  // This sets the initial condition for the velocity field inside the domain to a uniform value, which in this case is a vector field with all components being zero (no initial velocity).

boundaryField
{
    movingWall
    {
        type            fixedValue;     // The type of boundary condition: 'fixedValue' means the value is set and does not change.
        value           uniform (1 0 0); // The value of velocity on this boundary: a uniform velocity in the x-direction.
    }

    fixedWalls
    {
        type            noSlip;         // 'noSlip' condition means the velocity is zero relative to the wall (the fluid sticks to the wall).
    }

    frontAndBack
    {
        type            empty;          // 'empty' is used in 2D simulations to specify that there is no flow normal to these boundaries.
    }
}


// ************************************************************************ //

constant directory

Check the constant folder which we define the

and so on. In this example, we only have the physicalProperties which is shown as follows.

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;           // The file format is ASCII, meaning it's human-readable
    class       dictionary;      // This file is a dictionary, which is a set of key/value pairs in OpenFOAM
    location    "constant";      // The directory where this dictionary is located, typically 'constant' for physical properties
    object      physicalProperties; // The name of the object this file describes, in this case, 'physicalProperties'
}

nu              [0 2 -1 0 0 0 0] 0.01; // Defines the kinematic viscosity 'nu' with dimensions and a value of 0.01 m^2/s

Here, nu [0 2 -1 0 0 0 0] 0.01 part is important. These numbers are

Dimension Represents Number in nu Above
Kinematic Viscosity meter square per second ($ \mathrm{m^2/s} $) nu
Mass kilograms (kg) 0
Length meters (m) 2
Time seconds (s) -1
Temperature kelvin (K) 0
Quantity of Substance mole (mol) 0
Electric Current ampere (A) 0
Luminous Intensity candela (cd) 0
Kinematic Viscosity meter square per second ($ \mathrm{m^2/s} $) 0.01

Here in our case, the 0.01 represents the $ \nu = 0.01~\mathrm{m^2/s} $. We can reach the Reynolds Number ($ \mathrm{Re} $) of the flow from this.

$$ \mathrm{Re} = \dfrac{DU}{\nu}, $$

where $D$ is the length of our model which is $0.1~\mathrm{m}$, $U$ is the velocity of our lid model which is $1~\mathrm{m/s}$, hence,

$$ \mathrm{Re} = \dfrac{0.1~\mathrm{m}~1~\mathrm{m/s}}{0.01~\mathrm{m^2/s}} = 10. $$

Therefore, we can understand that our flow is a low-Re flow. This is important to chose the solvers and turbulence models in OpenFOAM.


system directory

The system directory contains configuration files that define the overall behavior of the simulation. Key files in this directory typically include:

These files collectively determine how the simulation is set up, solved, and written out. They need to be configured correctly for the simulation to run as intended.

In ''cavity'' case the directory contains

~/OpenFOAM/lecture02/cavity/system
                            ├── blockMeshDict
                            ├── controlDict
                            ├── fvSchemes
                            └── fvSolution

We studied on the blockMeshDict earlier. The controlDict is explained as follows.

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    location    "system";
    object      controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

application     icoFoam;        // Specifies the solver to be used

startFrom       startTime;      // Specifies the starting point of the simulation, 'startTime' means from the beginning

startTime       0;              // Sets the simulation start time to 0 seconds

stopAt          endTime;        // Determines the stopping criterion, 'endTime' means stop at a specified time

endTime         0.5;            // Sets the simulation end time to 0.5 seconds

deltaT          0.005;          // Sets the time step for the simulation to 0.005 seconds

writeControl    timeStep;       // Determines when to write data, 'timeStep' means write every fixed number of time steps

writeInterval   20;             // Sets the interval at which data is written to 20 time steps

purgeWrite      0;              // Specifies the number of past time directories to keep; 0 means keep all

writeFormat     ascii;          // Specifies the data format for writing, 'ascii' is human-readable

writePrecision  6;              // Sets the precision (number of significant digits) for writing data to 6

writeCompression off;           // Specifies whether to compress the output files, 'off' means no compression

timeFormat      general;        // Specifies the format for time values, 'general' is a flexible format

timePrecision   6;              // Sets the precision for writing time values to 6 significant digits

runTimeModifiable true;         // Allows certain parameters to be modified while the simulation is running

// ************************************************************************* //

The fvSchemes file defines numerical schemes used in an OpenFOAM simulation for different mathematical operations.

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    location    "system";
    object      fvSchemes;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

ddtSchemes                                              // Time differentiation schemes.
{
    default         Euler;
}

gradSchemes                                             // Gradient calculation schemes.
{
    default         Gauss linear;
    grad(p)         Gauss linear;
}

divSchemes                                              // Divergence calculation schemes for convective transport.
{
    default         none;
    div(phi,U)      Gauss linear;
}

laplacianSchemes                                        // Schemes for solving Laplacian (diffusion) terms.
{
    default         Gauss linear orthogonal;
}

interpolationSchemes                                    // Methods for interpolating values between mesh cells. 
{
    default         linear;
}

snGradSchemes                                           // Schemes for calculating surface-normal gradients.
{
    default         orthogonal;
}


// ************************************************************************* //

Other option names for each scheme category in OpenFOAM might include:

These are just a few examples, and OpenFOAM provides a wide range of schemes for each category. Each scheme has its own use cases and is chosen based on the requirements of the simulation for accuracy and stability.


The fvSolution file configures solvers, tolerances, and algorithms for solving the equations in an OpenFOAM simulation. It specifies solver settings for pressure (p), a final pressure correction step (pFinal), and velocity (U), along with the PISO algorithm parameters.

The file in this case is explained with other alternative methods below.

/*--------------------------------*- C++ -*----------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Version:  10
     \\/     M anipulation  |
\*---------------------------------------------------------------------------*/
FoamFile
{
    format      ascii;
    class       dictionary;
    location    "system";
    object      fvSolution;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

solvers
{
    p
    {
        solver          PCG;             // Other solvers: GAMG, PBiCG, BiCGStab
        preconditioner  DIC;             // Other preconditioners: DILU, GAMG
        tolerance       1e-06;
        relTol          0.05;
    }

    pFinal
    {
        $p;
        relTol          0;               // Final tolerance reset to zero for accuracy
    }

    U
    {
        solver          smoothSolver;    // For velocity
        smoother        symGaussSeidel;  // Other smoothers: GaussSeidel, DICGaussSeidel
        tolerance       1e-05;
        relTol          0;
    }
}

PISO
{
    nCorrectors     2;
    nNonOrthogonalCorrectors 0;         // Additional corrections for non-orthogonality
    pRefCell        0;                  // Reference cell for pressure level
    pRefValue       0;                  // Reference value for pressure in pRefCell
    // Additional PISO option: nOuterCorrectors for outer correctors
    // Additional PISO option: residualControl for controlling residuals
}


// ************************************************************************* //

d. Computing

The solver we use in this case is the icoFoam. It is one of the basic solvers in OpenFOAM. You can find some information by typing

foamInfo icoFoam

This command will print the output below.

File
    /opt/openfoam10/applications/solvers/incompressible/icoFoam/icoFoam.C

Description
    Transient solver for incompressible, laminar flow of Newtonian fluids.

Examples using "icoFoam"
    /opt/openfoam10/tutorials/incompressible/icoFoam/cavity/cavityGrade
    /opt/openfoam10/tutorials/incompressible/icoFoam/cavity/cavityClipped
    /opt/openfoam10/tutorials/incompressible/icoFoam/cavity/cavity
    /opt/openfoam10/tutorials/incompressible/icoFoam/elbow

To compute the ''cavity'' case, run the following command.

icoFoam

That's all. We have set a simulation using OpenFOAM and computed it.


e. Post-processing

Now, in the ~/OpenFOAM/lecture02/cavity folder there are some other files.

~/OpenFOAM/lecture02/cavity
                     ├── 0
                     ├── 0.1
                     ├── 0.2
                     ├── 0.3
                     ├── 0.4
                     ├── 0.5
                     ├── constant
                     └── system

We know the 0, constant and system folders. The other folders include the solution. For example 0.5 folder is as follows.

~/OpenFOAM/lecture02/cavity/0.5
                            ├── p           # Pressure field data at this time step.
                            ├── phi         # Flux field data, often representing the face fluxes for velocity or other scalar fields.
                            ├── U           # Velocity field data, containing the vector field of fluid velocities.
                            └── uniform     
                                └── time    # A special directory indicating that certain data (like time) is uniform across the domain or doesn't change with position.

Run

paraFoam

to visualize the results in ParaView.

Figure: Cavity Problem velocity contours and streamtraces with a set colorbar

Figure: Cavity Problem pressure contours and streamtraces with a set colorbar