libMesh
vtk_io.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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
29 #include "libmesh/ignore_warnings.h"
30 #include "vtkType.h"
31 #include "vtkSmartPointer.h"
32 #include "libmesh/restore_warnings.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> &) libmesh_override;
94 
103  virtual void read (const std::string &) libmesh_override;
104 
112  virtual void write (const std::string &) libmesh_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)
Setter for compression flag.
Definition: vtk_io.C:354
std::map< dof_id_type, dof_id_type > _local_node_map
maps global node id to node id of partition
Definition: vtk_io.h:161
vtkIdType find(ElemType libmesh_type)
Definition: vtk_io.h:178
ElemType
Defines an enum for geometric element types.
vtkSmartPointer< vtkUnstructuredGrid > _vtk_grid
Write the system vectors to vtk.
Definition: vtk_io.h:151
bool _compress
Flag to indicate whether the output should be compressed.
Definition: vtk_io.h:156
This class defines an abstract interface for Mesh output.
Definition: mesh_output.h:53
static ElementMaps _element_maps
ElementMaps object that is built statically and used by all instances of this class.
Definition: vtk_io.h:207
The libMesh namespace provides an interface to certain functionality in the library.
Helper object that holds a map from VTK to libMesh element types and vice-versa.
Definition: vtk_io.h:168
This class implements reading and writing meshes in the VTK format.
Definition: vtk_io.h:59
This is the MeshBase class.
Definition: mesh_base.h:68
virtual void read(const std::string &) libmesh_override
This method implements reading a mesh from a specified file in VTK format.
Definition: vtk_io.C:148
void nodes_to_vtk()
write the nodes from the mesh into a vtkUnstructuredGrid
Definition: vtk_io.C:361
ElemType find(vtkIdType vtk_type)
Definition: vtk_io.h:189
This class defines an abstract interface for Mesh input.
Definition: mesh_base.h:50
static ElementMaps build_element_maps()
Static function used to construct the _element_maps struct.
Definition: vtk_io.C:114
vtkUnstructuredGrid * get_vtk_grid()
Get a pointer to the VTK unstructured grid data structure.
Definition: vtk_io.C:347
std::map< ElemType, vtkIdType > writing_map
Definition: vtk_io.h:199
std::map< vtkIdType, ElemType > reading_map
Definition: vtk_io.h:200
virtual void write(const std::string &) libmesh_override
Output the mesh without solutions to a .pvtu file.
VTKIO(MeshBase &mesh)
Constructor.
Definition: vtk_io.C:74
void associate(ElemType libmesh_type, vtkIdType vtk_type)
Definition: vtk_io.h:171
void cells_to_vtk()
write the cells from the mesh into a vtkUnstructuredGrid
Definition: vtk_io.C:404
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) libmesh_override
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: vtk_io.C:242