PyFOAM is a powerful Python library that enables the manipulation and control of OpenFOAM cases programmatically. Since OpenFOAM is primarily used in Linux-based environments, setting it up in Google Colab allows users to automate and analyze CFD simulations remotely. This guide provides a step-by-step approach to installing OpenFOAM and PyFOAM in Google Colab and demonstrates their usage with a 2D lid-driven cavity case.
Since Google Colab does not have OpenFOAM pre-installed, it must be manually set up before using PyFOAM.
Run the following commands to install OpenFOAM:
%%bash
sh -c "wget -O - <https://dl.openfoam.com/add-debian-repo.sh> | bash"
apt-get install openfoam2312-default
To install a different version, replace openfoam2312
with the desired version.
After installation, initialize OpenFOAM in the Colab session:
%%bash
source /usr/lib/openfoam/openfoam2312/etc/bashrc
This enables OpenFOAM commands such as blockMesh
and icoFoam
.
Once OpenFOAM is installed, install PyFOAM using pip:
!pip install PyFoam
Fig 1. Lid-driven cavity
The lid-driven cavity flow is a well-known benchmark problem in fluid mechanics. The domain consists of a square cavity with stationary walls on three sides (left, right, and bottom) and a moving lid at the top (Fig. 1).