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 
126  // Protected Member Functions
127 
128  //- Initialise the output stream for writing
129  void initStream(Ostream& os) const;
130 
131  //- Return the base directory for output
132  fileName baseFileDir() const;
133 
134  //- Return the base directory for the current time value
135  fileName baseTimeDir() const;
137  //- Return the full path for the supplied file name
138  fileName filePath(const fileName& fName) const;
139 
140  //- Return autoPtr to a new file using file name
141  // Note: no check for if the file already exists
142  virtual autoPtr<OFstream> newFile(const fileName& fName) const;
143 
144  //- Return autoPtr to a new file for a given time
146  (
147  const word& name,
148  scalar timeValue
149  ) const;
150 
151  //- Return autoPtr to a new file using the simulation start time
153  (
154  const word& name
155  ) const;
156 
157  //- Reset internal file pointer to new file with new name
158  virtual void resetFile(const word& name);
159 
160  //- Return the value width when writing to stream with optional offset
161  Omanip<int> valueWidth(const label offset = 0) const;
162 
164  //- No copy assignment
165  void operator=(const writeFile&) = delete;
166 
167 
168  // Housekeeping
169 
170  //- Deprecated(2022-09) Return autoPtr to a new file for a given time
171  //
172  // \deprecated(2022-09) - use newFileAtTime function
173  FOAM_DEPRECATED_FOR(2022-09, "newFileAtTime function")
174  virtual autoPtr<OFstream> createFile
175  (
176  const word& name,
177  scalar timeValue
178  ) const
179  {
180  return newFileAtTime(name, timeValue);
181  }
182 
183  //- Deprecated(2022-09) Return autoPtr to a new file
184  //- using the simulation start time
185  //
186  // \deprecated(2022-09) - use newFileAtStartTime function
187  FOAM_DEPRECATED_FOR(2022-09, "newFileAtStartTime function")
188  virtual autoPtr<OFstream> createFile
189  (
190  const word& name
191  ) const
192  {
193  return newFileAtStartTime(name);
194  }
195 
196 
197 public:
198 
199  //- Additional characters for writing
200  static label addChars;
201 
202 
203  // Constructors
204 
205  //- Construct from objectRegistry, prefix, fileName
206  writeFile
207  (
208  const objectRegistry& obr,
209  const fileName& prefix,
210  const word& name = "undefined",
211  const bool writeToFile = true
212  );
213 
214  //- Construct from objectRegistry, prefix, fileName
215  //- and read options from dictionary
216  writeFile
217  (
218  const objectRegistry& obr,
219  const fileName& prefix,
220  const word& name,
221  const dictionary& dict,
222  const bool writeToFile = true
223  );
224 
225  //- Construct copy
226  writeFile(const writeFile& wf);
227 
228 
229  //- Destructor
230  virtual ~writeFile() = default;
231 
232 
233  // Member Functions
234 
235  //- Read
236  virtual bool read(const dictionary& dict);
237 
238  //- Return access to the file (if only 1)
239  virtual OFstream& file();
240 
241  //- Flag to allow writing to file
242  virtual bool writeToFile() const;
243 
244  //- Flag to allow writing to the file
245  virtual bool canWriteToFile() const;
246 
247  //- Flag to allow resetting the file
248  virtual bool canResetFile() const;
249 
250  //- Flag to allow writing the header
251  virtual bool canWriteHeader() const;
252 
253  //- Return width of character stream output
254  virtual label charWidth() const;
255 
256  //- Write a commented string to stream
257  virtual void writeCommented(Ostream& os, const string& str) const;
258 
259  //- Write a tabbed string to stream
260  virtual void writeTabbed(Ostream& os, const string& str) const;
262  //- Write a commented header to stream
263  virtual void writeHeader(Ostream& os, const string& str) const;
264 
265  //- Write the current time to stream
266  virtual void writeCurrentTime(Ostream& os) const;
267 
268  //- Write a break marker to the stream
269  virtual void writeBreak(Ostream& os) const;
270 
271  //- Write a (commented) header property and value pair
272  template<class Type>
273  void writeHeaderValue
274  (
275  Ostream& os,
276  const string& property,
277  const Type& value
278  ) const;
279 
280  //- Write a given value to stream with the space delimiter
281  template<class Type>
282  void writeValue
283  (
284  Ostream& os,
285  const Type& val
286  ) const;
287 };
288 
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 
292 } // End namespace functionObjects
293 } // End namespace Foam
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 #ifdef NoRepository
298  #include "writeFileTemplates.C"
299 #endif
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************************************************************* //
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:264
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:287
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:339
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:362
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.
writeFile(const objectRegistry &obr, const fileName &prefix, const word &name="undefined", const bool writeToFile=true)
Construct from objectRegistry, prefix, fileName.
Definition: writeFile.C:199
Output to file stream, using an OSstream.
Definition: OFstream.H:49
static label addChars
Additional characters for writing.
Definition: writeFile.H:274
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
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:293
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
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:299
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:244
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:281
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:306
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:241
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:349
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.
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:329