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

How to access keywords from dictionaries

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By tomislav_maric

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 31, 2012, 06:10
Default How to access keywords from dictionaries
  #1
Member
 
Francesco Capuano
Join Date: May 2010
Posts: 81
Rep Power: 15
francesco_capuano is on a distinguished road
Hi all,

I'm writing a custom application in OF and, just after the typical "include" list of files, I would like to access a keyword from a dictionary, e.g. the divergence scheme for div(phi,U). So I write

const dictionary& fvSchemes = db().lookupObject("fvSchemes")

so that, afterwards, I could call fvSchemes.lookup("div(phi,U)")

However, the compiler complains: db not declared in this scope

How can I define the database object?

Thanks in advance.
francesco_capuano is offline   Reply With Quote

Old   January 31, 2012, 11:37
Default
  #2
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21
tomislav_maric is on a distinguished road
Quote:
Originally Posted by francesco_capuano View Post
Hi all,

I'm writing a custom application in OF and, just after the typical "include" list of files, I would like to access a keyword from a dictionary, e.g. the divergence scheme for div(phi,U). So I write

const dictionary& fvSchemes = db().lookupObject("fvSchemes")

so that, afterwards, I could call fvSchemes.lookup("div(phi,U)")

However, the compiler complains: db not declared in this scope

How can I define the database object?

Thanks in advance.
fvMesh is (by inheritance) kind of an objectRegistry, you can look it up in Doxygen html source guide:

Code:
const objectRegistry& db = mesh.thisDb();
The mesh usually lives as long as the simulation, so this is why the fields, dicts, etc are bound to it, why it is a kind of a registry itself.

And be careful, inside the src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C, you'll see that the lookupObject is a templated function, so, provide the proper template argument (your code above won't work even after you get a ref to db()):


Code:
const IOdictionary& fvSchemes = db.lookupObject<IOdictionary>("fvSchemes");
And because fvMesh is objectRegistry, you can just use the methods directly, no need to pick up the reference...

T.
elisabet and atulkjoy like this.

Last edited by tomislav_maric; January 31, 2012 at 12:03.
tomislav_maric is offline   Reply With Quote

Old   January 31, 2012, 15:34
Default
  #3
Member
 
Francesco Capuano
Join Date: May 2010
Posts: 81
Rep Power: 15
francesco_capuano is on a distinguished road
Dear Tomislav,

thanks a lot for your clear and accurate reply. Actually there was another mistake in my code: one should use the subDict function of the dictionary class to extract keywords from the divSchemes sub-dictionary. So, for those interested, the final working code is:

const objectRegistry& db = mesh.thisDb();
const IOdictionary& fvSchemes = db.lookupObject<IOdictionary>("fvSchemes");
dictionary divSchemes = fvSchemes.subDict("divSchemes");
word scheme(divSchemes.lookup("div(phi,U"));

I have one more question: with this method, if the div(phi,U) value is, for instance, "Gauss linear", only "Gauss" is caught. How can you catch the entire entry, even if it consists of more than one word?
francesco_capuano is offline   Reply With Quote

Old   October 25, 2014, 17:05
Default
  #4
Senior Member
 
Yuehan
Join Date: Nov 2012
Posts: 142
Rep Power: 13
wc34071209 is on a distinguished road
Quote:
Originally Posted by francesco_capuano View Post
Dear Tomislav,

thanks a lot for your clear and accurate reply. Actually there was another mistake in my code: one should use the subDict function of the dictionary class to extract keywords from the divSchemes sub-dictionary. So, for those interested, the final working code is:

const objectRegistry& db = mesh.thisDb();
const IOdictionary& fvSchemes = db.lookupObject<IOdictionary>("fvSchemes");
dictionary divSchemes = fvSchemes.subDict("divSchemes");
word scheme(divSchemes.lookup("div(phi,U"));

I have one more question: with this method, if the div(phi,U) value is, for instance, "Gauss linear", only "Gauss" is caught. How can you catch the entire entry, even if it consists of more than one word?
The 'word' class holds a string which should not contain any space and therefore in this way you can not get the whole entry. You can do that by reading word by word. There should be some smarter way of doing this and I hope someone could drop a hint.
wc34071209 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Online libraries - with access to Journals momentum_waves Main CFD Forum 2 December 12, 2007 10:08
UDF Data Access Macro Woo Meng Wai FLUENT 0 November 6, 2007 20:23
Error: access: unbound variable,HELP sudhakar FLUENT 0 January 15, 2007 23:21
Access Density Allan Walsh FLUENT 3 October 4, 2005 07:55
access to variables at interface of porous media? Mazyar FLUENT 0 October 10, 2003 14:59


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