Library and Class Organization in Openfoam: Håkan Nilsson 1
Library and Class Organization in Openfoam: Håkan Nilsson 1
Learning outcomes
• You will be able to separate classes into separate directories and files, as it is done in Open-
FOAM.
• You will be able to read and understand most features of OpenFOAM classes.
• You will be able to figure out how OpenFOAM classes are related.
Here we have added #ifndef/#define/#endif to make sure that the class is not declared
multiple times, in the case that we #include "myClass.H" several times.
int main()
{
myClass myClassObject;
cout<< "myClassObject.i_: " << myClassObject.i_ << endl;
cout<< "myClassObject.j_: " << myClassObject.j_ << endl;
myClass myClassObject2;
cout<< "myClassObject2.i_: " << myClassObject2.i_ << endl;
myClassObject2.i_=30;
cout<< "myClassObject.i_: " << myClassObject.i_ << endl;
cout<< "myClassObject2.i_: " << myClassObject2.i_ << endl;
return 0;
}
The top-level solver needs to know about the class declaration (only), which is why we have
added #include "myClass.H". All pieces of code that use a class need to include the decla-
ration of that class!
Compile with wmake, and realize that there are three gcc lines in the output. The first is for
myClass.C, generating the object file myClass.o. The second is for myClassApp.C, generat-
ing myClassApp.o. The third is for the linking, including the class compiled right now and
any dynamic linking.
Make sure that the code still runs and gives the same output as before!
The Make/options file of the application just has to be updated according to the location of
the library header file.
A warning
It is quite common to make backups of files when developing code. However, a warning is issued
when developing OpenFOAM libraries:
All the files in the directory structure of a library will be linked to in the lnInclude directory,
so if you put backup files in a backup directory in the directory structure of that library they
will also end up linked to in lnInclude. It may be ok if they are not used, but it may also be
dangerous if you use the same file names as the original files. The linking in lnInclude will
only be done to one of the files with a particular name (the first or the last it finds, depending
on how the linking is set up). If an active header file is changed during implementation it may
mean that the old back-up header file is included in all files that depend on it. That may lead
to mysterios problems.
If you have to make backups it is better to make tar archives of the backup directories. Then
the original files are hidden in the archives. Just remember to remove the directory that you
put in the tar archive, so that the files are not linked to!