CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   How to read dictionary variables from another dictionary (https://www.cfd-online.com/Forums/openfoam/219596-how-read-dictionary-variables-another-dictionary.html)

anon_q August 1, 2019 09:18

How to read dictionary variables from another dictionary
 
Hello,
In OpenFOAM, we can create variables in dictionary files. For example, in 0/U (The U file in 0 folder)I can define a variable xvelocity like this:


Code:

...
// I define the variable xvelocity and assign it the value 15
xvelocity      15;
....
// In the same dictionary I can access the variable using $ sign.

value      ($xvelocity 0 0);

My question: Is it possible to access variables (in this example: xvelocity) from other files, say: 0/p or any other dictionary file in the OpenFOAM case?

adhiraj August 1, 2019 12:01

I don't know for sure, but it is possible that variables defined in a dictionary such as xvelocity in your example, are local to the dictionary they are defined in.

One problem with having such variables available to other files would be this: can you guarantee that the dictionary containing the variable is read before the one that needs to use it?

anon_q August 1, 2019 17:05

Quote:

Originally Posted by adhiraj (Post 740767)
I don't know for sure, but it is possible that variables defined in a dictionary such as xvelocity in your example, are local to the dictionary they are defined in.

One problem with having such variables available to other files would be this: can you guarantee that the dictionary containing the variable is read before the one that needs to use it?

Thank you for your reply :)

Currently I am looking for some tricks but I am not sure if that will work. I want to use #codeStream or codedFixedValue to access inputs from other dictionaries. I will post any relevant progressing steps here.

dscian August 1, 2019 17:47

It is possible. You need to include the file. In your 0/U file, add the following

Code:

#include  "path-to-file/file"
You can also check $FOAM_TUTORIALS/incompressible/simpleFoam/turbineSitting tutorial for the implementation.

anon_q August 1, 2019 18:00

Quote:

Originally Posted by dscian (Post 740797)
It is possible. You need to include the file. In your 0/U file, add the following

Code:

#include  "path-to-file/file"
You can also check $FOAM_TUTORIALS/incompressible/simpleFoam/turbineSitting tutorial for the implementation.

That will include the entire file which is not safe (which will pollute the current dictionary and may overwrite some variables ...etc). I am looking for a way to read only some specific entries.

SvenBent August 2, 2019 08:28

Hi.

You could just define all the variables you need in a separate file and include that into the files where you need them.
Say that you make a file:
0/variables/myvars
where you define the variables:
Code:

xVelocity 15;
banana 4;

And then in 0/U and 0/p you include it as dscian suggested.
That way you do not risk the chance of overwriting dictionaries.

Best regards
Mathias

anon_q August 2, 2019 08:35

Quote:

Originally Posted by SvenBent (Post 740884)
Hi.

You could just define all the variables you need in a separate file and include that into the files where you need them.
Say that you make a file:
0/variables/myvars
where you define the variables:
Code:

xVelocity 15;
banana 4;

And then in 0/U and 0/p you include it as dscian suggested.
That way you do not risk the chance of overwriting dictionaries.

Best regards
Mathias

Thank you for your reply. Actually, I am aware of this. But I don't really like to create a separate file just to share variables between dictionaries, in my case, this approach is very error-prone. So I am looking for a way to read variables from other dictionaries directly without using intermediate files.

anon_q August 2, 2019 19:05

I just figured it out, using #codeStream and using this function


Code:

#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>

std::string exec(const char* cmd) {
    char buffer[128];
    std::string result = "";
    FILE* pipe = popen(cmd, "r");
    if (!pipe) throw std::runtime_error("popen() failed!");
    try {
        while (fgets(buffer, sizeof buffer, pipe) != NULL) {
            result += buffer;
        }
    } catch (...) {
        pclose(pipe);
        throw;
    }
    pclose(pipe);
    return result;
}

This function will execute a shell command and return its value.

Now, we can combine this with the famous utilty foamDictionary to read dictionary entries and modifying them;

Here is an example of usage:

Code:

// This will change the value of the entry myVar in 0/p file
word mycmd = exec("foamDictionary 0/p -entry myVar -set 85580");

// and this will read its value and return it
word myvar = exec("foamDictionary 0/p -entry myVar -value");


krikre December 2, 2020 11:11

Quote:

Originally Posted by anon_q (Post 740798)
That will include the entire file which is not safe (which will pollute the current dictionary and may overwrite some variables ...etc). I am looking for a way to read only some specific entries.


The scope can be protected. This worked for me:


Code:



thermPropDict
{
#include "$FOAM_CASE/constant/thermophysicalProperties.poly"
}

etaMax $thermPropDict.mixture.transport.etaMax;

viscLimEl #calc "$etaMax*0.9";//Pas

It didn't work without the intermediate variable $etaMax. Seems $a.b wont work within #calc.



More tricks:

https://cfd.direct/openfoam/user-gui...c-file-format/


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