CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Advice on understanding namespace and classes in MRF

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 1, 2024, 16:49
Default Advice on understanding namespace and classes in MRF
  #1
New Member
 
Join Date: Jan 2024
Posts: 10
Rep Power: 2
gmax is on a distinguished road
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?
gmax is offline   Reply With Quote

Old   March 10, 2024, 15:44
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
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.
olesen is offline   Reply With Quote

Old   April 18, 2024, 04:44
Default
  #3
New Member
 
Join Date: Jan 2024
Posts: 10
Rep Power: 2
gmax is on a distinguished road
Quote:
Originally Posted by olesen View Post
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.
gmax is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:27.