/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "regular_mesh_sph_3d.h"

void gctl::regular_mesh_sph_3d::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)
{
	check_initiated(true); // 检查网格是否已经初始化
	base_mesh::init(REGULAR_MESH_SPH, MESH_3D, in_name, in_info);

	rm_lon_bnum = lon_bnum; rm_lat_bnum = lat_bnum; rm_rad_bnum = rad_bnum;
	rm_lon_min = lon_min; rm_lat_min = lat_min; rm_rad_min = rad_min;
	rm_lon_size = lon_size; rm_lat_size = lat_size; rm_rad_size = rad_size;

	node_num_ = (rm_lon_bnum+1) * (rm_lat_bnum+1) *(rm_rad_bnum+1);
	ele_num_  = rm_lon_bnum * rm_lat_bnum * rm_rad_bnum;

	nodes.resize(node_num_);
	elements.resize(ele_num_);

	int tmp_id;
	for (int k = 0; k < rm_rad_bnum+1; k++)
	{
		for (int j = 0; j < rm_lat_bnum+1; j++)
		{
			for (int i = 0; i < rm_lon_bnum+1; i++)
			{
				tmp_id = i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1);
				nodes[tmp_id].id = tmp_id;
				nodes[tmp_id].lon  = rm_lon_min+(i-0.5)*rm_lon_size;
				nodes[tmp_id].lat  = rm_lat_min+(j-0.5)*rm_lat_size;
				nodes[tmp_id].rad  = rm_rad_min+(k-0.5)*rm_rad_size;
			}
		}
	}

	for (int k = 0; k < rm_rad_bnum; k++)
	{
		for (int j = 0; j < rm_lat_bnum; j++)
		{
			for (int i = 0; i < rm_lon_bnum; i++)
			{
				tmp_id = i+j*rm_lon_bnum+k*rm_lon_bnum*rm_lat_bnum;
				elements[tmp_id].id = tmp_id;
				elements[tmp_id].vert[0] = nodes.get(i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[1] = nodes.get(i+1+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[2] = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[3] = nodes.get(i+(j+1)*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[4] = nodes.get(i+j*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[5] = nodes.get(i+1+j*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[6] = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[7] = nodes.get(i+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].dl = nodes.get(i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].ur = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
			}
		}
	}

	initialized_ = true;
	return;
}

void gctl::regular_mesh_sph_3d::show_mesh_dimension(std::ostream &os) const
{
	os << "node num: " << node_num_ << std::endl;
	os << "elem num: " << ele_num_ << std::endl;
	os << "x-range: " << rm_lon_min << " -> " << rm_lon_min + (rm_lon_bnum - 1)*rm_lon_size << "\n";
	os << "y-range: " << rm_lat_min << " -> " << rm_lat_min + (rm_lat_bnum - 1)*rm_lat_size << "\n";
	os << "z-range: " << rm_rad_min << " -> " << rm_rad_min + (rm_rad_bnum - 1)*rm_rad_size << "\n";
	os << "block-size: " << rm_lon_size << ", " << rm_lat_size << ", " << rm_rad_size << "\n";
	os << "dimension: " << rm_lon_bnum << ", " << rm_lat_bnum << ", " << rm_rad_bnum << "\n";
	return;
}

void gctl::regular_mesh_sph_3d::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, REGULAR_MESH_SPH, MESH_3D);

	// 读入网格信息
	infile.read((char*)&rm_lon_bnum, sizeof(int));
	infile.read((char*)&rm_lat_bnum, sizeof(int));
	infile.read((char*)&rm_rad_bnum, sizeof(int));
	infile.read((char*)&rm_lon_min, sizeof(double));
	infile.read((char*)&rm_lat_min, sizeof(double));
	infile.read((char*)&rm_rad_min, sizeof(double));
	infile.read((char*)&rm_lon_size, sizeof(double));
	infile.read((char*)&rm_lat_size, sizeof(double));
	infile.read((char*)&rm_rad_size, sizeof(double));

	node_num_ = (rm_lon_bnum+1) * (rm_lat_bnum+1) *(rm_rad_bnum+1);
	ele_num_  = rm_lon_bnum * rm_lat_bnum * rm_rad_bnum;

	nodes.resize(node_num_);
	elements.resize(ele_num_);

	int tmp_id;
	for (int k = 0; k < rm_rad_bnum+1; k++)
	{
		for (int j = 0; j < rm_lat_bnum+1; j++)
		{
			for (int i = 0; i < rm_lon_bnum+1; i++)
			{
				tmp_id = i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1);
				nodes[tmp_id].id = tmp_id;
				nodes[tmp_id].lon  = rm_lon_min+(i-0.5)*rm_lon_size;
				nodes[tmp_id].lat  = rm_lat_min+(j-0.5)*rm_lat_size;
				nodes[tmp_id].rad  = rm_rad_min+(k-0.5)*rm_rad_size;
			}
		}
	}

	for (int k = 0; k < rm_rad_bnum; k++)
	{
		for (int j = 0; j < rm_lat_bnum; j++)
		{
			for (int i = 0; i < rm_lon_bnum; i++)
			{
				tmp_id = i+j*rm_lon_bnum+k*rm_lon_bnum*rm_lat_bnum;
				elements[tmp_id].id = tmp_id;
				elements[tmp_id].vert[0] = nodes.get(i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[1] = nodes.get(i+1+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[2] = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[3] = nodes.get(i+(j+1)*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[4] = nodes.get(i+j*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[5] = nodes.get(i+1+j*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[6] = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].vert[7] = nodes.get(i+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].dl = nodes.get(i+j*(rm_lon_bnum+1)+k*(rm_lon_bnum+1)*(rm_lat_bnum+1));
				elements[tmp_id].ur = nodes.get(i+1+(j+1)*(rm_lon_bnum+1)+(k+1)*(rm_lon_bnum+1)*(rm_lat_bnum+1));
			}
		}
	}

	initialized_ = true;

	// 读入模型数据单元
	load_datablock(infile);

	infile.close();
	return;
}

void gctl::regular_mesh_sph_3d::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*)&rm_lon_bnum, sizeof(int));
	outfile.write((char*)&rm_lat_bnum, sizeof(int));
	outfile.write((char*)&rm_rad_bnum, sizeof(int));
	outfile.write((char*)&rm_lon_min, sizeof(double));
	outfile.write((char*)&rm_lat_min, sizeof(double));
	outfile.write((char*)&rm_rad_min, sizeof(double));
	outfile.write((char*)&rm_lon_size, sizeof(double));
	outfile.write((char*)&rm_lat_size, sizeof(double));
	outfile.write((char*)&rm_rad_size, sizeof(double));

	// 输出的模型数据单元
	save_datablock(outfile);

	outfile.close();
	return;
}

gctl::regular_mesh_sph_3d::regular_mesh_sph_3d() : base_mesh::base_mesh(){}

gctl::regular_mesh_sph_3d::regular_mesh_sph_3d(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)
{
	init(in_name, in_info, lon_size, lat_size, rad_size, lon_bnum, lat_bnum, rad_bnum, lon_min, lat_min, rad_min);
}

gctl::regular_mesh_sph_3d::~regular_mesh_sph_3d(){}

int gctl::regular_mesh_sph_3d::get_lon_bnum() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lon_bnum;
}

int gctl::regular_mesh_sph_3d::get_lat_bnum() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lat_bnum;
}

int gctl::regular_mesh_sph_3d::get_rad_bnum() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_rad_bnum;
}

double gctl::regular_mesh_sph_3d::get_lon_min() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lon_min;
}

double gctl::regular_mesh_sph_3d::get_lat_min() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lat_min;
}

double gctl::regular_mesh_sph_3d::get_rad_min() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_rad_min;
}

double gctl::regular_mesh_sph_3d::get_lon_size() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lon_size;
}

double gctl::regular_mesh_sph_3d::get_lat_size() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_lat_size;
}

double gctl::regular_mesh_sph_3d::get_rad_size() const
{
	check_initiated(); // 检查网格是否已经初始化
	return rm_rad_size;
}

void gctl::regular_mesh_sph_3d::save_gmsh(std::string filename, index_packed_e packed)
{
	point3dc tmp_c;
	gctl::array<vertex3dc> tmp_nodes(nodes.size());
	for (int i = 0; i < nodes.size(); i++)
	{
		tmp_c = nodes[i].s2c();
		tmp_nodes[i].id = i;
		tmp_nodes[i].x = tmp_c.x;
		tmp_nodes[i].y = tmp_c.y;
		tmp_nodes[i].z = tmp_c.z;
	}
	std::ofstream outfile;
	gctl::open_outfile(outfile, filename, ".msh");
	gctl::save2gmsh(outfile, elements, tmp_nodes, packed);
	outfile.close();
	tmp_nodes.clear();
	return;
}