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/)
-   -   OFstream for parallel processing (https://www.cfd-online.com/Forums/openfoam-programming-development/125627-ofstream-parallel-processing.html)

lavdwall October 28, 2013 05:21

OFstream for parallel processing
 
Hi everyone!

I wrote a code in which several useful numbers and calculated values are written to one single file during runtime. The file is constructed like this:

fileName heatData(runTime.path()/"postProcessing");
OFstream heatFile("heatData");

In the solver code, the following lines are included in order to write the average temperature at the wall, the average Nusselt number, the heat flux etc. to the "heatData" file.

heatFile<< runTime.timeName() << tab
<< average(Twall.boundaryField()[wallPatch]) << tab
<< Tbulk.value() << tab
<< gradTwallAv.value() << tab
<< heatFlux.value() <<tab
<< gamma.value() << tab
<< average(nusseltNumber.boundaryField()[wallPatch]) << ";" << endl;
heatFile.flush();

This works perfectly when I work on 1 processor. All values are written to the "heatData"-file in separate columns, just as I want. However, when I run my case in parallel on different processors (using decomposePar), the "heatData"-file looks totally useless. Data is written randomly, line breaks are randomly added and zeros are outputted instead of the real values.

Can anyone explain what's going on here? Or give me some help?

ngj October 28, 2013 14:56

Hi Laurien,

Yes, it is basically, because all of your processors try to write the information to the same file. Since not all of the processors have faces on all patches, this probably explains the zeros. The random input of values will be due to the fact that the processors are not synchronised all of the time.

Let me try to sketch something, which should probably (not compiled):

IN CONSTRUCTOR:

Code:

autoPtr<OFstream> file_;

if (Pstream::master())
{
    file_.reset( new OFstream(<path + name>));
}

Now, in the part, where you are writing to the file, you could do something along the following lines (I only show it for one output variable):

Code:

// Get reference to the boundary fields
label patchI = <some patch index>   
const scalarField& tw = T.boundaryField()[patchI];
const scalarField& magsf = mesh.magSf().boundaryField()[patchI];

// Get the sum of areas and T*area on that processor
scalar sarea = Foam::sum(magsf);
scalar sT = Foam::sum(tw*magsf);

// Collect across the processors
reduce(sarea, sumOp<scalar>());
reduce(sT, sumOp<scalar>());

// Alternatively, there might be a function called gAverage, but
// I have not checked.

// Only if the processor is the master, write to the file
if (Pstream::master())
{
    // Perform the patch averaging here.
    file_() << sT/sarea << endl;
}

I hope that this helps.

Kind regards,

Niels

Lieven October 28, 2013 15:59

You might also want to use a small function to get the output directory:

Code:

const fileName pollutant::getOutputDir() const
{
    // Create the output folder if it doesn't exist yet
    fileName outputDir;
    if (Pstream::parRun())
    {
        outputDir = mesh_.time().path()/".."/"postProcessing";
    }
    else
    {
        outputDir = mesh_.time().path()/"postProcessing";
    }
    if (!isDir(outputDir))
    {
        mkDir(outputDir);
    }

    return outputDir;
}

Else OpenFOAM will put it in a processor-directory when running in parallel (not really what you want).

Cheers,

L

fshak92 May 7, 2014 13:20

Hey FOAMers,

At the end of my parallel solver, i filled a vectorField with some values and i'd like to print them into a file :
Code:

Ub= ... ; 
reduce(Ub, sumOp<vectorField>() );
if  (Pstream::master()){
fileName outputFile(mesh.time().path()/".."/("test.csv");
OFstream test(outputFile);
test <<"First line to test" << endl ;
test << mag(gAverage(Ub)) <<  endl;}

The first line is written into the file, and then the simulation pauses forever,without any error(like being in an infinite loop!) .
If i don't use 'Pstream::master()' , simulation keeps going without any problem.

Tnx in advance for any idea about the reason.

mgaedtke December 1, 2015 11:06

Quote:

Originally Posted by fshak92 (Post 490449)
Hey FOAMers,

At the end of my parallel solver, i filled a vectorField with some values and i'd like to print them into a file :
Code:

Ub= ... ; 
reduce(Ub, sumOp<vectorField>() );
if  (Pstream::master()){
fileName outputFile(mesh.time().path()/".."/("test.csv");
OFstream test(outputFile);
test <<"First line to test" << endl ;
test << mag(gAverage(Ub)) <<  endl;}

The first line is written into the file, and then the simulation pauses forever,without any error(like being in an infinite loop!) .
If i don't use 'Pstream::master()' , simulation keeps going without any problem.

Tnx in advance for any idea about the reason.

I'm having the same problem. Have you found a solution?

Greetings, Max

Scram_1 May 3, 2018 10:45

Has anyone managed to figure out what the problem is? Even I'm stuck with the same problem.

Best,
Scram_1


All times are GMT -4. The time now is 17:24.