/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  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 "variogram.h"
#include "cmath"

bool gctl::variogram_model::valid()
{
    if (nugget <= 0.0 || sill <= 0.0 || range <= 0.0) return false;
    if (sill <= nugget) return false;
    return true;
}

double gctl::spherical_variogram(const double &d, const variogram_model &vm)
{
    if (d > vm.range) return vm.sill;
    return vm.nugget + ((vm.sill-vm.nugget) * ((3.0*d)/(2.0*vm.range) - (d*d*d)/(2.0*vm.range*vm.range*vm.range)));
}

double gctl::exponential_variogram(const double &d, const variogram_model &vm)
{
    return vm.nugget + ((vm.sill-vm.nugget) * (1.0-exp((-d/vm.range))));
}

double gctl::gaussian_variogram(const double &d, const variogram_model &vm)
{
    double e = d/vm.range;
    return vm.nugget + ((vm.sill-vm.nugget) * (1.0-exp(-1.0*e*e)));
}

double gctl::wave_variogram(const double &d, const variogram_model &vm)
{
    if (d == 0.0) return vm.nugget;

    double e = d/vm.range;
    return vm.nugget + ((vm.sill-vm.nugget) * (1.0-sin(e)/e));
}

double gctl::rational_quadratic_variogram(const double &d, const variogram_model &vm)
{
    double e = (d*d)/(vm.range*vm.range);
    return vm.nugget + ((vm.sill-vm.nugget) * (e/(1.0+e)));
}

double gctl::circular_variogram(const double &d, const variogram_model &vm)
{
    if(d > vm.range) return vm.sill;

    double e = d/vm.range;
    double p = 2.0/GCTL_Pi;
    double r = sqrt(1.0-e*e);
    return vm.nugget + ((vm.sill-vm.nugget) * (1-p*acos(e)+p*e*r));
}

double gctl::linear_variogram(const double &d, const variogram_model &vm)
{
    if (d >= vm.range) return vm.sill;
    return vm.nugget + (vm.sill-vm.nugget)*d/vm.range;
}

double gctl::variogram(const double &d, const variogram_model &vm, variogram_type_e type)
{
    if (type == SPHERICAL) return spherical_variogram(d, vm);
    else if (type == EXPONENTIAL) return exponential_variogram(d, vm);
    else if (type == GAUSSIAN) return gaussian_variogram(d, vm);
    else if (type == WAVE) return wave_variogram(d, vm);
    else if (type == RATIONAL_Q) return rational_quadratic_variogram(d, vm);
    else if (type == CIRCULAR) return circular_variogram(d, vm);
    else if (type == LINEAR) return linear_variogram(d, vm);
    return 0.0;
}