CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Why isn't there a #include in the createField.H header file? (https://www.cfd-online.com/Forums/openfoam/222317-why-isnt-there-include-createfield-h-header-file.html)

granzer November 19, 2019 08:13

Why isn't there a #include in the createField.H header file?
 
Hello, Why isnt there a #include in the createField.H header file of the icoFoam solver?

#
Info<< "Reading transportProperties\n" << endl;

IOdictionary transportProperties
(
IOobject
(
"transportProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

dimensionedScalar nu
(
transportProperties.lookup("nu")
);
#
It's my understanding that here an object named "transportProperties" of class "IOdictionary" is created. But where is the class declaration for IOdictionary?. Shouldn't there be a header file having the declaration for the IOdictionary be included here?


informative:
1) https://www.cfd-online.com/Forums/op...tregistry.html
2) https://stackoverflow.com/questions/...g-code-example
3) Explained: http://openfoamwiki.net/index.php/Ho..._with_OpenFOAM

GerhardHolzinger November 20, 2019 04:12

You shouldn't look at these files individually. The file createFields is part of the solver icoFoam. Take a look at how the C/C++ compilation process works. There is the concept of a compilation unit, and createFields is part of the compilation unit icoFoam. Thus, all the includes that define types, such as IOdictionary, have been included somewhere in icoFoam.

Below, are all the lines of icoFoam up to the point, where createFields gets included.

Code:

#include "fvCFD.H"
#include "pisoControl.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    #include "setRootCaseLists.H"
    #include "createTime.H"
    #include "createMesh.H"

    pisoControl piso(mesh);

    #include "createFields.H"

If you want to know, where icoFoam gets its definition of the type IOdictionary, I suggest to take a look at what fvCFD.H contains.

granzer November 20, 2019 07:24

Quote:

Originally Posted by GerhardHolzinger (Post 750259)
You shouldn't look at these files individually. The file createFields is part of the solver icoFoam. Take a look at how the C/C++ compilation process works. There is the concept of a compilation unit, and createFields is part of the compilation unit icoFoam. Thus, all the includes that define types, such as IOdictionary, have been included somewhere in icoFoam.

Below, are all the lines of icoFoam up to the point, where createFields gets included.

Code:

#include "fvCFD.H"
#include "pisoControl.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    #include "setRootCaseLists.H"
    #include "createTime.H"
    #include "createMesh.H"

    pisoControl piso(mesh);

    #include "createFields.H"

If you want to know, where icoFoam gets its definition of the type IOdictionary, I suggest taking a look at what fvCFD.H contains.

Thank you very much!. I am a beginner to C++ and thought all header had all the 'other' headers that are required in it, with something like ifandef to make sure multiple copies are not included in the compilation unit. Guess IOobject is important enough that it is included in a separate 'for all case' kind of header which is included before everything else like 'fvCFD.H'.

Can you please tell me about the way in which IOdictionary class is used here? there is no object name next to the class name. Instead, am I right in thinking, the object name 'transportProperties' is given as an argument in the IOobject constructor? Why not give it something like:

IOobject transportProperties
(
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)

?
I thought it could be an anonymous class object but it does have object name 'transportProperties'.

GerhardHolzinger November 21, 2019 05:28

Quote:

Originally Posted by granzer (Post 750280)
Guess IOobject is important enough that it is included in a separate 'for all case' kind of header which is included before everything else like 'fvCFD.H'.

This is an aspect that separates a good software design from a bad one.

If you compare multiple solvers, e.g. icoFoam, simpleFoam and pimpleFoam, you will find many similarities between them. Furthermore, compare an incompressible with a compressible solver, you will commonalities and differences.

As you have said, it is best to define/implement things that are often used just once, and reuse this unique implementation.


Quote:

Originally Posted by granzer (Post 750280)
IOdictionary transportProperties
(
IOobject
(
"transportProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

dimensionedScalar nu
(
transportProperties.lookup("nu")
);
#
It's my understanding that here an object named "transportProperties" of class "IOdictionary" is created. But where is the class declaration for IOdictionary?. Shouldn't there be a header file having the declaration for the IOdictionary be included here?

The first statement is correct, an object of the type IOdictionary is created by the name of transport properties. The declaration is in one of the headers. Here there is some digging involved.

Think of the #include statements as a formalized copy&paste, the compiler pastes the content of the header file where the include statement is found. This also works recursively, i.e. a header can include another header. gcc has an option to write the final product of the preprocesser, i.e. the result of all includes



The best way to learn about OpenFOAM is to study its code.


All times are GMT -4. The time now is 15:14.