writeFile.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2012-2018 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "writeFile.H"
30 #include "Time.H"
31 #include "polyMesh.H"
32 #include "IFstream.H"
33 #include "functionObject.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
38 
39 
40 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
41 
43 {
44  os.setf(ios_base::scientific, ios_base::floatfield);
45  os.precision(writePrecision_);
46  os.width(charWidth());
47 }
48 
49 
51 {
52  // Put in undecomposed case
53  // (Note: gives problems for distributed data running)
54 
55  fileName baseDir
56  (
57  fileObr_.time().globalPath()
59  );
60 
61  // Append mesh name if not default region
62  if (isA<polyMesh>(fileObr_))
63  {
64  const polyMesh& mesh = refCast<const polyMesh>(fileObr_);
65  if (mesh.name() != polyMesh::defaultRegion)
66  {
67  baseDir /= mesh.name();
68  }
69  }
70  baseDir.clean(); // Remove unneeded ".."
71 
72  return baseDir;
73 }
74 
75 
77 {
78  return baseFileDir()/prefix_/fileObr_.time().timeName();
79 }
80 
81 
83 (
84  const word& name,
85  scalar timeValue
86 ) const
87 {
88  autoPtr<OFstream> osPtr;
89 
90  if (Pstream::master() && writeToFile_)
91  {
92  if (useUserTime_)
93  {
94  timeValue = fileObr_.time().timeToUserTime(timeValue);
95  }
96 
97  const word timeName = Time::timeName(timeValue);
98 
99  fileName outputDir(baseFileDir()/prefix_/timeName);
100 
101  mkDir(outputDir);
102 
103  word fName(name);
104 
105  // Check if file already exists
106  IFstream is(outputDir/(fName + ".dat"));
107  if (is.good())
108  {
109  fName = fName + "_" + timeName;
110  }
111 
112  osPtr.reset(new OFstream(outputDir/(fName + ".dat")));
113 
114  if (!osPtr->good())
115  {
116  FatalIOErrorInFunction(osPtr()) << "Cannot open file"
117  << exit(FatalIOError);
118  }
119 
120  initStream(osPtr());
121  }
122 
123  return osPtr;
124 }
125 
126 
128 (
129  const word& name
130 ) const
131 {
132  return createFile(name, startTime_);
133 
134 }
135 
136 
138 {
139  fileName_ = fileName;
140  filePtr_ = createFile(fileName_);
141 }
142 
143 
145 (
146  const label offset
147 ) const
148 {
149  return setw(writePrecision_ + addChars + offset);
150 }
151 
152 
153 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
154 
156 :
157  fileObr_(wf.fileObr_),
158  prefix_(wf.prefix_),
159  fileName_(wf.fileName_),
160  filePtr_(nullptr),
161  writePrecision_(wf.writePrecision_),
162  writeToFile_(wf.writeToFile_),
163  updateHeader_(wf.updateHeader_),
164  writtenHeader_(wf.writtenHeader_),
165  useUserTime_(wf.useUserTime_),
166  startTime_(wf.startTime_)
167 {}
168 
169 
171 (
172  const objectRegistry& obr,
173  const fileName& prefix,
174  const word& name,
175  const bool writeToFile
176 )
177 :
178  fileObr_(obr),
179  prefix_(prefix),
180  fileName_(name),
181  filePtr_(nullptr),
182  writePrecision_(IOstream::defaultPrecision()),
183  writeToFile_(writeToFile),
184  updateHeader_(true),
185  writtenHeader_(false),
186  useUserTime_(true),
187  startTime_(obr.time().startTime().value())
188 {}
189 
190 
192 (
193  const objectRegistry& obr,
194  const fileName& prefix,
195  const word& name,
196  const dictionary& dict,
197  const bool writeToFile
198 )
199 :
200  writeFile(obr, prefix, name, writeToFile)
201 {
202  read(dict);
203 
204  if (writeToFile_)
205  {
206  filePtr_ = createFile(fileName_);
207  }
208 }
209 
210 
211 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
212 
214 {
215  writePrecision_ =
216  dict.getCheckOrDefault
217  (
218  "writePrecision",
220  labelMinMax::ge(0)
221  );
222 
223  updateHeader_ = dict.getOrDefault("updateHeader", updateHeader_);
224 
225  // Only write on master
226  writeToFile_ =
227  Pstream::master() && dict.getOrDefault("writeToFile", writeToFile_);
228 
229  // Use user time, e.g. CA deg in preference to seconds
230  useUserTime_ = dict.getOrDefault("useUserTime", true);
231 
232  return true;
233 }
234 
235 
237 {
238  if (!writeToFile_)
239  {
240  return Snull;
241  }
242 
243  if (!filePtr_)
244  {
246  << "File pointer not allocated\n";
247  }
248 
249  return *filePtr_;
250 }
251 
252 
254 {
255  return writeToFile_;
256 }
257 
258 
260 {
261  return writeToFile_ && (updateHeader_ || !writtenHeader_);
262 }
263 
264 
266 {
267  return writePrecision_ + addChars;
268 }
269 
270 
272 (
273  Ostream& os,
274  const string& str
275 ) const
276 {
277  os << setw(1) << "#";
278 
279  if (str.size())
280  {
281  os << setw(1) << ' '
282  << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str();
283  }
284 }
285 
286 
288 (
289  Ostream& os,
290  const string& str
291 ) const
292 {
293  os << tab << setw(charWidth()) << str.c_str();
294 }
295 
296 
298 (
299  Ostream& os,
300  const string& str
301 ) const
302 {
303  writeCommented(os, str);
304  os << nl;
305 }
306 
307 
309 {
310  const scalar timeValue =
311  (
312  useUserTime_
313  ? fileObr_.time().timeOutputValue()
314  : fileObr_.time().value()
315  );
316 
317  os << setw(charWidth()) << Time::timeName(timeValue);
318 }
319 
320 
322 {
323  writeHeader(os, "===");
324 }
325 
326 
327 // ************************************************************************* //
Foam::setf
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
writeFile.H
Foam::functionObjects::writeFile::writeCurrentTime
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:308
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:236
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:318
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::writeFile::addChars
static label addChars
Additional characters for writing.
Definition: writeFile.H:196
Foam::functionObjects::writeFile::writePrecision_
label writePrecision_
Write precision.
Definition: writeFile.H:138
Foam::functionObjects::writeFile::canWriteHeader
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:259
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::FatalIOError
IOerror FatalIOError
Foam::functionObjects::writeFile::baseFileDir
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:50
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:434
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::functionObjects::writeFile::writeHeader
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:298
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::writeFile::writeBreak
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:321
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:213
Foam::Time::timeName
virtual word timeName() const
Return current time name.
Definition: Time.C:790
Foam::functionObjects::writeFile::valueWidth
Omanip< int > valueWidth(const label offset=0) const
Return the value width when writing to stream with optional offset.
Definition: writeFile.C:145
Foam::autoPtr::good
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
Foam::functionObject::outputPrefix
static word outputPrefix
Directory prefix.
Definition: functionObject.H:376
IFstream.H
timeName
word timeName
Definition: getTimeIndex.H:3
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::functionObjects::writeFile::initStream
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:42
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
Time.H
Foam::autoPtr< Foam::OFstream >
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:272
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::writeFile::createFile
virtual autoPtr< OFstream > createFile(const word &name, scalar timeValue) const
Return autoPtr to a new file for a given time.
Definition: writeFile.C:83
Foam::Omanip
An Ostream manipulator taking arguments.
Definition: IOmanip.H:50
Foam::MinMax::ge
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition: MinMaxI.H:31
Foam::functionObjects::writeFile::writeFile
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true)
Construct from objectRegistry, prefix, fileName.
Definition: writeFile.C:171
Foam::functionObjects::writeFile::resetFile
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:137
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::functionObjects::writeFile::writeToFile
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:253
Foam::Snull
OFstream Snull
Global predefined null output stream "/dev/null".
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::functionObjects::writeFile::baseTimeDir
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:76
functionObject.H
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::functionObjects::writeFile::charWidth
virtual label charWidth() const
Return width of character stream output.
Definition: writeFile.C:265
Foam::Time::startTime
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:867
Foam::functionObjects::writeFile::writeTabbed
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:288
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178