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

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

namespace gctl
{
    /**
     * @brief 四面体磁性参数结构体
     * 
     */
    struct magtet_para
	{
		point3dc B; ///< 磁化强度矢量 directional magnetizations
		tensor F[4]; ///< 面的张量积 tensor products of four facets
		tensor E[12]; ///< 边的张量积 tensor products of six edges
		double edglen[12]; ///< 边的长度 edge lengths of six edges
	};

	typedef type_tetrahedron<magtet_para> mag_tetrahedron; ///< 带磁性参数的四面体结构体

    /**
     * @brief Calculate the magnetic parameters of given tetrahedral elements.
     * 
     * @param in_tet Input and output elements
     * @param out_para Output parameters
     * @param mag_B magnetization vecrtors
     */
    void callink_magnetic_para(array<mag_tetrahedron> &in_tet, array<magtet_para> &out_para, const array<point3dc> &mag_B);

    /**
	 * @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 and output elements
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle
	 * @param declina_deg declination angle
	 * @param field_tense  Tense of the Earth's magnetic field
	 */
	void callink_magnetic_para_earth(array<mag_tetrahedron> &in_tet, array<magtet_para> &out_para, 
		double inclina_deg, double declina_deg, double field_tense = GCTL_T0);

    /**
	 * @brief Calculate the magnetic parameters of given tetrahedral elements wrt. 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 wrt. the local Cartesian coordinates at every tetrahedral elements
	 * @param declina_deg declination angle of the magnetization vector wrt. 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<mag_tetrahedron> &in_tet, array<magtet_para> &out_para, 
		double inclina_deg, double declina_deg, array<point3dc> *mag_vec = nullptr, double field_tense = GCTL_T0);

    /**
     * @brief Calculate the magnetic componments of a single tetrahedron element under the Cartesian coordinates
     * 
     * @param out_b The output magnetic componments
     * @param a_ele The tetrahedron element
     * @param a_op The observation point
     * @return point3dc The returned magnetic componments
     */
    point3dc magkernel_single(const mag_tetrahedron &a_ele, const point3dc &a_op);

    /**
     * @brief Calculate the magnetic componments of a single tetrahedron element under the spherical coordinates
     * 
     * @param a_ele The tetrahedron element
     * @param a_op The observation point
     * @param R_ptr Pointer of the coordinate transform matrix. This is used to speed up the computation.
     * @return point3dc The returned magnetic componments
     */
    point3dc magkernel_single(const mag_tetrahedron &a_ele, const point3ds &a_op, tensor *R_ptr = nullptr);
    
    /**
     * @brief Calculate the magnetic componments of tetrahedron elements under the Cartesian coordinates
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly.
     * @param ele The tetrahedron elements
     * @param obsp The observation points
     * @param sus Magnetic susceptibilities
     * @param verbose Output info level
     */
    void magobser(array<point3dc> &out_obs, const array<mag_tetrahedron> &ele, const array<point3dc> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the magnetic componments of tetrahedron elements under the Spherical coordinates
     * 
     * @param out_obs Output magnetic field data. Directional components are stored accordingly.
     * @param ele The tetrahedron elements
     * @param obsp The observation points
     * @param sus Magnetic susceptibilities
     * @param verbose Output info level
     */
    void magobser(array<point3dc> &out_obs, const array<mag_tetrahedron> &ele, const array<point3ds> &obsp, 
        const array<double> &sus, verbose_type_e verbose = FullMsg);
};

#endif // _GCTL_MAG_KERNEL_TETRAHEDRON_H