Flow123d  release_2.2.0-914-gf1a3a4f
msh_vtkreader.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file msh_vtkreader.hh
15  * @brief
16  * @author dalibor
17  */
18 
19 #ifndef MSH_VTK_READER_HH
20 #define MSH_VTK_READER_HH
21 
22 #include <string>
23 #include <istream>
24 #include <pugixml.hpp>
25 
26 #include "io/msh_basereader.hh"
27 #include "system/file_path.hh"
28 
29 
30 class PvdMeshReader;
31 
32 
33 class VtkMeshReader : public BaseMeshReader {
34  friend class PvdMeshReader;
35 
36 public:
37  TYPEDEF_ERR_INFO(EI_VTKFile, std::string);
38  TYPEDEF_ERR_INFO(EI_ExpectedFormat, std::string);
39  TYPEDEF_ERR_INFO(EI_ErrMessage, std::string);
40  TYPEDEF_ERR_INFO(EI_SectionTypeName, std::string);
41  TYPEDEF_ERR_INFO(EI_TagType, std::string);
42  TYPEDEF_ERR_INFO(EI_TagName, std::string);
43  DECLARE_EXCEPTION(ExcInvalidFormat,
44  << "Invalid format of DataArray " << EI_FieldName::val << ", expected " << EI_ExpectedFormat::val << "\n"
45  << "in the input file: " << EI_VTKFile::qval);
46  DECLARE_EXCEPTION(ExcUnknownFormat,
47  << "Unsupported or missing format of DataArray " << EI_FieldName::val << "\n" << "in the input file: " << EI_VTKFile::qval);
48  DECLARE_EXCEPTION(ExcWrongType,
49  << EI_ErrMessage::val << " data type of " << EI_SectionTypeName::val << "\n" << "in the input file: " << EI_VTKFile::qval);
50  DECLARE_EXCEPTION(ExcIncompatibleMesh,
51  << "Incompatible meshes, " << EI_ErrMessage::val << "\n" << "for VTK input file: " << EI_VTKFile::qval);
52  DECLARE_EXCEPTION(ExcMissingTag,
53  << "Missing " << EI_TagType::val << " " << EI_TagName::val << "\n" << " in the input file: " << EI_VTKFile::qval);
54  DECLARE_EXCEPTION(ExcInvalidDofHandler,
55  << "Invalid DOF handler hash for field: " << EI_FieldName::qval << " in the input file: " << EI_VTKFile::qval << ".\n");
56 
57  /// Possible data sections in UnstructuredGrid - Piece node.
58  enum DataSections {
60  };
61 
62  /// Type of data formats - ascii, binary or compressed with zLib.
63  enum DataFormat {
65  };
66 
67  /**
68  * Construct the VTK format reader from given FilePath.
69  * This opens the file for reading.
70  */
71  VtkMeshReader(const FilePath &file_name);
72 
73  /// Destructor
74  virtual ~VtkMeshReader();
75 
76  /**
77  * Read regions from the VTK file and save the physical sections as regions in the RegionDB.
78  *
79  * Region Labels starting with '!' are treated as boundary regions. Elements of these regions are used just to
80  * assign regions to the boundary and are not used in actual FEM computations.
81  */
82  void read_physical_names(Mesh * mesh) override;
83 
84  /**
85  * Check if nodes and elements of VTK mesh is compatible with \p mesh.
86  *
87  * - to all nodes of VTK mesh must exists one and only one nodes in second mesh
88  * - the same must occur for elements
89  * - method fill vector \p bulk_elements_id_
90  * - it is necessary to call this method before calling \p get_element_data
91  */
92  void check_compatible_mesh(Mesh &mesh) override;
93 
94  /**
95  * Find header of DataArray section of VTK file by field name given by header_query.
96  */
97  MeshDataHeader & find_header(HeaderQuery &header_query) override;
98 
99 protected:
100  /**
101  * Map of DataArray sections in VTK file.
102  *
103  * For each field_name contains MeshDataHeader.
104  */
105  typedef typename std::multimap< std::string, MeshDataHeader > HeaderTable;
106 
107  /**
108  * Special constructor of VTK files defined in PVD file. Constructor is called from PVD mesh reader.
109  *
110  * Construct the VTK format reader from given FilePath and set shared map of element data values.
111  * This opens the file for reading.
112  */
113  VtkMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values, double time_step);
114 
115  /**
116  * private method for reading of nodes
117  */
118  void read_nodes(Mesh * mesh);
119 
120  /**
121  * Method for reading of elements.
122  * Input of the mesh allows changing regions within the input file.
123  */
124  void read_elements(Mesh * mesh);
125 
126  /**
127  * create data caches of node and elements DataArray tags
128  */
130 
131  /// Reads table of DataArray headers through pugixml interface
132  void make_header_table() override;
133 
134  /// Helper method that create DataArray header of given xml node (used from \p make_header_table)
135  MeshDataHeader create_header(pugi::xml_node node, unsigned int n_entities, Tokenizer::Position pos, OutputTime::DiscreteSpace disc);
136 
137  /// Get DataType by value of string
138  DataType get_data_type(std::string type_str);
139 
140  /// Return size of value of data_type.
141  unsigned int type_value_size(DataType data_type);
142 
143  /// Parse ascii data to data cache
144  void parse_ascii_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
145  Tokenizer::Position pos, bool boundary_domain);
146 
147  /// Parse binary data to data cache
148  void parse_binary_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
149  Tokenizer::Position pos, bool boundary_domain, DataType value_type);
150 
151  /// Uncompress and parse binary compressed data to data cache
152  void parse_compressed_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
153  Tokenizer::Position pos, bool boundary_domain, DataType value_type);
154 
155  /// Set base attributes of VTK and get count of nodes and elements.
156  void read_base_vtk_attributes(pugi::xml_node vtk_node, unsigned int &n_nodes, unsigned int &n_elements);
157 
158  /// Get position of AppendedData tag in VTK file
159  Tokenizer::Position get_appended_position();
160 
161  /**
162  * Implements @p BaseMeshReader::read_element_data.
163  */
164  void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components,
165  bool boundary_domain) override;
166 
167  /**
168  * Compare two points representing by armadillo vector.
169  *
170  * - used in \p check_compatible_mesh method
171  * - calculate with \p point_tolerance parameter
172  */
173  bool compare_points(arma::vec3 &p1, arma::vec3 &p2);
174 
175  /// Tolerance during comparison point data with GMSH nodes.
176  static const double point_tolerance;
177 
178  /// header type of VTK file (only for appended data)
180 
181  /// variants of data format (ascii, appended, compressed appended)
183 
184  /// Table with data of DataArray headers
185  HeaderTable header_table_;
186 
187  /// input stream allow read appended data, used only if this tag exists
188  std::istream *data_stream_;
189 
190  /// store count of read entities
191  unsigned int n_read_;
192 
193  /// time of VTK file (getting only during initialization from PVD reader)
194  double time_step_;
195 };
196 
197 #endif /* MSH_VTK_READER_HH */
198 
MeshDataHeader create_header(pugi::xml_node node, unsigned int n_entities, Tokenizer::Position pos, OutputTime::DiscreteSpace disc)
Helper method that create DataArray header of given xml node (used from make_header_table) ...
void make_header_table() override
Reads table of DataArray headers through pugixml interface.
bool compare_points(arma::vec3 &p1, arma::vec3 &p2)
double time_step_
time of VTK file (getting only during initialization from PVD reader)
DataSections
Possible data sections in UnstructuredGrid - Piece node.
HeaderTable header_table_
Table with data of DataArray headers.
void read_nodes(Mesh *mesh)
std::multimap< std::string, MeshDataHeader > HeaderTable
static const double point_tolerance
Tolerance during comparison point data with GMSH nodes.
DataFormat data_format_
variants of data format (ascii, appended, compressed appended)
VtkMeshReader(const FilePath &file_name)
void read_base_vtk_attributes(pugi::xml_node vtk_node, unsigned int &n_nodes, unsigned int &n_elements)
Set base attributes of VTK and get count of nodes and elements.
Definition: mesh.h:99
DataType get_data_type(std::string type_str)
Get DataType by value of string.
void parse_compressed_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos, bool boundary_domain, DataType value_type)
Uncompress and parse binary compressed data to data cache.
MeshDataHeader & find_header(HeaderQuery &header_query) override
void read_physical_names(Mesh *mesh) override
std::istream * data_stream_
input stream allow read appended data, used only if this tag exists
void check_compatible_mesh(Mesh &mesh) override
TYPEDEF_ERR_INFO(EI_VTKFile, std::string)
virtual ~VtkMeshReader()
Destructor.
DECLARE_EXCEPTION(ExcInvalidFormat,<< "Invalid format of DataArray "<< EI_FieldName::val<< ", expected "<< EI_ExpectedFormat::val<< "\n"<< "in the input file: "<< EI_VTKFile::qval)
DataType header_type_
header type of VTK file (only for appended data)
unsigned int n_read_
store count of read entities
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
void read_elements(Mesh *mesh)
void parse_binary_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos, bool boundary_domain, DataType value_type)
Parse binary data to data cache.
void create_node_element_caches()
void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain) override
DataFormat
Type of data formats - ascii, binary or compressed with zLib.
DataType
Types of VTK data (value &#39;undefined&#39; for empty value)
unsigned int type_value_size(DataType data_type)
Return size of value of data_type.
Tokenizer::Position get_appended_position()
Get position of AppendedData tag in VTK file.
void parse_ascii_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos, bool boundary_domain)
Parse ascii data to data cache.