/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "gctl/io.h"
#include "gctl/geometry.h"
#include "../lib/potential.h"

using namespace gctl;

int main(int argc, char const *argv[])
{
	array<vertex3dc> top_node, btm_node;
	array<mag_tricone> top_ele, btm_ele;

    gmshio fio;
    fio.init_file("data/stt/stt_1d_6371200_sph.msh", gctl::Input);
    fio.set_packed(gctl::Packed, gctl::Input);
    fio.read_mesh(top_ele, top_node);
	fio.read_mesh(btm_ele, btm_node);

	vertex3dc origin(point3dc(0.0, 0.0, 0.0), top_node.size());
	for (size_t i = 0; i < top_ele.size(); i++)
	{
		top_ele[i].set_origin(origin);
		btm_ele[i].set_origin(origin);
	}

	for (size_t i = 0; i < top_node.size(); i++)
    {
		top_node[i].set2module(6371200);
        btm_node[i].set2module(6371200 - 26500);
    }

	mag_dipole md;
    md.M = 7.61365e+22;
    md.n.set(point3dc(0.0, 0.0, 1.0));

	point3dc cen_c;
	array<point3dc> mag_B(top_ele.size());
	for (size_t i = 0; i < top_ele.size(); i++)
	{
		cen_c = 1.0/6.0*(*top_ele[i].vert[0] + *top_ele[i].vert[1] + *top_ele[i].vert[2] + 
			*btm_ele[i].vert[0] + *btm_ele[i].vert[1] + *btm_ele[i].vert[2]);
		mag_B[i] = magkernel_single(md, cen_c);		
	}

	array<magcone_para> top_para, btm_para;
    array<double> sus(top_ele.size(), 0.01);
	callink_magnetic_para(top_ele, top_para, mag_B);
	callink_magnetic_para(btm_ele, btm_para, mag_B);

	// 设置观测点位
	array<point3ds> obsp(top_ele.size());
	for (size_t i = 0; i < top_ele.size(); i++)
    {
		cen_c = 1.0/3.0*(*top_ele[i].vert[0] + *top_ele[i].vert[1] + *top_ele[i].vert[2]);

        obsp[i] = cen_c.c2s();
        obsp[i].rad += 200000;
    }

	// 正演计算
	array<double> obsval(obsp.size());
	array<point3dc> obsgrad;

	// 正演磁分量数据
	magobser(obsgrad, top_ele, btm_ele, obsp, sus, ShortMsg);

	// 保存网格
    fio.init_file("data/stt/stt_1d_out.msh", gctl::Output);
    fio.set_packed(gctl::NotPacked, gctl::Output);
    fio.save_mesh(top_ele, top_node);

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].x;
	}
	fio.save_data("Br", obsval, ElemData);

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].y;
	}
	fio.save_data("Bt", obsval, ElemData);

	for (int i = 0; i < obsp.size(); ++i)
	{
		obsval[i] = obsgrad[i].z;
	}
	fio.save_data("Bp", obsval, ElemData);
	return 0;
}