/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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_TETRAHEDRON_REN2017_H
#define _GCTL_MAG_KERNEL_TETRAHEDRON_REN2017_H

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

namespace gctl
{
    struct magtet_para_ren17
	{
		double mag_amp[4]; // 四面体四个面的磁化强度
		point3dc nf[4]; // 四面体面外法线矢量
		point3dc ne[12]; // 四面体边外法线矢量
		point3dc te[12]; // 四面体边切线矢量
	};

	typedef type_tetrahedron<magtet_para_ren17> magtet_ren17; ///< 带magtet_para_ren17属性的四面体结构体

    /**
	 * @brief Calculate the magnetic parameters of given tetrahedral elements.
	 * 
	 * @note  The magnetic susceptibility is impliclity setted using the magnetic magnetization values. 
	 * This is usefull for calculating magnetic anomalies of certain magnetizations.
	 * 
	 * @param in_tet Input elements
	 * @param out_para Output parameters
	 * @param magz Magnetic magnetizations
	 */
	void callink_magnetic_para_direct(array<magtet_ren17> &in_tet, array<magtet_para_ren17> &out_para, const array<point3dc> &magz);

	/**
	 * @brief Calculate the magnetic parameters of given tetrahedral elements.
	 * 
	 * @note  The value of magnetic susceptibility is taken as one here. This is usefull for calculating 
	 * kernel matrix of the magnetic anomalies.
	 * 
	 * @param in_tet Input elements
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle
	 * @param declina_deg declination angle
	 * @param field_tense  Tense of the magnetic field
	 */
	void callink_magnetic_para_earth(array<magtet_ren17> &in_tet, array<magtet_para_ren17> &out_para, 
		double inclina_deg, double declina_deg, double field_tense = GCTL_T0);

    /**
	 * @brief Calculate the magnetic parameters of given tetrahedral 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_tet Input elements
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle of the magnetization vector with respect to the local Cartesian coordinates at every tetrahedral elements
	 * @param declina_deg declination angle of the magnetization vector with respect to the local Cartesian coordinates at every tetrahedral elements
	 * @param mag_vec Output magnetization vectors (This is useful for data visualization)
	 * @param field_tense  Tense of the Earth's magnetic field
	 */
	void callink_magnetic_para_earth_sph(array<magtet_ren17> &in_tet, array<magtet_para_ren17> &out_para, 
		double inclina_deg, double declina_deg, array<point3dc> *mag_vec = nullptr, double field_tense = GCTL_T0);

    /**
	 * @brief Calculate the magnetic parameters of given tetrahedral 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_tet Input elements
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle of the magnetization vector with respect to the local Cartesian coordinates at every tetrahedral elements
	 * @param declina_deg declination angle of the magnetization vector with respect to the local Cartesian coordinates at every tetrahedral elements
	 * @param mag_vec Output magnetization vectors (This is useful for data visualization)
	 * @param field_tense  Tense of the Earth's magnetic field
	 */
	void callink_magnetic_para_earth_sph(magtet_ren17 &in_tet, magtet_para_ren17 &out_para, 
		double inclina_deg, double declina_deg, point3dc *mag_vec = nullptr, double field_tense = GCTL_T0);

    /**
     * @brief Calculate the kernel matrix of the magnetic poential data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magkernel(matrix<double> &out_kernel, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the kernel matrix of the magnetic conponents data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix. Directional components are stored accordingly.
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magkernel(matrix<point3dc> &out_kernel, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the kernel matrix of the magnetic tensor data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix. Directional components are stored accordingly.
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magkernel(matrix<tensor> &out_kernel, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the kernel matrix of the deltaT anomaly data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param geo_declina Declination angle of the geo-magnetic field.
     * @param geo_inclina Inclination angle of the geo-magnetic field.
     * @param verbose Output info level
     */
    void magkernel(matrix<double> &out_kernel, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        double geo_declina, double geo_inclina, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the kernel matrix of the magnetic conponents data.
     * 
     * @note Use callink_magnetic_para_earth_sph() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix. Directional components are stored accordingly.
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magkernel(matrix<point3dc> &out_kernel, const array<magtet_ren17> &ele, const array<point3ds> &obsp, 
        verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the kernel matrix of the magnetic tensor data.
     * 
     * @note Use callink_magnetic_para_earth_sph() to initialize the magtet_ren17 elements.
     * 
     * @param out_kernel Output kernel matrix. Directional components are stored accordingly.
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magkernel(matrix<tensor> &out_kernel, const array<magtet_ren17> &ele, const array<point3ds> &obsp, 
        verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the magnetic componments of a single tetrahedron element under the spherical coordinates
     * 
     * @param ele The tetrahedron element
     * @param obsp The observation point
     * @return point3dc The returned magnetic componments
     */
    point3dc magkernel_single(const magtet_ren17 &ele, const point3ds &obsp);

    /**
     * @brief Calculate magnetic potential data.
     * 
     * @note  The magnetic susceptibility is impliclity setted using the magnetic magnetization values. Use callink_magnetic_para_direct() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites 
     * @param verbose Output info level
     */
    void magobser(array<double> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic component data.
     * 
     * @note  The magnetic susceptibility is impliclity setted using the magnetic magnetization values. Use callink_magnetic_para_direct() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly. Delta_T anomalies could be retrived using the function magnetic_components2deltaT().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magobser(array<point3dc> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic tensor data.
     * 
     * @note  The magnetic susceptibility is impliclity setted using the magnetic magnetization values. Use callink_magnetic_para_direct() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly. Delta_T gradient anomalies could be retrived using the function magnetic_tensors2deltaTs().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param verbose Output info level
     */
    void magobser(array<tensor> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic potential data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param sus Magnetic susceptibilities 
     * @param verbose Output info level
     */
    void magobser(array<double> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic component data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly. Delta_T anomalies could be retrived using the function magnetic_components2deltaT().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param sus Magnetic susceptibilities 
     * @param verbose Output info level
     */
    void magobser(array<point3dc> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic tensor data.
     * 
     * @note Use callink_magnetic_para_earth() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional tensors are stored accordingly. Delta_T gradient anomalies could be retrived using the function magnetic_tensors2deltaTs().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param sus Magnetic susceptibilities 
     * @param verbose Output info level
     */
    void magobser(array<tensor> &out_obs, const array<magtet_ren17> &ele, const array<point3dc> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic component data.
     * 
     * @note Use callink_magnetic_para_earth_sph() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly. Delta_T anomalies could be retrived using the function magnetic_components2deltaT().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param sus Magnetic susceptibilities 
     * @param verbose Output info level
     */
    void magobser(array<point3dc> &out_obs, const array<magtet_ren17> &ele, const array<point3ds> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate magnetic tensor data.
     * 
     * @note Use callink_magnetic_para_earth_sph() to initialize the magtet_ren17 elements.
     * 
     * @param out_obs Output magnetic field data. Directional tensors are stored accordingly. Delta_T gradient anomalies could be retrived using the function magnetic_tensors2deltaTs().
     * @param ele Magnetic tetrahedrons.
     * @param obsp Observation sites
     * @param sus Magnetic susceptibilities 
     * @param verbose Output info level
     */
    void magobser(array<tensor> &out_obs, const array<magtet_ren17> &ele, const array<point3ds> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);
};

#endif // _GCTL_MAG_KERNEL_TETRAHEDRON_REN2017_H