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

How to read dictionary variables from another dictionary

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

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 1, 2019, 10:18
Default How to read dictionary variables from another dictionary
  #1
Senior Member
 
Evren Linda's Avatar
 
Evren Linda
Join Date: Mar 2018
Posts: 114
Rep Power: 3
Evren Linda is on a distinguished road
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?
Evren Linda is offline   Reply With Quote

Old   August 1, 2019, 13:01
Default
  #2
Senior Member
 
Adhiraj
Join Date: Sep 2010
Location: Maryland, United States
Posts: 125
Rep Power: 11
adhiraj is on a distinguished road
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?
adhiraj is offline   Reply With Quote

Old   August 1, 2019, 18:05
Default
  #3
Senior Member
 
Evren Linda's Avatar
 
Evren Linda
Join Date: Mar 2018
Posts: 114
Rep Power: 3
Evren Linda is on a distinguished road
Quote:
Originally Posted by adhiraj View Post
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.
Evren Linda is offline   Reply With Quote

Old   August 1, 2019, 18:47
Default
  #4
New Member
 
Utkan Caliskan
Join Date: Aug 2014
Posts: 14
Rep Power: 7
dscian is on a distinguished road
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.
dscian is offline   Reply With Quote

Old   August 1, 2019, 19:00
Default
  #5
Senior Member
 
Evren Linda's Avatar
 
Evren Linda
Join Date: Mar 2018
Posts: 114
Rep Power: 3
Evren Linda is on a distinguished road
Quote:
Originally Posted by dscian View Post
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.
Evren Linda is offline   Reply With Quote

Old   August 2, 2019, 09:28
Default
  #6
New Member
 
Mathias Poulsen
Join Date: Feb 2018
Location: Denmark
Posts: 5
Rep Power: 3
SvenBent is on a distinguished road
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
SvenBent is offline   Reply With Quote

Old   August 2, 2019, 09:35
Default
  #7
Senior Member
 
Evren Linda's Avatar
 
Evren Linda
Join Date: Mar 2018
Posts: 114
Rep Power: 3
Evren Linda is on a distinguished road
Quote:
Originally Posted by SvenBent View Post
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.
Evren Linda is offline   Reply With Quote

Old   August 2, 2019, 20:05
Default
  #8
Senior Member
 
Evren Linda's Avatar
 
Evren Linda
Join Date: Mar 2018
Posts: 114
Rep Power: 3
Evren Linda is on a distinguished road
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");

Last edited by Evren Linda; August 3, 2019 at 09:15.
Evren Linda is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
read IOObject::NO_WRITE/AUTO_WRITE from dictionary jameswilson620 Main CFD Forum 0 July 17, 2015 12:52
Parallel Error in ANSYS FLUENT 12 zeusxx FLUENT 25 July 17, 2015 05:40
How to read a list from a dictionary? ivan_cozza OpenFOAM Programming & Development 6 November 7, 2014 09:31
Overwriting dictionary from BC Phicau OpenFOAM Programming & Development 1 January 25, 2013 05:24
New Boundary Condition: Reading Dictionary Problem Koga OpenFOAM Programming & Development 0 November 26, 2012 06:01


All times are GMT -4. The time now is 22:20.