Duc Nguyen
ngmaduc@gmail.com
March 22, 2014
1 Tutorial Overview
This tutorial presents two major topics: the introduction to OpenFOAM, a well-known open-
source CFD code, and the instruction for using it to obtain numerical results and visual simulation
to fluid mechanics problems. In the first part, an introduction to OpenFOAM is presented and
followed by the instruction to build OpenFOAM framework on a personal computer. Next, the
standard steps to achieve simulation are presented from pre-processing, setting up and controlling
the solver to post-processing. These tasks are done by a set of open-source tools and utilities such as
Salome, Paraview, PyFoam, etc. which are also covered in this tutorial as part of the OpenFOAM
Framework. The fluid mechanics problems for simulation in this tutorial is the phenomenon of
liquid column rising in square tubes with different side length in micro gravity environment. This
problem is inferred in this tutorial as Capillary Race problem. Following the tutorials, the
numerical results and visual simulation are obtained to compare with the experimental data using
drop tower mentioned in [16].
1
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Today, OpenFOAM is a proven player in commercial CFD and academic research [12], said
Hrvoje Jasak, main creator of OpenFOAM and also Director of Wikki Ltd., a UK-based Open-
FOAM Consultancy said in the 6th International OpenFOAM Workshop [9], Penn State University
in 2011. The OpenFoam workshop has been annually held since 2006 visting 6 countries in 3 con-
tinents. Main users of OpenFOAM include industrial CFD consultancies, academic institution,
community. According to extend-project.de [3], the major OpenFOAM community, around 50
groups are active around the world using and developing OpenFOAM as the main research and/or
services. Several groups with details information are listed in Appendix A of this tutorial. The
contributions of users from these vibrant community such as creating and sharpening solvers for
OpenFOAM are frequent, many are on monthly basis; this strengthens the power OpenFOAM.
In this tutorials, an example of using OpenFOAM for CFD purpose is introduced. OpenFOAM
core package is combined with third party tools for enhancing CFD work flow of OpenFOAM.
This approach is so-called in this tutorial as OpenFOAM Framework and is described Figure 2.
Open-source enables OpenFOAM to couple with numerous third-party enhancing tools including
also optimization tool such as DAKOTA [6] and high quality rendering tools such as Blender [2]
which go beyond the regular scope of CFD work flow. This tutorial introduces several tools and
choose some of those as the tools to solve the problem in this tutorial and to detail the instruc-
tion. SALOME [11], a well-known CAE platform, is used for pre-processing, ParaView [10], also
a well-known visualization program for post-processing, matplotlib [7] for plotting, built-in Open-
FOAM utilites and Python program such as PyFoam [4] and swak4Foam [5] for manipulating the
OpenFOAM solver. Appendix B provides a description of several notable tools to work within
OpenFOAM framework. This tutorial opens the door for users to open-source code world and
learning opportunities of UNIX, python, C programming, and code development. It also fosters
users understanding of the underlying algorithms and computation for numerical simulation.
2 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Figure 3: (C) Dynamic rise heights of the menisci in (a)2.9, (b)3.8, (c)7.1, and (d)10.8mm ID
circular tubes are shown after 2s of flow where 0.65cS PDMS rises a maximum length of 143mmin
(b) 3.8mm ID tube. The arrow highlights the optimal tube used to achieve maximum flow length
within the freefall time. (D) Dynamic rise heights of the menisci in (a)2.9, (b)3.8, (c)7.1,and
(d)10.8mm ID circular tubes are shown after 2s of flow where 5cS PDMS rises a maximum length of
67mmin (d) 10.8mm ID tube. The arrow highlights the optimal tube used to achieve maximumflow
length within the freefall time. [16]
3 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Figure 4: Sample of reduced data from tests performed in Fig. 2.1b, where the capillary rise
in circular tubes height l (t) is plotted against t and t 1/2 for 0.65cS (solid symbols) and 5cS
PDMS (open symbols). Predicting the flow that travels the farthest in the 2.1s drop time available
is an optimization problem between capillary, inertial, and viscous forces as functions of tube
dimensions. [16]
5. Setting Transport Properties, Initial and Boundary Conditons. Setting Solver Parameters
and Stopping Criteria for Transient Solution
6. Running the solver and Extracting Data with PyFoam and swak4Foam
7. Visualization with ParaView, Video encoding the results with avconv, Plotting the results
with matplotlib
4 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Installation of VMware Player is similiar to other Windows 7 software. After installation, the
start screen of VMware Player looks like below:
An image of Ubuntu 13.04 (64-bit) code-name Raring Ringtail is recommended as the operat-
ing system for OpenFOAM framework to run on. Go to http://www.ubuntu.com/download/
desktop to download a copy. The file should be ubuntu-13.04-desktop-amd64.iso
Click on Create a new Virtual Machine and choose the file downloaded on the above step as
the installer disc image file
The next steps include naming the virtual machine, setting a personal account, specifying
the disk capacity for your virtual machine. It is recommended to have at least 20GB for the
disk size and stored as a single file to enhance the performance of the virtual machine or as
Split for moderate amount of capacity. During installation, other hardware configuration is
also required. Below is an example of configuration for a virtual machine. Click Finish to
start the installation of Ubuntu.
Internet connection is mandatory since the VMware Player needs to download the VMWare
Tools for Linux. The installation of Ubuntu comes shortly after.
5 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Open a terminal which is the main communication tool to connect with OpenFOAM Frame-
work. The combination key is Ctrl+Alt+T.
Basic commands for files and directories in Ubuntu which are frequently used such as copy,
move, delete. File listing, check current path, change permission, change owner of files and/or
directories, use source to run a simple script are also important
Use text editor such as gedit or vim to edit and save a script
6 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Understand the path and environment variables of files and directories in Ubuntu. For ex-
ample, / is the home folder which also is a good place to include working directories for
applications of OpenFOAM Framework; / is the root folder; ./ is the root current folder
Right click on the file and choose Open with Archive Manager to open the file. In the Archive
Manager window, click Extract and choose the destination folder. The result is the installer
folder. The README file inside the folder includes the installation instruction for SALOME.
Open a Terminal and go to the installer folder. Type in the command ./runInstall -g like
the following screen-shot.
The graphic SALOME v7.3.0 Installation Wizard opens. The installation is composed of 8
steps. Choose Install binaries as Installation Type. Choose the Installation directory folder.
The Installation directory folder is /salome_7.3.0. Choose all the products for the choice
of the products to be installed then the installation progress will begin. Some steps are shown
in the following screen-shots.
7 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
During the installation, absence of some libraries might cause issues. Using the sudo apt-get
install in terminal to get the required libraries.
After the installation finishes, path and environment variables should be added to Ubuntu.
This task is very common when installing softwares in Ubuntu. To do this, one is to go to
kernel folder of SALOME which is just installed and run the script salome.sh by command
source salome.sh every time opening a new terminal; another better one is to add to it the
end /.bashrc script.
Once the path and environment variables are acknowledged by Ubuntu, SALOME could be
started anywhere. To start SALOME, open a new Terminal and type runSalome. Salome
should start and open the main working window.
8 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
In this tutorial, openfoam222 will be installed by sudo apt-get install method. Installa-
tion of OpenFOAM requires many libraries to be updated.
9 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The OpenFOAM package installation is completed with the reminder to add the bashrc script
of OpenFOAM the main bashrc script. This is done similiar to the way to configure terminal
for Salome.
Testing the installation successful of OpenFOAM could be done by testing if Ubuntu termi-
nal could realize the OpenFOAM command like icoFOAM --help or OpenFOAM path and
enviroment variables as shown the following screen-shot.
The case in this tutorial and many others inherit from the OpenFOAM tutorial cases. Copy
the tutorial from $FOAM_TUTORIALS directory to $FOAM_RUN which is the main working direc-
tory to run OpenFOAM cases later. To view the full path of these directories, echo command
could be used.
10 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Go inside the PyFoam folder and run the setup python script. Since the requisite libraries
for PyFoam such as python, numpy, scipy are already included in the SALOME directory,
the setup of PyFoam will refer to those libraries.
To test the installation of PyFoam, use the simple python program below. PyFOAM should
be linked to OpenFOAM.
11 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
swak4Foam which stands for Swiss Army Knife for OpenFOAM is a set of useful tool for extracting
data from OpenFOAM result and/or setting the boundary and initial conditions. swak4Foam could
be installed using instruction from openfoamwiki.net/index.php/Contrib/swak4Foam. This tu-
torial introduces another method which is successfully installed with OpenFOAM 2.2.2.
swak4Foam requires 2 packages which are Bison, a parser generator and Flex, a lexical
analyser. Installation of the two package are done by sudo apt-get install
Go to this CFD-online thread and download the patch so swak4foam 0.2.4 http://www.
cfd-online.com/Forums/openfoam-installation/118194-swak4foam-0-2-3-of2-2-x-installation-e
html
Unzip the swak4Foam package to get the installer folder whose name is swak4foam-OF22x and
move the patch inside this folder. The folder structure of swak4Foam looks like he following
screen-shot.
Unzip and run the patch inside the installer folder as in the following screen-shot.
12 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Once the installation is completed, it could be tested by enter a command of swak4Foam such
as funkySetFields. The result should display the swak4Foam version as in the following
screen-shot.
Capillary Race. This is done by video encoder such as avconv or ffmpeg. Installation of these
is also done by the use of sudo apt-get install method.
matplotlib is a set of Python plotting libraries and also includes pylab which is a procedural
state machine resembling MATLAB. Users can easily use syntaxes as simple as ones in MATLAB
to extract then plotting data for example in this tutorial the height of the capillary rise. Installation
of matplotlib is described as following steps.
Unzip the downloaded package to a destination folder. Go to the folder and run the python
setup script with the prefix referring to SALOME directory where Python is installed. The
command is python setup.py install --prefix=/salome_7.3.0/Python-2.7.3/
In this tutorial, gnuplot will be used for plotting the residuals of the numerical computation.
Installation could be also done by by sudo apt-get install method.
Start up SALOME by running start command runSalome in a bash terminal. Click on the Geometry
button or choose from the environment drop-down list to activate Geometry module. A window in
the following screen-shot appears, click on New to create a new Geometry.
The dimension is configured default to mm regarding the tubes in this problems are in mm. To do
this, Click File > Properties and change Length units to mm as shown in the following screen-shot.
14 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Firstly, the following steps will create the four tubes using cylinder tool in Geometry module.
Again, the inner diameters of the four tubes are 2.9, 3.8, 7.1 and 10.8 mm and the location of these
tubes are at the origin, 8, 20 and 36 mm respectively in y-axis of YZ-plane.
The default option allows the construction with the center of the base at the origin. Enter
the radius of 1.45 and the height of 160 for the first cylinder then click on Apply and Close
as the following screen-shot. The cylinder is shown on the OCC viewer screen.
Next steps are to create the points at which the next three tubes are placed. Select New
Entity>Basic>Point. Enter the coordinate (0,8,0) for x,y,z coordinates for the position of
second tube as the following screen-shot. Do the same thing to create the position for the
third and the fourth tubes at 20 and 36 mm respectively in y-axis of YZ-plane.
15 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The other tubes are created using cylinder function of Geometry module. However, this time
the base point and the vector will be used for the construction of the tubes. Choose the
option as as the following screen-shot. Enter the points created above as base point and Oz
as the vector then enter the radii 1.9, 3.55 and 5.4 and the height of 160. Click Apply after
entering the set of parameters for the second and the third tubes, click Apply and close for
the fourth tubes. By default, the tubes are named Cylinder 1 to Cylinder 4 and could be
replaced by appropriate name if necessary. All the four cylinders should be displayed in the
OCC viewer screen as below.
The main mesh later is on a single domain; therefore, four cylinders will be combined
together to become only one subject. This could be done using Fuse function. Select
Operations>Boolean>Fuse. The Fuse Objects window opens, click on the arrow button
and choose the 4 cylinders in the Object Browser. Four objects should be updated in the
16 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Fuse Object window as shown in the screen-shot below. Click Apply and Close, the Fuse
object named Fuse 1 will be created and added to Object Browser.
Once the fuse is created, it is ready for prepare the necessary faces, edges which are inlet,
outlet, wall and side edges. For each tubes, two faces on the bottom and on the tops are
needed as inlet and outlet. One curved face represents the wall and one side edge represents
the height. SALOME has the Create Group function to select and create those objects. Select
New Entity>Group>Create Group. Choose Face as Shape Type, Fuse 1 as the Main Shape
as shown in the following screen-shot. Give the name for the desired object.
In the OCC viewer screen, rotating and panning the 4 tubes to choose the desired object. For
example, choose the inlet of the first cylinder, this should be highlighted in white as shown
in the following screen-shot. Name it Inlet1 also.
17 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Click Add for the object selection and Apply. The face will be added in the box of the Create
Group Window as in following screen-shot. The face object Inlet will be created and added
under the Fuse object Fuse 1 in Object Browser.
The Apply button keeps the Create Group Window open to select new objects. Repeat the
process to select all necessary inlet, outlet, wall and side edges of four tubes. For the side
edges, the Shape Type option is line. For the combined inlet, outlet, wall and side edges,
the shape selection box could be added by choosing directly from OCC viewer screen or by
choosing from the single corresponding objects in the object browser. Click Apply and Close
when finishing create all desired objects. The final list of objects under the Fuse 1 object is
shown in following screen-shot.
18 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
To double check the face creation, hide All and use eye button toggling one-by-one object to
double check the object creation. Click View>Hide All then right click or use eye button to
show the desired object. For example, if the Inlet or the Side Edges or the Wall are chosen
to show ,the OCC viewer screen should display as in following screen-shot.
Select Mesh>Create Mesh. Naming the mesh as Mesh 1 and choose the object to mesh which
is Fuse 1 in this the tutorial.
The Create Mesh window opens. Choose Triangle (Mefisto) as the Algorithm from the Drop-
down list. Click on the Gear button to choose Length from Edges 1 as Hypothesis as shown
in the following screenshot.
19 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Navigate to 1D tab, choose Wire Discretion from the Drop-down list for Algorithm as shown
in the following screen-shot.
Click on the Gear button to choose and configure the Hypothesis. Enter 20 as Number of Seg-
ments and Equidistant Distribution as Type of Distribution in the Hypothesis Construction
window as in the following screen-shot. Click OK.
The Create Mesh Window now should have the definition for 2D and 1D meshing as shown
in the following screen-shot. Click Apply and Close.
20 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Click Mesh>Compute Mesh to start meshing. A window appears showing progress and RAM
usage information; after the meshing calculation complete, the meshing information appears
as shown in the following screen-shot. At this stage, surface meshing is done.
The surface meshing will be modified with Sub-mesh to prepare for 3D extrusion. The inlet
and outlet of each tube will be related using Projection 2D function so that the triangular
mesh of the inlet and the outlet will be the same. Click Mesh>Create Sub-Mesh. Name
the Sub-Mesh, choose the overall mesh Mesh 1 as the main Mesh. Choose the geometry by
clicking at the desired object in the Object Browser window, for example Outlet1, the outlet
of the first tube to which the first inlet projecting to. Click on the Gear button to choose
the Hypothesis which is the Source Face. In the Hypothesis construction window, choose the
Inlet1 as the Source Face then click OK. Click Apply on the Create Mesh window.
21 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The Sub-mesh for lateral wall face is also needed prepare for 3D extrusion. In the same
Create Mesh window as above, choose the combined wall face under Fuse Object. For the
Algorithm, choose Quadrangle (mapping) from the drop-down list. The Hypothesis could be
left blank as default chosen as standard.
Click on Mesh>Compute Mesh to start meshing. Once the mesh computation succeed, the
surface meshing is ready for volume meshing.
Click Mesh>Create Sub-Mesh, on the 3D tab, make sure Mesh 1, Fuse 1 are choosen for Mesh
and Geometry. Choose 3D extrusion as Algorithm in the Drop-down list. Click Apply and
Close, then Click on Mesh>Compute Mesh to start final volume meshing. Once the mesh
computation succeed, the Mesh information should display Volume mesh.
22 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
For this tutorial, it is desired to have 100 elements in the z-axis for a fine mesh. In order
to do that, another Sub-mesh is needed. Click Mesh>Create Sub-Mesh, choose Side edge
as the geometry. Click on the Gear button to choose and configure the Hypothesis. Enter
100 as Number of Segments and Equidistant Distribution as Type of Distribution in the
Hypothesis Construction window. Click OK. Click Apply and Close on the Mesh window.
The final Mesh structure should look like the following screen-shot.
23 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Click Mesh>Compute Mesh to start meshing. Once the mesh computation succeed, the Mesh
information is the final information for the mesh used in this tutorial. The mesh should also
be seen in the VTK viewer screen.
Click on Mesh>Create Group. Choose Mesh 1 for the Mesh and Face as the Element Type.
In Group Type, choose Group on Geometry. Click on the arrow close to Geometrical Object
and choose Direct Geometry Selection. From the Object Browser window, choose Inlet which
is the combined Inlet of the Fuse. The Geometrical Object is updated. The name of this
boundary is important since later it should be exactly the same in the OpenFOAM.
It is also recommended to change the color of the boundaries. To do this, go do to the end of
the Create Group Window and choose the color. Click Apply to finish creating the boundary
for Inlet.
24 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Repeating the same tasks of choosing the faces, naming and coloring for the Wall and the
Outlet. The boundary is now colored as in the following screen-shot. The group of faces as
boundaries are updated in Object Browser under the Mesh.
The mesh is ready to be exported to .UNV format to used as input for OpenFOAM. To do
this, within the Mesh environment, Click File>Export and choose UNV file. Choose the place
to save this mesh, later it will be moved inside an OpenFOAM folder.
25 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The strandard directory structure of an OpenFOAM case is shown in Fig 5 above. The top-level
directory of an OpenFOAM case is stored as a folder in the folder run inside the user working
directory $WM_PROJECT_DIR. This run folder could be accessed by the using environment variable
$FOAM RUN. There are five elementary sub-folders with the required for any OpenFOAM case. Those
sub-folders and their roles are listed below:
1. system: contains the run-time control, solver parameters and utilities definition
2. constant: contains fluid transport properties, turbulence modelling properties and general
physical properties
3. constant/polyMesh: contains the entire mesh data and information such as points, faces and
boundaries.
4. 0: contains the boundary conditions, initial conditions, phase interactions. Both scalar field
and vector field could be included
5. time_directories: contains the solution both default by solvers or derived fields by users.
The OpenFOAM top-level case directory might include more sub-folders when more utilities are
applied such as PyFoam and swak4Foam. One important thing remember is always to run Open-
FOAM solvers and utilities in the top-level case directory.
the tutorial folder, find the sample case of interFoam solver name capillaryRise and copy to the
run folder as shown the following screenshot. After that, rename the folder to CapillaryRace065
for 0.65cS PDMS. Repeat these steps to create another OpenFOAM case folder CapillaryRace500
for 5.0cS PDMS.
In Section 6.4, a mesh with .UNV form has already been created by SALOME and now ready
to be imported to OpenFOAM. To do this, follow the steps below:
Copy the .UNV mesh produced by SALOME to the top level OpenFOAM case directory. Make
sure the top-level case directory looks like the following screen-shot.
Open a terminal and direct to the top level OpenFOAM case directory. Use ideasUnvtoFoam
to convert the .UNV file to the mesh format which could be processed by OpenFOAM.
When the conversion is completed, use the checkMesh to see if the mesh is ready for Open-
FOAM.
27 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The Mesh should be reported OK. Another way to check the completeness of this mesh
conversion task is go directly to the sub-folders at which the OpenFOAM mesh information
is stored. Go inside the constant/polyMesh folder and check the points file, many points
should look similar to the radius and the height dimension of the tubes.
It is recognizable that OpenFOAM will take the number exactly as entered from SALOME
and take them default in meter dimension. OpenFOAM also has the utilities to scale dimen-
sion from meter to millimetre. Following the steps below to convert from meter to millimetre
and check the points for the effect.
28 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
All the transport properties of fluid phases are described in the transportProperties inside
the constant folder. This file let users to enter 7 physical dimensions such as mass, length,
time, temperature, etc. Find the place in the phase to edit dynamic viscosity, density for
both two phases and surface tension between two phases. Phase 1 is defined as the PDMS
and phase 2 is defined as air. The other parameters in transportProperties should be kept.
...
phase1
{
transportModel Newtonian ;
nu nu [0 2 -1 0 0 0 0] 0.65 e -06;
rho rho [1 -3 0 0 0 0 0] 760;
...
phase2
{
transportModel Newtonian ;
nu nu [0 2 -1 0 0 0 0] 1.48 e -05;
rho rho [1 -3 0 0 0 0 0] 1.2;
...
sigma sigma [1 0 -2 0 0 0 0] 0.0159;
...
...
29 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
phase1
{
transportModel Newtonian ;
nu nu [0 2 -1 0 0 0 0] 5.0 e -06;
rho rho [1 -3 0 0 0 0 0] 913;
...
phase2
{
transportModel Newtonian ;
nu nu [0 2 -1 0 0 0 0] 1.48 e -05;
rho rho [1 -3 0 0 0 0 0] 1.2;
...
sigma sigma [1 0 -2 0 0 0 0] 0.0197;
...
The contact angle is defined in alpha1.org inside the 0 folder. Find the place below and edit
for a static constant contact angle of zero degree.
...
Wall
{
type c o n s t a n t A l p ha C o n t a c t A n g l e ;
theta0 0;
limit gradient ;
value uniform 0;
}
...
Use gedit to open 0\alpha1.org, 0\U, 0\p_rgh file, change the names, types and values for
boundaries as following. The other information should be kept.
Outlet
30 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
{
type zeroGradient ;
}
Wall
{
type c o n s t a n t A l p ha C o n t a c t A n g l e ;
theta0 0;
limit gradient ;
value uniform 0;
}
...
...
boundaryField
{
Inlet
{
type pressureInletOutletVelocity ;
value uniform (0 0 0);
}
Outlet
{
type pressureInletOutletVelocity ;
value uniform (0 0 0);
}
Wall
{
type fixedValue ;
value uniform (0 0 0);
}
...
...
boundaryField
{
Inlet
{
type fixedValue ;
value uniform 0;
}
Outlet
{
type fixedValue ;
value uniform 0;
}
Wall
{
type fixedFluxPressure ;
}
...
The initial condition for the distribution of the two fluids in the continuum is alpha1 which is
PDMS is filled at 10mm in height of each tube and air filled the rest of the tube. OpenFOAM has
31 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
setFields utility to do this task. This utility need a dictionary to define the region which alpha1
is filled. The tutorial case of capillaryRise has already defined this dictionary.
Use gedit to edit a file named setFieldsDict inside the system folder
Edit the box to cell block to fill the four tubes as following
...
regions
(
boxToCell
{
box ( -1.5 e -3 -1.5 e -3 0) (1.5 e -3 1.5 e -3 10 e -3);
fieldValues
(
volScalarFieldValue alpha1 1
);
}
boxToCell
{
box ( -2e -3 6e -3 0) (2 e -3 10 e -3 10 e -3);
fieldValues
(
volScalarFieldValue alpha1 1
);
}
boxToCell
{
box ( -3.6 e -3 16.4 e -3 0) (3.6 e -3 23.6 e -3 10 e -3);
fieldValues
(
volScalarFieldValue alpha1 1
);
}
boxToCell
{
fieldValues
(
volScalarFieldValue alpha1 1
);
}
...
One file name alpha1 inside 0 folder is needed to store the information of the filled region.
It is also the output of setFields utility. The conventional way to do it is make a copy of
alpha1.org and rename it to alpha1
32 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Go up to the top-level case directory and run setFields utility. The output should look like
following:
Next, use ParaView to check if the boundary and initial conditions are correct. Run paraFoam
in top-level case directory to create a .foam file and enter ParaView program. Sometimes, if
OpenFOAM could not recognize paraFoam, it is necessary to re-run the bash script as shown
in the screen-shot below. Rerun the paraFoam command, the ParaView opens up.
Several options are configured such as select the mesh, the field to display, select the kind of
display. Edit the parameters in Object Inspector window as shown in the following screen-
shot and click Apply.
33 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Unclick the center show and set view direction to -X axis. The four tubes and the initial
PDMS filled should be shown. At this point, setting Boundary and Initial Conditions are
completed.
Note that the default color range display of ParaView is in CIELAB. To change it to HSV
color range as it this tutorial, click on tab Display of Object Browser and chose Edit Color
Map. The Color Scale Editor opens, choose HSV as Color space and click Make Default.
34 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
9.3 Setting Solver Parameters and Stopping Criteria for Transient Solution
Solver Parameter and Stopping Criteria are important as they define how solver will run to get
converge and reliable results. These are stored at 3 files inside system sub-fodler: fvSchemes for
finite element algorithm, fvSolution for solver algorithm and convergence criteria, controlDict
for stopping criteria. In this tutorial, the settings of the tutorial case are generally kept; only
residual control, run time, time step, maximum inner iteration will be modified in this tutorial.
To set up the run time and time step, use gedit to modify the controlDict file. The full
explanation for the parameters could be found at OpenFOAM reference such as citeJoel. In
this tutorial, the run time for 0.65cS PDMS case will be 0.7s and the time step will be 0.001s,
the adjust time step option should be set to no. For 5.0cS PDMS the run time will be 1.4s
and the time step is still 0.001s. For the first time, the run time may be not known so 2s
could be set for the run time but after the first trial run, the time for at least one of the tube
has fluid reaching its top is known so the run time could be modified.
...
stopAt endTime ;
endTime 0.7;
deltaT 0.001;
...
runT imeModifiable yes ;
adjustTimeStep no ;
maxCo 0.2;
maxAlphaCo 0.2;
...
The Capillary Race in this tutorial involves transient problem with each time step itself is
constraint with conditions like for residual and Courant number. Such information of residual,
inner iteration, Courant number are known as solver parameters. It is desired that the solver
algorithm will continue to run until it satisfies the known-goood chosen solver parameters. The
Courant number represents Courant-Friedrichs-Lewy (CFL) condition for stability solution
for solving partial differential equations (PDE) numerically by the method of finite differences
[13]. The interFoam solver models the Capillary Race by PDE; therefore,the CFL condition
is indispensable. The maximum Courant number in this tutorial is also kept the same as the
tutorial case and equal to 0.2. It is stored in controlDict file.
35 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Each time step solution is allowed a specific number of iteration so-called the maximum
number of iteration to meet both residual and Courant number condition. The Courant
number condition is already set above. For the maximum number of iteration, use gedit to
edit a file named fvsolution inside the system folder. The parameter nOuterCorrectors
inside PIMPLE algorithm is edited to 100 for maximum number of iteration of each time
step and the new block of residual control is added for the final residual of each time step is
1e-05.
...
PIMPLE
{
momentumPredictor no ;
nOuterCorrectors 100;
// nCorrectors 3;
n N on O r t ho g o n al C o r re c t o rs 0;
nAlphaCorr 1;
nAlphaSubCycles 2;
cAlpha 1;
residualControl
{
p_rgh
{
tolerance 1e -05;
relTol 0;
absTol 0;
}
}
}
...
The important data from this simulation to compare with experimental data is the meniscus height,
the lowest point of the meniscus. The solver itself only provides the volume fraction of full domain
inside the tubes. It is necessary to analyze the data from the solver to attain the meniscus height.
The assumption of symmetry is applied which means that the menicus lowest point is along the
center line of the tubes at each time step. The meniscus height could be achieved by sampling the
36 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
volume fraction of alpha1 which is PDMS along the center line. The volume fraction of alpha1 is
1 for the region filled with PDMS while volume fraction of alpha1 is 0 if the region is filled with
air. The meniscus is where the volume fraction change from 1 to 0. In this tutorial, the meniscus
height is defined where the volume fraction is 0.5. The task of sampling volume fraction could be
done by using the sample utility of OpenFOAM. This could also be done using a Filter in Paraview
which even features plotting function. In this tutorial, script method based on swak4Foam will be
used.
The first step is to prepare a dictionary for sampling using swak4Foam. Since this dictio-
nary is not part of the tutorial case, a new file will be created from scratch. Use gedit to
create four files name sampledSets1, sampledSets2, sampledSets3, sampledSets4 inside
system folder to sample the meniscus height for 4 tubes. For example the sampledSets1 is
shown below.
fillHeight1 {
type swakExpression ;
valueType set ;
verbose true ;
setName lineUp1 ;
set {
type uniform ;
axis z ;
start (0 0 0) ;
end ( 0 0 0.16) ;
nPoints 200;
}
expression "( alpha1 > 0.5) ? pos (). z : 0";
accumulations (
max
);
interpolate true ;
interpolationType cellPoint ;
}
Those sampledSets and the necessary swak4Foam libraries should be declared in controlDict.
Use gedit to add the following to controlDict.
...
libs (
" libOpenFOAM . so "
" l i b s i m p l e S w a k F u n c t i o n O b j e c t s . so "
" li bs wak Fu nc ti onO bj ec ts . so "
" l i b si m p l eF u n c ti o n O bj e c t s . so "
);
functions
{
# include " sampledSets1 "
# include " sampledSets2 "
# include " sampledSets3 "
# include " sampledSets4 "
}
...
37 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Go to top-level case directory and enter the command pyFoamPlotRunner.py --clear interFoam
> log -case /$FOAM RUN/capillaryRace065 to start running the solver. PyFOAM should
start the interFoam and record the initial residuals and continuity.
Upon completion of the running of the solver, the data for residuals and continuity are shown
below for the cases of 0.65cS and 5.0cS respectively. The plot of the residual is only the initial
residual of a time step; the final residual is always in order of 1e-05.
38 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The final top-level case directory after the computation completed looks like below.
swak4Foam records the data for meniscus height to 4 folders inside postprocessing folder;
one for each tube. Inside each folder there is only one folder 0 storing the meniscus height
data in file with name according to swak4Foam dictionary.
39 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
First, make sure that the options below are configured such as choose the mesh, the field to
display, choose the kind of display. Edit the parameters in Object Inspector window as shown
in the following screenshot and click Apply.
40 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Choose Filter>Common>Slice, a new slice object named Slice1 added in the Pipeline Browser.
Rename it to MidPlane and in the Object Inspector, choose the Origin and Normal as shown
in the following screen-shot.
Choose Source>Annotate Time and Source>Text to add the time and the text to the solution.
These objects will be added to Pipeline Browser. Choose each object so it could be defined
and formatted in Object Inspector. The Pipeline Browser and the visual result could be like
the following screenshot.
41 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Use the Control toolbar in ParaView, users could visualize the dynamic transient solution or
see the solution at any time step.
42 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Make sure that the steps in Section 11.1 is completed. Click one File>Save Animation.
The Animation Settings Dialog will appear. Choose Resolution of 1920x1080 pixels as below.
Click Save Animation.
The destination folder the prefix for the image which will be saved. For example, the top-level
case directory will be used for the destination folder and height will be used for the prefix in
this tutorial. Click OK, ParaView will automatically run and after the process is done, the
image file will appear in the destination folder.
Browser
Open a terminal and go to top-level case directory where the still images are saved. Many
options could be chosen for avconv to work. The reference could be found [1]. The options
for avconv shown below could produce a decent HD1080 video. The file is encoded in .mp4
format with x264 codec. The name of the file is output.mp4. The file could be played well
with VLC player or Windows Media Player 11. In window, it is suggested the K-lite codec
package at http://www.free-codecs.com/k_lite_codec_pack_download.htm is installed
while gstreamer1.0 is recommended in Ubuntu.
43 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
Create a Python file as below to open the data file extracted and saved by swak4Foam in
Section 10. The file is named fillHeight.py in this tutorial and could be saved anywhere in
Ubuntu. However, it is recommended to the file in the running directory or the top-level case
directory. The figures for the plots will be saved as .png format at the same place as the
python file. The pylab and numpy libraries imported in the Python code are included with
SALOME and matplotlib.
import pylab , numpy
44 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
pylab . plot ( data2 [ range (0 ,699 ,35) ,0] , data2 [ range (0 ,699 ,35) ,1] * 1000 , kd ,
label = 3.8 mm ID )
pylab . plot ( data3 [ range (0 ,699 ,35) ,0] , data3 [ range (0 ,699 ,35) ,1] * 1000 , ks ,
label = 7.1 mm ID )
pylab . plot ( data4 [ range (0 ,699 ,35) ,0] , data4 [ range (0 ,699 ,35) ,1] * 1000 , ko ,
label = 10.8 mmID )
pylab . legend ()
pylab . xlabel ( r $t [ s ] $ )
pylab . ylabel ( r $l [ mm ] $ )
pylab . legend ( loc = upper left )
pylab . savefig ( lvst . png )
pylab . figure (1)
pylab . plot ( data5 [ range (0 ,699 ,35) ,0] ** 0.5 , data5 [ range (0 ,699 ,35) ,1] * 1000 ,
w ^ )
pylab . plot ( data6 [ range (0 ,699 ,35) ,0] ** 0.5 , data6 [ range (0 ,699 ,35) ,1] * 1000 ,
wd )
pylab . plot ( data7 [ range (0 ,699 ,35) ,0] ** 0.5 , data7 [ range (0 ,699 ,35) ,1] * 1000 ,
ws )
pylab . plot ( data8 [ range (0 ,699 ,35) ,0] ** 0.5 , data8 [ range (0 ,699 ,35) ,1] * 1000 ,
wo )
pylab . plot ( data1 [ range (0 ,699 ,35) ,0] ** 0.5 , data1 [ range (0 ,699 ,35) ,1] * 1000 ,
k ^ , label = 2.9 mm ID )
pylab . plot ( data2 [ range (0 ,699 ,35) ,0] ** 0.5 , data2 [ range (0 ,699 ,35) ,1] * 1000 ,
kd , label = 3.8 mm ID )
pylab . plot ( data3 [ range (0 ,699 ,35) ,0] ** 0.5 , data3 [ range (0 ,699 ,35) ,1] * 1000 ,
ks , label = 7.1 mm ID )
pylab . plot ( data4 [ range (0 ,699 ,35) ,0] ** 0.5 , data4 [ range (0 ,699 ,35) ,1] * 1000 ,
ko , label = 10.8 mmID )
pylab . legend ()
pylab . xlabel ( r $t ^{1/2}( s ) $ )
pylab . ylabel ( r $l [ mm ] $ )
pylab . legend ( loc = upper left )
pylab . savefig ( lvst0 .5. png )
pylab . show ()
For the case of 5.0cS PDMS, the time for at least one of the top reach the top of the tube is
1.4s so the run time is 1.4s which is longer than that of the case of 0.65cS PDMS. The plot
of meniscus height to the time and the plot of meniscus height to the square root of time are
shown below:
45 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
In this tutorial, gnuplot is used for plotting the residual of the numerical computation rather than
matplotlib since matplotlib requires more complicated code while using gnuplot is simpler:
Use gedit to create a script Residuals at the top-level case directory. This script plots the
residuals in log scale and save the plot in .png format also at the top-level case directory for
0.65cS and 5.0cS cases.
set term png
set output " reslog065 . png "
set autoscale
set logscale y
set ylabel Residual
set xlabel Iteration
plot " < cat log | grep Solving for p_rgh | cut -d - f9 | tr -d , " title
Residual with lines
46 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The geometry set-up in this tutorial is simplified than the set up of the experiment. The entrance
effect when the fluid enter the capillary tubes from the reservoir, the drag effect and the moving
contact line from the surrounding air are neglected. The capillary raise in each tube is also con-
sider symmetrical. The following conclusions could be made regarding the comparison between
OpenFOAM simulation results and experimental results.
The 0.65cS PDMS generally rises faster than do the 5.0cS PDMS especially for the longer
order of time scale.
47 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
The Capillary race by simulation is similar to the race in experiment in term of the order
of the meniscus height at finish time of 0.7s. From low to high, the order are 10.8mm,
7.1mm, 2.9mm, 3.8mm ID for 0.65cS PDMS and 2.9mm, 3.8mm, 10.8 mm, 7.1 mm ID for
5.0cS PDMS.
It tooks shorter or in other word the fluid ascends faster according to the simulation than in
the experiment. It takes only 0.7s for the simulation to reach the state of 2s in the experiment.
Recall that the simulation stops when one of the for tube has the fluid reaches it top. The
longer simulation result for the case of 5.0cS PDMS is also shown. For the case of 5.0cS
PDMS, OpenFOAM result for the order of the meniscus height when the simulation stops
at 1.4s is also similar to the experiment stop at 2.0s. The order from low to high is 2.9mm,
3.8mm, 7.1 mm and 10.8 mm ID.
The difference of the time scale of the rate of rise between OpenFOAM results and experi-
mental result challenges two things: first is that the entry effect and the moving contact line
actually dampen the rise which are still not included in this tutorial; second, finer mesh and
more accurate numerical method needs to be applied for more numerical converge and reliable
results. Parrallel processing, smart and adaptive meshing, fine-tuning boundary conditions
and numerical algorithm are suggested for further simulation.
The bright side of the OpenFOAM simulation of Capillary race it takes short time for
roughly correct simulation of the problem. Only 40 minutes is needed for 1000 data point of
1.4s of 5.0 cS fluid raising in each tube and 96 minutes for 0.7s of 0.65cS for the same data
points.
48 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
49 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
50 of 51
ME448 Capillary Race in Circular Tubes using OpenFOAM Duc Nguyen
References
[1] avconv documentation, www.libav.org/avconv.html.
[2] Blender, www.blender.org.
[3] Community-driven releases of openfoam, www.extend-project.de.
[4] Contrib/pyfoam - openfoamwiki, openfoamwiki.net/index.php/Contrib/PyFoam.
[5] Contrib/swak4foam - openfoamwiki, openfoamwiki.net/index.php/Contrib/swak4Foam.
[6] The design analysis kit for optimization and terascale applications (dakota),
www.dakota.sandia.gov/software.html.
[7] matplotlib: python plotting, www.matplotlib.org.
[8] Openfoam (open source field operation and manipulation), www.openfoam.com.
[9] Openfoam workshop, www.openfoamworkshop.org.
[10] Paraview - open source scientific visualization, www.paraview.org.
[11] Salome plaform, www.salome-platform.net.
[12] Hrvoje Jasak, Openfoam: a year in review, 2010.
[13] Richard Courant, Kurt Friedrichs, and Hans Lewy, On the partial difference equations of mathematical physics,
IBM journal of Research and Development 11 (1967), no. 2, 215234.
[14] Joel Guerrero, 2014 winter session - dicca, university of genoaintroductory openfoam course-training with an
overview to numericaloptimization using dakota, www.dicat.unige.it/guerrero/OpenFOAMc ourse2014a.html.
[15] Edward W Washburn, The dynamics of capillary flow, Physical review 17 (1921), no. 3, 273.
[16] Andrew Paul Wollman, Capillarity-driven droplet ejection, 2012.
51 of 51