CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

OFstream for parallel processing

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree8Likes
  • 4 Post By ngj
  • 4 Post By Lieven

Reply
 
LinkBack Thread Tools Display Modes
Old   October 28, 2013, 06:21
Default OFstream for parallel processing
  #1
New Member
 
Laurien Vandewalle
Join Date: Jun 2013
Location: Ghent, Belgium
Posts: 18
Rep Power: 5
lavdwall is on a distinguished road
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?
lavdwall is offline   Reply With Quote

Old   October 28, 2013, 15:56
Default
  #2
ngj
Senior Member
 
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Deltares, Delft, The Netherlands
Posts: 1,698
Rep Power: 27
ngj will become famous soon enoughngj will become famous soon enough
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
fumiya, dkxls, fshak92 and 1 others like this.
__________________
Please note that I do not use the Friend-feature, so do not be offended, if I do not accept a request.
ngj is offline   Reply With Quote

Old   October 28, 2013, 16:59
Default
  #3
Senior Member
 
Lieven
Join Date: Dec 2011
Location: Leuven, Belgium
Posts: 297
Rep Power: 14
Lieven will become famous soon enough
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
ngj, fumiya, fshak92 and 1 others like this.
Lieven is offline   Reply With Quote

Old   May 7, 2014, 13:20
Default
  #4
Senior Member
 
Join Date: Dec 2011
Posts: 121
Rep Power: 6
fshak92 is on a distinguished road
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.
fshak92 is offline   Reply With Quote

Old   December 1, 2015, 12:06
Default
  #5
New Member
 
Max Gaedtke
Join Date: Nov 2015
Posts: 1
Rep Power: 0
mgaedtke is on a distinguished road
Quote:
Originally Posted by fshak92 View Post
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
mgaedtke is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
parallel processing time+reconstruct time<serial processing?! immortality OpenFOAM 8 August 13, 2013 09:15
is parallel processing available in parafoam for post processing? sachinlb OpenFOAM Paraview & paraFoam 1 August 14, 2012 09:52
How does one initialise an OFstream in the constructor? bigphil OpenFOAM Programming & Development 2 August 18, 2011 08:48
NO model vs post processing in coal combustion,CFX sakalido CFX 1 April 15, 2011 14:07
Post Processing in FEM Abhijit Tilak Main CFD Forum 0 April 26, 2004 11:59


All times are GMT -4. The time now is 10:33.