tkfermi_c.py ダウンロード/コピー
tkfermi_c.py
tkfermi_c.py
1"""
2概要:
3 C言語で高速化されたtkfermiモジュールです。
4詳細説明:
5 元の実装と同じく非正規化積分を計算します。キャッシュは行いません。
6 級数展開、漸近展開、および被積分関数のみをC言語で実装しています。
7 SciPyのquad積分は元の区間と許容誤差を維持します。
8"""
9import ctypes
10from pathlib import Path
11import platform
12from scipy import LowLevelCallable, integrate
13
14_filename = {'Windows': 'tkfermi_core.dll', 'Darwin': 'libtkfermi_core.dylib'}.get(
15 platform.system(), 'libtkfermi_core.so')
16_path = Path(__file__).resolve().with_name(_filename)
17if not _path.exists():
18 raise ImportError('Native library missing. Run: python build.py')
19_lib = ctypes.CDLL(str(_path))
20for _name in ('fd_degenerate', 'fd_nondegenerate'):
21 _fn = getattr(_lib, _name)
22 _fn.argtypes = [ctypes.c_double, ctypes.c_double]
23 _fn.restype = ctypes.c_double
24_lib.fd_integrand.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_double)]
25_lib.fd_integrand.restype = ctypes.c_double
26_integrand = LowLevelCallable(_lib.fd_integrand)
27
28
29def FermiIntegral_fast(eta, r, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
30 """
31 概要:
32 高速なフェルミディラック積分の計算を行います。
33 詳細説明:
34 etaの値に応じて、縮退近似、非縮退近似、またはSciPyによる数値積分を選択して計算します。
35 引数:
36 :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
37 :type eta: float
38 :param r: 積分の次数パラメータ。
39 :type r: float
40 :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
41 :type epsabs: float
42 :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
43 :type epsrel: float
44 :param limit: 数値積分の上限回数。デフォルトは50です。
45 :type limit: int
46 戻り値:
47 :returns: フェルミディラック積分の計算結果。
48 :rtype: float
49 """
50 # Keep the original cache-key rounding even though caching is disabled.
51 eta, r = round(float(eta), 10), round(float(r), 10)
52 if eta >= 20.0:
53 return _lib.fd_degenerate(eta, r)
54 if eta < -0.1:
55 return _lib.fd_nondegenerate(eta, r)
56 lim = 17.0 + 4.5 * r + eta
57 options = dict(args=(eta, r), epsabs=epsabs, epsrel=epsrel, limit=limit)
58 ret = integrate.quad(_integrand, 0.0, 1.0, **options)[0]
59 ret += integrate.quad(_integrand, 1.0, 4.0, **options)[0]
60 ret += integrate.quad(_integrand, 4.0, lim, **options)[0]
61 return ret
62
63
64def FermiIntegral_half(eta, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
65 """
66 概要:
67 次数が0.5のフェルミディラック積分を計算します。
68 詳細説明:
69 FermiIntegral_fast関数を呼び出し、rに0.5を指定して計算を行います。
70 引数:
71 :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
72 :type eta: float
73 :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
74 :type epsabs: float
75 :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
76 :type epsrel: float
77 :param limit: 数値積分の上限回数。デフォルトは50です。
78 :type limit: int
79 戻り値:
80 :returns: 次数が0.5のフェルミディラック積分の計算結果。
81 :rtype: float
82 """
83 return FermiIntegral_fast(eta, 0.5, epsabs=epsabs, epsrel=epsrel, limit=limit)
84
85
86def FermiIntegral_3half(eta, *, epsabs=1.0e-10, epsrel=1.0e-8, limit=50):
87 """
88 概要:
89 次数が1.5のフェルミディラック積分を計算します。
90 詳細説明:
91 FermiIntegral_fast関数を呼び出し、rに1.5を指定して計算を行います。
92 引数:
93 :param eta: フェルミ準位や化学ポテンシャルに関連するパラメータ。
94 :type eta: float
95 :param epsabs: 絶対許容誤差。デフォルトは1.0e-10です。
96 :type epsabs: float
97 :param epsrel: 相対許容誤差。デフォルトは1.0e-8です。
98 :type epsrel: float
99 :param limit: 数値積分の上限回数。デフォルトは50です。
100 :type limit: int
101 戻り値:
102 :returns: 次数が1.5のフェルミディラック積分の計算結果。
103 :rtype: float
104 """
105 return FermiIntegral_fast(eta, 1.5, epsabs=epsabs, epsrel=epsrel, limit=limit)