Flow123d  release_3.0.0-968-gc87a28e79
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 
23 #include <istream> // for istream
24 #include <map> // for map, map<>::value_compare
25 #include <string> // for string
26 #include <armadillo>
27 #include "io/msh_basereader.hh" // for MeshDataHeader, DataType
28 #include "system/exceptions.hh" // for ExcStream, operator<<, EI
29 #include "system/file_path.hh" // for FilePath
30 #include "system/tokenizer.hh" // for Tokenizer, Tokenizer::Po...
31 
33 class Mesh;
34 class PvdMeshReader;
35 namespace pugi { class xml_node; }
36 
37 
38 
39 
40 class VtkMeshReader : public BaseMeshReader {
41  friend class PvdMeshReader;
42 
43 public:
44  TYPEDEF_ERR_INFO(EI_VTKFile, std::string);
45  TYPEDEF_ERR_INFO(EI_ExpectedFormat, std::string);
46  TYPEDEF_ERR_INFO(EI_ErrMessage, std::string);
47  TYPEDEF_ERR_INFO(EI_SectionTypeName, std::string);
48  TYPEDEF_ERR_INFO(EI_TagType, std::string);
49  TYPEDEF_ERR_INFO(EI_TagName, std::string);
50  DECLARE_EXCEPTION(ExcInvalidFormat,
51  << "Invalid format of DataArray " << EI_FieldName::val << ", expected " << EI_ExpectedFormat::val << "\n"
52  << "in the input file: " << EI_VTKFile::qval);
53  DECLARE_EXCEPTION(ExcUnknownFormat,
54  << "Unsupported or missing format of DataArray " << EI_FieldName::val << "\n" << "in the input file: " << EI_VTKFile::qval);
55  DECLARE_EXCEPTION(ExcWrongType,
56  << EI_ErrMessage::val << " data type of " << EI_SectionTypeName::val << "\n" << "in the input file: " << EI_VTKFile::qval);
57  DECLARE_EXCEPTION(ExcIncompatibleMesh,
58  << "Incompatible meshes, " << EI_ErrMessage::val << "\n" << "for VTK input file: " << EI_VTKFile::qval);
59  DECLARE_EXCEPTION(ExcMissingTag,
60  << "Missing " << EI_TagType::val << " " << EI_TagName::val << "\n" << " in the input file: " << EI_VTKFile::qval);
61  DECLARE_EXCEPTION(ExcInvalidDofHandler,
62  << "Invalid DOF handler hash for field: " << EI_FieldName::qval << " in the input file: " << EI_VTKFile::qval << ".\n");
63 
64  /// Possible data sections in UnstructuredGrid - Piece node.
65  enum DataSections {
67  };
68 
69  /// Type of data formats - ascii, binary or compressed with zLib.
70  enum DataFormat {
72  };
73 
74  /**
75  * Construct the VTK format reader from given FilePath.
76  * This opens the file for reading.
77  */
78  VtkMeshReader(const FilePath &file_name);
79 
80  /// Destructor
81  virtual ~VtkMeshReader();
82 
83  /**
84  * Read regions from the VTK file and save the physical sections as regions in the RegionDB.
85  *
86  * Region Labels starting with '!' are treated as boundary regions. Elements of these regions are used just to
87  * assign regions to the boundary and are not used in actual FEM computations.
88  */
89  void read_physical_names(Mesh * mesh) override;
90 
91  /**
92  * Check if nodes and elements of VTK mesh is compatible with \p mesh.
93  *
94  * - to all nodes of VTK mesh must exists one and only one nodes in second mesh
95  * - the same must occur for elements
96  * - method fill vector \p bulk_elements_id_
97  * - it is necessary to call this method before calling \p get_element_data
98  *
99  * OBSOLETE method - will be replace with Mesh::check_compatible_mesh after merge fields!
100  */
101  void check_compatible_mesh(Mesh &mesh) override;
102 
103  /**
104  * Find header of DataArray section of VTK file by field name given by header_query.
105  */
106  MeshDataHeader & find_header(HeaderQuery &header_query) override;
107 
108 protected:
109  /**
110  * Map of DataArray sections in VTK file.
111  *
112  * For each field_name contains MeshDataHeader.
113  */
114  typedef typename std::multimap< std::string, MeshDataHeader > HeaderTable;
115 
116  /**
117  * Special constructor of VTK files defined in PVD file. Constructor is called from PVD mesh reader.
118  *
119  * Construct the VTK format reader from given FilePath and set shared map of element data values.
120  * This opens the file for reading.
121  */
122  VtkMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values, double time_step);
123 
124  /**
125  * private method for reading of nodes
126  */
127  void read_nodes(Mesh * mesh);
128 
129  /**
130  * Method for reading of elements.
131  * Input of the mesh allows changing regions within the input file.
132  */
133  void read_elements(Mesh * mesh);
134 
135  /**
136  * create data caches of node and elements DataArray tags
137  */
139 
140  /// Reads table of DataArray headers through pugixml interface
141  void make_header_table() override;
142 
143  /// Helper method that create DataArray header of given xml node (used from \p make_header_table)
144  MeshDataHeader create_header(pugi::xml_node node, unsigned int n_entities, Tokenizer::Position pos, OutputTime::DiscreteSpace disc);
145 
146  /// Get DataType by value of string
147  DataType get_data_type(std::string type_str);
148 
149  /// Return size of value of data_type.
150  unsigned int type_value_size(DataType data_type);
151 
152  /// Parse ascii data to data cache
153  void parse_ascii_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
154  Tokenizer::Position pos, bool boundary_domain);
155 
156  /// Parse binary data to data cache
157  void parse_binary_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
158  Tokenizer::Position pos, bool boundary_domain, DataType value_type);
159 
160  /// Uncompress and parse binary compressed data to data cache
161  void parse_compressed_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
162  Tokenizer::Position pos, bool boundary_domain, DataType value_type);
163 
164  /// Set base attributes of VTK and get count of nodes and elements.
165  void read_base_vtk_attributes(pugi::xml_node vtk_node, unsigned int &n_nodes, unsigned int &n_elements);
166 
167  /// Get position of AppendedData tag in VTK file
168  Tokenizer::Position get_appended_position();
169 
170  /**
171  * Implements @p BaseMeshReader::read_element_data.
172  */
173  void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components,
174  bool boundary_domain) override;
175 
176  /**
177  * Compare two points representing by armadillo vector.
178  *
179  * - used in \p check_compatible_mesh method
180  * - calculate with \p point_tolerance parameter
181  */
182  bool compare_points(const arma::vec3 &p1, const arma::vec3 &p2);
183 
184  /// Tolerance during comparison point data with GMSH nodes.
185  static const double point_tolerance;
186 
187  /// header type of VTK file (only for appended data)
189 
190  /// variants of data format (ascii, appended, compressed appended)
192 
193  /// Table with data of DataArray headers
195 
196  /// input stream allow read appended data, used only if this tag exists
197  std::istream *data_stream_;
198 
199  /// store count of read entities
200  unsigned int n_read_;
201 
202  /// time of VTK file (getting only during initialization from PVD reader)
203  double time_step_;
204 };
205 
206 #endif /* MSH_VTK_READER_HH */
207 
VtkMeshReader::header_table_
HeaderTable header_table_
Table with data of DataArray headers.
Definition: msh_vtkreader.hh:194
PvdMeshReader
Definition: msh_pvdreader.hh:32
VtkMeshReader::create_node_element_caches
void create_node_element_caches()
Definition: msh_vtkreader.cc:627
VtkMeshReader::DataFormat
DataFormat
Type of data formats - ascii, binary or compressed with zLib.
Definition: msh_vtkreader.hh:70
VtkMeshReader::make_header_table
void make_header_table() override
Reads table of DataArray headers through pugixml interface.
Definition: msh_vtkreader.cc:205
VtkMeshReader::~VtkMeshReader
virtual ~VtkMeshReader()
Destructor.
Definition: msh_vtkreader.cc:90
msh_basereader.hh
file_path.hh
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
VtkMeshReader
Definition: msh_vtkreader.hh:40
VtkMeshReader::DECLARE_EXCEPTION
DECLARE_EXCEPTION(ExcInvalidFormat,<< "Invalid format of DataArray "<< EI_FieldName::val<< ", expected "<< EI_ExpectedFormat::val<< "\n"<< "in the input file: "<< EI_VTKFile::qval)
VtkMeshReader::get_appended_position
Tokenizer::Position get_appended_position()
Get position of AppendedData tag in VTK file.
Definition: msh_vtkreader.cc:128
arma::vec3
Definition: doxy_dummy_defs.hh:17
VtkMeshReader::create_header
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)
Definition: msh_vtkreader.cc:158
VtkMeshReader::cells
@ cells
Definition: msh_vtkreader.hh:66
VtkMeshReader::points
@ points
Definition: msh_vtkreader.hh:66
exceptions.hh
VtkMeshReader::parse_binary_data
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.
Definition: msh_vtkreader.cc:392
VtkMeshReader::parse_compressed_data
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.
Definition: msh_vtkreader.cc:407
BaseMeshReader
Definition: msh_basereader.hh:58
VtkMeshReader::HeaderTable
std::multimap< std::string, MeshDataHeader > HeaderTable
Definition: msh_vtkreader.hh:114
VtkMeshReader::cell_data
@ cell_data
Definition: msh_vtkreader.hh:66
VtkMeshReader::DataSections
DataSections
Possible data sections in UnstructuredGrid - Piece node.
Definition: msh_vtkreader.hh:65
ElementDataCacheBase
Definition: element_data_cache_base.hh:33
VtkMeshReader::type_value_size
unsigned int type_value_size(DataType data_type)
Return size of value of data_type.
Definition: msh_vtkreader.cc:331
VtkMeshReader::find_header
MeshDataHeader & find_header(HeaderQuery &header_query) override
Definition: msh_vtkreader.cc:262
VtkMeshReader::TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_VTKFile, std::string)
VtkMeshReader::read_element_data
void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain) override
Definition: msh_vtkreader.cc:340
VtkMeshReader::read_nodes
void read_nodes(Mesh *mesh)
Definition: msh_vtkreader.cc:572
VtkMeshReader::data_format_
DataFormat data_format_
variants of data format (ascii, appended, compressed appended)
Definition: msh_vtkreader.hh:191
BaseMeshReader::MeshDataHeader
Definition: msh_basereader.hh:100
VtkMeshReader::point_tolerance
static const double point_tolerance
Tolerance during comparison point data with GMSH nodes.
Definition: msh_vtkreader.hh:185
Mesh
Definition: mesh.h:80
VtkMeshReader::VtkMeshReader
VtkMeshReader(const FilePath &file_name)
Definition: msh_vtkreader.cc:68
VtkMeshReader::parse_ascii_data
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.
Definition: msh_vtkreader.cc:373
VtkMeshReader::read_base_vtk_attributes
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: msh_vtkreader.cc:97
VtkMeshReader::binary_uncompressed
@ binary_uncompressed
Definition: msh_vtkreader.hh:71
OutputTime::DiscreteSpace
DiscreteSpace
Definition: output_time.hh:104
VtkMeshReader::binary_zlib
@ binary_zlib
Definition: msh_vtkreader.hh:71
pugi
Definition: msh_vtkreader.hh:35
VtkMeshReader::compare_points
bool compare_points(const arma::vec3 &p1, const arma::vec3 &p2)
Definition: msh_vtkreader.cc:560
VtkMeshReader::time_step_
double time_step_
time of VTK file (getting only during initialization from PVD reader)
Definition: msh_vtkreader.hh:203
VtkMeshReader::header_type_
DataType header_type_
header type of VTK file (only for appended data)
Definition: msh_vtkreader.hh:188
VtkMeshReader::get_data_type
DataType get_data_type(std::string type_str)
Get DataType by value of string.
Definition: msh_vtkreader.cc:303
VtkMeshReader::ascii
@ ascii
Definition: msh_vtkreader.hh:71
VtkMeshReader::read_physical_names
void read_physical_names(Mesh *mesh) override
Definition: msh_vtkreader.cc:567
VtkMeshReader::data_stream_
std::istream * data_stream_
input stream allow read appended data, used only if this tag exists
Definition: msh_vtkreader.hh:197
VtkMeshReader::n_read_
unsigned int n_read_
store count of read entities
Definition: msh_vtkreader.hh:200
DataType
DataType
Types of VTK data (value 'undefined' for empty value)
Definition: msh_basereader.hh:45
tokenizer.hh
VtkMeshReader::check_compatible_mesh
void check_compatible_mesh(Mesh &mesh) override
Definition: msh_vtkreader.cc:462
VtkMeshReader::read_elements
void read_elements(Mesh *mesh)
Definition: msh_vtkreader.cc:594