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

How does one initialise an OFstream in the constructor?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 17, 2011, 08:17
Default How does one initialise an OFstream in the constructor?
  #1
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Hi,


I have a class which contains an OFstream member
Code:
OFstream outFile_;
For serial runs I normally initialise this member in the constructor using the quick method:
Code:
Foam::myClass::myClass
(
...
constructor arguments
...
)
:
outFile_("fileName")
{
...
main constructor here
...
}
But for parallel runs, I only want the master processor to create this file.

I have tried initialise the OFstream object in the main constructor (not the quick way above), but it is wrong:
Code:
if(Pstream::master())
{
outFile_ = new OFstream("fileName");
}
Could anybody let me know how I might do this correctly?


Philip
bigphil is offline   Reply With Quote

Old   August 17, 2011, 14:33
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 437
Rep Power: 22
marupio is on a distinguished road
You can use the conditional operator in initialization lists. Here's an example from my code:

Code:
    convergence_
    (
        dict_.found("convergence")
      ? readScalar(dict_.lookup("implicitConvergence"))
      : defaultConvergence_
    ),
The syntax (with typical OF whitespace) is:
Code:
    item
    (
        trueOrFalseExpression
      ? useThisIfTrue
      : useThisIfFalse
    ),
The trick is both the useThisIfTrue and useThisIfFalse have to be the same type of object. So you probably have to create a null OFstream for non-master processors, or something like that.

-Dave
marupio is offline   Reply With Quote

Old   August 18, 2011, 09:48
Default
  #3
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Dave,


Thanks, this looks like what I need.

I tried:
Code:
outFile_
(
Pstream::master()
? "fileName"
: NULL
)
This compiles fine but gets the run-time error:
Code:
error
what():  basic_string::_S_construct NULL not valid
terminate called after throwing an instance of 'std::logic_error'

So as a work-around, I have done this:
Code:
outFile_
(
Pstream::master()
? "fileName"
: "slaveNullFile"
)
This does the job but an empty slaveNullFile is created.


Thanks for the help,
Philip
bigphil is offline   Reply With Quote

Reply


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
Face Set Constructor for star v4.02 gonnafon Siemens 1 June 6, 2012 10:16
MultiComponentMixture constructor prashant24983 OpenFOAM Running, Solving & CFD 5 February 7, 2011 16:54
face order in fvMesh constructor schmittp54 OpenFOAM Programming & Development 2 November 2, 2010 10:42
A problem about class OFstream jennyrui2008 OpenFOAM Running, Solving & CFD 0 December 5, 2008 04:55
Initialise Profile Data Aloise CFX 3 December 9, 2006 11:39


All times are GMT -4. The time now is 19:50.