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);
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 
71  baseDir.clean(); // Remove unneeded ".."
72 
73  return baseDir;
74 }
75 
76 
78 {
79  return baseFileDir()/prefix_/fileObr_.time().timeName();
80 }
81 
82 
84 (
85  const word& name,
86  scalar timeValue
87 ) const
88 {
89  autoPtr<OFstream> osPtr;
90 
91  if (Pstream::master() && writeToFile_)
92  {
93  if (useUserTime_)
94  {
95  timeValue = fileObr_.time().timeToUserTime(timeValue);
96  }
97 
98  const word timeName = Time::timeName(timeValue);
99 
100  fileName outputDir(baseFileDir()/prefix_/timeName);
101 
102  mkDir(outputDir);
103 
104  word fName(name);
105 
106  // Check if file already exists
107  IFstream is(outputDir/(fName + ".dat"));
108  if (is.good())
109  {
110  fName = fName + "_" + timeName;
111  }
112 
113  osPtr.reset(new OFstream(outputDir/(fName + ".dat")));
114 
115  if (!osPtr->good())
116  {
117  FatalIOErrorInFunction(osPtr()) << "Cannot open file"
118  << exit(FatalIOError);
119  }
120 
121  initStream(osPtr());
122  }
123 
124  return osPtr;
125 }
126 
127 
129 (
130  const word& name
131 ) const
132 {
133  return createFile(name, startTime_);
134 
135 }
136 
137 
139 {
140  fileName_ = fileName;
141  filePtr_ = createFile(fileName_);
142 }
143 
144 
146 (
147  const label offset
148 ) const
149 {
150  return setw(writePrecision_ + addChars + offset);
151 }
152 
153 
154 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
155 
157 :
158  fileObr_(wf.fileObr_),
159  prefix_(wf.prefix_),
160  fileName_(wf.fileName_),
161  filePtr_(nullptr),
162  writePrecision_(wf.writePrecision_),
163  writeToFile_(wf.writeToFile_),
164  updateHeader_(wf.updateHeader_),
165  writtenHeader_(wf.writtenHeader_),
166  useUserTime_(wf.useUserTime_),
167  startTime_(wf.startTime_)
168 {}
169 
170 
172 (
173  const objectRegistry& obr,
174  const fileName& prefix,
175  const word& name,
176  const bool writeToFile
177 )
178 :
179  fileObr_(obr),
180  prefix_(prefix),
181  fileName_(name),
182  filePtr_(nullptr),
183  writePrecision_(IOstream::defaultPrecision()),
184  writeToFile_(writeToFile),
185  updateHeader_(true),
186  writtenHeader_(false),
187  useUserTime_(true),
188  startTime_(obr.time().startTime().value())
189 {}
190 
191 
193 (
194  const objectRegistry& obr,
195  const fileName& prefix,
196  const word& name,
197  const dictionary& dict,
198  const bool writeToFile
199 )
200 :
201  writeFile(obr, prefix, name, writeToFile)
202 {
203  read(dict);
204 
205  if (writeToFile_)
206  {
207  filePtr_ = createFile(fileName_);
208  }
209 }
210 
211 
212 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
213 
215 {
216  writePrecision_ =
217  dict.getCheckOrDefault
218  (
219  "writePrecision",
221  labelMinMax::ge(0)
222  );
223 
224  updateHeader_ = dict.getOrDefault("updateHeader", updateHeader_);
225 
226  // Only write on master
227  writeToFile_ =
228  Pstream::master() && dict.getOrDefault("writeToFile", writeToFile_);
229 
230  // Use user time, e.g. CA deg in preference to seconds
231  useUserTime_ = dict.getOrDefault("useUserTime", true);
232 
233  return true;
234 }
235 
236 
238 {
239  if (!writeToFile_)
240  {
241  return Snull;
242  }
243 
244  if (!filePtr_)
245  {
247  << "File pointer not allocated\n";
248  }
249 
250  return *filePtr_;
251 }
252 
253 
255 {
256  return writeToFile_;
257 }
258 
259 
261 {
262  return writeToFile_ && (updateHeader_ || !writtenHeader_);
263 }
264 
265 
267 {
268  return writePrecision_ + addChars;
269 }
270 
271 
273 (
274  Ostream& os,
275  const string& str
276 ) const
277 {
278  os << setw(1) << "#";
279 
280  if (str.size())
281  {
282  os << setw(1) << ' '
283  << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str();
284  }
285 }
286 
287 
289 (
290  Ostream& os,
291  const string& str
292 ) const
293 {
294  os << tab << setw(charWidth()) << str.c_str();
295 }
296 
297 
299 (
300  Ostream& os,
301  const string& str
302 ) const
303 {
304  writeCommented(os, str);
305  os << nl;
306 }
307 
308 
310 {
311  const scalar timeValue =
312  (
313  useUserTime_
314  ? fileObr_.time().timeOutputValue()
315  : fileObr_.time().value()
316  );
317 
318  os << setw(charWidth()) << Time::timeName(timeValue);
319 }
320 
321 
323 {
324  writeHeader(os, "===");
325 }
326 
327 
328 // ************************************************************************* //
Foam::setf
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:109
writeFile.H
Foam::functionObjects::writeFile::writeCurrentTime
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:309
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:237
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
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:260
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
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:49
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
Foam::functionObjects::writeFile::writeHeader
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:299
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:322
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:214
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:146
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::functionObject::outputPrefix
static word outputPrefix
Directory prefix.
Definition: functionObject.H:352
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:121
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:455
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::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:369
Time.H
Foam::autoPtr< Foam::OFstream >
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:273
Foam::tab
constexpr char tab
Definition: Ostream.H:384
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
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:84
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:172
Foam::functionObjects::writeFile::resetFile
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:138
Foam::fileName::clean
static bool clean(std::string &str)
Cleanup filename.
Definition: fileName.C:298
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:401
Foam::functionObjects::writeFile::writeToFile
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:254
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::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::functionObjects::writeFile::baseTimeDir
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:77
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:266
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:289
Foam::Ostream::width
virtual int width() const =0
Get width of output field.