FOAMY Lectures

CFD Lectures with OpenFOAM

by Dr. Cuneyt Sert

Lecture 2. Settings Things Up

2.1 Installation Options

As mentioned in Lecture 1, OpenFOAM is being developed under the Linux operating system and it works under Linux. To use it we

alt textIf you’ve used Linux before or even better if you currently have a computer running Linux,I guess you know how to find your way on your own. In short, your options are

alt textIf you are a Mac user, then you have to go either with the Docker option or install using source files.



alt textHere I am assuming that you have a Windows machine and have no prior Linux experience, because most of my students are in this situation. You can use OpenFOAM on a Windows machine in a number of different ways.

  1. alt text Dual booting: You can turn your Windows machine into a dual boot machine by installing Linux side-by-side to Windows. So, as you turn on your computer, you have the option to boot into Windows or into Linux. Although sounds cool, this not an effective working environment, especially if you need to go back-and-forth between Linux (for OpenFOAM) and Windows (for other things), and therefore not recommended.

  2. alt text Windows Subsystem for Linux (WSL): On Windows 10 you can activate the WSL option, download one of the Linux distributions from Microsoft Store such as Debian, Ubuntu or OpenSUSE and install it on your computer. And then you can install OpenFOAM on it. Details of how to do this will be shared in the coming sections. Concerns with this approach are; i) it gives you a terminal window for OpenFOAM, and inside it you just feel a bit lonely, ii) using software with GUI such as Paraview or Gnuplot requires installation of an X-Windows emulator software, iii) it only works for Windows 10. On my home computer, which is a laptop running Windows 10, I use OpenFOAM with this approach.

  3. alt textVirtualBox: This is a free virtualization software. It allows you install an operating system inside another. With it you can create a virtual Linux environment inside your Windows machine, and in it you can do whatever you do with a native Linux installation. This gives you a more natural Linux experience on a Windows machine and its details will be explained in the coming sections. The biggest concern here is performance, because this virtualization puts considerable load on your computer. If you are planning to do some serious CFD solutions and if your computer is a low end one, this may not be the best option for you. Try it and if you are not happy with the result you can try another option. On my office computer which runs Windows 7 with 32 GB of RAM, I use OpenFOAM with this approach.

  4. alt text Docker: This technology creates a "container" for the working environment you want (for us Linux + OpenFOAM and other necessary tools) inside your Windows computer. A concern here is that it gives you a working command line (a terminal window) for OpenFOAM, but it is not what Linux is all about. If you want to experience Linux more while experimenting with OpenFOAM, it feels a bit incomplete. OpenFOAM distributers provide these Docker images on their web sites.

2.2 Installing Linux inside Windows 10 using WSL

Windows Subsystem for Linux (WSL) is a relatively new technology. It allows you to install a Linux distribution inside Windows 10. Installation steps are
explained here. I advise you install the latest Ubuntu distribution, which is 18.04 as I am writing this. After activating WSL and installing Ubuntu, you'll have a Linux command line (terminal window) working inside Windows just like any other Windows application. Whatever you can do on the command line of an actual Ubuntu installation, you can do it here too.

WSL is primarily developed for system administrators to run things on the command line. To use Linux applications with a graphical user interface (GUI) such as a graphical text editor or ParaView, you need to run an X-Windows emulator software on Windows. The freely available Xming will do the job. Download and install it. Before running a Linux application with a GUI, you need to first start the Xming software. You need to do one more thing before being able to use GUI applications. Inside the Ubuntu command line edit the .bashrc file in your home directory using a text editor such as nano

nano ~/.bashrc

Add the following line to the end of it and save the file. In nano, you can use the commands listed at the bottom of your screen using the Ctrl key.

export DISPLAY=:0

To make this change effective, use the following command. You need to perform these steps only once.

> source .bashrc


If you are total Linux newbie, you may get help from several tutorials on the web such as this video series from Jozsef Nagy. Actually Jozsef's channel is popular for OpenFOAM tutorials, but these Linux related ones are also good. Have a look at them. In using OpenFOAM we'll be spending a lot of time inside a terminal (command line) and there are several resources on the web to learn the basics of it, such as Learning the Shell or Beginner's Guide to the Bash Terminal.

2.3 Installing Linux inside Windows using VirtualBox

VirtualBox is a free virtualization software from ORACLE. Download its latest version for "Windows host" and install it on your computer. Inside it you can install any Linux distribution you want. Here I decided to install Xubuntu, a light-weight version of the popular Ubuntu distribution making use of the lovely Xfce desktop environment. Step-by-step instructions for installing Xubuntu inside VirtualBox are given below.

2.4 Installing OpenFOAM in Linux

Now that we have a working Linux installation, let's install OpenFOAM in it. Let's first check which version of OpenFOAM is available in the official Ubuntu (Xubuntu is also a flavor of Ubuntu, so other than the look and feel, it is technically the same as Ubuntu) package repository. We can check it from the command line or do it online. Let's check it online by visiting
https://packages.ubuntu.com. Set the distribution name to "focal", which is another name for version 20.04 of Ubuntu that we installed, and search for "openfoam".

alt text

You'll see the following result, which says that version 19.06 of OpenFOAM is available in the "universe" repository, which is the community-maintained repository.

alt text

This result refers to OpenCFD's version 19.06, which is not the latest. It is 1 year old, but can be installed. You can follow the instructions given on OpenCFD's web site to install it. Instead I want to install CFD Direct's latest version, but it is not available in Ubuntu repositories. We need to get it from CFD Direct's own repository. Go to openfoam.org. Under the Download menu, at the very top you'll see that the latest version available is v8 (as of this writing). Select that, scroll down to "Download v8 | Ubuntu" section and click on Read More. You'll see that they provide Ubuntu packages. We can download and install it by following the step-by-step instructions given there, also shown below.

Open the Ubuntu app under Windows (or open a terminal window in Xubuntu), and execute the following 4 commands (change the OpenFOAM version number properly if you are not installing version 8).

> sudo sh -c "wget -O - http://dl.openfoam.org/gpg.key | apt-key add -"
> sudo add-apt-repository http://dl.openfoam.org/ubuntu
> sudo add-apt-repository sudo apt-get update
> sudo add-apt-repository sudo apt-get -y install openfoam8

The first two commands give us the permission to install packages from openfoam.org's repository. The third one gets the latest package list information and the last one installs OpenFOAM, together with many other packages that are required by it to work properly. If you get an error which reads like "Unable to fetch some archives..." and the installation stops, it means that there occurred a problem in downloading one of the packages. In that case try to execute the last two commands again.

The installation will take several minutes. Before starting to use OpenFOAM, we need to perform one small adjustment. We need to tell our system where the configration settings of OpenFOAM are installed in our hard drive. For this, we need to edit the .bashrc file in our home directory. For me it is /home/cuneyt/. Folowing commands will take you to your home directory and list the files and folders in it.

>cd
> ls -la

Some files have a dot in front of them, such as .bashrc. They are hidden files. Let's edit the .bashrc file with the nano text editor using the following command. nano is a simple text editor that runs inside a terminal. We'll use it a lot to quickly edit OpenFOAM case files without leaving the terminal.

nano .bashrc

Everytime we open a terminal window, a special Linux application known as the shell starts running. Most of the time this is the BASH shell. And the .bashrc file in our home directory has several shell commands in it that are executed when the shell starts to run. "rc" in the file name stands for "run commands". Scroll down to the bottom of the file and add the following line. Save the file using Ctrl+O and Enter. Exit nano using Ctrl+X.

> source /opt/openfoam8/etc/bashrc

Use the proper version number if you installed a different version of OpenFOAM. With this additional line, extra shell commands from another bashrc file that comes with OpenFOAM will be executed every time the shell starts so that the shell will be aware of the settings that are necessary to use OpenFOAM properly. To make this change effective issue the following command.

source .bashrc

You need to use these commands only once, not every time you want to use OpenFOAM. To test whether OpenFOAM is installed properly or not, execute the following command.

> simpleFoam -help

simpleFoam is one of the solvers that comes with OpenFOAM and this command shows a short documentation of how to use it. If you see some instructions on your screen, it means that you installed OpenFOAM properly. If instead you get an error message, it means that you either could not install OpenFOAM properly or could not change the .bashrc file properly. Together with OpenFOAM, ParaView 5.6 is also installed. In Xubuntu you can open it using the following command. Try it.

> paraview

Note: If you are using Ubuntu as WSL on Windows 10, you need to start the Xming emulator inside Windows first before using a GUI application in Ubuntu. After starting Xming, you can type paraview in the command window of Ubuntu to launch ParaView. Sometimes GUI applications do not launch properly in WSL, resulting in a non-functional, gray window. Wait for a few seconds and if things do not turn into normal, just close that window and try to launch the application again.

You are also advised to install Gnuplot using the following command. It is mostly used for plotting residuals and monitoring the solution during a run.

> sudo apt-get install gnuplot gnuplot-x11 gnuplot-doc libgd-tools

2.5 Folders and Files that Come with OpenFOAM

To see all the files that come with openfoam8 package, use the following command on a terminal

> dpkg-query -L openfoam8

You will see a very long list of files. Except a few, they are all inside the /opt/openfoam8/ folder. That's where OpenFOAM is installed. In Xubuntu launch the file manager using Super+F and go to that folder to have a look at the files.

alt text

You can also see the list of these files and folders inside a terminal using

> ls -l /opt/openfoam8

Some of the important files and folders you see here are ParaView is also installed under the /opt/paraviewopenfoam56 folder. But there is really nothing interesting to see there.

2.6 Environment Variables Set by OpenFOAM

Remember the line we added to the .bashrc file in our home folder. That is actually done to set certain environment variables that will make it easier to use OpenFOAM. If you run the following command on a terminal you will see a list of all the environment variables set in your Linux system.

> env

These are simply shortcut definitions that are easy to remember for things that are hard to remember. The ones that start with FOAM_ are set by OpenFOAM. You will see some of them used in OpenFOAM documentations and tutorials, such as the following ones

    FOAM_TUTORIALS = /opt/openfoam8/tutorials
    FOAM_RUN = /home/cuneyt/openFOAM/cuneyt-8/run
    FOAM_APPBIN = /opt/openfoam8/platforms/linux64GccDPInt32Opt/bin
    FOAM_SOLVERS = /opt/openfoam8/applications/solvers

For example, FOAM_TUTORIALS is a variable that points to the folder where OpenFOAM tutorials are stored. We use these environment variables by putting a $ sign in front of them, such as the following command which changes the working directory in a terminal window to OpenFOAM's tutorials directory.

> cd $FOAM_TUTORIALS

When you see an OpenFOAM related environment variable used in a documentation and wonder if it is defined in your system or not, or want to see what its value is, use the echo command as follows.

> echo $FOAM_UTILITIES

which on my machine shows /opt/openfoam8/applications/utilities, which is a folder related to OpenFOAM utilities. Nothing will be shown on the screen if the variable is not defined. You can use the life saver "tab completion" feature of the terminal by typing "echo $FOAM" and press the Tab key three times. All the environment variables whose name start with $FOAM will be shown. We use this "tab completion" trick all the time for all sorts of things when we work in a terminal.

< Previous Lecture    TOC    Next Lecture >