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

using namespace gctl;

int main(int argc, char const *argv[])
{
	try
	{
		// set up observation points
		array<point3dc> obes;
		gridspace(point3dc(0, 0, 0), point3dc(100, 0, 0), point3dc(0, 0, 0), point3dc(0, 100, 0), 41, 41, obes);

		gmshio fio("data/cube/cube_sorted", Input);

		array<vertex3dc> nodes;
		array<grav_triangle> eles;
		fio.read_mesh(eles, nodes);

		array<gravtri_para> gpara(eles.size());
		callink_gravity_para(eles, gpara);

		array<double> out_data;
		array<point3dc> out_grad;
		array<tensor> out_tens;

		gobser(out_data, eles, obes, 1.0, ShortMsg);
		save_netcdf_grid("data/cube/cube_gravity", out_data, 41, 41, 0.0, 2.5, 0.0, 2.5, "easting", "northing", "potential");


		gobser(out_grad, eles, obes, 1.0, ShortMsg);
		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].x;
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "gravity_x");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].y;
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "gravity_y");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].z;
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "gravity_z");


		gobser(out_tens, eles, obes, 1.0, ShortMsg);
		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 0);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_xx");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 1);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_xy");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 2);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_xz");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 0);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_yx");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 1);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_yy");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 2);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_yz");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 0);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_zx");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 1);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_zy");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 2);
		}
		append_netcdf_grid("data/cube/cube_gravity", out_data, "easting", "northing", "tensor_zz");
	}
	catch (std::exception &e)
	{
		GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
	}
	return 0;
}