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-2026 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.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"
57 #include "foamVtkOutputOptions.H"
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 namespace Foam
62 {
63 namespace vtk
64 {
65 
66 /*---------------------------------------------------------------------------*\
67  Class vtk::fileWriter Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class fileWriter
71 {
72 protected:
73 
74  // Protected Member Data
75 
76  //- Internal tracking of the output state.
77  enum class outputState : uint8_t
78  {
79  CLOSED = 0,
80  OPENED,
81  DECLARED,
82  FIELD_DATA,
83  PIECE,
84  CELL_DATA,
85  POINT_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
94 
95  //- The content type (PolyData, UnstructuredGrid ...)
97 
98  //- Parallel writing (via master)
99  bool parallel_;
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;
142 
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)
152  void endDataArray();
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);
178 
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  //- In parallel, write "procID" field (processor ids)
206  //- as CellData or PointData (depending on the current context).
207  // A no-op in serial.
208  bool writeProcIDs(label localSize);
209 
210  //- Write rank-local identity map
211  void writeLocalIDs(const word& fieldName, label localSize);
212 
213  //- Write global identity map
214  void writeGlobalIDs(const word& fieldName, label localSize);
215 
216 
217  // Other
218 
219  //- No copy construct
220  fileWriter(const fileWriter&) = delete;
221 
222  //- No copy assignment
223  void operator=(const fileWriter&) = delete;
224 
225 
226 public:
227 
228  // Constructors
229 
230  //- Construct from components
232 
233 
234  //- Destructor
235  virtual ~fileWriter();
236 
237 
238  // Member Functions
239 
240  //- The content type
241  inline vtk::fileTag contentType() const noexcept;
242 
243  //- The output options in use
244  inline vtk::outputOptions opts() const noexcept;
245 
246  //- File extension for current format type.
247  inline word ext() const;
248 
249  //- Commonly used query
250  inline bool legacy() const noexcept;
251 
252  //- Parallel output requested?
253  inline bool parallel() const noexcept;
254 
255  //- The output state in printable format
256  inline const word& state() const;
257 
258  //- The current output file name
259  inline const fileName& output() const noexcept;
260 
261 
262  //- Open file for writing (creates parent directory).
263  // The file name is normally without an extension, this will be added
264  // according to the content-type and the output format (legacy/xml).
265  // If the file name has an extension, it will be used where if
266  // appropriate or changed to suit the format (legacy/xml) type.
267  // \note Expected calling states: (CLOSED).
268  virtual bool open
269  (
270  const fileName& file,
271  bool parallel = UPstream::parRun()
272  );
273 
274  //- End the file contents and close the file after writing.
275  // \note Expected calling states: (PIECE | CELL_DATA | POINT_DATA).
276  void close();
277 
278 
279  //- Write file header (non-collective)
280  // \note Expected calling states: (OPENED)
281  virtual bool beginFile(std::string title = "");
282 
283  //- Begin FieldData output section for specified number of fields.
284  // \param nFields is for legacy format only.
285  // When nFields=0, this a no-op for legacy format.
286  // \note Expected calling states: (OPENED | DECLARED).
287  bool beginFieldData(label nFields = 0);
288 
289  //- Write mesh topology.
290  // Also writes the file header if not previously written.
291  // \note Must be called prior to writing CellData or PointData
292  virtual bool writeGeometry() = 0;
293 
294 
295  //- Begin CellData output section for specified number of fields.
296  // Must be called prior to writing any cell data fields.
297  // \param nFields is for legacy format only.
298  // When nFields=0, this a no-op for legacy format.
299  // \note Expected calling states: (PIECE | POINT_DATA).
300  //
301  // \return True if the state changed
302  virtual bool beginCellData(label nFields = 0) = 0;
303 
304  //- Begin PointData for specified number of fields.
305  // Must be called prior to writing any point data fields.
306  // \param nFields is for legacy format only.
307  // When nFields=0, this a no-op for legacy format.
308  // \note Expected calling states: (PIECE | CELL_DATA).
309  //
310  // \return True if the state changed
311  virtual bool beginPointData(label nFields = 0) = 0;
312 
313  //- True if output state corresponds to CELL_DATA
314  inline bool isCellData() const noexcept;
315 
316  //- True if output state corresponds to POINT_DATA
317  inline bool isPointData() const noexcept;
318 
319  //- The number of CellData written for the Piece thus far.
320  inline label nCellData() const noexcept;
321 
322  //- The number of PointData written for the Piece thus far.
323  inline label nPointData() const noexcept;
324 
325 
326  //- Explicitly end FieldData output and switch to DECLARED state
327  // Ignored (no-op) if not currently in the FIELD_DATA state.
328  bool endFieldData();
329 
330  //- Explicitly end CellData output and switch to PIECE state
331  // Ignored (no-op) if not currently in the CELL_DATA state.
332  bool endCellData();
333 
334  //- Explicitly end PointData output and switch to PIECE state
335  // Ignored (no-op) if not currently in the POINT_DATA state.
336  bool endPointData();
337 
338  //- Write "TimeValue" FieldData (name as per Catalyst output)
339  // Must be called within the FIELD_DATA state.
340  // \note As a convenience this can also be called from
341  // (OPENED | DECLARED) states, in which case it invokes
342  // beginFieldData(1) internally.
343  void writeTimeValue(scalar timeValue);
344 };
345 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 } // End namespace vtk
350 } // End namespace Foam
351 
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
353 
354 #include "foamVtkFileWriterI.H"
355 
356 #ifdef NoRepository
357  #include "foamVtkFileWriter.txx"
358 #endif
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 #endif
363 
364 // ************************************************************************* //
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.
bool writeProcIDs(label localSize)
In parallel, write "procID" field (processor ids) as CellData or PointData (depending on the current ...
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.
Definition: FixedList.H:920
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.
void writeGlobalIDs(const word &fieldName, label localSize)
Write global identity map.
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.
File contents declared (VTKFile header written)
bool enter_Piece()
Trigger change state to Piece. Resets nCellData_, nPointData_.
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.
virtual bool open(const fileName &file, bool parallel=UPstream::parRun())
Open file for writing (creates parent directory).
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: scalarImpl.H:265
bool beginFieldData(label nFields=0)
Begin FieldData output section for specified number of fields.
bool isPointData() const noexcept
True if output state corresponds to POINT_DATA.
void beginPoints(const label nPoints)
Start of a POINTS DataArray.
void writeLocalIDs(const word &fieldName, label localSize)
Write rank-local identity map.
void writeUniform(const word &fieldName, const Type &val, const label nValues)
Write uniform field content.
static const Enum< outputState > stateNames
Names for the output state (for messages, not for file output).
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:153
virtual bool beginFile(std::string title="")
Write file header (non-collective)
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.
Inside Piece (after geometry write)
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:69
const word & state() const
The output state in printable format.
Namespace for OpenFOAM.
virtual bool beginCellData(label nFields=0)=0
Begin CellData output section for specified number of fields.