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 |
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);
42  os.width(charWidth());
43 }
44 
45 
47 {
48  fileName baseDir = obr_.time().path();
49 
50  if (Pstream::parRun())
51  {
52  // Put in undecomposed case (Note: gives problems for
53  // distributed data running)
54  baseDir = baseDir/".."/outputPrefix;
55  }
56  else
57  {
58  baseDir = baseDir/outputPrefix;
59  }
60 
61  // Append mesh name if not default region
62  if (isA<polyMesh>(obr_))
63  {
64  const polyMesh& mesh = refCast<const polyMesh>(obr_);
65  if (mesh.name() != polyMesh::defaultRegion)
66  {
67  baseDir = baseDir/mesh.name();
68  }
69  }
70 
71  return baseDir;
72 }
73 
74 
76 {
77  return baseFileDir()/prefix_/obr_.time().timeName();
78 }
79 
80 
82 {
83  if (Pstream::master())
84  {
85  const word startTimeName =
86  obr_.time().timeName(obr_.time().startTime().value());
87 
88  forAll(names_, i)
89  {
90  if (!filePtrs_.set(i))
91  {
92  fileName outputDir(baseFileDir()/prefix_/startTimeName);
93  mkDir(outputDir);
94 
95  word fName(names_[i]);
96 
97  // Check if file already exists
98  IFstream is(outputDir/(fName + ".dat"));
99  if (is.good())
100  {
101  fName = fName + "_" + obr_.time().timeName();
102  }
103 
104  filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat")));
105 
106  initStream(filePtrs_[i]);
107 
108  writeFileHeader(i);
109 
110  }
111  }
112  }
113 }
114 
115 
117 {}
118 
119 
121 {
122  createFiles();
123 }
124 
125 
127 {
128  names_.clear();
129  names_.append(names);
130 
131  if (Pstream::master())
132  {
133  filePtrs_.clear();
134  filePtrs_.setSize(names_.size());
135 
136  createFiles();
137  }
138 }
139 
140 
142 {
143  names_.clear();
144  names_.append(name);
145 
146  if (Pstream::master())
147  {
148  filePtrs_.clear();
149  filePtrs_.setSize(1);
150 
151  createFiles();
152  }
153 }
154 
155 
157 {
158  return setw(IOstream::defaultPrecision() + addChars + offset);
159 }
160 
161 
162 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
163 
165 (
166  const objectRegistry& obr,
167  const word& prefix
168 )
169 :
170  obr_(obr),
171  prefix_(prefix),
172  names_(),
173  filePtrs_()
174 {}
175 
176 
178 (
179  const objectRegistry& obr,
180  const word& prefix,
181  const word& name
182 )
183 :
184  obr_(obr),
185  prefix_(prefix),
186  names_(),
187  filePtrs_()
188 {
189  names_.clear();
190  names_.append(name);
191  if (Pstream::master())
192  {
193  filePtrs_.clear();
194  filePtrs_.setSize(1);
195 
196  // Cannot create files - need to access virtual function
197  }
198 }
199 
200 
202 (
203  const objectRegistry& obr,
204  const word& prefix,
205  const wordList& names
206 )
207 :
208  obr_(obr),
209  prefix_(prefix),
210  names_(names),
211  filePtrs_()
212 {
213  names_.clear();
214  names_.append(names);
215  if (Pstream::master())
216  {
217  filePtrs_.clear();
218  filePtrs_.setSize(names_.size());
219 
220  // Cannot create files - need to access virtual function
221  }
222 }
223 
224 
225 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
226 
228 {}
229 
230 
231 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
232 
234 {
235  return names_;
236 }
237 
238 
240 {
241  if (!Pstream::master())
242  {
243  FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
244  << "Request for file() can only be done by the master process"
245  << abort(FatalError);
246  }
247 
248  if (filePtrs_.size() != 1)
249  {
250  WarningIn("Foam::Ostream& Foam::functionObjectFile::file()")
251  << "Requested single file, but multiple files are present"
252  << endl;
253  }
254 
255  if (!filePtrs_.set(0))
256  {
257  FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
258  << "File pointer at index " << 0 << " not allocated"
259  << abort(FatalError);
260  }
261 
262  return filePtrs_[0];
263 }
264 
265 
267 {
268  if (!Pstream::master())
269  {
270  FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::files()")
271  << "Request for files() can only be done by the master process"
272  << abort(FatalError);
273  }
274 
275  return filePtrs_;
276 }
277 
278 
280 {
281  if (!Pstream::master())
282  {
284  (
285  "Foam::OFstream& Foam::functionObjectFile::file(const label)"
286  )
287  << "Request for file(i) can only be done by the master process"
288  << abort(FatalError);
289  }
290 
291  if (!filePtrs_.set(i))
292  {
293  FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()")
294  << "File pointer at index " << i << " not allocated"
295  << abort(FatalError);
296  }
297 
298  return filePtrs_[i];
299 }
300 
301 
303 {
305 }
306 
307 
309 (
310  Ostream& os,
311  const string& str
312 ) const
313 {
314  os << setw(1) << "#" << setw(1) << ' '
315  << setw(charWidth() - 2) << str.c_str();
316 }
317 
318 
320 (
321  Ostream& os,
322  const string& str
323 ) const
324 {
325  os << tab << setw(charWidth()) << str.c_str();
326 }
327 
328 
330 (
331  Ostream& os,
332  const string& str
333 ) const
334 {
335  os << setw(1) << "#" << setw(1) << ' '
336  << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str() << nl;
337 }
338 
339 
340 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:380
OFstream & file()
Return access to the file (if only 1)
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:164
const wordList & names() const
Return const access to the names.
fileName path() const
Return path.
Definition: Time.H:281
functionObjectFile(const functionObjectFile &)
Disallow default bitwise copy construct.
virtual int width() const =0
Get width of output field.
virtual void resetName(const word &name)
Reset the list of names to a single name entry.
A class for handling words, derived from string.
Definition: word.H:59
virtual void resetNames(const wordList &names)
Reset the list of names from a wordList.
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
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:741
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
virtual void initStream(Ostream &os) const
Initialise the output stream for writing.
static label addChars
Additional characters for writing.
dynamicFvMesh & mesh
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:872
Input from file stream.
Definition: IFstream.H:81
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
virtual void createFiles()
Create the output file.
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
static const word outputPrefix
Directory prefix.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
const word & name() const
Return name.
Definition: IOobject.H:260
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
virtual void write()
Write function.
virtual void writeFileHeader(const label i=0)
File header information.
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:306
static const char tab
Definition: Ostream.H:259
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
error FatalError
Registry of regIOobjects.
A class for handling file names.
Definition: fileName.H:69
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
label charWidth() const
Return width of character stream output.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:398
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:97
const Time & time() const
Return time.
virtual ~functionObjectFile()
Destructor.
virtual fileName baseFileDir() const
Return the base directory for output.
virtual Omanip< int > valueWidth(const label offset=0) const
Return the value width when writing to stream with optional offset.
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:420
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
virtual fileName baseTimeDir() const
Return the base directory for the current time value.
PtrList< OFstream > & files()
Return access to the files.
const Type & value() const
Return const reference to value.
void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.