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

#include "gm_data.h"
#include "gctl/optimization.h"

#ifdef GCTL_POTENTIAL_AUTODIFF
#include "autodiff/reverse/var.hpp"
#include "autodiff/reverse/var/eigen.hpp"
#endif // GCTL_POTENTIAL_AUTODIFF

namespace gctl
{
	struct magblock_para
	{
		// 磁化倾角与磁化偏角 （度）
		double inclina_deg, declina_deg;
	};

	typedef type_block<magblock_para> mag_block; ///< 带mag_para属性的块体结构体

	/**
	 * @brief Calculate the kernel matrix of magnetic data.
	 * 
	 * @param out_kernel      Output kernal matrix
	 * @param ele             
	 * @param obsp 
	 * @param geo_inclina 
	 * @param geo_declina 
	 * @param comp_id 
	 * @param verbose 
	 */
	void magkernel(matrix<double> &out_kernel, const array<mag_block> &ele, const array<point3dc> &obsp, 
		double geo_inclina, double geo_declina, magnetic_field_type_e comp_id = DeltaT, verbose_type_e verbose = FullMsg);

	void magkernel(matrix<double> &out_kernel, const array<mag_block> &ele, const array<point3dc> &obsp, 
		magnetic_field_type_e comp_id = Za, verbose_type_e verbose = FullMsg);

	void magobser(array<double> &out_obs, const array<mag_block> &ele, const array<point3dc> &obsp, 
		const array<double> &sus, double geo_inclina, double geo_declina, magnetic_field_type_e comp_id = DeltaT, 
		verbose_type_e verbose = FullMsg);

	void magobser(array<double> &out_obs, const array<mag_block> &ele, const array<point3dc> &obsp, 
		const array<double> &sus, magnetic_field_type_e comp_id = Za, verbose_type_e verbose = FullMsg);

#ifdef GCTL_POTENTIAL_AUTODIFF

	double magobser_wrt_thickness(array<double> &btm_derivatives, loss_func &lf, 
        const array<point3dc> &obsp, const array<double> &sus, const array<mag_block> &ele, 
        magnetic_field_type_e comp_id = Za, verbose_type_e verbose = FullMsg);

#endif // GCTL_POTENTIAL_AUTODIFF

};

#endif // _GCTL_MAG_KERNEL_BLOCK_H