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 -------------------------------------------------------------------------------
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::vtmWriter
28 
29 Description
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 
66 SourceFiles
67  foamVtmWriter.C
68 
69 \*---------------------------------------------------------------------------*/
70 
71 #ifndef Foam_vtk_vtmWriter_H
72 #define Foam_vtk_vtmWriter_H
73 
74 #include "foamVtkOutputOptions.H"
75 #include "DynamicList.H"
76 
77 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 
79 namespace Foam
80 {
81 
82 // Forward declarations
83 class Time;
84 
85 namespace vtk
86 {
87 
88 /*---------------------------------------------------------------------------*\
89  Class vtk::vtmWriter Declaration
90 \*---------------------------------------------------------------------------*/
91 
92 class 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  //- Construct null
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
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;
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 
206 public:
207 
208  // Constructors
209 
210  //- Construct null, 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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::vtk::vtmWriter::setTime
void setTime(scalar timeValue)
Define "TimeValue" for FieldData (name as per Catalyst output)
Definition: foamVtmWriter.C:375
Foam::vtk::vtmWriter::add
void add(const word &blockName, const vtmWriter &other)
Definition: foamVtmWriter.C:572
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:58
Foam::vtk::vtmWriter::beginBlock
label beginBlock(const word &blockName=word::null)
Start a new block, optionally with a name.
Definition: foamVtmWriter.C:389
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::DynamicList< vtmEntry >
Foam::vtk::vtmWriter::endBlock
label endBlock(const word &blockName=word::null)
End the previous block, optionally with name checking.
Definition: foamVtmWriter.C:398
Foam::vtk::vtmWriter::dump
void dump(Ostream &os) const
Print debug view of block and dataset contents.
Definition: foamVtmWriter.C:259
Foam::vtk::vtmWriter::size
label size() const
The number of data sets.
Definition: foamVtmWriter.C:359
Foam::vtk::vtmWriter::~vtmWriter
~vtmWriter()=default
Destructor.
Foam::vtk::vtmWriter::empty
bool empty() const
If there are no data sets.
Definition: foamVtmWriter.C:345
Foam::vtk::vtmWriter::append_vtp
bool append_vtp(const fileName &file)
Add a (.vtp) file.
Definition: foamVtmWriterI.H:36
format
word format(conversionProperties.get< word >("format"))
Foam::vtk::vtmWriter::write
label write(const fileName &file)
Definition: foamVtmWriter.C:581
Foam::vtk::vtmWriter::clear
void clear()
Clear all entries and reset output.
Definition: foamVtmWriter.C:335
foamVtmWriterI.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::vtk::vtmWriter::vtmWriter
vtmWriter()
Construct null, with autoName on.
Definition: foamVtmWriter.C:317
Foam::vtk::fileTag
fileTag
Some common XML tags for vtk files.
Definition: foamVtkCore.H:113
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
foamVtkOutputOptions.H
Foam::vtk::vtmWriter::append
bool append(const fileName &file)
Add a file. The name is either empty or created with autoName.
Definition: foamVtmWriter.C:422
Foam::isType
bool isType(const Type &t)
Check is typeid is identical to the TargetType.
Definition: typeInfo.H:218
Foam::vtk::vtmWriter
Provides a means of accumulating file entries for generating a vtkMultiBlockDataSet (....
Definition: foamVtmWriter.H:91
Foam::vtk::vtmWriter::append_vtu
bool append_vtu(const fileName &file)
Add a (.vtu) file.
Definition: foamVtmWriterI.H:52
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
DynamicList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::vtk::vtmWriter::ext
static word ext()
File extension (always "vtm")
Definition: foamVtmWriterI.H:30
Foam::vtk::vtmWriter::repair
void repair(bool collapse=false)
Sanity fixes on the data.
Definition: foamVtmWriter.C:206
Foam::vtk::formatter
Abstract class for a VTK output stream formatter.
Definition: foamVtkFormatter.H:68