functionObjectFile.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "functionObjectFile.H"
27 #include "Time.H"
28 #include "polyMesh.H"
29 #include "IFstream.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 const Foam::word Foam::functionObjectFile::outputPrefix = "postProcessing";
35 
36 
37 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
38 
40 {
41  os.setf(ios_base::scientific, ios_base::floatfield);
43  os.width(charWidth());
44 }
45 
46 
48 {
49  fileName baseDir = obr_.time().path();
50 
51  if (Pstream::parRun())
52  {
53  // Put in undecomposed case (Note: gives problems for
54  // distributed data running)
55  baseDir = baseDir/".."/outputPrefix;
56  }
57  else
58  {
59  baseDir = baseDir/outputPrefix;
60  }
61 
62  // Append mesh name if not default region
63  if (isA<polyMesh>(obr_))
64  {
65  const polyMesh& mesh = refCast<const polyMesh>(obr_);
67  {
68  baseDir = baseDir/mesh.name();
69  }
70  }
71 
72  return baseDir;
73 }
74 
75 
77 {
78  return baseFileDir()/prefix_/obr_.time().timeName();
79 }
80 
81 
83 (
84  const word& name
85 ) const
86 {
87  autoPtr<OFstream> osPtr;
88 
89  if (Pstream::master() && writeToFile_)
90  {
91  const word startTimeName =
92  obr_.time().timeName(obr_.time().startTime().value());
93 
94  fileName outputDir(baseFileDir()/prefix_/startTimeName);
95 
96  mkDir(outputDir);
97 
98  word fName(name);
99 
100  // Check if file already exists
101  IFstream is(outputDir/(fName + ".dat"));
102  if (is.good())
103  {
104  fName = fName + "_" + obr_.time().timeName();
105  }
106 
107  osPtr.set(new OFstream(outputDir/(fName + ".dat")));
108 
109  initStream(osPtr());
110  }
111 
112  return osPtr;
113 }
114 
115 
117 {
118  fileName_ = fileName;
119  filePtr_ = createFile(fileName_);
120 }
121 
122 
124 {
125  return setw(IOstream::defaultPrecision() + addChars + offset);
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
130 
132 (
133  const objectRegistry& obr,
134  const word& prefix
135 )
136 :
137  obr_(obr),
138  prefix_(prefix),
139  fileName_("undefined"),
140  filePtr_(),
141  writePrecision_(IOstream::defaultPrecision()),
142  writeToFile_(true)
143 {}
144 
145 
147 (
148  const objectRegistry& obr,
149  const word& prefix,
150  const word& fileName,
151  const dictionary& dict
152 )
153 :
154  obr_(obr),
155  prefix_(prefix),
156  fileName_(fileName),
157  filePtr_(),
158  writePrecision_(IOstream::defaultPrecision()),
159  writeToFile_(true)
160 {
161  read(dict);
162 
163  if (writeToFile_)
164  {
165  filePtr_ = createFile(fileName_);
166  }
167 }
168 
169 
170 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
171 
173 {}
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
179 {
180  writePrecision_ =
181  dict.lookupOrDefault("writePrecision", IOstream::defaultPrecision());
182 
183  // Only write on master process
184  writeToFile_ = dict.lookupOrDefault("writeToFile", true);
185  writeToFile_ = writeToFile_ && Pstream::master();
186 }
187 
188 
190 {
191  if (!writeToFile_)
192  {
193  return Snull;
194  }
195 
196  if (!filePtr_.valid())
197  {
199  << "File pointer not allocated";
200  }
201 
202  return filePtr_();
203 }
204 
205 
207 {
208  return writeToFile_;
209 }
210 
211 
213 {
214  return IOstream::defaultPrecision() + addChars;
215 }
216 
217 
219 (
220  Ostream& os,
221  const string& str
222 ) const
223 {
224  os << setw(1) << "#" << setw(1) << ' '
225  << setw(charWidth() - 2) << str.c_str();
226 }
227 
228 
230 (
231  Ostream& os,
232  const string& str
233 ) const
234 {
235  os << tab << setw(charWidth()) << str.c_str();
236 }
237 
238 
240 (
241  Ostream& os,
242  const string& str
243 ) const
244 {
245  os << setw(1) << "#" << setw(1) << ' '
246  << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str() << nl;
247 }
248 
249 
251 {
252  os << setw(charWidth()) << obr_.time().timeName();
253 }
254 
255 
256 // ************************************************************************* //
Foam::setf
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:164
Foam::functionObjectFile::writeTime
void writeTime(Ostream &os) const
Write the current time to stream.
Definition: functionObjectFile.C:250
Foam::functionObjectFile::valueWidth
virtual Omanip< int > valueWidth(const label offset=0) const
Return the value width when writing to stream with optional offset.
Definition: functionObjectFile.C:123
Foam::Snull
OFstream Snull
Global predefined null output stream "/dev/null".
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::functionObjectFile::baseFileDir
virtual fileName baseFileDir() const
Return the base directory for output.
Definition: functionObjectFile.C:47
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::functionObjectFile::functionObjectFile
functionObjectFile(const functionObjectFile &)
Disallow default bitwise copy construct.
Foam::functionObjectFile::charWidth
label charWidth() const
Return width of character stream output.
Definition: functionObjectFile.C:212
Foam::functionObjectFile::writeTabbed
void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: functionObjectFile.C:230
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::fileName::path
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:293
Foam::dictionary::lookupOrDefault
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Definition: dictionaryTemplates.C:33
Foam::functionObjectFile::read
void read(const dictionary &dict)
Read.
Definition: functionObjectFile.C:178
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::functionObjectFile::file
OFstream & file()
Return access to the file (if only 1)
Definition: functionObjectFile.C:189
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::autoPtr::set
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
Foam::functionObjectFile::initStream
virtual void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: functionObjectFile.C:39
Foam::functionObjectFile::outputPrefix
static const word outputPrefix
Directory prefix.
Definition: functionObjectFile.H:117
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::functionObjectFile::writeHeader
void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: functionObjectFile.C:240
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
Foam::functionObjectFile::writeToFile
bool writeToFile() const
Return true if can write to file.
Definition: functionObjectFile.C:206
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::functionObjectFile::resetFile
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: functionObjectFile.C:116
Foam::autoPtr< Foam::OFstream >
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
functionObjectFile.H
Foam::functionObjectFile::addChars
static label addChars
Additional characters for writing.
Definition: functionObjectFile.H:120
Foam::tab
static const char tab
Definition: Ostream.H:259
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
Foam::Omanip
Definition: IOmanip.H:47
Foam::functionObjectFile::createFile
virtual autoPtr< OFstream > createFile(const word &name) const
Return an autoPtr to a new file.
Definition: functionObjectFile.C:83
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::functionObjectFile::writeCommented
void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: functionObjectFile.C:219
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::functionObjectFile::baseTimeDir
virtual fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: functionObjectFile.C:76
Foam::functionObjectFile::writePrecision_
label writePrecision_
Write precision.
Definition: functionObjectFile.H:76
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::fvMesh::name
const word & name() const
Return reference to name.
Definition: fvMesh.H:257
Foam::functionObjectFile::~functionObjectFile
virtual ~functionObjectFile()
Destructor.
Definition: functionObjectFile.C:172
Foam::Ostream::width
virtual int width() const =0
Get width of output field.