image.png

image.png

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.

1. Installing OpenFOAM and PyFOAM in Google Colab

Since Google Colab does not have OpenFOAM pre-installed, it must be manually set up before using PyFOAM.

Step 1: Install OpenFOAM

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.

Step 2: Set Up the OpenFOAM Environment

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.

Step 3: Install PyFOAM

Once OpenFOAM is installed, install PyFOAM using pip:

!pip install PyFoam

2. Running the 2D Lid-Driven Cavity Case

Fig 1. Lid-driven cavity

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).