kronig_penney.py ダウンロード/コピー
kronig_penney.py
kronig_penney.py
1import sys
2import numpy as np
3from numpy import sqrt, exp, sin, cos, tan, cosh, sinh
4import numpy.linalg as LA
5from pprint import pprint
6import csv
7from matplotlib import pyplot as plt
8
9"""
10概要:
11 1D band calculation by Kronig-Penney model
12詳細説明:
13 このモジュールは1次元のKronig-Penneyモデルを用いて、ポテンシャル障壁中の電子の
14 エネルギーバンドや波動関数を計算し、matplotlibによるグラフ描画を行います。
15 graph、band、wfの3つのモードがあり、それぞれ方程式の誤差グラフ、バンド構造、
16 波動関数を描画します。
17"""
18
19#===================================
20# physical constants
21#===================================
22pi = 3.14159265358979323846
23pi2 = 2.0 * pi
24h = 6.6260755e-34 # Js";
25hbar = 1.05459e-34 # "Js";
26c = 2.99792458e8 # m/s";
27e = 1.60218e-19 # C";
28e0 = 8.854418782e-12; # C<sup>2</sup>N<sup>-1</sup>m<sup>-2</sup>";
29kB = 1.380658e-23 # JK<sup>-1</sup>";
30me = 9.1093897e-31 # kg";
31R = 8.314462618 # J/K/mol
32a0 = 5.29177e-11 # m";
33
34
35#========================
36# global configuration
37#========================
38mode = 'graph' # graph|band|wf
39
40#========================
41# Crystal definition
42#========================
43# Si
44a = 5.4064 # angstrom, lattice parameter
45
46#========================
47# Potential
48#========================
49bwidth = 0.5 # A, barrier width
50bpot = 10.0 # eV, barrier height
51
52#=====================================
53# 解を走査するグラフ表示
54#=====================================
55kg = 0.0 # k point to be plotted
56# 解を走査するエネルギー範囲
57Emin = 0.0
58Emax = 9.5
59# グラフを表示するエネルギー点数
60nE = 51
61# 解を走査するエネルギー点数
62nEsearch = nE
63# Secant法パラメータ
64eps = 1.0e-8
65nmaxiter = 100
66dump = 0.0
67
68#========================
69# Band
70#========================
71kmin = -0.5 # in pi/a
72kmax = 0.5 # in pi/a
73nk = 21
74
75# プロットするエネルギー範囲
76Erange = [0.0, 10.0] # eV
77
78# リストに保存する準位最大数
79nMaxLevel = 15
80
81#========================
82# Wave function
83#========================
84#波動関数を描画するx範囲
85xwmin = 0.0 # A
86xwmax = 3.0 * a # A
87nxw = 101
88#描画する波動関数の波数
89kw = 0.0
90#描画する波動関数の準位番号
91iLevel = 0
92
93#===================================
94# figure configuration
95#===================================
96figsize = (6, 8)
97fontsize = 12
98legend_fontsize = 8
99
100
101#==============================================
102# fundamental functions
103#==============================================
104# 実数値に変換できない文字列をfloat()で変換するとエラーになってプログラムが終了する
105# この関数は、変換できなかったらNoneを返すが、プログラムは終了させない
106def pfloat(str):
107 """
108 概要:
109 文字列をfloat型に変換します。
110 詳細説明:
111 変換できない場合はNoneを返し、プログラムを異常終了させません。
112 引数:
113 :param str: 変換対象の文字列。
114 :type str: str
115 戻り値:
116 :returns: 変換後の浮動小数点数、またはNone。
117 :rtype: float または None
118 """
119 try:
120 return float(str)
121 except:
122 return None
123
124# pfloat()のint版
125def pint(str):
126 """
127 概要:
128 文字列をint型に変換します。
129 詳細説明:
130 変換できない場合はNoneを返し、プログラムを異常終了させません。
131 引数:
132 :param str: 変換対象の文字列。
133 :type str: str
134 戻り値:
135 :returns: 変換後の整数、またはNone。
136 :rtype: int または None
137 """
138 try:
139 return int(str)
140 except:
141 return None
142
143# 起動時引数を取得するsys.argリスト変数は、範囲外のindexを渡すとエラーになってプログラムが終了する
144# egtarg()では、範囲外のindexを渡したときは、defvalを返す
145def getarg(position, defval = None):
146 """
147 概要:
148 コマンドライン引数から指定位置の文字列を取得します。
149 詳細説明:
150 sys.argvの範囲外のインデックスを指定した場合は、デフォルト値を返します。
151 引数:
152 :param position: 取得する引数のインデックス。
153 :type position: int
154 :param defval: 範囲外の場合に返すデフォルト値。
155 :type defval: any
156 戻り値:
157 :returns: 取得した文字列、またはデフォルト値。
158 :rtype: str または any
159 """
160 try:
161 return sys.argv[position]
162 except:
163 return defval
164
165# 起動時引数を実数に変換して返す
166def getfloatarg(position, defval = None):
167 """
168 概要:
169 コマンドライン引数から指定位置の値を浮動小数点数として取得します。
170 引数:
171 :param position: 取得する引数のインデックス。
172 :type position: int
173 :param defval: 範囲外や変換失敗時のデフォルト値。
174 :type defval: float または None
175 戻り値:
176 :returns: 取得した浮動小数点数、またはデフォルト値。
177 :rtype: float または None
178 """
179 return pfloat(getarg(position, defval))
180
181# 起動時引数を整数値に変換して返す
182def getintarg(position, defval = None):
183 """
184 概要:
185 コマンドライン引数から指定位置の値を整数として取得します。
186 引数:
187 :param position: 取得する引数のインデックス。
188 :type position: int
189 :param defval: 範囲外や変換失敗時のデフォルト値。
190 :type defval: int または None
191 戻り値:
192 :returns: 取得した整数、またはデフォルト値。
193 :rtype: int または None
194 """
195 return pint(getarg(position, defval))
196
197# x を aで割った余り x0 と整数 n
198def round01(x, a):
199 """
200 概要:
201 値xをaで割った余りとその商の整数を返します。
202 引数:
203 :param x: 対象の数値。
204 :type x: float
205 :param a: 周期または除数。
206 :type a: float
207 戻り値:
208 :returns: 余りx0と商の整数nのタプル。
209 :rtype: tuple
210 """
211 if x >= 0.0:
212 n = int(x / a)
213 else:
214 n = int(x / a) - 1
215 x0 = x - n * a
216 return x0, n
217
218def usage():
219 """
220 概要:
221 プログラムのコマンドライン引数の使い方を標準出力に表示します。
222 """
223 print("")
224 print("Usage: Variables in () are optional")
225 print(" python {}".format(sys.argv[0]))
226 print(" python {} (graph a bwidth bpot k Emin Emax nE)".format(sys.argv[0]))
227 print(" python {} (band a bwidth bpot nG kmin kmax nk)".format(sys.argv[0]))
228 print(" python {} (wf a bwidth bpot kw iLevel xwmin xwmax nxw)".format(sys.argv[0]))
229 print(" ex: python {} {} {} {} {} {} {} {} {}"
230 .format(sys.argv[0], 'graph', a, bwidth, bpot, kg, Emin, Emax, nE))
231 print(" ex: python {} {} {} {} {} {} {} {}"
232 .format(sys.argv[0], 'band', a, bwidth, bpot, kmin, kmax, nk))
233 print(" ex: python {} {} {} {} {} {} {} {} {} {}"
234 .format(sys.argv[0], 'wf', a, bwidth, bpot, kw, iLevel, xwmin, xwmax, nxw))
235
236def terminate(message = None):
237 """
238 概要:
239 メッセージと使い方を表示してプログラムを終了します。
240 引数:
241 :param message: 終了前に表示するメッセージ。
242 :type message: str または None
243 """
244 print("")
245 if message is not None:
246 print("")
247 print(message)
248 print("")
249
250 usage()
251 print("")
252 exit()
253
254#==============================================
255# update default values by startup arguments
256#==============================================
257argv = sys.argv
258#if len(argv) == 1:
259# terminate()
260
261mode = getarg (1, mode)
262a = getfloatarg(2, a)
263bwidth = getfloatarg(3, bwidth)
264bpot = getfloatarg(4, bpot)
265
266if mode == 'graph':
267 kg = getfloatarg(5, kg)
268 Emin = getfloatarg(6, Emin)
269 Emax = getfloatarg(7, Emax)
270 nE = getintarg (8, nE)
271elif mode == 'band':
272 kmin = getfloatarg( 5, kmin)
273 kmax = getfloatarg( 6, kmax)
274 nk = getintarg ( 7, nk)
275elif mode == 'wf':
276 kw = getfloatarg( 5, kw)
277 iLevel = getintarg ( 6, iLevel)
278 xwmin = getfloatarg( 7, xwmin)
279 xwmax = getfloatarg( 8, xwmax)
280 nxw = getintarg (9, nxw)
281
282
283# rectangular barrier potential
284def pot(x):
285 """
286 概要:
287 位置xにおける矩形障壁ポテンシャルの高さを返します。
288 詳細説明:
289 グローバル変数のa、bwidth、bpotを使用します。
290 引数:
291 :param x: 計算する位置。
292 :type x: float
293 戻り値:
294 :returns: ポテンシャルのエネルギー値。
295 :rtype: float
296 """
297 global a
298 global bwidth, bpot
299
300 xred, nred = round01(x, a)
301
302 if a - bwidth <= xred < a:
303 return bpot
304 return 0.0
305
306# ポテンシャルV(x)のリストを返す
307def build_potential(xmin, xstep, n):
308 """
309 概要:
310 指定された範囲とステップ数でポテンシャル関数の配列を生成します。
311 引数:
312 :param xmin: 開始位置。
313 :type xmin: float
314 :param xstep: 位置のステップ刻み幅。
315 :type xstep: float
316 :param n: 生成する点数。
317 :type n: int
318 戻り値:
319 :returns: x座標の配列とポテンシャル値の配列のタプル。
320 :rtype: tuple
321 """
322 xpot = np.empty(n)
323 ypot = np.empty(n)
324 for i in range(n):
325 xx = xmin + i * xstep
326 xpot[i] = xx
327 ypot[i] = pot(xx)
328 return xpot, ypot
329
330# Kronig-Penneyモデルの方程式の誤差
331def cal_delta(E, k, w, b, V0):
332 """
333 概要:
334 Kronig-Penneyモデルの境界条件方程式の誤差を計算します。
335 詳細説明:
336 与えられたエネルギーEにおける方程式の左辺と右辺の差分を返します。
337 引数:
338 :param E: 電子のエネルギー。
339 :type E: float
340 :param k: 波数。
341 :type k: float
342 :param w: 井戸の幅。
343 :type w: float
344 :param b: 障壁の幅。
345 :type b: float
346 :param V0: 障壁の高さ。
347 :type V0: float
348 戻り値:
349 :returns: 方程式の誤差。
350 :rtype: float
351 """
352 alpha = sqrt(2.0 * me * E * e) / hbar
353 beta = sqrt(2.0 * me * (V0 - E) * e) / hbar
354 ka = k * pi2
355 alphaw = alpha * w * 1.0e-10
356 betab = beta * b * 1.0e-10
357 delta = (beta*beta - alpha*alpha)/2.0/alpha/beta * sin(alphaw) * sinh(betab) \
358 + cos(alphaw) * cosh(betab) \
359 - cos(ka)
360# print("a=", E, ka, alphaw, betab, delta)
361 return delta
362
363# ciがKronig-Penneyモデルの方程式を満たすかどうかを確認
364# デバッグ用
365def check_ci(ci, kw, Ei, w, b, V0, eps, IsPrint = 0):
366 """
367 概要:
368 係数行列ciが境界条件を満たしているか確認します。
369 詳細説明:
370 主としてデバッグ用の関数です。条件を満たさない場合はプログラムを終了させます。
371 引数:
372 :param ci: 波動関数の係数のリスト。
373 :type ci: list
374 :param kw: 波数。
375 :type kw: float
376 :param Ei: エネルギー準位。
377 :type Ei: float
378 :param w: 井戸の幅。
379 :type w: float
380 :param b: 障壁の幅。
381 :type b: float
382 :param V0: 障壁の高さ。
383 :type V0: float
384 :param eps: 許容する誤差の閾値。
385 :type eps: float
386 :param IsPrint: デバッグ情報を出力するかどうかのフラグ。
387 :type IsPrint: int
388 """
389 alpha = sqrt(2.0 * me * Ei * e) / hbar
390 beta = sqrt(2.0 * me * (V0 - Ei) * e) / hbar
391 ka = kw * pi2
392 lambda_ = exp(1.0j * ka)
393 alphaw = alpha * w * 1.0e-10
394 betab = beta * b * 1.0e-10
395 alpha *= 1.0e-10
396 beta *= 1.0e-10
397
398 Passed = 1
399 vmax = 0.0
400 if 1:
401 Mij = np.empty([4, 4], dtype = complex)
402 M3ij = np.empty([3, 3], dtype = complex)
403 V3i = np.empty([3, 1], dtype = complex)
404 Mij[0, 0] = Mij[0, 1] = 1.0
405 Mij[0, 2] = Mij[0, 3] = -1.0
406 Mij[1, 0] = 1.0j * alpha
407 Mij[1, 1] = -1.0j * alpha
408 Mij[1, 2] = -beta
409 Mij[1, 3] = beta
410 Mij[2, 0] = exp( 1.0j * alphaw)
411 Mij[2, 1] = exp(-1.0j * alphaw)
412 Mij[2, 2] = -lambda_ * exp(-betab)
413 Mij[2, 3] = -lambda_ * exp( betab)
414 Mij[3, 0] = 1.0j * alpha * exp( 1.0j * alphaw)
415 Mij[3, 1] = -1.0j * alpha * exp(-1.0j * alphaw)
416 Mij[3, 2] = -lambda_ * beta * exp(-betab)
417 Mij[3, 3] = lambda_ * beta * exp( betab)
418 if IsPrint:
419 for i in range(4):
420 print(" ci[{}] = {:12.4g}+j{:12.4g}".format(i, ci[i].real, ci[i].imag))
421 for i in range(4):
422 v = Mij[i, 0] * ci[0] + Mij[i, 1] * ci[1] + Mij[i, 2] * ci[2] + Mij[i, 3] * ci[3]
423 v = abs(v)
424 if IsPrint:
425 print(" abs(Mij@ci[{}]) = {}".format(i, v), eps)
426 if v > eps:
427 Passed = 0
428 if v > vmax:
429 vmax = v
430
431 if not Passed:
432 print("Error: Mij @ ci is not zero: abs(Mij@ci)={} > eps={}".format(vmax, eps))
433 exit()
434
435
436def refine_E(E0, E1, nmaxiter, eps, dump, k, w, b, V0, IsPrint = 0):
437 """
438 概要:
439 割線法を用いてエネルギーEの真の解を探索し高精度化します。
440 詳細説明:
441 Kronig-Penneyモデルの方程式の誤差が0になるようなエネルギーEを計算します。
442 引数:
443 :param E0: 前のステップのエネルギー推定値。
444 :type E0: float
445 :param E1: 現在のステップのエネルギー推定値。
446 :type E1: float
447 :param nmaxiter: 最大反復回数。
448 :type nmaxiter: int
449 :param eps: 収束判定の閾値。
450 :type eps: float
451 :param dump: 更新時のダンピング係数。
452 :type dump: float
453 :param k: 波数。
454 :type k: float
455 :param w: 井戸の幅。
456 :type w: float
457 :param b: 障壁の幅。
458 :type b: float
459 :param V0: 障壁の高さ。
460 :type V0: float
461 :param IsPrint: デバッグ情報を出力するかどうかのフラグ。
462 :type IsPrint: int
463 戻り値:
464 :returns: 収束したエネルギーE、更新量dE、およびその時の誤差deltaのタプル。
465 :rtype: tuple
466 """
467 delta0 = cal_delta(E0, k, w, b, V0)
468 delta1 = cal_delta(E1, k, w, b, V0)
469 for i in range(nmaxiter):
470 diff = (delta1 - delta0) / (E1 - E0)
471 if diff >= 0.0:
472 diff += dump
473 else:
474 diff = -(abs(diff) + dump)
475
476 dE = -delta1 / diff
477 E2 = E1 + dE
478 delta2 = cal_delta(E2, k, w, b, V0)
479
480 if abs(dE) < eps:
481 if IsPrint:
482 print(" converged at E = {:12.6g} with dE = {:12.6g} delta = {:12.6g}"
483 .format(E2, dE, delta2))
484 return E2, dE, delta2
485 else:
486 E0 = E1
487 E1 = E2
488 delta0 = delta1
489 delta1 = delta2
490 continue
491 else:
492 print(" Not converged for {} iterations.".format(nmaxiter))
493 print(" E = {:12.6g} with dE = {:12.6g} delta = {:12.6g}".format(E2, dE, delta2))
494 return None, None, None
495
496# delta(E)を走査し、delta(E)=0を満たすEのリストを返す
497def find_Elist(Emin, Emax, nEsearch, k, w, b, V0):
498 """
499 概要:
500 指定エネルギー範囲で誤差が0になるエネルギーEのリストとその係数を探索します。
501 詳細説明:
502 方程式の誤差の符号が変わる区間を見つけ、refine_Eで解を精度良く求めます。
503 引数:
504 :param Emin: 探索するエネルギーの下限。
505 :type Emin: float
506 :param Emax: 探索するエネルギーの上限。
507 :type Emax: float
508 :param nEsearch: エネルギー範囲の探索分割数。
509 :type nEsearch: int
510 :param k: 波数。
511 :type k: float
512 :param w: 井戸の幅。
513 :type w: float
514 :param b: 障壁の幅。
515 :type b: float
516 :param V0: 障壁の高さ。
517 :type V0: float
518 戻り値:
519 :returns: エネルギー準位のリストと対応する係数行列のリストのタプル。
520 :rtype: tuple
521 """
522# nEsearch *= 100
523 Estep = (Emax - Emin) / (nEsearch - 1)
524# print("Estep=", Estep)
525 d0 = None
526 iband = 0
527 Elist = []
528 Alist = []
529 for iE in range(nEsearch):
530 E = Emin + iE * Estep
531 if E == 0.0:
532 continue
533 if V0 <= E:
534 break
535
536 delta = cal_delta(E, k, w, b, V0)
537
538 if d0 is None:
539 d0 = delta
540 continue
541 if d0 * delta < 0.0:
542 d0 = delta
543
544# print(" E[{}]={:12.6g} eV delta={:8.4g}".format(iband, E, delta))
545 E, dE, delta0 = refine_E(E - Estep, E, nmaxiter, eps, dump, k, w, b, V0, IsPrint = 0)
546 print(" E[{}]={:12.6g} eV dE={:12.6g} delta={:12.6g}".format(iband, E, dE, delta0))
547
548 Elist.append(E)
549# Elist.append(E - 0.5 * Estep)
550
551 alpha = sqrt(2.0 * me * E * e) / hbar
552 beta = sqrt(2.0 * me * (V0 - E) * e) / hbar
553 ka = k * pi2
554 lambda_ = exp(1.0j * ka)
555 alphaw = alpha * w * 1.0e-10
556 betab = beta * b * 1.0e-10
557 alpha *= 1.0e-10
558 beta *= 1.0e-10
559
560 Mij = np.empty([4, 4], dtype = complex)
561 M3ij = np.empty([3, 3], dtype = complex)
562 V3i = np.empty([3, 1], dtype = complex)
563 Mij[0, 0] = Mij[0, 1] = 1.0
564 Mij[0, 2] = Mij[0, 3] = -1.0
565 Mij[1, 0] = 1.0j * alpha
566 Mij[1, 1] = -1.0j * alpha
567 Mij[1, 2] = -beta
568 Mij[1, 3] = beta
569 Mij[2, 0] = exp( 1.0j * alphaw)
570 Mij[2, 1] = exp(-1.0j * alphaw)
571 Mij[2, 2] = -lambda_ * exp(-betab)
572 Mij[2, 3] = -lambda_ * exp( betab)
573 Mij[3, 0] = 1.0j * alpha * exp( 1.0j * alphaw)
574 Mij[3, 1] = -1.0j * alpha * exp(-1.0j * alphaw)
575 Mij[3, 2] = -lambda_ * beta * exp(-betab)
576 Mij[3, 3] = lambda_ * beta * exp( betab)
577
578 A = 1.0
579 M3ij[0, 0] = Mij[1, 1]
580 M3ij[0, 1] = Mij[1, 2]
581 M3ij[0, 2] = Mij[1, 3]
582 M3ij[1, 0] = Mij[2, 1]
583 M3ij[1, 1] = Mij[2, 2]
584 M3ij[1, 2] = Mij[2, 3]
585 M3ij[2, 0] = Mij[3, 1]
586 M3ij[2, 1] = Mij[3, 2]
587 M3ij[2, 2] = Mij[3, 3]
588 V3i[0, 0] = -A * Mij[1, 0]
589 V3i[1, 0] = -A * Mij[2, 0]
590 V3i[2, 0] = -A * Mij[3, 0]
591
592 Ai = LA.solve(M3ij, V3i)
593
594 ci = [A, Ai[0, 0], Ai[1, 0], Ai[2, 0]]
595# check_ci(ci, k, E, w, b, V0, 3.0e-3, IsPrint = 0)
596 Alist.append(ci)
597
598 E += Estep
599
600 return Elist, Alist
601
602# ciから、Ei(k)の波動関数を計算する
603def cal_wavefunction(ci, x, kw, Ei, w, b, V0):
604 """
605 概要:
606 与えられた係数とパラメータを用いて位置xの波動関数の値を計算します。
607 引数:
608 :param ci: 波動関数の係数のリスト。
609 :type ci: list
610 :param x: 計算する位置。
611 :type x: float
612 :param kw: 波数。
613 :type kw: float
614 :param Ei: エネルギー準位。
615 :type Ei: float
616 :param w: 井戸の幅。
617 :type w: float
618 :param b: 障壁の幅。
619 :type b: float
620 :param V0: 障壁の高さ。
621 :type V0: float
622 戻り値:
623 :returns: 複素数としての波動関数の値。
624 :rtype: complex
625 """
626 IsPrint = 1
627
628 a = w + b
629
630 xmin = -b
631 xmax = w
632 x0, n = round01(x, a)
633 if x0 < -xmin:
634 x0 += a
635 if x0 >= xmax:
636 x0 -= a
637 if not xmin <= x0 < xmax:
638 print("Error: x0 out of range: x={:8.4g} {} x0={:8.4g} w={:8.4g} b={:8.4g}".format(x, n, x0, w, b))
639 exit()
640
641# if IsPrint:
642# print("x={:8.4g} {} x0={:8.4g} w={:8.4g} b={:8.4g}".format(x, n, x0, w, b))
643
644# check_ci(ci, kw, Ei, w, b, V0, 3.0e-3)
645
646 alpha = sqrt(2.0 * me * Ei * e) / hbar
647 beta = sqrt(2.0 * me * (V0 - Ei) * e) / hbar
648 alpha *= 1.0e-10
649 beta *= 1.0e-10
650 phase0 = pi2 / a * kw * x0
651 kph0 = exp(1.0j * phase0)
652
653# Calculate the periodic function u(x) from phi(x) in -b <= x < w
654 if xmin <= x0 < 0.0: # in barrier, defined in -b <= x < 0, w <= x < a
655 f = ci[2] * exp(beta * x0) + ci[3] * exp(-beta * x0)
656 u = f / kph0
657 else: # in well, defined in 0 <= x < w
658 f = ci[0] * exp(1.0j * alpha * x0) + ci[1] * exp(-1.0j * alpha * x0)
659 u = f / kph0
660
661# Calculate Bloch function phi(x) = exp(ikx) * u(x)
662 f = exp(1.0j * pi2 / a * kw * x) * u
663
664 return f + 0.0j
665# デバッグ用: 周期関数部分 u(x) を返す
666# return u + 0.0j
667
668
669def wf():
670 """
671 概要:
672 波動関数モードのメイン処理を行い、結果をグラフに描画します。
673 詳細説明:
674 特定の波数とエネルギー準位に対応する波動関数を計算し、ポテンシャルとともにプロットします。
675 """
676 global mode
677 global a
678 global bwidth, bpot
679 global nEsearch, nMaxLevel
680 global kw, iLevel
681 global xwmin, xwmax, nxw
682
683 xwstep = (xwmax - xwmin) / (nxw - 1)
684 Estep = bpot / (nEsearch - 1)
685
686 print("")
687 print("=== Input parameterss ===")
688 print("mode:", mode)
689 print("a=", a, "A")
690 print("Wave function to be plotted: k = {} iLevel = {}".format(kw, iLevel))
691 print("x range: {} - {} at {} step, {} points".format(xwmin, xwmax, xwstep, nxw))
692 print("potential: w={} A h={} eV".format(bwidth, bpot))
693
694 print("")
695 V0 = bpot
696 b = bwidth
697 w = a - b
698
699 print("")
700 print("at k={:8.4g}".format(kw))
701
702 Elist, Alist = find_Elist(0.0, V0, nEsearch, kw, w, b, V0)
703
704 xplot, yplot = build_potential(xwmin, xwstep, nxw)
705
706 print("")
707 print("=== Calculate wave function ===")
708 print("Energy levels:", Elist, "eV")
709 print("at k = {}".format(kw))
710 print("{}-th energy level".format(iLevel))
711 Ei = Elist[iLevel]
712 ci = Alist[iLevel]
713
714 print(" E = {:12.6g} eV".format(Elist[iLevel]))
715 print(" A = {:12.4g}+j{:12.4g}".format(ci[0].real, ci[0].imag))
716 print(" B = {:12.4g}+j{:12.4g}".format(ci[1].real, ci[1].imag))
717 print(" C = {:12.4g}+j{:12.4g}".format(ci[2].real, ci[2].imag))
718 print(" D = {:12.4g}+j{:12.4g}".format(ci[3].real, ci[3].imag))
719 sumci = abs(ci[0] + ci[1] - ci[2] - ci[3])
720 print(" sum(ci) = {:12.4e}".format(sumci))
721 alpha = sqrt(2.0 * me * Ei * e) / hbar * 1.0e-10
722 beta = sqrt(2.0 * me * (V0 - Ei) * e) / hbar * 1.0e-10
723 print(" alpha = {:12.6g} A^-1".format(alpha))
724 print(" beta = {:12.6g} A^-1".format(beta))
725
726 print("")
727 print("Normalization")
728 nxintg = int(a / xwstep + 1.0001)
729 xintgstep = a / (nxintg - 1)
730 chg = 0.0
731 for i in range(nxintg):
732 x = 0.0 + i * xintgstep
733 yval = cal_wavefunction(ci, x, kw, Ei, w, b, V0)
734 chg += yval * yval.conjugate()
735
736 chg = chg.real * xintgstep
737 kywf = 1.0 / sqrt(chg)
738 print("integ(|psi(x)|^2) = ", chg)
739 print("Normalization coefficient = ", kywf)
740 for i in range(4):
741 ci[i] *= kywf
742 print(" A = {:12.4g}+j{:12.4g}".format(ci[0].real, ci[0].imag))
743 print(" B = {:12.4g}+j{:12.4g}".format(ci[1].real, ci[1].imag))
744 print(" C = {:12.4g}+j{:12.4g}".format(ci[2].real, ci[2].imag))
745 print(" D = {:12.4g}+j{:12.4g}".format(ci[3].real, ci[3].imag))
746
747 ywf = np.empty(nxw, dtype = complex)
748 for i in range(nxw):
749 x = xwmin + i * xwstep
750 ywf[i] = cal_wavefunction(ci, x, kw, Ei, w, b, V0)
751
752 charge = [(ywf[i] * ywf[i].conjugate()).real for i in range(nxw)]
753
754 fig = plt.figure(figsize = (16, 4)) #figsize)
755 ax2 = fig.add_subplot(1, 1, 1)
756# ax2 = fig.add_subplot(2, 1, 2)
757 ax1 = ax2.twinx()
758
759 ax1.set_xlim([xwmin, xwmax])
760 ax1.plot(xplot, yplot, linewidth = 0.5, label = 'U(x)')
761 ax1.plot(ax1.get_xlim(), [0.0, 0.0], color = 'r', linestyle = 'dashed', linewidth = 0.5)
762 ax2.set_xlim([xwmin, xwmax])
763 ax2.plot(xplot, ywf.real, color = 'r', linewidth = 1.5, label = "real")
764 ax2.plot(xplot, ywf.imag, color = 'b', linewidth = 1.5, label = "imaginary")
765 ax2.plot(xplot, charge, color = 'black', linewidth = 0.5, label = "charge")
766 ax2.plot(ax1.get_xlim(), [0.0, 0.0], color = 'r', linestyle = 'dashed', linewidth = 0.5)
767 ax1.set_xlabel("x (A)", fontsize = fontsize)
768 ax1.set_ylabel("U(x)", fontsize = fontsize)
769 ax2.set_xlabel("x (A)", fontsize = fontsize)
770 ax2.set_ylabel(r"$\Psi$($x$)", fontsize = fontsize)
771
772 handler1, label1 = ax1.get_legend_handles_labels()
773 handler2, label2 = ax2.get_legend_handles_labels()
774 ax2.legend(handler1 + handler2, label1 + label2, loc = 2, borderaxespad = 0.0, fontsize = legend_fontsize)
775# ax2.legend(fontsize = legend_fontsize)
776
777 ax1.tick_params(labelsize = fontsize)
778 ax2.tick_params(labelsize = fontsize)
779 plt.tight_layout()
780
781 plt.pause(0.1)
782 print("Press ENTER to exit>>", end = '')
783 input()
784
785 terminate()
786
787def band():
788 """
789 概要:
790 バンド計算モードのメイン処理を行い、バンド構造をグラフに描画します。
791 詳細説明:
792 波数を走査しながらエネルギー固有値を求め、E-k分散関係をプロットします。
793 """
794 global mode
795 global a
796 global bwidth, bpot
797 global kmin, kmax, nk
798 global nEsearch, nMaxLevel
799
800 kstep = (kmax - kmin) / (nk - 1)
801
802 print("")
803 print("=== Input parameterss ===")
804 print("mode:", mode)
805 print("a=", a, "A")
806 print("potential: w={} A h={} eV".format(bwidth, bpot))
807 print("k range: {} - {} at {} step, {} points".format(kmin, kmax, kstep, nk))
808 print("")
809
810 print("")
811 V0 = bpot
812 b = bwidth
813 w = a - b
814
815 xk = [kmin + i * kstep for i in range(nk)]
816 yE = np.zeros([nMaxLevel, nk])
817 nMaxBand = 0
818 for ik in range(nk):
819 k = kmin + ik * kstep
820 print("at k={:8.4g}".format(k))
821
822 Elist, Alist = find_Elist(0.0, V0, nEsearch, k, w, b, V0)
823 n = len(Elist)
824 if n > nMaxBand:
825 nMaxBand = n
826 for iband in range(min(n, nMaxLevel)):
827 yE[iband][ik] = Elist[iband]
828
829
830 fig = plt.figure(figsize = figsize)
831 ax1 = fig.add_subplot(1, 1, 1)
832
833 ax1.set_xlim([-0.5, 0.5])
834 ax1.set_ylim(Erange)
835# ax1.set_ylim([0.0, ax1.get_ylim()[1]])
836
837 for iL in range(nMaxBand):
838 ax1.plot(xk, yE[iL], linestyle = '', marker = 'o', markersize = 5.0,
839 markerfacecolor = 'none', markeredgecolor = 'black', markeredgewidth = 0.5)
840 ax1.set_xlabel(r"$k$ $(\pi$$/a)$", fontsize = fontsize)
841 ax1.set_ylabel("E (eV)", fontsize = fontsize)
842# ax1.legend(fontsize = legend_fontsize)
843 plt.tight_layout()
844
845 plt.pause(0.1)
846
847 print("Press ENTER to exit>>", end = '')
848 input()
849
850 terminate()
851
852def graphview():
853 """
854 概要:
855 グラフモードのメイン処理を行い、方程式の誤差のエネルギー依存性を描画します。
856 詳細説明:
857 エネルギーを走査してKronig-Penneyの方程式の残差を計算しプロットします。解の位置を可視化できます。
858 """
859 global mode
860 global a
861 global bwidth, bpot
862 global Emin, Emax, nE
863
864 Estep = (Emax - Emin) / (nE - 1)
865
866 V0 = bpot
867 b = bwidth
868 w = a - b
869
870 print("")
871 print("=== Input parameterss ===")
872 print("mode:", mode)
873 print("a=", a, "A")
874 print(" barrier: w={} A h={} eV".format(b, V0))
875 print(" well : w={} A h={} eV".format(w, 0.0))
876 print("Energy range: {} - {}, {} eV step {} points".format(Emin, Emax, Estep, nE))
877 print("at k = {}".format(kg))
878 print("")
879
880 xE = []
881 yD = []
882 for i in range(1, nE):
883 E = Emin + i * Estep
884 if V0 <= E:
885 break
886
887 delta = cal_delta(E, kg, w, b, V0)
888
889 xE.append(E)
890 yD.append(delta)
891
892 fig = plt.figure(figsize = figsize)
893 ax1 = fig.add_subplot(1, 1, 1)
894
895 ax1.plot(xE, yD)
896 ax1.set_xlim([Emin, Emax])
897 ax1.plot([Emin, Emax], [0.0, 0.0], linestyle = 'dashed', color = 'r', linewidth = 0.5)
898 ax1.set_xlabel("E (eV)", fontsize = fontsize)
899 ax1.set_ylabel("delta", fontsize = fontsize)
900# ax1.legend(fontsize = legend_fontsize)
901 ax1.tick_params(labelsize = fontsize)
902 plt.tight_layout()
903
904 plt.pause(0.1)
905 print("Press ENTER to exit>>", end = '')
906 input()
907
908 terminate()
909
910def main():
911 """
912 概要:
913 グローバル変数のmodeに従って適切なモードの処理を呼び出します。
914 """
915 global mode
916
917 if mode == 'graph':
918 graphview()
919 elif mode == 'band':
920 band()
921 elif mode == 'wf':
922 wf()
923 else:
924 terminate("Error: Invalid mode [{}]".format(mode))
925
926
927if __name__ == "__main__":
928 main()