A comprehensive guide to compilation of OpenFOAM on Ubuntu and Windows WSL.
Michael Bloomberg once wisely noted that “the first step in solving any problem is to identify that there is one.” Today, I want to tackle a common challenge that many encounter at the outset of their journey into Computational Fluid Dynamics (CFD): How do you install OpenFOAM? It’s a question I’ve been approached with countless times, especially by those starting their MSc or embarking on undergraduate projects. While the OpenFOAM website offers instructions for installation, it can sometimes be overwhelming, with one page leading to another and steps getting mixed up. To simplify this process, I aim to provide a ‘one-stop-shop’ guide for installing and compiling OpenFOAM.
Now, you might wonder why I emphasize “compiling” instead of just “installing” or using pre-built versions. The distinction lies in the fact that compiling OpenFOAM involves building the tool from scratch. It’s akin to installing a game or software, but with the added benefit of customization. As a researcher, I often need to modify the code, enhance solver functionalities, introduce custom conditions to CFD simulations, or even incorporate external solvers or modules. These tasks necessitate compiling OpenFOAM, as pre-built packages lack the essential tools for CFD researchers, such as visualization tools (e.g., ParaView/runTimePostProcessing) or external-solver modules (e.g., PETSc).
Before delving into installation options, here’s a crucial heads-up: OpenFOAM is inherently a Linux-based program. Therefore, you’ll need a Linux-based operating system, like Ubuntu, or run Linux on Windows using Windows Subsystem for Linux (WSL). WSL allows you to install a Linux distribution, such as Ubuntu, on Windows and use Linux applications seamlessly. Familiarity with navigating a Linux-based system is essential, and there are various cheat sheets available for quick learning of Linux terminal commands. Moving forward, let’s assume you have a Linux-based system and are comfortable navigating through the command line.
Now, there are two main avenues for installing or compiling OpenFOAM on your system:
curl https://dl.openfoam.com/add-debian-repo.sh | sudo bash
sudo apt-get update
Choose and install the preferred OpenFOAM package from the options below:
sudo apt-get install openfoam2312
sudo apt-get install openfoam2312-dev
sudo apt-get install openfoam2312-default
openfoam2312
This installation method ensures that OpenFOAM operates in a self-contained environment. Your tutorials and simulations run within a container, safeguarding the overall system. If you ever need to remove OpenFOAM, use the following command:
sudo apt-get autoremove openfoam2312-default
Installing OpenFOAM on Windows becomes straightforward if you’ve already set up Windows Subsystem for Linux (WSL). If not, I’ll cover the required steps in an upcoming article. Assuming WSL is installed, follow the same steps as the Ubuntu installation, and everything should seamlessly fall into place!
While the installation of pre-compiled binaries in the earlier sections was relatively straightforward, compiling OpenFOAM from source presents a more intricate process. However, for a dedicated researcher, mastering the art of compiling OpenFOAM, whether on a personal system or an HPC cluster, is a crucial skill. Let’s dive into the steps.
Begin by opening the Ubuntu terminal or the Windows WSL terminal and checking the present working directory. Execute the following command in your command line:
pwd
This should display an output similar to the example below:
/mnt/c/WINDOWS/system32
To compile OpenFOAM, you must be in your $HOME
directory. Navigate to it by typing:
cd ~
Now, the terminal should have moved to the $HOME
directory. Once here, obtain the files required for compiling OpenFOAM:
mkdir OpenFOAM
cd OpenFOAM
wget https://dl.openfoam.com/source/v2312/OpenFOAM-v2312.tgz
wget https://dl.openfoam.com/source/v2312/ThirdParty-v2312.tgz
tar -xzvf OpenFOAM-v2312.tgz ### Wait for the process to finish before typing in the next command.
tar -xzvf ThirdParty-v2312.tgz ### This should take longer than the previoud step.
Before embarking on the OpenFOAM build, it’s crucial to ensure that your system meets the necessary requirements. These system prerequisites encompass the background toolchain and dependencies essential for running OpenFOAM. Among these requirements are a functioning C++14 compiler and the GNU `make’ build toolchain. Without delving into the details of each dependency, here are the steps to satisfy these prerequisites. Note that each command may prompt you to install the corresponding packages; respond with ‘y’ as suggested.
Execute the following commands on your bash command line; this process should take approximately 5 minutes.
sudo apt-get update
sudo apt-get install build-essential autoconf autotools-dev cmake gawk gnuplot
sudo apt-get install flex libfl-dev libreadline-dev zlib1g-dev openmpi-bin libopenmpi-dev mpi-default-bin mpi-default-dev
sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev
sudo apt-get install libfftw3-dev libscotch-dev libptscotch-dev libboost-system-dev libboost-thread-dev libcgal-dev
Now that the necessary files are in place, and the system dependencies are installed, proceed to source OpenFOAM:
source ~/OpenFOAM/OpenFOAM-v2312/etc/bashrc
After sourcing the OpenFOAM environment, test the system readiness with:
foamSystemCheck
This step should confirm that “OpenFOAM is ready for compilation.”
Next, change the main OpenFOAM directory ($WM_PROJECT_DIR). If this step fails, the environment is not correctly configured:
foam
At this juncture, it’s time to compile OpenFOAM. This process may take anywhere from 30 minutes to 6 hours. Various options are available to expedite the process, including utilizing multiple processors during code compilation. To compile with all cores (-j), reduce output verbosity (-s, -silent), enable queuing (-q, -queue), and log the output to a file (e.g., log.linux64GccDPInt32Opt
) for later inspection, execute the following command:
./Allwmake -j -s -q -l
Upon the successful completion of the compilation in the previous step, assuming no errors occurred, close the terminal and open a new one.
echo 'alias OF2312='source ~/OpenFOAM/OpenFOAM-v2312/etc/bashrc'' >> ~/.bashrc
OF2312
in the new terminal to activate/source the OpenFOAM 2312 environment.foamInstallationTest
to confirm the successful build.foamTestTutorial -full incompressible/simpleFoam/pitzDaily
# Create the user "run" directory:
mkdir -p "$FOAM_RUN"
# Change to the user "run" directory:
run
# Copy tutorial
cp -r "$FOAM_TUTORIALS"/incompressible/simpleFoam/pitzDaily ./
# Run the tutorial
( cd pitzDaily && blockMesh && simpleFoam )
As we conclude this guide on installing and compiling OpenFOAM, you’ve now equipped yourself with the foundational knowledge to navigate through the intricacies of this powerful tool. We’ve covered everything from installing pre-compiled binaries to the more involved process of compiling from source. In our next article, “First Steps: Setting up your first case,” we will delve into the practical aspect of applying your newly acquired OpenFOAM environment. Whether you’re embarking on your first CFD simulation or refining your skills, this next installment will guide you through the initial steps of setting up your first case. Get ready to explore the exciting world of Computational Fluid Dynamics hands-on!
Stay tuned for an immersive journey into the practical application of OpenFOAM. Until then, happy simulating!
Here are some more articles you might like to read next: