/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  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 "gm_tet_mesh.h"

gctl::gm_tetrahedron_mesh::gm_tetrahedron_mesh(){}

gctl::gm_tetrahedron_mesh::gm_tetrahedron_mesh(std::string in_name, std::string in_info, const array<vertex3dc> &in_nodes, 
	const array<tetrahedron> &in_tets) : tetrahedron_mesh::tetrahedron_mesh(in_name, in_info, in_nodes, in_tets){}

gctl::gm_tetrahedron_mesh::~gm_tetrahedron_mesh(){}

void gctl::gm_tetrahedron_mesh::gkernel(matrix<double> &out_kernel, const array<point3dc> &obsp, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gkernel(out_kernel, grav_ele_, obsp, comp_id, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gkernel(matrix<double> &out_kernel, const array<point3ds> &obsp, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

    gctl::gkernel(out_kernel, grav_ele_, obsp, comp_id, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<double> &out_obs, std::string data_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<point3dc> &out_obs, std::string data_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<tensor> &out_obs, std::string data_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<double> &out_obs, std::string data_name, const array<point3ds> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<point3dc> &out_obs, std::string data_name, const array<point3ds> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::gobser(array<tensor> &out_obs, std::string data_name, const array<point3ds> &obsp, verbose_type_e verbose)
{
	array<double> rho(get_elenum(), 0.0);

	meshdata &data = get_data(data_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				rho[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::gobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	grav_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(grav_ele_.get(i), elements.get(i));
	}
	callink_gravity_para(grav_ele_, grav_para_);

	gctl::gobser(out_obs, grav_ele_, obsp, rho, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magkernel(matrix<double> &out_kernel, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magkernel(out_kernel, mag_ele_, obsp, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magkernel(matrix<point3dc> &out_kernel, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magkernel(out_kernel, mag_ele_, obsp, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magkernel(matrix<tensor> &out_kernel, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magkernel(out_kernel, mag_ele_, obsp, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<double> &out_obs, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magobser(out_obs, mag_ele_, obsp, sus, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<point3dc> &out_obs, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magobser(out_obs, mag_ele_, obsp, sus, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<tensor> &out_obs, std::string sus_name, const array<point3dc> &obsp, 
	double inclina_deg, double declina_deg, verbose_type_e verbose)
{
	meshdata &data = get_data(sus_name);
	mesh_data_value_e vtype = data.valtype_;

	array<double> sus(get_elenum(), 0.0);

	if (vtype == Scalar)
	{
		for (int i = 0; i < data.datval_.size(); i++)
		{
			if (!std::isnan(data.datval_[i]))
			{
				sus[i] = data.datval_[i];
			}
		}
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magobser(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_earth(mag_ele_, mag_para_, inclina_deg, declina_deg);

	gctl::magobser(out_obs, mag_ele_, obsp, sus, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<double> &out_obs, std::string magz_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<point3dc> magz(get_elenum(), point3dc(0.0, 0.0, 0.0));
	meshdata &data = get_data(magz_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Vector)
	{
		magz = data.export_vector();
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magkernel(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_direct(mag_ele_, mag_para_, magz);

	gctl::magobser(out_obs, mag_ele_, obsp, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<point3dc> &out_obs, std::string magz_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<point3dc> magz(get_elenum(), point3dc(0.0, 0.0, 0.0));
	meshdata &data = get_data(magz_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Vector)
	{
		magz = data.export_vector();
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magkernel(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_direct(mag_ele_, mag_para_, magz);

	gctl::magobser(out_obs, mag_ele_, obsp, verbose);
	return;
}

void gctl::gm_tetrahedron_mesh::magobser(array<tensor> &out_obs, std::string magz_name, const array<point3dc> &obsp, verbose_type_e verbose)
{
	array<point3dc> magz(get_elenum(), point3dc(0.0, 0.0, 0.0));
	meshdata &data = get_data(magz_name);
	mesh_data_value_e vtype = data.valtype_;
	if (vtype == Vector)
	{
		magz = data.export_vector();
	}
	else
	{
		std::string err_str = "Unsupported data value type. From gctl::gm_tetrahedron_mesh::magkernel(...)";
		throw runtime_error(err_str);
	}

	int e_num = get_elenum();
	mag_ele_.resize(e_num);
	for (size_t i = 0; i < e_num; i++)
	{
		copy_type_tetrahedron(mag_ele_.get(i), elements.get(i));
	}
	callink_magnetic_para_direct(mag_ele_, mag_para_, magz);

	gctl::magobser(out_obs, mag_ele_, obsp, verbose);
	return;
}