Tutorial: preparing a CFD simulation in OpenFOAM
OpenFOAM is a powerful tool when it comes to performing CFD simulations, however, it is not easy to start working with it due to the lack of a graphical interface it has. In this tutorial we will learn the basic concepts to be able to understand the structure of this program.
At the end of this tutorial, you will be able to:
- Understand the folder structure on which any OpenFOAM simulation is based.
- Configure the basic files and check that everything has been done correctly to launch the simulation.
Índice de contenidos
Introduction and general structure.
To begin with, the most advisable thing is to start with a case already solved that we will copy to a new folder. A good starting method is to check what type of phenomenon we are simulating, access the OpenFOAM tutorials folder (hereinafter, OF) located in the “tutorials” folder (inside the program installation folder) and copy the file. It is advisable to name this case as a “base case” to differentiate it from possible studies that we carry out derived from it.
When opening any case of OF we will always find the same structure:
- Folder “0”. It is the folder where the initial conditions of each simulation are located. There are different files for each of the equations to be solved, each of which will contain information on all the “boundary conditions” that are defined in the mesh that we are going to simulate.
- “constant” folder. In this directory several files are located that will define certain variables that will remain constant throughout the simulation (turbulence, viscosity, thermophysical variables…) as well as the mesh file of the case.
- “system” folder. Finally, in this folder we will find files referring to simulation conditions, such as number of iterations, solver to use, relaxation factors …
As you can see, none of the folders contains anything that might surprise an engineer who knows CFD through other software, the only difference is that it cannot be viewed through a GUI. We will now go through the contents of each of these folders using as an example a simple simulation of a ventilation duct in turbulent regime.
Although it seems more logical to start preparing the initial simulation conditions, the truth is that it is preferable to prepare the contents of the “constant” folder before everything else, since certain files in the rest of the directories will change based on it. This folder always contains a “polyMesh” subfolder (where the case mesh is located) and different files that will depend on the phenomenon to be simulated. For this case, only the files “transportProperties” and “turbulenceProperties” will be necessary, which will contain the transport and turbulence properties, respectively.
In the first we must define the KINEMATIC viscosity of the fluid to simulate (dynamic viscosity / density), while in the second we will define the type of turbulence model that we want to use in the case. There is not much difficulty in defining the viscosity, we simply write the numerical value on the “nu” line.
For the turbulence model, the first step is to verify if we are in a laminar or turbulent regime. To do this, a good way to proceed is to check what average velocity we will have in our volume and calculate the Reynolds number with it. In the line “simulationType” we will specify if the model is laminar (“laminar”) or turbulent (“RAS”) and, having chosen turbulent, in “RASModel” we will define one of the multiple possibilities of turbulent models offered by OF. Choosing the most appropriate turbulence model would require a more detailed explanation that we could develop in another article. For the moment, a good choice is usually the “kEpsilon” model, which works reasonably well in many cases.
It is now convenient to check that the mesh is correct, checking its files in the “polyMesh” folder. Regardless of whether the mesh has been created with another program and exported, or created with the OF itself, this folder will contain different files with all the information, which are “boundary”, “faces”, “neighbour”, “owner” and ” points “. The most important is the “boundary” file, which will contain information about the boundary conditions with their names and the type of “boundary” they are. For a simple simulation like this we only have “inlet”, “outlet” and “wall”. The first two are the inlet and outlet of the fluid whose type is defined as “patch” and the last are the rest of the walls where the control volume ends, whose type is “wall”. It is important to check these types, as well as the names of these boundary conditions, as they will help us to prepare the initial conditions of the simulation.
In this folder, there will be a file for each equation that the solver will solve. That is why, depending on the files that we have defined in “constant”, more or fewer files will be needed in this directory. In this simulation, only the files “p” (pressure), “U” (speed) and those referring to the selected turbulence model are necessary, in this case being “k”, “epsilon” and “nut”. If we had used the “kOmega” model, we would have to include the “omega” file, or if we were also modelling heat transfer, the “T” (temperature) file, etc.
Within each file, all the names of the “boundaries” included in the mesh must appear, and it must be specified what type of condition will be imposed. OF has numerous boundary conditions, like those used in other programs. Their explanation exceeds the objectives of this tutorial, although in the following link you can find a guide where they are all.
To finish preparing the simulation, you must configure the case parameters. Inside the “system” folder, we will find, at least, three files necessary for any simulation: “controlDict”, “fvSchemes”, and “fvSolution”. The main file, and which we will talk about in this tutorial is “controlDict”. The other two files include information regarding the numeric schemes that the solver will use, relaxation factors, and convergence criteria. This information is necessary to achieve a quick and correct convergence of the case, but to launch a first simulation, the corresponding tutorial that we have copied can be used.
The file “controlDict” must be modified. Many parameters appear in it, but the most important are the type of solver that we are going to use and the number of iterations that we want to perform. For this tutorial, we will use the solver “simpleFoam” and the number of iterations of 500. These values must be included in the lines “application” and “endTime”, respectively. With the rest of the lines, you can define parameters such as the interval of iterations to save, whether we want to start from instant 0 or not (for example, to follow a simulation that we had stopped), etc.
Summary of the case structure.
- “0”: initial conditions
- “p”: pressure
- “U”: velocity
- “k”: turbulence model variable
- “epsilon”: turbulence model variable
- “nut”: turbulence model variable
- “constant”: constants
- “polyMesh”: case mesh
- “transportProperties”: transport variables
- “turbulenceProperties”: turbulence variables
- “controlDict”: main simulation parameters
- “fvSchemes”: solver numeric schemes
- “fvSolution”: convergence criteria and relaxation factors
- “0”: initial conditions
Final check before simulating.
With all the structure created, we would already be able to launch the simulation, executing the solver that we have selected. As it is highly likely that we have made an error in modifying a file, a good practice is to execute the “checkMesh” command at this point, which checks the quality of the mesh, but in turn serves as an indicator of whether we have made an error in the configuration of some boundary condition. If everything is correct, the simulation can be launched. Congratulations!