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 | Req'd | Dflt
51  writePrecision | Number of decimal points | label | no | <system dflt>
52  writeToFile | Flag to produce text file output | bool | no | true
53  useUserTime | Flag to use user time, e.g. degrees | bool | no | true
54  \endtable
55 
56 See also
57  - Foam::functionObject
58  - Foam::functionObjects::logFiles
59 
60 SourceFiles
61  writeFile.C
62 
63 \*---------------------------------------------------------------------------*/
64 
65 #ifndef functionObjects_writeFile_H
66 #define functionObjects_writeFile_H
67 
68 #include "objectRegistry.H"
69 #include "OFstream.H"
70 #include "IOmanip.H"
71 
72 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
73 
74 namespace Foam
75 {
76 namespace functionObjects
77 {
78 
79 /*---------------------------------------------------------------------------*\
80  Class functionObjects::writeFile Declaration
81 \*---------------------------------------------------------------------------*/
82 
83 class writeFile
84 {
85 protected:
86 
87  // Protected data
88 
89  //- Reference to the region objectRegistry
90  const objectRegistry& fileObr_;
91 
92  //- Prefix
93  const fileName prefix_;
94 
95  //- Name of file
96  word fileName_;
97 
98  //- File pointer
99  autoPtr<OFstream> filePtr_;
100 
101  //- Write precision
102  label writePrecision_;
103 
104  //- Flag to enable/disable writing to file
105  bool writeToFile_;
106 
107  //- Flag to update the header, e.g. on mesh changes.
108  //- Default is true.
109  bool updateHeader_;
110 
111  //- Flag to identify whether the header has been written
112  bool writtenHeader_;
113 
114  //- Flag to use the specified user time, e.g. CA deg instead
115  //- of seconds. Default = true
117 
118  //- Start time value
119  scalar startTime_;
120 
121 
122  // Protected Member Functions
123 
124  //- Initialise the output stream for writing
125  void initStream(Ostream& os) const;
126 
127  //- Return the base directory for output
129 
130  //- Return the base directory for the current time value
131  fileName baseTimeDir() const;
132 
133  //- Return autoPtr to a new file for a given time
135  (
136  const word& name,
137  scalar timeValue
138  ) const;
139 
140  //- Return autoPtr to a new file using the simulation start time
142  (
143  const word& name
144  ) const;
145 
146  //- Reset internal file pointer to new file with new name
147  virtual void resetFile(const word& name);
148 
149  //- Return the value width when writing to stream with optional offset
150  Omanip<int> valueWidth(const label offset = 0) const;
151 
152 
153  //- No copy assignment
154  void operator=(const writeFile&) = delete;
155 
156 
157 public:
158 
159  //- Additional characters for writing
160  static label addChars;
161 
162 
163  // Constructors
164 
165  //- Construct from objectRegistry, prefix, fileName
166  writeFile
167  (
168  const objectRegistry& obr,
169  const fileName& prefix,
170  const word& name = "undefined",
171  const bool writeToFile = true
172  );
173 
174  //- Construct from objectRegistry, prefix, fileName
175  //- and read options from dictionary
176  writeFile
177  (
178  const objectRegistry& obr,
179  const fileName& prefix,
180  const word& name,
181  const dictionary& dict,
182  const bool writeToFile = true
183  );
184 
185  //- Construct copy
186  writeFile(const writeFile& wf);
187 
188 
189  //- Destructor
190  virtual ~writeFile() = default;
191 
192 
193  // Member Functions
194 
195  //- Read
196  virtual bool read(const dictionary& dict);
197 
198  //- Return access to the file (if only 1)
199  virtual OFstream& file();
200 
201  //- Flag to allow writing to file
202  virtual bool writeToFile() const;
203 
204  //- Flag to allow writing the header
205  virtual bool canWriteHeader() const;
206 
207  //- Return width of character stream output
208  virtual label charWidth() const;
209 
210  //- Write a commented string to stream
211  virtual void writeCommented(Ostream& os, const string& str) const;
212 
213  //- Write a tabbed string to stream
214  virtual void writeTabbed(Ostream& os, const string& str) const;
215 
216  //- Write a commented header to stream
217  virtual void writeHeader(Ostream& os, const string& str) const;
218 
219  //- Write the current time to stream
220  virtual void writeCurrentTime(Ostream& os) const;
221 
222  //- Write a break marker to the stream
223  virtual void writeBreak(Ostream& os) const;
224 
225  //- Write a (commented) header property and value pair
226  template<class Type>
227  void writeHeaderValue
228  (
229  Ostream& os,
230  const string& property,
231  const Type& value
232  ) const;
233 };
234 
235 
236 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237 
238 } // End namespace functionObjects
239 } // End namespace Foam
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #ifdef NoRepository
244  #include "writeFileTemplates.C"
245 #endif
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 #endif
250 
251 // ************************************************************************* //
Foam::functionObjects::writeFile::prefix_
const fileName prefix_
Prefix.
Definition: writeFile.H:116
Foam::functionObjects::writeFile::writeCurrentTime
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:309
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::writeFile::file
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:237
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::functionObjects::writeFile::writtenHeader_
bool writtenHeader_
Flag to identify whether the header has been written.
Definition: writeFile.H:135
Foam::functionObjects::writeFile::fileObr_
const objectRegistry & fileObr_
Reference to the region objectRegistry.
Definition: writeFile.H:113
writeFileTemplates.C
Foam::functionObjects::writeFile::addChars
static label addChars
Additional characters for writing.
Definition: writeFile.H:183
Foam::functionObjects::writeFile::writePrecision_
label writePrecision_
Write precision.
Definition: writeFile.H:125
objectRegistry.H
Foam::functionObjects::writeFile::canWriteHeader
virtual bool canWriteHeader() const
Flag to allow writing the header.
Definition: writeFile.C:260
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:122
OFstream.H
Foam::functionObjects::writeFile::writeHeader
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:299
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:128
Foam::functionObjects::writeFile::writeBreak
virtual void writeBreak(Ostream &os) const
Write a break marker to the stream.
Definition: writeFile.C:322
Foam::functionObjects::writeFile::read
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:214
Foam::functionObjects::writeFile::startTime_
scalar startTime_
Start time value.
Definition: writeFile.H:142
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:146
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:119
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:121
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:87
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:273
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:84
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:172
Foam::functionObjects::writeFile::resetFile
virtual void resetFile(const word &name)
Reset internal file pointer to new file with new name.
Definition: writeFile.C:138
Foam::functionObjects::writeFile::updateHeader_
bool updateHeader_
Definition: writeFile.H:132
Foam::functionObjects::writeFile
Base class for writing single files from the function objects.
Definition: writeFile.H:106
Foam::functionObjects::writeFile::writeToFile
virtual bool writeToFile() const
Flag to allow writing to file.
Definition: writeFile.C:254
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:77
Foam::functionObjects::writeFile::useUserTime_
bool useUserTime_
Definition: writeFile.H:139
Foam::functionObjects::writeFile::charWidth
virtual label charWidth() const
Return width of character stream output.
Definition: writeFile.C:266
Foam::functionObjects::writeFile::writeTabbed
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:289