|
[Sponsors] |
Run time Selection Mechanism - Some help required to understand |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
March 23, 2009, 14:39 |
Run time Selection Mechanism - Some help required to understand
|
#1 |
Senior Member
Join Date: Mar 2009
Posts: 248
Rep Power: 18 |
Dear Forum Users
Good Evening to all Run Time Selection Mechanism is one of the wonderful features of OpenFOAM. Understanding how it works is a bit involved. I have tried to look into its implementation but need some help from experts. I hope I will be able to get some answers. Lets start :-) Certain Macros constitute the core of this implementation, namely:
Code:
// Declare run-time constructor selection tables declareRunTimeSelectionTable ( autoPtr, polyPatch, word, ( const word& name, const label size, const label start, const label index, const polyBoundaryMesh& bm ), (name, size, start, index, bm) ); declareRunTimeSelectionTable ( autoPtr, polyPatch, dictionary, ( const word& name, const dictionary& dict, const label index, const polyBoundaryMesh& bm ), (name, dict, index, bm) ); Code:
defineRunTimeSelectionTable(polyPatch, word); defineRunTimeSelectionTable(polyPatch, dictionary); addToRunTimeSelectionTable(polyPatch, polyPatch, word); addToRunTimeSelectionTable(polyPatch, polyPatch, dictionary); macro - declareRunTimeSelectionTable: The first macro reads (Note: syntax correctness requires forward slashes as macros cannot have line breaks) : Code:
#define declareRunTimeSelectionTable\ (autoPtr,baseType,argNames,argList,parList) /* Construct from argList function pointer type */ typedef autoPtr<baseType> (*argNames##ConstructorPtr)argList; /* Construct from argList function table type */ typedef HashTable<argNames##ConstructorPtr, word, string::hash> argNames##ConstructorTable; /* Construct from argList function pointer table pointer */ static argNames##ConstructorTable* argNames##ConstructorTablePtr_; /* Class to add constructor from argList to table */ template<class baseType##Type> class add##argNames##ConstructorToTable { public: static autoPtr<baseType> New argList { return autoPtr<baseType>(new baseType##Type parList); } add##argNames##ConstructorToTable ( const word& lookup = baseType##Type::typeName ) { construct##argNames##ConstructorTables(); argNames##ConstructorTablePtr_->insert(lookup, New); } ~add##argNames##ConstructorToTable() { destroy##argNames##ConstructorTables(); } }; /* Table Constructor called from the table add function */ static void construct##argNames##ConstructorTables(); /* Table destructor called from the table add function destructor */ static void destroy##argNames##ConstructorTables()
Code:
declareRunTimeSelectionTable ( autoPtr – autoPtr baseclass - polyPatch argNames – word argList - (const word& name, const label size, const label start, const label index, const polyBoundaryMesh& bm), parList - (name, size, start, index,bm) ); // it first defines a typedef for the construction of a function pointer type from argList. Code:
typedef autoPtr<polyPatch> (* wordConstructorPtr) ( const word& name, const label size, const label start, const label index, const polyBoundaryMesh& bm ); Code:
typedef HashTable < wordConstructorPtr, word, string::hash> wordConstructorTable; Code:
static wordConstructorTable* wordConstructorTablePtr_; Code:
template<class polyPatchType> class addwordConstructorToTable { public: // static function static autoPtr<polyPatch> New ( const word& name, const label size, const label start, const label index, const polyBoundaryMesh& bm ) { return autoPtr<polyPatch> (new polyPatch (name, size, start, index, bm)); } // constructor addwordConstructorToTable ( const word& lookup = polyPatchType::typeName ) { constructwordConstructorTables(); wordConstructorTablePtr_->insert(lookup, New); } // destructor ~addwordConstructorToTable() { destroywordConstructorTables(); } }; Code:
static void constructwordConstructorTables(); static void destroywordConstructorTables(); macro - defineRunTimeSelectionTable( ) Macro defineRunTimeSelectionTable(baseType, argNames) already expanded for the class polyPatch reads : Code:
#define defineRunTimeSelectionTable( polyPatch, word ) defineRunTimeSelectionTablePtr(polyPatch, word ); defineRunTimeSelectionTableConstructor(polyPatch, word) defineRunTimeSelectionTableDestructor(polyPatch,word) defineRunTimeSelectionTablePtr(polyPatch, word); Code:
#define defineRunTimeSelectionTablePtr(polyPatch, word) /* Define the constructor function table and initialized to NULL */ polyPatch::wordConstructorTable* polyPatch::wordConstructorTablePtr_ = NULL Code:
#define defineRunTimeSelectionTableConstructor(polyPatch, word) /* Table Constructor called from the table add function void polyPatch :: constructwordConstructorTables() { static bool constructed = false; if (!constructed) { polyPatch::wordConstructorTablePtr_ = new polyPatch::wordConstructorTable; constructed = true; } } Code:
#define defineRunTimeSelectionTableDestructor(polyPatch, word) /* Table destructor called from the table add function destructor */ void polyPatch::destroywordConstructorTables() { if (polyPatch::wordConstructorTablePtr_) { delete polyPatch::wordConstructorTablePtr_; polyPatch::wordConstructorTablePtr_ = NULL; } } macro - addToRunTimeSelectionTable( ) - this macro is pretty simple . it reads Code:
#define addToRunTimeSelectionTable(baseType,thisType,argNames) /* Add the thisType constructor function to the table */ baseType::add##argNames##ConstructorToTable<thisType> add##thisType##argNames##ConstructorTo##baseType##Table_ Code:
polyPatch::addwordConstructorToTable<polyPatch> addpolyPatchwordConstructorTopolyPatchTable_ Now the questions :-)) 1) How / where is the constructor addwordConstructorToTable is called to construct the table . If the code Code:
polyPatch::addwordConstructorToTable<polyPatch> addpolyPatchwordConstructorTopolyPatchTable_ Now If I am wrong in asking the first question and we assume that ctor is indeed called then the static function call - constructwordConstructorTables() will create a new HashTable and return a pointer to it. The next statement Code:
wordConstructorTablePtr_->insert(lookup, New); 2) As I understand from the second code statement in the ctor body, a key is inserted into the HashTable corresponding to the base class. How are the other types of patches entered into this table. 3) Finally what troubles me most is that I am unable to grasp when / where are these tables created during the creational process of fvMesh. Thanks a lot for your attention. Best Regards Jaswi |
|
August 26, 2013, 23:28 |
|
#2 |
Senior Member
Dongyue Li
Join Date: Jun 2012
Location: Beijing, China
Posts: 849
Rep Power: 18 |
Very Neat!! But Really tough tough for a newbie though...
|
|
April 18, 2014, 08:44 |
|
#3 |
Member
xuhe-openfoam
Join Date: Aug 2013
Location: DaLian,china
Posts: 82
Rep Power: 13 |
hi,
maybe macro - addToRunTimeSelectionTable( ) Code:
#define addToRunTimeSelectionTable(baseType,thisType,argNames) /* Add the thisType constructor function to the table */ baseType::add##argNames##ConstructorToTable<thisType> add##thisType##argNames##ConstructorTo##baseType##Table_ Code:
polyPatch::addwordConstructorToTable<thisType> addthisTypewordConstructorTopolyPatchTable_ |
|
October 29, 2015, 14:42 |
|
#4 |
New Member
Bruno Kassar
Join Date: Apr 2014
Location: Rio de Janeiro
Posts: 9
Rep Power: 12 |
Hi all,
While trying to understand runTimeSelection mechanism, I bumped into this thread and found it very interesting. Maybe this link will help future readers: https://openfoamwiki.net/index.php/O...tion_mechanism |
|
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Physical Reason for stability of Implicit Schemes? | radhakrishnan | Main CFD Forum | 26 | October 3, 2023 23:05 |
Differences between serial and parallel runs | carsten | OpenFOAM Bugs | 11 | September 12, 2008 12:16 |
Convergence moving mesh | lr103476 | OpenFOAM Running, Solving & CFD | 30 | November 19, 2007 15:09 |
Total run time | Dominic | Main CFD Forum | 0 | August 25, 2006 00:12 |
local time scale factor selection | mike | CFX | 0 | April 19, 2006 09:52 |