Loading...
Searching...
No Matches
foamVtmWriter.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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::vtk::vtmWriter
28
29Description
30 Provides a means of accumulating file entries for generating
31 a vtkMultiBlockDataSet (.vtm) file.
32
33 For example, to generate the following content:
34 \verbatim
35 <?xml version='1.0'?>
36 <VTKFile type='vtkMultiBlockDataSet' ...>
37 <vtkMultiBlockDataSet>
38 <DataSet name='internal' file='internal.vtu' />
39 <Block name='boundary'>
40 <DataSet name='inlet' file='boundary/inlet.vtp' />
41 <DataSet name='outlet' file='boundary/outlet.vtp' />
42 </Block>
43 </vtkMultiBlockDataSet>
44 <FieldData>
45 <DataArray type='Float32' Name='TimeValue' ...>
46 12.345
47 </DataArray>
48 </FieldData>
49 </VTKFile>
50 \endverbatim
51
52 The following code would be used:
53 \code
54 vtm.clear();
55 vtm.setTime(12.345);
56
57 vtm.append("internal", "internal.vtu");
58
59 vtm.beginBlock("boundary");
60 vtm.append("boundary/inlet.vtp");
61 vtm.append("boundary/outlet.vtp");
62
63 vtm.write("outputName");
64 \endcode
65
66SourceFiles
67 foamVtmWriter.cxx
68 foamVtmWriterI.H
69
70\*---------------------------------------------------------------------------*/
71
72#ifndef Foam_vtk_vtmWriter_H
73#define Foam_vtk_vtmWriter_H
74
76#include "DynamicList.H"
77#include <ostream>
78
79// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80
81namespace Foam
82{
83
84// Forward declarations
85class Time;
86class OSstream;
87
88namespace vtk
89{
91/*---------------------------------------------------------------------------*\
92 Class vtk::vtmWriter Declaration
93\*---------------------------------------------------------------------------*/
94
95class vtmWriter
96{
97 //- Simple structure for containing entries
98 struct vtmEntry
99 {
100 enum entryType : char
101 {
102 NONE = 0,
103 DATA = 'D',
104 BEGIN_BLOCK = '{', END_BLOCK = '}',
105 };
106
107 //- The entry type (none, node or leaf begin/end)
108 char type_;
109
110 //- The node data type (eg, MultiBlock, PolyData, ...)
111 vtk::fileTag content_;
112
113 //- The 'name' entry (to describe block or data)
114 string name_;
115
116 //- The 'file' entry (data only)
117 fileName file_;
118
119 // Constructors
120
121 vtmEntry(const vtmEntry&) = default;
122 vtmEntry(vtmEntry&&) = default;
123 vtmEntry& operator=(const vtmEntry&) = default;
124 vtmEntry& operator=(vtmEntry&&) = default;
125
126 //- Default construct
127 vtmEntry()
128 :
129 type_(entryType::NONE),
130 content_(vtk::fileTag::UNKNOWN)
131 {}
132
133 //- Construct from components
134 vtmEntry
135 (
136 int what,
137 const string& name,
138 const fileName& file,
140 )
141 :
142 type_(what), content_(content), name_(name), file_(file)
143 {}
144
145
146 // Factory Methods
147
148 //- Begin a named block
149 static vtmEntry block(const string& name)
150 {
151 return vtmEntry(entryType::BEGIN_BLOCK, name, "");
152 }
153
154 //- End a block
155 static vtmEntry endblock()
156 {
157 return vtmEntry(entryType::END_BLOCK, "", "");
158 }
159
160 //- Specify a data entry
161 static vtmEntry entry
162 (
163 const fileName& file,
165 )
166 {
167 return vtmEntry(entryType::DATA, "", file, content);
168 }
169
170 //- Specify a named data entry
171 static vtmEntry entry
172 (
173 const string& name,
174 const fileName& file,
176 )
177 {
178 return vtmEntry(entryType::DATA, name, file, content);
179 }
180
181
182 // Member Functions
183
184 //- Test the type
185 bool isType(entryType what) const noexcept
186 {
187 return (type_ == what);
188 }
189
190 //- Change the content type for a node
191 bool setDataType(vtk::fileTag dataType) noexcept
192 {
193 if (type_ == entryType::DATA)
194 {
195 content_ = dataType;
196 return true;
197 }
198 else
199 {
200 return false;
201 }
202 }
203
204 //- Reset to NONE
205 void clear();
206
207 //- True if the entry is good.
208 bool good() const noexcept;
209
210 //- Output valid entry as XML
211 bool write(vtk::formatter& format) const;
212 };
213
214
215 // Private Member Data
216
217 //- A vtm file entry: begin/end block, dataset
218 DynamicList<vtmEntry> entries_;
219
220 //- LIFO stack of current block names
221 DynamicList<word> blocks_;
222
223 //- TimeValue for FieldData
224 scalar timeValue_;
225
226 //- Auto-generate names from 'file' entry?
227 bool autoName_;
228
229 //- Has a TimeValue for FieldData?
230 bool hasTime_;
231
232
233 // Private Member Functions
234
235 //- Remove NONE entries
236 bool pruneEmpty();
237
238 //- Remove empty blocks
239 bool pruneEmptyBlocks();
240
241 //- Collapse block if it has a single dataset and the names allow it
242 bool collapseBlocks();
243
244 //- Add a file with specified file and data types.
245 //- The name is either empty or created with autoName
246 // \return True if file is non-empty
247 template
248 <
249 vtk::fileTag FileType,
250 vtk::fileTag DataType = FileType
251 >
252 inline bool appendTyped(const fileName& file);
253
254 //- Add a file with name, specified file and data types.
255 // \return True if file is non-empty
256 template
257 <
258 vtk::fileTag FileType,
259 vtk::fileTag DataType = FileType
260 >
261 inline bool appendTyped(const word& name, const fileName& file);
262
263
264public:
265
266 // Constructors
267
268 //- Default construct, with autoName on
269 vtmWriter();
270
271 //- Construct with specified behaviour for autoName
272 explicit vtmWriter(bool autoName);
273
274
275 //- Destructor
276 ~vtmWriter() = default;
277
278
279 // Member Functions
280
281 //- File extension (always "vtm")
282 inline static word ext();
283
284 //- If there are no data sets
285 bool empty() const;
286
287 //- The number of data sets
288 label size() const;
289
290
291 // Content Management
292
293 //- Clear all entries and reset output
294 void clear();
295
296 //- Define "TimeValue" for FieldData (name as per Catalyst output)
297 void setTime(scalar timeValue);
298
299 //- Define "TimeValue" for FieldData (name as per Catalyst output)
300 void setTime(const Time& t);
301
302
303 //- Start a new block, optionally with a name
304 // \return block depth
305 label beginBlock(const word& blockName = word::null);
306
307 //- End the previous block, optionally with name checking
308 // \return block depth
309 label endBlock(const word& blockName = word::null);
310
311
312 //- Add a file. The name is either empty or created with autoName
313 // \return True if file is non-empty
314 bool append(const fileName& file);
315
316 //- Add a file with name
317 // \return True if file is non-empty
318 bool append(const word& name, const fileName& file);
319
320 //- Add a file with given contentType extension
321 //- The name is either empty or created with autoName
322 // \return True if file is non-empty
323 bool append(const fileName& file, vtk::fileTag contentType);
324
325 //- Add a file with name, with given contentType extension
326 // \return True if file is non-empty
327 bool append
328 (
329 const word& name,
330 const fileName& file,
331 vtk::fileTag contentType
332 );
333
334
335 // Convenience Methods
336
337 //- Add a PolyData (.vtp) file
338 // \return True if file is non-empty
339 inline bool append_poly(const fileName& file);
340
341 //- Add a PolyData (.vtp) file with name
342 // \return True if file is non-empty
343 inline bool append_poly(const word& name, const fileName& file);
345 //- Add an UnstructuredGrid (.vtu) file
346 // \return True if file is non-empty
347 inline bool append_ugrid(const fileName& file);
348
349 //- Add an UnstructuredGrid (.vtu) file with name
350 // \return True if file is non-empty
351 inline bool append_ugrid(const word& name, const fileName& file);
352
353 //- Add a (.vtkhdf) file. Often need to specify the
354 //- content type as well (PolyData, UnstructuredGrid)
355 // \return True if file is non-empty
356 template<vtk::fileTag DataType = vtk::fileTag::VTK_HDF>
357 inline bool append_hdf(const fileName& file);
358
359 //- Add a (.vtkhdf) file with name. Often need to specify
360 //- content type as well (PolyData, UnstructuredGrid)
361 // \return True if file is non-empty
362 template<vtk::fileTag DataType = vtk::fileTag::VTK_HDF>
363 inline bool append_hdf(const word& name, const fileName& file);
364
365
366 // Content Management
368 //- Sanity fixes on the data
369 void repair(bool collapse=false);
370
371 //- Add in content from another vtm and place under the given block
372 //- name.
373 void add(const word& blockName, const vtmWriter& other);
374
375 //- Add in content from another vtm and place under the given block
376 //- name. Adjust the added 'file' entries to include the given prefix.
377 void add
378 (
379 const word& blockName,
380 const fileName& prefix,
381 const vtmWriter& other
382 );
383
384
385 // Writing
386
387 //- Write the blocks and TimeValue (xml format).
388 // \return number of data sets
389 label write_xml(std::ostream& os) const;
391 //- Write the blocks and TimeValue (xml format).
392 // \return number of data sets
393 label write_xml(OSstream& os) const;
394
395 //- Open file for writing (creates parent directory) and write the
396 //- blocks and TimeValue.
397 // The file name is with/without an extension.
398 // \return number of data sets
399 label write_xml(const fileName& file) const;
400
401 //- Forwards to write_xml().
402 //- \return number of data sets
403 label write(const fileName& file) const;
404
405 //- Print debug view of block and dataset contents
406 void dump(Ostream& os) const;
407
408
409 // Housekeeping
410
411 //- Same as append_poly - used until (2025-12)
412 bool append_vtp(const fileName& file) { return append_poly(file); }
413
414 //- Same as append_ugrid - used until (2025-12)
415 bool append_vtu(const fileName& file) { return append_ugrid(file); }
416
417 //- Same as append_poly - used until (2025-12)
418 bool append_vtp(const word& name, const fileName& file)
419 {
420 return append_poly(name, file);
421 }
422
423 //- Same as append_ugrid - used until (2025-12)
424 bool append_vtu(const word& name, const fileName& file)
425 {
426 return append_ugrid(name, file);
427 }
428};
429
430
431// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
432
433} // End namespace vtk
434} // End namespace Foam
435
436// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437
438#include "foamVtmWriterI.H"
439
440
441// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442
443#endif
444
445// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition block.H:57
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
A class for handling file names.
Definition fileName.H:75
Abstract class for a VTK output stream formatter.
label beginBlock(const word &blockName=word::null)
Start a new block, optionally with a name.
vtmWriter()
Default construct, with autoName on.
label endBlock(const word &blockName=word::null)
End the previous block, optionally with name checking.
bool append(const fileName &file)
Add a file. The name is either empty or created with autoName.
void repair(bool collapse=false)
Sanity fixes on the data.
void dump(Ostream &os) const
Print debug view of block and dataset contents.
static word ext()
File extension (always "vtm").
bool append_vtp(const word &name, const fileName &file)
Same as append_poly - used until (2025-12).
label write_xml(const fileName &file) const
Open file for writing (creates parent directory) and write the blocks and TimeValue.
bool append(const word &name, const fileName &file, vtk::fileTag contentType)
Add a file with name, with given contentType extension.
~vtmWriter()=default
Destructor.
bool append_poly(const fileName &file)
Add a PolyData (.vtp) file.
bool empty() const
If there are no data sets.
void setTime(scalar timeValue)
Define "TimeValue" for FieldData (name as per Catalyst output).
bool append(const fileName &file, vtk::fileTag contentType)
Add a file with given contentType extension The name is either empty or created with autoName.
bool append_ugrid(const fileName &file)
Add an UnstructuredGrid (.vtu) file.
bool append_vtu(const word &name, const fileName &file)
Same as append_ugrid - used until (2025-12).
label write(const fileName &file) const
Forwards to write_xml().
void add(const word &blockName, const fileName &prefix, const vtmWriter &other)
Add in content from another vtm and place under the given block name. Adjust the added 'file' entries...
label size() const
The number of data sets.
label write_xml(OSstream &os) const
Write the blocks and TimeValue (xml format).
void add(const word &blockName, const vtmWriter &other)
Add in content from another vtm and place under the given block name.
void clear()
Clear all entries and reset output.
bool append_hdf(const fileName &file)
Add a (.vtkhdf) file. Often need to specify the content type as well (PolyData, UnstructuredGrid).
bool append_vtp(const fileName &file)
Same as append_poly - used until (2025-12).
bool append_vtu(const fileName &file)
Same as append_ugrid - used until (2025-12).
label write_xml(std::ostream &os) const
Write the blocks and TimeValue (xml format).
vtmWriter(bool autoName)
Construct with specified behaviour for autoName.
bool append(const word &name, const fileName &file)
Add a file with name.
void setTime(const Time &t)
Define "TimeValue" for FieldData (name as per Catalyst output).
A class for handling words, derived from Foam::string.
Definition word.H:66
static const word null
An empty word.
Definition word.H:84
OBJstream os(runTime.globalPath()/outputName)
auto & name
Namespace for handling VTK output. Contains classes and functions for writing VTK file content.
fileTag
Some common XML tags for vtk files.
@ UNKNOWN
placeholder
Namespace for OpenFOAM.
bool isType(const U &obj)
Check if typeid of the object and Type are identical.
Definition typeInfo.H:112
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
runTime write()
word format(conversionProperties.get< word >("format"))