libMesh
vtk_io.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 #include "libmesh/utility.h"
27 
28 #ifdef LIBMESH_HAVE_VTK
29 // Ignore "deprecated...header" warning from strstream
30 #include "libmesh/ignore_warnings.h"
31 #include "vtkType.h"
32 #include "vtkSmartPointer.h"
33 #include "libmesh/restore_warnings.h"
34 #endif
35 
36 // C++ includes
37 #include <cstddef>
38 #include <map>
39 
40 // Forward declarations
41 class vtkUnstructuredGrid;
42 
43 namespace libMesh
44 {
45 
46 class MeshBase;
47 
60 class VTKIO : public MeshInput<MeshBase>,
61  public MeshOutput<MeshBase>
62 {
63 public:
68  explicit
69  VTKIO (MeshBase & mesh);
70 
75  explicit
76  VTKIO (const MeshBase & mesh);
77 
83 
92  virtual void write_nodal_data (const std::string &,
93  const std::vector<Number> &,
94  const std::vector<std::string> &) override;
95 
104  virtual void read (const std::string &) override;
105 
113  virtual void write (const std::string &) override;
114 
115 #ifdef LIBMESH_HAVE_VTK
116 
120  void set_compression(bool b);
121 
125  vtkUnstructuredGrid * get_vtk_grid();
126 
127 private:
132  void nodes_to_vtk();
133 
137  void cells_to_vtk();
138 
142  void node_values_to_vtk(const std::string & name,
143  const std::vector<Real> & local_values);
144 
150  void get_local_node_values(std::vector<Number> & local_values,
151  std::size_t variable,
152  const std::vector<Number> & soln,
153  const std::vector<std::string> & names);
154 
161  // void system_vectors_to_vtk(const EquationSystems & es,
162  // vtkUnstructuredGrid * & grid);
163 
169  vtkSmartPointer<vtkUnstructuredGrid> _vtk_grid;
170 
174  bool _compress;
175 
179  std::map<dof_id_type, dof_id_type> _local_node_map;
180 
181 
186  struct ElementMaps
187  {
188  // Associate libmesh_type with vtk_type (searchable in both directions).
189  void associate(ElemType libmesh_type, vtkIdType vtk_type)
190  {
191  writing_map[libmesh_type] = vtk_type;
192  reading_map[vtk_type] = libmesh_type;
193  }
194 
195  // Find an entry in the writing map, or throw an error.
196  vtkIdType find(ElemType libmesh_type)
197  {
198  return libmesh_map_find(writing_map, libmesh_type);
199  }
200 
201  // Find an entry in the reading map, or throw an error.
202  ElemType find(vtkIdType vtk_type)
203  {
204  return libmesh_map_find(reading_map, vtk_type);
205  }
206 
207  std::map<ElemType, vtkIdType> writing_map;
208  std::map<vtkIdType, ElemType> reading_map;
209  };
210 
215  static std::map<ElemMappingType, ElementMaps> _element_maps;
216 
220  static std::map<ElemMappingType, ElementMaps> build_element_maps();
221 
222 #endif
223 };
224 
225 
226 
227 } // namespace libMesh
228 
229 
230 #endif // LIBMESH_VTK_IO_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
void set_compression(bool b)
Setter for compression flag.
Definition: vtk_io.C:477
void node_values_to_vtk(const std::string &name, const std::vector< Real > &local_values)
write the nodal values of soln to a vtkUnstructuredGrid
Definition: vtk_io.C:654
static std::map< ElemMappingType, ElementMaps > _element_maps
ElementMaps objects that are built statically and used by all instances of this class.
Definition: vtk_io.h:215
ElemType
Defines an enum for geometric element types.
std::map< dof_id_type, dof_id_type > _local_node_map
maps global node id to node id of partition
Definition: vtk_io.h:179
vtkIdType find(ElemType libmesh_type)
Definition: vtk_io.h:196
vtkSmartPointer< vtkUnstructuredGrid > _vtk_grid
Write the system vectors to vtk.
Definition: vtk_io.h:169
bool _compress
Flag to indicate whether the output should be compressed.
Definition: vtk_io.h:174
virtual void write(const std::string &) override
Output the mesh without solutions to a .pvtu file.
This class defines an abstract interface for Mesh output.
Definition: mesh_output.h:53
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:186
This class implements reading and writing meshes in the VTK format.
Definition: vtk_io.h:60
This is the MeshBase class.
Definition: mesh_base.h:74
virtual void read(const std::string &) override
This method implements reading a mesh from a specified file in VTK format.
Definition: vtk_io.C:185
static std::map< ElemMappingType, ElementMaps > build_element_maps()
Static function used to construct _element_maps.
Definition: vtk_io.C:122
void nodes_to_vtk()
write the nodes from the mesh into a vtkUnstructuredGrid and update the local_node_map.
Definition: vtk_io.C:484
ElemType find(vtkIdType vtk_type)
Definition: vtk_io.h:202
This class defines an abstract interface for Mesh input.
Definition: mesh_base.h:56
virtual void write_nodal_data(const std::string &, const std::vector< Number > &, const std::vector< std::string > &) override
This method implements writing a mesh with nodal data to a specified file where the nodal data and va...
Definition: vtk_io.C:352
vtkUnstructuredGrid * get_vtk_grid()
Get a pointer to the VTK unstructured grid data structure.
Definition: vtk_io.C:470
std::map< ElemType, vtkIdType > writing_map
Definition: vtk_io.h:207
std::map< vtkIdType, ElemType > reading_map
Definition: vtk_io.h:208
VTKIO(MeshBase &mesh)
Constructor.
Definition: vtk_io.C:82
void associate(ElemType libmesh_type, vtkIdType vtk_type)
Definition: vtk_io.h:189
void get_local_node_values(std::vector< Number > &local_values, std::size_t variable, const std::vector< Number > &soln, const std::vector< std::string > &names)
Extract the values of soln that correspond to the nodes.
Definition: vtk_io.C:673
void cells_to_vtk()
write the cells from the mesh into a vtkUnstructuredGrid
Definition: vtk_io.C:564