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

using namespace gctl::geometry3d;

void gctl::callink_gravity_para(array<grav_tetrahedron> &in_tet, array<gravtet_para> &out_para)
{
	point3dc v1, v2, v3, nf;

	out_para.resize(in_tet.size());
	for (int i = 0; i < in_tet.size(); ++i)
	{
		if (in_tet[i].vert[0] == nullptr || in_tet[i].vert[1] == nullptr || 
			in_tet[i].vert[2] == nullptr || in_tet[i].vert[3] == nullptr)
		{
			throw runtime_error("Invalid vertex pointer. From callink_gravity_para(...)");
		}

		for (int f = 0; f < 4; ++f)
		{
			v1 = *in_tet[i].fget(f, 1) - *in_tet[i].fget(f, 0);
			v2 = *in_tet[i].fget(f, 2) - *in_tet[i].fget(f, 0);
			nf = cross(v1, v2).normal();
			out_para[i].F[f] = kron(nf, nf);

			for (int e = 0; e < 3; ++e)
			{
				v3 = *in_tet[i].fget(f, (e+1)%3) - *in_tet[i].fget(f, e);
				out_para[i].edglen[e+f*3] = v3.module();
				out_para[i].E[e+f*3] = kron(nf, cross(v3, nf).normal());
			}
		}

		in_tet[i].att = out_para.get(i);
	}
	return;
}

typedef void (*gkernel_tetra_ptr)(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);

void gkernel_tetrahedron_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vx(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vxx(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vxy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vxz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vyy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vyz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vzz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose);

/**
 * @brief      Integrated callback function of the gravitational kernel of a tetrahedron element
 *
 * @param      out_kernel  The output kernel
 * @param[in]  ele         The tetrahedron elements
 * @param[in]  obsp        The observation points
 * @param[in]  comp_id     The component identifier
 * @param[in]  verbose     The verbose level
 */
void gctl::gkernel(matrix<double> &out_kernel, const array<grav_tetrahedron> &ele, 
	const array<point3dc> &obsp, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gkernel_tetra_ptr tetra_kernel;

	switch (comp_id)
	{
		case GravPot:
			tetra_kernel = gkernel_tetrahedron_pot;
			break;
		case Vx:
			tetra_kernel = gkernel_tetrahedron_vx;
			break;
		case Vy:
			tetra_kernel = gkernel_tetrahedron_vy;
			break;
		case Vz:
			tetra_kernel = gkernel_tetrahedron_vz;
			break;
		case Txx:
			tetra_kernel = gkernel_tetrahedron_vxx;
			break;
		case Txy:
			tetra_kernel = gkernel_tetrahedron_vxy;
			break;
		case Txz:
			tetra_kernel = gkernel_tetrahedron_vxz;
			break;
		case Tyx:
			tetra_kernel = gkernel_tetrahedron_vxy;
			break;
		case Tyy:
			tetra_kernel = gkernel_tetrahedron_vyy;
			break;
		case Tyz:
			tetra_kernel = gkernel_tetrahedron_vyz;
			break;
		case Tzx:
			tetra_kernel = gkernel_tetrahedron_vxz;
			break;
		case Tzy:
			tetra_kernel = gkernel_tetrahedron_vyz;
			break;
		case Tzz:
			tetra_kernel = gkernel_tetrahedron_vzz;
			break;
		default:
			tetra_kernel = gkernel_tetrahedron_vz;
			break;
	}

	return tetra_kernel(out_kernel, ele, obsp, verbose);
}


typedef void (*gkernel_tetra_ptr_sph)(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);

void gkernel_tetrahedron_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vrr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vrt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vrp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vtt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vtp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);
void gkernel_tetrahedron_vpp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose);

/**
 * @brief      Integrated callback function of the gravitational kernel of a tetrahedron element
 *
 * @param      out_kernel  The output kernel
 * @param[in]  ele         The tetrahedron elements
 * @param[in]  obsp        The observation points
 * @param[in]  comp_id     The component identifier
 * @param[in]  verbose     The verbose level
 */
void gctl::gkernel(matrix<double> &out_kernel, const array<grav_tetrahedron> &ele, 
	const array<point3ds> &obsp, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gkernel_tetra_ptr_sph tetra_kernel;

	switch (comp_id)
	{
		case GravPot:
			tetra_kernel = gkernel_tetrahedron_pot;
			break;
		case Vz:
			tetra_kernel = gkernel_tetrahedron_vr;
			break;
		case Vy:
			tetra_kernel = gkernel_tetrahedron_vt;
			break;
		case Vx:
			tetra_kernel = gkernel_tetrahedron_vp;
			break;
		case Tzz:
			tetra_kernel = gkernel_tetrahedron_vrr;
			break;
		case Tzy:
			tetra_kernel = gkernel_tetrahedron_vrt;
			break;
		case Tzx:
			tetra_kernel = gkernel_tetrahedron_vrp;
			break;
		case Tyz:
			tetra_kernel = gkernel_tetrahedron_vrt;
			break;
		case Tyy:
			tetra_kernel = gkernel_tetrahedron_vtt;
			break;
		case Tyx:
			tetra_kernel = gkernel_tetrahedron_vtp;
			break;
		case Txz:
			tetra_kernel = gkernel_tetrahedron_vrp;
			break;
		case Txy:
			tetra_kernel = gkernel_tetrahedron_vtp;
			break;
		case Txx:
			tetra_kernel = gkernel_tetrahedron_vpp;
			break;
		default:
			tetra_kernel = gkernel_tetrahedron_vr;
			break;
	}

	return tetra_kernel(out_kernel, ele, obsp, verbose);
}


double gkernel_tetra_potential_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op);
gctl::point3dc gkernel_tetra_gradient_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op);
gctl::tensor gkernel_tetra_tensor_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op);

double gkernel_tetra_potential_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op);
gctl::point3dc gkernel_tetra_gradient_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op);
gctl::tensor gkernel_tetra_tensor_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op);


void gkernel_tetrahedron_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_potential");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_potential_sig(ele[j], obsp[i]);
		}
	}
	return;
}

void gkernel_tetrahedron_vx(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vx");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig(ele[j], obsp[i]).x;
		}
	}
	return;
}

void gkernel_tetrahedron_vy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vy");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig(ele[j], obsp[i]).y;
		}
	}
	return;
}

void gkernel_tetrahedron_vz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vz");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig(ele[j], obsp[i]).z;
		}
	}
	return;
}

void gkernel_tetrahedron_vxx(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vxx");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(0, 0);
		}
	}
	return;
}

void gkernel_tetrahedron_vxy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vxy");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(0, 1);
		}
	}
	return;
}

void gkernel_tetrahedron_vxz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vxz");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(0, 2);
		}
	}
	return;
}

void gkernel_tetrahedron_vyy(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vyy");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(1, 1);
		}
	}
	return;
}

void gkernel_tetrahedron_vyz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vyz");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(1, 2);
		}
	}
	return;
}

void gkernel_tetrahedron_vzz(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3dc> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vzz");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig(ele[j], obsp[i]).at(2, 2);
		}
	}
	return;
}


void gkernel_tetrahedron_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_potential");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_potential_sig_sph(ele[j], obsp[i]);
		}
	}
	return;
}

void gkernel_tetrahedron_vr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vr");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig_sph(ele[j], obsp[i]).x;
		}
	}
	return;
}

void gkernel_tetrahedron_vt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vt");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig_sph(ele[j], obsp[i]).y;
		}
	}
	return;
}

void gkernel_tetrahedron_vp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vp");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_gradient_sig_sph(ele[j], obsp[i]).z;
		}
	}
	return;
}

void gkernel_tetrahedron_vrr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrr");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(0, 0);
		}
	}
	return;
}

void gkernel_tetrahedron_vrt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrt");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(0, 1);
		}
	}
	return;
}

void gkernel_tetrahedron_vrp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrp");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(0, 2);
		}
	}
	return;
}

void gkernel_tetrahedron_vtt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vtt");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(1, 1);
		}
	}
	return;
}

void gkernel_tetrahedron_vtp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vtp");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(1, 2);
		}
	}
	return;
}

void gkernel_tetrahedron_vpp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::grav_tetrahedron> &ele, 
	const gctl::array<gctl::point3ds> &obsp, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();

	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vpp");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tetra_tensor_sig_sph(ele[j], obsp[i]).at(2, 2);
		}
	}
	return;
}


void gctl::gobser(array<double> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3dc> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_potential");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tetra_potential_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gctl::gobser(array<point3dc> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3dc> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, point3dc(0.0, 0.0, 0.0));

	gctl::progress_bar bar(e_size, "gobser_gradient");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] = out_obs[i] + gkernel_tetra_gradient_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gctl::gobser(array<tensor> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3dc> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, tensor(0.0));

	gctl::progress_bar bar(e_size, "gobser_tensor");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] = out_obs[i] + gkernel_tetra_tensor_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gctl::gobser(array<double> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3ds> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_potential");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tetra_potential_sig_sph(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gctl::gobser(array<point3dc> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3ds> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, point3dc(0.0, 0.0, 0.0));

	gctl::progress_bar bar(e_size, "gobser_gradient");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] = out_obs[i] + gkernel_tetra_gradient_sig_sph(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gctl::gobser(array<tensor> &out_obs, const array<grav_tetrahedron> &ele, 
	const array<point3ds> &ops, const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();

	out_obs.resize(o_size, tensor(0.0));

	gctl::progress_bar bar(e_size, "gobser_tensor");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] = out_obs[i] + gkernel_tetra_tensor_sig_sph(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}


// define individual algorithm here
double gkernel_tetra_potential_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum, edge_sum;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	gctl::point3dc face_tmp(0.0, 0.0, 0.0);
	gctl::point3dc edge_tmp(0.0, 0.0, 0.0);
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	int *v_order = a_ele.vec_order;

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - a_op;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - a_op;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - a_op;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0], cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * r_ijk[0];
		face_sum += dot(r_ijk[0], face_tmp) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], a_op);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], a_op);

			re = 0.5*(*a_ele.vert[v_order[e+3*f]] + *a_ele.vert[v_order[(e+1)%3+3*f]]) - a_op;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * re;
			edge_sum += dot(re, edge_tmp) * Le;
		}
	}

	// 重力正演中通常z方向取垂直向下为正方向
	return -0.5e+8*GCTL_G0*(face_sum - edge_sum);
}

gctl::point3dc gkernel_tetra_gradient_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	gctl::point3dc face_sum(0.0, 0.0, 0.0);
	gctl::point3dc edge_sum(0.0, 0.0, 0.0);
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	int *v_order = a_ele.vec_order;

	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - a_op;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - a_op;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - a_op;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0], cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_sum = face_sum + (gp->F[f] * r_ijk[0]) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], a_op);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], a_op);

			re = 0.5*(*a_ele.vert[v_order[e+3*f]] + *a_ele.vert[v_order[(e+1)%3+3*f]]) - a_op;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_sum = edge_sum + (gp->E[e+3*f] * re) * Le;
		}
	}

	gctl::point3dc out_p = 1.0e+8*GCTL_G0*(face_sum - edge_sum);
	out_p.z *= -1.0; // 重力正演中通常z方向取垂直向下为正方向
	return out_p;
}

gctl::tensor gkernel_tetra_tensor_sig(const gctl::grav_tetrahedron &a_ele, const gctl::point3dc &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	gctl::point3dc r_ijk[3];
	gctl::tensor face_sum(0.0);
	gctl::tensor edge_sum(0.0);
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	int *v_order = a_ele.vec_order;

	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - a_op;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - a_op;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - a_op;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0], cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_sum = face_sum + wf * gp->F[f];

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], a_op);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], a_op);

			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_sum = edge_sum + Le  * gp->E[e+3*f];
		}
	}

	// 重力正演中通常z方向取垂直向下为正方向
	gctl::tensor out_t = 1.0e+8*GCTL_G0*(face_sum - edge_sum);
	out_t[0][0] *= -1.0;
	out_t[0][1] *= -1.0;
	out_t[1][0] *= -1.0;
	out_t[1][1] *= -1.0;
	out_t[2][2] *= -1.0;
	return out_t;
}

double gkernel_tetra_potential_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum, edge_sum;
	gctl::point3dc re, op_c;
	gctl::point3dc r_ijk[3];
	gctl::point3dc face_tmp(0.0, 0.0, 0.0);
	gctl::point3dc edge_tmp(0.0, 0.0, 0.0);
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	op_c = s2c(a_op);

	int *v_order = a_ele.vec_order;

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - op_c;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - op_c;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * r_ijk[0];
		face_sum = face_sum + dot(r_ijk[0], face_tmp) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], op_c);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], op_c);

			re = 0.5*(*a_ele.vert[v_order[e+3*f]] + *a_ele.vert[v_order[(e+1)%3+3*f]]) - op_c;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * re;
			edge_sum = edge_sum + dot(re, edge_tmp) * Le;
		}
	}

	// 重力正演中通常负r方向为正方向
	return -0.5e+8*GCTL_G0*(face_sum - edge_sum);
}

gctl::point3dc gkernel_tetra_gradient_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	gctl::point3dc face_sum(0.0, 0.0, 0.0);
	gctl::point3dc edge_sum(0.0, 0.0, 0.0);
	gctl::tensor R;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	R[0][0] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][1] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][2] = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[1][0] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][1] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][2] = -1.0*sin((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[2][0] = -1.0*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][1] = cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][2] = 0.0;

	op_c = s2c(a_op);

	int *v_order = a_ele.vec_order;

	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - op_c;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - op_c;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * r_ijk[0];
		face_sum = face_sum + (R * face_tmp) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], op_c);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], op_c);

			re = 0.5*(*a_ele.vert[v_order[e+3*f]] + *a_ele.vert[v_order[(e+1)%3+3*f]]) - op_c;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * re;
			edge_sum = edge_sum + (R * edge_tmp) * Le;
		}
	}

	gctl::point3dc out_p = 1.0e+8*GCTL_G0*(face_sum - edge_sum);
	out_p.x *= -1.0; // 重力正演中通常负r方向为正方向
	return out_p;
}

gctl::tensor gkernel_tetra_tensor_sig_sph(const gctl::grav_tetrahedron &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::tensor face_tmp, edge_tmp;
	gctl::tensor face_sum(0.0), edge_sum(0.0);
	// 这里我们需要完整的转换矩阵
	gctl::tensor R, R_T;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravtet_para* gp = a_ele.att;

	R[0][0] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][1] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][2] = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[1][0] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][1] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][2] = -1.0*sin((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[2][0] = -1.0*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][1] = cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][2] = 0.0;

	R_T = R.transpose();

	op_c = s2c(a_op);

	int *v_order = a_ele.vec_order;

	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.vert[v_order[3*f]] - op_c;
		r_ijk[1] = *a_ele.vert[v_order[3*f+1]] - op_c;
		r_ijk[2] = *a_ele.vert[v_order[3*f+2]] - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * R_T;
		face_sum = face_sum + (R * face_tmp) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.vert[v_order[e+3*f]], op_c);
			dv2 = distance(*a_ele.vert[v_order[(e+1)%3+3*f]], op_c);

			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * R_T;
			edge_sum = edge_sum + (R * edge_tmp) * Le;
		}
	}

	// 重力正演中通常负r方向为正方向
	gctl::tensor out_t = 1.0e+8*GCTL_G0*(face_sum - edge_sum);
	out_t[0][0] *= -1.0;
	out_t[1][2] *= -1.0;
	out_t[2][1] *= -1.0;
	out_t[1][1] *= -1.0;
	out_t[2][2] *= -1.0;
	return out_t;
}
