vtk_io.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 #ifndef LIBMESH_VTK_IO_H
20 #define LIBMESH_VTK_IO_H
21 
22 // Local includes
23 #include "libmesh/libmesh_common.h"
24 #include "libmesh/mesh_input.h"
25 #include "libmesh/mesh_output.h"
26 
27 #ifdef LIBMESH_HAVE_VTK
28 // Ignore "deprecated...header" warning from strstream
30 #include "vtkType.h"
31 #include "vtkSmartPointer.h"
33 #endif
34 
35 // C++ includes
36 #include <cstddef>
37 #include <map>
38 
39 // Forward declarations
40 class vtkUnstructuredGrid;
41 
42 namespace libMesh
43 {
44 
45 class MeshBase;
46 
59 class VTKIO : public MeshInput<MeshBase>,
60  public MeshOutput<MeshBase>
61 {
62 public:
67  explicit
68  VTKIO (MeshBase & mesh);
69 
74  explicit
75  VTKIO (const MeshBase & mesh);
76 
82 
91  virtual void write_nodal_data (const std::string &,
92  const std::vector<Number> &,
93  const std::vector<std::string> &) override;
94 
103  virtual void read (const std::string &) override;
104 
112  virtual void write (const std::string &) override;
113 
114 #ifdef LIBMESH_HAVE_VTK
115 
119  void set_compression(bool b);
120 
124  vtkUnstructuredGrid * get_vtk_grid();
125 
126 private:
130  void nodes_to_vtk();
131 
135  void cells_to_vtk();
136 
143  // void system_vectors_to_vtk(const EquationSystems & es,
144  // vtkUnstructuredGrid * & grid);
145 
151  vtkSmartPointer<vtkUnstructuredGrid> _vtk_grid;
152 
156  bool _compress;
157 
161  std::map<dof_id_type, dof_id_type> _local_node_map;
162 
163 
168  struct ElementMaps
169  {
170  // Associate libmesh_type with vtk_type (searchable in both directions).
171  void associate(ElemType libmesh_type, vtkIdType vtk_type)
172  {
173  writing_map[libmesh_type] = vtk_type;
174  reading_map[vtk_type] = libmesh_type;
175  }
176 
177  // Find an entry in the writing map, or throw an error.
178  vtkIdType find(ElemType libmesh_type)
179  {
180  std::map<ElemType, vtkIdType>::iterator it = writing_map.find(libmesh_type);
181 
182  if (it == writing_map.end())
183  libmesh_error_msg("Element type " << libmesh_type << " not available in VTK.");
184 
185  return it->second;
186  }
187 
188  // Find an entry in the reading map, or throw an error.
189  ElemType find(vtkIdType vtk_type)
190  {
191  std::map<vtkIdType, ElemType>::iterator it = reading_map.find(vtk_type);
192 
193  if (it == reading_map.end())
194  libmesh_error_msg("Element type " << vtk_type << " not available in libMesh.");
195 
196  return it->second;
197  }
198 
199  std::map<ElemType, vtkIdType> writing_map;
200  std::map<vtkIdType, ElemType> reading_map;
201  };
202 
208 
213 
214 #endif
215 };
216 
217 
218 
219 } // namespace libMesh
220 
221 
222 #endif // LIBMESH_VTK_IO_H
void set_compression(bool b)
Definition: vtk_io.C:357
std::map< dof_id_type, dof_id_type > _local_node_map
Definition: vtk_io.h:161
vtkIdType find(ElemType libmesh_type)
Definition: vtk_io.h:178
vtkSmartPointer< vtkUnstructuredGrid > _vtk_grid
Definition: vtk_io.h:151
bool _compress
Definition: vtk_io.h:156
virtual void write(const std::string &) override
static ElementMaps _element_maps
Definition: vtk_io.h:207
Base class for Mesh.
Definition: mesh_base.h:77
virtual void read(const std::string &) override
Definition: vtk_io.C:149
void nodes_to_vtk()
Definition: vtk_io.C:364
ElemType find(vtkIdType vtk_type)
Definition: vtk_io.h:189
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
Definition: vtk_io.C:245
static ElementMaps build_element_maps()
Definition: vtk_io.C:115
vtkUnstructuredGrid * get_vtk_grid()
Definition: vtk_io.C:350
std::map< ElemType, vtkIdType > writing_map
Definition: vtk_io.h:199
std::map< vtkIdType, ElemType > reading_map
Definition: vtk_io.h:200
VTKIO(MeshBase &mesh)
Definition: vtk_io.C:75
void associate(ElemType libmesh_type, vtkIdType vtk_type)
Definition: vtk_io.h:171
void cells_to_vtk()
Definition: vtk_io.C:407