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/)
-   -   How to save a time-dependent scalar variable ? (https://www.cfd-online.com/Forums/openfoam-programming-development/152628-how-save-time-dependent-scalar-variable.html)

Yann May 5, 2015 09:35

How to save a time-dependent scalar variable ?
 
Hello Foamers,

I'm currently working on a custom solver and there is something I can't figure out.

The solver calculates and uses a scalar variable (a rotational speed). It works fine but since this variable isn't written anywhere, I can't stop a case and restart it later.

My first idea was to use an IOobject to write this variable with the other data in each time directory but I can't figure how. Since it's only a single scalar I have to use a IOdictionary but I'm unable to write data in it.

So dear experienced users, how would you deal with this ?
Any suggestions are welcome.

Thanks,
Yann

alexeym May 5, 2015 10:33

Hi,

You can try to use UniformDimensionedField [1]. Constructor [2] will fit, if you have scalar value, or [3] if you would like to read it. See $FOAM_SRC/src/finiteVolume/cfdTools/general/include/readGravitationalAcceleration.H for example of syntax.

1. http://foam.sourceforge.net/docs/cpp/a02750.html
2. http://foam.sourceforge.net/docs/cpp...767af9c8b8573a
3. http://foam.sourceforge.net/docs/cpp...12a8bffe2acdf7

Yann May 6, 2015 12:19

Thanks for your reply Alexey. I'm going to try it but I might have the same problem.

I worked a bit on this dictionary thing. I work within a modified mixerGgiFvMesh class which inherits from the dynamicFvMesh class.
Here is how my dictionary is declared in myMixerGgiFvMesh.C :

Code:

myVarDict_
(
    IOdictionary
    (
        IOobject
        (
            "myVar",
            time().timeName(),
            *this,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        )
    )
),

This works great : I can read and write "myVar" entry in the dictionary. But I'd like this dictionary to be written in a file in each time directory of my case. It doesn't work and my guess is that my dictionary isn't in the good registry since I work within the myMixerGgiFvMesh class. (correct me if I'm wrong)

But if I change "*this" to, lets say, "mesh()", I get an error when compiling saying :

Code:

error: ‘mesh’ was not declared in this scope
Am I wrong?
Any idea how to write the dictionary file in each time directory?

Thanks,
Yann

alexeym May 6, 2015 13:01

Example would be great. As if I declare this (quick modification of icoFoam solver)

Code:

    IOdictionary test
    (
        IOobject
        (
            "testVar",
            runTime.timeName(),
            mesh,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        )
    );

and then do this:

Code:

test.set<scalar>("deltaT", runTime.deltaT().value());
dictionary testVar with line "deltaT <deltaT value>;" is written in every time folder.

Quote:

Code:

error: ‘mesh’ was not declared in this scope
Am I wrong?
Any idea how to write the dictionary file in each time directory?
mixerGgiFvMesh is a child of dynamicFvMesh which in turn is a child of fvMesh, so there is no mesh() method. You can try to use thisDb() instead of *this, it will be registry of the fvMesh class.

Yann May 7, 2015 03:56

Thanks again for your kind help Alexey.

I already tried to use thisDb() instead of *this but it doesn't change anything, the dictionary isn't written in time folders.

But I'm sure my dictionary is properly declared since the first value is read from the "0" folder and if I do a :

Code:

Info<< "reading myVarDict : " << readScalar(myVarDict_.lookup("myVar")) << endl;
The correct value is prompted in the terminal during simulation. So it seems it's really a matter of being able to write this dictionary in the proper folder...


All times are GMT -4. The time now is 13:07.