writeFile.H
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-2016 OpenFOAM Foundation
9  Copyright (C) 2015-2022 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 Class
28  Foam::functionObjects::writeFile
29 
30 Description
31  Base class for writing single files from the function objects.
32 
33 Usage
34  \verbatim
35  <dictName>
36  {
37  // Inherited entries
38  ...
39 
40  // Optional entries
41  writePrecision <int>;
42  writeToFile <bool>;
43  useUserTime <bool>;
44  updateHeader <bool>;
45  }
46  \endverbatim
47 
48  where the entries mean:
49  \table
50  Property | Description | Type | Reqd | Deflt
51  writePrecision | Number of decimal points | int | no | <system dflt>
52  writeToFile | Produce text file output? | bool | no | true
53  useUserTime | Use user time (e.g. degrees)? | bool | no | true
54  updateHeader | Update header on mesh changes? | bool | no | true
55  \endtable
56 
57 Note
58  The file header is normally updated whenver the mesh points or
59  topology changes. In some cases, the function object is actually
60  unaffected by these changes.
61  Use the \c updateHeader flag to override the default behaviour.
62 
63 SourceFiles
64  writeFile.C
65  writeFileTemplates.C
66 
67 \*---------------------------------------------------------------------------*/
68 
69 #ifndef functionObjects_writeFile_H
70 #define functionObjects_writeFile_H
71 
72 #include "objectRegistry.H"
73 #include "OFstream.H"
74 #include "IOmanip.H"
75 
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
77 
78 namespace Foam
79 {
80 namespace functionObjects
81 {
82 
83 /*---------------------------------------------------------------------------*\
84  Class functionObjects::writeFile Declaration
85 \*---------------------------------------------------------------------------*/
86 
87 class writeFile
88 {
89 protected:
90 
91  // Protected Data
92 
93  //- Reference to the region objectRegistry
94  const objectRegistry& fileObr_;
95 
96  //- Prefix
97  const fileName prefix_;
98 
99  //- Name of file
100  word fileName_;
101 
102  //- File pointer
103  autoPtr<OFstream> filePtr_;
104 
105  //- Write precision
106  label writePrecision_;
107 
108  //- Flag to enable/disable writing to file
109  bool writeToFile_;
110 
111  //- Flag to update the header, e.g. on mesh changes.
112  //- Default is true.
113  bool updateHeader_;
114 
115  //- Flag to identify whether the header has been written
116  bool writtenHeader_;
117 
118  //- Flag to use the specified user time, e.g. CA deg instead
119  //- of seconds. Default = true
120  bool useUserTime_;
122  //- Start time value
123  scalar startTime_;
124 
125  //- File extension; default = .dat
126  string ext_;
127 
128 
129  // Protected Member Functions
130 
131  //- Initialise the output stream for writing
132  void initStream(Ostream& os) const;
133 
134  //- Return the base directory for output
135  fileName baseFileDir() const;
137  //- Return the base directory for the current time value
138  fileName baseTimeDir() const;
139 
140  //- Return the full path for the supplied file name
141  fileName filePath(const fileName& fName) const;
142 
143  //- Return autoPtr to a new file using file name
144  // Note: no check for if the file already exists
145  virtual autoPtr<OFstream> newFile(const fileName& fName) const;
147  //- Return autoPtr to a new file for a given time
149  (
150  const word& name,
151  scalar timeValue
152  ) const;
153 
154  //- Return autoPtr to a new file using the simulation start time
156  (
157  const word& name
158  ) const;
159 
160  //- Reset internal file pointer to new file with new name
161  virtual void resetFile(const word& name);
162 
163  //- Return the value width when writing to stream with optional offset
164  Omanip<int> valueWidth(const label offset = 0) const;
165 
166 
167  //- No copy assignment
168  void operator=(const writeFile&) = delete;
169 
170 
171  // Housekeeping
172 
173  //- Deprecated(2022-09) Return autoPtr to a new file for a given time
174  //
175  // \deprecated(2022-09) - use newFileAtTime function
176  FOAM_DEPRECATED_FOR(2022-09, "newFileAtTime function")
177  virtual autoPtr<OFstream> createFile
178  (
179  const word& name,
180  scalar timeValue
181  ) const
182  {
183  return newFileAtTime(name, timeValue);
184  }
185 
186  //- Deprecated(2022-09) Return autoPtr to a new file
187  //- using the simulation start time
188  //
189  // \deprecated(2022-09) - use newFileAtStartTime function
190  FOAM_DEPRECATED_FOR(2022-09, "newFileAtStartTime function")
191  virtual autoPtr<OFstream> createFile
192  (
193  const word& name
194  ) const
195  {
196  return newFileAtStartTime(name);
197  }
198 
199 
200 public:
201 
202  //- Additional characters for writing
203  static label addChars;
204 
205 
206  // Constructors
207 
208  //- Construct from objectRegistry, prefix, fileName
209  writeFile
210  (
211  const objectRegistry& obr,
212  const fileName& prefix,
213  const word& name = "undefined",
214  const bool writeToFile = true,
215  const string& ext = ".dat"
216  );
217 
218  //- Construct from objectRegistry, prefix, fileName
219  //- and read options from dictionary
220  writeFile
221  (
222  const objectRegistry& obr,
223  const fileName& prefix,
224  const word& name,
225  const dictionary& dict,
226  const bool writeToFile = true,
227  const string& ext = ".dat"
228  );
229 
230  //- Construct copy
231  writeFile(const writeFile& wf);
232 
233 
234  //- Destructor
235  virtual ~writeFile() = default;
236 
237 
238  // Member Functions
239 
240  //- Read
241  virtual bool read(const dictionary& dict);
242 
243  //- Set extension
244  virtual const string& setExt(const string& ext);
245 
246  //- Return access to the file (if only 1)
247  virtual OFstream& file();
248 
249  //- Flag to allow writing to file
250  virtual bool writeToFile() const;
251 
252  //- Flag to allow writing to the file
253  virtual bool canWriteToFile() const;
254 
255  //- Flag to allow resetting the file
256  virtual bool canResetFile() const;
257 
258  //- Flag to allow writing the header
259  virtual bool canWriteHeader() const;
260 
261  //- Return width of character stream output
262  virtual label charWidth() const;
263 
264  //- Write a commented string to stream
265  virtual void writeCommented(Ostream& os, const string& str) const;
267  //- Write a tabbed string to stream
268  virtual void writeTabbed(Ostream& os, const string& str) const;
269 
270  //- Write a commented header to stream
271  virtual void writeHeader(Ostream& os, const string& str) const;
272 
273  //- Write the current time to stream
274  virtual void writeCurrentTime(Ostream& os) const;
275 
276  //- Write a break marker to the stream
277  virtual void writeBreak(Ostream& os) const;
278 
279  //- Write a (commented) header property and value pair
280  template<class Type>
281  void writeHeaderValue
282  (
283  Ostream& os,
284  const string& property,
285  const Type& value
286  ) const;
287 
288  //- Write a given value to stream with the space delimiter
289  template<class Type>
290  void writeValue
291  (
292  Ostream& os,
293  const Type& val
294  ) const;
295 };
296 
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 } // End namespace functionObjects
301 } // End namespace Foam
302 
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 
305 #ifdef NoRepository
306  #include "writeFileTemplates.C"
307 #endif
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 #endif
312 
313 // ************************************************************************* //
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:270
dictionary dict
virtual ~writeFile()=default
Destructor.
A class for handling file names.
Definition: fileName.H:72
virtual bool canWriteToFile() const
Flag to allow writing to the file.
Definition: writeFile.C:292
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:344
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true, const string &ext=".dat")
Construct from objectRegistry, prefix, fileName.
Definition: writeFile.C:200
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:367
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:165
void writeValue(Ostream &os, const Type &val) const
Write a given value to stream with the space delimiter.
Output to file stream as an OSstream, normally using std::ofstream for the actual output...
Definition: OFstream.H:71
static label addChars
Additional characters for writing.
Definition: writeFile.H:279
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:157
virtual autoPtr< OFstream > newFile(const fileName &fName) const
Return autoPtr to a new file using file name.
Definition: writeFile.C:82
virtual const string & setExt(const string &ext)
Set extension.
Definition: writeFile.C:263
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
autoPtr< OFstream > filePtr_
File pointer.
Definition: writeFile.H:136
fileName filePath(const fileName &fName) const
Return the full path for the supplied file name.
Definition: writeFile.C:73
virtual bool canResetFile() const
Flag to allow resetting the file.
Definition: writeFile.C:298
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:318
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:304
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:35
virtual autoPtr< OFstream > createFile(const word &name, scalar timeValue) const
Deprecated(2022-09) Return autoPtr to a new file for a given time.
Definition: writeFile.H:249
A class for handling words, derived from Foam::string.
Definition: word.H:63
void operator=(const writeFile &)=delete
No copy assignment.
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:286
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:66
virtual autoPtr< OFstream > newFileAtTime(const word &name, scalar timeValue) const
Return autoPtr to a new file for a given time.
Definition: writeFile.C:110
virtual label charWidth() const
Return width of character stream output.
Definition: writeFile.C:311
label writePrecision_
Write precision.
Definition: writeFile.H:141
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Istream and Ostream manipulators taking arguments.
OBJstream os(runTime.globalPath()/outputName)
virtual autoPtr< OFstream > newFileAtStartTime(const word &name) const
Return autoPtr to a new file using the simulation start time.
Definition: writeFile.C:156
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:240
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:354
bool writeToFile_
Flag to enable/disable writing to file.
Definition: writeFile.H:146
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:43
Omanip< int > valueWidth(const label offset=0) const
Return the value width when writing to stream with optional offset.
Definition: writeFile.C:173
scalar startTime_
Start time value.
Definition: writeFile.H:168
bool useUserTime_
Flag to use the specified user time, e.g. CA deg instead of seconds. Default = true.
Definition: writeFile.H:163
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:121
bool updateHeader_
Flag to update the header, e.g. on mesh changes. Default is true.
Definition: writeFile.H:152
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Registry of regIOobjects.
string ext_
File extension; default = .dat.
Definition: writeFile.H:173
Base class for writing single files from the function objects.
Definition: writeFile.H:112
word fileName_
Name of file.
Definition: writeFile.H:131
const fileName prefix_
Prefix.
Definition: writeFile.H:126
Namespace for OpenFOAM.
An Ostream manipulator taking arguments.
Definition: IOmanip.H:44
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:334