/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * Geophysical Computational Tools & Library (GCTL) * * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) * * GCTL is distributed under a dual licensing scheme. You can redistribute * it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, either version 2 * of the License, or (at your option) any later version. You should have * received a copy of the GNU Lesser General Public License along with this * program. If not, see . * * If the terms and conditions of the LGPL v.2. would prevent you from using * the GCTL, please consider the option to obtain a commercial license for a * fee. These licenses are offered by the GCTL's original author. As a rule, * licenses are provided "as-is", unlimited in time for a one time fee. Please * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget * to include some description of your company and the realm of its activities. * Also add information on how to contact you by electronic and paper mail. ******************************************************/ #include "regular_mesh_3d.h" void gctl::regular_mesh_3d::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) { if (initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is already initialized."); } base_mesh::init(REGULAR_MESH, MESH_3D, in_name, in_info); rm_xbnum = xbnum; rm_ybnum = ybnum; rm_zbnum = zbnum; rm_xmin = xmin; rm_ymin = ymin; rm_zmin = zmin; rm_xsize = xsize; rm_ysize = ysize; rm_zsize = zsize; node_num = (rm_xbnum+1) * (rm_ybnum+1) *(rm_zbnum+1); ele_num = rm_xbnum * rm_ybnum * rm_zbnum; nodes.resize(node_num); elements.resize(ele_num); for (int k = 0; k < rm_zbnum+1; k++) { for (int j = 0; j < rm_ybnum+1; j++) { for (int i = 0; i < rm_xbnum+1; i++) { nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].id = i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1); nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].x = rm_xmin+(i-0.5)*rm_xsize; nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].y = rm_ymin+(j-0.5)*rm_ysize; nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].z = rm_zmin+(k-0.5)*rm_zsize; } } } for (int k = 0; k < rm_zbnum; k++) { for (int j = 0; j < rm_ybnum; j++) { for (int i = 0; i < rm_xbnum; i++) { elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].id = i+j*rm_xbnum+k*rm_xbnum*rm_ybnum; elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[0] = nodes.get(i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[1] = nodes.get(i+1+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[2] = nodes.get(i+1+(j+1)*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[3] = nodes.get(i+(j+1)*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[4] = nodes.get(i+j*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[5] = nodes.get(i+1+j*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[6] = nodes.get(i+1+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[7] = nodes.get(i+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].dl = nodes.get(i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].ur = nodes.get(i+1+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); } } } initialized = true; return; } void gctl::regular_mesh_3d::load_binary(std::string filename) { if (initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is already initialized."); } std::ifstream infile; gctl::open_infile(infile, filename, ".2m", std::ios::in|std::ios::binary); // 读入网格头信息 load_headinfo(infile, REGULAR_MESH, MESH_3D); // 读入网格信息 infile.read((char*)&rm_xbnum, sizeof(int)); infile.read((char*)&rm_ybnum, sizeof(int)); infile.read((char*)&rm_zbnum, sizeof(int)); infile.read((char*)&rm_xmin, sizeof(double)); infile.read((char*)&rm_ymin, sizeof(double)); infile.read((char*)&rm_zmin, sizeof(double)); infile.read((char*)&rm_xsize, sizeof(double)); infile.read((char*)&rm_ysize, sizeof(double)); infile.read((char*)&rm_zsize, sizeof(double)); node_num = (rm_xbnum+1) * (rm_ybnum+1) *(rm_zbnum+1); ele_num = rm_xbnum * rm_ybnum * rm_zbnum; nodes.resize(node_num); elements.resize(ele_num); for (int k = 0; k < rm_zbnum+1; k++) { for (int j = 0; j < rm_ybnum+1; j++) { for (int i = 0; i < rm_xbnum+1; i++) { nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].id = i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1); nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].x = rm_xmin+(i-0.5)*rm_xsize; nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].y = rm_ymin+(j-0.5)*rm_ysize; nodes[i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)].z = rm_zmin+(k-0.5)*rm_zsize; } } } for (int k = 0; k < rm_zbnum; k++) { for (int j = 0; j < rm_ybnum; j++) { for (int i = 0; i < rm_xbnum; i++) { elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].id = i+j*rm_xbnum+k*rm_xbnum*rm_ybnum; elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[0] = nodes.get(i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[1] = nodes.get(i+1+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[2] = nodes.get(i+1+(j+1)*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[3] = nodes.get(i+(j+1)*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[4] = nodes.get(i+j*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[5] = nodes.get(i+1+j*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[6] = nodes.get(i+1+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].vert[7] = nodes.get(i+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].dl = nodes.get(i+j*(rm_xbnum+1)+k*(rm_xbnum+1)*(rm_ybnum+1)); elements[i+j*rm_xbnum+k*rm_xbnum*rm_ybnum].ur = nodes.get(i+1+(j+1)*(rm_xbnum+1)+(k+1)*(rm_xbnum+1)*(rm_ybnum+1)); } } } initialized = true; // 读入模型数据单元 load_datablock(infile); infile.close(); return; } void gctl::regular_mesh_3d::save_binary(std::string filename) { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } std::ofstream outfile; gctl::open_outfile(outfile, filename, ".2m", std::ios::out|std::ios::binary); // 首先输出网格的头信息 save_headinfo(outfile); // 输出网格信息 outfile.write((char*)&rm_xbnum, sizeof(int)); outfile.write((char*)&rm_ybnum, sizeof(int)); outfile.write((char*)&rm_zbnum, sizeof(int)); outfile.write((char*)&rm_xmin, sizeof(double)); outfile.write((char*)&rm_ymin, sizeof(double)); outfile.write((char*)&rm_zmin, sizeof(double)); outfile.write((char*)&rm_xsize, sizeof(double)); outfile.write((char*)&rm_ysize, sizeof(double)); outfile.write((char*)&rm_zsize, sizeof(double)); // 输出的模型数据单元 save_datablock(outfile); outfile.close(); return; } gctl::regular_mesh_3d::regular_mesh_3d() : base_mesh::base_mesh(){} gctl::regular_mesh_3d::regular_mesh_3d(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) { init(in_name, in_info, xbnum, ybnum, zbnum, xmin, ymin, zmin, xsize, ysize, zsize); } gctl::regular_mesh_3d::~regular_mesh_3d(){} int gctl::regular_mesh_3d::get_xbnum() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_xbnum; } int gctl::regular_mesh_3d::get_ybnum() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_ybnum; } int gctl::regular_mesh_3d::get_zbnum() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_zbnum; } double gctl::regular_mesh_3d::get_xmin() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_xmin; } double gctl::regular_mesh_3d::get_ymin() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_ymin; } double gctl::regular_mesh_3d::get_zmin() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_zmin; } double gctl::regular_mesh_3d::get_xsize() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_xsize; } double gctl::regular_mesh_3d::get_ysize() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_ysize; } double gctl::regular_mesh_3d::get_zsize() const { if (!initialized) { throw std::runtime_error("[gctl::regular_mesh_3d] The mesh is not initialized."); } return rm_zsize; } void gctl::regular_mesh_3d::save_gmsh(std::string filename, index_packed_e packed) { std::ofstream outfile; gctl::open_outfile(outfile, filename, ".msh"); gctl::save2gmsh(outfile, elements, nodes, packed); outfile.close(); return; } void gctl::regular_mesh_3d::save_gmsh(std::string filename, mesh_data_type_e d_type, output_type_e out_mode, index_packed_e packed) { base_mesh::save_gmsh(filename, d_type, 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(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); } } */