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

How to access keywords from dictionaries

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   January 31, 2012, 07:10
Default How to access keywords from dictionaries
  #1
Member
 
Francesco Capuano
Join Date: May 2010
Posts: 78
Rep Power: 7
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, 12:37
Default
  #2
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 259
Blog Entries: 5
Rep Power: 11
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.

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

Old   January 31, 2012, 16:34
Default
  #3
Member
 
Francesco Capuano
Join Date: May 2010
Posts: 78
Rep Power: 7
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
Member
 
Cong
Join Date: Nov 2012
Posts: 77
Rep Power: 4
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

Thread Tools
Display Modes

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 On
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 11:08
UDF Data Access Macro Woo Meng Wai FLUENT 0 November 6, 2007 21:23
Error: access: unbound variable,HELP sudhakar FLUENT 0 January 16, 2007 00: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 02:44.