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

OFstream for parallel processing

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

Like Tree15Likes
  • 9 Post By ngj
  • 6 Post By Lieven

Reply
 
LinkBack Thread Tools Search this Thread 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: 29
Rep Power: 13
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: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37
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 6 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: 299
Rep Power: 23
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 3 others like this.
Lieven is offline   Reply With Quote

Old   May 7, 2014, 14:20
Default
  #4
Senior Member
 
Join Date: Dec 2011
Posts: 121
Rep Power: 15
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

Old   May 3, 2018, 11:45
Default
  #6
Member
 
Join Date: Oct 2015
Posts: 63
Rep Power: 11
Scram_1 is on a distinguished road
Has anyone managed to figure out what the problem is? Even I'm stuck with the same problem.

Best,
Scram_1
Scram_1 is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
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 10:15
[OpenFOAM] is parallel processing available in parafoam for post processing? sachinlb ParaView 1 August 14, 2012 10:52
How does one initialise an OFstream in the constructor? bigphil OpenFOAM Programming & Development 2 August 18, 2011 09:48
NO model vs post processing in coal combustion,CFX sakalido CFX 1 April 15, 2011 15:07
Post Processing in FEM Abhijit Tilak Main CFD Forum 0 April 26, 2004 12:59


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