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

#include "cmath"

using namespace gctl;

int main(int argc, char const *argv[])
{
	try
	{
		std::string mesh_file = "../data/fmm3d/cube.1";

		// read triangular mesh's vertice and elements
		array<vertex3dc> tetgen_node;
		array<tetrahedron> tetgen_tet;
		read_Tetgen_node(mesh_file, tetgen_node);
		read_Tetgen_element(mesh_file, tetgen_tet, tetgen_node);

		array<fmm_vertex3dc> fmm_node;
		array<fmm_tetrahedron> fmm_ele;
		array<double> node_time(tetgen_node.size(), GCTL_BDL_MAX);

		array<double> mesh_slow(fmm_ele.size(), 1.0);
		create_fmm_mesh(tetgen_node, tetgen_tet, node_time, mesh_slow, fmm_node, fmm_ele);

		std::ofstream outfile;
		gctl::open_outfile(outfile, mesh_file, ".msh");
		save2gmsh(outfile, tetgen_tet, tetgen_node, gctl::NotPacked);

		// declare a source point and calculate
		seis_point3d_tet source;
		source.set(point3dc(5.0, 250.0, 250.0), 1);
		source.find_host_element(fmm_ele.get(), fmm_ele.size());

		// assign initial tags for elements
		source.host_ele->tag = 1;
		for (int i = 0; i < 4; i++)
		{
			source.host_ele->vert[i]->tag = 2;
			*source.host_ele->vert[i]->time_ptr = *source.host_ele->slow_ptr * 
			distance(*source.host_ele->vert[i], source);
		}

		// declare a source point and calculate
		seis_point3d_tet receiver;
		receiver.set(point3dc(995.0, 250.0, 495.0), 1);
		receiver.find_host_element(fmm_ele.get(), fmm_ele.size());

		std::vector<fmm_vertex3dc*> rece_node;
		for (int i = 0; i < 4; i++)
		{
			rece_node.push_back(receiver.host_ele->vert[i]);
		}

		array<double> jn_temp(fmm_ele.size());
		array<double> time_ele_grad(fmm_ele.size(), 0.0);
		sparray2d<double> jn(fmm_node.size(), fmm_ele.size(), 0.0);
		std::vector<fmm_vertex3dc*> wave_front;
		std::vector<fmm_vertex3dc*> march_record;

		double temp_val;
		for (int i = 0; i < 4; i++)
		{
			//初始化前四个梯度值
			//对元素的慢度求梯度即为源到顶点的距离
			temp_val= distance(*source.host_ele->vert[i], source);
			jn.at(source.host_ele->vert[i]->id)->set(source.host_ele->id, temp_val);
		}

		// calculate
		clock_t start = clock();
		fmm3d_forward_tetrahedron(&fmm_node, &fmm_ele, &wave_front, &march_record, &rece_node, &jn, &jn_temp);
		clock_t end = clock();
		std::cout << "FMM's time: " << 1000.0*(end - start)/(double)CLOCKS_PER_SEC << " ms" << std::endl;

		double r[4], w_sum;
		for (int i = 0; i < 4; i++)
		{
			r[i] = distance(*receiver.host_ele->vert[i], receiver) + GCTL_ZERO;
		}
		w_sum = 1.0/r[0] + 1.0/r[1] + 1.0/r[2] + 1.0/r[3];

		receiver.time = *receiver.host_ele->vert[0]->time_ptr/(r[0]*w_sum) + 
						*receiver.host_ele->vert[1]->time_ptr/(r[1]*w_sum) + 
						*receiver.host_ele->vert[2]->time_ptr/(r[2]*w_sum) + 
						*receiver.host_ele->vert[3]->time_ptr/(r[3]*w_sum);


		for (int i = 0; i < 4; i++)
		{
			jn.at(receiver.host_ele->vert[i]->id)->export_dense(time_ele_grad, 1.0/(r[i]*w_sum), gctl::AppendVal);
		}

		for (int i = 0; i < node_time.size(); i++)
		{
			if (node_time[i] == GCTL_BDL_MAX)
			{
				node_time[i] = NAN;
			}
		}

		std::cout << "Receiver's time = " << receiver.time << std::endl;
		save_gmsh_data(outfile, "Arrival time", node_time.get(), node_time.size(), NodeData, gctl::NotPacked);
		save_gmsh_data(outfile, "receiver's gradient", time_ele_grad.get(), time_ele_grad.size(), ElemData, gctl::NotPacked);
		outfile.close();
	}
	catch(std::exception &e)
	{
		GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
	}
}