foamVtkFileWriter.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) 2018-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::vtk::fileWriter
28
29Description
30 Base class for VTK output writers that handle geometry and fields
31 (eg, vtp, vtu data).
32 These output formats are structured as DECLARED, FIELD_DATA, PIECE
33 followed by any CELL_DATA or POINT_DATA.
34
35 This writer base tracks these expected output states internally
36 to help avoid logic errors in the callers.
37
38 The FieldData element must be placed prior to writing any geometry
39 Piece. This moves the information to the front of the output file
40 for visibility and simplifies the logic when creating
41 multi-piece geometries.
42
43SourceFiles
44 foamVtkFileWriter.C
45 foamVtkFileWriterI.H
46 foamVtkFileWriterTemplates.C
47
48\*---------------------------------------------------------------------------*/
49
50#ifndef Foam_vtk_fileWriter_H
51#define Foam_vtk_fileWriter_H
52
53#include <fstream>
54#include "Enum.H"
55#include "UPstream.H"
57
58// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59
60namespace Foam
61{
62namespace vtk
63{
64
65/*---------------------------------------------------------------------------*\
66 Class vtk::fileWriter Declaration
67\*---------------------------------------------------------------------------*/
69class fileWriter
70{
71protected:
72
73 // Protected Member Data
74
75 //- Internal tracking of the output state.
76 enum outputState : uint8_t
77 {
78 CLOSED = 0,
83 CELL_DATA,
85 };
86
87 //- Names for the output state (for messages, not for file output).
88 static const Enum<outputState> stateNames;
89
90
91 //- The output state
93
94 //- The content type (PolyData, UnstructuredGrid ...)
96
97 //- Parallel writing (via master)
98 bool parallel_;
99
100 //- Requested output options
102
103 //- The number of CellData written for the Piece thus far.
104 label nCellData_;
105
106 //- The number of PointData written for the Piece thus far.
107 label nPointData_;
108
109 //- The output file name
111
112 //- The VTK formatter in use (only valid on master process)
114
115 //- The backend ostream in use (only opened on master process)
116 std::ofstream os_;
117
118
119 // Protected Member Functions
120
121 //- Verify that formatter in either allocated or not required
122 void checkFormatterValidity() const;
123
124 //- Generate message reporting bad writer state
125 Ostream& reportBadState(Ostream&, outputState expected) const;
126
127 //- Generate message reporting bad writer state
129
130 //- The backend ostream in use
131 inline std::ofstream& os() noexcept;
132
133 //- The VTK formatter in use. FatalError for off-processor.
134 inline vtk::formatter& format();
135
136 //- True if output state corresponds to the test state.
137 inline bool isState(outputState test) const noexcept;
138
139 //- True if output state does not correspond to the test state.
140 inline bool notState(outputState test) const noexcept;
141
142 //- Start of a field or DataArray output (legacy or non-legacy).
143 template<class Type>
144 void beginDataArray
145 (
146 const word& fieldName,
147 const label nValues
148 );
149
150 //- Flush formatter and end of DataArray output (non-legacy)
151 void endDataArray();
152
153 //- Start of a POINTS DataArray
154 void beginPoints(const label nPoints);
155
156 //- End of a POINTS DataArray
157 void endPoints();
158
159 //- Trigger change state to Piece. Resets nCellData_, nPointData_.
160 bool enter_Piece();
161
162 //- Explicitly end Piece output and switch to DECLARED state
163 // Ignored (no-op) if not currently in the PIECE state.
164 bool endPiece();
165
166 //- Trigger change state to CellData.
167 // Legacy requires both parameters. XML doesn't require either.
168 //
169 // \return True if the state changed
170 bool enter_CellData(label nEntries, label nFields);
171
172 //- Trigger change state to PointData
173 // Legacy requires both parameters. XML doesn't require either.
174 //
175 // \return True if the state changed
176 bool enter_PointData(label nEntries, label nFields);
177
178 //- Emit file footer (end data, end piece, end file)
179 bool exit_File();
180
181
182 // Field writing
183
184 //- Write uniform field content.
185 // No context checking (eg, file-open, CellData, PointData, etc)
186 // The value and count can be different on each processor
187 template<class Type>
188 void writeUniform
189 (
190 const word& fieldName,
191 const Type& val,
192 const label nValues
193 );
194
195 //- Write basic (primitive) field content
196 // No context checking (eg, file-open, CellData, PointData, etc)
197 template<class Type>
198 void writeBasicField
199 (
200 const word& fieldName,
201 const UList<Type>& field
202 );
203
204 //- Write nValues of processor ids as CellData (no-op in serial)
205 bool writeProcIDs(const label nValues);
206
207
208 // Other
209
210 //- No copy construct
211 fileWriter(const fileWriter&) = delete;
212
213 //- No copy assignment
214 void operator=(const fileWriter&) = delete;
215
216
217public:
218
219 // Constructors
220
221 //- Construct from components
223 (
224 const vtk::fileTag contentType,
225 const vtk::outputOptions opts
226 );
227
228
229 //- Destructor
230 virtual ~fileWriter();
231
232
233 // Member Functions
234
235 //- The content type
236 inline vtk::fileTag contentType() const noexcept;
237
238 //- The output options in use
239 inline vtk::outputOptions opts() const noexcept;
240
241 //- File extension for current format type.
242 inline word ext() const;
243
244 //- Commonly used query
245 inline bool legacy() const noexcept;
246
247 //- Parallel output requested?
248 inline bool parallel() const noexcept;
249
250 //- The output state in printable format
251 inline const word& state() const;
252
253 //- The current output file name
254 inline const fileName& output() const noexcept;
255
256
257 //- Open file for writing (creates parent directory).
258 // The file name is normally without an extension, this will be added
259 // according to the content-type and the output format (legacy/xml).
260 // If the file name has an extension, it will be used where if
261 // appropriate or changed to suit the format (legacy/xml) type.
262 // \note Expected calling states: (CLOSED).
263 virtual bool open
264 (
265 const fileName& file,
266 bool parallel = Pstream::parRun()
267 );
268
269 //- End the file contents and close the file after writing.
270 // \note Expected calling states: (PIECE | CELL_DATA | POINT_DATA).
271 void close();
272
273
274 //- Write file header (non-collective)
275 // \note Expected calling states: (OPENED)
276 virtual bool beginFile(std::string title = "");
277
278 //- Begin FieldData output section for specified number of fields.
279 // \param nFields is for legacy format only.
280 // When nFields=0, this a no-op for legacy format.
281 // \note Expected calling states: (OPENED | DECLARED).
282 bool beginFieldData(label nFields = 0);
283
284 //- Write mesh topology.
285 // Also writes the file header if not previously written.
286 // \note Must be called prior to writing CellData or PointData
287 virtual bool writeGeometry() = 0;
288
289
290 //- Begin CellData output section for specified number of fields.
291 // Must be called prior to writing any cell data fields.
292 // \param nFields is for legacy format only.
293 // When nFields=0, this a no-op for legacy format.
294 // \note Expected calling states: (PIECE | POINT_DATA).
295 //
296 // \return True if the state changed
297 virtual bool beginCellData(label nFields = 0) = 0;
298
299 //- Begin PointData for specified number of fields.
300 // Must be called prior to writing any point data fields.
301 // \param nFields is for legacy format only.
302 // When nFields=0, this a no-op for legacy format.
303 // \note Expected calling states: (PIECE | CELL_DATA).
304 //
305 // \return True if the state changed
306 virtual bool beginPointData(label nFields = 0) = 0;
307
308 //- Return the number of CellData written for the Piece thus far.
309 inline label nCellData() const noexcept;
310
311 //- Return the number of PointData written for the Piece thus far.
312 inline label nPointData() const noexcept;
313
314
315 //- Explicitly end FieldData output and switch to DECLARED state
316 // Ignored (no-op) if not currently in the FIELD_DATA state.
317 bool endFieldData();
318
319 //- Explicitly end CellData output and switch to PIECE state
320 // Ignored (no-op) if not currently in the CELL_DATA state.
321 bool endCellData();
322
323 //- Explicitly end PointData output and switch to PIECE state
324 // Ignored (no-op) if not currently in the POINT_DATA state.
325 bool endPointData();
326
327 //- Write "TimeValue" FieldData (name as per Catalyst output)
328 // Must be called within the FIELD_DATA state.
329 // \note As a convenience this can also be called from
330 // (OPENED | DECLARED) states, in which case it invokes
331 // beginFieldData(1) internally.
332 void writeTimeValue(scalar timeValue);
333};
334
335
336// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
337
338} // End namespace vtk
339} // End namespace Foam
340
341// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342
343#include "foamVtkFileWriterI.H"
344
345#ifdef NoRepository
347#endif
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350
351#endif
352
353// ************************************************************************* //
writer writeProcIDs()
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Inter-processor communications stream.
Definition: Pstream.H:63
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class for handling file names.
Definition: fileName.H:76
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
bool enter_Piece()
Trigger change state to Piece. Resets nCellData_, nPointData_.
bool enter_CellData(label nEntries, label nFields)
Trigger change state to CellData.
static const Enum< outputState > stateNames
Names for the output state (for messages, not for file output).
vtk::fileTag contentType() const noexcept
The content type.
bool isState(outputState test) const noexcept
True if output state corresponds to the test state.
fileName outputFile_
The output file name.
virtual bool beginPointData(label nFields=0)=0
Begin PointData for specified number of fields.
void writeUniform(const word &fieldName, const Type &val, const label nValues)
Write uniform field content.
vtk::outputOptions opts() const noexcept
The output options in use.
const word & state() const
The output state in printable format.
void beginPoints(const label nPoints)
Start of a POINTS DataArray.
bool legacy() const noexcept
Commonly used query.
virtual bool beginCellData(label nFields=0)=0
Begin CellData output section for specified number of fields.
const fileName & output() const noexcept
The current output file name.
bool endCellData()
Explicitly end CellData output and switch to PIECE state.
label nPointData() const noexcept
Return the number of PointData written for the Piece thus far.
label nCellData() const noexcept
Return the number of CellData written for the Piece thus far.
autoPtr< vtk::formatter > format_
The VTK formatter in use (only valid on master process)
std::ofstream & os() noexcept
The backend ostream in use.
void close()
End the file contents and close the file after writing.
bool parallel_
Parallel writing (via master)
void endPoints()
End of a POINTS DataArray.
void checkFormatterValidity() const
Verify that formatter in either allocated or not required.
void endDataArray()
Flush formatter and end of DataArray output (non-legacy)
bool exit_File()
Emit file footer (end data, end piece, end file)
bool parallel() const noexcept
Parallel output requested?
bool enter_PointData(label nEntries, label nFields)
Trigger change state to PointData.
void beginDataArray(const word &fieldName, const label nValues)
Start of a field or DataArray output (legacy or non-legacy).
void writeBasicField(const word &fieldName, const UList< Type > &field)
Write basic (primitive) field content.
label nCellData_
The number of CellData written for the Piece thus far.
bool endFieldData()
Explicitly end FieldData output and switch to DECLARED state.
bool endPointData()
Explicitly end PointData output and switch to PIECE state.
outputState state_
The output state.
void writeTimeValue(scalar timeValue)
Write "TimeValue" FieldData (name as per Catalyst output)
bool beginFieldData(label nFields=0)
Begin FieldData output section for specified number of fields.
bool notState(outputState test) const noexcept
True if output state does not correspond to the test state.
word ext() const
File extension for current format type.
label nPointData_
The number of PointData written for the Piece thus far.
vtk::outputOptions opts_
Requested output options.
std::ofstream os_
The backend ostream in use (only opened on master process)
virtual bool open(const fileName &file, bool parallel=Pstream::parRun())
Open file for writing (creates parent directory).
virtual bool beginFile(std::string title="")
Write file header (non-collective)
virtual bool writeGeometry()=0
Write mesh topology.
outputState
Internal tracking of the output state.
@ FIELD_DATA
Inside FieldData.
@ DECLARED
File contents declared (VTKFile header written)
@ POINT_DATA
Inside PointData.
@ OPENED
File is opened.
@ CELL_DATA
Inside CellData.
@ CLOSED
File is closed.
@ PIECE
Inside Piece (after geometry write)
Ostream & reportBadState(Ostream &, outputState expected) const
Generate message reporting bad writer state.
bool endPiece()
Explicitly end Piece output and switch to DECLARED state.
vtk::fileTag contentType_
The content type (PolyData, UnstructuredGrid ...)
vtk::formatter & format()
The VTK formatter in use. FatalError for off-processor.
Abstract class for a VTK output stream formatter.
Encapsulated combinations of output format options. This is primarily useful when defining the output...
A class for handling words, derived from Foam::string.
Definition: word.H:68
rDeltaTY field()
label nPoints
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:114
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223