Flow123d  build_with_4.0.3-95067a1
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  * Find header of DataArray section of VTK file by field name given by header_query.
93  */
94  MeshDataHeader & find_header(HeaderQuery &header_query) override;
95 
96 protected:
97  /**
98  * Map of DataArray sections in VTK file.
99  *
100  * For each field_name contains MeshDataHeader.
101  */
102  typedef typename std::multimap< std::string, MeshDataHeader > HeaderTable;
103 
104  /**
105  * Special constructor of VTK files defined in PVD file. Constructor is called from PVD mesh reader.
106  *
107  * Construct the VTK format reader from given FilePath and set shared map of element data values.
108  * This opens the file for reading.
109  */
110  VtkMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values, double time_step);
111 
112  /**
113  * private method for reading of nodes
114  */
115  void read_nodes(Mesh * mesh);
116 
117  /**
118  * Method for reading of elements.
119  * Input of the mesh allows changing regions within the input file.
120  */
121  void read_elements(Mesh * mesh);
122 
123  /**
124  * create data caches of node and elements DataArray tags
125  */
127 
128  /// Reads table of DataArray headers through pugixml interface
129  void make_header_table() override;
130 
131  /// Helper method that create DataArray header of given xml node (used from \p make_header_table)
132  MeshDataHeader create_header(pugi::xml_node node, unsigned int n_entities, Tokenizer::Position pos, OutputTime::DiscreteSpace disc);
133 
134  /// Get DataType by value of string
135  DataType get_data_type(std::string type_str);
136 
137  /// Return size of value of data_type.
138  unsigned int type_value_size(DataType data_type);
139 
140  /// Parse ascii data to data cache
141  void parse_ascii_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
142  Tokenizer::Position pos);
143 
144  /// Parse binary data to data cache
145  void parse_binary_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
146  Tokenizer::Position pos);
147 
148  /// Uncompress and parse binary compressed data to data cache
149  void parse_compressed_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities,
150  Tokenizer::Position pos);
151 
152  /// Set base attributes of VTK and get count of nodes and elements.
153  void read_base_vtk_attributes(pugi::xml_node vtk_node, unsigned int &n_nodes, unsigned int &n_elements);
154 
155  /// Get position of AppendedData tag in VTK file
156  Tokenizer::Position get_appended_position();
157 
158  /**
159  * Implements @p BaseMeshReader::read_element_data.
160  */
161  void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header) override;
162 
163  /// Tolerance during comparison point data with GMSH nodes.
164  static const double point_tolerance;
165 
166  /// header type of VTK file (only for appended data)
168 
169  /// variants of data format (ascii, appended, compressed appended)
171 
172  /// Table with data of DataArray headers
174 
175  /// input stream allow read appended data, used only if this tag exists
176  std::istream *data_stream_;
177 
178  /// store count of read entities
179  unsigned int n_read_;
180 
181  /// time of VTK file (getting only during initialization from PVD reader)
182  double time_step_;
183 };
184 
185 #endif /* MSH_VTK_READER_HH */
186 
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
Definition: mesh.h:362
void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header) override
VtkMeshReader(const FilePath &file_name)
TYPEDEF_ERR_INFO(EI_VTKFile, std::string)
TYPEDEF_ERR_INFO(EI_ErrMessage, std::string)
void parse_binary_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos)
Parse binary data to data cache.
std::multimap< std::string, MeshDataHeader > HeaderTable
DECLARE_EXCEPTION(ExcMissingTag,<< "Missing "<< EI_TagType::val<< " "<< EI_TagName::val<< "\n"<< " in the input file: "<< EI_VTKFile::qval)
virtual ~VtkMeshReader()
Destructor.
DataFormat data_format_
variants of data format (ascii, appended, compressed appended)
Tokenizer::Position get_appended_position()
Get position of AppendedData tag in VTK file.
void read_elements(Mesh *mesh)
TYPEDEF_ERR_INFO(EI_SectionTypeName, std::string)
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)
HeaderTable header_table_
Table with data of DataArray headers.
DataSections
Possible data sections in UnstructuredGrid - Piece node.
static const double point_tolerance
Tolerance during comparison point data with GMSH nodes.
double time_step_
time of VTK file (getting only during initialization from PVD reader)
TYPEDEF_ERR_INFO(EI_TagName, std::string)
std::istream * data_stream_
input stream allow read appended data, used only if this tag exists
void parse_ascii_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos)
Parse ascii data to data cache.
DECLARE_EXCEPTION(ExcInvalidDofHandler,<< "Invalid DOF handler hash for field: "<< EI_FieldName::qval<< " in the input file: "<< EI_VTKFile::qval<< ".\n")
void parse_compressed_data(ElementDataCacheBase &data_cache, unsigned int n_components, unsigned int n_entities, Tokenizer::Position pos)
Uncompress and parse binary compressed data to data cache.
unsigned int type_value_size(DataType data_type)
Return size of value of data_type.
TYPEDEF_ERR_INFO(EI_TagType, std::string)
DECLARE_EXCEPTION(ExcWrongType,<< EI_ErrMessage::val<< " data type of "<< EI_SectionTypeName::val<< "\n"<< "in the input file: "<< EI_VTKFile::qval)
void make_header_table() override
Reads table of DataArray headers through pugixml interface.
DataFormat
Type of data formats - ascii, binary or compressed with zLib.
DataType get_data_type(std::string type_str)
Get DataType by value of string.
TYPEDEF_ERR_INFO(EI_ExpectedFormat, std::string)
unsigned int n_read_
store count of read entities
DataType header_type_
header type of VTK file (only for appended data)
DECLARE_EXCEPTION(ExcUnknownFormat,<< "Unsupported or missing format of DataArray "<< EI_FieldName::val<< "\n"<< "in the input file: "<< EI_VTKFile::qval)
void read_nodes(Mesh *mesh)
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.
void create_node_element_caches()
void read_physical_names(Mesh *mesh) override
DECLARE_EXCEPTION(ExcIncompatibleMesh,<< "Incompatible meshes, "<< EI_ErrMessage::val<< "\n"<< "for VTK input file: "<< EI_VTKFile::qval)
DECLARE_EXCEPTION(ExcInvalidFormat,<< "Invalid format of DataArray "<< EI_FieldName::val<< ", expected "<< EI_ExpectedFormat::val<< "\n"<< "in the input file: "<< EI_VTKFile::qval)
MeshDataHeader & find_header(HeaderQuery &header_query) override
DataType
Types of VTK data (value 'undefined' for empty value)