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-------------------------------------------------------------------------------
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
27Class
28 Foam::functionObjects::writeFile
29
30Description
31 Base class for writing single files from the function objects.
32
33Usage
34
35 \verbatim
36 <userDefinedSubDictName1>
37 {
38 // Mandatory and other optional entries
39 ...
40
41 // Optional (inherited) entries (runtime modifiable)
42 writePrecision 8;
43 writeToFile true;
44 useUserTime true;
45 }
46 \endverbatim
47
48 where the entries mean:
49 \table
50 Property | Description | Type | Reqd | Dflt
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
57Note
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
63See also
64 - Foam::functionObject
65 - Foam::functionObjects::logFiles
66
67SourceFiles
68 writeFile.C
69
70\*---------------------------------------------------------------------------*/
71
72#ifndef functionObjects_writeFile_H
73#define functionObjects_writeFile_H
74
75#include "objectRegistry.H"
76#include "OFstream.H"
77#include "IOmanip.H"
78
79// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80
81namespace Foam
82{
83namespace functionObjects
84{
85
86/*---------------------------------------------------------------------------*\
87 Class functionObjects::writeFile Declaration
88\*---------------------------------------------------------------------------*/
89
90class writeFile
91{
92protected:
93
94 // Protected data
95
96 //- Reference to the region objectRegistry
97 const objectRegistry& fileObr_;
98
99 //- Prefix
100 const fileName prefix_;
101
102 //- Name of file
103 word fileName_;
104
105 //- File pointer
106 autoPtr<OFstream> filePtr_;
107
108 //- Write precision
109 label writePrecision_;
110
111 //- Flag to enable/disable writing to file
112 bool writeToFile_;
113
114 //- Flag to update the header, e.g. on mesh changes.
115 //- Default is true.
116 bool updateHeader_;
117
118 //- Flag to identify whether the header has been written
120
121 //- Flag to use the specified user time, e.g. CA deg instead
122 //- of seconds. Default = true
123 bool useUserTime_;
124
125 //- Start time value
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
136
137 //- Return the base directory for the current time value
139
140 //- Return autoPtr to a new file for a given time
142 (
143 const word& name,
144 scalar timeValue
145 ) const;
146
147 //- Return autoPtr to a new file using the simulation start time
149 (
150 const word& name
151 ) const;
153 //- Reset internal file pointer to new file with new name
154 virtual void resetFile(const word& name);
156 //- Return the value width when writing to stream with optional offset
157 Omanip<int> valueWidth(const label offset = 0) const;
158
159
160 //- No copy assignment
161 void operator=(const writeFile&) = delete;
162
163
164public:
165
166 //- Additional characters for writing
167 static label addChars;
168
169
170 // Constructors
171
172 //- Construct from objectRegistry, prefix, fileName
174 (
175 const objectRegistry& obr,
176 const fileName& prefix,
177 const word& name = "undefined",
178 const bool writeToFile = true
179 );
180
181 //- Construct from objectRegistry, prefix, fileName
182 //- and read options from dictionary
184 (
185 const objectRegistry& obr,
186 const fileName& prefix,
187 const word& name,
188 const dictionary& dict,
189 const bool writeToFile = true
190 );
191
192 //- Construct copy
193 writeFile(const writeFile& wf);
194
195
196 //- Destructor
197 virtual ~writeFile() = default;
198
199
200 // Member Functions
201
202 //- Read
203 virtual bool read(const dictionary& dict);
204
205 //- Return access to the file (if only 1)
206 virtual OFstream& file();
207
208 //- Flag to allow writing to file
209 virtual bool writeToFile() const;
210
211 //- Flag to allow writing the header
212 virtual bool canWriteHeader() const;
213
214 //- Return width of character stream output
215 virtual label charWidth() const;
216
217 //- Write a commented string to stream
218 virtual void writeCommented(Ostream& os, const string& str) const;
219
220 //- Write a tabbed string to stream
221 virtual void writeTabbed(Ostream& os, const string& str) const;
222
223 //- Write a commented header to stream
224 virtual void writeHeader(Ostream& os, const string& str) const;
225
226 //- Write the current time to stream
227 virtual void writeCurrentTime(Ostream& os) const;
228
229 //- Write a break marker to the stream
230 virtual void writeBreak(Ostream& os) const;
231
232 //- Write a (commented) header property and value pair
233 template<class Type>
235 (
236 Ostream& os,
237 const string& property,
238 const Type& value
239 ) const;
240
241 //- Write a given value to stream with the space delimiter
242 template<class Type>
243 void writeValue
244 (
245 Ostream& os,
246 const Type& val
247 ) const;
248};
249
250
251// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
252
253} // End namespace functionObjects
254} // End namespace Foam
255
256// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257
258#ifdef NoRepository
259 #include "writeFileTemplates.C"
260#endif
261
262// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263
264#endif
265
266// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Output to file stream, using an OSstream.
Definition: OFstream.H:57
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
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
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
Base class for writing single files from the function objects.
Definition: writeFile.H:120
void operator=(const writeFile &)=delete
No copy assignment.
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
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
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
const fileName prefix_
Prefix.
Definition: writeFile.H:129
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
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:148
void writeValue(Ostream &os, const Type &val) const
Write a given value to stream with the space delimiter.
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 ~writeFile()=default
Destructor.
scalar startTime_
Start time value.
Definition: writeFile.H:155
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:134
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:126
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:256
Registry of regIOobjects.
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
dictionary dict