gctl_mesh/lib/mesh/linear_mesh_2d.cpp
2025-04-04 22:36:13 +08:00

283 lines
8.2 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 <http://www.gnu.org/licenses/>.
*
* 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 "linear_mesh_2d.h"
void gctl::linear_mesh_2d::init(std::string in_name, std::string in_info, double xmin, double ymin,
const gctl::array<double> &xsizes, const gctl::array<double> &ysizes)
{
check_initiated(true); // 检查网格是否已经初始化
base_mesh::init(LINEAR_MESH, MESH_2D, in_name, in_info);
lm_xmin = xmin;
lm_ymin = ymin;
lm_xbnum = xsizes.size();
lm_ybnum = ysizes.size();
lm_xsizes.resize(lm_xbnum);
lm_ysizes.resize(lm_ybnum);
node_num_ = (lm_xbnum+1) * (lm_ybnum+1);
ele_num_ = lm_xbnum * lm_ybnum;
for (int i = 0; i < lm_xbnum; i++)
{
lm_xsizes[i] = xsizes[i];
}
for (int i = 0; i < lm_ybnum; i++)
{
lm_ysizes[i] = ysizes[i];
}
nodes.resize(node_num_);
elements.resize(ele_num_);
double x_p, y_p = lm_ymin - 0.5*lm_ysizes[0];
for (int j = 0; j < lm_ybnum+1; j++)
{
x_p = lm_xmin - 0.5*lm_xsizes[0];
for (int i = 0; i < lm_xbnum+1; i++)
{
nodes[i+j*(lm_xbnum+1)].id = i+j*(lm_xbnum+1);
nodes[i+j*(lm_xbnum+1)].x = x_p;
nodes[i+j*(lm_xbnum+1)].y = y_p;
if (i < lm_xbnum) x_p += lm_xsizes[i];
}
if (j < lm_ybnum) y_p += lm_ysizes[j];
}
for (int j = 0; j < lm_ybnum; j++)
{
for (int i = 0; i < lm_xbnum; i++)
{
elements[i+j*lm_xbnum].id = i+j*lm_xbnum;
elements[i+j*lm_xbnum].vert[0] = nodes.get(i+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[1] = nodes.get(i+1+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[2] = nodes.get(i+1+(j+1)*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[3] = nodes.get(i+(j+1)*(lm_xbnum+1));
elements[i+j*lm_xbnum].dl = nodes.get(i+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].ur = nodes.get(i+1+(j+1)*(lm_xbnum+1));
}
}
initialized_ = true;
return;
}
void gctl::linear_mesh_2d::show_mesh_dimension(std::ostream &os) const
{
os << "node num: " << node_num_ << std::endl;
os << "elem num: " << ele_num_ << std::endl;
os << "x-range: " << get_xmin() << " -> " << get_xmax() << "\n";
os << "y-range: " << get_ymin() << " -> " << get_ymax() << "\n";
os << "dimension: " << lm_xbnum << ", " << lm_ybnum << "\n";
return;
}
void gctl::linear_mesh_2d::load_binary(std::string filename)
{
check_initiated(true); // 检查网格是否已经初始化
std::ifstream infile;
gctl::open_infile(infile, filename, ".2m", std::ios::in|std::ios::binary);
// 读入网格头信息
load_headinfo(infile, LINEAR_MESH, MESH_2D);
// 读入网格信息
infile.read((char*)&lm_xbnum, sizeof(int));
infile.read((char*)&lm_ybnum, sizeof(int));
infile.read((char*)&lm_xmin, sizeof(double));
infile.read((char*)&lm_ymin, sizeof(double));
lm_xsizes.resize(lm_xbnum);
lm_ysizes.resize(lm_ybnum);
node_num_ = (lm_xbnum+1) * (lm_ybnum+1);
ele_num_ = lm_xbnum * lm_ybnum;
for (int i = 0; i < lm_xbnum; i++)
{
infile.read((char*)lm_xsizes.get(i), sizeof(double));
}
for (int i = 0; i < lm_ybnum; i++)
{
infile.read((char*)lm_ysizes.get(i), sizeof(double));
}
nodes.resize(node_num_);
elements.resize(ele_num_);
double x_p, y_p = lm_ymin - 0.5*lm_ysizes[0];
for (int j = 0; j < lm_ybnum+1; j++)
{
x_p = lm_xmin - 0.5*lm_xsizes[0];
for (int i = 0; i < lm_xbnum+1; i++)
{
nodes[i+j*(lm_xbnum+1)].id = i+j*(lm_xbnum+1);
nodes[i+j*(lm_xbnum+1)].x = x_p;
nodes[i+j*(lm_xbnum+1)].y = y_p;
if (i < lm_xbnum) x_p += lm_xsizes[i];
}
if (j < lm_ybnum) y_p += lm_ysizes[j];
}
for (int j = 0; j < lm_ybnum; j++)
{
for (int i = 0; i < lm_xbnum; i++)
{
elements[i+j*lm_xbnum].id = i+j*lm_xbnum;
elements[i+j*lm_xbnum].vert[0] = nodes.get(i+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[1] = nodes.get(i+1+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[2] = nodes.get(i+1+(j+1)*(lm_xbnum+1));
elements[i+j*lm_xbnum].vert[3] = nodes.get(i+(j+1)*(lm_xbnum+1));
elements[i+j*lm_xbnum].dl = nodes.get(i+j*(lm_xbnum+1));
elements[i+j*lm_xbnum].ur = nodes.get(i+1+(j+1)*(lm_xbnum+1));
}
}
initialized_ = true;
// 读入模型数据单元
load_datablock(infile);
infile.close();
return;
}
void gctl::linear_mesh_2d::save_binary(std::string filename)
{
check_initiated(); // 检查网格是否已经初始化
std::ofstream outfile;
gctl::open_outfile(outfile, filename, ".2m", std::ios::out|std::ios::binary);
// 首先输出网格的头信息
save_headinfo(outfile);
// 输出网格信息
outfile.write((char*)&lm_xbnum, sizeof(int));
outfile.write((char*)&lm_ybnum, sizeof(int));
outfile.write((char*)&lm_xmin, sizeof(double));
outfile.write((char*)&lm_ymin, sizeof(double));
for (int i = 0; i < lm_xbnum; i++)
{
outfile.write((char*)lm_xsizes.get(i), sizeof(double));
}
for (int i = 0; i < lm_ybnum; i++)
{
outfile.write((char*)lm_ysizes.get(i), sizeof(double));
}
// 输出的模型数据单元
save_datablock(outfile);
outfile.close();
return;
}
gctl::linear_mesh_2d::linear_mesh_2d() : base_mesh::base_mesh(){}
gctl::linear_mesh_2d::linear_mesh_2d(std::string in_name, std::string in_info, double xmin, double ymin,
const gctl::array<double> &xsizes, const gctl::array<double> &ysizes)
{
init(in_name, in_info, xmin, ymin, xsizes, ysizes);
}
gctl::linear_mesh_2d::~linear_mesh_2d(){}
int gctl::linear_mesh_2d::get_xbnum() const
{
check_initiated(); // 检查网格是否已经初始化
return lm_xbnum;
}
int gctl::linear_mesh_2d::get_ybnum() const
{
check_initiated(); // 检查网格是否已经初始化
return lm_ybnum;
}
double gctl::linear_mesh_2d::get_xmin() const
{
check_initiated(); // 检查网格是否已经初始化
return lm_xmin;
}
double gctl::linear_mesh_2d::get_ymin() const
{
check_initiated(); // 检查网格是否已经初始化
return lm_ymin;
}
double gctl::linear_mesh_2d::get_xmax() const
{
check_initiated(); // 检查网格是否已经初始化
double xmax = lm_xmin - 0.5*lm_xsizes[0];
for (size_t i = 0; i < lm_xsizes.size(); i++)
{
xmax += lm_xsizes[i];
}
xmax -= 0.5*lm_xsizes.back();
return xmax;
}
double gctl::linear_mesh_2d::get_ymax() const
{
check_initiated(); // 检查网格是否已经初始化
double ymax = lm_ymin - 0.5*lm_ysizes[0];
for (size_t i = 0; i < lm_ysizes.size(); i++)
{
ymax += lm_ysizes[i];
}
ymax -= 0.5*lm_ysizes.back();
return ymax;
}
const gctl::array<double> *gctl::linear_mesh_2d::get_xsizes() const
{
check_initiated(); // 检查网格是否已经初始化
return &lm_xsizes;
}
const gctl::array<double> *gctl::linear_mesh_2d::get_ysizes() const
{
check_initiated(); // 检查网格是否已经初始化
return &lm_ysizes;
}
void gctl::linear_mesh_2d::save_gmsh(std::string filename, index_packed_e packed)
{
meshio_.init_file(filename, Output);
meshio_.set_packed(packed, Output);
meshio_.save_mesh(elements, nodes);
return;
}