OpenFOAM Development in VSCode with Docker Containers and Git

  • no OF installation required,
  • consistent and isolated developing environment,
  • easy deployment and ability to run applications on different machines,
  • ability to work with different versions on the same machine, etc.
  • Line 1: specifies the Base Images that you wish to use for your Image. I develop based on the official Image of latest version of OpenFOAM-v2006
  • Line 3: installs the applications that you wish to have in the Centos-OS of the image using the command `RUN yum …`
  • Line 4: specifies the user of OpenFOAM. In this case I specify the `ofuser` as the user in the OF bashrc file
  • Line 6: specifies the terminal bash
  • As the source-codes in OF are specified with the file-type extensions of *.C, the intellisense engine fails to comprehend the structure of the code and many useful functionalities of VSCode will be disabled, e.g. code completion, Go to Defenition/Declaration, Inheritance Hieratchy, etc. The tools that could be very useful specially for understanding the source code. To solve this issue, in line 10, I copy a file settings.json into .vscode directory of the OF source code. The content of this file is:
  • Line 12: specifies the name of the container user account. Remember that you can run the container with root access if you replace the ofuser with root. But running MPI applications in root is not encouraged.
  • Usually I copy the .bashrc and .vimrc files of my local host (Possible for Mac/Linux distributions) to have the same vim and bash experience as I have in my host. Lines 15–16 do this.
  • Needless to say that for OF applications, two files of OF bashrc and OF need to be sourced. That is why I add the following two lines to my .bashrc file
source /opt/OpenFOAM/OpenFOAM-v2006/etc/bashrc WM_NCOMPPROCS=4 WM_MPLIB=SYSTEMOPENMPI FOAM_CONFIG_NOUSER=unsetsource /opt/OpenFOAM/
  • Line 18: creates .ssh directory to hold future ssh-keys from the host, required for git version control
  • The last line 19 creates the directory of $USER_RUN that is usually used to track the OF case files.
localHost-> docker run --rm -it -v ~/.ssh:/root/.ssh -v $PWD/ofuser:/OpenFOAM/ofuser-v2006 vscodedockerof:latest bash
  • “— rm” eliminates the container once it is closed,
  • “-it” runs the container in interactive mode through a terminal
  • “-v” mounts a volume from local host to the container: “~/.ssh:/ofuser/.ssh”, mounts the volume “~/.ssh” to “/ofuser/.ssh”, thus the public key copied into your git key registry would be unlocked with the private key copied in “/ofuser/.ssh”, allowing easier git integration in your container. $PWD/ofuser:/OpenFOAM/ofuser-v2006 mounts the volume ofuser in your working directory of your host into ofuser-v2006 working directory of your OF container, thus all your simulations (in run directory) or your modified source codes (in src directory) will be saved in the local machine once you exit the container.




Postdoctoral Researcher in Computational Mechanics, Fluid Dynamics, Scientific Software developer, Machine Learning Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SundaeSwap Live Demo (August 19th) — Latest News about the upcoming Cardano DEX

Being a better programmer than this morning — some aspects to focus on

AutoHotKey Window Management

Where to learn programming Or Get New Skills?

w3schools categories

Lessons learned from Systems Thinking

We are a FinOps Certified Service Provider!

Making Oracle Database Kubernetes-native

GameDev : create an Enemy(Part I)!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmad Amani

Ahmad Amani

Postdoctoral Researcher in Computational Mechanics, Fluid Dynamics, Scientific Software developer, Machine Learning Engineer

More from Medium

Multimodule Projects in Git Part1: Handling Subprojects Using Git Submodule

Top 10 essential DevOps tools

How to send Messages in Microsoft Teams using Incoming Webhook Adaptive Card

Setting Up SSH key for GIT using GUI-PuTTYgen