CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Advice on understanding namespace and classes in MRF (https://www.cfd-online.com/Forums/openfoam-programming-development/254804-advice-understanding-namespace-classes-mrf.html)

gmax March 1, 2024 16:49

Advice on understanding namespace and classes in MRF
 
Hi everyone, I've read a lot of useful information on this forum and today I wanted to learn more about c++ in OpenFoam.

I'm trying to introduce variable rotation axes into MRF. I came across a syntax that I often encounter in code but that I didn't completely understand, I report the part of the code that I'm trying to understand below:



Code:

Foam::IOobject Foam::IOMRFZoneList::createIOobject
 
(
 
    const fvMesh& mesh,
 
    const word& solverName
 
) const
 
{
 
    IOobject io
 
    (
 
        "MRFProperties" + solverName,
 
        mesh.time().constant(),
 
        mesh,
 
        IOobject::MUST_READ,
 
        IOobject::NO_WRITE
 
    );
 
 
 
    if (io.typeHeaderOk<IOdictionary>(true))
 
    {
 
        Info<< "Creating MRF zone list from " << io.name() << endl;
 
 
 
        io.readOpt(IOobject::MUST_READ_IF_MODIFIED);
 
    }
 
    else
 
    {
 
        Info<< "No MRF models present" << nl << endl;
 
 
 
        io.readOpt(IOobject::NO_READ);
 
    }
 
 
 
    return io;
 
}

From what I understand, an object of type
Code:

IOobject
is created, from the
Code:

FOAM
namespace, what I don't understand is the second part
Code:

Foam::IOMRFZoneList::createIOobject
. From this writing I would say that you are describing how the createIOobject method should work.
But in reality I don't understand this exactly, why do I create an object and then specify a method of creation for that object. Maybe I'm not understanding well what this line in C++ means and for this reason I ask you if you can describe to me what this type of writing represents.


I tried to replicate this writing by creating code like this:
.H
Code:

#include<iostream>

namespaceSchim{

classBase{
public:
voidmess(){
std::cout<<" classe base"<<std::endl;

      }
voidfunc();
int a;

};

classDeri: publicBase{
public:
voidmess(){
std::cout<<" classe deri"<<std::endl;

      }

};

}

namespaceSchim{
namespacederivato {
classnnsd: publicBase{
public:
voidfunc(int );

voidmess(){
std::cout<<" classe dentro name dentro name"<<std::endl;
      }

  };

}




}

and .C file
Code:

#include"class.H"

void Schim::derivato::nnsd::Base::func(){
    std::cout<<" sono nuovo"<<std::endl;
};

intmain(){

int a;
a =2;
Schim::Base t1;
Schim::derivato::nnsd t2;
//Schim::Base Schim::derivato::nnsd::func(int a);
t1.mess();
t1.func();
t2.mess();
t2.func(2);

return0;
}

My attempt was to create something like:
Code:

Schim::Base Schim::derivato::nnsd::func(){ some stuf}
But this didn't work, what am I doing wrong?

olesen March 10, 2024 15:44

You are right, the code there is pretty weird. If you imagine the IOobject is something like the Java File class as a somewhat lightweight representation of a file + path location, plus some preferences about reading/writing etc.


It starts initially by declaring the io thing as "MUST_READ", but doesn't actually do anything. The next bit of magic occurs with the typeHeaderOk method. There it attempts to open the file location which was described by the IOobject. If it can find the file, it reads the first bit for the "FoamFile ..." header and checks that against the IOdictionary type id. If all that goes well, tag it as "MUST_READ_IF_MODIFIED" for later use. If it fails (file not found, wrong type etc), mark as NO_READ and don't use it again.


For whatever reason, this helper has been declared as method. It should really be a file-static or class static method, but that is just a question of style, not functionality.

gmax April 18, 2024 04:44

Quote:

Originally Posted by olesen (Post 866008)
You are right, the code there is pretty weird. If you imagine the IOobject is something like the Java File class as a somewhat lightweight representation of a file + path location, plus some preferences about reading/writing etc.


It starts initially by declaring the io thing as "MUST_READ", but doesn't actually do anything. The next bit of magic occurs with the typeHeaderOk method. There it attempts to open the file location which was described by the IOobject. If it can find the file, it reads the first bit for the "FoamFile ..." header and checks that against the IOdictionary type id. If all that goes well, tag it as "MUST_READ_IF_MODIFIED" for later use. If it fails (file not found, wrong type etc), mark as NO_READ and don't use it again.


For whatever reason, this helper has been declared as method. It should really be a file-static or class static method, but that is just a question of style, not functionality.


Thanks for the reply, now I understand that it uses this method to determine whether the MRF file exists or not.



One thing that still eludes me is the use of the class:

Code:

Foam::IOobject
inside :
Code:

Foam::IOobject Foam::IOMRFZoneList::createIOobject
.


I'll try to explain it because I might be wrong. From what I see in the .H file the Namespace Foam is expanded
Code:

Foam{ class IOMRFZoneList: .[..]}
, inside I find the class IOMRFZoneList this class has a private object called
Code:

IOobject createIOobject(const fvMesh& mesh) const;
. This is an object that is created as an IOobject, a feature inherited from public IOdictionary in .H file.



If what I described is correct then when I try to build this object
Code:

IOobject createIOobject(const fvMesh& mesh) const;
i need to specify what object it is using
Code:

Foam::IOobject
.



I think I've made a bit of confusion, but if this is the interpretation then I can't replicate it by writing code from scratch.


Thanks for the reply.


All times are GMT -4. The time now is 22:52.