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/)
-   -   Getting access to mesh (fvMesh) via object registry (https://www.cfd-online.com/Forums/openfoam-programming-development/104745-getting-access-mesh-fvmesh-via-object-registry.html)

Chris Lucas July 14, 2012 15:31

Getting access to mesh (fvMesh) via object registry
 
Hi

I want to read a dictionary within a class. I can do this e.g. with a code similar to this

IOdictionary myDict
(
IOobject
(
"myDict",
mesh.time().constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

The problem is that I need the access to "mesh" for this. However, the class is not connected to the mesh (or any other field that has access to the mesh). Additionally, giving the class direct access to mesh would be very difficult.

Therefore the question, can I get access to the mesh via the object registry? If yes, how?

Or this there another way to load the dict without the mesh?

Thanks for the help

Christian

gregor July 16, 2012 07:25

Hi Christian,

Yes you can. A simple but by no means the most elegant way could look like this

Code:

const volVectorField& U = obr_.lookupObject<volVectorField>("U"); //  use object registry to acces U

fvMesh & mesh  =  U.mesh();

I am using the U reference to acces the mesh because i need U anyways. May be that helps

tomislav_maric July 17, 2012 04:50

Foam::Time is an objectRegistry, you can register the IOdictionary to it:


Code:

    IOdictionary myDict
    (
        IOobject
        (
            "myDict",
            runTime.constant(),
            runTime,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        )
    );

Quote:

Originally Posted by Chris Lucas (Post 371463)
Hi

I want to read a dictionary within a class. I can do this e.g. with a code similar to this

IOdictionary myDict
(
IOobject
(
"myDict",
mesh.time().constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

The problem is that I need the access to "mesh" for this. However, the class is not connected to the mesh (or any other field that has access to the mesh). Additionally, giving the class direct access to mesh would be very difficult.

Therefore the question, can I get access to the mesh via the object registry? If yes, how?

Or this there another way to load the dict without the mesh?

Thanks for the help

Christian


Chris Lucas July 18, 2012 03:23

Hi,

thank you both for your help. Unfortunately, both ideas don't work in my case.

@gregor

for your idea (as I understand it), I need to provide the object registry in the constructor (as shown in the class "probes"). However, this is not possible without changing a larger part of the library.

@tomislav_maric

I know that option but I have the same problem as with the mesh, I have no direct access to runTime.

I guess the simplest way (unless someone has another idea) is to read the file using "fstream" and then get the data I need

Best Regards,
Christian

gregor July 18, 2012 07:48

Quote:

Originally Posted by Chris Lucas (Post 372059)
However, this is not possible without changing a larger part of the library.
Christian

Which library are you working on ? I think almost everywhere you have some acces to the object registry either through a geometricField (like U,p ...) or directly through runTime. All the constructor needs is a reference to an object registry.

Chris Lucas July 19, 2012 03:11

Hi,

thank you for your help.

Your right, I should have started with this :).

I'm working in the thermophysical library (OpenFoam 1.6 ext.) at a class similar to the perfectGas (I need to modify my realGasMixtureEOS class)

I know that my problem is gone when I switch to OpenFOAM 2.x.x. But for now, I need to use OpenFOAM 1.6 ext.

Best Regards,
Christian

gregor July 23, 2012 06:44

What happens if you use
Code:

dictionary dict(IFstream(dictName)());
and then do a dict.read(Istream &) ?

Chris Lucas July 24, 2012 08:55

Hi,

thanks for the help. I tried to use the code, but it didn't work. :mad:

Best Regards
Christian

wavemaster November 7, 2012 10:04

Accessing mesh over Time and Objectregistry
 
Dear All,
I need to access the fvMesh inside a motionFunction which is to be used in a solidBodyMotionFvMesh.
I am able to access gravity for example via
Code:

  uniformDimensionedVectorField g
    (
        IOobject
        (
            "g",
            time_.constant(),
            time_.db(),
            IOobject::MUST_READ,
            IOobject::NO_WRITE
        )
    );

but I have not managed to access a reference to the fvMesh. The recommended solution above does not seem to work,
Code:

const volVectorField& U = time_.db().lookupObject<volVectorField>("U"); //  use object registry to acces U

const fvMesh & mesh  =  U.mesh();

I get

Code:

error: invalid use of incomplete type ‘const volVectorField {aka const struct Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>}’
/home/pal/OpenFOAM/OpenFOAM-2.1.x/src/finiteVolume/lnInclude/volFieldsFwd.H:52:7: error: declaration of ‘const volVectorField {aka const struct Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh>}’

As you can see I am using OpenFOAM-2.1.x. There should definitely be an elegant way to do this, but I am stuck...
Any comments as always much appreciated,
Cheers,
Pal

wavemaster November 7, 2012 10:10

Header Files
 
The error about incomplete Types was caused by a missing header file...
#include "volFields.H"

wavemaster November 7, 2012 10:26

Access to U fails
 
So when I run my code it fails because it does not find U although the velocity field exists.
Quote:

const volVectorField& U = time_.lookupObject<volVectorField>("U");
At the same time using time_ (runTime) I can access g in the constant folder...
Since time_ is of Type Time and a reference from runTime I thought I could access any value in the object registry. According to http://openfoamwiki.net/index.php/Op...istry#Overview the fvmesh is below runTime in memory. In my error message
Quote:

--> FOAM FATAL ERROR:

request for fvMesh mesh from objectRegistry flapAMIfreemotion failed
available objects of type fvMesh are

1
(
region0
)
, I can see that the objectRegistry where it is searching is my library(?) but still cannot figure out why I can access g but not fields or fvMesh.
Do I need to use a function of objectRegistry to access fvMesh? parent() should give me the parent registry but does not seem to do anything...
The nicest solution would be of the kind
Quote:

const fvMesh & mesh = time_.db().parent().lookupObject<fvMesh>("mesh");
but I really cannot make it work..

Any clues on why it is more difficult to access fields?
Cheers,
Pal

marupio November 7, 2012 23:47

"fvMesh" is the class name. The instance is "mesh". In nearly all solvers, "mesh" has the name i.e. mesh.name() "region0". Lookup "region0", not "fvMesh".

wavemaster November 8, 2012 04:13

Thank You!!!!!!!!!!!
 
Hi Marupio,
Thank you so much, it works fine now. I could have noticed before since Foam even tells you, but sometimes its quite hard to see the obvious.
You saved my day (and the rest of the week)...
Cheers,
Pal

AleDR May 30, 2014 11:07

How to access mesh from fvSolution dictionary
 
Hi everybody!

I am trying to access the mesh object from fvSolution dictionary when using codeStream.
My last attempt was:
Code:

PISO
{
    nCorrectors    2;
    nNonOrthogonalCorrectors 0;
    pRefCell        0;
    pRefValue      #codeStream  //to set level from analytical solution
{
    code
    #{
        const volScalarField& fieldRef = db().lookupObject<volScalarField>("p");
        const scalar xCoord = fieldRef.mesh()[0].x();
        const scalar yCoord = fieldRef.mesh()[0].y();

        const scalar pi_ = Foam::constant::mathematical::pi;
        scalar pRefA = -0.25 * (cos(2.0*pi_*xCoord) + cos(2.0*pi_*yCoord));
        os << pRefA;
    #};
};

But it ended with a:
Code:

error: ‘volScalarField’ does not name a type
I tried other combinations to directly access the mesh without relying on a field, but without success... Can anyone help me? :)
Finally I should be able to access runTime.timeValue()as well, but again it seems out of reach!

If you are curious the reason is that I would like to give a pRefValue in line with the analytic solution of Taylor vortex test case, in order to compute error norms via a coded functionObject. It worked fine for the U field, but now I would like to remove indeterminacy on the pressure field to evaluate p norms as well!

Thank you all!

.A.

AleDR June 4, 2014 05:52

Mmmh, since as already shown in this post the pRefValue is only weakly enforced, probably a better idea is to reverse the procedure: translate the analytical solution for pressure the right amount of units (i.e. the actual value of the volScalarField p in the pRefCell).

Anyway for the problem above I suppose that the most natural way should be to access the cell centres volVectorField by mesh object available in the master objectRegistry and then to extract the x and y components corresponding to the cell with ID equal to pRefCell. But I will be extremely happy if anybody can suggest alternative procedures! ;)

fvallejog July 29, 2016 21:17

Quote:

Originally Posted by tomislav_maric (Post 371818)
Foam::Time is an objectRegistry, you can register the IOdictionary to it:


Code:

    IOdictionary myDict
    (
        IOobject
        (
            "myDict",
            runTime.constant(),
            runTime,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        )
    );


Thank yo very much. I have the following problem: I wanted to put the diffusion coefficient in only region, but the solver calculated new values for this. The error was I had "mesh", and not "RunTime". You are the best, friend.

TurbJet January 23, 2018 20:05

Quote:

Originally Posted by gregor (Post 371606)
Hi Christian,

Yes you can. A simple but by no means the most elegant way could look like this

Code:

const volVectorField& U = obr_.lookupObject<volVectorField>("U"); //  use object registry to acces U

fvMesh & mesh  =  U.mesh();

I am using the U reference to acces the mesh because i need U anyways. May be that helps

I tried this, but the compiler informed me that "obr_ is not declared in the scope".

Is there any way I can solve this?

saicharan662000@gmail.com February 4, 2022 00:00

Hi guys,
@tomislav_maric
I want to use cell volumes in twoPhaseMixtureThermo.C in compressibleInterfoam.
const volScalarField& cellVolume = mesh.V();


I used the above line in my code.
But I got error saying that

mesh was not declared in this scope
Can anyone help me with this?
Thanks in advance

openfoam_aero January 15, 2024 02:57

I think you need to declare the mesh object. Something like
Code:

const Foam::word polyMeshPath = "target";
const Foam::IOobject meshIO("target", polyMeshPath, runTime, Foam::IOobject::MUST_READ);

// Create the fvMesh object using the updated path to the polyMesh directory
Foam::fvMesh target(meshIO);

should work. I have defined the IO dictionary that consists of the regioin, the path, runTime object and then i used it to create my mesh object. Once this is done, you can access the member functions that are part of the mesh object such as mesh.C(), mesh.V() and so on.

I forgot to mention - the above code creates the mesh object for a non default location (default is constant/polyMesh) but in this case you need to have a constant/target/polyMesh and a system/target for it to work.


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