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

How does one initialise an OFstream in the constructor?

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

Reply
 
LinkBack Thread Tools Display Modes
Old   August 17, 2011, 07:17
Default How does one initialise an OFstream in the constructor?
  #1
Senior Member
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin,Ireland
Posts: 570
Rep Power: 19
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, 13:33
Default
  #2
Senior Member
 
David Gaden
Join Date: Apr 2009
Location: Winnipeg, Canada
Posts: 397
Rep Power: 12
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, 08:48
Default
  #3
Senior Member
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin,Ireland
Posts: 570
Rep Power: 19
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

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
Face Set Constructor for star v4.02 gonnafon CD-adapco 1 June 6, 2012 09: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 09:59.