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 | ├── boyaoClass.C |
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 | EXE_INC = \ |
In the files file:
1 | boyaoClass.C |
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 | EXE_INC = \ |
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 | functions |
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 | namespace Foam |
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 | for (auto it = functionObject::dictionaryConstructorTablePtr_->begin() ; |
This method can be used what ever the table you are going to check.