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.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  //- Write nValues of processor ids as CellData or PointData
206  //- (no-op in serial)
207  bool writeProcIDs(const label nValues);
208 
209 
210  // Other
211 
212  //- No copy construct
213  fileWriter(const fileWriter&) = delete;
214 
215  //- No copy assignment
216  void operator=(const fileWriter&) = delete;
217 
218 
219 public:
220 
221  // Constructors
222 
223  //- Construct from components
224  fileWriter
225  (
226  const vtk::fileTag contentType,
227  const vtk::outputOptions opts
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;
257 
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 
275 
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;
290 
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;
300 
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 #include "foamVtkFileWriterI.H"
352 
353 #ifdef NoRepository
354  #include "foamVtkFileWriter.txx"
355 #endif
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 #endif
360 
361 // ************************************************************************* //
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.
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.
File contents declared (VTKFile header written)
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.
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 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:68
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.