22 #include <pugixml.hpp>
23 #include "boost/lexical_cast.hpp"
44 data_stream.read(
reinterpret_cast<char *
>(&val),
sizeof(val));
56 ASSERT(
false).error(
"Unsupported header_type!\n");
102 }
catch(ExcWrongType &e) {
103 e << EI_SectionTypeName(
"base parameter header_type");
111 THROW( ExcWrongType() << EI_ErrMessage(
"Forbidden") << EI_SectionTypeName(
"base parameter header_type")
112 << EI_VTKFile(
tok_.f_name()));
114 std::string compressor = vtk_node.attribute(
"compressor").as_string();
115 if (compressor ==
"vtkZLibDataCompressor")
121 pugi::xml_node piece_node = vtk_node.child(
"UnstructuredGrid").child(
"Piece");
122 n_nodes = piece_node.attribute(
"NumberOfPoints").as_uint();
123 n_elements = piece_node.attribute(
"NumberOfCells").as_uint();
129 Tokenizer::Position appended_pos;
133 tok_.set_position( Tokenizer::Position() );
134 if (!
tok_.skip_to(
"AppendedData"))
135 THROW(ExcMissingTag() << EI_TagType(
"tag") << EI_TagName(
"AppendedData") << EI_VTKFile(
tok_.f_name()) );
137 appended_pos =
tok_.get_position();
148 appended_pos.line_counter_++;
152 data_stream_ =
new std::ifstream(
tok_.f_name(), std::ios_base::in | std::ios_base::binary );
162 header.
field_name = node.attribute(
"Name").as_string();
166 }
catch(ExcWrongType &e) {
167 e << EI_SectionTypeName(
"DataArray " + header.
field_name);
170 if (disc == OutputTime::DiscreteSpace::NATIVE_DATA) {
171 header.
n_components = node.attribute(
"n_dofs_per_element").as_uint();
174 header.
n_components = node.attribute(
"NumberOfComponents").as_uint(1);
177 std::string
format = node.attribute(
"format").as_string();
180 THROW(ExcInvalidFormat() << EI_FieldName(header.
field_name) << EI_ExpectedFormat(
"ascii") << EI_VTKFile(
tok_.f_name()) );
181 std::streampos file_pos = pos.file_position_;
182 file_pos += node.attribute(
"offset").as_uint();
183 header.
position = Tokenizer::Position( file_pos, pos.line_counter_, pos.line_position_ );
184 }
else if (
format==
"ascii") {
186 THROW(ExcInvalidFormat() << EI_FieldName(header.
field_name) << EI_ExpectedFormat(
"appended") << EI_VTKFile(
tok_.f_name()) );
188 tok_.set_position( Tokenizer::Position() );
190 std::string found_str = (is_point) ?
"<Points>" :
"Name=\"" + header.
field_name +
"\"";
191 if (!
tok_.skip_to(found_str))
192 THROW(ExcMissingTag() << EI_TagType(
"DataArray tag") << EI_TagName(header.
field_name) << EI_VTKFile(
tok_.f_name()) );
194 if (is_point)
tok_.skip_to(
"DataArray");
198 THROW(ExcUnknownFormat() << EI_FieldName(header.
field_name) << EI_VTKFile(
tok_.f_name()) );
207 pugi::xml_document doc;
208 doc.load_file(
tok_.f_name().c_str() );
209 unsigned int n_nodes, n_elements;
216 Tokenizer::Position appended_pos;
223 pugi::xml_node node = doc.child(
"VTKFile").child(
"UnstructuredGrid").child(
"Piece");
228 auto points_header =
create_header( node.child(
"Points").child(
"DataArray"), n_nodes, appended_pos,
229 OutputTime::DiscreteSpace::MESH_DEFINITION );
230 points_header.field_name =
"Points";
231 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"Points", points_header) );
232 auto con_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"connectivity"),
233 n_elements, appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
234 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"connectivity", con_header) );
235 auto offsets_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"offsets"),
236 n_elements, appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
237 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"offsets", offsets_header) );
238 auto types_header =
create_header( node.child(
"Cells").find_child_by_attribute(
"DataArray",
"Name",
"types"), n_elements,
239 appended_pos, OutputTime::DiscreteSpace::MESH_DEFINITION );
240 header_table_.insert( std::pair<std::string, MeshDataHeader>(
"types", types_header) );
242 pugi::xml_node point_node = node.child(
"PointData");
243 for (pugi::xml_node subnode = point_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
244 auto header =
create_header( subnode, n_nodes, appended_pos, OutputTime::DiscreteSpace::NODE_DATA );
245 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
248 pugi::xml_node cell_node = node.child(
"CellData");
249 for (pugi::xml_node subnode = cell_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
250 auto header =
create_header( subnode, n_elements, appended_pos, OutputTime::DiscreteSpace::ELEM_DATA );
251 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
254 pugi::xml_node native_node = node.child(
"Flow123dData");
255 for (pugi::xml_node subnode = native_node.child(
"DataArray"); subnode; subnode = subnode.next_sibling(
"DataArray")) {
256 auto header =
create_header( subnode, n_elements, appended_pos, OutputTime::DiscreteSpace::NATIVE_DATA );
257 header_table_.insert( std::pair<std::string, MeshDataHeader>(header.field_name, header) );
268 THROW( ExcFieldNameNotFound() << EI_FieldName(header_query.
field_name) << EI_MeshFile(
tok_.f_name()));
269 }
else if (count == 1) {
273 if (header_query.
discretization != table_it->second.discretization) {
274 if (header_query.
discretization != OutputTime::DiscreteSpace::UNDEFINED && header_query.
discretization != OutputTime::DiscreteSpace::NATIVE_DATA) {
276 "Invalid value of 'input_discretization' for field '{}', time: {}.\nCorrect discretization type will be used.\n",
282 if (header_query.
discretization == OutputTime::DiscreteSpace::NATIVE_DATA)
284 THROW(ExcInvalidDofHandler() << EI_FieldName(header_query.
field_name) << EI_VTKFile(
tok_.f_name()) );
295 THROW( ExcMissingFieldDiscretization() << EI_FieldName(header_query.
field_name) << EI_Time(header_query.
time) << EI_MeshFile(
tok_.f_name()));
320 if (
it != types.end()) {
323 THROW( ExcWrongType() << EI_ErrMessage(
"Unknown") << EI_VTKFile(
tok_.f_name()));
333 static const std::vector<unsigned int> sizes = { 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 0 };
335 return sizes[data_type];
341 bool boundary_domain) {
343 ASSERT(!boundary_domain).error(
"Reading VTK data of boundary elements is not supported yet!\n");
346 case DataFormat::ascii: {
350 case DataFormat::binary_uncompressed: {
353 actual_header.
type );
356 case DataFormat::binary_zlib: {
368 LogOut().fmt(
"time: {}; {} entities of field {} read.\n",
374 Tokenizer::Position pos,
bool boundary_domain)
378 tok_.set_position( pos );
381 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
385 }
catch (boost::bad_lexical_cast &) {
386 THROW(ExcWrongFormat() << EI_Type(
"DataArray tag") << EI_TokenizerMsg(
tok_.position_msg())
387 << EI_MeshFile(
tok_.f_name()) );
393 Tokenizer::Position pos,
bool boundary_domain,
DataType value_type)
400 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
408 Tokenizer::Position pos,
bool boundary_domain,
DataType value_type)
416 block_sizes.reserve(n_blocks);
417 for (uint64_t i = 0; i < n_blocks; ++i) {
421 stringstream decompressed_data;
422 uint64_t decompressed_data_size = 0;
423 for (uint64_t i = 0; i < n_blocks; ++i) {
424 uint64_t decompressed_block_size = (i==n_blocks-1 && p_size>0) ? p_size : u_size;
425 uint64_t compressed_block_size = block_sizes[i];
428 data_stream_->read(&data_block[0], compressed_block_size);
436 strm.next_in = (Bytef *)(&data_block[0]);
437 strm.avail_in = compressed_block_size;
438 strm.next_out = (Bytef *)(&buffer[0]);
439 strm.avail_out = decompressed_block_size;
443 inflate(&strm, Z_NO_FLUSH);
447 decompressed_data << std::string(buffer.begin(), buffer.end());
448 decompressed_data_size += decompressed_block_size;
453 uint64_t data_size = decompressed_data_size /
type_value_size(value_type);
455 for (
unsigned int i_row = 0; i_row < n_entities; ++i_row) {
475 unsigned int i_node, i_elm_node;
479 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of Points section. Did you call create_node_element_caches()?\n");
483 unsigned int n_nodes = node_vec.size() / 3;
484 node_ids.resize(n_nodes);
485 for (
unsigned int i=0; i<n_nodes; ++i) {
486 arma::vec3 point = { node_vec[3*i], node_vec[3*i+1], node_vec[3*i+2] };
487 int found_i_node = -1;
488 bih_tree.
find_point(point, searched_elements);
492 for (i_node=0; i_node<ele->
n_nodes(); i_node++)
496 if (found_i_node == -1) found_i_node = i_elm_node;
497 else if (found_i_node != i_elm_node) {
498 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"duplicate nodes found in GMSH file")
499 << EI_VTKFile(
tok_.f_name()));
504 if (found_i_node == -1) {
505 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"no node found in GMSH file") << EI_VTKFile(
tok_.f_name()));
507 node_ids[i] = (
unsigned int)found_i_node;
508 searched_elements.clear();
515 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
525 ASSERT(field_it !=
element_data_values_->end()).error(
"Missing cache of connectivity section. Did you call create_node_element_caches()?\n");
535 unsigned int i_con = 0, last_offset=0, dim;
536 for (
unsigned int i=0; i<offsets_vec.size(); ++i) {
537 dim = offsets_vec[i] - last_offset - 1;
538 for ( ; i_con<offsets_vec[i]; ++i_con ) {
539 node_list.push_back( node_ids[connectivity_vec[i_con]] );
542 for (
auto i_elm : candidate_list) {
545 if (result_list.size() != 1) {
546 THROW( ExcIncompatibleMesh() << EI_ErrMessage(
"intersect_element_lists must produce one element")
547 << EI_VTKFile(
tok_.f_name()));
552 last_offset = offsets_vec[i];
580 unsigned int n_nodes = vect.size()/3;
583 for (
unsigned int i=0, ivec=0; i<n_nodes; ++i) {
584 point(0)=vect[ivec]; ++ivec;
585 point(1)=vect[ivec]; ++ivec;
586 point(2)=vect[ivec]; ++ivec;
597 ASSERT(offset_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
602 ASSERT(conn_it !=
element_data_values_->end()).error(
"Missing cache of offsets section. Did you call create_node_element_caches()?\n");
610 unsigned int i_con = 0, last_offset=0, dim;
612 for (
unsigned int i=0; i<offsets_vec.size(); ++i) {
613 dim = offsets_vec[i] - last_offset - 1;
614 for ( ; i_con<offsets_vec[i]; ++i_con ) {
615 node_list.push_back( connectivity_vec[i_con] );
620 last_offset = offsets_vec[i];
639 HeaderQuery header_params(
"Points", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
640 auto point_header = this->
find_header(header_params);
643 this->get_element_data<double>(point_header.n_entities, point_header.n_components,
false, 0 );
646 HeaderQuery offsets_params(
"offsets", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
647 auto offset_header = this->
find_header(offsets_params);
649 std::vector<unsigned int> &offsets_vec = *( this->get_element_data<unsigned int>(offset_header.n_entities, offset_header.n_components,
false, 0 ) );
652 HeaderQuery con_params(
"connectivity", 0.0, OutputTime::DiscreteSpace::MESH_DEFINITION);
654 con_header.n_entities = offsets_vec[offsets_vec.size()-1];
656 this->get_element_data<unsigned int>(con_header.n_entities, con_header.n_components,
false, 0 );