Create user define applications in OpenFOAM
OpenFOAM consists of many solvers and utilities, which could also be called applications.
A simple example for a solver is the simpleFoam application and an example for the utilities is the checkMesh application.
In the following the aspects of
- the location,
- the structure,
- the config files,
- the script and
- the compilation
are going to be handled.
1. The location
While solvers and utilities are located inside the source code of OpenFOAM, there is a specific place for user-defined applications.
In the run directory of the corresponding OpenFOAM version step back one directory and create a folder.
mkdir applications
One can have quick access to the run directory of OpenFOAM by typing
cd $FOAM_RUN
Remember to having OpenFOAM sourced - mostly needed when multiple versions are available on a system.
2. The structure
Inside the applications folder, create a new folder for a new application e.g. myApp.
mkdir myApp
Now create a new C file
touch myApp.C
and a Make directory.
mkdir Make
Inside this directory, two more files are needed.
touch files
touch options
The overall structure of a minimal application should then look like this:
run/applications
├── Make
│ ├── files
│ └── options
├── myApp.C
3. The config files
As mentioned above one needs to create some config files to allow OpenFOAM to interact with the program. These files are the files and options files.
3.1 Options
The options files are used to locate header files that are needed for the program and which are not located inside the directory of the application. The syntax will be like
EXE_INC = \\
-I<directoryPath1> \\
-I<directoryPath2> \\
3.2 Files
The files files are used to tell the compiler which source file needs to be compiled. For this short example, these could look like this
myApp.C
EXE = $(FOAM_USER_APPBIN)/myApp
where $(FOAM_USER_APPBIN)
is a shortcut for the application directory for user-defined applications of the corresponding OpenFOAM version.
4. The script
The following script is a minimal example to interact with OpenFOAM and will simply give back a short message.
#include "IOstreams.H"
#include "OFstream.H"
using namespace Foam;
int main()
{
Info<< "Foam is wishing you well!" << endl;
return 0;
}
The script has three main sections. The first one can be called the import area where necessary packages can be loaded. This is done by the syntax
#include packageName
The second area is the definition area where the used variables can be defined, which could be later called in the main program. For example
int numberVariable = 5;
float floatVariable = 2.01;
string stringVariable = "Foam saying hi!";
The third area is the main program. The main program must be inside the main function, which always needs to return a zero. Also, the main loop needs the required arguments.
into main (){
Info << stringVariable << std::endl;
}
5. The compilation
The compilation process in OpenFOAM is handled by wmake, which is the inhouse-compiler from OpenFOAM. It is based on cmake.
To compile the mentioned application go to the main directory of the application and call
cd $FOAM_RUN/../applications/myApp/
wmake myApp.C
After changes are made, it is recommended to first clean before recompiling.
wclean
wmake
Conclusion
After this tutorial one should be able to understand the way of implementing a user-defined application in OpenFOAM. Also, the program should return the message:“Foam is wishing you well!”, to the screen while executing
sayHi
in the Terminal. Have in mind that OpenFOAM needs to be sourced already.
Detailed information on developing an application is not covered in this tutorial.
Further information on this topic can be found on the following links:
- Stefan Radl - OpenFOAM Programming
- OpenFOAM: Three weeks series (Day 11/12/13/14/15)
- OpenFOAM User Guide: Applications and libraries
System information
OpenFOAM | Programming | Hardware |
---|---|---|
Version 3.0 | C | Acer 5742G |
Sources
Links | Latest Access |
---|---|
Stefan Radl: OpenFOAM Programming | 15.10.2021 |
OpenFOAM: Three weeks series | 15.10.2021 |
OpenFOAM User Guide: Applications and libraries | 15.10.2021 |