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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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);
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 region name if not default region
62 const auto* meshPtr = isA<polyMesh>(fileObr_);
63 if (meshPtr)
64 {
65 baseDir /= meshPtr->regionName();
66 }
67 baseDir.clean(); // Remove unneeded ".."
68
69 return baseDir;
70}
71
72
74{
75 return baseFileDir()/prefix_/fileObr_.time().timeName();
76}
77
78
80(
81 const word& name,
82 scalar timeValue
83) const
84{
86
87 if (Pstream::master() && writeToFile_)
88 {
89 if (useUserTime_)
90 {
91 timeValue = fileObr_.time().timeToUserTime(timeValue);
92 }
93
94 const word timeName = Time::timeName(timeValue);
95
96 fileName outputDir(baseFileDir()/prefix_/timeName);
97
98 mkDir(outputDir);
99
100 word fName(name);
101
102 // Check if file already exists
103 IFstream is(outputDir/(fName + ".dat"));
104 if (is.good())
105 {
106 fName = fName + "_" + timeName;
107 }
108
109 osPtr.reset(new OFstream(outputDir/(fName + ".dat")));
110
111 if (!osPtr->good())
112 {
113 FatalIOErrorInFunction(osPtr()) << "Cannot open file"
114 << exit(FatalIOError);
115 }
116
117 initStream(osPtr());
118 }
119
120 return osPtr;
121}
122
123
125(
126 const word& name
127) const
128{
129 return createFile(name, startTime_);
130
131}
132
133
135{
136 fileName_ = fileName;
137 filePtr_ = createFile(fileName_);
138}
139
140
142(
143 const label offset
144) const
145{
146 return setw(writePrecision_ + addChars + offset);
147}
148
149
150// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
151
153:
154 fileObr_(wf.fileObr_),
155 prefix_(wf.prefix_),
156 fileName_(wf.fileName_),
157 filePtr_(nullptr),
158 writePrecision_(wf.writePrecision_),
159 writeToFile_(wf.writeToFile_),
160 updateHeader_(wf.updateHeader_),
161 writtenHeader_(wf.writtenHeader_),
162 useUserTime_(wf.useUserTime_),
163 startTime_(wf.startTime_)
164{}
165
166
168(
169 const objectRegistry& obr,
170 const fileName& prefix,
171 const word& name,
172 const bool writeToFile
173)
174:
175 fileObr_(obr),
176 prefix_(prefix),
177 fileName_(name),
178 filePtr_(nullptr),
179 writePrecision_(IOstream::defaultPrecision()),
180 writeToFile_(writeToFile),
181 updateHeader_(true),
182 writtenHeader_(false),
183 useUserTime_(true),
184 startTime_(obr.time().startTime().value())
185{}
186
187
189(
190 const objectRegistry& obr,
191 const fileName& prefix,
192 const word& name,
193 const dictionary& dict,
194 const bool writeToFile
195)
196:
197 writeFile(obr, prefix, name, writeToFile)
198{
199 read(dict);
200
201 if (writeToFile_)
202 {
204 }
205}
206
207
208// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
209
211{
212 writePrecision_ =
213 dict.getCheckOrDefault
214 (
215 "writePrecision",
218 );
219
220 updateHeader_ = dict.getOrDefault("updateHeader", updateHeader_);
221
222 // Only write on master
223 writeToFile_ =
224 Pstream::master() && dict.getOrDefault("writeToFile", writeToFile_);
225
226 // Use user time, e.g. CA deg in preference to seconds
227 useUserTime_ = dict.getOrDefault("useUserTime", true);
228
229 return true;
230}
231
232
234{
235 if (!writeToFile_)
236 {
237 return Snull;
238 }
239
240 if (!filePtr_)
241 {
243 << "File pointer not allocated\n";
244 }
245
246 return *filePtr_;
247}
248
249
251{
252 return writeToFile_;
253}
254
255
257{
258 return writeToFile_ && (updateHeader_ || !writtenHeader_);
259}
260
261
263{
264 return writePrecision_ + addChars;
265}
266
267
269(
270 Ostream& os,
271 const string& str
272) const
273{
274 os << setw(1) << "#";
275
276 if (str.size())
277 {
278 os << setw(1) << ' '
279 << setf(ios_base::left) << setw(charWidth() - 2) << str.c_str();
280 }
281}
282
283
285(
286 Ostream& os,
287 const string& str
288) const
289{
290 os << tab << setw(charWidth()) << str.c_str();
291}
292
293
295(
296 Ostream& os,
297 const string& str
298) const
299{
300 writeCommented(os, str);
301 os << nl;
302}
303
304
306{
307 const scalar timeValue =
308 (
309 useUserTime_
310 ? fileObr_.time().timeOutputValue()
311 : fileObr_.time().value()
312 );
313
314 os << setw(charWidth()) << Time::timeName(timeValue);
315}
316
317
319{
320 writeHeader(os, "===");
321}
322
323
324// ************************************************************************* //
Foam::label startTime
Input from file stream, using an ISstream.
Definition: IFstream.H:57
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition: MinMaxI.H:31
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
virtual int width() const
Get width of output field.
Definition: OSstream.C:314
An Ostream manipulator taking arguments.
Definition: IOmanip.H:143
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
virtual word timeName() const
Return current time name.
Definition: Time.C:790
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A class for handling file names.
Definition: fileName.H:76
static bool clean(std::string &str)
Definition: fileName.C:199
static word outputPrefix
Directory prefix.
Base class for writing single files from the function objects.
Definition: writeFile.H:120
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:50
label writePrecision_
Write precision.
Definition: writeFile.H:138
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:285
word fileName_
Name of file.
Definition: writeFile.H:132
virtual label charWidth() const
Return width of character stream output.
Definition: writeFile.C:262
Omanip< int > valueWidth(const label offset=0) const
Return the value width when writing to stream with optional offset.
Definition: writeFile.C:142
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:295
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:210
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:318
virtual autoPtr< OFstream > createFile(const word &name, scalar timeValue) const
Return autoPtr to a new file for a given time.
Definition: writeFile.C:80
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:42
bool writeToFile_
Flag to enable/disable writing to file.
Definition: writeFile.H:141
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:233
static label addChars
Additional characters for writing.
Definition: writeFile.H:196
autoPtr< OFstream > filePtr_
File pointer.
Definition: writeFile.H:135
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:269
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:305
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:250
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:73
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:134
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:256
Registry of regIOobjects.
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
Foam::autoPtr< Foam::dynamicFvMesh > meshPtr
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
word timeName
Definition: getTimeIndex.H:3
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:169
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:515
static void writeHeader(Ostream &os, const word &fieldName)
OFstream Snull
Global predefined null output stream "/dev/null".
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
IOerror FatalIOError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
dictionary dict