/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "toml.hpp"
#include "../lib/potential.h"
#include "gctl/core.h"
#include "gctl/geometry.h"
#include "gctl/io.h"

using namespace gctl;
int main(int argc, char *argv[]) try
{
    toml::value toml_data = toml::parse(argv[1]);
    std::string in_file = toml::find<std::string>(toml_data, "input-file");
    std::string out_file = toml::find<std::string>(toml_data, "output-file");
    std::string rho_name = toml::find<std::string>(toml_data, "rho-name");
    double ab_rho = toml::find<double>(toml_data, "ab-rho");

    // read model
    array<triangle2d> ele;
    array<vertex2dc> vert;
    array<gmsh_physical_group> phys;
    _2i_vector ele_tag;

    gmshio mio;
    mio.init_file(in_file, Input);
    mio.set_packed(NotPacked, Input);
    mio.read_mesh(ele, vert, &ele_tag);
    mio.read_physical_groups(phys);

    for (size_t i = 0; i < ele.size(); i++)
    {
        if (cross(*ele[i].vert[1] - *ele[i].vert[0], *ele[i].vert[2] - *ele[i].vert[0]) < 0.0)
        {
            vertex2dc *tmp = ele[i].vert[0]; ele[i].vert[0] = ele[i].vert[1]; ele[i].vert[1] = tmp;
        }
    }

    array<double> rho(ele.size());
    for (size_t i = 0; i < ele.size(); i++)
    {
        if (ele_tag[i][0] == mio.physical_name2tag(phys, rho_name)) rho[i] = ab_rho;
        else rho[i] = 0.0;
    }

    // calculate gravity
    array<point2dp> obsp;
    grid_points_1d(obsp, 100.0, 80.0, -0.1, 6381000.0);

    array<double> gx(obsp.size());
    array<double> gz(obsp.size());
    array<double> gxx(obsp.size());
    array<double> gxz(obsp.size());
    array<double> gzx(obsp.size());
    array<double> gzz(obsp.size());
    gobser(gx, ele, obsp, rho, Vx, FullMsg);
    gobser(gz, ele, obsp, rho, Vz, FullMsg);
    gobser(gxx, ele, obsp, rho, Txx, FullMsg);
    gobser(gxz, ele, obsp, rho, Txz, FullMsg);
    gobser(gzx, ele, obsp, rho, Tzx, FullMsg);
    gobser(gzz, ele, obsp, rho, Tzz, FullMsg);

    array<double> r(obsp.size()), d(obsp.size());
    for (size_t i = 0; i < obsp.size(); i++)
    {
        r[i] = obsp[i].rad;
        d[i] = deg(obsp[i].arc) - 90.0;
    }

    geodsv_io gio;
    gio.init_table(obsp.size(), 8);
    gio.column_names({"rad", "deg", "gx", "gz", "gxx", "gxz", "gzx", "gzz"});
    gio.fill_column(r, "rad");
    gio.fill_column(d, "deg");
    gio.fill_column(gx, "gx");
    gio.fill_column(gz, "gz");
    gio.fill_column(gxx, "gxx");
    gio.fill_column(gxz, "gxz");
    gio.fill_column(gzx, "gzx");
    gio.fill_column(gzz, "gzz");
    gio.save_csv(out_file);

    system("gnuplot data/shell2d/plot.gnu");
    return 0;
}
catch (std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}