/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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;
		read_Triangle_node(mesh_file, mesh_node);
		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());

		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);

		// declare a source point and calculate
		seis_point2d_tri source_1;
		source_1.set(point2dc(5.0, 10.0), 1);

		// declare a receiver point and a gradient array and calculate
		seis_point2d_tri receiver_1;
		receiver_1.set(point2dc(235.0, 90.0), 1);

		array<double> time_ele_grad(fmm_ele.size());

		// calculate
		source2receiver_direct(&fmm_node, &fmm_ele, &source_1, &receiver_1, &time_ele_grad);

		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_1.time << std::endl;

		// save to gmsh file
		std::ofstream outfile;
		open_outfile(outfile, mesh_file, ".msh");
		save2gmsh(outfile, mesh_ele, mesh_node, gctl::NotPacked);
		save_gmsh_data(outfile, "First arrival time", node_time.get(), node_time.size(), NodeData, gctl::NotPacked);
		save_gmsh_data(outfile, "Elements' 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);
	}
	return 0;
}