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/)
-   -   Accessing mesh dictionary from solver (https://www.cfd-online.com/Forums/openfoam-programming-development/82132-accessing-mesh-dictionary-solver.html)

Pascal_doran November 17, 2010 18:00

Accessing mesh dictionary from solver
 
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

olesen November 18, 2010 02:29

Quote:

Originally Posted by Pascal_doran (Post 283829)
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.

Pascal_doran November 18, 2010 21:50

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

olesen November 19, 2010 02:25

Quote:

Originally Posted by Pascal_doran (Post 283991)
...
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 November 19, 2010 02:40

Quote:

Originally Posted by Pascal_doran (Post 283991)
...
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.

l_r_mcglashan November 19, 2010 04:29

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. :(.

albet October 31, 2017 07:26

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


All times are GMT -4. The time now is 05:16.