/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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"

using namespace gctl;

int main(int argc, char const *argv[])
{
	try
	{
		std::string mesh_file = "../data/fmm2d/sample_mesh.1";
		std::string mod_file = "../data/fmm2d/sample_model.txt";

		// read triangular mesh's vertice and elements
		array<vertex2dc> mesh_node;
		array<triangle2d> mesh_ele;
		array<int> node_boundary;

		read_Triangle_node(mesh_file, mesh_node, gctl::Packed, &node_boundary);
		read_Triangle_element(mesh_file, mesh_ele, mesh_node);

		array<fmm_vertex2dc> fmm_node;
		array<fmm_triangle2d> fmm_ele;
		array<double> node_time(mesh_node.size(), GCTL_BDL_MAX);

		// save to gmsh file
		std::ofstream outfile;
		open_outfile(outfile, mesh_file, ".msh");
		save2gmsh(outfile, mesh_ele, mesh_node, gctl::NotPacked);

		array<double> mesh_slow;
		text_descriptor desc;
		desc.head_num_ = 1;

		get_data_column(mod_file, {&mesh_slow}, {1}, desc);
		create_fmm_mesh(mesh_node, mesh_ele, node_time, mesh_slow, fmm_node, fmm_ele);

		for (int i = 0; i < fmm_node.size(); i++)
		{
			if (node_boundary[i] && fabs(mesh_node[i].x) < 1e-6 )
			{
				*fmm_node.at(i).time_ptr = 0.0;
				fmm_node.at(i).tag = 2;
			}
		}

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

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

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

		array<double> node_grad(fmm_ele.size(), 0.0);
		jn.at(4451)->export_dense(node_grad);

		save_gmsh_data(outfile, "Arrival time", node_time.get(), node_time.size(), NodeData, gctl::NotPacked);
		save_gmsh_data(outfile, "Model gradient", node_grad.get(), node_grad.size(), ElemData, gctl::NotPacked);
		outfile.close();
	}
	catch(std::exception &e)
	{
		GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
	}
	return 0;
}