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 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.C
68
69\*---------------------------------------------------------------------------*/
70
71#ifndef Foam_vtk_vtmWriter_H
72#define Foam_vtk_vtmWriter_H
73
75#include "DynamicList.H"
76
77// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78
79namespace Foam
80{
81
82// Forward declarations
83class Time;
84
85namespace vtk
86{
87
88/*---------------------------------------------------------------------------*\
89 Class vtk::vtmWriter Declaration
90\*---------------------------------------------------------------------------*/
92class vtmWriter
93{
94 //- Simple structure for containing entries
95 struct vtmEntry
96 {
97 enum Type
98 {
99 NONE = 0,
100 DATA = 'D',
101 BEGIN_BLOCK = '{',
102 END_BLOCK = '}'
103 };
104
105 //- The entry type
106 int type_;
107
108 //- The 'name' entry (to describe block or data)
109 string name_;
110
111 //- The 'file' entry (data only)
112 fileName file_;
113
114 // Constructors
115
116 vtmEntry(const vtmEntry&) = default;
117 vtmEntry(vtmEntry&&) = default;
118 vtmEntry& operator=(const vtmEntry&) = default;
119 vtmEntry& operator=(vtmEntry&&) = default;
120
121 //- Default construct
122 vtmEntry()
123 :
124 type_(NONE)
125 {}
126
127 //- Construct from components
128 vtmEntry(int what, const string& name, const fileName& file)
129 :
130 type_(what), name_(name), file_(file)
131 {}
132
133
134 // Factory Methods
135
136 static vtmEntry block(const string& name)
137 {
138 return vtmEntry(BEGIN_BLOCK, name, "");
139 }
140
141 static vtmEntry endblock()
142 {
143 return vtmEntry(END_BLOCK, "", "");
144 }
145
146 static vtmEntry entry(const fileName& file)
147 {
148 return vtmEntry(DATA, "", file);
149 }
150
151 static vtmEntry entry(const string& name, const fileName& file)
152 {
153 return vtmEntry(DATA, name, file);
154 }
155
156
157 // Member Functions
158
159 //- Test the type
160 bool isType(Type what) const noexcept
161 {
162 return type_ == what;
163 }
164
165 //- Reset to NONE
166 void clear();
167
168 //- True if the entry is good.
169 bool good() const noexcept;
170
171 //- Output valid entry as XML
172 bool write(vtk::formatter& format) const;
173 };
174
175
176 // Private Member Data
177
178 //- Auto-generate names from 'file' entry?
179 bool autoName_;
180
181 //- Has a TimeValue for FieldData?
182 bool hasTime_;
183
184 //- A vtm file entry: begin/end block, dataset
185 DynamicList<vtmEntry> entries_;
186
187 //- LIFO stack of current block names
188 DynamicList<word> blocks_;
189
190 //- TimeValue for FieldData
191 scalar timeValue_;
192
193
194 // Private Member Functions
195
196 //- Remove NONE entries
197 bool pruneEmpty();
198
199 //- Remove empty blocks
200 bool pruneEmptyBlocks();
201
202 //- Collapse block if it has a single dataset and the names allow it
203 bool collapseBlocks();
204
205
206public:
207
208 // Constructors
209
210 //- Default construct, with autoName on
211 vtmWriter();
212
213 //- Construct with specified behaviour for autoName
214 explicit vtmWriter(bool autoName);
215
216
217 //- Destructor
218 ~vtmWriter() = default;
219
220
221 // Member Functions
222
223 //- File extension (always "vtm")
224 inline static word ext();
225
226 //- If there are no data sets
227 bool empty() const;
228
229 //- The number of data sets
230 label size() const;
231
232
233 // Content Management
234
235 //- Clear all entries and reset output
236 void clear();
237
238 //- Define "TimeValue" for FieldData (name as per Catalyst output)
239 void setTime(scalar timeValue);
240
241 //- Define "TimeValue" for FieldData (name as per Catalyst output)
242 void setTime(const Time& t);
243
244
245 //- Start a new block, optionally with a name
246 // \return block depth
247 label beginBlock(const word& blockName = word::null);
248
249 //- End the previous block, optionally with name checking
250 // \return block depth
251 label endBlock(const word& blockName = word::null);
252
253
254 //- Add a file. The name is either empty or created with autoName
255 // \return True if file is non-empty
256 bool append(const fileName& file);
257
258 //- Add a file with name
259 // \return True if file is non-empty
260 bool append(const word& name, const fileName& file);
261
262 //- Add a file with given contentType extension
263 //- The name is either empty or created with autoName
264 // \return True if file is non-empty
265 bool append(const fileName& file, vtk::fileTag contentType);
266
267 //- Add a file with name, with given contentType extension
268 // \return True if file is non-empty
269 bool append
270 (
271 const word& name,
272 const fileName& file,
273 vtk::fileTag contentType
274 );
275
276 //- Add a (.vtp) file
277 // \return True if file is non-empty
278 inline bool append_vtp(const fileName& file);
279
280 //- Add a (.vtp) file with name
281 // \return True if file is non-empty
282 inline bool append_vtp(const word& name, const fileName& file);
283
284 //- Add a (.vtu) file
285 // \return True if file is non-empty
286 inline bool append_vtu(const fileName& file);
287
288 //- Add a (.vtu) file with name
289 // \return True if file is non-empty
290 inline bool append_vtu(const word& name, const fileName& file);
291
292
293 // Content Management
294
295 //- Sanity fixes on the data
296 void repair(bool collapse=false);
297
298 //- Add in content from another vtm and place under the given block
299 //- name.
300 void add(const word& blockName, const vtmWriter& other);
301
302 //- Add in content from another vtm and place under the given block
303 //- name. Adjust the added 'file' entries to include the given prefix.
304 void add
305 (
306 const word& blockName,
307 const fileName& prefix,
308 const vtmWriter& other
309 );
310
311 // Writing
312
313 //- Open file for writing (creates parent directory) and write the
314 //- blocks and TimeValue.
315 // The file name is with/without an extension.
316 // \return number of data sets
317 label write(const fileName& file);
318
319 //- Print debug view of block and dataset contents
320 void dump(Ostream& os) const;
321};
322
323
324// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325
326} // End namespace vtk
327} // End namespace Foam
328
329// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330
331#include "foamVtmWriterI.H"
332
333
334// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335
336#endif
337
338// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:61
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
A class for handling file names.
Definition: fileName.H:76
Abstract class for a VTK output stream formatter.
Provides a means of accumulating file entries for generating a vtkMultiBlockDataSet (....
Definition: foamVtmWriter.H:92
vtmWriter()
Default construct, with autoName on.
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")
label endBlock(const word &blockName=word::null)
End the previous block, optionally with name checking.
label size() const
The number of data sets.
~vtmWriter()=default
Destructor.
bool empty() const
If there are no data sets.
void setTime(scalar timeValue)
Define "TimeValue" for FieldData (name as per Catalyst output)
void add(const word &blockName, const vtmWriter &other)
void clear()
Clear all entries and reset output.
bool append_vtp(const fileName &file)
Add a (.vtp) file.
label beginBlock(const word &blockName=word::null)
Start a new block, optionally with a name.
bool append_vtu(const fileName &file)
Add a (.vtu) file.
A class for handling words, derived from Foam::string.
Definition: word.H:68
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:114
Namespace for OpenFOAM.
bool isType(const Type &t)
Check is typeid is identical to the TargetType.
Definition: typeInfo.H:218
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
runTime write()
word format(conversionProperties.get< word >("format"))