22 #include "boost/lexical_cast.hpp" 39 data_stream.read(reinterpret_cast<char *>(&val),
sizeof(val));
51 ASSERT(
false).error(
"Unsupported header_type!\n");
97 }
catch(ExcWrongType &e) {
98 e << EI_SectionTypeName(
"base parameter header_type");
106 THROW( ExcWrongType() << EI_ErrMessage(
"Forbidden") << EI_SectionTypeName(
"base parameter header_type")
107 << EI_VTKFile(
tok_.f_name()));
109 std::string compressor = vtk_node.attribute(
"compressor").as_string();
110 if (compressor ==
"vtkZLibDataCompressor")
116 pugi::xml_node piece_node = vtk_node.child(
"UnstructuredGrid").child(
"Piece");
117 n_nodes = piece_node.attribute(
"NumberOfPoints").as_uint();
118 n_elements = piece_node.attribute(
"NumberOfCells").as_uint();
124 Tokenizer::Position appended_pos;
128 tok_.set_position( Tokenizer::Position() );
129 if (!
tok_.skip_to(
"AppendedData"))
130 THROW(ExcMissingTag() << EI_TagType(
"tag") << EI_TagName(
"AppendedData") << EI_VTKFile(
tok_.f_name()) );
132 appended_pos =
tok_.get_position();
143 appended_pos.line_counter_++;
147 data_stream_ =
new std::ifstream(
tok_.f_name(), std::ios_base::in | std::ios_base::binary );
157 header.
field_name = node.attribute(
"Name").as_string();
161 }
catch(ExcWrongType &e) {
162 e << EI_SectionTypeName(
"DataArray " + header.
field_name);
165 if (disc == OutputTime::DiscreteSpace::NATIVE_DATA) {
166 header.
n_components = node.attribute(
"n_dofs_per_element").as_uint();
169 header.
n_components = node.attribute(
"NumberOfComponents").as_uint(1);
172 std::string
format = node.attribute(
"format").as_string();
173 if (format==
"appended") {
175 THROW(ExcInvalidFormat() << EI_FieldName(header.
field_name) << EI_ExpectedFormat(
"ascii") << EI_VTKFile(
tok_.f_name()) );
176 std::streampos file_pos = pos.file_position_;
177 file_pos += node.attribute(
"offset").as_uint();
178 header.
position = Tokenizer::Position( file_pos, pos.line_counter_, pos.line_position_ );
179 }
else if (format==
"ascii") {
181 THROW(ExcInvalidFormat() << EI_FieldName(header.
field_name) << EI_ExpectedFormat(
"appended") << EI_VTKFile(
tok_.f_name()) );
183 tok_.set_position( Tokenizer::Position() );
185 std::string found_str = (is_point) ?
"<Points>" :
"Name=\"" + header.
field_name +
"\"";
186 if (!
tok_.skip_to(found_str))
187 THROW(ExcMissingTag() << EI_TagType(
"DataArray tag") << EI_TagName(header.
field_name) << EI_VTKFile(
tok_.f_name()) );
189 if (is_point)
tok_.skip_to(
"DataArray");
193 THROW(ExcUnknownFormat() << EI_FieldName(header.
field_name) << EI_VTKFile(
tok_.f_name()) );
202 pugi::xml_document doc;
203 doc.load_file(
tok_.f_name().c_str() );
204 unsigned int n_nodes, n_elements;
211 Tokenizer::Position appended_pos;
218 pugi::xml_node node = doc.child(
"VTKFile").child(
"UnstructuredGrid").child(
"Piece");
223 auto points_header =
create_header( node.child(
"Points").child(
"DataArray"), n_nodes, appended_pos,
224 OutputTime::DiscreteSpace::MESH_DEFINITION );
226 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"Points", points_header) );
227 auto con_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"connectivity"),
228 n_elements, appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
229 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"connectivity", con_header) );
230 auto offsets_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"offsets"),
231 n_elements, appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
232 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"offsets", offsets_header) );
233 auto types_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"types"), n_elements,
234 appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
235 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"types", types_header) );
237 pugi::xml_node point_node = node.child(
"PointData");
238 for (pugi::xml_node subnode = point_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
239 auto header =
create_header( subnode, n_nodes, appended_pos, OutputTime::DiscreteSpace::NODE_DATA );
240 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
243 pugi::xml_node cell_node = node.child(
"CellData");
244 for (pugi::xml_node subnode = cell_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
245 auto header =
create_header( subnode, n_elements, appended_pos, OutputTime::DiscreteSpace::ELEM_DATA );
246 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
249 pugi::xml_node native_node = node.child(
"Flow123dData");
250 for (pugi::xml_node subnode = native_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
251 auto header =
create_header( subnode, n_elements, appended_pos, OutputTime::DiscreteSpace::NATIVE_DATA );
252 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
263 THROW( ExcFieldNameNotFound() << EI_FieldName(header_query.
field_name) << EI_MeshFile(
tok_.f_name()));
264 }
else if (count == 1) {
268 if (header_query.
discretization != table_it->second.discretization) {
269 if (header_query.
discretization != OutputTime::DiscreteSpace::UNDEFINED) {
271 "Invalid value of 'input_discretization' for field '{}', time: {}.\nCorrect discretization type will be used.\n",
277 if (header_query.
discretization == OutputTime::DiscreteSpace::NATIVE_DATA)
279 THROW(ExcInvalidDofHandler() << EI_FieldName(header_query.
field_name) << EI_VTKFile(
tok_.f_name()) );
283 HeaderTable::iterator table_it;
285 if (header_query.
discretization != table_it->second.discretization) {
290 THROW( ExcMissingFieldDiscretization() << EI_FieldName(header_query.
field_name) << EI_Time(header_query.
time) << EI_MeshFile(
tok_.f_name()));
315 if (it != types.end()) {
318 THROW( ExcWrongType() << EI_ErrMessage(
"Unknown") << EI_VTKFile(
tok_.f_name()));
328 static const std::vector<unsigned int> sizes = { 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 0 };
330 return sizes[data_type];
336 bool boundary_domain) {
338 ASSERT(!boundary_domain).error(
"Reading VTK data of boundary elements is not supported yet!\n");
341 case DataFormat::ascii: {
345 case DataFormat::binary_uncompressed: {
348 actual_header.
type );
351 case DataFormat::binary_zlib: {
363 LogOut().fmt(
"time: {}; {} entities of field {} read.\n",
369 Tokenizer::Position pos,
bool boundary_domain)
373 tok_.set_position( pos );
376 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
380 }
catch (boost::bad_lexical_cast &) {
381 THROW(ExcWrongFormat() << EI_Type(
"DataArray tag") << EI_TokenizerMsg(
tok_.position_msg())
382 << EI_MeshFile(
tok_.f_name()) );
388 Tokenizer::Position pos,
bool boundary_domain,
DataType value_type)
395 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
403 Tokenizer::Position pos,
bool boundary_domain,
DataType value_type)
411 block_sizes.reserve(n_blocks);
412 for (uint64_t i = 0; i < n_blocks; ++i) {
416 stringstream decompressed_data;
417 uint64_t decompressed_data_size = 0;
418 for (uint64_t i = 0; i < n_blocks; ++i) {
419 uint64_t decompressed_block_size = (i==n_blocks-1 && p_size>0) ? p_size : u_size;
420 uint64_t compressed_block_size = block_sizes[i];
423 data_stream_->read(&data_block[0], compressed_block_size);
431 strm.next_in = (Bytef *)(&data_block[0]);
432 strm.avail_in = compressed_block_size;
433 strm.next_out = (Bytef *)(&buffer[0]);
434 strm.avail_out = decompressed_block_size;
438 inflate(&strm, Z_NO_FLUSH);
442 decompressed_data << std::string(buffer.begin(), buffer.end());
443 decompressed_data_size += decompressed_block_size;
448 uint64_t data_size = decompressed_data_size /
type_value_size(value_type);
450 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
470 unsigned int i_node, i_elm_node;
474 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of Points section. Did you call create_node_element_caches()?\n");
478 unsigned int n_nodes = node_vec.size() / 3;
479 node_ids.resize(n_nodes);
480 for (
unsigned int i=0; i<n_nodes; ++i) {
481 arma::vec3 point = { node_vec[3*i], node_vec[3*i+1], node_vec[3*i+2] };
482 int found_i_node = -1;
483 bih_tree.
find_point(point, searched_elements);
491 if (found_i_node == -1) found_i_node = i_elm_node;
492 else if (found_i_node != i_elm_node) {
493 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"duplicate nodes found in GMSH file")
494 << EI_VTKFile(
tok_.f_name()));
499 if (found_i_node == -1) {
500 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"no node found in GMSH file") << EI_VTKFile(
tok_.f_name()));
502 node_ids[i] = (
unsigned int)found_i_node;
503 searched_elements.clear();
510 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
520 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of connectivity section. Did you call create_node_element_caches()?\n");
530 unsigned int i_con = 0, last_offset=0, dim;
531 for (
unsigned int i=0; i<offsets_vec.size(); ++i) {
532 dim = offsets_vec[i] - last_offset - 1;
533 for ( ; i_con<offsets_vec[i]; ++i_con ) {
534 node_list.push_back( node_ids[connectivity_vec[i_con]] );
537 for (
auto i_elm : candidate_list) {
538 if ( mesh.
element( i_elm )->dim() == dim ) result_list.push_back(i_elm);
540 if (result_list.size() != 1) {
541 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"intersect_element_lists must produce one element")
542 << EI_VTKFile(
tok_.f_name()));
547 last_offset = offsets_vec[i];
571 ASSERT(it !=
element_data_values_->end()).error(
"Missing cache of Points section. Did you call create_node_element_caches()?\n");
575 unsigned int n_nodes = vect.size()/3;
578 for (
unsigned int i=0, ivec=0; i<n_nodes; ++i) {
579 point(0)=vect[ivec]; ++ivec;
580 point(1)=vect[ivec]; ++ivec;
581 point(2)=vect[ivec]; ++ivec;
592 ASSERT(offset_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
597 ASSERT(conn_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
604 unsigned int i_con = 0, last_offset=0, dim;
606 for (
unsigned int i=0; i<offsets_vec.size(); ++i) {
607 dim = offsets_vec[i] - last_offset - 1;
608 for ( ; i_con<offsets_vec[i]; ++i_con ) {
609 node_list.push_back( connectivity_vec[i_con] );
614 last_offset = offsets_vec[i];
631 HeaderQuery header_params(
"Points", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
632 auto point_header = this->
find_header(header_params);
635 this->get_element_data<double>(point_header.n_entities, point_header.n_components,
false, 0 );
638 HeaderQuery offsets_params(
"offsets", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
639 auto offset_header = this->
find_header(offsets_params);
641 std::vector<unsigned int> &offsets_vec = *( this->get_element_data<unsigned int>(offset_header.n_entities, offset_header.n_components,
false, 0 ) );
644 HeaderQuery con_params(
"connectivity", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
646 con_header.n_entities = offsets_vec[offsets_vec.size()-1];
648 this->get_element_data<unsigned int>(con_header.n_entities, con_header.n_components,
false, 0 );
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)
int IdxInt
Define integers that are indices into large arrays (elements, nodes, dofs etc.)
#define FOR_ELEMENT_NODES(i, j)
HeaderTable header_table_
Table with data of DataArray headers.
void read_nodes(Mesh *mesh)
static const double point_tolerance
Tolerance during comparison point data with GMSH nodes.
DataFormat data_format_
variants of data format (ascii, appended, compressed appended)
template unsigned int read_binary_value< unsigned int >(std::istream &data_stream)
void add_node(unsigned int node_id, arma::vec3 coords)
Add new node of given id and coordinates to mesh.
VtkMeshReader(const FilePath &file_name)
template uint64_t read_binary_value< uint64_t >(std::istream &data_stream)
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
std::string format(CStringRef format_str, ArgList args)
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.
DataType get_data_type(std::string type_str)
Get DataType by value of string.
std::string data_section_name_
Store name of field data section specify for type of mesh file.
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.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
#define LogOut()
Macro defining 'log' record of log.
uint64_t read_header_type(DataType data_header_type, std::istream &data_stream)
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
virtual void read_ascii_data(Tokenizer &tok, unsigned int n_components, unsigned int i_row)=0
Class for O(log N) lookup for intersections with a set of bounding boxes.
unsigned int index(const T *pointer) const
virtual ~VtkMeshReader()
Destructor.
DataType header_type_
header type of VTK file (only for appended data)
const BIHTree & get_bih_tree()
Getter for BIH. Creates and compute BIH at first call.
unsigned int n_read_
store count of read entities
Dedicated class for storing path to input and output files.
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 find_point(const Space< 3 >::Point &point, std::vector< unsigned int > &result_list, bool full_list=false) const
vector< IdxInt > bulk_elements_id_
void create_node_element_caches()
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
T read_binary_value(std::istream &data_stream)
void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain) override
ComponentDataPtr get_component_data(unsigned int component_idx)
Return vector of element data for get component.
MeshDataHeader actual_header_
Header of actual loaded data.
void intersect_element_lists(vector< unsigned int > const &nodes_list, vector< unsigned int > &intersection_element_list)
#define WarningOut()
Macro defining 'warning' record of log.
void add_element(unsigned int elm_id, unsigned int dim, unsigned int region_id, unsigned int partition_id, std::vector< unsigned int > node_ids)
Add new element of given id to mesh.
bool has_compatible_mesh_
DataType
Types of VTK data (value 'undefined' for empty value)
Tokenizer tok_
Tokenizer used for reading ASCII file format.
virtual void read_binary_data(std::istream &data_stream, unsigned int n_components, unsigned int i_row)=0
unsigned int type_value_size(DataType data_type)
Return size of value of data_type.
void reserve_node_size(unsigned int n_nodes)
Reserve size of node vector.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Tokenizer::Position get_appended_position()
Get position of AppendedData tag in VTK file.
std::vector< int > const & get_element_vector(bool boundary_domain)
NodeVector node_vector
Vector of nodes of the mesh.
ElementVector element
Vector of elements of the mesh.
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.