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

		point3dc cen;
		for (int i = 0; i < fmm_ele.size(); i++)
		{
			cen = 0.25*(*fmm_ele[i].vert[0] + *fmm_ele[i].vert[1] + 
				*fmm_ele[i].vert[2] + *fmm_ele[i].vert[3]);
			if (cen.x > 400 && cen.x < 600 && cen.y > 300 && cen.y < 400 && cen.z > 300 && cen.z < 400)
			{
				mesh_slow[i] = 0.25;
			}
		}

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

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

		// setup temporary arrays
		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;

		// calculate
		clock_t start = clock();
		source2receiver_direct(&fmm_node, &fmm_ele, &source, &receiver, &time_ele_grad, &wave_front, &march_record, &jn, &jn_temp);
		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;
			}
		}

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