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-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 Class
28  Foam::functionObjects::writeFile
29 
30 Description
31  Base class for writing single files from the function objects.
32 
33 Usage
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 
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 See also
64  - Foam::functionObject
65  - Foam::functionObjects::logFiles
66 
67 SourceFiles
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 
81 namespace Foam
82 {
83 namespace functionObjects
84 {
85 
86 /*---------------------------------------------------------------------------*\
87  Class functionObjects::writeFile Declaration
88 \*---------------------------------------------------------------------------*/
89 
90 class writeFile
91 {
92 protected:
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
126  scalar startTime_;
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;
152 
153  //- Reset internal file pointer to new file with new name
154  virtual void resetFile(const word& name);
155 
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 
164 public:
165 
166  //- Additional characters for writing
167  static label addChars;
168 
169 
170  // Constructors
171 
172  //- Construct from objectRegistry, prefix, fileName
173  writeFile
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
183  writeFile
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>
234  void writeHeaderValue
235  (
236  Ostream& os,
237  const string& property,
238  const Type& value
239  ) const;
240 };
241 
242 
243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244 
245 } // End namespace functionObjects
246 } // End namespace Foam
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #ifdef NoRepository
251  #include "writeFileTemplates.C"
252 #endif
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 #endif
257 
258 // ************************************************************************* //
Foam::functionObjects::writeFile::prefix_
const fileName prefix_
Prefix.
Definition: writeFile.H:129
Foam::functionObjects::writeFile::writeCurrentTime
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:308
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:236
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::functionObjects::writeFile::writtenHeader_
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:148
Foam::functionObjects::writeFile::fileObr_
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:126
writeFileTemplates.C
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
objectRegistry.H
Foam::functionObjects::writeFile::canWriteHeader
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:259
Foam::functionObjects::writeFile::baseFileDir
fileName baseFileDir() const
Return the base directory for output.
Definition: writeFile.C:50
Foam::functionObjects::writeFile::writeHeaderValue
void writeHeaderValue(Ostream &os, const string &property, const Type &value) const
Write a (commented) header property and value pair.
Definition: writeFileTemplates.C:32
Foam::functionObjects::writeFile::filePtr_
autoPtr< OFstream > filePtr_
File pointer.
Definition: writeFile.H:135
OFstream.H
Foam::functionObjects::writeFile::writeHeader
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:298
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::writeFile::writeToFile_
bool writeToFile_
Flag to enable/disable writing to file.
Definition: writeFile.H:141
Foam::functionObjects::writeFile::writeBreak
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:321
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:213
Foam::functionObjects::writeFile::startTime_
scalar startTime_
Start time value.
Definition: writeFile.H:155
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:145
Foam::functionObjects::writeFile::~writeFile
virtual ~writeFile()=default
Destructor.
IOmanip.H
Istream and Ostream manipulators taking arguments.
Foam::functionObjects::writeFile::fileName_
word fileName_
Name of file.
Definition: writeFile.H:132
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:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::functionObjects::writeFile::initStream
void initStream(Ostream &os) const
Initialise the output stream for writing.
Definition: writeFile.C:42
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::functionObjects::writeFile::operator=
void operator=(const writeFile &)=delete
No copy assignment.
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::functionObjects::writeFile::writeCommented
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:272
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:83
Foam::Omanip
An Ostream manipulator taking arguments.
Definition: IOmanip.H:50
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:171
Foam::functionObjects::writeFile::resetFile
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:137
Foam::functionObjects::writeFile::updateHeader_
bool updateHeader_
Definition: writeFile.H:145
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:119
Foam::functionObjects::writeFile::writeToFile
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:253
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::functionObjects::writeFile::baseTimeDir
fileName baseTimeDir() const
Return the base directory for the current time value.
Definition: writeFile.C:76
Foam::functionObjects::writeFile::useUserTime_
bool useUserTime_
Definition: writeFile.H:152
Foam::functionObjects::writeFile::charWidth
virtual label charWidth() const
Return width of character stream output.
Definition: writeFile.C:265
Foam::functionObjects::writeFile::writeTabbed
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:288