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