OpenFOAM Compilation Tutorial

This tutorial is intended to help new OpenFOAM users to compile its source code.
I tried to make it as simple as possible for the ones who are not familiar with Linux.
In some situations, it is useful to compile the OpenFOAM code, such as, when you have access to a machine, but you do not have admin privileges. In this case, you can compile the code in your own home folder.
Personally, I prefer to extract and compile the OpenFOAM code in the "/opt" directory, so that the original code remains safe from modifications.
This guide was developed using Ubuntu 18.04 LTS 64-bit and OpenFOAM 18.12, downloaded from OpenFOAM.com .
It was also tested with OpenFOAM 19.06 in Ubuntu 18.04 and in Mint 19.1 Cinnamon 64-bit. It works like a charm!
For Windows users, you may setup a Virtual Machine following either of these tutorials: Create Ubuntu VM or Create Mint VM.

1) Download and extract the code

Go to openfoam.com and download the source codes:
  • OpenFOAM-v1812.tgz and
  • ThirdParty-v1812.tgz.
Create the OpenFOAM directory under /opt.

> sudo mkdir /opt/OpenFOAM

then, extract both files to this folder. As we will work on restricted directories, let us first gain admin privileges by issuing the sudo su command:

> sudo su

Now, let's extract the compressed files in /opt/OpenFOAM. First go to the folder where the downloaded files are located and, then extract them as follows:

> tar -xzf OpenFOAM-v1812.tgz -C /opt/OpenFOAM/
> tar -xzf ThirdParty-v1812.tgz -C /opt/OpenFOAM/


Edit the bashrc file stored at /opt/OpenFOAM/OpenFOAM-v1812/etc/. Uncomment the line
projectDir="/opt/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION" in order to set the correct path.
If you desire to extract the code to a different location, just edit the bashrc accordingly and you will be just fine!
Don't forget to replace /opt/OpenFOAM by the chosen directory path.


2) Install necessary packages and compile the source code

Allow the system to understand OpenFOAM keywords, PATHs etc, by sourcing the OpenFOAM bashrc file:

> source /opt/OpenFOAM/OpenFOAM-v1812/etc/bashrc

The system will complain that the platform is not yet compiled. That's okay for now. It is actually not compiled, yet.
As we are at this point, edit your bashrc with the source code.
Type gedit /home/[user]/.bashrc, where [user] is your username and append the source command to the end of the file.

Before compilation, install the required packages for OpenFOAM compilation:

> apt-get install build-essential flex bison cmake zlib1g-dev libboost-system-dev libboost-thread-dev libopenmpi-dev openmpi-bin gnuplot libreadline-dev libncurses-dev libxt-dev
> apt-get install qt4-dev-tools libqt4-dev libqt4-opengl-dev libqtwebkit-dev
> apt-get install qtdeclarative5-dev qttools5-dev qt5-default libqt5x11extras5-dev libqt5help5 freeglut3-dev
> apt-get install libscotch-dev libcgal-dev


In order to compiler faster, you may change the number of processors. For the tutorial, I am using 4:

> export WM_NCOMPPROCS=4

Check the system to see if everything is OK, so far.

> foamSystemCheck

and then, go to the OpenFoam installation directory

> foam

Now, you are ready to compile OpenFoam:

> ./Allwmake

3) Paraview compilation

Now, let us compile the Paraview source code provided in the ThirdParty folder.
Before, we need to download VTK-8 from https://www.vtk.org/files/release/8.1/VTK-8.1.1.tar.gz.
Unpack it to the ThirParty directory, then go to this directory and compile VTK:

> tar -xzf /home/bkassar/d/openfoam1812/VTK-8.1.1.tar.gz -C /opt/OpenFOAM/ThirdParty-v1812/
> cd /opt/OpenFOAM/ThirdParty-v1812/
> ./makeVTK 8.1.1


If it doesn't work, edit the file makeVTK.
Replace the line VTK_VERSION="$vtk_version" with VTK_VERSION=8.1.1.
And then compile VTK:

> ./makeVTK

Now you may compile Paraview:

> cd ../ThirdParty-v1812 > ./makeParaview

You will need to update the OpenFOAM environment by running:

> wmRefresh

In order to make the best integration between Paraview and OpenFOAM, build the Paraview reader module library (PVFoamReader).

> cd $WM_PROJECT_DIR/applications/utilities/postProcessing/graphics/PVReaders
> ./Allwclean
> ./Allwmake


That's it!

4) Testing the compilation

Let us run a first tutorial in order to test the compilation. Create a folder to make a copy of the damBreak tutorial. Here, I will create the folder runs under the HOME directory. First, quit the sudo login by issuing the exit command. Then, do the following:

> mkdir ~/runs
> cp -r $WM_PROJECT_DIR/tutorials/multiphase/interFoam/laminar/damBreak/damBreak/ ~/runs
> cd ~/runs/damBreak
> ls -l


You will see the files and folders inside the damBreak tutorial.
damBreak folder

You may run the scripts ./Allclean and ./Allrun to run the simulation.
However, it is worth it to do it by hand for the first time, in order to know what is going on.
First, create the mesh by calling the blockMesh utility.
It follows the instructions defined in the file blockMeshDict located at the system folder.

> blockMesh

Initialize the alpha.water field with the instructions defined in system/setFieldsDict.
This procedure sets the initial configuration of the liquid and gas domains.

> setFields

Now, you may run the interFoam solver.

> interFoam > log.txt

Finally, let's see the results with Paraview.

> paraFoam

Observe that the initial time-step is omitted by default.
Uncheck the toggle "Skip 0/ time" as indicated below, therefore you will be allowed to see the initial time step:
damBreak folder
Here follows a movie of the simulation:




Reported pitfalls:

  1. Some users reported not being able to compile the code in sudo mode.
    Solution: Do not forget to source the OpenFOAM bashrc file!



Hope this tutorial helps.
be well,
Bruno

Last update: September 2nd, 2019