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- either need to install Linux on our computer
- or create a Linux environment under MS Windows or macOS.
If 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
- You can download and install the OpenFOAM package prepared for your Linux distribution, which is the easiest way. OpenCFD provides RPM packages that can be installed on distributions such as CentOS, Fedora, OpenSUSE, etc. CFD Direct provides an Ubuntu package. This is the recommended option for installing OpenFOAM on Linux.
- If you want to use a version of OpenFOAM on a Linux distribution for which no package is provided (such as using OpenCFD version on Ubuntu) you can go with the Docker option. Docker is a technology that allows you create a self-contained environment with the software you want, independent of the underlying operating system. Both OpenCFD and CFD Direct has Docker images on their web sites.
- You can download the source files from the code repositories and compile them on any Linux distribution you want by following the detailed instructions provided by the developers.
If you are a Mac user, then you have to go either with the Docker option or install using source files.
Here 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.
- 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.
- 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.
- VirtualBox: 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.
- 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.- Download the latest "Windows host" version of VirtualBox and install it on your Windows machine.
- Download the ISO file of Xubuntu distribution. It is better to use the latest Long Term Support (LTS) version available, which is 20.04 as of this writing. Therefore download the file named xubuntu-20.04-desktop-amd64.iso.
- Start VirtualBox.
- From the File menu select Virtual Media Manager. Go to the Optical Disks tab. Click Add and browse to the Xubuntu ISO file that you downloaded.
- From the Machine menu select New. Give a name to your machine such as Xubuntu and select a folder on your hard drive to store the virtual disk into which Xubuntu will be installed.
- Set a memory size to be used by the virtual machine. My computer has 32 GB of memory and I selected 8192 MB (= 8GB) of it to be used by the virtual machine. At least select something like 4096 MB (= 4 GB). You can change this option later.
- Continue with the default "Create a virtual disk now" option.
- Continue with the default VDI option.
- Continue with the default "Dynamically allocated" option.
- Select a disk size for the virtual machine. Note that a fresh install of Xubuntu, without any extra software takes about 10 GB hard disk space. Therefore at least select something like 15 GB. This depends on the available free space on your hard drive.
- Press the Start button to install Xubuntu.
- Select the downloaded ISO image file for the start-up disk. Xubuntu will be installed using the files in that image.
- Get rid of the "auto capture keyboard" and "mouse pointer integration" messages.
- Click the Install Xubuntu button.
- Select the appropriate Keyboard Layout.
- Uncheck "Download updates while installing Xubuntu" option. Check "Install third-party software ..." option.
- Continue with the default "Erase disk and install Xubuntu" option. Don't panic with the warning shown. It will only use the virtual disk image we created earlier. Nothing will happen to your Windows files.
- Select your time zone by clicking on the map.
- Provide the details for the "Who are you?" questions. Select the "Login automatically" option.
- Rest of the installation will take about 5-10 minutes to finish.
- Press the Restart button when the installation is done.
- Press Enter.
- Xubuntu will boot as a virtual machine. So, here you have a full featured Linux with all its capabilities running inside Windows. If this the first time you are booting into Linux, WELCOME. You will find many new great adventures here :-), together with a lot of trouble :-(
- You may see a warning window about installing updates. Either install them or do it later.
- First trouble is that as you maximize the Oracle VM VirtualBox window inside which Xubuntu runs, you will see that Xubuntu does not get maximized. And it is really difficult to work inside this little window.
- To overcome this trouble, go to the Devices menu in the Oracle VM VirtualBox window and select "Insert Guest Additions CD image..." This will mount a CD image that comes with VirtualBox in Xubuntu to perform some optional settings.
- Inside Xubuntu right click on the desktop and select "Open Terminal Here".
- Inside the terminal first go to the mounted CD image folder using the following command (the first > character seen below is the shell
prompt. If you copy-paste commands from here to your terminal, do not copy that one).
> cd /media/cuneyt/VBox_GAs_6.1.14
Folder name details would be different on your machine. Writing "cd /media/" part and typing the Tab key a couple of times will fill the details for you automatically. Next, type the following command, which will ask for your password. Enter the password that you selected during the installation of Xubuntu.
> sudo sh ./autorun.sh
This command will install the guest additions. Using the menu icon at the top left corner of the screen, restart Xubuntu and you'll notice that now Xubuntu can fill the whole screen as the VirtualBox window is maximized. You can even go to the full screen mode for a better experience. That way it will just feel like you are working inside a real Linux machine, not a virtual one. - You may find the following keyboard shortcuts useful in using Xubuntu to launch certain applications quickly. "Super" is the key on the
lower left part of your keyboard. On my keyboard it is the key with the Windows logo on it.
Super + T : Terminal
Super + F : File manager
Super + E : Mousepad text editor
Super + W : Web browser
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".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.
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 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
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.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- COPYING: This file has the GLPv3 license with which OpenFOAM is distributed. Long and not very fun to read. But have a look at it if you've never read an open-source license before.
- doc/Guides: You will see the User Guide as a PDF here. It is the official OpenFOAM documentation with a few hundred pages. Other files in the doc folder are not useful for us at this point.
- src: This folder has tons of C++ source files. You do not need to bother with this folder if you are going to use OpenFOAM "as it is". But you need to dive into it if you want to make code modifications to implement new features or modify the existing ones.
- applications: This folder contains the C++ source files used to built the default applications (solvers and utilities). Again you do not need to bother with this if you are not going to make any code development.
- platforms: This folder has all the executables for the applications already built for us. You'll be running these executables from the command line when you're solving problems. For example the simpleFoam executable that we used to test our OpenFoam installation is in this folder.
- tutorials: This folder has several sample problems that are ready to be solved. They are grouped into different folders, such as incompressible, compressible, multiphase, etc. There are also samples for mesh generation. These are important because we can learn a lot by studying their files and solving these problems. Also, typically what we do to solve a new problem is to find a similar problem in this folder, copy its files to a new folder and edit the files according to our needs. This is usually easier than creating everything from scratch. Although this directory is called tutorials, actually what we have here are just ready to use files to solve problems, but not detailed explanations of those files or step-by-step instructions of how to use them. Actually it is better to call them sample problems, not tutorials.
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 onesFOAM_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.