CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   objectRegistry::lookupObject<scalar> (https://www.cfd-online.com/Forums/openfoam-programming-development/88823-objectregistry-lookupobject-scalar.html)

cfd.with.openfoam January 18, 2014 22:45

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

marupio January 19, 2014 04:31

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.

cfd.with.openfoam January 19, 2014 11:23

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 January 19, 2014 12:31

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 January 25, 2014 22:50

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.

kcn June 30, 2015 03:53

Quote:

Originally Posted by marupio (Post 309556)
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);

Elliptic CFD June 26, 2017 10:36

Quote:

Originally Posted by kcn (Post 552655)
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

alimea November 19, 2017 09:21

Quote:

Originally Posted by alberto (Post 309443)
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

uosilos March 8, 2022 11:28

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)


olesen March 8, 2022 14:53

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.


All times are GMT -4. The time now is 01:37.