Automation of OpenFOAM multiphase bubble simulations

January 4, 2022

Let us consider a channel in which we wish to observe the movement and dynamics of bubbles.

One of the ways to accomplish this using the open source CFD toolbox OpenFOAM (OF).

We can use the OF multiphase solvers such as interFoam, here we introduce a bubble in the flow and observe the propagation of this bubble. The introduction of the bubble is done using a function called setFieldsDict.

This function basically sets few cells in the mesh to a certain volume fraction as defined by the user. Say for this example we require to observe air bubbles in a channel with water. Hence we set the volume fraction of water (alpha.water) as 0 where the air bubbles exist. Then the simulation is run to observe what happens to this bubble. We assume the bubbles to be spherical at the start of the simulation, thus we can use the sphere to cell function inside the setFields function.

This function requires the radius and the coordinates of the sphere in this case the origin and the radius of the bubble. The function then identifies the cells and sets the water volume fraction to 0. This is quite straight forward when dealing with one or two bubbles.

But this becomes tedious when it must be scaled up to say a hundred bubbles. This also becomes an issue if the origin of the bubbles must be randomised. The specific format in which the function must be written also poses a challenge.

A snippet of the sphereToCell function:

Random or non-random origin points can be created using a programming language like python, but how do we create this specific template file using python? Here we can take use of something called jinja or jinja2.

Jinja is a template engine, we provide a certain template and jinja uses that template to create files that are useful for us. It is mostly used by web developers to create HTML files, but as a CFD engineer it is very useful in creating OF files. Using jinja is very simple, we create a template (in this case a setFieldsDict template) in which we specify what are variables and what are constants.

Thus, for this example the jinja template would look something like this:

Here the defaultFieldValues sets the entire domain with a alpha.water of 1 i.e. the entire domain is comprised of water. Since we require the code to create multiple sphereToCell entries, the sphereToCell is enclosed in a for loop.

When we wish to introduce a statement such as loops we enclose it inside a {%....%} and when we wish to introduce a variable we use {{….}}.

In this example the variable i will run from 0 to length which in this case is a scalar value. Here x, y and z coordinates are supplied as arrays. Thus, we will create sphereToCell entries i times. Let’s say for example length is equal to 2. The array x, y and z contain the following entries. X = [1, 2, 3]; Y= [4, 5, 6]; Z= [7, 8, 9] and radius is equal to 0.2m.

This would generate the following setFieldsDict file:

This can be scaled to hundred or even a thousand sphereToCell entries. Best part about this is that we can use the full potential of python to create random origin points, origin points within bounds, random radius within bounds, and many more. Another important advantage of using this method is that we run setFields on once before the start of the simulation.

In this article jinja has been used to create a setFields entry, but by modifying the template file slightly we can also create other files such as U, p, p_rgh, k, omega and so on.

Basically, the whole file structure of the OF case can be generated (except the mesh) using this function and python.  

We would like to thank Bernhard Gschaider who is a contributor for pyFoam (another great tool for OF users).

For any questions regarding this topic, please send a mail at:

For further information on jinja please check out the jinja documentation at:

This tutorial on how to use jinja:

Newtrace is enabling wide-scale adoption of affordable green Hydrogen through their novel electrolyzer technology to help fight climate change and build a sustainable future for all.  Follow us to stay informed about our updates!

About Author

Related posts
No items found.