"""
概要:
    C言語で高速化されたtkfermiモジュールです。
詳細説明:
    元の実装と同じく非正規化積分を計算します。キャッシュは行いません。
    級数展開、漸近展開、および被積分関数のみをC言語で実装しています。
    SciPyのquad積分は元の区間と許容誤差を維持します。
"""
import ctypes
from pathlib import Path
import platform
from scipy import LowLevelCallable, integrate

_filename = {'Windows': 'tkfermi_core.dll', 'Darwin': 'libtkfermi_core.dylib'}.get(
    platform.system(), 'libtkfermi_core.so')
_path = Path(__file__).resolve().with_name(_filename)
if not _path.exists():
    raise ImportError('Native library missing. Run: python build.py')
_lib = ctypes.CDLL(str(_path))
for _name in ('fd_degenerate', 'fd_nondegenerate'):
    _fn = getattr(_lib, _name)
    _fn.argtypes = [ctypes.c_double, ctypes.c_double]
    _fn.restype = ctypes.c_double
_lib.fd_integrand.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_double)]
_lib.fd_integrand.restype = ctypes.c_double
_integrand = LowLevelCallable(_lib.fd_integrand)


def FermiIntegral_fast(eta, r, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
    """
    概要:
        高速なフェルミディラック積分の計算を行います。
    詳細説明:
        etaの値に応じて、縮退近似、非縮退近似、またはSciPyによる数値積分を選択して計算します。
    引数:
        :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
        :type eta: float
        :param r: 積分の次数パラメータ。
        :type r: float
        :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
        :type epsabs: float
        :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
        :type epsrel: float
        :param limit: 数値積分の上限回数。デフォルトは50です。
        :type limit: int
    戻り値:
        :returns: フェルミディラック積分の計算結果。
        :rtype: float
    """
    # Keep the original cache-key rounding even though caching is disabled.
    eta, r = round(float(eta), 10), round(float(r), 10)
    if eta >= 20.0:
        return _lib.fd_degenerate(eta, r)
    if eta < -0.1:
        return _lib.fd_nondegenerate(eta, r)
    lim = 17.0 + 4.5 * r + eta
    options = dict(args=(eta, r), epsabs=epsabs, epsrel=epsrel, limit=limit)
    ret = integrate.quad(_integrand, 0.0, 1.0, **options)[0]
    ret += integrate.quad(_integrand, 1.0, 4.0, **options)[0]
    ret += integrate.quad(_integrand, 4.0, lim, **options)[0]
    return ret


def FermiIntegral_half(eta, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
    """
    概要:
        次数が0.5のフェルミディラック積分を計算します。
    詳細説明:
        FermiIntegral_fast関数を呼び出し、rに0.5を指定して計算を行います。
    引数:
        :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
        :type eta: float
        :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
        :type epsabs: float
        :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
        :type epsrel: float
        :param limit: 数値積分の上限回数。デフォルトは50です。
        :type limit: int
    戻り値:
        :returns: 次数が0.5のフェルミディラック積分の計算結果。
        :rtype: float
    """
    return FermiIntegral_fast(eta, 0.5, epsabs=epsabs, epsrel=epsrel, limit=limit)


def FermiIntegral_3half(eta, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
    """
    概要:
        次数が1.5のフェルミディラック積分を計算します。
    詳細説明:
        FermiIntegral_fast関数を呼び出し、rに1.5を指定して計算を行います。
    引数:
        :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
        :type eta: float
        :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
        :type epsabs: float
        :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
        :type epsrel: float
        :param limit: 数値積分の上限回数。デフォルトは50です。
        :type limit: int
    戻り値:
        :returns: 次数が1.5のフェルミディラック積分の計算結果。
        :rtype: float
    """
    return FermiIntegral_fast(eta, 1.5, epsabs=epsabs, epsrel=epsrel, limit=limit)