MAOFnepO
++C ot noitcudortnI nA
Arrays
An array is a series of elements of the same type placed
in contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
That means that, for example, we can store 5 values of
type int in an array without having to declare 5 different
variables, each one with a different identifier
5/8/2014 2
OpenFOAM
Arrays
Defenition of an array:
type name [elements];
float volScalarField [4]; volScalarField [0]=1000;
Then volScalarField is array of 4 float numbers
5/8/2014 3
OpenFOAM
1000 0 0 0
float float float float
pointers
In arrays we have only as much memory available as
we declared for our variables.
But, what if we need a variable amount of memory that
can only be determined during runtime?
We can use dynamic memory with pointers.
Pointers point at a memory location.
5/8/2014 4
OpenFOAM
pointers
Defenition of a pointer:
pointer = new type
pointer = new type [number_of_elements]
int * ali;
ali= new int [5];
5/8/2014 5
OpenFOAM
pointers
Turbulence models are treated with the turbulence
pointer in OpenFOAM.
See:
$FOAM_SOLVERS/incompressible/pisoFoam/createFields.H:
autoPtr turbulence
(incompressible::turbulenceModel::New(U, phi, laminarTransport) );
5/8/2014 6
OpenFOAM
types
Pre-dened C++ types are:
C++ allows the definition of our own types based on
other existing data types. We can do this using the
keyword typedef, whose format is:
typedef existingType newTypeName ;
5/8/2014 7
OpenFOAM
types
OpenFOAM provides numerous types.
See scalarField.H in:
/opt/openfoam201/src/OpenFOAM/fields/Fields/scalarField
typedef Field scalarField;
See scalar.H in:
/opt/openfoam201/src/OpenFOAM/primitives/Scalar/scalar
typedef floatScalar scalar;
5/8/2014 8
OpenFOAM
types
See floatScalar.H in:
/opt/openfoam201/src/OpenFOAM/primitives/Scalar/fl
oatScalar
typedef float floatScalar;
5/8/2014 9
OpenFOAM
types
Types in OpenFOAM:
scalar---> float
vector---> array (with size 3) of float
scalarField---> array with undefined size of scalars
vectorField---> array with undefined size of vectors
5/8/2014 10
OpenFOAM
types
Types in OpenFOAM:
volScalarField---> array of scalars with mesh size
volVectorField---> array of vectors with mesh size
5/8/2014 11
OpenFOAM
functions
5/8/2014 12
OpenFOAM
A function is a group of statements that is executed when it is called from some point of the program.
type name ( parameter1, parameter2, ...) { statements }
type is the data type specifier of the data returned by the function. This can be void also. name is the identifier by which it will be possible to call the function. parameters (as many as needed): Each parameter consists of a data
type specifier followed by an identifier, like any regular variable
declaration (for example: int x) and which acts within the function as a
regular local variable. They allow to pass arguments to the function
when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded
by braces { }.
functions
5/8/2014 13
OpenFOAM
// function example
#include
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout
functions
Now see void kEpsilon::correct() in :
/opt/openfoam201/src/turbulenceModels/incompressib
le/RAS/kEpsilon/kEpsilon.C
Or type:
> src
> cd /turbulenceModels/incompressible/RAS/kEpsilon
> gedit kEpsilon.C
5/8/2014 14
OpenFOAM
functions
All functions must be declared before their definition
This is definition:
type name ( parameter1, parameter2, ...) { statements }
This is declaration:
type name ( parameter1, parameter2, ...)
5/8/2014 15
OpenFOAM
functions
In OpenFOAM declarations are in header files (*.H)
Example:
See kEpsilon.H in
/opt/openfoam201/src/turbulenceModels/incompressib
le/RAS/kEpsilon/kEpsilon.H
5/8/2014 16
OpenFOAM
functions
There might be some cases where you need to
manipulate from inside a function the value of an
external variable.
Use arguments passed by reference
In OpenFOAM almost all arguments passed by
reference
5/8/2014 17
OpenFOAM
functions
x=2, y=6, z=14
5/8/2014 18
OpenFOAM
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout
functions
See
In ODEChemistryModel.H and .C files
/opt/openfoam201/src/thermophysicalModels/chemistr
yModel/chemistryModel/ODEChemistryModel
5/8/2014 19
OpenFOAM
virtual tmp omega(const
scalarField& c, const scalar T, const scalar
p) const;
functions
What is omega:
=1 = =1
,
=
= =1 = =1
,
=
,
= =1
=1
,
5/8/2014 20
OpenFOAM
5/8/2014 21
OpenFOAM
=1
= =1
,
= =1
=1
,
= =1
,
classes
The types that we have just had a look at are in fact
classes, and the variables we assign to a type are
objects of that class.
int ali=0;
ali=ali+1;
5/8/2014 22
OpenFOAM
function
object
class
classes
See createFields.H in:
/opt/openfoam201/solvers/combustion/reactingFoam
volScalarField rho
(
IOobject( "rho",
runTime.timeName(),
mesh
),
thermo.rho()
);
5/8/2014 23
OpenFOAM
classobject
psiChemistryModel& chemistry =
pChemistry();
classes
We can gather functions in a class
Also we can gather data in a class
Classes can be gathered in a library.
5/8/2014 24
OpenFOAM
classes
public attributes are visible from outside the class.
private attributes are only visible within the class.
See kEpsilon.H in:
$FOAM_SRC/turbulenceModels/incompressible/RAS/kEpsilon
5/8/2014 25
OpenFOAM
classes
Classes can be compiled and then we can use these
compiled classes in our program just by including
heather (.H) files of the classes
#include heather file of a class"
See heather files included reactingFoam.C in:
/opt/openfoam201/solvers/combustion/reactingFoam
5/8/2014 26
OpenFOAM
Member functions
Functions in a class must be declared and defined.
Declarations in OpenFOAM are always in .H files.
See kEpsilon.H
Definitions are always in .C files.
See kEpsilon.C
5/8/2014 27
OpenFOAM
Member functions
The member functions may be dened either in the
denition of the class, or in the declaration of the class.
templateinline
Foam::PtrList&Foam::ODEChemistryModel::RR(){ return RR_;}
In
$FOAM_SRC/thermophysicalModels/chemistryModel/chemistryModel/ODE
ChemistryModel/ODEChemistryModelI.H
5/8/2014 28
OpenFOAM
we can refer within the body of the program to any of
the public members of the object as if they were normal
functions or normal variables, just by putting the object's
name followed by a dot (.)
See YEqn.H in:
/opt/openfoam201/solvers/combustion/reactingFoam
chemistry.RR(i)---> RR is a function of psiChemistryModel class
chemistry is an object of the psiChemistryModel class
5/8/2014 29
OpenFOAM
constructors
Objects generally need to initialize variables or assign dynamic
memory during their process of creation to become operative.
a class can include a special function called constructor, which is
automatically called whenever a new object of this class is
created.
This constructor function must have the same name as the class,
and cannot have any return type; not even void.
5/8/2014 30
OpenFOAM
constructors
As said before functions of a class are declared in .H
files and defined in .C files. This is the same with
constructors.
See kEpsilon.H and .C in:
/opt/openfoam201/src/turbulenceModels/incompressib
le/RAS/kEpsilon/kEpsilon.C
5/8/2014 31
OpenFOAM
constructors
kEpsilon
(
const volVectorField& U,
const surfaceScalarField& phi,
transportModel& transport,
const word& turbulenceModelName = turbulenceModel::typeName,
const word& modelName = typeName
);
5/8/2014 32
OpenFOAM
kEpsilon.H
constructors
ODEChemistryModel
(
const fvMesh& mesh,
const word& ODEModelName,
const word& thermoTypeName
);
5/8/2014 33
OpenFOAM
ODEChemistryModel.H
constructorskEpsilon::kEpsilon
(
const volVectorField& U,
const surfaceScalarField& phi,
transportModel& transport,
const word& turbulenceModelName,
const word& modelName
)
:
RASModel(modelName, U, phi, transport, turbulenceModelName),
Cmu_
( dimensioned::lookupOrAddToDict
(
"Cmu",
coeffDict_, 0.09
)
),
5/8/2014 34
OpenFOAM
kEpsilon.C
constructors
At the end of kEpsilon constructor in the kEpsilon.C file
you can see:
{
bound(k_, kMin_);
bound(epsilon_, epsilonMin_);
nut_ = Cmu_*sqr(k_)/epsilon_;
nut_.correctBoundaryConditions();
printCoeffs();
}
5/8/2014 35
OpenFOAM
templates
Function templates are special functions that can
operate with generic types. This allows us to create a
function template whose functionality can be adapted
to more than one type or class without repeating the
entire code for each type.
In C++ this can be achieved using template parameters.
5/8/2014 36
OpenFOAM
templates
The format for declaring function templates with type
parameters is:
template function_declaration;
template function_declaration;
5/8/2014 37
OpenFOAM
templates
For example, to create a template function that
devides two objects we could use:
template
myType Devide (myType a, myType b)
{
return (a/b);
}
5/8/2014 38
OpenFOAM
templates
To use this function template we use the following
format for the function call:
function_name (parameters);
For example, to call GetMax to compare two integer
values of type int we can write:
int x,y;
GetMax (x,y);
5/8/2014 39
OpenFOAM
templates
We also have the possibility to write class templates, so that a class
can have members that use template parameters as types.
5/8/2014 40
OpenFOAM
template class mypair {
T values [2];public:mypair (T first, T second){values[0]=first;
values[1]=second;}
};
constructor
mypair myobject (115, 36);
Create Objects:
mypair myfloats (3.0, 2.18);
Object name
templates
See createFields.H in:
/opt/openfoam201/applications/solvers/combustion/re
actingFoam
5/8/2014 41
OpenFOAM
autoPtr
pChemistry
(
psiChemistryModel::New(mesh)
);
Class name Type
Object name
Constructor input
See ODEChemistryModel.H and .C at home.
5/8/2014 42
OpenFOAM
template
class ODEChemistryModel
templateFoam::ODEChemistryModel::ODEChemistryModel( const fvMesh& mesh,
const word& ODEModelName, const word&
thermoTypeName)
inheritance
A key feature of C++ classes is inheritance. Inheritance
allows to create classes which are derived from other
classes, so that they automatically include some of its
"parent's" members, plus its own.
5/8/2014 43
OpenFOAM
inheritance
For example, we are going to suppose that we want to
declare a series of classes that describe polygons like
our CRectangle, or like CTriangle. They have certain
common properties, such as both can be described by
means of only two sides: height and base.
5/8/2014 44
OpenFOAM
inheritance
This could be represented in the world of classes with a
class CPolygon from which we would derive the two
other ones: CRectangle and CTriangle.
5/8/2014 45
OpenFOAM
inheritance
The class CPolygon would contain members that are
common for both types of polygon. In our case: width
and height. And CRectangle and CTriangle would be its
derived classes, with specific features that are different
from one type of polygon to the other, e.g. area.
5/8/2014 46
OpenFOAM
inheritance
In order to derive a class from another, we use a colon
(:) in the declaration of the derived class using the
following format:
class derived_class_name: public base_class_name
{ /*...*/ };
5/8/2014 47
OpenFOAM
inheritance
5/8/2014 48
OpenFOAM
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area ()
{ return (width * height / 2); }
};
protected members are
accessible from members of
their same class and from their
friends, but also from members
of their derived classes.
In OpenFOAM there are too many classes that are
derived from base (parent) classes.
For example there is RASModel class that is the base
class of all RANS models. All RANS models derived from
this parent class and inherit all the accessible members
of the base class.
5/8/2014 49
OpenFOAM
inheritance
See kEpsilon.H
class kEpsilon: public RASModel {.}
See its parents RASModel clas in:
/opt/openfoam201/src/turbilenceModels/incompressibl
e/RAS/RASModel/RASModel.H and RASModel.C
E. g. kEpsilon class inherits turbulence_ from its parent.
5/8/2014 50
OpenFOAM
parentderived
Virtual functions
Virtual member functions are used for dynamic binding,
i.e. the function will work differently depending on how
it is called, and it is determined at run-time.
The reserved word virtual is used in front of the member
function declaration to declare it as virtual.
5/8/2014 51
OpenFOAM
Virtual functions
This is used widely in derived classes in OpenFOAM.
We have a function say divDevReff that is declared
virtually in the parent class turbulrnceModel. Then it
defines virtually and differently in its derived classes (All
RANS models and all LES models).
5/8/2014 52
OpenFOAM
Virtual functions
See turbulenceModel.H in:
opt/src/turbulenceModels/incompressible/turbulenceModel
virtual tmp divDevReff(volVectorField& U)
const = 0;
And see also kEpsilon.H and .C
5/8/2014 53
OpenFOAM
tmp
kEpsilon::divDevReff(volVectorField& U)
const{ return ( - fvm::laplacian(nuEff(), U) -
fvc::div(nuEff()*dev(T(fvc::grad(U)))) );}
Making new libraries
You can simply make your own classes and libraries and
use them in your code.
Copy and paste LES folder in:
/opt/openfoam201/src/turbulenceModels/compressible
Rename it to modifiedLES
Go to make folder in modifiedLES folder
5/8/2014 54
OpenFOAM
Making new libraries
Make below change:
LIB = $(FOAM_LIBBIN)/libcompressibleLESModels --->
LIB = $(FOAM_LIBBIN)/libModifiedcompressibleLESModels
Now you can make any modification in your LES models
Then delete lnInclude folder
Run wmake libso in terminal
5/8/2014 55
OpenFOAM
Making new libraries
Go to XiFOAM folder
Copy it and rename it to myXiFOAM
Go to make folder and open options file
Make below change:
-lcompressibleLESModels \
To
-lModifiedcompressibleLESModels \
5/8/2014 56
OpenFOAM
MAOFnepO
friends
In principle, private and protected members of a class
cannot be accessed from outside the same class in
which they are declared. However, this rule does not
affect friends.
A friend is a function (not a member function) or class
that has access to the private members of a particular
class.
5/8/2014 58
OpenFOAM