EF-T-metal.py ダウンロード/コピー

EF-T-metal.py をダウンロード

EF-T-metal.py
EF-T-metal.py
  1import sys
  2from math import exp, sqrt
  3import numpy as np
  4from scipy import integrate         # 数値積分関数 integrateを読み込む
  5from scipy import optimize          # newton関数はscipy.optimizeモジュールに入っている
  6from matplotlib import pyplot as plt
  7
  8
  9"""
 10概要:
 11    金属のEFの温度依存性を数値積分とNewton法で計算します。
 12詳細説明:
 13    金属のフェルミ準位の温度依存性を数値積分とNewton法を用いて計算し、近似式と比較します。
 14    Calculate EF(T) for metal using numerical integration and the Newton method,
 15    and compare with the approximation formula.
 16"""
 17
 18
 19# constants
 20pi   = 3.14159265358979323846
 21h    = 6.6260755e-34    # Js";
 22hbar = 1.05459e-34      # "Js";
 23c    = 2.99792458e8     # m/s";
 24e    = 1.60218e-19      # C";
 25kB   = 1.380658e-23     # JK<sup>-1</sup>";
 26me   = 9.1093897e-31    # kg";
 27
 28# spin, effective mass, electron density
 29S = 1.0 / 2.0
 30m = 1.0
 31N = 5.0e22   # cm^-3
 32
 33# Temperature range
 34Tmin  =    0  # K
 35Tmax  = 4000
 36Tstep =  200
 37nT = int((Tmax - Tmin) / Tstep + 1)
 38
 39# integration will be performed in the range
 40# from EF - nrange * kBT to EF + nrange * kBT 
 41nrange = 6.0
 42
 43# Relative accuracy of the quad functin
 44eps = 1.0e-8
 45
 46# Treat argments
 47argv = sys.argv
 48if len(argv) >= 2:
 49    N = float(argv[1])
 50if len(argv) >= 3:
 51    m = float(argv[2])
 52
 53D0 = 0.0
 54
 55
 56# Density-Of-States function
 57def De(E):
 58    """
 59    概要:
 60        状態密度関数を計算します。
 61    引数:
 62        :param E: エネルギー (eV)
 63        :type E: float
 64    戻り値:
 65        :returns: 状態密度
 66        :rtype: float
 67    """
 68    if E < 0.0:
 69        return 0.0 
 70    return D0 * sqrt(E)
 71
 72# Fermi-Dirac distribution function
 73def fe(E, T, EF):
 74    """
 75    概要:
 76        フェルミ・ディラック分布関数を計算します。
 77    引数:
 78        :param E: エネルギー (eV)
 79        :type E: float
 80        :param T: 温度 (K)
 81        :type T: float
 82        :param EF: フェルミ準位 (eV)
 83        :type EF: float
 84    戻り値:
 85        :returns: 分布関数の値
 86        :rtype: float
 87    """
 88    if T == 0.0:
 89        if E < EF:
 90            return 1.0
 91        else:
 92            return 0.0
 93    return 1.0 / (exp((E - EF) * e / kB / T) + 1.0)
 94
 95# Define the function to be integrated
 96def Defe(E, T, EF):
 97    """
 98    概要:
 99        積分対象の関数である状態密度とフェルミ・ディラック分布関数の積を計算します。
100    引数:
101        :param E: エネルギー (eV)
102        :type E: float
103        :param T: 温度 (K)
104        :type T: float
105        :param EF: フェルミ準位 (eV)
106        :type EF: float
107    戻り値:
108        :returns: 状態密度と分布関数の積
109        :rtype: float
110    """
111    return De(E) * fe(E, T, EF)
112
113# Calculate electron density with a given EF
114# For E < EF - dE, use the analytical integration
115# For E > EF - dE, use the numerical integration with quad
116# The upper limit of integration is determined as EF + dE
117def Ne(T, EF, dE):
118    """
119    概要:
120        指定されたフェルミ準位での電子密度を計算します。
121    詳細説明:
122        E < EF - dE の範囲では解析的に積分を行い、E > EF - dE の範囲では数値積分を行います。積分の領域の上限は EF + dE となります。
123    引数:
124        :param T: 温度 (K)
125        :type T: float
126        :param EF: フェルミ準位 (eV)
127        :type EF: float
128        :param dE: 積分領域を決定するためのエネルギー幅 (eV)
129        :type dE: float
130    戻り値:
131        :returns: 計算された電子密度
132        :rtype: float
133    """
134    Emax = EF - dE
135    if Emax > 0.0:
136        Ne1 = 2.0 / 3.0 * D0 * pow(Emax, 1.5)
137    else:
138        Ne1 = 0.0
139    (Emin, Emax) = (EF - dE, EF + dE)
140    if Emin < 0.0:
141        Emin = 0.0
142    ret = integrate.quad(lambda E: Defe(E, T, EF), Emin, Emax, epsrel = eps)
143    return Ne1 + ret[0]
144
145# Calculate EF base on 
146# the charge neutrality (electron number) condition, Ne(T, EF, dE) - N = 0,
147# using the Newton method with the initial value EF0
148def CalEF(N, T, EF0, dE):
149    """
150    概要:
151        電荷中性条件に基づいてフェルミ準位を計算します。
152    詳細説明:
153        電子数の条件 Ne(T, EF, dE) - N = 0 を満たすフェルミ準位を、初期値 EF0 を用いたNewton法で計算します。
154    引数:
155        :param N: 電子密度 (cm-3)
156        :type N: float
157        :param T: 温度 (K)
158        :type T: float
159        :param EF0: フェルミ準位の初期値 (eV)
160        :type EF0: float
161        :param dE: 積分領域のエネルギー幅 (eV)
162        :type dE: float
163    戻り値:
164        :returns: 計算されたフェルミ準位 (eV)
165        :rtype: float
166    """
167    global D0
168
169    if T == 0.0:
170        return pow(1.5 / D0 * N, 2.0 / 3.0)  # eV
171
172    EF = optimize.newton(lambda EF: Ne(T, EF, dE) - N, EF0)
173    return EF
174    
175
176def main():
177    """
178    概要:
179        金属のフェルミ準位の温度依存性を計算しグラフを描画します。
180    詳細説明:
181        設定された温度範囲においてフェルミ準位の厳密解と近似解を計算し標準出力に表示したあとグラフとしてプロットします。
182    引数:
183        なし
184    戻り値:
185        なし
186    """
187    global m, D0
188    
189    print("m = %g me" % (m))
190    m *= me
191# Calculate the prefactor of De(E)
192    D0 = (2.0 * S + 1.0) * 2.0 * pi * pow(2.0*m, 1.5) / h**3  # m^-3 J^-1.5
193    D0 *= 1.0e-6 * e**1.5   # states/(cm^3 eV^1.5)
194    print("")
195
196# EF at 0 K, to be used for the initial value of the Newton method
197    EF0 = pow(1.5 / D0 * N, 2.0 / 3.0)  # eV
198    N0 = Ne(0.0, EF0, 0.0)
199    print("EF at 0K = ", EF0, " eV")
200    print("Ne at 0K = ", N0, " cm-3")
201    print("")
202
203    xT = []
204    yEF = []
205    yEFa = []
206    EFprev = EF0
207    print(" T(K)   \t  EF(eV)  \tEFapprox(eV)\tNcheck(cm-3)")
208    for i in range(nT):
209        T = Tmin + i * Tstep
210
211        kBTe = kB * T / e
212# Calculate the range of numerical integration
213        dE = nrange * kBTe
214        EF = CalEF(N, T, EFprev, dE)
215        Ncheck = Ne(T, EF, dE)
216        Ndiff = 1.0 / 2.0 * D0 * pow(EF0, -1.0 / 2.0)
217        EFapprox = EF0 - pi * pi / 6.0 * pow(kBTe, 2.0) * Ndiff / De(EF0)
218        xT.append(T)
219        yEF.append(EF)
220        yEFa.append(EFapprox)
221        print("%8.4f\t%10.6f\t%10.6f\t%16.6g" % (T, EF, EFapprox, Ncheck))
222        EFprev = EF
223
224
225#=============================
226# Plot graphs
227#=============================
228    fig = plt.figure()
229
230    ax1 = fig.add_subplot(1, 1, 1)
231    ax1.plot(xT, yEF, label = 'EF(exact)')
232    ax1.plot(xT, yEFa, label = 'EF(approx)', color = 'red')
233    ax1.set_xlabel("T (K)")
234    ax1.set_ylabel("EF (eV)")
235    ax1.legend()
236
237    plt.pause(0.1)
238    
239
240    print("Press ENTER to exit>>", end = '')
241    input()
242
243
244
245if __name__ == '__main__':
246    main()