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

objectRegistry::lookupObject<scalar>

Register Blogs Community New Posts Updated Threads Search

Like Tree35Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 18, 2014, 23:45
Default
  #41
Member
 
Fluid Dynamics
Join Date: Mar 2013
Posts: 41
Rep Power: 13
cfd.with.openfoam is on a distinguished road
Quick update -

I think now I can read and write from disk using the IOdictionary method as well. But as you know this is not a good way to go about it.

I will be looking forward to any comments/workarounds on updating a dictionary during runtime from a BC inside the database so that it can be looked up by another BC - on the fly or w/o writing anything to the disk.

Thank you very much
cfd.with.openfoam is offline   Reply With Quote

Old   January 19, 2014, 05:31
Default
  #42
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
If you only need read access to objects created by the other BC, then you can pick them up through the objectRegistry. If you need write access you can cheat with a const cast or have your solver give them access from the top level.

If you want to share non-IOobjects, you can use IOlist or you can lookup the custom BC's themselves and put the data access methods in.
SHUBHAM9595 likes this.
__________________
~~~
Follow me on twitter @DavidGaden
marupio is offline   Reply With Quote

Old   January 19, 2014, 12:23
Default
  #43
Member
 
Fluid Dynamics
Join Date: Mar 2013
Posts: 41
Rep Power: 13
cfd.with.openfoam is on a distinguished road
Hi David,

Thank you for your time. Some comments -

"If you only need read access to objects created by the other BC, then you can pick them up through the objectRegistry."

If I create an IOobject inside the updatecoeffs() member function of my Vel. BC then it is destroyed as soon as I go out and the object can't be looked up inside the updatecoeffs() member function of my Temp. BC. Is there any workaround for this? This is my first coding exercise in OpenFOAM so I don't know too much here.

"If you need write access you can cheat with a const cast or have your solver give them access from the top level"
How to do this David? Can you please explain?

"If you want to share non-IOobjects, you can use IOlist or you can lookup the custom BC's themselves and put the data access methods in. "

This is also beyond my current understanding.

Looking forward to your response.
Thank you
cfd.with.openfoam is offline   Reply With Quote

Old   January 19, 2014, 13:31
Default
  #44
Member
 
Fluid Dynamics
Join Date: Mar 2013
Posts: 41
Rep Power: 13
cfd.with.openfoam is on a distinguished road
Hi again David,

Just wanted to mention that (I think) I have read all the posts in this thread carefully.

Also in my messages I have only used words w/o any code. So here is what I have tried based (completely) on your posts in this thread.

// In the solver i.e createFields.H
Code:
IOdictionary compDict
(
    IOobject
    (
        "compDict",
        runTime.system(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
    )
);
But in my Vel. BC I can't use & to look it up so I use the following which just makes a copy inside the Vel. BC, which gets destroyed as soon as I go out of the members function i.e updateCoeffs
Code:
IOdictionary compDict = db().lookupObject<IOdictionary>
(
   "compDict"
);
Then the variable is set in the Vel BC -
Code:
compDict.set("Zimbo",Zimbo);
Finally in the Temp. BC - again I look up the dictionary
Code:
IOdictionary compDict = db().lookupObject<IOdictionary>
(
   "compDict"
);
Then I try to look up the variable which for obvious reasons just doesn't work
Code:
 ZimboD = readScalar(compDict.lookup("Zimbo"));
You can see that I am just following your suggestions but as you can see that whatever I am doing inside my Vel. BC is not gonna work since I don't have a non-constant reference.

Hopefully it will give you a better picture of where I am.

Thank you for your time.
cfd.with.openfoam is offline   Reply With Quote

Old   January 25, 2014, 23:50
Default
  #45
Member
 
Fluid Dynamics
Join Date: Mar 2013
Posts: 41
Rep Power: 13
cfd.with.openfoam is on a distinguished road
Hi Everybody,

I think I managed to get around my issue by using the following
Code:
IOdictionary& compDict = 
const_cast<IOdictionary&>(db().lookupObject<IOdictionary>("compDict"));
instead of this
Code:
IOdictionary compDict = db().lookupObject<IOdictionary>("compDict"));
in the Vel. BC.

Hope it helps somebody
Thanks for your suggestions David.
Nucleophobe and atulkjoy like this.
cfd.with.openfoam is offline   Reply With Quote

Old   June 30, 2015, 04:53
Default
  #46
kcn
Member
 
Join Date: May 2014
Posts: 31
Rep Power: 12
kcn is on a distinguished road
Quote:
Originally Posted by marupio View Post
May require some changes while debugging, but here's the essence:

Code:
// in createFields.H
IOdictionary scalarDict
(
    IOobject
    (
        "scalarDict",
        runTime.constant(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
    )
);

// In runTime loop:
scalarDict.set("nameOfScalar", valueOfScalar);

// and you're done
I hope that helps!
Hi,

Can you please tell me how to modify this code to update the value of a subdictionary? I did the following but it didn't work.


// in createFields.H IOdictionary transportProperties ( IOobject ( "transportProperties", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::AUTO_WRITE ) );
// Define subdictionary

dictionary updatedProperties
(
transportProperties.subDict("updatedProperties")
);

// In runTime loop: updatedProperties.set("nameOfScalar", valueOfScalar);
kcn is offline   Reply With Quote

Old   June 26, 2017, 11:36
Default
  #47
New Member
 
Ehimen
Join Date: Jun 2016
Posts: 12
Rep Power: 10
Elliptic CFD is on a distinguished road
Quote:
Originally Posted by kcn View Post
Hi,

Can you please tell me how to modify this code to update the value of a subdictionary? I did the following but it didn't work.


// in createFields.H IOdictionary transportProperties ( IOobject ( "transportProperties", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED, IOobject::AUTO_WRITE ) );
// Define subdictionary

dictionary updatedProperties
(
transportProperties.subDict("updatedProperties")
);

// In runTime loop: updatedProperties.set("nameOfScalar", valueOfScalar);
I use the add function rather than set (updatedProperties.add("nameOfScalar", valueOfScalar). The second line should be updatedProperties.regIOobject::write();

This will enable the modification of the dictionary at every timestep.

To read the value added to the dictionary, use: updatedProperties.lookup("nameOfScalar") >> newvalueOfScalar;

Should be good to go. I use OpenFOAM 4
Elliptic CFD is offline   Reply With Quote

Old   November 19, 2017, 10:21
Default
  #48
Senior Member
 
A. Min
Join Date: Mar 2015
Posts: 308
Rep Power: 12
alimea is on a distinguished road
Quote:
Originally Posted by alberto View Post
Hi,

let's consider a general example to search in a dictionary. :-)

Your solver uses the "transportProperties" dictionary, which contains a subdictionary, called "mySubDict". This sub-dictionary contains your dimensioned scalar, named "myScalar".

In the BC, you can recover "myScalar" as follows:

Code:
// Extract the dictionary from the database
const dictionary& transportProperties = db().lookupObject<IOdictionary>
(
   "transportProperties"
);

// Exctract subdictionary from the main dictionary
dictionary mySubDict
(
    transportProperties.subDict("mySubDict")
);

// Extracting scalar value
dimensionedScalar myScalar(mySubDict.lookup("myScalar"));
Of course this works also if you do not have sub-dictionaries, using the "lookup" method directly on the main dictionary.

Note: this value is not updated at runtime. Since it seems you need that, if the value is computed from fields, the easy way is to compute it on the patch looking up for the fields.

Best,

Hi
I'm not a professional in openFaom programing!
Could you please explain about some lines of this code:1
1- what is db() ?
2- what's the meaning of this line: lookupObject<IOdictionary>

thanks
alimea is offline   Reply With Quote

Old   March 8, 2022, 12:28
Default
  #49
Member
 
UOCFD
Join Date: Oct 2020
Posts: 40
Rep Power: 6
uosilos is on a distinguished road
I have a similar problem to yours, so in my BC I have included


Code:
IOdictionary copyOpenFraction = db().lookupObject<IOdictionary>("copyOpenFraction");
which seems to have compiled without problems.

Moreover, I added to the solver's createFields.H


Code:
IOdictionary copyOpenFraction 
(
    IOobject
    (
        "copyOpenFraction",
        runTime.system(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
    ),
    0      
);
However, in my interaction model, which has to read the aforementioned variable from the BC (updated every t-step), it is giving errors:

Code:
IOdictionary& copyOpenFraction = const_cast<IOdictionary&>(db().lookupObject<IOdictionary>("copyOpenFraction"));
Code:
lnInclude/myLocalInteraction.C:221:76: error: there are no arguments to ‘db’ that depend on a template parameter, so a declaration of ‘db’ must be available [-fpermissive]
   221 |                 IOdictionary& copyOpenFraction = const_cast<IOdictionary&>(db().lookupObject<IOdictionary>("copyOpenFraction"));
      |                                                                            ^~
lnInclude/myLocalInteraction.C:221:76: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
uosilos is offline   Reply With Quote

Old   March 8, 2022, 15:53
Default
  #50
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,715
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
I would hazard a guess that your local interaction model (or the method where your error shows) is templated on some parameter. The usual way to resolve your error is with something like this:
Code:
this->db().lookupObject<whateverOtherType>(name)
Other situations might require an interspersed 'template' keyword. You will also find that sort of thing in a few places in OpenFOAM too.
olesen 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 16:20.