Basics of C++ in Openfoam: Håkan Nilsson 1
Basics of C++ in Openfoam: Håkan Nilsson 1
Learning outcomes
• You will learn the basic syntax in C++
• You will learn how to use classes to implement simple C++ codes, and how member functions
are called in the top-level code.
• You will learn how to implement functions in the top-level code, understand the difference
between declaration and definition, and see how that can be practically used.
• You will learn how OpenFOAM compilation relates to compilation of a simple C++ code.
Note that you will be asked to pack up your final cleaned-up directories and submit them for
assessment of completion.
• After this introduction you should be able to recognize and make minor
modifications to most C++ features in OpenFOAM.
• Some books:
– C++ direkt by Jan Skansholm (ISBN 91-44-01463-5)
int myInteger;
• Variables can be added, substracted, multiplied and divided as long as they have the same
type, or if the types have definitions on how to convert between the types.
• In C++ it is possible to define special types (classes), and there are many types defined for
you in OpenFOAM.
• User-defined types must have the required conversions defined. Some of the types in Open-
FOAM can be used together in arithmetic expressions, but not all of them.
• By associating a declaration with a namespace, the declaration will only be visible if that
namespace is used. The standard declarations are used by starting with:
Foam::function();
where function() is a function defined in namespace Foam. This must be used if any other
namespace containing a declaration of another function() is also visible.
where << and >> are output and input operators, and endl is a manipulator that generates
a new line (there are many other manipulators).
• In OpenFOAM a new output stream Info is however defined, and it is recommended to use
that one instead since it takes care of write-outs for parallel simulations.
int main()
{
return 0;
}
in this case, main takes no arguments, but it may (as in OpenFOAM applications).
• The main function should always return an integer, and default is 0, so for the main function
it is allowed to write only:
main()
{
}
• Logical operators: && || ! (or, for some compilers: and or not). Generates
bool (boolean)
• Standard library cmath contains trigonometric functions, logaritmic functions and square
root. (use #include cmath; if you need them)
• Standard library cstdlib contains general functions, and some of them can be used for
arithmetics. (use #include cstdlib; if you need them)
• for-statements:
• while-statements:
main()
{
float myFloat;
cout << "Please type a float!" << endl;
cin >> myFloat;
cout << "sin(" << myFloat << ") = " << sin(myFloat) << endl;
if (myFloat < 5.5){cout << myFloat << " is less than 5.5" << endl;} else
{cout << myFloat << " is not less than 5.5" << endl;};
for ( int i=0; i<myFloat; i++ ) {cout << "For-looping: " << i << endl;}
int j=0;
while (j<myFloat) {cout << "While-looping: " << j << endl; j++;}
} //Note conversion of myFloat to int in loops!
Compile and run with:
g++ basic2.C -o basic2; ./basic2
• Array template operations: The template classes define member functions that can be used
for those types, for instance: size(), empty(), assign(), push_back(), pop_back(),
front(), clear(), capacity() etc.
v.assign(4, 1.0); gives {1.0, 1.0, 1.0, 1.0}
takes two arguments of type double, and returns type double. The variable nvalues is a
local variable, and is only visible inside the function. Note that any code after the return
statement will not be executed.
• A function doesn’t have to take arguments, and it doesn’t have to return anything (the
output type is then specified as void).
• There may be several functions with the same names, as long as there is a difference in the
arguments to the functions - the number of arguments or the types of the arguments.
• A good way to program C++ is to make files in pairs, one with the declaration, and one
with the definition. This is done throughout OpenFOAM.
The reference parameter x1 will now be a reference to the argument to the function in-
stead of a local variable in the function. (standard arrays are always treated as reference
parameters).
• Reference parameters can also be used to avoid copying of large fields when calling a func-
tion. To avoid changing the parameter in the function it can be declared as const, i.e.
void checkWord(const string& s)
This often applies for parameters of class-type, which can be large.
• Default values can be specified, and then the function may be called without that parameter,
i.e.
void checkWord(const string& s, int nmbr=1)
main()
{
double d1=2.1;
double d2=3.7;
cout << "Modified average: " << average(d1,d2) << endl;
cout << "Half modified average: " << average(d1,d2,4) << endl;
cout << "d1: " << d1 << ", d2: " << d2 << endl;
}
Compile and run with: g++ basic6.C -o basic6; ./basic6
autoPtr<incompressible::turbulenceModel> turbulence
(
incompressible::turbulenceModel::New(U, phi, laminarTransport)
);
In file $FOAM_SOLVERS/incompressible/simpleFoam/simpleFoam.C:
turbulence->correct();
• User defined types can be defined in classes. OpenFOAM provides many types/classes that
are useful for solving partial differential equations.
• OpenFOAM classes are used by including the class declarations in the header of the code,
and linking to the corresponding compiled OpenFOAM library at compilation.
• The path to included files that are in another path than the current directory must be
specified by -I
• The path to libraries that are linked to is specified with -L
Here, -DWM_DP is for double precision floats and -DWM_LABEL_SIZE=32 is for 32 bit int.
We include header files (declarations) from $FOAM_SRC/OpenFOAM/lnInclude
We link to library (definitions) $WM_PROJECT_DIR/lib/$WM_OPTIONS/libOpenFOAM.so