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

Accessing mesh dictionary from solver

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 17, 2010, 19:00
Default Accessing mesh dictionary from solver
  #1
Member
 
Pascal
Join Date: Jun 2009
Location: Montreal
Posts: 65
Rep Power: 17
Pascal_doran is on a distinguished road
Hi all,

I have a very simple rectangular mesh (let say 8m X 4m X 2m) and I have nx, ny, nz cells (which are defined in blockMeshDict file) in each direction. I would like to access the nx variable within icoFoam solver. More precisely I would like to have a code inside icoFoam.C that says:
Code:
int readnx(readInt(mesh.blockMeshDict().subDict("blocks").lookup("nx")))
(But this code doesn't work...) And what "*.H" file should I include?

I've been able to do successfully something similar for reading the PISO control:
Code:
int monNCorr(readInt(mesh.solutionDict().subDict("PISO").lookup("nCorrectors")))
Thank you,
Pascal
Pascal_doran is offline   Reply With Quote

Old   November 18, 2010, 03:29
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Pascal_doran View Post
Hi all,

I have a very simple rectangular mesh (let say 8m X 4m X 2m) and I have nx, ny, nz cells (which are defined in blockMeshDict file) in each direction. I would like to access the nx variable within icoFoam solver. More precisely I would like to have a code inside icoFoam.C that says:
Code:
int readnx(readInt(mesh.blockMeshDict().subDict("blocks").lookup("nx")))
(But this code doesn't work...)
You first need to realize that the blockMeshDict is not really associated with the mesh in any way whatsoever. Although you may have used blockMesh and the blockMeshDict to generate a mesh, you could just as easily have created your mesh somehow else (snappyHex, salome, engrid, ...), in which case the blockMeshDict has absolutely nothing to do with the mesh at all!

This should help you understand why "mesh.blockMeshDict()" doesn't and shouldn't exist.

Having said that, if you nonetheless wish to get values from the blockMeshDict (in the hope that it is somehow related to your mesh), you need to open the file yourself.
For example,

Code:
        IOdictionary meshDict
        (
            IOobject
            (
                "blockMeshDict",
                runTime.constant(),
                polyMeshDir,
                runTime,
                IOobject::MUST_READ,
                IOobject::NO_WRITE,
                false
            )
        );
and then access the values that you want.
Depending upon where you use this code snippet, you may wish to replace the variable 'runTime' with a time object from your mesh.
olesen is offline   Reply With Quote

Old   November 18, 2010, 22:50
Default
  #3
Member
 
Pascal
Join Date: Jun 2009
Location: Montreal
Posts: 65
Rep Power: 17
Pascal_doran is on a distinguished road
Thank you for your help Mr. Olesen,

As you said I added in the solver:
Code:
    fileName polyMeshDir;

    IOdictionary meshDict
    (
        IOobject
        (
            "blockMeshDict",
            runTime.constant(),
            polyMeshDir,
            runTime,
            IOobject::MUST_READ,
            IOobject::NO_WRITE,
            false
        )
    );
I had to add the following line at the beginning:
Code:
fileName polyMeshDir;
BTW why does it try to find the "blockMeshDic" file in "case/constant" directory? I added a link to "case/constant/polyMesh" and it works fine, but I don't know why it's not able find the "blockMeshDic" file by itself.

Then, to know what "meshDic" contain I used the following line of code:
Code:
Info<< meshDict.lookup("blocks") << endl;
And there's the output:
Code:
24
(
(
hex
(
0
1
2
3
4
5
6
7
)
(
100
50
1
)
simpleGrading
(
1
1
1
)
)
)
Now I know the information I need is in it (nx=100, ny=50, nz=1). But I'm stuck here. How can I access those three values? I tried:
Code:
Info<< meshDict.subDict("blocks") << endl;
Code:
Info<< meshDict.subDict("blocks").lookup("hex") << endl;
and few other things but nothing works...
At the end I would like to have a line that allow me to create the variable
Code:
int readNx(readInt(meshDict.subDict("blocks")[0].lookup("nx")));
Thank you for help!
Pascal
Pascal_doran is offline   Reply With Quote

Old   November 19, 2010, 03:25
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Pascal_doran View Post
...
I had to add the following line at the beginning:
Code:
fileName polyMeshDir;
BTW why does it try to find the "blockMeshDic" file in "case/constant" directory? I added a link to "case/constant/polyMesh" and it works fine, but I don't know why it's not able find the "blockMeshDic" file by itself.
Sorry, I mistakenly thought you'd worked with OpenFOAM programming before. The value of polyMeshDir should correspond to where the "polyMesh" directory is found. Normally you'd just use the value from the variable
Code:
Foam::polyMesh::meshSubDir
Of course if you have a multi-region solver, you'll need to adjust it accordingly. For example, as shown in the following pseudo-code:
Code:
    fileName polyMeshDir;

    if ( ... )   // multi-region
    {
        // constant/<region>/polyMesh/blockMeshDict
        polyMeshDir = regionName/polyMesh::meshSubDir;
    }
    else
    {
        // constant/polyMesh/blockMeshDict
        polyMeshDir = polyMesh::meshSubDir;
    }
olesen is offline   Reply With Quote

Old   November 19, 2010, 03:40
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by Pascal_doran View Post
...
Now I know the information I need is in it (nx=100, ny=50, nz=1). But I'm stuck here. How can I access those three values?

This is definely a non-trival task. You can get at "block" itself fairly easily, but you'll notice it contains a list of entries that you'll need to process yourself.
If we examine a general entry
Code:
blocks
(
    hex (0 1 2 3 4 5 6 7)     (5 50 1) simpleGrading (1 1 1)
    hex (1 8 9 2 5 10 11 6)   (40 50 1) simpleGrading (1 1 1)
    hex (3 2 12 13 7 6 14 15) (5 50 1) simpleGrading (1 1 1)
);
And separate it into its underlying OpenFOAM data types:
Code:
blocks
(
    word labelList labelList word labelList
    word labelList labelList word labelList
    word labelList labelList word labelList
);
Noting that the dictionary lookup on "block" returns a ITstream, you'll have to parse this yourself to get at the values.

This is not a bit of effort, but you don't even have any assurance that the values you extract are related to your mesh! (as I mentioned in a previous post). I would thus view this entire approach as more than a slight waste of time.
olesen is offline   Reply With Quote

Old   November 19, 2010, 05:29
Default
  #6
Senior Member
 
Laurence R. McGlashan
Join Date: Mar 2009
Posts: 370
Rep Power: 23
l_r_mcglashan will become famous soon enough
When I had to get a list of heights in a geometry so that I could do a lagrangian-style calculation, I used the STL to get a list of independent heights. I gave it a vector axialPositions that contained the relevant component of every cell:

Code:
    std::sort(axialPositions.begin(), axialPositions.end());
    axialPositions.erase(std::unique(axialPositions.begin(), axialPositions.end(), equalToTolerance), axialPositions.end());

At the time I couldn't think of anything better. .
__________________
Laurence R. McGlashan :: Website
l_r_mcglashan is offline   Reply With Quote

Old   October 31, 2017, 08:26
Default
  #7
Member
 
Amir
Join Date: Jan 2017
Posts: 32
Rep Power: 9
albet is on a distinguished road
Dear Foamers,
I am new to OpenFOAM and c++, I hope somebody helps me.
Question: Can I replace findPatchIDs with patchSet?

I want to use

Code:
labelHashSet  patchSet  (const UList< wordRe > &patchNames, const bool warnNotFound=true, const bool usePatchGroups=true) const
for a specified wall patch (e.g. wall named "wallA") instead of

Code:
labelHashSet findPatchIDs()const
for a given polyPatch type (e.g. wallPolyPatch)
1. is it possible?
2. how can I obtain
Code:
UList< wordRe > &patchNames
for a specified patch?

Regards,
Amir
albet is offline   Reply With Quote

Reply

Tags
read mesh


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
[ICEM] Generating Mesh for STL Car in Windtunnel Simulation tommymoose ANSYS Meshing & Geometry 48 April 15, 2013 05:24
Bug in turbulent dynamic Mesh Solver thomas OpenFOAM Running, Solving & CFD 0 October 29, 2008 04:48
LES with moving mesh solver gtg627e OpenFOAM Running, Solving & CFD 2 July 23, 2007 11:40
Problems using dynamic mesh and 6dof solver Brett FLUENT 0 March 3, 2006 18:45
compressible two phase flow in CFX4.4 youngan CFX 0 July 2, 2003 00:32


All times are GMT -4. The time now is 18:50.