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

using namespace gctl;

int main(int argc, char *argv[])
{
    array<vertex2dc> vert;
    array<triangle2d> elem;

    gmshio gio;
    gio.init_file("data/cylinder2d/cylinder2d", Input);
    gio.set_packed(NotPacked, Input);
    gio.read_mesh(elem, vert);

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

    array<double> rho(elem.size(), 1.0);

    array<point2dc> obsp(201);
    obsp.sequence(point2dc(-500.0, 0.0), point2dc(5.0, 0.0));

    array<double> vx(obsp.size()), vz(obsp.size());
    array<double> vxx(obsp.size()), vzz(obsp.size());
    array<double> vxz(obsp.size()), vzx(obsp.size());
    gobser(vx, elem, obsp, rho, gctl::Vx);
    gobser(vz, elem, obsp, rho, gctl::Vz);
    gobser(vxx, elem, obsp, rho, gctl::Txx);
    gobser(vxz, elem, obsp, rho, gctl::Txz);
    gobser(vzx, elem, obsp, rho, gctl::Tzx);
    gobser(vzz, elem, obsp, rho, gctl::Tzz);

    geodsv_io tio;
    tio.init_table(obsp.size(), 8);
    tio.column_names({"X", "Y", "Vx", "Vz", "Vxx", "Vxz", "Vzx", "Vzz"});
    tio.fill_column_point2dc(obsp, "X", "Y");
    tio.fill_column(vx, "Vx", 12);
    tio.fill_column(vz, "Vz", 12);
    tio.fill_column(vxx, "Vxx", 12);
    tio.fill_column(vxz, "Vxz", 12);
    tio.fill_column(vzx, "Vzx", 12);
    tio.fill_column(vzz, "Vzz", 12);
    tio.save_text("data/cylinder2d/gravity_2d");
    return 0;
}