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

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

		// 使用外部临时数组与向量
		array<bool> temp_index(fmm_node.size());
		array<double> jn_temp(fmm_ele.size());
		array<double> time_ele_grad(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;

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

		// declare a new seismic point
		seis_point2d_tri source_1;
		source_1.set(point2dc(40.0, 98.0), 0);

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

		std::vector<fmm_vertex2dc*> refl_nodes;
		for (int i = 0; i < fmm_node.size(); i++)
		{
			if (node_boundary[i] && fabs(fmm_node[i].y) < 1e-6)
			{
				refl_nodes.push_back(fmm_node.get(i));
			}
		}

		source2receiver_reflect(&fmm_node, &fmm_ele, &source_1, &receiver_1, &refl_nodes, 
			&time_ele_grad, &wave_front, &march_record, &jn, &jn_temp, &temp_index);

		std::cout << "receiver's time = " << receiver_1.time << std::endl;
		save_gmsh_data(outfile, "Elements' gradient", time_ele_grad.get(), time_ele_grad.size(), ElemData, gctl::NotPacked);

		receiver_1.set(point2dc(235.0, 98.0), 1);
		source2receiver_reflect(&fmm_node, &fmm_ele, &source_1, &receiver_1, &refl_nodes, 
			&time_ele_grad, &wave_front, &march_record, &jn, &jn_temp, &temp_index);

		std::cout << "receiver's time = " << receiver_1.time << std::endl;
		save_gmsh_data(outfile, "Elements' gradient 2", 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;
}