diff --git a/example/mesh_ex10.cpp b/example/mesh_ex10.cpp index cbf2dd2..fdf3300 100644 --- a/example/mesh_ex10.cpp +++ b/example/mesh_ex10.cpp @@ -32,6 +32,6 @@ int main(int argc, char *argv[]) gctl::regular_mesh_sph_3d rm_3ds; rm_3ds.init("mesh-1", "null", 30.25, 30.25, 2005, 0.5, 0.5, 10, 40, 40, 50); rm_3ds.add_data(gctl::ElemData, gctl::Scalar, "data-1", 2.5); - rm_3ds.save_gmsh("mesh_sample10",gctl::OverWrite, gctl::NotPacked); + rm_3ds.save_gmsh_withdata("mesh_sample10",gctl::OverWrite, gctl::NotPacked); return 0; } \ No newline at end of file diff --git a/example/mesh_ex6.cpp b/example/mesh_ex6.cpp index 53f01b2..dac659c 100644 --- a/example/mesh_ex6.cpp +++ b/example/mesh_ex6.cpp @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) try } } - rg_mesh.save_gmsh("sample6-out", gctl::OverWrite); + rg_mesh.save_gmsh_withdata("sample6-out", gctl::OverWrite); return 0; } catch(std::exception &e) diff --git a/example/mesh_ex7.cpp b/example/mesh_ex7.cpp index 3750a5e..2c36e9f 100644 --- a/example/mesh_ex7.cpp +++ b/example/mesh_ex7.cpp @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) try } } - rg_mesh.save_gmsh("sample7-out", "double-data", gctl::OverWrite); + rg_mesh.save_gmsh_withdata("sample7-out", "double-data", gctl::OverWrite); return 0; } catch(std::exception &e) diff --git a/example/mesh_ex8.cpp b/example/mesh_ex8.cpp index 9616ac1..532eede 100644 --- a/example/mesh_ex8.cpp +++ b/example/mesh_ex8.cpp @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) try t_mesh.load_triangle("sample8", gctl::Packed); t_mesh.add_data(gctl::ElemData, gctl::Scalar, "example", 1.0); - t_mesh.save_gmsh("sample8-out", "example", gctl::OverWrite, gctl::NotPacked); + t_mesh.save_gmsh_withdata("sample8-out", "example", gctl::OverWrite, gctl::NotPacked); t_mesh.save_binary("sample8-out"); gctl::triangle2d_mesh t2_mesh; diff --git a/example/mesh_ex9.cpp b/example/mesh_ex9.cpp index 611ecf0..bba0587 100644 --- a/example/mesh_ex9.cpp +++ b/example/mesh_ex9.cpp @@ -56,7 +56,7 @@ int main(int argc, char *argv[]) try } } - l2d_mesh.save_gmsh("sample9-out", gctl::OverWrite, gctl::NotPacked); + l2d_mesh.save_gmsh_withdata("sample9-out", gctl::OverWrite, gctl::NotPacked); return 0; } catch(std::exception &e) diff --git a/lib/mesh/linear_mesh_2d.cpp b/lib/mesh/linear_mesh_2d.cpp index 03e1294..1a5f6a2 100644 --- a/lib/mesh/linear_mesh_2d.cpp +++ b/lib/mesh/linear_mesh_2d.cpp @@ -281,17 +281,4 @@ void gctl::linear_mesh_2d::save_gmsh(std::string filename, index_packed_e packed gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; -} - - -void gctl::linear_mesh_2d::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::linear_mesh_2d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} +} \ No newline at end of file diff --git a/lib/mesh/linear_mesh_2d.h b/lib/mesh/linear_mesh_2d.h index df505ef..d4fc000 100644 --- a/lib/mesh/linear_mesh_2d.h +++ b/lib/mesh/linear_mesh_2d.h @@ -40,14 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, double xmin, double ymin, - const array &xsizes, const array &ysizes); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed){} + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为regular_mesh_2d的专有函数 */ @@ -57,6 +57,9 @@ namespace gctl const array &xsizes, const array &ysizes); virtual ~linear_mesh_2d(); + void init(std::string in_name, std::string in_info, double xmin, double ymin, + const array &xsizes, const array &ysizes); + int get_xbnum() const; int get_ybnum() const; double get_xmin() const; @@ -66,10 +69,6 @@ namespace gctl const array *get_xsizes() const; const array *get_ysizes() const; - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - protected: int lm_xbnum, lm_ybnum; double lm_xmin, lm_ymin; diff --git a/lib/mesh/linear_mesh_3d.cpp b/lib/mesh/linear_mesh_3d.cpp index c5df6d9..161e60e 100644 --- a/lib/mesh/linear_mesh_3d.cpp +++ b/lib/mesh/linear_mesh_3d.cpp @@ -371,16 +371,4 @@ void gctl::linear_mesh_3d::save_gmsh(std::string filename, index_packed_e packed gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; -} - -void gctl::linear_mesh_3d::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::linear_mesh_3d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} +} \ No newline at end of file diff --git a/lib/mesh/linear_mesh_3d.h b/lib/mesh/linear_mesh_3d.h index b27b537..9a0684f 100644 --- a/lib/mesh/linear_mesh_3d.h +++ b/lib/mesh/linear_mesh_3d.h @@ -40,15 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, double xmin, double ymin, - double zmin, const array &xsizes, const array &ysizes, - const array &zsizes); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed){} + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为regular_mesh_2d的专有函数 */ @@ -59,6 +58,10 @@ namespace gctl const array &zsizes); virtual ~linear_mesh_3d(); + void init(std::string in_name, std::string in_info, double xmin, double ymin, + double zmin, const array &xsizes, const array &ysizes, + const array &zsizes); + int get_xbnum() const; int get_ybnum() const; int get_zbnum() const; @@ -72,10 +75,6 @@ namespace gctl const array *get_ysizes() const; const array *get_zsizes() const; - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - protected: int lm_xbnum, lm_ybnum, lm_zbnum; double lm_xmin, lm_ymin, lm_zmin; diff --git a/lib/mesh/mesh.cpp b/lib/mesh/mesh.cpp index 3fccb81..fc48f88 100644 --- a/lib/mesh/mesh.cpp +++ b/lib/mesh/mesh.cpp @@ -306,14 +306,15 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, output_type_e out { if (out_mode == OverWrite) save_gmsh(filename, packed); - std::ofstream outfile; - gctl::open_outfile(outfile, filename, ".msh", std::ios::out|std::ios::app); + //std::ofstream outfile; + //gctl::open_outfile(outfile, filename, ".msh", std::ios::out|std::ios::app); for (size_t i = 0; i < datalist_.size(); i++) { if (datalist_[i].loctype_ == NodeData) { - if (datalist_[i].valtype_ == Scalar) gctl::save_gmsh_data(outfile, datalist_[i].name_, datalist_[i].datval_, gctl::NodeData, packed); + //gctl::save_gmsh_data(outfile, datalist_[i].name_, datalist_[i].datval_, gctl::NodeData, packed); + if (datalist_[i].valtype_ == Scalar) meshio_.save_data(datalist_[i].name_, datalist_[i].datval_, NodeData); else if (datalist_[i].valtype_ == Vector) { array vec_data(node_num_); @@ -324,7 +325,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, output_type_e out vec_data[j].z = datalist_[i].datval_[3*j+2]; } - gctl::save_gmsh_data(outfile, datalist_[i].name_, vec_data, gctl::NodeData, packed); + //gctl::save_gmsh_data(outfile, datalist_[i].name_, vec_data, gctl::NodeData, packed); + meshio_.save_data(datalist_[i].name_, vec_data, NodeData); } //else if (datalist_[i].valtype_ == Tensor) //{ @@ -348,7 +350,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, output_type_e out } else if (datalist_[i].loctype_ == ElemData) { - if (datalist_[i].valtype_ == Scalar) gctl::save_gmsh_data(outfile, datalist_[i].name_, datalist_[i].datval_, gctl::ElemData, packed); + //gctl::save_gmsh_data(outfile, datalist_[i].name_, datalist_[i].datval_, gctl::ElemData, packed); + if (datalist_[i].valtype_ == Scalar) meshio_.save_data(datalist_[i].name_, datalist_[i].datval_, ElemData); else if (datalist_[i].valtype_ == Vector) { array vec_data(ele_num_); @@ -359,7 +362,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, output_type_e out vec_data[j].z = datalist_[i].datval_[3*j+2]; } - gctl::save_gmsh_data(outfile, datalist_[i].name_, vec_data, gctl::ElemData, packed); + //gctl::save_gmsh_data(outfile, datalist_[i].name_, vec_data, gctl::ElemData, packed); + meshio_.save_data(datalist_[i].name_, vec_data, ElemData); } //else if (datalist_[i].valtype_ == Tensor) //{ @@ -383,8 +387,6 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, output_type_e out } else throw std::runtime_error("[gctl::base_mesh] Invalid input data location."); } - - outfile.close(); return; } @@ -392,13 +394,14 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, std::string datna { if (out_mode == OverWrite) save_gmsh(filename, packed); - std::ofstream outfile; - gctl::open_outfile(outfile, filename, ".msh", std::ios::out|std::ios::app); + //std::ofstream outfile; + //gctl::open_outfile(outfile, filename, ".msh", std::ios::out|std::ios::app); const meshdata &data = get_data(datname); if (data.loctype_ == NodeData) { - if (data.valtype_ == Scalar) gctl::save_gmsh_data(outfile, data.name_, data.datval_, gctl::NodeData, packed); + //gctl::save_gmsh_data(outfile, data.name_, data.datval_, gctl::NodeData, packed); + if (data.valtype_ == Scalar) meshio_.save_data(data.name_, data.datval_, NodeData); else if (data.valtype_ == Vector) { array vec_data(node_num_); @@ -409,7 +412,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, std::string datna vec_data[j].z = data.datval_[3*j+2]; } - gctl::save_gmsh_data(outfile, data.name_, vec_data, gctl::NodeData, packed); + //gctl::save_gmsh_data(outfile, data.name_, vec_data, gctl::NodeData, packed); + meshio_.save_data(data.name_, vec_data, NodeData); } //else if (data.valtype_ == Tensor) //{ @@ -433,7 +437,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, std::string datna } else if (data.loctype_ == ElemData) { - if (data.valtype_ == Scalar) gctl::save_gmsh_data(outfile, data.name_, data.datval_, gctl::ElemData, packed); + //gctl::save_gmsh_data(outfile, data.name_, data.datval_, gctl::ElemData, packed); + if (data.valtype_ == Scalar) meshio_.save_data(data.name_, data.datval_, ElemData); else if (data.valtype_ == Vector) { array vec_data(ele_num_); @@ -444,7 +449,8 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, std::string datna vec_data[j].z = data.datval_[3*j+2]; } - gctl::save_gmsh_data(outfile, data.name_, vec_data, gctl::ElemData, packed); + //gctl::save_gmsh_data(outfile, data.name_, vec_data, gctl::ElemData, packed); + meshio_.save_data(data.name_, vec_data, ElemData); } //else if (data.valtype_ == Tensor) //{ @@ -467,8 +473,59 @@ void gctl::base_mesh::save_gmsh_withdata(std::string filename, std::string datna else throw std::runtime_error("[gctl::base_mesh] Invalid input data value type."); } else throw std::runtime_error("[gctl::base_mesh] Invalid input data location."); + return; +} - outfile.close(); +void gctl::base_mesh::load_gmsh_withdata(std::string filename, index_packed_e packed) +{ + load_gmsh(filename, packed); + + _2d_vector all_node_data, all_elem_data; + _1s_vector nodedata_names, elemdata_names; + meshio_.read_data(all_node_data, nodedata_names, NodeData); + meshio_.read_data(all_elem_data, elemdata_names, ElemData); + + array tmp_data; + meshdata *data_ptr = nullptr; + for (size_t i = 0; i < nodedata_names.size(); i++) + { + add_data(NodeData, Scalar, nodedata_names[i], 0.0); + data_ptr = get_data_ptr(nodedata_names[i]); + if (data_ptr != nullptr) + { + for (size_t n = 0; n < node_num_; n++) + { + data_ptr->datval_[i] = all_node_data[i][n]; + } + } + } + + for (size_t i = 0; i < elemdata_names.size(); i++) + { + add_data(ElemData, Scalar, elemdata_names[i], 0.0); + data_ptr = get_data_ptr(elemdata_names[i]); + if (data_ptr != nullptr) + { + for (size_t n = 0; n < ele_num_; n++) + { + data_ptr->datval_[i] = all_elem_data[i][n]; + } + } + } + return; +} + +void gctl::base_mesh::load_gmsh_withdata(std::string filename, std::string datname, + mesh_data_type_e type, index_packed_e packed) +{ + load_gmsh(filename, packed); + + array tmp_data; + meshio_.read_data(tmp_data, datname); + + if (type == NodeData) add_data(NodeData, datname, tmp_data); + else if (type == ElemData) add_data(NodeData, datname, tmp_data); + else throw std::runtime_error("[gctl::base_mesh] Invalid mesh data type."); return; } diff --git a/lib/mesh/mesh.h b/lib/mesh/mesh.h index 175a464..949dc8e 100644 --- a/lib/mesh/mesh.h +++ b/lib/mesh/mesh.h @@ -237,7 +237,7 @@ namespace gctl * @param datname 数据名 */ void remove_data(std::string datname); - + /** * @brief 保存网格到Gmsh文件,并保存所有数据。 * @@ -257,6 +257,22 @@ namespace gctl */ void save_gmsh_withdata(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); + /** + * @brief 保存网格到Gmsh文件,并读入所有数据。 + * + * @param filename 文件名 + * @param packed 索引是否为紧凑(从0开始) + */ + void load_gmsh_withdata(std::string filename, index_packed_e packed = Packed); + + /** + * @brief 保存网格到Gmsh文件,并读入特定数据。 + * + * @param filename 文件名 + * @param packed 索引是否为紧凑(从0开始) + */ + void load_gmsh_withdata(std::string filename, std::string datname, mesh_data_type_e type, index_packed_e packed = Packed); + /** * @brief (基类纯虚函数)保存网格到Gmsh文件。 * @@ -266,10 +282,12 @@ namespace gctl virtual void save_gmsh(std::string filename, index_packed_e packed = Packed) = 0; /** - * @brief (基类纯虚函数)显示网格的维度信息 - * + * @brief (基类纯虚函数)读入Gmsh文件。 + * + * @param filename 文件名 + * @param packed 索引是否为紧凑(从0开始) */ - virtual void show_mesh_dimension(std::ostream &os) const = 0; + virtual void load_gmsh(std::string filename, index_packed_e packed = Packed) = 0; /** * @brief (基类纯虚函数)读入二进制网格文件 @@ -285,11 +303,17 @@ namespace gctl */ virtual void save_binary(std::string filename) = 0; + /** + * @brief (基类纯虚函数)显示网格的维度信息 + */ + virtual void show_mesh_dimension(std::ostream &os) const = 0; + protected: mesh_type_e meshtype_; mesh_dim_e meshdim_; std::string meshname_; std::string meshinfo_; + gmshio meshio_; int node_num_, ele_num_; bool initialized_; diff --git a/lib/mesh/regular_grid.cpp b/lib/mesh/regular_grid.cpp index 0b28627..819d000 100644 --- a/lib/mesh/regular_grid.cpp +++ b/lib/mesh/regular_grid.cpp @@ -659,18 +659,6 @@ void gctl::regular_grid::save_gmsh(std::string filename, index_packed_e packed) return; } -void gctl::regular_grid::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::regular_grid::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} - void gctl::regular_grid::save_text(std::string filename, const array &datname) { meshdata &curr_data = get_data(datname[0]); diff --git a/lib/mesh/regular_grid.h b/lib/mesh/regular_grid.h index 4885950..2068fff 100644 --- a/lib/mesh/regular_grid.h +++ b/lib/mesh/regular_grid.h @@ -48,6 +48,8 @@ namespace gctl /** * 以下为mesh类型的虚函数实现 */ + + void load_gmsh(std::string filename, index_packed_e packed = Packed){} /** * @brief (基类纯虚函数的具体实现)保存网格到Gmsh文件。 @@ -57,11 +59,6 @@ namespace gctl */ void save_gmsh(std::string filename, index_packed_e packed = Packed); - /** - * @brief (基类纯虚函数的具体实现)显示网格的维度信息 - */ - void show_mesh_dimension(std::ostream &os) const; - /** * @brief (基类纯虚函数的具体实现)读入二进制网格文件 * @@ -76,6 +73,11 @@ namespace gctl */ void save_binary(std::string filename); + /** + * @brief (基类纯虚函数的具体实现)显示网格的维度信息 + */ + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为regular_grid的专有函数 */ @@ -198,25 +200,6 @@ namespace gctl */ void save_surfer_grid(std::string filename, std::string datname, surfer_file_type_e grid_type = Surfer7Grid); - /** - * @brief 保存规则网格数据到Gmsh文件中 - * - * @param filename 文件名 - * @param out_mode 输出模式 - * @param packed 索引是否为紧凑(从0开始) - */ - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - - /** - * @brief 保存规则网格数据到Gmsh文件中 - * - * @param filename 文件名 - * @param datname 数据名称 - * @param out_mode 输出模式 - * @param packed 索引是否为紧凑(从0开始) - */ - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - /** * @brief 保存规则网格数据到文本文件中 * diff --git a/lib/mesh/regular_mesh_2d.cpp b/lib/mesh/regular_mesh_2d.cpp index 209e64d..d8742df 100644 --- a/lib/mesh/regular_mesh_2d.cpp +++ b/lib/mesh/regular_mesh_2d.cpp @@ -218,16 +218,4 @@ void gctl::regular_mesh_2d::save_gmsh(std::string filename, index_packed_e packe gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; -} - -void gctl::regular_mesh_2d::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::regular_mesh_2d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} +} \ No newline at end of file diff --git a/lib/mesh/regular_mesh_2d.h b/lib/mesh/regular_mesh_2d.h index 39f7bb4..6eb4405 100644 --- a/lib/mesh/regular_mesh_2d.h +++ b/lib/mesh/regular_mesh_2d.h @@ -40,14 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, int xbnum, int ybnum, - double xmin, double ymin, double xsize, double ysize); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed){} + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为regular_mesh_2d的专有函数 */ @@ -57,6 +57,9 @@ namespace gctl double xmin, double ymin, double xsize, double ysize); virtual ~regular_mesh_2d(); + void init(std::string in_name, std::string in_info, int xbnum, int ybnum, + double xmin, double ymin, double xsize, double ysize); + int get_xbnum() const; int get_ybnum() const; double get_xmin() const; @@ -64,10 +67,6 @@ namespace gctl double get_xsize() const; double get_ysize() const; - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - protected: int rm_xbnum, rm_ybnum; double rm_xmin, rm_ymin; diff --git a/lib/mesh/regular_mesh_3d.cpp b/lib/mesh/regular_mesh_3d.cpp index 8883add..49e42ee 100644 --- a/lib/mesh/regular_mesh_3d.cpp +++ b/lib/mesh/regular_mesh_3d.cpp @@ -265,44 +265,4 @@ void gctl::regular_mesh_3d::save_gmsh(std::string filename, index_packed_e packe gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; -} - -void gctl::regular_mesh_3d::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::regular_mesh_3d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} - -/* -void gctl::regular_mesh_3d::edit_data(std::string datname, physical_type_e p_type, value_operator_e v_type, std::string para_str, double in_val) -{ - std::string err_str; - meshdata *dat_ptr = get_data(datname); - if (dat_ptr->get_valtype() != Double) - { - err_str = "Incompatible value type of data: " << datname << ". From regular_mesh_3d::edit_data(...)"; - throw runtime_error(err_str); - } - - array* val_ptr = (array*) dat_ptr->get_datval(); - // 按形体类型解析参数字符串 - if (p_type == Block) - { - double xmin, xmax, ymin, ymax, zmin, zmax; - block_parameter_parser(para_str, xmin, xmax, ymin, ymax, zmin, zmax); - - - } - else - { - err_str = "Invalid physical entity type. From regular_mesh_3d::edit_data(...)"; - throw runtime_error(err_str); - } -} -*/ +} \ No newline at end of file diff --git a/lib/mesh/regular_mesh_3d.h b/lib/mesh/regular_mesh_3d.h index ec42b47..ab65a04 100644 --- a/lib/mesh/regular_mesh_3d.h +++ b/lib/mesh/regular_mesh_3d.h @@ -40,14 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, int xbnum, int ybnum, int zbnum, - double xmin, double ymin, double zmin, double xsize, double ysize, double zsize); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed){} + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为regular_mesh_3d的专有函数 */ @@ -57,6 +57,9 @@ namespace gctl double xmin, double ymin, double zmin, double xsize, double ysize, double zsize); virtual ~regular_mesh_3d(); + void init(std::string in_name, std::string in_info, int xbnum, int ybnum, int zbnum, + double xmin, double ymin, double zmin, double xsize, double ysize, double zsize); + int get_xbnum() const; int get_ybnum() const; int get_zbnum() const; @@ -67,13 +70,6 @@ namespace gctl double get_ysize() const; double get_zsize() const; - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - - //void edit_data(std::string datname, physical_type_e p_type, value_operator_e v_type, std::string para_str, double in_val); - //void purge_data(std::string datname, double in_val); - protected: int rm_xbnum, rm_ybnum, rm_zbnum; double rm_xmin, rm_ymin, rm_zmin; diff --git a/lib/mesh/regular_mesh_sph_3d.cpp b/lib/mesh/regular_mesh_sph_3d.cpp index 0849249..5dcecae 100644 --- a/lib/mesh/regular_mesh_sph_3d.cpp +++ b/lib/mesh/regular_mesh_sph_3d.cpp @@ -283,16 +283,4 @@ void gctl::regular_mesh_sph_3d::save_gmsh(std::string filename, index_packed_e p outfile.close(); tmp_nodes.clear(); return; -} - -void gctl::regular_mesh_sph_3d::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::regular_mesh_sph_3d::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} +} \ No newline at end of file diff --git a/lib/mesh/regular_mesh_sph_3d.h b/lib/mesh/regular_mesh_sph_3d.h index fd4fcd5..fc7661e 100644 --- a/lib/mesh/regular_mesh_sph_3d.h +++ b/lib/mesh/regular_mesh_sph_3d.h @@ -39,13 +39,14 @@ namespace gctl /** * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, double lon_min, double lat_min, double rad_min, - double lon_size, double lat_size, double rad_size, int lon_bnum, int lat_bnum, int rad_bnum); - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed){} + void save_gmsh(std::string filename, index_packed_e packed = Packed); - void load_binary(std::string filename); - void save_binary(std::string filename); + void load_binary(std::string filename); + void save_binary(std::string filename); + + void show_mesh_dimension(std::ostream &os) const; /** * 以下为regular_mesh_sph_3d的专有函数 @@ -55,6 +56,9 @@ namespace gctl double rad_min, double lon_size, double lat_size, double rad_size, int lon_bnum, int lat_bnum, int rad_bnum); virtual ~regular_mesh_sph_3d(); + void init(std::string in_name, std::string in_info, double lon_min, double lat_min, double rad_min, + double lon_size, double lat_size, double rad_size, int lon_bnum, int lat_bnum, int rad_bnum); + int get_lon_bnum() const; int get_lat_bnum() const; int get_rad_bnum() const; @@ -65,10 +69,6 @@ namespace gctl double get_lat_size() const; double get_rad_size() const; - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); - protected: int rm_lon_bnum, rm_lat_bnum, rm_rad_bnum; double rm_lon_min, rm_lat_min, rm_rad_min; diff --git a/lib/mesh/tet_mesh.cpp b/lib/mesh/tet_mesh.cpp index 408d2e5..461e464 100644 --- a/lib/mesh/tet_mesh.cpp +++ b/lib/mesh/tet_mesh.cpp @@ -202,16 +202,4 @@ void gctl::tetrahedron_mesh::save_gmsh(std::string filename, index_packed_e pack gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; -} - -void gctl::tetrahedron_mesh::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} - -void gctl::tetrahedron_mesh::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); - return; -} +} \ No newline at end of file diff --git a/lib/mesh/tet_mesh.h b/lib/mesh/tet_mesh.h index ea87a83..16ab0ca 100644 --- a/lib/mesh/tet_mesh.h +++ b/lib/mesh/tet_mesh.h @@ -40,14 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, const array &in_nodes, - const array &in_tets); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed); + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为triangle_mesh的专有函数 */ @@ -57,14 +57,13 @@ namespace gctl const array &in_tets); virtual ~tetrahedron_mesh(); + void init(std::string in_name, std::string in_info, const array &in_nodes, + const array &in_tets); + const array &get_nodes() const; const array &get_elements() const; void load_tetgen(std::string filename, index_packed_e packed = Packed); - void load_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); protected: array nodes; diff --git a/lib/mesh/tri2d_mesh.cpp b/lib/mesh/tri2d_mesh.cpp index c6656c6..9793084 100644 --- a/lib/mesh/tri2d_mesh.cpp +++ b/lib/mesh/tri2d_mesh.cpp @@ -27,28 +27,28 @@ #include "tri2d_mesh.h" -void gctl::triangle2d_mesh::init(std::string in_name, std::string in_info, const array &in_nodes, +void gctl::triangle2d_mesh::init(std::string in_name, std::string in_info, const array &in_nodes_, const array &in_triangles) { check_initiated(true); // 检查是否已经初始化 base_mesh::init(TRI_TET_MESH, MESH_2D, in_name, in_info); - node_num_ = in_nodes.size(); + node_num_ = in_nodes_.size(); ele_num_ = in_triangles.size(); - nodes.resize(node_num_); + nodes_.resize(node_num_); for (int i = 0; i < node_num_; i++) { - nodes[i] = in_nodes[i]; + nodes_[i] = in_nodes_[i]; } - elements.resize(ele_num_); + elems_.resize(ele_num_); for (int i = 0; i < ele_num_; i++) { - elements[i].id = i; + elems_[i].id = i; for (int j = 0; j < 3; j++) { - elements[i].vert[j] = nodes.get(in_triangles[i].vert[j]->id); + elems_[i].vert[j] = nodes_.get(in_triangles[i].vert[j]->id); } } @@ -58,8 +58,8 @@ void gctl::triangle2d_mesh::init(std::string in_name, std::string in_info, const void gctl::triangle2d_mesh::show_mesh_dimension(std::ostream &os) const { - os << "node num: " << node_num_ << std::endl; - os << "elem num: " << ele_num_ << std::endl; + os << "Node num: " << node_num_ << std::endl; + os << "Elem num: " << ele_num_ << std::endl; return; } @@ -68,8 +68,7 @@ void gctl::triangle2d_mesh::load_binary(std::string filename) check_initiated(true); // 检查是否已经初始化 std::ifstream infile; - gctl::open_infile(infile, filename, ".2m", - std::ios::in|std::ios::binary); + gctl::open_infile(infile, filename, ".2m", std::ios::in|std::ios::binary); // 读入网格头信息 load_headinfo(infile, TRI_TET_MESH, MESH_2D); @@ -78,22 +77,22 @@ void gctl::triangle2d_mesh::load_binary(std::string filename) infile.read((char*)&node_num_, sizeof(int)); infile.read((char*)&ele_num_, sizeof(int)); - nodes.resize(node_num_); - elements.resize(ele_num_); + nodes_.resize(node_num_); + elems_.resize(ele_num_); for (int i = 0; i < node_num_; i++) { - infile.read((char*)nodes.get(i), sizeof(gctl::vertex2dc)); + infile.read((char*)nodes_.get(i), sizeof(gctl::vertex2dc)); } int in_index; for (int i = 0; i < ele_num_; i++) { - elements[i].id = i; + elems_[i].id = i; for (int j = 0; j < 3; j++) { infile.read((char*)&in_index, sizeof(int)); - elements[i].vert[j] = nodes.get(in_index); + elems_[i].vert[j] = nodes_.get(in_index); } } @@ -110,8 +109,7 @@ void gctl::triangle2d_mesh::save_binary(std::string filename) check_initiated(); // 检查是否已经初始化 std::ofstream outfile; - gctl::open_outfile(outfile, filename, ".2m", - std::ios::out|std::ios::binary); + gctl::open_outfile(outfile, filename, ".2m", std::ios::out|std::ios::binary); // 首先输出网格的头信息 save_headinfo(outfile); @@ -122,7 +120,7 @@ void gctl::triangle2d_mesh::save_binary(std::string filename) for (int i = 0; i < node_num_; i++) { - outfile.write((char*)nodes.get(i), sizeof(gctl::vertex2dc)); + outfile.write((char*)nodes_.get(i), sizeof(gctl::vertex2dc)); } int in_index; @@ -130,7 +128,7 @@ void gctl::triangle2d_mesh::save_binary(std::string filename) { for (int j = 0; j < 3; j++) { - in_index = elements[i].vert[j]->id; + in_index = elems_[i].vert[j]->id; outfile.write((char*)&in_index, sizeof(int)); } } @@ -144,10 +142,10 @@ void gctl::triangle2d_mesh::save_binary(std::string filename) gctl::triangle2d_mesh::triangle2d_mesh() : base_mesh::base_mesh(){} -gctl::triangle2d_mesh::triangle2d_mesh(std::string in_name, std::string in_info, const array &in_nodes, +gctl::triangle2d_mesh::triangle2d_mesh(std::string in_name, std::string in_info, const array &in_nodes_, const array &in_triangles) { - init(in_name, in_info, in_nodes, in_triangles); + init(in_name, in_info, in_nodes_, in_triangles); } gctl::triangle2d_mesh::~triangle2d_mesh(){} @@ -155,23 +153,39 @@ gctl::triangle2d_mesh::~triangle2d_mesh(){} const gctl::array &gctl::triangle2d_mesh::get_nodes() const { check_initiated(); // 检查是否已经初始化 - return nodes; + return nodes_; } const gctl::array &gctl::triangle2d_mesh::get_elements() const { check_initiated(); // 检查是否已经初始化 - return elements; + return elems_; +} + +void gctl::triangle2d_mesh::reorder2anticlockwise() +{ + check_initiated(); // 检查是否已经初始化 + vertex2dc *v_ptr; + for (size_t i = 0; i < get_elenum(); i++) + { + if (cross(*elems_[i].vert[1] - *elems_[i].vert[0], *elems_[i].vert[2] - *elems_[i].vert[0]) < 0) + { + v_ptr = elems_[i].vert[1]; + elems_[i].vert[1] = elems_[i].vert[2]; + elems_[i].vert[2] = v_ptr; + } + } + return; } void gctl::triangle2d_mesh::load_triangle(std::string filename, index_packed_e packed) { - gctl::read_Triangle_node(filename, nodes, packed); - gctl::read_Triangle_element(filename, elements, nodes, packed); + gctl::read_Triangle_node(filename, nodes_, packed); + gctl::read_Triangle_element(filename, elems_, nodes_, packed); // 设置名称与信息等 - node_num_ = nodes.size(); - ele_num_ = elements.size(); + node_num_ = nodes_.size(); + ele_num_ = elems_.size(); base_mesh::init(TRI_TET_MESH, MESH_2D, filename, "Imported from a .node and .ele file."); initialized_ = true; return; @@ -179,37 +193,96 @@ void gctl::triangle2d_mesh::load_triangle(std::string filename, index_packed_e p void gctl::triangle2d_mesh::load_gmsh(std::string filename, index_packed_e packed) { - std::ifstream infile; - gctl::open_infile(infile, filename, ".msh"); - gctl::read_gmsh_node(infile, nodes, packed); - gctl::read_gmsh_element(infile, elements, nodes, packed); - infile.close(); + meshio_.init_file(filename, Input); + meshio_.set_packed(packed, Input); + meshio_.read_mesh(elems_, nodes_, &elems_tag_); // 设置名称与信息等 - node_num_ = nodes.size(); - ele_num_ = elements.size(); + node_num_ = nodes_.size(); + ele_num_ = elems_.size(); base_mesh::init(TRI_TET_MESH, MESH_2D, filename, "Imported from a .msh file."); initialized_ = true; return; } +void gctl::triangle2d_mesh::load_gmsh_groups() +{ + check_initiated(); // 检查是否已经初始化 + meshio_.read_physical_groups(groups_); + return; +} + void gctl::triangle2d_mesh::save_gmsh(std::string filename, index_packed_e packed) { std::ofstream outfile; gctl::open_outfile(outfile, filename, ".msh"); - gctl::save2gmsh(outfile, elements, nodes, packed); + gctl::save2gmsh(outfile, elems_, nodes_, packed); outfile.close(); + + meshio_.init_file(filename, Output); + meshio_.set_packed(packed, Output); + meshio_.save_mesh(elems_, nodes_); return; } -void gctl::triangle2d_mesh::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed) +void gctl::triangle2d_mesh::groups2data(std::string datname, _1s_vector phynames, _1d_vector phyvals) { - base_mesh::save_gmsh_withdata(filename, out_mode, packed); - return; -} + check_initiated(); // 检查是否已经初始化 -void gctl::triangle2d_mesh::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed) -{ - base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed); + if (phynames.size() != phyvals.size() || phynames.empty()) + { + throw std::invalid_argument("The size of phynames and phyvals must be the same and non-empty."); + } + + if (groups_.empty() || elems_tag_.empty()) + { + throw std::invalid_argument("No physical groups found."); + } + + if (elems_tag_.empty()) throw std::invalid_argument("No physical tags found."); + for (size_t i = 0; i < ele_num_; i++) + { + if (elems_tag_[i].empty()) throw std::invalid_argument("No physical tags found."); + } + + array tag_idx(phynames.size()); + for (size_t i = 0; i < phynames.size(); i++) + { + tag_idx[i] = meshio_.physical_name2tag(groups_, phynames[i]); + } + + if (saved(datname)) + { + meshdata &dat = get_data(datname); + if (dat.loctype_ != ElemData) throw std::invalid_argument("The data type must be ElemData."); + + for (size_t i = 0; i < ele_num_; i++) + { + for (size_t e = 0; e < phynames.size(); e++) + { + if (elems_tag_[i][0] == tag_idx[e]) + { + dat.datval_[i] = phyvals[e]; + break; + } + } + } + } + else + { + meshdata &newdat = add_data(ElemData, Scalar, datname, 0.0); + + for (size_t i = 0; i < ele_num_; i++) + { + for (size_t e = 0; e < phynames.size(); e++) + { + if (elems_tag_[i][0] == tag_idx[e]) + { + newdat.datval_[i] = phyvals[e]; + break; + } + } + } + } return; -} +} \ No newline at end of file diff --git a/lib/mesh/tri2d_mesh.h b/lib/mesh/tri2d_mesh.h index 145a0a4..a12f3e8 100644 --- a/lib/mesh/tri2d_mesh.h +++ b/lib/mesh/tri2d_mesh.h @@ -40,14 +40,14 @@ namespace gctl * 以下为mesh类型的虚函数实现 */ - void init(std::string in_name, std::string in_info, const array &in_nodes, - const array &in_triangles); - - void show_mesh_dimension(std::ostream &os) const; + void load_gmsh(std::string filename, index_packed_e packed = Packed); + void save_gmsh(std::string filename, index_packed_e packed = Packed); void load_binary(std::string filename); void save_binary(std::string filename); + void show_mesh_dimension(std::ostream &os) const; + /** * 以下为triangle2d_mesh的专有函数 */ @@ -57,18 +57,31 @@ namespace gctl const array &in_triangles); virtual ~triangle2d_mesh(); + void init(std::string in_name, std::string in_info, const array &in_nodes, + const array &in_triangles); + const array &get_nodes() const; const array &get_elements() const; + void reorder2anticlockwise(); + void load_triangle(std::string filename, index_packed_e packed = Packed); - void load_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, index_packed_e packed = Packed); - void save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed = Packed); - void save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed = Packed); + void load_gmsh_groups(); + + /** + * @brief 将模型物理分组转换为模型数据组 + * + * @param datname 赋值给模型数据组的名称(如果数组不存在则新建) + * @param phynames 物理组名称 + * @param phyvals 物理组值 + */ + void groups2data(std::string datname, _1s_vector phynames, _1d_vector phyvals); protected: - array nodes; - array elements; + array nodes_; + array elems_; + array groups_; + _2i_vector elems_tag_; }; } diff --git a/tool/gridmanager/gridmanager.cpp b/tool/gridmanager/gridmanager.cpp index 8d1f3ca..e49a073 100644 --- a/tool/gridmanager/gridmanager.cpp +++ b/tool/gridmanager/gridmanager.cpp @@ -377,7 +377,7 @@ void save_gmsh(const std::vector &cmd_units) // save gmsh if (cmd_units.size() < 3) throw std::runtime_error("save: insufficient parameters."); - rg.save_gmsh(cmd_units[2], OverWrite, NotPacked); + rg.save_gmsh_withdata(cmd_units[2], OverWrite, NotPacked); return; }