10 programing open foam building blocks

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

Upload: francisca-claudia-melo-melo

Post on 31-Dec-2015

40 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 10 Programing Open Foam Building Blocks

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

Page 2: 10 Programing Open Foam Building Blocks

Introductory OpenFOAM® Course

University of Genoa, DICCA Dipartimento di Ingegneria Civile, Chimica e Ambientale

From 8th to 12th July, 2013

Page 3: 10 Programing Open Foam Building Blocks

Your Lecturer

Joel GUERRERO

[email protected]

   

[email protected]  

Page 4: 10 Programing Open Foam Building Blocks

Acknowledgements

These slides and the tutorials presented are based upon personal experience, OpenFOAM® source code, OpenFOAM® user guide, OpenFOAM® programmer’s 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.”

Page 5: 10 Programing Open Foam Building Blocks

Today’s 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.”

Page 6: 10 Programing Open Foam Building Blocks

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®

Page 7: 10 Programing Open Foam Building Blocks

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®

Page 8: 10 Programing Open Foam Building Blocks

•  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®

Page 9: 10 Programing Open Foam Building Blocks

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

Page 10: 10 Programing Open Foam Building Blocks

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

0 Scalar scalar

1 Vector vector x(), y(), z()

2 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®

Page 11: 10 Programing Open Foam Building Blocks

In OpenFOAM®, the tensor

can be declared in the following way tensor T(1, 2, 3, 4, 5, 6, 7, 8, 9);

We can access the component or 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®

T =

1 2 34 5 67 8 9

T13 Txz

Page 12: 10 Programing Open Foam Building Blocks

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.

Operation Remarks Mathematical description

OpenFOAM® description

Addition a + b a + b

Scalar multiplication sa s * a

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®

The list is not complete

Page 13: 10 Programing Open Foam Building Blocks

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®

Page 14: 10 Programing Open Foam Building Blocks

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

Page 15: 10 Programing Open Foam Building Blocks

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:

“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.”

dimensionedTensor sigma (

“sigma”, dimensionSet(1, -1, -2, 0, 0, 0, 0), tensor(10e6,0,0,0,10e6,0,0,0,10e6)

);

≡ σ =

106 0 00 106 00 0 106

Page 16: 10 Programing Open Foam Building Blocks

Units correspondence in dimensionSet

No. Property Unit Symbol

1 Mass Kilogram kg

2 Length meters m

3 Time second s

4 Temperature Kelvin K

5 Quantity moles mol

6 Current ampere A

7 Luminuous intensity candela 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®

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

Page 17: 10 Programing Open Foam Building Blocks

Units correspondence in dimensionSet

“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®

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

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

≡ σ =

106 0 00 106 00 0 106

Page 18: 10 Programing Open Foam Building Blocks

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®

Page 19: 10 Programing Open Foam Building Blocks

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®

Page 20: 10 Programing Open Foam Building Blocks

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®

Page 21: 10 Programing Open Foam Building Blocks

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®

Page 22: 10 Programing Open Foam Building Blocks

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®

Page 23: 10 Programing Open Foam Building Blocks

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®

Page 24: 10 Programing Open Foam Building Blocks

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®

Page 25: 10 Programing Open Foam Building Blocks

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®

Page 26: 10 Programing Open Foam Building Blocks

Stored data in fvMesh class

“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®

Class Description Symbol Access function

volScalarField Cell volumes V()

surfaceVectorField Face area vector Sf()

surfaceScalarField Face area magnitude magSf()

volVectorField Cell centres C()

surfaceVectorField Face centres Cf()

surfaceScalarField Face fluxes Phi()

V

Sf

|Sf |C

Cf

φg

Page 27: 10 Programing Open Foam Building Blocks

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®

Page 28: 10 Programing Open Foam Building Blocks

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®

Page 29: 10 Programing Open Foam Building Blocks

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®

Page 30: 10 Programing Open Foam Building Blocks

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®

Page 31: 10 Programing Open Foam Building Blocks

Discretization of the basic PDE terms in OpenFOAM®

Term description Mathematical expression

fvm:: fvc::

Laplacian laplacian(phi) laplacian(Gamma, phi)

Time derivative ddt(phi) ddt(rho,phi)

Convection div(psi,scheme) div(psi,phi)

Source Sp(rho,phi) SuSp(rho,phi)

,

,

,

vol<type>Field scalar, volScalarField 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®

∇2φ ∇ · Γ∇φ

ρφ

φ ρ ψ

∇ · (ψ) ∇ · (ψφ)

∂φ

∂t

∂ρφ

∂t

The list is not complete

Page 32: 10 Programing Open Foam Building Blocks

Solution of the convection-diffusion equation Let us now solve the convection-diffusion equation 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®

∂T

∂t+∇ · (φT )−∇ · (Γ∇T ) = 0

Page 33: 10 Programing Open Foam Building Blocks

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®

Page 34: 10 Programing Open Foam Building Blocks

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®

Page 35: 10 Programing Open Foam Building Blocks

I just presented the basic building blocks 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.”

Programming in OpenFOAM®

For more information you should refer to the programmer’s guide, doxygen documentation and the OpenFOAM® coding guide in the openfoamwiki site. http://openfoamwiki.net/index.php/Category:OpenFOAM_coding_guide

Page 36: 10 Programing Open Foam Building Blocks

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®

Page 37: 10 Programing Open Foam Building Blocks

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

Programming in OpenFOAM®

Page 38: 10 Programing Open Foam Building Blocks

Thank you for your attention

Page 39: 10 Programing Open Foam Building Blocks

Hands-on session

In the course’s 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.”