Loading...
Searching...
No Matches
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-2023 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.cxx
45 foamVtkFileWriter.txx
46 foamVtkFileWriterI.H
47
48\*---------------------------------------------------------------------------*/
49
50#ifndef Foam_vtk_fileWriter_H
51#define Foam_vtk_fileWriter_H
52
53#include <fstream>
54
55#include "Enum.H"
56#include "UPstream.H"
58
59// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60
61namespace Foam
62{
63namespace vtk
64{
66/*---------------------------------------------------------------------------*\
67 Class vtk::fileWriter Declaration
68\*---------------------------------------------------------------------------*/
69
70class fileWriter
71{
72protected:
73
74 // Protected Member Data
75
76 //- Internal tracking of the output state.
77 enum class outputState : uint8_t
78 {
79 CLOSED = 0,
83 PIECE,
84 CELL_DATA,
86 };
87
88 //- Names for the output state (for messages, not for file output).
89 static const Enum<outputState> stateNames;
90
91
92 //- The output state
95 //- The content type (PolyData, UnstructuredGrid ...)
97
98 //- Parallel writing (via master)
100
101 //- Requested output options
103
104 //- The number of CellData written for the Piece thus far.
105 label nCellData_;
106
107 //- The number of PointData written for the Piece thus far.
108 label nPointData_;
110 //- The output file name
112
113 //- The VTK formatter in use (only valid on master process)
115
116 //- The backend ostream in use (only opened on master process)
117 std::ofstream os_;
118
120 // Protected Member Functions
121
122 //- Verify that formatter in either allocated or not required
123 void checkFormatterValidity() const;
125 //- Generate message reporting bad writer state
126 Ostream& reportBadState(Ostream&, outputState expected) const;
127
128 //- Generate message reporting bad writer state
130
131 //- The backend ostream in use
132 inline std::ofstream& os() noexcept;
133
134 //- The VTK formatter in use. FatalError for off-processor.
135 inline vtk::formatter& format();
136
137 //- True if output state corresponds to the test state.
138 inline bool isState(outputState test) const noexcept;
139
140 //- True if output state does not correspond to the test state.
141 inline bool notState(outputState test) const noexcept;
143 //- Start of a field or DataArray output (legacy or non-legacy).
144 template<class Type>
145 void beginDataArray
146 (
147 const word& fieldName,
148 const label nValues
149 );
150
151 //- Flush formatter and end of DataArray output (non-legacy)
153
154 //- Start of a POINTS DataArray
155 void beginPoints(const label nPoints);
156
157 //- End of a POINTS DataArray
158 void endPoints();
159
160 //- Trigger change state to Piece. Resets nCellData_, nPointData_.
161 bool enter_Piece();
162
163 //- Explicitly end Piece output and switch to DECLARED state
164 // Ignored (no-op) if not currently in the PIECE state.
165 bool endPiece();
166
167 //- Trigger change state to CellData.
168 // Legacy requires both parameters. XML doesn't require either.
169 //
170 // \return True if the state changed
171 bool enter_CellData(label nEntries, label nFields);
172
173 //- Trigger change state to PointData
174 // Legacy requires both parameters. XML doesn't require either.
175 //
176 // \return True if the state changed
177 bool enter_PointData(label nEntries, label nFields);
179 //- Emit file footer (end data, end piece, end file)
180 bool exit_File();
181
182
183 // Field writing
184
185 //- Write uniform field content.
186 // No context checking (eg, file-open, CellData, PointData, etc)
187 // The value and count can be different on each processor
188 template<class Type>
189 void writeUniform
190 (
191 const word& fieldName,
192 const Type& val,
193 const label nValues
194 );
195
196 //- Write basic (primitive) field content
197 // No context checking (eg, file-open, CellData, PointData, etc)
198 template<class Type>
199 void writeBasicField
200 (
201 const word& fieldName,
202 const UList<Type>& field
203 );
204
205 //- Write nValues of processor ids as CellData or PointData
206 //- (no-op in serial)
207 bool writeProcIDs(const label nValues);
208
210 // Other
211
212 //- No copy construct
213 fileWriter(const fileWriter&) = delete;
214
215 //- No copy assignment
216 void operator=(const fileWriter&) = delete;
217
219public:
220
221 // Constructors
222
223 //- Construct from components
225 (
226 const vtk::fileTag contentType,
228 );
229
230
231 //- Destructor
232 virtual ~fileWriter();
233
234
235 // Member Functions
236
237 //- The content type
238 inline vtk::fileTag contentType() const noexcept;
239
240 //- The output options in use
241 inline vtk::outputOptions opts() const noexcept;
242
243 //- File extension for current format type.
244 inline word ext() const;
245
246 //- Commonly used query
247 inline bool legacy() const noexcept;
248
249 //- Parallel output requested?
250 inline bool parallel() const noexcept;
251
252 //- The output state in printable format
253 inline const word& state() const;
254
255 //- The current output file name
256 inline const fileName& output() const noexcept;
258
259 //- Open file for writing (creates parent directory).
260 // The file name is normally without an extension, this will be added
261 // according to the content-type and the output format (legacy/xml).
262 // If the file name has an extension, it will be used where if
263 // appropriate or changed to suit the format (legacy/xml) type.
264 // \note Expected calling states: (CLOSED).
265 virtual bool open
266 (
267 const fileName& file,
268 bool parallel = UPstream::parRun()
269 );
270
271 //- End the file contents and close the file after writing.
272 // \note Expected calling states: (PIECE | CELL_DATA | POINT_DATA).
273 void close();
274
276 //- Write file header (non-collective)
277 // \note Expected calling states: (OPENED)
278 virtual bool beginFile(std::string title = "");
279
280 //- Begin FieldData output section for specified number of fields.
281 // \param nFields is for legacy format only.
282 // When nFields=0, this a no-op for legacy format.
283 // \note Expected calling states: (OPENED | DECLARED).
284 bool beginFieldData(label nFields = 0);
285
286 //- Write mesh topology.
287 // Also writes the file header if not previously written.
288 // \note Must be called prior to writing CellData or PointData
289 virtual bool writeGeometry() = 0;
291
292 //- Begin CellData output section for specified number of fields.
293 // Must be called prior to writing any cell data fields.
294 // \param nFields is for legacy format only.
295 // When nFields=0, this a no-op for legacy format.
296 // \note Expected calling states: (PIECE | POINT_DATA).
297 //
298 // \return True if the state changed
299 virtual bool beginCellData(label nFields = 0) = 0;
301 //- Begin PointData for specified number of fields.
302 // Must be called prior to writing any point data fields.
303 // \param nFields is for legacy format only.
304 // When nFields=0, this a no-op for legacy format.
305 // \note Expected calling states: (PIECE | CELL_DATA).
306 //
307 // \return True if the state changed
308 virtual bool beginPointData(label nFields = 0) = 0;
309
310 //- True if output state corresponds to CELL_DATA
311 inline bool isCellData() const noexcept;
312
313 //- True if output state corresponds to POINT_DATA
314 inline bool isPointData() const noexcept;
315
316 //- The number of CellData written for the Piece thus far.
317 inline label nCellData() const noexcept;
318
319 //- The number of PointData written for the Piece thus far.
320 inline label nPointData() const noexcept;
321
322
323 //- Explicitly end FieldData output and switch to DECLARED state
324 // Ignored (no-op) if not currently in the FIELD_DATA state.
325 bool endFieldData();
326
327 //- Explicitly end CellData output and switch to PIECE state
328 // Ignored (no-op) if not currently in the CELL_DATA state.
329 bool endCellData();
330
331 //- Explicitly end PointData output and switch to PIECE state
332 // Ignored (no-op) if not currently in the POINT_DATA state.
333 bool endPointData();
334
335 //- Write "TimeValue" FieldData (name as per Catalyst output)
336 // Must be called within the FIELD_DATA state.
337 // \note As a convenience this can also be called from
338 // (OPENED | DECLARED) states, in which case it invokes
339 // beginFieldData(1) internally.
340 void writeTimeValue(scalar timeValue);
341};
342
343
344// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345
346} // End namespace vtk
347} // End namespace Foam
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351#include "foamVtkFileWriterI.H"
352
353#ifdef NoRepository
354 #include "foamVtkFileWriter.txx"
355#endif
356
357// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358
359#endif
360
361// ************************************************************************* //
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
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:89
Inter-processor communications stream.
Definition UPstream.H:69
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class for handling file names.
Definition fileName.H:75
bool enter_Piece()
Trigger change state to Piece. Resets nCellData_, nPointData_.
bool enter_CellData(label nEntries, label nFields)
Trigger change state to CellData.
bool isCellData() const noexcept
True if output state corresponds to CELL_DATA.
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 writeProcIDs(const label nValues)
Write nValues of processor ids as CellData or PointData (no-op in serial).
Ostream & reportBadState(Ostream &, outputState, outputState) const
Generate message reporting bad writer state.
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.
bool isPointData() const noexcept
True if output state corresponds to POINT_DATA.
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
The number of PointData written for the Piece thus far.
label nCellData() const noexcept
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.
Ostream & reportBadState(Ostream &, outputState expected) const
Generate message reporting bad writer 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=UPstream::parRun())
Open file for writing (creates parent directory).
virtual bool writeGeometry()=0
Write mesh topology.
fileWriter(const fileWriter &)=delete
No copy construct.
outputState
Internal tracking of the output state.
@ DECLARED
File contents declared (VTKFile header written).
@ PIECE
Inside Piece (after geometry write).
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.
virtual bool beginFile(std::string title="")
Write file header (non-collective).
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:66
rDeltaTY field()
label nPoints
Namespace for handling VTK output. Contains classes and functions for writing VTK file content.
fileTag
Some common XML tags for vtk files.
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265