10 Programing Open Foam Building Blocks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Disclaimer

This offering is not approved or


endorsed by OpenCFD Limited, the
producer of the OpenFOAM software
and owner of the OPENFOAM and
OpenCFD trade marks.

Introductory OpenFOAM Course


From 8th to 12th July, 2013

University of Genoa, DICCA


Dipartimento di Ingegneria Civile, Chimica e Ambientale

Your Lecturer

Joel GUERRERO

joel.guerrero@unige.it



guerrero@wolfdynamics.com

Acknowledgements
These slides and the tutorials presented are based upon personal
experience, OpenFOAM source code, OpenFOAM user guide,
OpenFOAM programmers guide, and presentations from previous
OpenFOAM training sessions and OpenFOAM workshops.
We gratefully acknowledge the following OpenFOAM users for their
consent to use their material:
Hrvoje Jasak. Wikki Ltd.
Hakan Nilsson. Department of Applied Mechanics, Chalmers
University of Technology.
Eric Paterson. Applied Research Laboratory Professor of Mechanical
Engineering, Pennsylvania State University.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Todays lecture

1. Programming in OpenFOAM.
Building blocks.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
During this session, we will:
First, we will start by taking a look at the algebra of tensors in
OpenFOAM.
We will then take a look at how to generate tensor fields from tensors.
We will learn how to access mesh information.
Finally we will see how to discretize a model equation and solve the
linear system of equations using OpenFOAM classes and templates.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
During this session, we will:
And of course, we are going to program a little bit in C++.
Do not be afraid, after all this is not a C++ course.
Remember, all OpenFOAM components are implemented in library
form for easy re-use.
OpenFOAM encourage code re-use. So basically we are going to
take something that already exist and we are going to modify it to our
needs.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

In the directory $WM_PROJECT_DIR/applications/test, you will find


the source code of several test cases that show the usage of most of
the OpenFOAM classes, including tensors.
For your convenience, I already copied the directory
$WM_PROJECT_DIR/applications/test to the folder
$path_to_openfoamcourse/test

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Some preliminaries,
OpenFOAM represent scalars, vectors and matrices as tensors
fields. A zero rank tensor is a scalar, a first rank tensor is a vector and
a second rank tensor is a matrix.
The PDEs we want to solve involve derivatives of tensor fields with
respect to time and space.
The PDEs must be discretized in time and space before we solve
them.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Basic tensor classes in OpenFOAM
OpenFOAM contains a C++ class library named primitive
($FOAM_SRC/OpenFOAM/primitives/). In this library, you will find
the classes for the tensor mathematics.
In the following table, we show the basic tensor classes available in
OpenFOAM, with their respective access functions.
Tensor Rank

Common name

Basic class

Access function

Scalar

scalar

Vector

vector

x(), y(), z()

Tensor

tensor

xx(), xy(), xz()

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
In OpenFOAM, the tensor

1
T= 4
7

2
5
8

can be declared in the following way

3
6
9

tensor T(1, 2, 3, 4, 5, 6, 7, 8, 9);


We can access the component T13 or Txz using the xz ( ) access function. For
instance, the code
Info << Txz = << T.xz ( ) << endl;
Will give the following output
Txz = 3
Notice that in OpenFOAM we use the function Info instead of the function cout.
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Algebraic tensor operations in OpenFOAM
Tensor operations operate on the entire tensor entity instead of a series
of operations on its components.
The OpenFOAM syntax closely mimics the syntax used in written
mathematics, using descriptive functions or symbolic operators.
Some of the algebraic tensor operations are listed in the following table.
The list is not complete

Mathematical
description

OpenFOAM
description

Addition

a+b

a+b

Scalar
multiplication

sa

s*a

Operation

Remarks

Outer product

rank a, b >=1

ab

a*b

Inner product

rank a, b >=1

a.b

a&b

Double inner
product

rank a, b >=2

a:b

a && b

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Examples of the use of some tensor classes
In the directory $WM_PROJECT_DIR/applications/test you will find
many examples showing you the use of the classes in OpenFOAM.
In the directory $path_to_openfoamcourse/test you will find a copy
of the folder $WM_PROJECT_DIR/applications/test.
Let us now compile a tensor class example. From the terminal:
cd $path_to_openfoamcourse/test/tensor
wmake (this will compile the source code and put the binary in the
directory $FOAM_USER_APPBIN)
test-tensor
At this point look at the output and study the file Test-tensor.C, and try to
understand what we have done.
After all, is not that difficult, right?
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Dimensional units in OpenFOAM
As you might already notice, OpenFOAM is fully dimensional.
Dimensional checking is implemented as a safeguard against
implementing a meaningless operation.
OpenFOAM encourages the user to attach dimensional units to
any tensor and will then perform dimension checking of any
tensor operation.
You can check $FOAM_SRC/OpenFOAM/dimensionedTypes/ for
the dimensional classes.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Dimensional units in OpenFOAM
Units are defined using the dimensionSet class tensor, with its units
defined using the dimensioned<Type> template class, the <Type>
being scalar, vector, tensor, etc. The dimensioned<Type> stores the
variable name, the dimensions and the tensor values.
For example, a tensor with dimensions is declare in the following way:
dimensionedTensor sigma
(
sigma,
dimensionSet(1, -1, -2, 0, 0, 0, 0),
tensor(10e6,0,0,0,10e6,0,0,0,10e6)
);

10
= 0
0

0
106
0

0
0
106

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Units correspondence in dimensionSet
No.

Property

Unit

Symbol

Mass

Kilogram

kg

Length

meters

Time

second

Temperature

Kelvin

Quantity

moles

mol

Current

ampere

Luminuous
intensity

candela

cd

dimensionSet (kg, m, s, K, mol, A, cd)


This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Units correspondence in dimensionSet

dimensionedTensor sigma
(
sigma,
dimensionSet(1, -1, -2, 0, 0, 0, 0),
tensor(1e6,0,0,0,1e6,0,0,0,1e6)
);

10
= 0
0

0
106
0

0
0
106

dimensionSet (kg, m, s, K, mol, A, cd)


This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Dimensional units examples
Add the following lines to Test-tensor.C (in the directory
$path_to_openfoamcourse/test/my_tensor you will find the modified files):
Before the main function main ( ):
#include dimensionedTensor.H
Before return(0):
dimensionedTensor sigma
(
"sigma",
dimensionSet(1, -1, -2, 0, 0, 0, 0),
tensor(1e6,0,0,0,1e6,0,0,0,1e6)
);
Info<< "Sigma: " << sigma << endl;
Compile, run, and look at the output:
Sigma sigma [1 -1 -2 0 0 0 0] (1e+06 0 0 0 1e+06 0 0 0 1e+06)
Notice that the object sigma, which belongs to the dimensionedTensor class,
contains the name, the dimensions and values.
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Dimensional units examples
Try to add the following member functions of the dimensionedTensor class in
Test-tensor.C (in the directory $path_to_openfoamcourse/test/my_tensor you
will find the modified files):
Info << Sigma name: << sigma.name ( ) << endl;
Info << Sigma dimensions: << sigma.dimensions ( ) << endl;
Info << Sigma value: << sigma.value ( ) << endl;
Also, extract some of the values of the tensor by adding the following line:
Info<< "Sigma yy (22) value: " << sigma.value().yy() << endl;
Note that the value() member function first converts the expression to a tensor,
which has a yy() member function. The dimensionedTensor class does not have
a yy() member function, so it is not possible to directly get its value by using
sigma.yy().

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
OpenFOAM lists and fields
OpenFOAM frequently needs to store sets of data and perform functions,
such as mathematical operations.
OpenFOAM therefore provides an array template class List<Type>, making
it possible to create a list of any object of class Type that inherits the functions
of the Type. For example a List of vector is List<vector>.
Lists of the tensor classes are defined in OpenFOAM by the template class
Field<Type>. For better code legibility, all instances of Field<Type>, e.g.
Field<vector>, are renamed using typedef declarations as scalarField,
vectorField, tensorField, symmTensorField, tensorThirdField and
symmTensorThirdField.
You can check $FOAM_SRC/OpenFOAM/fields/Fields for the field classes.
Algebraic operations can be performed between fields subject to obvious
restrictions such as the fields having the same number of elements.
OpenFOAM also supports operations between a field and a zero rank
tensor, e.g. all values of a Field U can be multiplied by the scalar 2 by simple
doing the following operation, U = 2.0 * U.
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Construction of a tensor field in OpenFOAM
Add the following to Test-tensor.C (in the directory
$path_to_openfoamcourse/test/my_tensor you will find the modified files):
Before main():
#include "tensorField.H"
Before return(0):
tensorField tf1(2, tensor::one);
Info<< "tf1: " << tf1 << endl;
tf1[0] = tensor(1, 2, 3, 4, 5, 6, 7, 8, 9);
Info<< "tf1: " << tf1 << endl;
Info<< "2.0*tf1: " << 2.0*tf1 << endl;
Compile, run, and look at the output:

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

Did something look familiar to you?


Absolutely yes, we already study these concepts in the
previous lecture. That is: typedef declarations, classes
and templates.
Remember, OpenFOAM uses typedef declarations,
classes and templates a lot!

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

Let us take a look at the whole solution process,


from creation of the tensors, to mesh assembly
and fields creation, to equation discretization;
by using OpenFOAM classes and template
classes

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Discretization of a tensor field in OpenFOAM
The dicretization is done using the FVM (Finite Volume Method).
No limitations on the number of faces bounding each cell.
No restriction on the alignment of each face.
The mesh class polyMesh is used to construct the polyhedral mesh using the
minimum information required.
You can check $FOAM_SRC/OpenFOAM/meshes/ for the polyMesh classes.
The fvMesh class extends the polyMesh class to include additional data
needed for the FV discretization.
You can check $FOAM_SRC/src/finiteVolume/fvMesh for the fvMesh
classes.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Discretization of a tensor field in OpenFOAM
The template class geometricField relates a tensor field to an fvMesh. Using
typedef declarations geometricField is renamed to volField (cell center),
surfaceField (cell faces), and pointField (cell vertices).
You can check $FOAM_SRC/OpenFOAM/fields/GeometricFields for the
geometricField classes.
The template class geometricField stores internal fields, boundary fields,
mesh information, dimensions, old values and previous iteration values.
A geometricField inherits all the tensor algebra of its corresponding field, has
dimension checking, and can be subjected to specific discretization
procedures.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Stored data in fvMesh class
Class

Description

Symbol

volScalarField

Cell volumes

surfaceVectorField

Face area vector

V
Sf

surfaceScalarField

Face area magnitude

volVectorField

Cell centres

surfaceVectorField
surfaceScalarField

|Sf |

Access
function
V()
Sf()
magSf()

Face centres

C
Cf

Cf()

Face fluxes

Phi()

C()

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Examine a fvMesh
Let us study a fvMesh example. First let us compile the Test-mesh
application. From the terminal
cd $path_to_openfoamcourse/test/mesh
wmake
In $path_to_openfoamcourse/test/my_cavity, you will find a modified copy
of the cavity tutorial. From the terminal:
cd $path_to_openfoamcourse/test/my_cavity
blockMesh
Now run Test-mesh in the $path_to_openfoamcourse/test/my_cavity
directory. Look at the output.
Let us take a look at the file Test-mesh.C:
C() gives the center of all cells and boundary faces.
V() gives the volume of all the cells.
Cf() gives the center of all the faces.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Examine a fvMesh
Now, try to add in Test-mesh.C the following lines (in the directory
$path_to_openfoamcourse/test/my_mesh you will find the modified files):
before return(0):
Info<< mesh.C().internalField()[1][1] << endl;
Info<< mesh.boundaryMesh()[0].name() << endl;
Compile, run, and look at the output.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Examine a volScalarField
Let us now read a volScalarField that corresponds to the mesh in
$path_to_openfoamcourse/test/my_cavity. Add the following to Test-mesh.C:

Before return(0):
volScalarField p
(
IOobject
(
"p",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
Info<< p << endl;
Info<< p.boundaryField()[0] << endl;

Compile, run, and look at the output.


This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Equation discretization in OpenFOAM
In this phase, OpenFOAM converts the PDEs into a set of linear algebraic
equations, Ax=b, where x and b are volFields (geometricField). A is a
fvMatrix, which is created by a discretization of a geometricField and inherits
the algebra of its corresponding field, and it supports many of the standard
algebraic matrix operations.
The fvm (Finite Volume Method) and fvc (Finite Volume Calculus) classes
contain static functions for the differential operators, and discretize any
geometricField.
fvm returns a fvMatrix, and fvc returns a geometricField.
In the directories $FOAM_SRC/finiteVolume/finiteVolume/fvc and
$FOAM_SRC/finiteVolume/finiteVolume/fvm you will find the respective
classes.
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Discretization of the basic PDE terms in OpenFOAM
The list is not complete

Mathematical
expression

Term description

Laplacian

Time derivative

Convection

vol<type>Field

laplacian(phi)
laplacian(Gamma, phi)

ddt(phi)
ddt(rho,phi)

() , ()

Source

fvm::
fvc::

scalar, volScalarField

div(psi,scheme)
div(psi,phi)
Sp(rho,phi)
SuSp(rho,phi)

surfaceScalarField

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Solution of the convection-diffusion equation
Let us now solve the convection-diffusion equation

T
+ (T ) (T ) = 0
t
Using OpenFOAM equation mimicking pragma, we can write this
equation as
solve
(
fvm::ddt(T)
+ fvm::div(phi,T)
- fvm::laplacian(DT,T)
);
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
Solution of the convection-diffusion equation
In $path_to_openfoamcourse/test/my_convection_diffusion, you will find
the source code for the solver of the convection-diffusion equation. In the
terminal:
cd $path_to_openfoamcourse/test/my_convection_diffusion
wmake
Now go to the
$path_to_openfoamcourse/test/my_convection_diffusion/case directory
and run the new solver. In the terminal:
cd $path_to_openfoamcourse/test/my_convection_diffusion/case
blockMesh
my_convection_difusion
paraFoam

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

Now you know how to program in


OpenFOAM using all C++ features.
Not that hard, right?

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM
I just presented the basic building blocks to program in
OpenFOAM.

For more information you should refer to the


programmers guide, doxygen documentation
and the OpenFOAM coding guide in the
openfoamwiki site.
http://openfoamwiki.net/index.php/Category:OpenFOAM_coding_guide
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

As usual,
remember that you have
the source code.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Programming in OpenFOAM

Additional tutorials
In the folders $path_to_openfoamcourse/c++_tuts,
$path_to_openfoamcourse/test and $path_to_openfoamcourse/programming
you will find many tutorials, try to go through each one to understand the basic
concepts of C++ and how to program in OpenFOAM.

This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

Thank you for your attention

Hands-on session

In the courses directory ($path_to_openfoamcourse) you will find many


tutorials (which are different from those that come with the OpenFOAM
installation), let us try to go through each one to understand and get functional
using OpenFOAM.
If you have a case of your own, let me know and I will try to do my best to help
you to setup your case. But remember, the physics is yours.
This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM and OpenCFD trade marks.

You might also like