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 -------------------------------------------------------------------------------
10 License
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 
26 Class
27  Foam::vtk::fileWriter
28 
29 Description
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 
43 SourceFiles
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"
56 #include "foamVtkOutputOptions.H"
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 namespace vtk
63 {
64 
65 /*---------------------------------------------------------------------------*\
66  Class vtk::fileWriter Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class fileWriter
70 {
71 protected:
72 
73  // Protected Member Data
74 
75  //- Internal tracking of the output state.
76  enum outputState : uint8_t
77  {
78  CLOSED = 0,
82  PIECE,
83  CELL_DATA,
84  POINT_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_;
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 
119  // Protected Member Functions
120 
121  //- Verify that formatter in either allocated or not required
122  void checkFormatterValidity() const;
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 or PointData
205  //- (no-op in serial)
206  bool writeProcIDs(const label nValues);
207 
208 
209  // Other
210 
211  //- No copy construct
212  fileWriter(const fileWriter&) = delete;
213 
214  //- No copy assignment
215  void operator=(const fileWriter&) = delete;
216 
217 
218 public:
219 
220  // Constructors
221 
222  //- Construct from components
223  fileWriter
224  (
225  const vtk::fileTag contentType,
226  const vtk::outputOptions opts
227  );
228 
229 
230  //- Destructor
231  virtual ~fileWriter();
232 
233 
234  // Member Functions
235 
236  //- The content type
237  inline vtk::fileTag contentType() const noexcept;
238 
239  //- The output options in use
240  inline vtk::outputOptions opts() const noexcept;
241 
242  //- File extension for current format type.
243  inline word ext() const;
244 
245  //- Commonly used query
246  inline bool legacy() const noexcept;
247 
248  //- Parallel output requested?
249  inline bool parallel() const noexcept;
250 
251  //- The output state in printable format
252  inline const word& state() const;
253 
254  //- The current output file name
255  inline const fileName& output() const noexcept;
256 
257 
258  //- Open file for writing (creates parent directory).
259  // The file name is normally without an extension, this will be added
260  // according to the content-type and the output format (legacy/xml).
261  // If the file name has an extension, it will be used where if
262  // appropriate or changed to suit the format (legacy/xml) type.
263  // \note Expected calling states: (CLOSED).
264  virtual bool open
265  (
266  const fileName& file,
267  bool parallel = UPstream::parRun()
268  );
269 
270  //- End the file contents and close the file after writing.
271  // \note Expected calling states: (PIECE | CELL_DATA | POINT_DATA).
272  void close();
273 
274 
275  //- Write file header (non-collective)
276  // \note Expected calling states: (OPENED)
277  virtual bool beginFile(std::string title = "");
278 
279  //- Begin FieldData output section for specified number of fields.
280  // \param nFields is for legacy format only.
281  // When nFields=0, this a no-op for legacy format.
282  // \note Expected calling states: (OPENED | DECLARED).
283  bool beginFieldData(label nFields = 0);
284 
285  //- Write mesh topology.
286  // Also writes the file header if not previously written.
287  // \note Must be called prior to writing CellData or PointData
288  virtual bool writeGeometry() = 0;
289 
290 
291  //- Begin CellData output section for specified number of fields.
292  // Must be called prior to writing any cell data fields.
293  // \param nFields is for legacy format only.
294  // When nFields=0, this a no-op for legacy format.
295  // \note Expected calling states: (PIECE | POINT_DATA).
296  //
297  // \return True if the state changed
298  virtual bool beginCellData(label nFields = 0) = 0;
299 
300  //- Begin PointData for specified number of fields.
301  // Must be called prior to writing any point data fields.
302  // \param nFields is for legacy format only.
303  // When nFields=0, this a no-op for legacy format.
304  // \note Expected calling states: (PIECE | CELL_DATA).
305  //
306  // \return True if the state changed
307  virtual bool beginPointData(label nFields = 0) = 0;
308 
309  //- True if output state corresponds to CELL_DATA
310  inline bool isCellData() const noexcept;
311 
312  //- True if output state corresponds to POINT_DATA
313  inline bool isPointData() const noexcept;
314 
315  //- The number of CellData written for the Piece thus far.
316  inline label nCellData() const noexcept;
317 
318  //- The number of PointData written for the Piece thus far.
319  inline label nPointData() const noexcept;
320 
321 
322  //- Explicitly end FieldData output and switch to DECLARED state
323  // Ignored (no-op) if not currently in the FIELD_DATA state.
324  bool endFieldData();
325 
326  //- Explicitly end CellData output and switch to PIECE state
327  // Ignored (no-op) if not currently in the CELL_DATA state.
328  bool endCellData();
329 
330  //- Explicitly end PointData output and switch to PIECE state
331  // Ignored (no-op) if not currently in the POINT_DATA state.
332  bool endPointData();
333 
334  //- Write "TimeValue" FieldData (name as per Catalyst output)
335  // Must be called within the FIELD_DATA state.
336  // \note As a convenience this can also be called from
337  // (OPENED | DECLARED) states, in which case it invokes
338  // beginFieldData(1) internally.
339  void writeTimeValue(scalar timeValue);
340 };
341 
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
345 } // End namespace vtk
346 } // End namespace Foam
347 
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349 
350 #include "foamVtkFileWriterI.H"
351 
352 #ifdef NoRepository
354 #endif
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 #endif
359 
360 // ************************************************************************* //
bool notState(outputState test) const noexcept
True if output state does not correspond to the test state.
bool parallel() const noexcept
Parallel output requested?
autoPtr< vtk::formatter > format_
The VTK formatter in use (only valid on master process)
rDeltaTY field()
A class for handling file names.
Definition: fileName.H:72
vtk::fileTag contentType_
The content type (PolyData, UnstructuredGrid ...)
label nCellData() const noexcept
The number of CellData written for the Piece thus far.
void checkFormatterValidity() const
Verify that formatter in either allocated or not required.
bool isCellData() const noexcept
True if output state corresponds to CELL_DATA.
Abstract class for a VTK output stream formatter.
vtk::formatter & format()
The VTK formatter in use. FatalError for off-processor.
label nCellData_
The number of CellData written for the Piece thus far.
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data). These output formats are structured as DECLARED, FIELD_DATA, PIECE followed by any CELL_DATA or POINT_DATA.
vtk::outputOptions opts_
Requested output options.
void endDataArray()
Flush formatter and end of DataArray output (non-legacy)
void writeBasicField(const word &fieldName, const UList< Type > &field)
Write basic (primitive) field content.
bool enter_CellData(label nEntries, label nFields)
Trigger change state to CellData.
bool enter_PointData(label nEntries, label nFields)
Trigger change state to PointData.
vtk::outputOptions opts() const noexcept
The output options in use.
File contents declared (VTKFile header written)
void writeTimeValue(scalar timeValue)
Write "TimeValue" FieldData (name as per Catalyst output)
outputState
Internal tracking of the output state.
virtual bool beginPointData(label nFields=0)=0
Begin PointData for specified number of fields.
label nPointData_
The number of PointData written for the Piece thus far.
bool isState(outputState test) const noexcept
True if output state corresponds to the test state.
bool endFieldData()
Explicitly end FieldData output and switch to DECLARED state.
bool parallel_
Parallel writing (via master)
void endPoints()
End of a POINTS DataArray.
bool legacy() const noexcept
Commonly used query.
outputState state_
The output state.
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Ostream & reportBadState(Ostream &, outputState expected) const
Generate message reporting bad writer state.
bool enter_Piece()
Trigger change state to Piece. Resets nCellData_, nPointData_.
bool writeProcIDs(const label nValues)
Write nValues of processor ids as CellData or PointData (no-op in serial)
word ext() const
File extension for current format type.
A class for handling words, derived from Foam::string.
Definition: word.H:63
label nPoints
virtual bool writeGeometry()=0
Write mesh topology.
bool endCellData()
Explicitly end CellData output and switch to PIECE state.
bool endPointData()
Explicitly end PointData output and switch to PIECE state.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
vtk::fileTag contentType() const noexcept
The content type.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
virtual bool open(const fileName &file, bool parallel=UPstream::parRun())
Open file for writing (creates parent directory).
bool beginFieldData(label nFields=0)
Begin FieldData output section for specified number of fields.
virtual bool beginFile(std::string title="")
Write file header (non-collective)
bool isPointData() const noexcept
True if output state corresponds to POINT_DATA.
void beginPoints(const label nPoints)
Start of a POINTS DataArray.
void writeUniform(const word &fieldName, const Type &val, const label nValues)
Write uniform field content.
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:122
static const Enum< outputState > stateNames
Names for the output state (for messages, not for file output).
void close()
End the file contents and close the file after writing.
bool endPiece()
Explicitly end Piece output and switch to DECLARED state.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
fileName outputFile_
The output file name.
label nPointData() const noexcept
The number of PointData written for the Piece thus far.
const fileName & output() const noexcept
The current output file name.
std::ofstream & os() noexcept
The backend ostream in use.
std::ofstream os_
The backend ostream in use (only opened on master process)
bool exit_File()
Emit file footer (end data, end piece, end file)
void beginDataArray(const word &fieldName, const label nValues)
Start of a field or DataArray output (legacy or non-legacy).
Inter-processor communications stream.
Definition: UPstream.H:60
const word & state() const
The output state in printable format.
Inside Piece (after geometry write)
Namespace for OpenFOAM.
virtual bool beginCellData(label nFields=0)=0
Begin CellData output section for specified number of fields.