/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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> node;
	array<mag_triangle> mag_ele;

    gmshio fio;
    fio.init_file("data/stt/stt_2d.msh", gctl::Input);
    fio.set_packed(gctl::Packed, gctl::Input);
    fio.read_mesh(mag_ele, node);

    array<magtri_para> mag_para;
    array<double> sus(mag_ele.size(), 1.0);

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

	point3ds cen_s;
	array<point3dc> mag_B(mag_ele.size());
	for (size_t i = 0; i < mag_ele.size(); i++)
	{
		cen_s = mag_ele[i].center().c2s();
		cen_s.rad -= 20000;
		mag_B[i] = magkernel_single(md, cen_s.s2c());		
	}

	callink_magnetic_para(mag_ele, mag_para, mag_B);

	// 设置观测点位
	array<point3ds> obsp(mag_ele.size());
	for (size_t i = 0; i < mag_ele.size(); i++)
    {
        obsp[i] = mag_ele[i].center().c2s();
        obsp[i].rad += 400000;
    }

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

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

    // 改变球体半径大小
    for (size_t i = 0; i < node.size(); i++)
    {
        node[i].set2module(6371008.8 - 40000);
    }

	callink_magnetic_para(mag_ele, mag_para, mag_B);

    magobser(obsgrad2, mag_ele, obsp, sus, ShortMsg);

	// 保存网格
    fio.init_file("data/stt/stt_2d_out.msh", gctl::Output);
    fio.set_packed(gctl::NotPacked, gctl::Output);
    fio.save_mesh(mag_ele, node);

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

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

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

	return 0;
}