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

#ifndef _GCTL_MAG_KERNEL_TESSEROID_H
#define _GCTL_MAG_KERNEL_TESSEROID_H

#include "gm_data.h"
#include "gctl/poly/tesseroid.h"
#include "gctl/utility/progress_bar.h"

#ifdef GCTL_POTENTIAL_MAGTESS

namespace gctl
{
	/**
	 * @brief tesseroid 单元体磁化参数
	 * 
	 */
	struct magtess_para
	{
		// 磁化矢量参数
		double bx, by, bz, h, f;
		// 地磁场磁化倾角（弧度）
		double geo_iarc, geo_darc;

		magtess_para()
		{
			bx = by = bz = h = f = geo_iarc = geo_darc = NAN;
		}
	};

	///< 带magtess_para属性的tesseroid结构体
	typedef type_tesseroid<magtess_para> mag_tesseroid;

	/**
	 * @brief S设置 Tesseroid 单元体的地磁场参数
	 * 
	 * @param out_para 磁化参数结构体
	 * @param inclina 地磁场倾角
	 * @param declina 地磁场偏角
	 */
	void set_geomag_angles(array<magtess_para> &out_para, const array<double> &inclina, const array<double> &declina);

	/**
	 * @brief 设置 Tesseroid 单元体的磁化参数
	 * 
	 * @param in_tess 单元体
	 * @param out_para 磁化参数结构体
	 * @param mag_B 磁化矢量
	 */
	void callink_magnetic_para(array<mag_tesseroid> &in_tess, array<magtess_para> &out_para, 
		const point3dc &mag_B);

	/**
	 * @brief 设置 Tesseroid 单元体的磁化参数
	 * 
	 * @param in_tess 单元体
	 * @param out_para 磁化参数结构体
	 * @param mag_B 磁化矢量
	 */
	void callink_magnetic_para(array<mag_tesseroid> &in_tess, array<magtess_para> &out_para, 
		const array<point3dc> &mag_B);

	/**
	 * @brief Calculate the magnetic parameters of given tesseroid elements respect to the spherical coordinates.
	 * 
	 * @note  The value of magnetic susceptibility is taken as one here. This is usefull for calculating 
	 * kernel matrix of the magnetic anomalies.
	 * 
	 * @param in_tess Input tesseroids
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle of the magnetization vector with respect to the local Cartesian coordinates at every tesseroid elements
	 * @param declina_deg declination angle of the magnetization vector with respect to the local Cartesian coordinates at every tesseroid elements
	 * @param field_tense  Tense of the Earth's magnetic field
	 */
	void callink_magnetic_para_earth_sph(array<mag_tesseroid> &in_tess, array<magtess_para> &out_para, 
		double inclina_deg, double declina_deg, double field_tense = GCTL_T0);
	
	/**
	 * @brief 计算tesseroid单元体的磁异常核函数
	 * 
	 * @param out_kernel 输出的核函数
	 * @param ele tesseroid单元体
	 * @param obsp 观测点
	 * @param comp_id 异常分量
	 * @param gauss_order 高斯积分阶数
	 * @param verbose 显示的信息类型
	 */
	void magkernel(matrix<double> &out_kernel, const array<mag_tesseroid> &ele, const array<point3ds> &obsp,
		magnetic_field_type_e comp_id = Za, int gauss_order = 4, verbose_type_e verbose = FullMsg);

	/**
	 * @brief 计算tesseroid单元体的磁异常
	 * 
	 * @param out_obs 输出的核函数
	 * @param ele tesseroid单元体
	 * @param obsp 观测点
	 * @param sus 磁化率
	 * @param comp_id 异常分量
	 * @param gauss_order 高斯积分阶数
	 * @param verbose 显示的信息类型
	 */
	void magobser(array<double> &out_obs, const array<mag_tesseroid> &ele, const array<point3ds> &obsp,
		const array<double> &sus, magnetic_field_type_e comp_id = Za, int gauss_order = 4,
		verbose_type_e verbose = FullMsg);
};

#endif // GCTL_POTENTIAL_MAGTESS

#endif // _GCTL_MAG_KERNEL_TESSEROID_H