From 4aaea1cf33192fdff89c31335c07d7526b2fe89b Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Mon, 20 Jan 2025 17:17:50 +0800 Subject: [PATCH] tmp --- lib/io/gmsh_io.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/lib/io/gmsh_io.h b/lib/io/gmsh_io.h index cb4c7d0..1da881b 100644 --- a/lib/io/gmsh_io.h +++ b/lib/io/gmsh_io.h @@ -65,6 +65,7 @@ namespace gctl template void read_node(array> &out_nodes); template void read_node(array> &out_nodes); + template void read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag = nullptr); template void read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag = nullptr); template void read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag = nullptr); template void read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag = nullptr); @@ -151,6 +152,14 @@ namespace gctl return; } + template + void gmshio::read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag) + { + initialized(Input); + read_gmsh_element(gmsh_in, out_elements, nodes, in_packed, ele_tag); + return; + } + template void gmshio::read_element(array> &out_elements, const array> &nodes, _2i_vector *ele_tag) { @@ -1135,6 +1144,105 @@ namespace gctl return; } + /** + * @brief Read element index from a Gmsh file. + * + * @param[in] infile The input file stream + * @param element The output element object array + * @param node The node array + * @param[in] packed Indicates whether the index in the node file starts from zero. The + * index is deemed to be started with one if this option is false. The default value of this + * variable is true. + * @param[in] ele_tag Return elements' tags by a 2D integer vector. + * + */ + template + void read_gmsh_element(std::ifstream &infile, array> &element, + const array> &node, index_packed_e packed = Packed, + _2i_vector *ele_tag = nullptr) + { + if (node.empty()) + throw runtime_error("The input array is empty. From gctl::read_gmsh_element(...)"); + + // 重置数据 + if (!element.empty()) element.clear(); + // 重置标签 + if (ele_tag != nullptr && !ele_tag->empty()) ele_tag->clear(); + + // 将文件指针重置到文件头 + infile.clear(std::ios::goodbit); + infile.seekg(std::ios::beg); + + int i_size = 0, ele_count = 0; + int tmp_int, ele_type, attri_num; + int tmp_index[2]; + std::string tmp_str; + std::stringstream tmp_ss; + while(getline(infile,tmp_str)) + { + if (tmp_str == "$Elements") //读入模型空间顶点集 msh文件版本为2.2 + { + getline(infile,tmp_str); + gctl::str2ss(tmp_str, tmp_ss); + tmp_ss >> i_size; //第一个数为顶点的个数 + + // 我们先用一个临时的向量来储存元素 + edge2d tmp_edge; + std::vector element_vec; + std::vector tmp_tag; + + for (int i = 0; i < i_size; i++) + { + getline(infile,tmp_str); + str2ss(tmp_str, tmp_ss); + tmp_ss >> tmp_int >> ele_type >> attri_num; + if (ele_type == 1) + { + tmp_edge.id = ele_count; + + tmp_tag.clear(); + for (int a = 0; a < attri_num; a++) + { + tmp_ss >> tmp_int; + tmp_tag.push_back(tmp_int); + } + + if (ele_tag != nullptr) + ele_tag->push_back(tmp_tag); + + tmp_ss >> tmp_index[0] >> tmp_index[1]; + + if (packed == NotPacked) + { + for (int j = 0; j < 2; j++) + tmp_edge.vert[j] = node.get(tmp_index[j]-1); + } + else + { + for (int j = 0; j < 2; j++) + tmp_edge.vert[j] = node.get(tmp_index[j]); + } + element_vec.push_back(tmp_edge); + + ele_count++; + } + } + + //将元素转移到向量上来 + element.resize(element_vec.size()); + for (int i = 0; i < element.size(); i++) + { + element[i].id = element_vec[i].id; + for (int j = 0; j < 2; j++) + element[i].vert[j] = element_vec[i].vert[j]; + } + destroy_vector(element_vec); + break; + } + } + return; + } + /** * @brief Read element index from a Gmsh file. *