Test a self-build function in OpenFOAM

In OpenFOAM, user defined function is easy to build

In the previous study, I found that OpenFOAM has a function to build a new function object using the following command:

1
foamNewFunctionObject boyaoClass

Here, a function directory called boyaoClass is built. After the compilation of the function, this directory becomes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
├── boyaoClass.C
├── boyaoClass.H
├── lnInclude
│   ├── boyaoClass.C -> ../boyaoClass.C
│   └── boyaoClass.H -> ../boyaoClass.H
└── Make
├── files
├── linux64GccDPInt32Opt
│   ├── boyaoClass.C.dep
│   ├── boyaoClass.o
│   ├── options
│   ├── sourceFiles
│   └── variables
└── options

In this study, I will focus on some details of how we implement the code into OpenFOAM solver that we are using. There are two ways to use this function. The first way is to include this function as a header file in the solver you are going to build. Or you can also use this function by define something in the system/controldict file. The first method is straightforward. The only thing which needs to be mentioned is how to build your make library.

Firstly, we look into the make file defined in boyaoClass. In the options file:

1
2
3
4
5
6
7
EXE_INC = \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude

LIB_LIBS = \
-lfiniteVolume \
-lmeshTools

In the files file:

1
2
3
boyaoClass.C

LIB = $(FOAM_USER_LIBBIN)/libboyaoClassFunctionObject

Here, the FOAM_USER_LIBBIN, which is the user library is used. In my computer, by using the following command, it will show where this is:

1
echo $FOAM_USER_LIBBIN

And it shows as:

1
/home/boyao/OpenFOAM/boyao-7/platforms/linux64GccDPInt32Opt/lib

So, when we want to include this function, we should include this library folder. Now, let’s try to implement this function. In a already built solver, we modify the make library to include the function:

1
2
3
4
5
6
7
8
9
10
11
12
EXE_INC = \
-DFULLDEBUG -g -O0 \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-I$(LIB_SRC)/meshTools/lnInclude\
-I../boyaoClass/lnInclude

EXE_LIBS = \
-L$(FOAM_USER_LIBBIN) \
-lfiniteVolume \
-lfvOptions \
-lmeshTools \
-lboyaoClassFunctionObject

In both the EXE_INC and the EXE_LIBS we have to include the new function. Then, include this boyaoClass.H header file. after this, this function class can be used in this solver.

Another way to implement a function is to change the controllDict file. In the controllDict file, add the following things:

1
2
3
4
5
6
7
8
9
10
11
functions
{
fieldAverage1
{
type boyaoClass;
libs ("libboyaoClassFunctionObject.so");
wordData someWord;
scalarData 1.0;
labelData 1;
}
}

This will also call the function boyaoClass we build.

One last thing about the use of the runTimeSelection used in the function class. In the boyaoClass.C:

1
2
3
4
5
6
7
8
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(boyaoClass, 0);
addToRunTimeSelectionTable(functionObject, boyaoClass, dictionary);
}
}

The addToRunTimeSelectionTable add the boyaoClass to the table functionObject::dictionaryConstructorTablePtr_. By using the following code, we can check what are other members in this table:

1
2
3
4
for (auto it = functionObject::dictionaryConstructorTablePtr_->begin() ;
it != functionObject::dictionaryConstructorTablePtr_->end(); it++) {
Info << it.key() << nl;
}

This method can be used what ever the table you are going to check.