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/)
-   -   Global variable in parallel code (https://www.cfd-online.com/Forums/openfoam-programming-development/151056-global-variable-parallel-code.html)

Aleksey_R April 3, 2015 09:42

Global variable in parallel code
 
Greetings, dear foamers!

What is the easiest way to implement global variable in OF code, which is synchronized between the processes on request and which is written to file same as all other vector/scalar fields?

I would be grateful for any ideas.

alexeym April 3, 2015 09:52

Hi,

Little clarification, what is the type of this variable? In general "all other vector/scalar fields" can be considered as global variables.

Aleksey_R April 3, 2015 09:56

Hello and thank you for reply.
I mean that I need not a field, but only 1 floating point value, which is synchronized over all processes.
I suppose that it could be uniformDimensionedScalarField, and synchronization can be done by means of OPstream and IPstream, but I don't know how to make reconstructPar "reconstruct" this field.

alexeym April 3, 2015 11:14

Hi,

Curiouser and curiouser ;)

How the scalar should be synchronized? Maximum of all processors, sum of all processors? Any way there is reduce that can perform binary operation on the scalar from different processors.

Concerning output. Take OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.C as an example. When I needed to calculate energy and mass balances in the simulations and write them into file, I've used this code in the constructor of the balances calculation class:

Code:

    if (active_)
    {
        fileName balancesSubDir = "balances"/mesh_.time().timeName();
           
        if (Pstream::parRun())
        {
            balancesSubDir = mesh_.time().path()/".."/balancesSubDir;
        }
        else
        {
            balancesSubDir = mesh_.time().path()/balancesSubDir;
        }
        mkDir(balancesSubDir);
   
        // Writing file headers
        if (mass_)
        {
            Info<< "Creating mass balances ... " << endl;
            mPtr_ = new OFstream(balancesSubDir/"mass.dat");
            *mPtr_ << "# 1_t(s) 2_Mass(kg) 3_dM(kg) 4_dMest(kg)" << endl;
        }
...

Though maybe there is another, more idiomatic, way to do it.


All times are GMT -4. The time now is 01:04.