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

Follow the code when runTimeSelection

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 20, 2019, 11:26
Default Follow the code when runTimeSelection
  #1
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 10
frobaux is on a distinguished road
Hi everyone,
I am currently trying to understand the code (precisely the overset library) and follow it up to its roots.



I'm stuck on a runTimeSelection that, even if I read this page, I do not completely get.
The code in cellCellStencil.C (function ::New)
Code:
auto cstrIter = meshConstructorTablePtr_->cfind(stencilType);
where stencilType='cellVolumeWeight'

I didnt completly understand what meshConstructor table is but i think this there is a link somewhere to the derived class of cellCellStencil.C.



Then

Code:
return autoPtr<cellCellStencil>(cstrIter()(mesh, dict, true))
And this is where I really don't get it. From what I can guess, it returns an autoPtr, pointing to an instance of 'cellVolumeWeight', am I right?


What constructor of this class does he use? Is there a way to print stuffs.


When I do

Code:
Info << cstrIter()
I get "1" ...? (->type() of .type() are does not compile)


Thanks for your clues! =)


Have a good day
frobaux is offline   Reply With Quote

Old   May 20, 2019, 12:56
Default
  #2
Member
 
Join Date: Dec 2018
Location: Darmstadt, Germany
Posts: 87
Rep Power: 8
raumpolizei is on a distinguished road
The function New is basically a base class factory function returning a pointer to an object of the base class, therefore enabling dynamic polymorphism at runtime (so that you can use different type of "baseclass" at runtime without recompiling the code). Most of it is hidden to the user and managed via macros. In OpenFOAM in a class enabling runtime polymorphism, you will find macros as declareRunTimeSelectionTable and defineRunTimeSelectionTable. If you take a look at the definitions of these macros, for instance in OF1812 https://www.openfoam.com/documentati...ce.html#l00273 you will find out that the meshConstructorTable you are referring to is of type HashTable:
Code:
     typedef HashTable<argNames##ConstructorPtr, word, string::hash>            \
          argNames##ConstructorTable; // here argNames is "mesh"
The line calling find is returning a function pointer pointing to the constructor in the table refering to the specific input (or key, here stencilType). The constructors are added to the constructorTable at compile time via other macros, like addToRunTimeSelectionTable (these are the classes that are runtime selectable). The Typename("...") line that you are seeing in almost every class definition is basically defining the key to access the constructor of that specific class in the constructorTable of the base class. Anyway, if there is no such key in the hashMap an error is thrown. Otherwise, the constructor gets called and the resulting instantiated object is passed to the autoPtr which is being returned from that function. This is what
Code:
return autoPtr<cellCellStencil>(cstrIter()(mesh, dict, true)); //calls constructor, pass mesh dict and true as arguments.
does. I highly recommend you the article series by Tomislav Maric on Sourceflux dedicated to the mechanism of runtime selection (http://sourceflux.de/blog/series/rts-2). It helped me a lot understanding the implementation in OF. As prerequisite, I would read a little bit about runtime or dynamic polymorphism in order to better grasp the concepts.
raumpolizei is offline   Reply With Quote

Old   May 21, 2019, 04:47
Default
  #3
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 10
frobaux is on a distinguished road
Hello,



Thanks for your quick response, it seems more clear now.



If I want to print something to understand, does cstrIter() have an attribute that I can send to Info to help me?



I going to read that article!
frobaux is offline   Reply With Quote

Old   May 21, 2019, 05:34
Default
  #4
Member
 
Join Date: Dec 2018
Location: Darmstadt, Germany
Posts: 87
Rep Power: 8
raumpolizei is on a distinguished road
Well, what you could do is:
Code:
Info << "Selecting cellCellStencil Type " << stencilType << nl;
after stencilType has been initialized. You could also modify the constructor being called (here the cellVolumeWeight constructor). These are just quick modifications to better understand what the code is doing. I would not start to do that everywhere in the code as at some point, you may break it. A better way would be to create a separate library and to create a new class myCellVolumeWeight and do some testing with that.

Good luck!
raumpolizei is offline   Reply With Quote

Old   May 21, 2019, 05:51
Default
  #5
Member
 
Fabien Robaux
Join Date: Oct 2016
Posts: 51
Rep Power: 10
frobaux is on a distinguished road
Ok,
For the new library that's exactly what I've done, I have created my own library copied from the original to do that kind of small tests.

For the Info on stencilType I have already done this. I also test the type of AutoPtr created.



Ps. The ressource you linked me is really insteresting!
Thanks again!
frobaux is offline   Reply With Quote

Reply

Tags
iterator, runtimeselection


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
The FOAM Documentation Project - SHUT-DOWN holger_marschall OpenFOAM 242 March 7, 2013 13:30
How to make code run in parallel? cwang5 OpenFOAM Programming & Development 1 May 30, 2011 05:47
Open Source Vs Commercial Software MechE OpenFOAM 28 May 16, 2011 12:02
Error in CFX Solver Leuchte CFX 5 November 6, 2010 07:12
Small 3-D code Zdravko Stojanovic Main CFD Forum 2 July 19, 2010 11:11


All times are GMT -4. The time now is 11:51.