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-2019 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 foamVtkFileWriter_H
49 #define foamVtkFileWriter_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 (master process)
112 
113  //- The backend ostream in use (master process)
114  std::ofstream os_;
115 
116 
117  // Protected Member Functions
118 
119  //- The backend ostream in use
120  inline std::ofstream& os();
121 
122  //- The VTK formatter in use
123  inline vtk::formatter& format();
124 
125  //- True if the output state corresponds to the test state.
126  inline bool isState(outputState test) const;
127 
128  //- True if the output state does not correspond to the test state.
129  inline bool notState(outputState test) const;
130 
131  //- Write uniform field content.
132  // No context checking (eg, file-open, CellData, PointData, etc)
133  template<class Type>
134  void writeUniform
135  (
136  const word& fieldName,
137  const Type& val,
138  const label nValues
139  );
140 
141 
142  //- Trigger change state to Piece. Resets nCellData_, nPointData_.
143  bool enter_Piece();
144 
145  //- Explicitly end Piece output and switch to DECLARED state
146  // Ignored (no-op) if not currently in the PIECE state.
147  bool endPiece();
148 
149  //- Trigger change state to CellData.
150  // Legacy requires both parameters. XML doesn't require either.
151  //
152  // \return True if the state changed
153  bool enter_CellData(label nEntries, label nFields);
154 
155  //- Trigger change state to PointData
156  // Legacy requires both parameters. XML doesn't require either.
157  //
158  // \return True if the state changed
159  bool enter_PointData(label nEntries, label nFields);
160 
161  //- Emit file footer (end data, end piece, end file)
162  bool exit_File();
163 
164 
165  //- No copy construct
166  fileWriter(const fileWriter&) = delete;
167 
168  //- No copy assignment
169  void operator=(const fileWriter&) = delete;
170 
171 
172 public:
173 
174  // Constructors
175 
176  //- Construct from components
177  fileWriter
178  (
181  );
182 
183 
184  //- Destructor
185  virtual ~fileWriter();
186 
187 
188  // Member Functions
189 
190  //- The content type
191  inline vtk::fileTag contentType() const;
192 
193  //- The output options in use
194  inline vtk::outputOptions opts() const;
195 
196  //- File extension for current format type.
197  inline word ext() const;
198 
199  //- Commonly used query
200  inline bool legacy() const;
201 
202  //- Parallel output requested?
203  inline bool parallel() const;
204 
205  //- The output state in printable format
206  inline const word& state() const;
207 
208  //- The current output file name
209  inline const fileName& output() const;
210 
211 
212  //- Open file for writing (creates parent directory).
213  // The file name is normally without an extension, this will be added
214  // according to the content-type and the output format (legacy/xml).
215  // If the file name has an extension, it will be used where if
216  // appropriate or changed to suit the format (legacy/xml) type.
217  // \note Expected calling states: (CLOSED).
218  bool open(const fileName& file, bool parallel=Pstream::parRun());
219 
220  //- End the file contents and close the file after writing.
221  // \note Expected calling states: (PIECE | CELL_DATA | POINT_DATA).
222  void close();
223 
224 
225  //- Write file header (non-collective)
226  // \note Expected calling states: (OPENED)
227  virtual bool beginFile(std::string title = "");
228 
229  //- Begin FieldData output section for specified number of fields.
230  // \param nFields is for legacy format only.
231  // When nFields=0, this a no-op for legacy format.
232  // \note Expected calling states: (OPENED | DECLARED).
233  bool beginFieldData(label nFields = 0);
234 
235  //- Write mesh topology.
236  // Also writes the file header if not previously written.
237  // \note Must be called prior to writing CellData or PointData
238  virtual bool writeGeometry() = 0;
239 
240 
241  //- Begin CellData output section for specified number of fields.
242  // Must be called prior to writing any cell data fields.
243  // \param nFields is for legacy format only.
244  // When nFields=0, this a no-op for legacy format.
245  // \note Expected calling states: (PIECE | POINT_DATA).
246  //
247  // \return True if the state changed
248  virtual bool beginCellData(label nFields = 0) = 0;
249 
250  //- Begin PointData for specified number of fields.
251  // Must be called prior to writing any point data fields.
252  // \param nFields is for legacy format only.
253  // When nFields=0, this a no-op for legacy format.
254  // \note Expected calling states: (PIECE | CELL_DATA).
255  //
256  // \return True if the state changed
257  virtual bool beginPointData(label nFields = 0) = 0;
258 
259  //- Return the number of CellData written for the Piece thus far.
260  inline label nCellData() const;
261 
262  //- Return the number of PointData written for the Piece thus far.
263  inline label nPointData() const;
264 
265 
266  //- Explicitly end FieldData output and switch to DECLARED state
267  // Ignored (no-op) if not currently in the FIELD_DATA state.
268  bool endFieldData();
269 
270  //- Explicitly end CellData output and switch to PIECE state
271  // Ignored (no-op) if not currently in the CELL_DATA state.
272  bool endCellData();
273 
274  //- Explicitly end PointData output and switch to PIECE state
275  // Ignored (no-op) if not currently in the POINT_DATA state.
276  bool endPointData();
277 
278  //- Write "TimeValue" FieldData (name as per Catalyst output)
279  // Must be called within the FIELD_DATA state.
280  // \note As a convenience this can also be called from
281  // (OPENED | DECLARED) states, in which case it invokes
282  // beginFieldData(1) internally.
283  void writeTimeValue(scalar timeValue);
284 
285 };
286 
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 } // End namespace vtk
291 } // End namespace Foam
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 #include "foamVtkFileWriterI.H"
296 
297 #ifdef NoRepository
299 #endif
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #endif
304 
305 // ************************************************************************* //
Foam::vtk::fileWriter::enter_Piece
bool enter_Piece()
Trigger change state to Piece. Resets nCellData_, nPointData_.
Definition: foamVtkFileWriter.C:51
Foam::vtk::outputOptions
Encapsulated combinations of output format options. This is primarily useful when defining the output...
Definition: foamVtkOutputOptions.H:59
Foam::vtk::fileWriter::parallel
bool parallel() const
Parallel output requested?
Definition: foamVtkFileWriterI.H:80
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:301
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:69
Foam::vtk::fileWriter::opts
vtk::outputOptions opts() const
The output options in use.
Definition: foamVtkFileWriterI.H:62
UPstream.H
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:96
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:410
Foam::vtk::fileWriter::writeTimeValue
void writeTimeValue(scalar timeValue)
Write "TimeValue" FieldData (name as per Catalyst output)
Definition: foamVtkFileWriter.C:446
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:35
Foam::UPstream::parRun
static bool & parRun()
Test if this a parallel run, or allow modify access.
Definition: UPstream.H:434
Foam::vtk::fileWriter::contentType
vtk::fileTag contentType() const
The content type.
Definition: foamVtkFileWriterI.H:56
Foam::vtk::fileWriter::beginFieldData
bool beginFieldData(label nFields=0)
Begin FieldData output section for specified number of fields.
Definition: foamVtkFileWriter.C:358
Foam::vtk::fileWriter::notState
bool notState(outputState test) const
True if the output state does not correspond to the test state.
Definition: foamVtkFileWriterI.H:48
Foam::vtk::fileWriter::os_
std::ofstream os_
The backend ostream in use (master process)
Definition: foamVtkFileWriter.H:113
Foam::vtk::fileWriter::beginFile
virtual bool beginFile(std::string title="")
Write file header (non-collective)
Definition: foamVtkFileWriter.C:317
Foam::vtk::fileWriter::os
std::ofstream & os()
The backend ostream in use.
Definition: foamVtkFileWriterI.H:30
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 (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::state_
outputState state_
The output state.
Definition: foamVtkFileWriter.H:98
Foam::vtk::fileWriter::nPointData
label nPointData() const
Return the number of PointData written for the Piece thus far.
Definition: foamVtkFileWriterI.H:104
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:240
Foam::vtk::fileWriter::opts_
outputOptions opts_
The requested output options.
Definition: foamVtkFileWriter.H:92
Foam::vtk::fileWriter::output
const fileName & output() const
The current output file name.
Definition: foamVtkFileWriterI.H:92
Foam::vtk::fileWriter::fileWriter
fileWriter(const fileWriter &)=delete
No copy construct.
Foam::vtk::fileWriter::beginPointData
virtual bool beginPointData(label nFields=0)=0
Begin PointData for specified number of fields.
Foam::vtk::fileWriter::nCellData
label nCellData() const
Return the number of CellData written for the Piece thus far.
Definition: foamVtkFileWriterI.H:98
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:135
Foam::vtk::fileWriter::operator=
void operator=(const fileWriter &)=delete
No copy assignment.
Foam::vtk::fileWriter::~fileWriter
virtual ~fileWriter()
Destructor.
Definition: foamVtkFileWriter.C:232
Foam::vtk::fileWriter::endPiece
bool endPiece()
Explicitly end Piece output and switch to DECLARED state.
Definition: foamVtkFileWriter.C:74
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:428
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
foamVtkFileWriterI.H
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:174
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::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:392
Foam::vtk::fileWriter::isState
bool isState(outputState test) const
True if the output state corresponds to the test state.
Definition: foamVtkFileWriterI.H:42
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
Enum.H