Setting up my OpenFOAM Development Environment on Windows

Well, if you’re this far, you’re no stranger to open source fluid simulation software. Or you’ve finally broken free from the binds that commercial CFD software pose — “I’m just going to do it myself”, you say all too confidently as you jostle out of your fifteenth meeting with your software support that month.

A full Linux development environment is often too intimidating for those just starting to make a switch from commercial tools to OpenFOAM. Windows until recently was, and to some extent still is, a poor solution for numerical computing. The NTFS file system on Windows is slower than the ext4 for frequent I/O which is a mainstay of computing applications. There were limited options to those looking to run OpenFOAM on Windows without a dual-boot:

  1. A VM install was the go-to solution, however I found that it significantly slowed my host device.
  2. A native Windows build, blueCFD-Core, based on a MSys2 build platform (built with on a Cygwin layer for the Linux “feel” and MinGw, for compilation on Windows.). The full software stack makes it quite bulky for use.
  3. Windows Subsystem for Linux (WSL): The original WSL is a GNU environment for Windows without any virtualization, and with a very slow read/write ability to the Windows file system.

Let’s look at how we can get started with our OpenFOAM-WSL2 environment. 

My move to Windows was recent — circumstances around the pandemic made me switch to a new system which I did not want to have a dual boot on. Luckily, we live in times of the WSL2. The new version of the architecture in WSL allows increased file system performance, perations like cmake and git clone can run up to 20x faster — count me in.

I will not describe setting up the WSL 2 VM in this article. There is a fairly comprehensive install guide here.


Installing OpenFOAM

  1. Once you have WSL ready, download the OpenFOAM release (here, 19.12).
  2. Copy the .tgz to your folder of choice.
cp -ar /mnt/c/Users/<USER>/Downloads/OpenFOAM-v1912-windows10.tgz .

3. Untar to the /opt folder and change permissions.

sudo tar -xvzf  OpenFOAM-v1912-windows10.tgz -C /opt/
sudo chown -R $USER /opt/OpenFOAM

4. Source your .bashrc file!

echo "source /opt/OpenFOAM/OpenFOAM-v1912/etc/bashrc" >> ~/.bashrc
source $HOME/.bashrc

And you’re good to go. Verify your source by running a tutorial case.


Setting up your environment with VS Code

The purpose of this article is to set up a development environment for OpenFOAM — you need to create:

  1. A directory that houses your custom solver and library source code. This is preferably somewhere in your $WM_PROJECT_DIR folder.
  2. Directories for the binaries generated from your custom solver and the .so files generated from your custom library. You’ll need to create a directory that is defined by your variables $FOAM_USER_APPBIN and $FOAM_USER_LIBBIN respectively.

This is how my custom solver and library directory (as described in 1.) should look like:

myReactingParcelFoam is my custom solver modifying reactingParcelFoam, and surfaceFilmModels is where I hold the libraries that the solver depends on.

Notice the vscode folder? The perfect pair to OpenFOAM programming, as I’ve discovered, is Visual Studio Code. You can download this to run on your Windows system. Yes — you run it on Windows and keep the source in your WSL environment. You need to install the remote WSL extension in VS Code to couple the two environments. Additionally, install the C/C++ extension too.

Image for post

Voila! All you need to do is run the following command in the directory you want to create a VS Code directory for:

code .

This creates a .vscode folder in that directory. Additionally, it will open up VS Code with your project, for instance:

Image for post

The files in .vscode that are of relevance are: launch.json, tasks.json and c_cpp_properties.json. Accordingly, these will be created when you add a configuration, but you will have to modify them. These give VS Code inputs on what task to run.

Image for post
What your extensions should look like
Image for post

If you’ve gone through these .json files, the command in tasks.json is wmake, which is a wrapper around make for OpenFOAM. This is given a label wmake-build which is referenced in the “preLaunchTask” field in launch.json.

“program” specifies where to deposit the binary from the executable. As discussed, this should be in $FOAM_USER_APPBIN.

The final .json file, c_cpp_properties.json, contains the include paths for the compiler, as well as the settings for Intellisense.

Image for post

Edit the Make/files to make sure your binary ends up in the right place after wmake.

Image for post

This process is very similar for compiling your own libraries. You will have to write in wmake libso in the tasks.json file instead to compile all the .so files.

And voila, you can compile OpenFOAM code! Complete with code completion and references to other code blocks — this allows for a seamless start into the freedom OpenFOAM development brings with it, all courtesy of our OpenFOAM-WSL2 environment. 

Sample screenshot

You can check out my next article in the series for an understanding on how to put this environment into practice here.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *