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

Accessing dictionaries from constant folder in multi region solver

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 13, 2022, 11:18
Question Accessing dictionaries from constant folder in multi region solver
  #1
F42
New Member
 
Join Date: Jun 2018
Posts: 20
Rep Power: 8
F42 is on a distinguished road
Hello,


I wrote my own solver based on chtMultiRegionFoam.
Therefore, I read dictionaries from the constant folder. Now I want to use more than one solid and one fluid region but I can't figure out how to acess the dictionaries for an arbitrary number of regions. I'm using of8.



I tried:


Code:
    

forAll(solidRegions, i)
{

fvMesh& mesh = solidRegions[i];
 
IOdictionary myProperties
(
    IOobject
    (
        "myProperties",
        mesh.time().constant(),
        mesh,
        IOobject::MUST_READ_IF_MODIFIED,
        IOobject::NO_WRITE
    )
 );
}
When I try to acess the dict properties later on it throws an error saying that myProperties dict is not declared in this scope:



Code:
forAll(solidRegions,i)
{

const scalar myScalar = readScalar(myProperties.lookup("myScalar"));
}
When running the solver it throws the error


Code:
--> FOAM FATAL ERROR: 

request for dictionary myProperties from objectRegistry solid1 failed available objects of type dictionary are


3
(
fvSchemes
fvSolution
data
)
I was using the solver with only one solid and one fluid region for quite some time. I used "solidRegions[0]" for the object registry and it worked flawlessly. I don't understand why it's not working for i regions.



Any hints about how to use dictionaries in a multi region solver are appreciated. Thanks.
F42 is offline   Reply With Quote

Old   January 14, 2022, 05:24
Default
  #2
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
Not really sure how you expect this to work. In the first loop you create and discard "myProperties" several times. At later stage you are hoping to access the variable from a different scope??? Consider the following standard C/C++ (not OpenFOAM-specific):
Code:
int foo = 100;
if (true)
{
    int bar = 200;
}

// print results
{
    printf("foo = %d\n", foo);
    printf("bar = %d\n", bar);
}
You will find that "bar" is unknown.
olesen is offline   Reply With Quote

Old   January 14, 2022, 06:48
Default
  #3
F42
New Member
 
Join Date: Jun 2018
Posts: 20
Rep Power: 8
F42 is on a distinguished road
Well, I believed that every dictionary "belongs" to a mesh. So while looping through the mesh regions I tried to create the same dictionary for every mesh region. I thought the respecitve dictionaries are only known "inside" their region and could be accessed later on while looping through the mesh regions again .

I want to make use of openFoam's dictionary function and easily define different properties for each physical region for the simulation in the case/constant folder. (basically like it's done with thermophysicalProperties or whatever).


Can you tell me where I did go wrong and how to actually achieve this? Tanks!
F42 is offline   Reply With Quote

Old   January 14, 2022, 09:45
Default
  #4
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
Quote:
Originally Posted by F42 View Post
Well, I believed that every dictionary "belongs" to a mesh. So while looping through the mesh regions I tried to create the same dictionary for every mesh region. I thought the respecitve dictionaries are only known "inside" their region and could be accessed later on while looping through the mesh regions again .

I want to make use of openFoam's dictionary function and easily define different properties for each physical region for the simulation in the case/constant folder. (basically like it's done with thermophysicalProperties or whatever).

Can you tell me where I did go wrong and how to actually achieve this? Tanks!
If you want an IOdictionary (or any other regIOobject) to be registered to a mesh region, then you typically to check that it doesn't already exist on the registry.
You have the possibility to have the registered objects stored externally to the registry or actually stored on the registry. In your original code example, you added have "myProperties" as a local variable, which is indeed registered on the objectRegistry, but since that variable is local, it removes itself from the registry when it goes out of scope.

So one possiblity:
Code:
PtrList<IOdictionary> allMyProperties(solidRegions.size());

forAll(solidRegions, regioni)
{
    auto& mesh = solidRegions[regioni];
    allMyProperties.set
    (
        regioni,
        new IOdictionary
        (
             IOobject
             (
                  "myProperties",
                    mesh.time().constant(),
                    mesh,
                    IOobject::MUST_READ_IF_MODIFIED,
                    IOobject::NO_WRITE
             )
        )
    );
}
This is the simplest. If you want to store directly on the registry, you will need "store" instead of using the PtrList, but I think this solution is simpler for your needs.
F42 likes this.

Last edited by olesen; January 19, 2022 at 06:56.
olesen is offline   Reply With Quote

Old   January 17, 2022, 11:46
Thumbs up
  #5
F42
New Member
 
Join Date: Jun 2018
Posts: 20
Rep Power: 8
F42 is on a distinguished road
Thank you! It works perfectly so far .
For my understanding: What's the advantage of storing the objects externally to the registry vs on the registry?


There was just an "o" missing in your code. The following code works on of8 if anybody has the same issue as me:


Quote:
Originally Posted by olesen View Post
Code:
PtrList<IOdictionary> allMyProperties(solidRegions.size());

forAll(solidRegions, regioni)
{
    auto& mesh = solidRegions[regioni];
    allMyProperties.set
    (
        regioni,
        new IOdictionary
        (
             IOobject
             (
                  "myProperties",
                    mesh.time().constant(),
                    mesh,
                    IOobject::MUST_READ_IF_MODIFIED,
                    IOobject::NO_WRITE
             )
        )
    );
}
F42 is offline   Reply With Quote

Old   January 19, 2022, 06:56
Default
  #6
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
Quote:
Originally Posted by F42 View Post
Thank you! It works perfectly so far .
For my understanding: What's the advantage of storing the objects externally to the registry vs on the registry?

The answer is "depend". If you have things that you will mostly or only be accessing via the objectRegistry lookups in various places, then storing to the registry probably fits well with your needs. Perhaps for your case you would like to access them both via the registries AND as a linear list, the external storage makes sense.


In some places, it is useful to check if a field exists (on the registry). If it doesn't, then create a local-scope autoPtr of the field and have it registered on the registry. When the autoPtr goes out of scope, its deletion will also remove the field from the registry. Gives a nice cleanup behaviour: leaves it on the registry if it already existed before, but also cleans up the local-scope one too.
olesen is offline   Reply With Quote

Reply

Tags
chtmultiregionfoam, dictionary, multi regions, object registry


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
chtMultiRegionFoam solver stops without any error amol_patel OpenFOAM Running, Solving & CFD 4 July 5, 2024 02:41
Negative initial temperature error (chtMultiRegionFoam) jebin OpenFOAM Pre-Processing 60 July 17, 2022 06:10
chtMultiRegionFoam speed up qwertz OpenFOAM Running, Solving & CFD 8 September 18, 2021 07:16
PEMFC model with FLUENT brahimchoice FLUENT 22 April 19, 2020 16:44
problem with Min/max rho tH3f0rC3 OpenFOAM 8 July 31, 2019 10:48


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