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.
75  {
76  CLOSED = 0,
78  DECLARED,
79  FIELD_DATA,
80  PIECE,
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()
Definition: foamVtkFileWriter.C:82
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:55
Foam::vtk::fileWriter::writeGeometry
virtual bool writeGeometry()=0
foamVtkFileWriterTemplates.C
Foam::vtk::fileWriter
Base class for VTK output writers that handle geometry and fields (eg, vtp, vtu data)....
Definition: foamVtkFileWriter.H:62
Foam::Enum< outputState >
Foam::vtk::fileWriter::close
void close()
Definition: foamVtkFileWriter.C:370
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::vtk::fileWriter::legacy
bool legacy() const
Definition: foamVtkFileWriterI.H:67
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::vtk::fileWriter::opts
vtk::outputOptions opts() const
Definition: foamVtkFileWriterI.H:55
UPstream.H
Foam::vtk::fileWriter::beginDataArray
void beginDataArray(const word &fieldName, const label nValues)
Definition: foamVtkFileWriterTemplates.C:28
Foam::vtk::fileWriter::CELL_DATA
@ CELL_DATA
Inside CellData.
Definition: foamVtkFileWriter.H:76
Foam::vtk::fileWriter::enter_CellData
bool enter_CellData(label nEntries, label nFields)
Definition: foamVtkFileWriter.C:125
Foam::vtk::fileWriter::output
const fileName & output() const noexcept
Definition: foamVtkFileWriterI.H:85
Foam::vtk::fileWriter::outputFile_
fileName outputFile_
Definition: foamVtkFileWriter.H:103
Foam::vtk::fileWriter::endCellData
bool endCellData()
Definition: foamVtkFileWriter.C:475
Foam::vtk::fileWriter::writeTimeValue
void writeTimeValue(scalar timeValue)
Definition: foamVtkFileWriter.C:511
Foam::vtk::fileWriter::beginCellData
virtual bool beginCellData(label nFields=0)=0
Foam::vtk::fileWriter::writeUniform
void writeUniform(const word &fieldName, const Type &val, const label nValues)
Definition: foamVtkFileWriterTemplates.C:82
Foam::vtk::fileWriter::contentType
vtk::fileTag contentType() const
Definition: foamVtkFileWriterI.H:49
Foam::vtk::fileWriter::nPointData
label nPointData() const noexcept
Definition: foamVtkFileWriterI.H:97
Foam::vtk::fileWriter::os
std::ofstream & os() noexcept
Definition: foamVtkFileWriterI.H:23
Foam::vtk::fileWriter::beginFieldData
bool beginFieldData(label nFields=0)
Definition: foamVtkFileWriter.C:425
Foam::vtk::fileWriter::os_
std::ofstream os_
Definition: foamVtkFileWriter.H:109
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Definition: foamVtkFileWriter.C:386
Foam::vtk::fileWriter::notState
bool notState(outputState test) const noexcept
Definition: foamVtkFileWriterI.H:41
Foam::vtk::fileWriter::stateNames
static const Enum< outputState > stateNames
Definition: foamVtkFileWriter.H:81
Foam::vtk::fileWriter::format_
autoPtr< vtk::formatter > format_
Definition: foamVtkFileWriter.H:106
Foam::vtk::fileWriter::nPointData_
label nPointData_
Definition: foamVtkFileWriter.H:100
Foam::vtk::fileWriter::nCellData
label nCellData() const noexcept
Definition: foamVtkFileWriterI.H:91
Foam::vtk::fileWriter::state_
outputState state_
Definition: foamVtkFileWriter.H:94
Foam::vtk::fileWriter::parallel
bool parallel() const noexcept
Definition: foamVtkFileWriterI.H:73
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
Foam::vtk::fileWriter::FIELD_DATA
@ FIELD_DATA
Inside FieldData.
Definition: foamVtkFileWriter.H:74
Foam::vtk::fileWriter::open
bool open(const fileName &file, bool parallel=Pstream::parRun())
Definition: foamVtkFileWriter.C:311
Foam::vtk::fileWriter::opts_
outputOptions opts_
Definition: foamVtkFileWriter.H:88
Foam::vtk::fileWriter::beginPointData
virtual bool beginPointData(label nFields=0)=0
Foam::vtk::fileWriter::contentType_
vtk::fileTag contentType_
Definition: foamVtkFileWriter.H:85
Foam::vtk::fileWriter::enter_PointData
bool enter_PointData(label nEntries, label nFields)
Definition: foamVtkFileWriter.C:162
Foam::vtk::fileWriter::endPiece
bool endPiece()
Definition: foamVtkFileWriter.C:103
Foam::vtk::fileWriter::isState
bool isState(outputState test) const noexcept
Definition: foamVtkFileWriterI.H:35
field
rDeltaTY field()
Foam::vtk::fileWriter::checkFormatterValidity
void checkFormatterValidity() const
Definition: foamVtkFileWriter.C:45
Foam::vtk::fileWriter::writeBasicField
void writeBasicField(const word &fieldName, const UList< Type > &field)
Definition: foamVtkFileWriterTemplates.C:112
Foam::vtk::fileWriter::parallel_
bool parallel_
Definition: foamVtkFileWriter.H:91
Foam::vtk::fileWriter::DECLARED
@ DECLARED
File contents declared (VTKFile header written)
Definition: foamVtkFileWriter.H:73
Foam::vtk::fileWriter::endPointData
bool endPointData()
Definition: foamVtkFileWriter.C:493
Foam::vtk::fileTag
fileTag
Definition: foamVtkCore.H:109
Foam
Definition: atmBoundaryLayer.C:26
Foam::vtk::fileWriter::OPENED
@ OPENED
File is opened.
Definition: foamVtkFileWriter.H:72
foamVtkOutputOptions.H
Foam::vtk::fileWriter::state
const word & state() const
Definition: foamVtkFileWriterI.H:79
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
Foam::vtk::fileWriter::CLOSED
@ CLOSED
File is closed.
Definition: foamVtkFileWriter.H:71
Foam::vtk::fileWriter::PIECE
@ PIECE
Inside Piece (after geometry write)
Definition: foamVtkFileWriter.H:75
Foam::vtk::fileWriter::exit_File
bool exit_File()
Definition: foamVtkFileWriter.C:248
Foam::vtk::fileWriter::reportBadState
Ostream & reportBadState(Ostream &, outputState expected) const
Definition: foamVtkFileWriter.C:58
Foam::Pstream
Inter-processor communications stream.
Definition: Pstream.H:52
Foam::vtk::fileWriter::endPoints
void endPoints()
Definition: foamVtkFileWriter.C:232
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:99
Foam::vtk::fileWriter::writeProcIDs
bool writeProcIDs(const label nValues)
Definition: foamVtkFileWriter.C:538
Foam::vtk::fileWriter::POINT_DATA
@ POINT_DATA
Inside PointData.
Definition: foamVtkFileWriter.H:77
Foam::vtk::fileWriter::nCellData_
label nCellData_
Definition: foamVtkFileWriter.H:97
Foam::vtk::fileWriter::endDataArray
void endDataArray()
Definition: foamVtkFileWriter.C:199
Foam::vtk::fileWriter::format
vtk::formatter & format()
Definition: foamVtkFileWriterI.H:29
Foam::vtk::fileWriter::outputState
outputState
Definition: foamVtkFileWriter.H:69
Foam::vtk::fileWriter::endFieldData
bool endFieldData()
Definition: foamVtkFileWriter.C:457
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::vtk::fileWriter::ext
word ext() const
Definition: foamVtkFileWriterI.H:61
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:64
Foam::vtk::fileWriter::beginPoints
void beginPoints(const label nPoints)
Definition: foamVtkFileWriter.C:209
Enum.H