0.1.44
This commit is contained in:
		| @@ -1,7 +1,7 @@ | ||||
| [metadata] | ||||
| # replace with your username: | ||||
| name = guan | ||||
| version = 0.1.43 | ||||
| version = 0.1.44 | ||||
| author = guanjihuan | ||||
| author_email = guanjihuan@163.com | ||||
| description = An open source python package | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| Metadata-Version: 2.1 | ||||
| Name: guan | ||||
| Version: 0.1.43 | ||||
| Version: 0.1.44 | ||||
| Summary: An open source python package | ||||
| Home-page: https://py.guanjihuan.com | ||||
| Author: guanjihuan | ||||
|   | ||||
| @@ -4,18 +4,14 @@ pyproject.toml | ||||
| setup.cfg | ||||
| src/guan/Fourier_transform.py | ||||
| src/guan/Green_functions.py | ||||
| src/guan/Hamiltonian_of_finite_size_systems.py | ||||
| src/guan/Hamiltonian_of_models_in_reciprocal_space.py | ||||
| src/guan/Hamiltonian_of_examples.py | ||||
| src/guan/__init__.py | ||||
| src/guan/band_structures_and_wave_functions.py | ||||
| src/guan/basic_functions.py | ||||
| src/guan/data_processing.py | ||||
| src/guan/density_of_states.py | ||||
| src/guan/file_processing.py | ||||
| src/guan/others.py | ||||
| src/guan/plot_figures.py | ||||
| src/guan/quantum_transport.py | ||||
| src/guan/read_and_write.py | ||||
| src/guan/topological_invariant.py | ||||
| src/guan.egg-info/PKG-INFO | ||||
| src/guan.egg-info/SOURCES.txt | ||||
|   | ||||
							
								
								
									
										574
									
								
								PyPI/src/guan/Hamiltonian_of_examples.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										574
									
								
								PyPI/src/guan/Hamiltonian_of_examples.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,574 @@ | ||||
| # Module: Hamiltonian_of_examples | ||||
|  | ||||
| # 构建一维的有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_one_direction(N, on_site=0, hopping=1, period=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping = np.array(hopping) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N*dim, N*dim), dtype=complex) | ||||
|     for i0 in range(N): | ||||
|         hamiltonian[i0*dim+0:i0*dim+dim, i0*dim+0:i0*dim+dim] = on_site | ||||
|     for i0 in range(N-1): | ||||
|         hamiltonian[i0*dim+0:i0*dim+dim, (i0+1)*dim+0:(i0+1)*dim+dim] = hopping | ||||
|         hamiltonian[(i0+1)*dim+0:(i0+1)*dim+dim, i0*dim+0:i0*dim+dim] = hopping.transpose().conj() | ||||
|     if period == 1: | ||||
|         hamiltonian[(N-1)*dim+0:(N-1)*dim+dim, 0:dim] = hopping | ||||
|         hamiltonian[0:dim, (N-1)*dim+0:(N-1)*dim+dim] = hopping.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建二维的方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1, N2, on_site=0, hopping_1=1, hopping_2=1, period_1=0, period_2=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping_1 = np.array(hopping_1) | ||||
|     hopping_2 = np.array(hopping_2) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N1*N2*dim, N1*N2*dim), dtype=complex)     | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = on_site | ||||
|     for i1 in range(N1-1): | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, (i1+1)*N2*dim+i2*dim+0:(i1+1)*N2*dim+i2*dim+dim] = hopping_1 | ||||
|             hamiltonian[(i1+1)*N2*dim+i2*dim+0:(i1+1)*N2*dim+i2*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = hopping_1.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2-1): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, i1*N2*dim+(i2+1)*dim+0:i1*N2*dim+(i2+1)*dim+dim] = hopping_2 | ||||
|             hamiltonian[i1*N2*dim+(i2+1)*dim+0:i1*N2*dim+(i2+1)*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = hopping_2.transpose().conj() | ||||
|     if period_1 == 1: | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[(N1-1)*N2*dim+i2*dim+0:(N1-1)*N2*dim+i2*dim+dim, i2*dim+0:i2*dim+dim] = hopping_1 | ||||
|             hamiltonian[i2*dim+0:i2*dim+dim, (N1-1)*N2*dim+i2*dim+0:(N1-1)*N2*dim+i2*dim+dim] = hopping_1.transpose().conj() | ||||
|     if period_2 == 1: | ||||
|         for i1 in range(N1): | ||||
|             hamiltonian[i1*N2*dim+(N2-1)*dim+0:i1*N2*dim+(N2-1)*dim+dim, i1*N2*dim+0:i1*N2*dim+dim] = hopping_2 | ||||
|             hamiltonian[i1*N2*dim+0:i1*N2*dim+dim, i1*N2*dim+(N2-1)*dim+0:i1*N2*dim+(N2-1)*dim+dim] = hopping_2.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建三维的立方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_three_directions_for_cubic_lattice(N1, N2, N3, on_site=0, hopping_1=1, hopping_2=1, hopping_3=1, period_1=0, period_2=0, period_3=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping_1 = np.array(hopping_1) | ||||
|     hopping_2 = np.array(hopping_2) | ||||
|     hopping_3 = np.array(hopping_3) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N1*N2*N3*dim, N1*N2*N3*dim), dtype=complex)  | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = on_site | ||||
|     for i1 in range(N1-1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, (i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1 | ||||
|                 hamiltonian[(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2-1): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+dim] = hopping_2 | ||||
|                 hamiltonian[i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_2.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3-1): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+dim] = hopping_3 | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_3.transpose().conj() | ||||
|     if period_1 == 1: | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+dim, i2*N3*dim+i3*dim+0:i2*N3*dim+i3*dim+dim] = hopping_1 | ||||
|                 hamiltonian[i2*N3*dim+i3*dim+0:i2*N3*dim+i3*dim+dim, (N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1.transpose().conj() | ||||
|     if period_2 == 1: | ||||
|         for i1 in range(N1): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+dim, i1*N2*N3*dim+i3*dim+0:i1*N2*N3*dim+i3*dim+dim] = hopping_2 | ||||
|                 hamiltonian[i1*N2*N3*dim+i3*dim+0:i1*N2*N3*dim+i3*dim+dim, i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+dim] = hopping_2.transpose().conj() | ||||
|     if period_3 == 1: | ||||
|         for i1 in range(N1): | ||||
|             for i2 in range(N2): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+dim, i1*N2*N3*dim+i2*N3*dim+0:i1*N2*N3*dim+i2*N3*dim+dim] = hopping_3 | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+0:i1*N2*N3*dim+i2*N3*dim+dim, i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+dim] = hopping_3.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建有限尺寸的SSH模型哈密顿量 | ||||
| def hamiltonian_of_finite_size_ssh_model(N, v=0.6, w=1, onsite_1=0, onsite_2=0, period=1): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2*N, 2*N)) | ||||
|     for i in range(N): | ||||
|         hamiltonian[i*2+0, i*2+0] = onsite_1 | ||||
|         hamiltonian[i*2+1, i*2+1] = onsite_2 | ||||
|         hamiltonian[i*2+0, i*2+1] = v | ||||
|         hamiltonian[i*2+1, i*2+0] = v | ||||
|     for i in range(N-1): | ||||
|         hamiltonian[i*2+1, (i+1)*2+0] = w | ||||
|         hamiltonian[(i+1)*2+0, i*2+1] = w | ||||
|     if period==1: | ||||
|         hamiltonian[0, 2*N-1] = w | ||||
|         hamiltonian[2*N-1, 0] = w | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 获取Zigzag边的石墨烯条带的元胞间跃迁 | ||||
| def get_hopping_term_of_graphene_ribbon_along_zigzag_direction(N, eta=0): | ||||
|     import numpy as np | ||||
|     hopping = np.zeros((4*N, 4*N), dtype=complex) | ||||
|     for i0 in range(N): | ||||
|         hopping[4*i0+0, 4*i0+0] = eta | ||||
|         hopping[4*i0+1, 4*i0+1] = eta | ||||
|         hopping[4*i0+2, 4*i0+2] = eta | ||||
|         hopping[4*i0+3, 4*i0+3] = eta | ||||
|         hopping[4*i0+1, 4*i0+0] = 1 | ||||
|         hopping[4*i0+2, 4*i0+3] = 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hopping | ||||
|  | ||||
| # 构建有限尺寸的石墨烯哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_two_directions_for_graphene(N1, N2, period_1=0, period_2=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     on_site = guan.hamiltonian_of_finite_size_system_along_one_direction(4) | ||||
|     hopping_1 = guan.get_hopping_term_of_graphene_ribbon_along_zigzag_direction(1) | ||||
|     hopping_2 = np.zeros((4, 4), dtype=complex) | ||||
|     hopping_2[3, 0] = 1 | ||||
|     hamiltonian = guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1, N2, on_site, hopping_1, hopping_2, period_1, period_2) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 获取石墨烯有效模型沿着x方向的在位能和跃迁项(其中,动量qy为参数) | ||||
| def get_onsite_and_hopping_terms_of_2d_effective_graphene_along_one_direction(qy, t=1, staggered_potential=0, eta=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     h00 = np.zeros((2, 2), dtype=complex) | ||||
|     h00[0, 0] = staggered_potential | ||||
|     h00[1, 1] = -staggered_potential | ||||
|     h00[0, 1] = -1j*constant*t*np.sin(qy) | ||||
|     h00[1, 0] = 1j*constant*t*np.sin(qy) | ||||
|     h01 = np.zeros((2, 2), dtype=complex) | ||||
|     h01[0, 0] = eta | ||||
|     h01[1, 1] = eta | ||||
|     if valley_index == 0: | ||||
|         h01[0, 1] = constant*t*(-1j/2) | ||||
|         h01[1, 0] = constant*t*(-1j/2) | ||||
|     else: | ||||
|         h01[0, 1] = constant*t*(1j/2) | ||||
|         h01[1, 0] = constant*t*(1j/2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return h00, h01 | ||||
|  | ||||
| # 获取BHZ模型的在位能和跃迁项 | ||||
| def get_onsite_and_hopping_terms_of_bhz_model(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((4, 4), dtype=complex) | ||||
|     H1 = np.zeros((4, 4), dtype=complex) | ||||
|     H2 = np.zeros((4, 4), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H0[2, 2] = E_s | ||||
|     H0[3, 3] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[2, 2] = V_ss | ||||
|     H1[3, 3] = V_pp | ||||
|     H1[0, 1] = V_sp | ||||
|     H1[1, 0] = -np.conj(V_sp) | ||||
|     H1[2, 3] = np.conj(V_sp) | ||||
|     H1[3, 2] = -V_sp | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[2, 2] = V_ss | ||||
|     H2[3, 3] = V_pp | ||||
|     H2[0, 1] = 1j*V_sp | ||||
|     H2[1, 0] = 1j*np.conj(V_sp) | ||||
|     H2[2, 3] = -1j*np.conj(V_sp) | ||||
|     H2[3, 2] = -1j*V_sp | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
|  | ||||
| # 获取半个BHZ模型的在位能和跃迁项(自旋向上) | ||||
| def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_up(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((2, 2), dtype=complex) | ||||
|     H1 = np.zeros((2, 2), dtype=complex) | ||||
|     H2 = np.zeros((2, 2), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[0, 1] = V_sp | ||||
|     H1[1, 0] = -np.conj(V_sp) | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[0, 1] = 1j*V_sp | ||||
|     H2[1, 0] = 1j*np.conj(V_sp) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
|  | ||||
| # 获取半个BHZ模型的在位能和跃迁项(自旋向下) | ||||
| def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_down(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((2, 2), dtype=complex) | ||||
|     H1 = np.zeros((2, 2), dtype=complex) | ||||
|     H2 = np.zeros((2, 2), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[0, 1] = np.conj(V_sp) | ||||
|     H1[1, 0] = -V_sp | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[0, 1] = -1j*np.conj(V_sp) | ||||
|     H2[1, 0] = -1j*V_sp | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
|  | ||||
| # 一维链的哈密顿量(倒空间) | ||||
| def hamiltonian_of_simple_chain(k): | ||||
|     import guan | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=0, hopping=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 二维方格子的哈密顿量(倒空间) | ||||
| def hamiltonian_of_square_lattice(k1, k2): | ||||
|     import guan | ||||
|     hamiltonian = guan.two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell=0, hopping_1=1, hopping_2=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维方格子条带的哈密顿量(倒空间) | ||||
| def hamiltonian_of_square_lattice_in_quasi_one_dimension(k, N=10, period=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     h00 = np.zeros((N, N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((N, N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N-1):    | ||||
|         h00[i, i+1] = 1 | ||||
|         h00[i+1, i] = 1 | ||||
|     if period == 1: | ||||
|         h00[N-1, 0] = 1 | ||||
|         h00[0, N-1] = 1 | ||||
|     for i in range(N):    | ||||
|         h01[i, i] = 1 | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 三维立方格子的哈密顿量(倒空间) | ||||
| def hamiltonian_of_cubic_lattice(k1, k2, k3): | ||||
|     import guan | ||||
|     hamiltonian = guan.three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell=0, hopping_1=1, hopping_2=1, hopping_3=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # SSH模型的哈密顿量(倒空间) | ||||
| def hamiltonian_of_ssh_model(k, v=0.6, w=1): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0,1] = v+w*cmath.exp(-1j*k) | ||||
|     hamiltonian[1,0] = v+w*cmath.exp(1j*k) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯的哈密顿量(倒空间) | ||||
| def hamiltonian_of_graphene(k1, k2, staggered_potential=0, t=1, a='default'): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if a == 'default': | ||||
|         a = 1/math.sqrt(3) | ||||
|     h0 = np.zeros((2, 2), dtype=complex)  # mass term | ||||
|     h1 = np.zeros((2, 2), dtype=complex)  # nearest hopping | ||||
|     h0[0, 0] = staggered_potential      | ||||
|     h0[1, 1] = -staggered_potential | ||||
|     h1[1, 0] = t*(cmath.exp(1j*k2*a)+cmath.exp(1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j/2*k2*a))    | ||||
|     h1[0, 1] = h1[1, 0].conj() | ||||
|     hamiltonian = h0 + h1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯有效模型的哈密顿量(倒空间) | ||||
| def effective_hamiltonian_of_graphene(qx, qy, t=1, staggered_potential=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 0] = staggered_potential | ||||
|     hamiltonian[1, 1] = -staggered_potential | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     if valley_index == 0: | ||||
|         hamiltonian[0, 1] = constant*t*(qx-1j*qy) | ||||
|         hamiltonian[1, 0] = constant*t*(qx+1j*qy) | ||||
|     else: | ||||
|         hamiltonian[0, 1] = constant*t*(-qx-1j*qy) | ||||
|         hamiltonian[1, 0] = constant*t*(-qx+1j*qy) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯有效模型离散化后的哈密顿量(倒空间) | ||||
| def effective_hamiltonian_of_graphene_after_discretization(qx, qy, t=1, staggered_potential=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 0] = staggered_potential | ||||
|     hamiltonian[1, 1] = -staggered_potential | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     if valley_index == 0: | ||||
|         hamiltonian[0, 1] = constant*t*(np.sin(qx)-1j*np.sin(qy)) | ||||
|         hamiltonian[1, 0] = constant*t*(np.sin(qx)+1j*np.sin(qy)) | ||||
|     else: | ||||
|         hamiltonian[0, 1] = constant*t*(-np.sin(qx)-1j*np.sin(qy)) | ||||
|         hamiltonian[1, 0] = constant*t*(-np.sin(qx)+1j*np.sin(qy)) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维Zigzag边石墨烯条带的哈密顿量(倒空间) | ||||
| def hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension(k, N=10, M=0, t=1, period=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     h00 = np.zeros((4*N, 4*N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((4*N, 4*N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N): | ||||
|         h00[i*4+0, i*4+0] = M | ||||
|         h00[i*4+1, i*4+1] = -M | ||||
|         h00[i*4+2, i*4+2] = M | ||||
|         h00[i*4+3, i*4+3] = -M | ||||
|         h00[i*4+0, i*4+1] = t | ||||
|         h00[i*4+1, i*4+0] = t | ||||
|         h00[i*4+1, i*4+2] = t | ||||
|         h00[i*4+2, i*4+1] = t | ||||
|         h00[i*4+2, i*4+3] = t | ||||
|         h00[i*4+3, i*4+2] = t | ||||
|     for i in range(N-1): | ||||
|         h00[i*4+3, (i+1)*4+0] = t | ||||
|         h00[(i+1)*4+0, i*4+3] = t | ||||
|     if period == 1: | ||||
|         h00[(N-1)*4+3, 0] = t | ||||
|         h00[0, (N-1)*4+3] = t | ||||
|     for i in range(N): | ||||
|         h01[i*4+1, i*4+0] = t | ||||
|         h01[i*4+2, i*4+3] = t | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # Haldane模型的哈密顿量(倒空间) | ||||
| def hamiltonian_of_haldane_model(k1, k2, M=2/3, t1=1, t2=1/3, phi='default', a='default'): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if phi == 'default': | ||||
|         phi=math.pi/4 | ||||
|     if a == 'default': | ||||
|         a=1/math.sqrt(3) | ||||
|     h0 = np.zeros((2, 2), dtype=complex)  # mass term | ||||
|     h1 = np.zeros((2, 2), dtype=complex)  # nearest hopping | ||||
|     h2 = np.zeros((2, 2), dtype=complex)  # next nearest hopping | ||||
|     h0[0, 0] = M | ||||
|     h0[1, 1] = -M | ||||
|     h1[1, 0] = t1*(cmath.exp(1j*k2*a)+cmath.exp(1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)) | ||||
|     h1[0, 1] = h1[1, 0].conj() | ||||
|     h2[0, 0] = t2*cmath.exp(-1j*phi)*(cmath.exp(1j*math.sqrt(3)*k1*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a+1j*3/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j*3/2*k2*a)) | ||||
|     h2[1, 1] = t2*cmath.exp(1j*phi)*(cmath.exp(1j*math.sqrt(3)*k1*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a+1j*3/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j*3/2*k2*a)) | ||||
|     hamiltonian = h0 + h1 + h2 + h2.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维Haldane模型条带的哈密顿量(倒空间) | ||||
| def hamiltonian_of_haldane_model_in_quasi_one_dimension(k, N=10, M=2/3, t1=1, t2=1/3, phi='default', period=0): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if phi == 'default': | ||||
|         phi=math.pi/4 | ||||
|     h00 = np.zeros((4*N, 4*N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((4*N, 4*N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N): | ||||
|         h00[i*4+0, i*4+0] = M | ||||
|         h00[i*4+1, i*4+1] = -M | ||||
|         h00[i*4+2, i*4+2] = M | ||||
|         h00[i*4+3, i*4+3] = -M | ||||
|         h00[i*4+0, i*4+1] = t1 | ||||
|         h00[i*4+1, i*4+0] = t1 | ||||
|         h00[i*4+1, i*4+2] = t1 | ||||
|         h00[i*4+2, i*4+1] = t1 | ||||
|         h00[i*4+2, i*4+3] = t1 | ||||
|         h00[i*4+3, i*4+2] = t1 | ||||
|         h00[i*4+0, i*4+2] = t2*cmath.exp(-1j*phi) | ||||
|         h00[i*4+2, i*4+0] = h00[i*4+0, i*4+2].conj() | ||||
|         h00[i*4+1, i*4+3] = t2*cmath.exp(-1j*phi) | ||||
|         h00[i*4+3, i*4+1] = h00[i*4+1, i*4+3].conj() | ||||
|     for i in range(N-1): | ||||
|         h00[i*4+3, (i+1)*4+0] = t1 | ||||
|         h00[(i+1)*4+0, i*4+3] = t1 | ||||
|         h00[i*4+2, (i+1)*4+0] = t2*cmath.exp(1j*phi) | ||||
|         h00[(i+1)*4+0, i*4+2] = h00[i*4+2, (i+1)*4+0].conj() | ||||
|         h00[i*4+3, (i+1)*4+1] = t2*cmath.exp(1j*phi) | ||||
|         h00[(i+1)*4+1, i*4+3] = h00[i*4+3, (i+1)*4+1].conj() | ||||
|     if period == 1: | ||||
|         h00[(N-1)*4+3, 0] = t1 | ||||
|         h00[0, (N-1)*4+3] = t1 | ||||
|         h00[(N-1)*4+2, 0] = t2*cmath.exp(1j*phi) | ||||
|         h00[0, (N-1)*4+2] = h00[(N-1)*4+2, 0].conj() | ||||
|         h00[(N-1)*4+3, 1] = t2*cmath.exp(1j*phi) | ||||
|         h00[1, (N-1)*4+3] = h00[(N-1)*4+3, 1].conj() | ||||
|     for i in range(N): | ||||
|         h01[i*4+1, i*4+0] = t1 | ||||
|         h01[i*4+2, i*4+3] = t1 | ||||
|         h01[i*4+0, i*4+0] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+1, i*4+1] = t2*cmath.exp(-1j*phi) | ||||
|         h01[i*4+2, i*4+2] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+3, i*4+3] = t2*cmath.exp(-1j*phi) | ||||
|         h01[i*4+1, i*4+3] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+2, i*4+0] = t2*cmath.exp(-1j*phi) | ||||
|         if i != 0: | ||||
|             h01[i*4+1, (i-1)*4+3] = t2*cmath.exp(1j*phi) | ||||
|     for i in range(N-1): | ||||
|         h01[i*4+2, (i+1)*4+0] = t2*cmath.exp(-1j*phi) | ||||
|     hamiltonian = h00 + h01*cmath.exp(1j*k) + h01.transpose().conj()*cmath.exp(-1j*k) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 一个量子反常霍尔效应的哈密顿量(倒空间) | ||||
| def hamiltonian_of_one_QAH_model(k1, k2, t1=1, t2=1, t3=0.5, m=-1): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 1] = 2*t1*math.cos(k1)-1j*2*t1*math.cos(k2) | ||||
|     hamiltonian[1, 0] = 2*t1*math.cos(k1)+1j*2*t1*math.cos(k2) | ||||
|     hamiltonian[0, 0] = m+2*t3*math.sin(k1)+2*t3*math.sin(k2)+2*t2*math.cos(k1+k2) | ||||
|     hamiltonian[1, 1] = -(m+2*t3*math.sin(k1)+2*t3*math.sin(k2)+2*t2*math.cos(k1+k2)) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # BHZ模型的哈密顿量(倒空间) | ||||
| def hamiltonian_of_bhz_model(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((4, 4), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = np.conj(d1_d2) | ||||
|     hamiltonian[1, 0] = d1_d2 | ||||
|     hamiltonian[2, 2] = varepsilon+d3 | ||||
|     hamiltonian[3, 3] = varepsilon-d3 | ||||
|     hamiltonian[2, 3] = -d1_d2  | ||||
|     hamiltonian[3, 2] = -np.conj(d1_d2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 半BHZ模型的哈密顿量(自旋向上)(倒空间) | ||||
| def hamiltonian_of_half_bhz_model_for_spin_up(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = np.conj(d1_d2) | ||||
|     hamiltonian[1, 0] = d1_d2 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 半BHZ模型的哈密顿量(自旋向下)(倒空间) | ||||
| def hamiltonian_of_half_bhz_model_for_spin_down(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = -d1_d2  | ||||
|     hamiltonian[1, 0] = -np.conj(d1_d2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # BBH模型的哈密顿量(倒空间) | ||||
| def hamiltonian_of_bbh_model(kx, ky, gamma_x=0.5, gamma_y=0.5, lambda_x=1, lambda_y=1): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     # label of atoms in a unit cell | ||||
|     # (2) —— (0) | ||||
|     #  |      | | ||||
|     # (1) —— (3)    | ||||
|     hamiltonian = np.zeros((4, 4), dtype=complex) | ||||
|     hamiltonian[0, 2] = gamma_x+lambda_x*cmath.exp(1j*kx) | ||||
|     hamiltonian[1, 3] = gamma_x+lambda_x*cmath.exp(-1j*kx) | ||||
|     hamiltonian[0, 3] = gamma_y+lambda_y*cmath.exp(1j*ky) | ||||
|     hamiltonian[1, 2] = -gamma_y-lambda_y*cmath.exp(-1j*ky) | ||||
|     hamiltonian[2, 0] = np.conj(hamiltonian[0, 2]) | ||||
|     hamiltonian[3, 1] = np.conj(hamiltonian[1, 3]) | ||||
|     hamiltonian[3, 0] = np.conj(hamiltonian[0, 3]) | ||||
|     hamiltonian[2, 1] = np.conj(hamiltonian[1, 2]) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # Kagome模型的哈密顿量(倒空间) | ||||
| def hamiltonian_of_kagome_lattice(kx, ky, t=1): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     k1_dot_a1 = kx | ||||
|     k2_dot_a2 = kx/2+ky*math.sqrt(3)/2 | ||||
|     k3_dot_a3 = -kx/2+ky*math.sqrt(3)/2 | ||||
|     hamiltonian = np.zeros((3, 3), dtype=complex) | ||||
|     hamiltonian[0, 1] = 2*math.cos(k1_dot_a1) | ||||
|     hamiltonian[0, 2] = 2*math.cos(k2_dot_a2) | ||||
|     hamiltonian[1, 2] = 2*math.cos(k3_dot_a3) | ||||
|     hamiltonian = hamiltonian + hamiltonian.transpose().conj() | ||||
|     hamiltonian = -t*hamiltonian | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
| @@ -1,260 +0,0 @@ | ||||
| # Module: Hamiltonian_of_finite_size_systems | ||||
|  | ||||
| # 构建一维的有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_one_direction(N, on_site=0, hopping=1, period=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping = np.array(hopping) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N*dim, N*dim), dtype=complex) | ||||
|     for i0 in range(N): | ||||
|         hamiltonian[i0*dim+0:i0*dim+dim, i0*dim+0:i0*dim+dim] = on_site | ||||
|     for i0 in range(N-1): | ||||
|         hamiltonian[i0*dim+0:i0*dim+dim, (i0+1)*dim+0:(i0+1)*dim+dim] = hopping | ||||
|         hamiltonian[(i0+1)*dim+0:(i0+1)*dim+dim, i0*dim+0:i0*dim+dim] = hopping.transpose().conj() | ||||
|     if period == 1: | ||||
|         hamiltonian[(N-1)*dim+0:(N-1)*dim+dim, 0:dim] = hopping | ||||
|         hamiltonian[0:dim, (N-1)*dim+0:(N-1)*dim+dim] = hopping.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建二维的方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1, N2, on_site=0, hopping_1=1, hopping_2=1, period_1=0, period_2=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping_1 = np.array(hopping_1) | ||||
|     hopping_2 = np.array(hopping_2) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N1*N2*dim, N1*N2*dim), dtype=complex)     | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = on_site | ||||
|     for i1 in range(N1-1): | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, (i1+1)*N2*dim+i2*dim+0:(i1+1)*N2*dim+i2*dim+dim] = hopping_1 | ||||
|             hamiltonian[(i1+1)*N2*dim+i2*dim+0:(i1+1)*N2*dim+i2*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = hopping_1.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2-1): | ||||
|             hamiltonian[i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim, i1*N2*dim+(i2+1)*dim+0:i1*N2*dim+(i2+1)*dim+dim] = hopping_2 | ||||
|             hamiltonian[i1*N2*dim+(i2+1)*dim+0:i1*N2*dim+(i2+1)*dim+dim, i1*N2*dim+i2*dim+0:i1*N2*dim+i2*dim+dim] = hopping_2.transpose().conj() | ||||
|     if period_1 == 1: | ||||
|         for i2 in range(N2): | ||||
|             hamiltonian[(N1-1)*N2*dim+i2*dim+0:(N1-1)*N2*dim+i2*dim+dim, i2*dim+0:i2*dim+dim] = hopping_1 | ||||
|             hamiltonian[i2*dim+0:i2*dim+dim, (N1-1)*N2*dim+i2*dim+0:(N1-1)*N2*dim+i2*dim+dim] = hopping_1.transpose().conj() | ||||
|     if period_2 == 1: | ||||
|         for i1 in range(N1): | ||||
|             hamiltonian[i1*N2*dim+(N2-1)*dim+0:i1*N2*dim+(N2-1)*dim+dim, i1*N2*dim+0:i1*N2*dim+dim] = hopping_2 | ||||
|             hamiltonian[i1*N2*dim+0:i1*N2*dim+dim, i1*N2*dim+(N2-1)*dim+0:i1*N2*dim+(N2-1)*dim+dim] = hopping_2.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建三维的立方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_three_directions_for_cubic_lattice(N1, N2, N3, on_site=0, hopping_1=1, hopping_2=1, hopping_3=1, period_1=0, period_2=0, period_3=0): | ||||
|     import numpy as np | ||||
|     on_site = np.array(on_site) | ||||
|     hopping_1 = np.array(hopping_1) | ||||
|     hopping_2 = np.array(hopping_2) | ||||
|     hopping_3 = np.array(hopping_3) | ||||
|     if on_site.shape==(): | ||||
|         dim = 1 | ||||
|     else: | ||||
|         dim = on_site.shape[0] | ||||
|     hamiltonian = np.zeros((N1*N2*N3*dim, N1*N2*N3*dim), dtype=complex)  | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = on_site | ||||
|     for i1 in range(N1-1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, (i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1 | ||||
|                 hamiltonian[(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(i1+1)*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2-1): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+dim] = hopping_2 | ||||
|                 hamiltonian[i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(i2+1)*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_2.transpose().conj() | ||||
|     for i1 in range(N1): | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3-1): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim, i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+dim] = hopping_3 | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(i3+1)*dim+dim, i1*N2*N3*dim+i2*N3*dim+i3*dim+0:i1*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_3.transpose().conj() | ||||
|     if period_1 == 1: | ||||
|         for i2 in range(N2): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+dim, i2*N3*dim+i3*dim+0:i2*N3*dim+i3*dim+dim] = hopping_1 | ||||
|                 hamiltonian[i2*N3*dim+i3*dim+0:i2*N3*dim+i3*dim+dim, (N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+0:(N1-1)*N2*N3*dim+i2*N3*dim+i3*dim+dim] = hopping_1.transpose().conj() | ||||
|     if period_2 == 1: | ||||
|         for i1 in range(N1): | ||||
|             for i3 in range(N3): | ||||
|                 hamiltonian[i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+dim, i1*N2*N3*dim+i3*dim+0:i1*N2*N3*dim+i3*dim+dim] = hopping_2 | ||||
|                 hamiltonian[i1*N2*N3*dim+i3*dim+0:i1*N2*N3*dim+i3*dim+dim, i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+0:i1*N2*N3*dim+(N2-1)*N3*dim+i3*dim+dim] = hopping_2.transpose().conj() | ||||
|     if period_3 == 1: | ||||
|         for i1 in range(N1): | ||||
|             for i2 in range(N2): | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+dim, i1*N2*N3*dim+i2*N3*dim+0:i1*N2*N3*dim+i2*N3*dim+dim] = hopping_3 | ||||
|                 hamiltonian[i1*N2*N3*dim+i2*N3*dim+0:i1*N2*N3*dim+i2*N3*dim+dim, i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+0:i1*N2*N3*dim+i2*N3*dim+(N3-1)*dim+dim] = hopping_3.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 构建有限尺寸的SSH模型哈密顿量 | ||||
| def hamiltonian_of_finite_size_ssh_model(N, v=0.6, w=1, onsite_1=0, onsite_2=0, period=1): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2*N, 2*N)) | ||||
|     for i in range(N): | ||||
|         hamiltonian[i*2+0, i*2+0] = onsite_1 | ||||
|         hamiltonian[i*2+1, i*2+1] = onsite_2 | ||||
|         hamiltonian[i*2+0, i*2+1] = v | ||||
|         hamiltonian[i*2+1, i*2+0] = v | ||||
|     for i in range(N-1): | ||||
|         hamiltonian[i*2+1, (i+1)*2+0] = w | ||||
|         hamiltonian[(i+1)*2+0, i*2+1] = w | ||||
|     if period==1: | ||||
|         hamiltonian[0, 2*N-1] = w | ||||
|         hamiltonian[2*N-1, 0] = w | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 获取Zigzag边的石墨烯条带的元胞间跃迁 | ||||
| def get_hopping_term_of_graphene_ribbon_along_zigzag_direction(N, eta=0): | ||||
|     import numpy as np | ||||
|     hopping = np.zeros((4*N, 4*N), dtype=complex) | ||||
|     for i0 in range(N): | ||||
|         hopping[4*i0+0, 4*i0+0] = eta | ||||
|         hopping[4*i0+1, 4*i0+1] = eta | ||||
|         hopping[4*i0+2, 4*i0+2] = eta | ||||
|         hopping[4*i0+3, 4*i0+3] = eta | ||||
|         hopping[4*i0+1, 4*i0+0] = 1 | ||||
|         hopping[4*i0+2, 4*i0+3] = 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hopping | ||||
|  | ||||
| # 构建有限尺寸的石墨烯哈密顿量(可设置是否为周期边界条件) | ||||
| def hamiltonian_of_finite_size_system_along_two_directions_for_graphene(N1, N2, period_1=0, period_2=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     on_site = guan.hamiltonian_of_finite_size_system_along_one_direction(4) | ||||
|     hopping_1 = guan.get_hopping_term_of_graphene_ribbon_along_zigzag_direction(1) | ||||
|     hopping_2 = np.zeros((4, 4), dtype=complex) | ||||
|     hopping_2[3, 0] = 1 | ||||
|     hamiltonian = guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1, N2, on_site, hopping_1, hopping_2, period_1, period_2) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 获取石墨烯有效模型沿着x方向的在位能和跃迁项(其中,动量qy为参数) | ||||
| def get_onsite_and_hopping_terms_of_2d_effective_graphene_along_one_direction(qy, t=1, staggered_potential=0, eta=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     h00 = np.zeros((2, 2), dtype=complex) | ||||
|     h00[0, 0] = staggered_potential | ||||
|     h00[1, 1] = -staggered_potential | ||||
|     h00[0, 1] = -1j*constant*t*np.sin(qy) | ||||
|     h00[1, 0] = 1j*constant*t*np.sin(qy) | ||||
|     h01 = np.zeros((2, 2), dtype=complex) | ||||
|     h01[0, 0] = eta | ||||
|     h01[1, 1] = eta | ||||
|     if valley_index == 0: | ||||
|         h01[0, 1] = constant*t*(-1j/2) | ||||
|         h01[1, 0] = constant*t*(-1j/2) | ||||
|     else: | ||||
|         h01[0, 1] = constant*t*(1j/2) | ||||
|         h01[1, 0] = constant*t*(1j/2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return h00, h01 | ||||
|  | ||||
| # 获取BHZ模型的在位能和跃迁项 | ||||
| def get_onsite_and_hopping_terms_of_bhz_model(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((4, 4), dtype=complex) | ||||
|     H1 = np.zeros((4, 4), dtype=complex) | ||||
|     H2 = np.zeros((4, 4), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H0[2, 2] = E_s | ||||
|     H0[3, 3] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[2, 2] = V_ss | ||||
|     H1[3, 3] = V_pp | ||||
|     H1[0, 1] = V_sp | ||||
|     H1[1, 0] = -np.conj(V_sp) | ||||
|     H1[2, 3] = np.conj(V_sp) | ||||
|     H1[3, 2] = -V_sp | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[2, 2] = V_ss | ||||
|     H2[3, 3] = V_pp | ||||
|     H2[0, 1] = 1j*V_sp | ||||
|     H2[1, 0] = 1j*np.conj(V_sp) | ||||
|     H2[2, 3] = -1j*np.conj(V_sp) | ||||
|     H2[3, 2] = -1j*V_sp | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
|  | ||||
| # 获取半个BHZ模型的在位能和跃迁项(自旋向上) | ||||
| def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_up(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((2, 2), dtype=complex) | ||||
|     H1 = np.zeros((2, 2), dtype=complex) | ||||
|     H2 = np.zeros((2, 2), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[0, 1] = V_sp | ||||
|     H1[1, 0] = -np.conj(V_sp) | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[0, 1] = 1j*V_sp | ||||
|     H2[1, 0] = 1j*np.conj(V_sp) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
|  | ||||
| # 获取半个BHZ模型的在位能和跃迁项(自旋向下) | ||||
| def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_down(A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01, a=1): | ||||
|     import numpy as np | ||||
|     E_s = C+M-4*(D+B)/(a**2) | ||||
|     E_p = C-M-4*(D-B)/(a**2) | ||||
|     V_ss = (D+B)/(a**2) | ||||
|     V_pp = (D-B)/(a**2) | ||||
|     V_sp = -1j*A/(2*a) | ||||
|     H0 = np.zeros((2, 2), dtype=complex) | ||||
|     H1 = np.zeros((2, 2), dtype=complex) | ||||
|     H2 = np.zeros((2, 2), dtype=complex) | ||||
|     H0[0, 0] = E_s | ||||
|     H0[1, 1] = E_p | ||||
|     H1[0, 0] = V_ss | ||||
|     H1[1, 1] = V_pp | ||||
|     H1[0, 1] = np.conj(V_sp) | ||||
|     H1[1, 0] = -V_sp | ||||
|     H2[0, 0] = V_ss | ||||
|     H2[1, 1] = V_pp | ||||
|     H2[0, 1] = -1j*np.conj(V_sp) | ||||
|     H2[1, 0] = -1j*V_sp | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return H0, H1, H2 | ||||
| @@ -1,315 +0,0 @@ | ||||
| # Module: Hamiltonian_of_models_in_reciprocal_space | ||||
|  | ||||
| # 一维链的哈密顿量 | ||||
| def hamiltonian_of_simple_chain(k): | ||||
|     import guan | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=0, hopping=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 二维方格子的哈密顿量 | ||||
| def hamiltonian_of_square_lattice(k1, k2): | ||||
|     import guan | ||||
|     hamiltonian = guan.two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell=0, hopping_1=1, hopping_2=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维方格子条带的哈密顿量 | ||||
| def hamiltonian_of_square_lattice_in_quasi_one_dimension(k, N=10, period=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     h00 = np.zeros((N, N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((N, N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N-1):    | ||||
|         h00[i, i+1] = 1 | ||||
|         h00[i+1, i] = 1 | ||||
|     if period == 1: | ||||
|         h00[N-1, 0] = 1 | ||||
|         h00[0, N-1] = 1 | ||||
|     for i in range(N):    | ||||
|         h01[i, i] = 1 | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 三维立方格子的哈密顿量 | ||||
| def hamiltonian_of_cubic_lattice(k1, k2, k3): | ||||
|     import guan | ||||
|     hamiltonian = guan.three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell=0, hopping_1=1, hopping_2=1, hopping_3=1) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # SSH模型的哈密顿量 | ||||
| def hamiltonian_of_ssh_model(k, v=0.6, w=1): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0,1] = v+w*cmath.exp(-1j*k) | ||||
|     hamiltonian[1,0] = v+w*cmath.exp(1j*k) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯的哈密顿量 | ||||
| def hamiltonian_of_graphene(k1, k2, staggered_potential=0, t=1, a='default'): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if a == 'default': | ||||
|         a = 1/math.sqrt(3) | ||||
|     h0 = np.zeros((2, 2), dtype=complex)  # mass term | ||||
|     h1 = np.zeros((2, 2), dtype=complex)  # nearest hopping | ||||
|     h0[0, 0] = staggered_potential      | ||||
|     h0[1, 1] = -staggered_potential | ||||
|     h1[1, 0] = t*(cmath.exp(1j*k2*a)+cmath.exp(1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j/2*k2*a))    | ||||
|     h1[0, 1] = h1[1, 0].conj() | ||||
|     hamiltonian = h0 + h1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯有效模型的哈密顿量 | ||||
| def effective_hamiltonian_of_graphene(qx, qy, t=1, staggered_potential=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 0] = staggered_potential | ||||
|     hamiltonian[1, 1] = -staggered_potential | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     if valley_index == 0: | ||||
|         hamiltonian[0, 1] = constant*t*(qx-1j*qy) | ||||
|         hamiltonian[1, 0] = constant*t*(qx+1j*qy) | ||||
|     else: | ||||
|         hamiltonian[0, 1] = constant*t*(-qx-1j*qy) | ||||
|         hamiltonian[1, 0] = constant*t*(-qx+1j*qy) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 石墨烯有效模型离散化后的哈密顿量 | ||||
| def effective_hamiltonian_of_graphene_after_discretization(qx, qy, t=1, staggered_potential=0, valley_index=0): | ||||
|     import numpy as np | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 0] = staggered_potential | ||||
|     hamiltonian[1, 1] = -staggered_potential | ||||
|     constant = -np.sqrt(3)/2 | ||||
|     if valley_index == 0: | ||||
|         hamiltonian[0, 1] = constant*t*(np.sin(qx)-1j*np.sin(qy)) | ||||
|         hamiltonian[1, 0] = constant*t*(np.sin(qx)+1j*np.sin(qy)) | ||||
|     else: | ||||
|         hamiltonian[0, 1] = constant*t*(-np.sin(qx)-1j*np.sin(qy)) | ||||
|         hamiltonian[1, 0] = constant*t*(-np.sin(qx)+1j*np.sin(qy)) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维Zigzag边石墨烯条带的哈密顿量 | ||||
| def hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension(k, N=10, M=0, t=1, period=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
|     h00 = np.zeros((4*N, 4*N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((4*N, 4*N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N): | ||||
|         h00[i*4+0, i*4+0] = M | ||||
|         h00[i*4+1, i*4+1] = -M | ||||
|         h00[i*4+2, i*4+2] = M | ||||
|         h00[i*4+3, i*4+3] = -M | ||||
|         h00[i*4+0, i*4+1] = t | ||||
|         h00[i*4+1, i*4+0] = t | ||||
|         h00[i*4+1, i*4+2] = t | ||||
|         h00[i*4+2, i*4+1] = t | ||||
|         h00[i*4+2, i*4+3] = t | ||||
|         h00[i*4+3, i*4+2] = t | ||||
|     for i in range(N-1): | ||||
|         h00[i*4+3, (i+1)*4+0] = t | ||||
|         h00[(i+1)*4+0, i*4+3] = t | ||||
|     if period == 1: | ||||
|         h00[(N-1)*4+3, 0] = t | ||||
|         h00[0, (N-1)*4+3] = t | ||||
|     for i in range(N): | ||||
|         h01[i*4+1, i*4+0] = t | ||||
|         h01[i*4+2, i*4+3] = t | ||||
|     hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # Haldane模型的哈密顿量 | ||||
| def hamiltonian_of_haldane_model(k1, k2, M=2/3, t1=1, t2=1/3, phi='default', a='default'): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if phi == 'default': | ||||
|         phi=math.pi/4 | ||||
|     if a == 'default': | ||||
|         a=1/math.sqrt(3) | ||||
|     h0 = np.zeros((2, 2), dtype=complex)  # mass term | ||||
|     h1 = np.zeros((2, 2), dtype=complex)  # nearest hopping | ||||
|     h2 = np.zeros((2, 2), dtype=complex)  # next nearest hopping | ||||
|     h0[0, 0] = M | ||||
|     h0[1, 1] = -M | ||||
|     h1[1, 0] = t1*(cmath.exp(1j*k2*a)+cmath.exp(1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j/2*k2*a)) | ||||
|     h1[0, 1] = h1[1, 0].conj() | ||||
|     h2[0, 0] = t2*cmath.exp(-1j*phi)*(cmath.exp(1j*math.sqrt(3)*k1*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a+1j*3/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j*3/2*k2*a)) | ||||
|     h2[1, 1] = t2*cmath.exp(1j*phi)*(cmath.exp(1j*math.sqrt(3)*k1*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a+1j*3/2*k2*a)+cmath.exp(-1j*math.sqrt(3)/2*k1*a-1j*3/2*k2*a)) | ||||
|     hamiltonian = h0 + h1 + h2 + h2.transpose().conj() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 准一维Haldane模型条带的哈密顿量 | ||||
| def hamiltonian_of_haldane_model_in_quasi_one_dimension(k, N=10, M=2/3, t1=1, t2=1/3, phi='default', period=0): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     import math | ||||
|     if phi == 'default': | ||||
|         phi=math.pi/4 | ||||
|     h00 = np.zeros((4*N, 4*N), dtype=complex)  # hopping in a unit cell | ||||
|     h01 = np.zeros((4*N, 4*N), dtype=complex)  # hopping between unit cells | ||||
|     for i in range(N): | ||||
|         h00[i*4+0, i*4+0] = M | ||||
|         h00[i*4+1, i*4+1] = -M | ||||
|         h00[i*4+2, i*4+2] = M | ||||
|         h00[i*4+3, i*4+3] = -M | ||||
|         h00[i*4+0, i*4+1] = t1 | ||||
|         h00[i*4+1, i*4+0] = t1 | ||||
|         h00[i*4+1, i*4+2] = t1 | ||||
|         h00[i*4+2, i*4+1] = t1 | ||||
|         h00[i*4+2, i*4+3] = t1 | ||||
|         h00[i*4+3, i*4+2] = t1 | ||||
|         h00[i*4+0, i*4+2] = t2*cmath.exp(-1j*phi) | ||||
|         h00[i*4+2, i*4+0] = h00[i*4+0, i*4+2].conj() | ||||
|         h00[i*4+1, i*4+3] = t2*cmath.exp(-1j*phi) | ||||
|         h00[i*4+3, i*4+1] = h00[i*4+1, i*4+3].conj() | ||||
|     for i in range(N-1): | ||||
|         h00[i*4+3, (i+1)*4+0] = t1 | ||||
|         h00[(i+1)*4+0, i*4+3] = t1 | ||||
|         h00[i*4+2, (i+1)*4+0] = t2*cmath.exp(1j*phi) | ||||
|         h00[(i+1)*4+0, i*4+2] = h00[i*4+2, (i+1)*4+0].conj() | ||||
|         h00[i*4+3, (i+1)*4+1] = t2*cmath.exp(1j*phi) | ||||
|         h00[(i+1)*4+1, i*4+3] = h00[i*4+3, (i+1)*4+1].conj() | ||||
|     if period == 1: | ||||
|         h00[(N-1)*4+3, 0] = t1 | ||||
|         h00[0, (N-1)*4+3] = t1 | ||||
|         h00[(N-1)*4+2, 0] = t2*cmath.exp(1j*phi) | ||||
|         h00[0, (N-1)*4+2] = h00[(N-1)*4+2, 0].conj() | ||||
|         h00[(N-1)*4+3, 1] = t2*cmath.exp(1j*phi) | ||||
|         h00[1, (N-1)*4+3] = h00[(N-1)*4+3, 1].conj() | ||||
|     for i in range(N): | ||||
|         h01[i*4+1, i*4+0] = t1 | ||||
|         h01[i*4+2, i*4+3] = t1 | ||||
|         h01[i*4+0, i*4+0] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+1, i*4+1] = t2*cmath.exp(-1j*phi) | ||||
|         h01[i*4+2, i*4+2] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+3, i*4+3] = t2*cmath.exp(-1j*phi) | ||||
|         h01[i*4+1, i*4+3] = t2*cmath.exp(1j*phi) | ||||
|         h01[i*4+2, i*4+0] = t2*cmath.exp(-1j*phi) | ||||
|         if i != 0: | ||||
|             h01[i*4+1, (i-1)*4+3] = t2*cmath.exp(1j*phi) | ||||
|     for i in range(N-1): | ||||
|         h01[i*4+2, (i+1)*4+0] = t2*cmath.exp(-1j*phi) | ||||
|     hamiltonian = h00 + h01*cmath.exp(1j*k) + h01.transpose().conj()*cmath.exp(-1j*k) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 一个量子反常霍尔效应的哈密顿量 | ||||
| def hamiltonian_of_one_QAH_model(k1, k2, t1=1, t2=1, t3=0.5, m=-1): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     hamiltonian[0, 1] = 2*t1*math.cos(k1)-1j*2*t1*math.cos(k2) | ||||
|     hamiltonian[1, 0] = 2*t1*math.cos(k1)+1j*2*t1*math.cos(k2) | ||||
|     hamiltonian[0, 0] = m+2*t3*math.sin(k1)+2*t3*math.sin(k2)+2*t2*math.cos(k1+k2) | ||||
|     hamiltonian[1, 1] = -(m+2*t3*math.sin(k1)+2*t3*math.sin(k2)+2*t2*math.cos(k1+k2)) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # BHZ模型的哈密顿量 | ||||
| def hamiltonian_of_bhz_model(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((4, 4), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = np.conj(d1_d2) | ||||
|     hamiltonian[1, 0] = d1_d2 | ||||
|     hamiltonian[2, 2] = varepsilon+d3 | ||||
|     hamiltonian[3, 3] = varepsilon-d3 | ||||
|     hamiltonian[2, 3] = -d1_d2  | ||||
|     hamiltonian[3, 2] = -np.conj(d1_d2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 半BHZ模型的哈密顿量(自旋向上) | ||||
| def hamiltonian_of_half_bhz_model_for_spin_up(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = np.conj(d1_d2) | ||||
|     hamiltonian[1, 0] = d1_d2 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # 半BHZ模型的哈密顿量(自旋向下) | ||||
| def hamiltonian_of_half_bhz_model_for_spin_down(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     hamiltonian = np.zeros((2, 2), dtype=complex) | ||||
|     varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) | ||||
|     d3 = -2*B*(2-(M/2/B)-math.cos(kx)-math.cos(ky)) | ||||
|     d1_d2 = A*(math.sin(kx)+1j*math.sin(ky)) | ||||
|     hamiltonian[0, 0] = varepsilon+d3 | ||||
|     hamiltonian[1, 1] = varepsilon-d3 | ||||
|     hamiltonian[0, 1] = -d1_d2  | ||||
|     hamiltonian[1, 0] = -np.conj(d1_d2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # BBH模型的哈密顿量 | ||||
| def hamiltonian_of_bbh_model(kx, ky, gamma_x=0.5, gamma_y=0.5, lambda_x=1, lambda_y=1): | ||||
|     import numpy as np | ||||
|     import cmath | ||||
|     # label of atoms in a unit cell | ||||
|     # (2) —— (0) | ||||
|     #  |      | | ||||
|     # (1) —— (3)    | ||||
|     hamiltonian = np.zeros((4, 4), dtype=complex) | ||||
|     hamiltonian[0, 2] = gamma_x+lambda_x*cmath.exp(1j*kx) | ||||
|     hamiltonian[1, 3] = gamma_x+lambda_x*cmath.exp(-1j*kx) | ||||
|     hamiltonian[0, 3] = gamma_y+lambda_y*cmath.exp(1j*ky) | ||||
|     hamiltonian[1, 2] = -gamma_y-lambda_y*cmath.exp(-1j*ky) | ||||
|     hamiltonian[2, 0] = np.conj(hamiltonian[0, 2]) | ||||
|     hamiltonian[3, 1] = np.conj(hamiltonian[1, 3]) | ||||
|     hamiltonian[3, 0] = np.conj(hamiltonian[0, 3]) | ||||
|     hamiltonian[2, 1] = np.conj(hamiltonian[1, 2]) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
|  | ||||
| # Kagome模型的哈密顿量 | ||||
| def hamiltonian_of_kagome_lattice(kx, ky, t=1): | ||||
|     import numpy as np | ||||
|     import math | ||||
|     k1_dot_a1 = kx | ||||
|     k2_dot_a2 = kx/2+ky*math.sqrt(3)/2 | ||||
|     k3_dot_a3 = -kx/2+ky*math.sqrt(3)/2 | ||||
|     hamiltonian = np.zeros((3, 3), dtype=complex) | ||||
|     hamiltonian[0, 1] = 2*math.cos(k1_dot_a1) | ||||
|     hamiltonian[0, 2] = 2*math.cos(k2_dot_a2) | ||||
|     hamiltonian[1, 2] = 2*math.cos(k3_dot_a3) | ||||
|     hamiltonian = hamiltonian + hamiltonian.transpose().conj() | ||||
|     hamiltonian = -t*hamiltonian | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return hamiltonian | ||||
| @@ -2,15 +2,11 @@ | ||||
|  | ||||
| from .basic_functions import * | ||||
| from .Fourier_transform import * | ||||
| from .Hamiltonian_of_finite_size_systems import * | ||||
| from .Hamiltonian_of_models_in_reciprocal_space import * | ||||
| from .Hamiltonian_of_examples import * | ||||
| from .band_structures_and_wave_functions import * | ||||
| from .Green_functions import * | ||||
| from .density_of_states import * | ||||
| from .quantum_transport import * | ||||
| from .topological_invariant import * | ||||
| from .plot_figures import * | ||||
| from .read_and_write import * | ||||
| from .file_processing import * | ||||
| from .data_processing import * | ||||
| from .others import * | ||||
| @@ -154,7 +154,7 @@ def find_vector_array_with_fixed_gauge_by_making_one_component_real(vector_array | ||||
|     guan.statistics_of_guan_package() | ||||
|     return vector_array | ||||
|  | ||||
| # 旋转两个简并的波函数(说明:参数比较多,效率不高) | ||||
| # 旋转两个简并的波函数(说明:参数比较多,算法效率不高) | ||||
| def rotation_of_degenerate_vectors(vector1, vector2, index1=None, index2=None, precision=0.01, criterion=0.01, show_theta=0): | ||||
|     import numpy as np | ||||
|     import math | ||||
| @@ -185,7 +185,7 @@ def rotation_of_degenerate_vectors(vector1, vector2, index1=None, index2=None, p | ||||
|     guan.statistics_of_guan_package() | ||||
|     return vector1, vector2 | ||||
|  | ||||
| # 旋转两个简并的波函数向量组(说明:参数比较多,效率不高) | ||||
| # 旋转两个简并的波函数向量组(说明:参数比较多,算法效率不高) | ||||
| def rotation_of_degenerate_vectors_array(vector1_array, vector2_array, precision=0.01, criterion=0.01, show_theta=0): | ||||
|     import numpy as np | ||||
|     import guan | ||||
| @@ -201,4 +201,35 @@ def rotation_of_degenerate_vectors_array(vector1_array, vector2_array, precision | ||||
|     for i0 in range(Num_k): | ||||
|         vector1_array[i0], vector2_array[i0] = guan.rotation_of_degenerate_vectors(vector1=vector1_array[i0], vector2=vector2_array[i0], index1=index1, index2=index2, precision=precision, criterion=criterion, show_theta=show_theta) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return vector1_array, vector2_array | ||||
|     return vector1_array, vector2_array | ||||
|  | ||||
| # 在一组数据中找到数值相近的数 | ||||
| def find_close_values_in_one_array(array, precision=1e-2): | ||||
|     new_array = [] | ||||
|     i0 = 0 | ||||
|     for a1 in array: | ||||
|         j0 = 0 | ||||
|         for a2 in array: | ||||
|             if j0>i0 and abs(a1-a2)<precision:  | ||||
|                 new_array.append([a1, a2]) | ||||
|             j0 +=1 | ||||
|         i0 += 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return new_array | ||||
|  | ||||
| # 寻找能带的简并点 | ||||
| def find_degenerate_points(k_array, eigenvalue_array, precision=1e-2): | ||||
|     import guan | ||||
|     degenerate_k_array = [] | ||||
|     degenerate_eigenvalue_array = [] | ||||
|     i0 = 0 | ||||
|     for k in k_array: | ||||
|         degenerate_points = guan.find_close_values_in_one_array(eigenvalue_array[i0], precision=precision) | ||||
|         if len(degenerate_points) != 0: | ||||
|             degenerate_k_array.append(k) | ||||
|             degenerate_eigenvalue_array.append(degenerate_points) | ||||
|         i0 += 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return degenerate_k_array, degenerate_eigenvalue_array | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,727 +0,0 @@ | ||||
| # Module: file_processing | ||||
|  | ||||
| # 自动先后运行程序(串行) | ||||
| def run_programs_sequentially(program_files=['./a.py', './b.py'], execute='python ', show_time=0): | ||||
|     import os | ||||
|     import time | ||||
|     if show_time == 1: | ||||
|         start = time.time() | ||||
|     i0 = 0 | ||||
|     for program_file in program_files: | ||||
|         i0 += 1 | ||||
|         if show_time == 1: | ||||
|             start_0 = time.time() | ||||
|         os.system(execute+program_file) | ||||
|         if show_time == 1: | ||||
|             end_0 = time.time() | ||||
|             print('Running time of program_'+str(i0)+' = '+str((end_0-start_0)/60)+' min') | ||||
|     if show_time == 1: | ||||
|         end = time.time() | ||||
|         print('Total running time = '+str((end-start)/60)+' min') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 如果不存在文件夹,则新建文件夹 | ||||
| def make_directory(directory='./test'): | ||||
|     import os | ||||
|     if not os.path.exists(directory): | ||||
|         os.makedirs(directory) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 复制一份文件 | ||||
| def copy_file(file1='./a.txt', file2='./b.txt'): | ||||
|     import shutil | ||||
|     shutil.copy(file1, file2) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 拼接两个PDF文件 | ||||
| def combine_two_pdf_files(input_file_1='a.pdf', input_file_2='b.pdf', output_file='combined_file.pdf'): | ||||
|     import PyPDF2 | ||||
|     output_pdf = PyPDF2.PdfWriter() | ||||
|     with open(input_file_1, 'rb') as file1: | ||||
|         pdf1 = PyPDF2.PdfReader(file1) | ||||
|         for page in range(len(pdf1.pages)): | ||||
|             output_pdf.add_page(pdf1.pages[page]) | ||||
|     with open(input_file_2, 'rb') as file2: | ||||
|         pdf2 = PyPDF2.PdfReader(file2) | ||||
|         for page in range(len(pdf2.pages)): | ||||
|             output_pdf.add_page(pdf2.pages[page]) | ||||
|     with open(output_file, 'wb') as combined_file: | ||||
|         output_pdf.write(combined_file) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 查找文件名相同的文件 | ||||
| def find_repeated_file_with_same_filename(directory='./', ignored_directory_with_words=[], ignored_file_with_words=[], num=1000): | ||||
|     import os | ||||
|     from collections import Counter | ||||
|     file_list = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             file_list.append(files[i0]) | ||||
|             for word in ignored_directory_with_words: | ||||
|                 if word in root: | ||||
|                     file_list.remove(files[i0])        | ||||
|             for word in ignored_file_with_words: | ||||
|                 if word in files[i0]: | ||||
|                     try: | ||||
|                         file_list.remove(files[i0])    | ||||
|                     except: | ||||
|                         pass  | ||||
|     count_file = Counter(file_list).most_common(num) | ||||
|     repeated_file = [] | ||||
|     for item in count_file: | ||||
|         if item[1]>1: | ||||
|             repeated_file.append(item) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return repeated_file | ||||
|  | ||||
| # 统计各个子文件夹中的文件数量 | ||||
| def count_file_in_sub_directory(directory='./', sort=0, reverse=1, print_show=1, smaller_than_num=None): | ||||
|     import os | ||||
|     import numpy as np | ||||
|     dirs_list = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         if dirs != []: | ||||
|             for i0 in range(len(dirs)): | ||||
|                 dirs_list.append(root+'/'+dirs[i0]) | ||||
|     count_file_array = [] | ||||
|     for sub_dir in dirs_list: | ||||
|         file_list = [] | ||||
|         for root, dirs, files in os.walk(sub_dir): | ||||
|             for i0 in range(len(files)): | ||||
|                 file_list.append(files[i0]) | ||||
|         count_file = len(file_list) | ||||
|         count_file_array.append(count_file) | ||||
|         if sort == 0: | ||||
|             if print_show == 1: | ||||
|                 if smaller_than_num == None: | ||||
|                     print(sub_dir) | ||||
|                     print(count_file) | ||||
|                     print() | ||||
|                 else: | ||||
|                     if count_file<smaller_than_num: | ||||
|                         print(sub_dir) | ||||
|                         print(count_file) | ||||
|                         print() | ||||
|     if sort == 0: | ||||
|         sub_directory = dirs_list | ||||
|         num_in_sub_directory = count_file_array | ||||
|     if sort == 1: | ||||
|         sub_directory = [] | ||||
|         num_in_sub_directory = [] | ||||
|         if reverse == 1: | ||||
|             index_array = np.argsort(count_file_array)[::-1] | ||||
|         else: | ||||
|             index_array = np.argsort(count_file_array) | ||||
|         for i0 in index_array: | ||||
|             sub_directory.append(dirs_list[i0]) | ||||
|             num_in_sub_directory.append(count_file_array[i0]) | ||||
|             if print_show == 1: | ||||
|                 if smaller_than_num == None: | ||||
|                     print(dirs_list[i0]) | ||||
|                     print(count_file_array[i0]) | ||||
|                     print() | ||||
|                 else: | ||||
|                     if count_file_array[i0]<smaller_than_num: | ||||
|                         print(dirs_list[i0]) | ||||
|                         print(count_file_array[i0]) | ||||
|                         print() | ||||
|      | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return sub_directory, num_in_sub_directory | ||||
|  | ||||
| # 改变当前的目录位置 | ||||
| def change_directory_by_replacement(current_key_word='code', new_key_word='data'): | ||||
|     import os | ||||
|     code_path = os.getcwd() | ||||
|     data_path = code_path.replace('\\', '/')  | ||||
|     data_path = data_path.replace(current_key_word, new_key_word)  | ||||
|     if os.path.exists(data_path) == False: | ||||
|         os.makedirs(data_path) | ||||
|     os.chdir(data_path) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在多个子文件夹中产生必要的文件,例如 readme.md | ||||
| def creat_necessary_file(directory, filename='readme', file_format='.md', content='', overwrite=None, ignored_directory_with_words=[]): | ||||
|     import os | ||||
|     directory_with_file = [] | ||||
|     ignored_directory = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if root not in directory_with_file: | ||||
|                 directory_with_file.append(root) | ||||
|             if files[i0] == filename+file_format: | ||||
|                 if root not in ignored_directory: | ||||
|                     ignored_directory.append(root) | ||||
|     if overwrite == None: | ||||
|         for root in ignored_directory: | ||||
|             directory_with_file.remove(root) | ||||
|     ignored_directory_more =[] | ||||
|     for root in directory_with_file:  | ||||
|         for word in ignored_directory_with_words: | ||||
|             if word in root: | ||||
|                 if root not in ignored_directory_more: | ||||
|                     ignored_directory_more.append(root) | ||||
|     for root in ignored_directory_more: | ||||
|         directory_with_file.remove(root)  | ||||
|     for root in directory_with_file: | ||||
|         os.chdir(root) | ||||
|         f = open(filename+file_format, 'w', encoding="utf-8") | ||||
|         f.write(content) | ||||
|         f.close() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 删除特定文件名的文件(慎用) | ||||
| def delete_file_with_specific_name(directory, filename='readme', file_format='.md'): | ||||
|     import os | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if files[i0] == filename+file_format: | ||||
|                 os.remove(root+'/'+files[i0]) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 所有文件移到根目录(慎用) | ||||
| def move_all_files_to_root_directory(directory): | ||||
|     import os | ||||
|     import shutil | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             shutil.move(root+'/'+files[i0], directory+'/'+files[i0]) | ||||
|     for i0 in range(100): | ||||
|         for root, dirs, files in os.walk(directory): | ||||
|             try: | ||||
|                 os.rmdir(root)  | ||||
|             except: | ||||
|                 pass | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将文件目录结构写入Markdown文件 | ||||
| def write_file_list_in_markdown(directory='./', filename='a', reverse_positive_or_negative=1, starting_from_h1=None, banned_file_format=[], hide_file_format=None, divided_line=None, show_second_number=None, show_third_number=None):  | ||||
|     import os | ||||
|     f = open(filename+'.md', 'w', encoding="utf-8") | ||||
|     filenames1 = os.listdir(directory) | ||||
|     u0 = 0 | ||||
|     for filename1 in filenames1[::reverse_positive_or_negative]: | ||||
|         filename1_with_path = os.path.join(directory,filename1)  | ||||
|         if os.path.isfile(filename1_with_path): | ||||
|             if os.path.splitext(filename1)[1] not in banned_file_format: | ||||
|                 if hide_file_format == None: | ||||
|                     f.write('+ '+str(filename1)+'\n\n') | ||||
|                 else: | ||||
|                     f.write('+ '+str(os.path.splitext(filename1)[0])+'\n\n') | ||||
|         else: | ||||
|             u0 += 1 | ||||
|             if divided_line != None and u0 != 1: | ||||
|                 f.write('--------\n\n') | ||||
|             if starting_from_h1 == None: | ||||
|                 f.write('#') | ||||
|             f.write('# '+str(filename1)+'\n\n') | ||||
|  | ||||
|             filenames2 = os.listdir(filename1_with_path)  | ||||
|             i0 = 0      | ||||
|             for filename2 in filenames2[::reverse_positive_or_negative]: | ||||
|                 filename2_with_path = os.path.join(directory, filename1, filename2)  | ||||
|                 if os.path.isfile(filename2_with_path): | ||||
|                     if os.path.splitext(filename2)[1] not in banned_file_format: | ||||
|                         if hide_file_format == None: | ||||
|                             f.write('+ '+str(filename2)+'\n\n') | ||||
|                         else: | ||||
|                             f.write('+ '+str(os.path.splitext(filename2)[0])+'\n\n') | ||||
|                 else:  | ||||
|                     i0 += 1 | ||||
|                     if starting_from_h1 == None: | ||||
|                         f.write('#') | ||||
|                     if show_second_number != None: | ||||
|                         f.write('## '+str(i0)+'. '+str(filename2)+'\n\n') | ||||
|                     else: | ||||
|                         f.write('## '+str(filename2)+'\n\n') | ||||
|                      | ||||
|                     j0 = 0 | ||||
|                     filenames3 = os.listdir(filename2_with_path) | ||||
|                     for filename3 in filenames3[::reverse_positive_or_negative]: | ||||
|                         filename3_with_path = os.path.join(directory, filename1, filename2, filename3)  | ||||
|                         if os.path.isfile(filename3_with_path):  | ||||
|                             if os.path.splitext(filename3)[1] not in banned_file_format: | ||||
|                                 if hide_file_format == None: | ||||
|                                     f.write('+ '+str(filename3)+'\n\n') | ||||
|                                 else: | ||||
|                                     f.write('+ '+str(os.path.splitext(filename3)[0])+'\n\n') | ||||
|                         else: | ||||
|                             j0 += 1 | ||||
|                             if starting_from_h1 == None: | ||||
|                                 f.write('#') | ||||
|                             if show_third_number != None: | ||||
|                                 f.write('### ('+str(j0)+') '+str(filename3)+'\n\n') | ||||
|                             else: | ||||
|                                 f.write('### '+str(filename3)+'\n\n') | ||||
|  | ||||
|                             filenames4 = os.listdir(filename3_with_path) | ||||
|                             for filename4 in filenames4[::reverse_positive_or_negative]: | ||||
|                                 filename4_with_path = os.path.join(directory, filename1, filename2, filename3, filename4)  | ||||
|                                 if os.path.isfile(filename4_with_path): | ||||
|                                     if os.path.splitext(filename4)[1] not in banned_file_format: | ||||
|                                         if hide_file_format == None: | ||||
|                                             f.write('+ '+str(filename4)+'\n\n') | ||||
|                                         else: | ||||
|                                             f.write('+ '+str(os.path.splitext(filename4)[0])+'\n\n') | ||||
|                                 else:  | ||||
|                                     if starting_from_h1 == None: | ||||
|                                         f.write('#') | ||||
|                                     f.write('#### '+str(filename4)+'\n\n') | ||||
|  | ||||
|                                     filenames5 = os.listdir(filename4_with_path) | ||||
|                                     for filename5 in filenames5[::reverse_positive_or_negative]: | ||||
|                                         filename5_with_path = os.path.join(directory, filename1, filename2, filename3, filename4, filename5)  | ||||
|                                         if os.path.isfile(filename5_with_path):  | ||||
|                                             if os.path.splitext(filename5)[1] not in banned_file_format: | ||||
|                                                 if hide_file_format == None: | ||||
|                                                     f.write('+ '+str(filename5)+'\n\n') | ||||
|                                                 else: | ||||
|                                                     f.write('+ '+str(os.path.splitext(filename5)[0])+'\n\n') | ||||
|                                         else: | ||||
|                                             if starting_from_h1 == None: | ||||
|                                                 f.write('#') | ||||
|                                             f.write('##### '+str(filename5)+'\n\n') | ||||
|  | ||||
|                                             filenames6 = os.listdir(filename5_with_path) | ||||
|                                             for filename6 in filenames6[::reverse_positive_or_negative]: | ||||
|                                                 filename6_with_path = os.path.join(directory, filename1, filename2, filename3, filename4, filename5, filename6)  | ||||
|                                                 if os.path.isfile(filename6_with_path):  | ||||
|                                                     if os.path.splitext(filename6)[1] not in banned_file_format: | ||||
|                                                         if hide_file_format == None: | ||||
|                                                             f.write('+ '+str(filename6)+'\n\n') | ||||
|                                                         else: | ||||
|                                                             f.write('+ '+str(os.path.splitext(filename6)[0])+'\n\n') | ||||
|                                                 else: | ||||
|                                                     if starting_from_h1 == None: | ||||
|                                                         f.write('#') | ||||
|                                                     f.write('###### '+str(filename6)+'\n\n') | ||||
|     f.close() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 从网页的标签中获取内容 | ||||
| def get_html_from_tags(link, tags=['title', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'li', 'a']): | ||||
|     from bs4 import BeautifulSoup | ||||
|     import urllib.request | ||||
|     import ssl | ||||
|     ssl._create_default_https_context = ssl._create_unverified_context | ||||
|     html = urllib.request.urlopen(link).read().decode('utf-8') | ||||
|     soup = BeautifulSoup(html, features="lxml") | ||||
|     all_tags = soup.find_all(tags) | ||||
|     content = '' | ||||
|     for tag in all_tags: | ||||
|         text = tag.get_text().replace('\n', '') | ||||
|         if content == '': | ||||
|             content = text | ||||
|         else: | ||||
|             content = content + '\n\n' + text | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return content | ||||
|  | ||||
| # 生成二维码 | ||||
| def creat_qrcode(data="https://www.guanjihuan.com", filename='a', file_format='.png'): | ||||
|     import qrcode | ||||
|     img = qrcode.make(data) | ||||
|     img.save(filename+file_format) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将PDF文件转成文本 | ||||
| def pdf_to_text(pdf_path): | ||||
|     from pdfminer.pdfparser import PDFParser, PDFDocument | ||||
|     from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter | ||||
|     from pdfminer.converter import PDFPageAggregator | ||||
|     from pdfminer.layout import LAParams, LTTextBox | ||||
|     from pdfminer.pdfinterp import PDFTextExtractionNotAllowed | ||||
|     import logging  | ||||
|     logging.Logger.propagate = False  | ||||
|     logging.getLogger().setLevel(logging.ERROR)  | ||||
|     praser = PDFParser(open(pdf_path, 'rb')) | ||||
|     doc = PDFDocument() | ||||
|     praser.set_document(doc) | ||||
|     doc.set_parser(praser) | ||||
|     doc.initialize() | ||||
|     if not doc.is_extractable: | ||||
|         raise PDFTextExtractionNotAllowed | ||||
|     else: | ||||
|         rsrcmgr = PDFResourceManager() | ||||
|         laparams = LAParams() | ||||
|         device = PDFPageAggregator(rsrcmgr, laparams=laparams) | ||||
|         interpreter = PDFPageInterpreter(rsrcmgr, device) | ||||
|         content = '' | ||||
|         for page in doc.get_pages(): | ||||
|             interpreter.process_page(page)                         | ||||
|             layout = device.get_result()                      | ||||
|             for x in layout: | ||||
|                 if isinstance(x, LTTextBox): | ||||
|                     content  = content + x.get_text().strip() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return content | ||||
|  | ||||
| # 获取PDF文件页数 | ||||
| def get_pdf_page_number(pdf_path): | ||||
|     import PyPDF2 | ||||
|     pdf_file = open(pdf_path, 'rb') | ||||
|     pdf_reader = PyPDF2.PdfReader(pdf_file) | ||||
|     num_pages = len(pdf_reader.pages) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return num_pages | ||||
|  | ||||
| # 获取PDF文件指定页面的内容 | ||||
| def pdf_to_txt_for_a_specific_page(pdf_path, page_num=1): | ||||
|     import PyPDF2 | ||||
|     pdf_file = open(pdf_path, 'rb') | ||||
|     pdf_reader = PyPDF2.PdfReader(pdf_file) | ||||
|     num_pages = len(pdf_reader.pages) | ||||
|     for page_num0 in range(num_pages): | ||||
|         if page_num0 == page_num-1: | ||||
|             page = pdf_reader.pages[page_num0] | ||||
|             page_text = page.extract_text() | ||||
|     pdf_file.close() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return page_text | ||||
|  | ||||
| # 获取PDF文献中的链接。例如: link_starting_form='https://doi.org' | ||||
| def get_links_from_pdf(pdf_path, link_starting_form=''): | ||||
|     import PyPDF2 | ||||
|     import re | ||||
|     pdfReader = PyPDF2.PdfFileReader(pdf_path) | ||||
|     pages = pdfReader.getNumPages() | ||||
|     i0 = 0 | ||||
|     links = [] | ||||
|     for page in range(pages): | ||||
|         pageSliced = pdfReader.getPage(page) | ||||
|         pageObject = pageSliced.getObject() | ||||
|         if '/Annots' in pageObject.keys(): | ||||
|             ann = pageObject['/Annots'] | ||||
|             old = '' | ||||
|             for a in ann: | ||||
|                 u = a.getObject() | ||||
|                 if '/A' in u.keys(): | ||||
|                     if re.search(re.compile('^'+link_starting_form), u['/A']['/URI']): | ||||
|                         if u['/A']['/URI'] != old: | ||||
|                             links.append(u['/A']['/URI'])  | ||||
|                             i0 += 1 | ||||
|                             old = u['/A']['/URI']         | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return links | ||||
|  | ||||
| # 通过Sci-Hub网站下载文献 | ||||
| def download_with_scihub(address=None, num=1): | ||||
|     from bs4 import BeautifulSoup | ||||
|     import re | ||||
|     import requests | ||||
|     import os | ||||
|     if num==1 and address!=None: | ||||
|         address_array = [address] | ||||
|     else: | ||||
|         address_array = [] | ||||
|         for i in range(num): | ||||
|             address = input('\nInput:') | ||||
|             address_array.append(address) | ||||
|     for address in address_array: | ||||
|         r = requests.post('https://sci-hub.st/', data={'request': address}) | ||||
|         print('\nResponse:', r) | ||||
|         print('Address:', r.url) | ||||
|         soup = BeautifulSoup(r.text, features='lxml') | ||||
|         pdf_URL = soup.embed['src'] | ||||
|         # pdf_URL = soup.iframe['src'] # This is a code line of history version which fails to get pdf URL. | ||||
|         if re.search(re.compile('^https:'), pdf_URL): | ||||
|             pass | ||||
|         else: | ||||
|             pdf_URL = 'https:'+pdf_URL | ||||
|         print('PDF address:', pdf_URL) | ||||
|         name = re.search(re.compile('fdp.*?/'),pdf_URL[::-1]).group()[::-1][1::] | ||||
|         print('PDF name:', name) | ||||
|         print('Directory:', os.getcwd()) | ||||
|         print('\nDownloading...') | ||||
|         r = requests.get(pdf_URL, stream=True) | ||||
|         with open(name, 'wb') as f: | ||||
|             for chunk in r.iter_content(chunk_size=32): | ||||
|                 f.write(chunk) | ||||
|         print('Completed!\n') | ||||
|     if num != 1: | ||||
|         print('All completed!\n') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将文本转成音频 | ||||
| def str_to_audio(str='hello world', filename='str', rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0): | ||||
|     import pyttsx3 | ||||
|     import guan | ||||
|     if print_text==1: | ||||
|         print(str) | ||||
|     engine = pyttsx3.init() | ||||
|     voices = engine.getProperty('voices')   | ||||
|     engine.setProperty('voice', voices[voice].id) | ||||
|     engine.setProperty("rate", rate) | ||||
|     if save==1: | ||||
|         engine.save_to_file(str, filename+'.wav') | ||||
|         engine.runAndWait() | ||||
|         print('Wav file saved!') | ||||
|         if compress==1: | ||||
|             import os | ||||
|             os.rename(filename+'.wav', 'temp.wav') | ||||
|             guan.compress_wav_to_mp3('temp.wav', output_filename=filename+'.mp3', bitrate=bitrate) | ||||
|             os.remove('temp.wav') | ||||
|     if read==1: | ||||
|         engine.say(str) | ||||
|         engine.runAndWait() | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将txt文件转成音频 | ||||
| def txt_to_audio(txt_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0): | ||||
|     import pyttsx3 | ||||
|     import guan | ||||
|     f = open(txt_path, 'r', encoding ='utf-8') | ||||
|     text = f.read() | ||||
|     if print_text==1: | ||||
|         print(text) | ||||
|     engine = pyttsx3.init() | ||||
|     voices = engine.getProperty('voices')   | ||||
|     engine.setProperty('voice', voices[voice].id) | ||||
|     engine.setProperty("rate", rate) | ||||
|     if save==1: | ||||
|         import re | ||||
|         filename = re.split('[/,\\\]', txt_path)[-1][:-4] | ||||
|         engine.save_to_file(text, filename+'.wav') | ||||
|         engine.runAndWait() | ||||
|         print('Wav file saved!') | ||||
|         if compress==1: | ||||
|             import os | ||||
|             os.rename(filename+'.wav', 'temp.wav') | ||||
|             guan.compress_wav_to_mp3('temp.wav', output_filename=filename+'.mp3', bitrate=bitrate) | ||||
|             os.remove('temp.wav') | ||||
|     if read==1: | ||||
|         engine.say(text) | ||||
|         engine.runAndWait() | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将PDF文件转成音频 | ||||
| def pdf_to_audio(pdf_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0): | ||||
|     import pyttsx3 | ||||
|     import guan | ||||
|     text = guan.pdf_to_text(pdf_path) | ||||
|     text = text.replace('\n', ' ') | ||||
|     if print_text==1: | ||||
|         print(text) | ||||
|     engine = pyttsx3.init() | ||||
|     voices = engine.getProperty('voices')   | ||||
|     engine.setProperty('voice', voices[voice].id) | ||||
|     engine.setProperty("rate", rate) | ||||
|     if save==1: | ||||
|         import re | ||||
|         filename = re.split('[/,\\\]', pdf_path)[-1][:-4] | ||||
|         engine.save_to_file(text, filename+'.wav') | ||||
|         engine.runAndWait() | ||||
|         print('Wav file saved!') | ||||
|         if compress==1: | ||||
|             import os | ||||
|             os.rename(filename+'.wav', 'temp.wav') | ||||
|             guan.compress_wav_to_mp3('temp.wav', output_filename=filename+'.mp3', bitrate=bitrate) | ||||
|             os.remove('temp.wav') | ||||
|     if read==1: | ||||
|         engine.say(text) | ||||
|         engine.runAndWait() | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 将wav音频文件压缩成MP3音频文件 | ||||
| def compress_wav_to_mp3(wav_path, output_filename='a.mp3', bitrate='16k'): | ||||
|     # Note: Beside the installation of pydub, you may also need download FFmpeg on http://www.ffmpeg.org/download.html and add the bin path to the environment variable. | ||||
|     from pydub import AudioSegment | ||||
|     sound = AudioSegment.from_mp3(wav_path) | ||||
|     sound.export(output_filename,format="mp3",bitrate=bitrate) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 播放学术单词 | ||||
| def play_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_translation=1, show_link=1, translation_time=2, rest_time=1): | ||||
|     from bs4 import BeautifulSoup | ||||
|     import re | ||||
|     import urllib.request | ||||
|     import requests | ||||
|     import os | ||||
|     import pygame | ||||
|     import time | ||||
|     import ssl | ||||
|     import random | ||||
|     ssl._create_default_https_context = ssl._create_unverified_context | ||||
|     html = urllib.request.urlopen("https://www.guanjihuan.com/archives/4418").read().decode('utf-8') | ||||
|     if bre_or_ame == 'ame': | ||||
|         directory = 'words_mp3_ameProns/' | ||||
|     elif bre_or_ame == 'bre': | ||||
|         directory = 'words_mp3_breProns/' | ||||
|     exist_directory = os.path.exists(directory) | ||||
|     html_file = urllib.request.urlopen("https://file.guanjihuan.com/words/"+directory).read().decode('utf-8') | ||||
|     if exist_directory == 0: | ||||
|         os.makedirs(directory) | ||||
|     soup = BeautifulSoup(html, features='lxml') | ||||
|     contents = re.findall('<h2.*?</a></p>', html, re.S) | ||||
|     if random_on==1: | ||||
|         random.shuffle(contents) | ||||
|     if reverse==1: | ||||
|         contents.reverse() | ||||
|     for content in contents: | ||||
|         soup2 = BeautifulSoup(content, features='lxml') | ||||
|         all_h2 = soup2.find_all('h2') | ||||
|         for h2 in all_h2: | ||||
|             if re.search('\d*. ', h2.get_text()): | ||||
|                 word = re.findall('[a-zA-Z].*', h2.get_text(), re.S)[0] | ||||
|                 exist = os.path.exists(directory+word+'.mp3') | ||||
|                 if not exist: | ||||
|                     try: | ||||
|                         if re.search(word, html_file): | ||||
|                             r = requests.get("https://file.guanjihuan.com/words/"+directory+word+".mp3", stream=True) | ||||
|                             with open(directory+word+'.mp3', 'wb') as f: | ||||
|                                 for chunk in r.iter_content(chunk_size=32): | ||||
|                                     f.write(chunk) | ||||
|                     except: | ||||
|                         pass | ||||
|                 print(h2.get_text()) | ||||
|                 try: | ||||
|                     pygame.mixer.init() | ||||
|                     track = pygame.mixer.music.load(directory+word+'.mp3') | ||||
|                     pygame.mixer.music.play() | ||||
|                     if show_link==1: | ||||
|                         print('https://www.ldoceonline.com/dictionary/'+word) | ||||
|                 except: | ||||
|                     pass | ||||
|                 translation = re.findall('<p>.*?</p>', content, re.S)[0][3:-4] | ||||
|                 if show_translation==1: | ||||
|                     time.sleep(translation_time) | ||||
|                     print(translation) | ||||
|                 time.sleep(rest_time) | ||||
|                 pygame.mixer.music.stop() | ||||
|                 print() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 播放挑选过后的学术单词 | ||||
| def play_selected_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_link=1, rest_time=3): | ||||
|     from bs4 import BeautifulSoup | ||||
|     import re | ||||
|     import urllib.request | ||||
|     import requests | ||||
|     import os | ||||
|     import pygame | ||||
|     import time | ||||
|     import ssl | ||||
|     import random | ||||
|     ssl._create_default_https_context = ssl._create_unverified_context | ||||
|     html = urllib.request.urlopen("https://www.guanjihuan.com/archives/24732").read().decode('utf-8') | ||||
|     if bre_or_ame == 'ame': | ||||
|         directory = 'words_mp3_ameProns/' | ||||
|     elif bre_or_ame == 'bre': | ||||
|         directory = 'words_mp3_breProns/' | ||||
|     exist_directory = os.path.exists(directory) | ||||
|     html_file = urllib.request.urlopen("https://file.guanjihuan.com/words/"+directory).read().decode('utf-8') | ||||
|     if exist_directory == 0: | ||||
|         os.makedirs(directory) | ||||
|     soup = BeautifulSoup(html, features='lxml') | ||||
|     contents = re.findall('<li>\d.*?</li>', html, re.S) | ||||
|     if random_on==1: | ||||
|         random.shuffle(contents) | ||||
|     if reverse==1: | ||||
|         contents.reverse() | ||||
|     for content in contents: | ||||
|         soup2 = BeautifulSoup(content, features='lxml') | ||||
|         all_li = soup2.find_all('li') | ||||
|         for li in all_li: | ||||
|             if re.search('\d*. ', li.get_text()): | ||||
|                 word = re.findall('\s[a-zA-Z].*?\s', li.get_text(), re.S)[0][1:-1] | ||||
|                 exist = os.path.exists(directory+word+'.mp3') | ||||
|                 if not exist: | ||||
|                     try: | ||||
|                         if re.search(word, html_file): | ||||
|                             r = requests.get("https://file.guanjihuan.com/words/"+directory+word+".mp3", stream=True) | ||||
|                             with open(directory+word+'.mp3', 'wb') as f: | ||||
|                                 for chunk in r.iter_content(chunk_size=32): | ||||
|                                     f.write(chunk) | ||||
|                     except: | ||||
|                         pass | ||||
|                 print(li.get_text()) | ||||
|                 try: | ||||
|                     pygame.mixer.init() | ||||
|                     track = pygame.mixer.music.load(directory+word+'.mp3') | ||||
|                     pygame.mixer.music.play() | ||||
|                     if show_link==1: | ||||
|                         print('https://www.ldoceonline.com/dictionary/'+word) | ||||
|                 except: | ||||
|                     pass | ||||
|                 time.sleep(rest_time) | ||||
|                 pygame.mixer.music.stop() | ||||
|                 print() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 播放元素周期表上的单词 | ||||
| def play_element_words(random_on=0, show_translation=1, show_link=1, translation_time=2, rest_time=1): | ||||
|     from bs4 import BeautifulSoup | ||||
|     import re | ||||
|     import urllib.request | ||||
|     import requests | ||||
|     import os | ||||
|     import pygame | ||||
|     import time | ||||
|     import ssl | ||||
|     import random | ||||
|     ssl._create_default_https_context = ssl._create_unverified_context | ||||
|     html = urllib.request.urlopen("https://www.guanjihuan.com/archives/10897").read().decode('utf-8') | ||||
|     directory = 'prons/' | ||||
|     exist_directory = os.path.exists(directory) | ||||
|     html_file = urllib.request.urlopen("https://file.guanjihuan.com/words/periodic_table_of_elements/"+directory).read().decode('utf-8') | ||||
|     if exist_directory == 0: | ||||
|         os.makedirs(directory) | ||||
|     soup = BeautifulSoup(html, features='lxml') | ||||
|     contents = re.findall('<h2.*?</a></p>', html, re.S) | ||||
|     if random_on==1: | ||||
|         random.shuffle(contents) | ||||
|     for content in contents: | ||||
|         soup2 = BeautifulSoup(content, features='lxml') | ||||
|         all_h2 = soup2.find_all('h2') | ||||
|         for h2 in all_h2: | ||||
|             if re.search('\d*. ', h2.get_text()): | ||||
|                 word = re.findall('[a-zA-Z].* \(', h2.get_text(), re.S)[0][:-2] | ||||
|                 exist = os.path.exists(directory+word+'.mp3') | ||||
|                 if not exist: | ||||
|                     try: | ||||
|                         if re.search(word, html_file): | ||||
|                             r = requests.get("https://file.guanjihuan.com/words/periodic_table_of_elements/prons/"+word+".mp3", stream=True) | ||||
|                             with open(directory+word+'.mp3', 'wb') as f: | ||||
|                                 for chunk in r.iter_content(chunk_size=32): | ||||
|                                     f.write(chunk) | ||||
|                     except: | ||||
|                         pass | ||||
|                 print(h2.get_text()) | ||||
|                 try: | ||||
|                     pygame.mixer.init() | ||||
|                     track = pygame.mixer.music.load(directory+word+'.mp3') | ||||
|                     pygame.mixer.music.play() | ||||
|                     if show_link==1: | ||||
|                         print('https://www.merriam-webster.com/dictionary/'+word) | ||||
|                 except: | ||||
|                     pass | ||||
|                 translation = re.findall('<p>.*?</p>', content, re.S)[0][3:-4] | ||||
|                 if show_translation==1: | ||||
|                     time.sleep(translation_time) | ||||
|                     print(translation) | ||||
|                 time.sleep(rest_time) | ||||
|                 pygame.mixer.music.stop() | ||||
|                 print() | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,413 +0,0 @@ | ||||
| # Module: plot_figures | ||||
|  | ||||
| # 导入plt, fig, ax | ||||
| def import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20): | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=adjust_bottom, left=adjust_left) | ||||
|     ax.grid() | ||||
|     ax.tick_params(labelsize=labelsize)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return plt, fig, ax | ||||
|  | ||||
| # 基于plt, fig, ax开始画图 | ||||
| def plot_without_starting_fig(plt, fig, ax, x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, style='', y_min=None, y_max=None, linewidth=None, markersize=None, color=None):  | ||||
|     if color==None: | ||||
|         ax.plot(x_array, y_array, style, linewidth=linewidth, markersize=markersize) | ||||
|     else: | ||||
|         ax.plot(x_array, y_array, style, linewidth=linewidth, markersize=markersize, color=color) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y_min=min(y_array) | ||||
|         if y_max==None: | ||||
|             y_max=max(y_array) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 画图 | ||||
| def plot(x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style='', y_min=None, y_max=None, linewidth=None, markersize=None, adjust_bottom=0.2, adjust_left=0.2):  | ||||
|     import guan | ||||
|     plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize) | ||||
|     ax.plot(x_array, y_array, style, linewidth=linewidth, markersize=markersize) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y_min=min(y_array) | ||||
|         if y_max==None: | ||||
|             y_max=max(y_array) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 一组横坐标数据,两组纵坐标数据画图 | ||||
| def plot_two_array(x_array, y1_array, y2_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, markersize_1=None, markersize_2=None, adjust_bottom=0.2, adjust_left=0.2):  | ||||
|     import guan | ||||
|     plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)  | ||||
|     ax.plot(x_array, y1_array, style_1, linewidth=linewidth_1, markersize=markersize_1) | ||||
|     ax.plot(x_array, y2_array, style_2, linewidth=linewidth_2, markersize=markersize_2) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y1_min=min(y1_array) | ||||
|             y2_min=min(y2_array) | ||||
|             y_min=min([y1_min, y2_min]) | ||||
|         if y_max==None: | ||||
|             y1_max=max(y1_array) | ||||
|             y2_max=max(y2_array) | ||||
|             y_max=max([y1_max, y2_max]) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 两组横坐标数据,两组纵坐标数据画图 | ||||
| def plot_two_array_with_two_horizontal_array(x1_array, x2_array, y1_array, y2_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, markersize_1=None, markersize_2=None, adjust_bottom=0.2, adjust_left=0.2):  | ||||
|     import guan | ||||
|     plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)  | ||||
|     ax.plot(x1_array, y1_array, style_1, linewidth=linewidth_1, markersize=markersize_1) | ||||
|     ax.plot(x2_array, y2_array, style_2, linewidth=linewidth_2, markersize=markersize_2) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y1_min=min(y1_array) | ||||
|             y2_min=min(y2_array) | ||||
|             y_min=min([y1_min, y2_min]) | ||||
|         if y_max==None: | ||||
|             y1_max=max(y1_array) | ||||
|             y2_max=max(y2_array) | ||||
|             y_max=max([y1_max, y2_max]) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 一组横坐标数据,三组纵坐标数据画图 | ||||
| def plot_three_array(x_array, y1_array, y2_array, y3_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', style_3='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, linewidth_3=None,markersize_1=None, markersize_2=None, markersize_3=None, adjust_bottom=0.2, adjust_left=0.2):  | ||||
|     import guan | ||||
|     plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)  | ||||
|     ax.plot(x_array, y1_array, style_1, linewidth=linewidth_1, markersize=markersize_1) | ||||
|     ax.plot(x_array, y2_array, style_2, linewidth=linewidth_2, markersize=markersize_2) | ||||
|     ax.plot(x_array, y3_array, style_3, linewidth=linewidth_3, markersize=markersize_3) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y1_min=min(y1_array) | ||||
|             y2_min=min(y2_array) | ||||
|             y3_min=min(y3_array) | ||||
|             y_min=min([y1_min, y2_min, y3_min]) | ||||
|         if y_max==None: | ||||
|             y1_max=max(y1_array) | ||||
|             y2_max=max(y2_array) | ||||
|             y3_max=max(y3_array) | ||||
|             y_max=max([y1_max, y2_max, y3_max]) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 三组横坐标数据,三组纵坐标数据画图 | ||||
| def plot_three_array_with_three_horizontal_array(x1_array, x2_array, x3_array, y1_array, y2_array, y3_array, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=20, show=1, save=0, filename='a', file_format='.jpg', dpi=300, style_1='', style_2='', style_3='', y_min=None, y_max=None, linewidth_1=None, linewidth_2=None, linewidth_3=None,markersize_1=None, markersize_2=None, markersize_3=None, adjust_bottom=0.2, adjust_left=0.2):  | ||||
|     import guan | ||||
|     plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=adjust_bottom, adjust_left=adjust_left, labelsize=labelsize)  | ||||
|     ax.plot(x1_array, y1_array, style_1, linewidth=linewidth_1, markersize=markersize_1) | ||||
|     ax.plot(x2_array, y2_array, style_2, linewidth=linewidth_2, markersize=markersize_2) | ||||
|     ax.plot(x3_array, y3_array, style_3, linewidth=linewidth_3, markersize=markersize_3) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     if y_min!=None or y_max!=None: | ||||
|         if y_min==None: | ||||
|             y1_min=min(y1_array) | ||||
|             y2_min=min(y2_array) | ||||
|             y3_min=min(y3_array) | ||||
|             y_min=min([y1_min, y2_min, y3_min]) | ||||
|         if y_max==None: | ||||
|             y1_max=max(y1_array) | ||||
|             y2_max=max(y2_array) | ||||
|             y3_max=max(y3_array) | ||||
|             y_max=max([y1_max, y2_max, y3_max]) | ||||
|         ax.set_ylim(y_min, y_max) | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 画三维图 | ||||
| def plot_3d_surface(x_array, y_array, matrix, xlabel='x', ylabel='y', zlabel='z', title='', fontsize=20, labelsize=15, show=1, save=0, filename='a', file_format='.jpg', dpi=300, z_min=None, z_max=None, rcount=100, ccount=100):  | ||||
|     import numpy as np | ||||
|     import matplotlib.pyplot as plt | ||||
|     from matplotlib import cm | ||||
|     from matplotlib.ticker import LinearLocator | ||||
|     matrix = np.array(matrix) | ||||
|     fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) | ||||
|     plt.subplots_adjust(bottom=0.1, right=0.65)  | ||||
|     x_array, y_array = np.meshgrid(x_array, y_array) | ||||
|     if len(matrix.shape) == 2: | ||||
|         surf = ax.plot_surface(x_array, y_array, matrix, rcount=rcount, ccount=ccount, cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     elif len(matrix.shape) == 3: | ||||
|         for i0 in range(matrix.shape[2]): | ||||
|             surf = ax.plot_surface(x_array, y_array, matrix[:,:,i0], rcount=rcount, ccount=ccount, cmap=cm.coolwarm, linewidth=0, antialiased=False)  | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_zlabel(zlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.zaxis.set_major_locator(LinearLocator(5))  | ||||
|     ax.zaxis.set_major_formatter('{x:.2f}')   | ||||
|     if z_min!=None or z_max!=None: | ||||
|         if z_min==None: | ||||
|             z_min=matrix.min() | ||||
|         if z_max==None: | ||||
|             z_max=matrix.max() | ||||
|         ax.set_zlim(z_min, z_max) | ||||
|     ax.tick_params(labelsize=labelsize)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() + ax.get_zticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels]  | ||||
|     cax = plt.axes([0.8, 0.1, 0.05, 0.8])  | ||||
|     cbar = fig.colorbar(surf, cax=cax)   | ||||
|     cbar.ax.tick_params(labelsize=labelsize) | ||||
|     for l in cbar.ax.yaxis.get_ticklabels(): | ||||
|         l.set_family('Times New Roman') | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 画Contour图 | ||||
| def plot_contour(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=15, cmap='jet', levels=None, show=1, save=0, filename='a', file_format='.jpg', dpi=300): | ||||
|     import numpy as np | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.2, right=0.75, left=0.2)  | ||||
|     x_array, y_array = np.meshgrid(x_array, y_array) | ||||
|     contour = ax.contourf(x_array,y_array,matrix,cmap=cmap, levels=levels)  | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=labelsize)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     cax = plt.axes([0.8, 0.2, 0.05, 0.68]) | ||||
|     cbar = fig.colorbar(contour, cax=cax)  | ||||
|     cbar.ax.tick_params(labelsize=labelsize)  | ||||
|     for l in cbar.ax.yaxis.get_ticklabels(): | ||||
|         l.set_family('Times New Roman') | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 画棋盘图/伪彩色图 | ||||
| def plot_pcolor(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fontsize=20, labelsize=15, cmap='jet', levels=None, show=1, save=0, filename='a', file_format='.jpg', dpi=300):   | ||||
|     import numpy as np | ||||
|     import matplotlib.pyplot as plt | ||||
|     fig, ax = plt.subplots() | ||||
|     plt.subplots_adjust(bottom=0.2, right=0.75, left=0.2)  | ||||
|     x_array, y_array = np.meshgrid(x_array, y_array) | ||||
|     contour = ax.pcolor(x_array,y_array,matrix, cmap=cmap) | ||||
|     ax.set_title(title, fontsize=fontsize, fontfamily='Times New Roman') | ||||
|     ax.set_xlabel(xlabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.set_ylabel(ylabel, fontsize=fontsize, fontfamily='Times New Roman')  | ||||
|     ax.tick_params(labelsize=labelsize)  | ||||
|     labels = ax.get_xticklabels() + ax.get_yticklabels() | ||||
|     [label.set_fontname('Times New Roman') for label in labels] | ||||
|     cax = plt.axes([0.8, 0.2, 0.05, 0.68]) | ||||
|     cbar = fig.colorbar(contour, cax=cax)  | ||||
|     cbar.ax.tick_params(labelsize=labelsize)  | ||||
|     for l in cbar.ax.yaxis.get_ticklabels(): | ||||
|         l.set_family('Times New Roman') | ||||
|     if save == 1: | ||||
|         plt.savefig(filename+file_format, dpi=dpi)  | ||||
|     if show == 1: | ||||
|         plt.show() | ||||
|     plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 通过坐标画点和线 | ||||
| def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distance=1.1, line_style='-k', linewidth=1, dot_style='ro', markersize=3, show=1, save=0, filename='a', file_format='.eps', dpi=300): | ||||
|     import numpy as np | ||||
|     import matplotlib.pyplot as plt | ||||
|     coordinate_array = np.array(coordinate_array) | ||||
|     print(coordinate_array.shape) | ||||
|     x_range = max(coordinate_array[:, 0])-min(coordinate_array[:, 0]) | ||||
|     y_range = max(coordinate_array[:, 1])-min(coordinate_array[:, 1]) | ||||
|     fig, ax = plt.subplots(figsize=(6*x_range/y_range,6)) | ||||
|     plt.subplots_adjust(left=0, bottom=0, right=1, top=1) | ||||
|     plt.axis('off') | ||||
|     if draw_lines==1: | ||||
|         for i1 in range(coordinate_array.shape[0]): | ||||
|             for i2 in range(coordinate_array.shape[0]): | ||||
|                 if np.sqrt((coordinate_array[i1, 0] - coordinate_array[i2, 0])**2+(coordinate_array[i1, 1] - coordinate_array[i2, 1])**2) < max_distance: | ||||
|                     ax.plot([coordinate_array[i1, 0], coordinate_array[i2, 0]], [coordinate_array[i1, 1], coordinate_array[i2, 1]], line_style, linewidth=linewidth) | ||||
|     if draw_dots==1: | ||||
|         for i in range(coordinate_array.shape[0]): | ||||
|             ax.plot(coordinate_array[i, 0], coordinate_array[i, 1], dot_style, markersize=markersize) | ||||
|     if show==1: | ||||
|         plt.show() | ||||
|     if save==1: | ||||
|         if file_format=='.eps': | ||||
|             plt.savefig(filename+file_format) | ||||
|         else: | ||||
|             plt.savefig(filename+file_format, dpi=dpi) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 合并两个图片 | ||||
| def combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filename='a', file_format='.jpg', dpi=300): | ||||
|     import numpy as np | ||||
|     num = np.array(image_path_array).shape[0] | ||||
|     if num != 2: | ||||
|         print('Error: The number of images should be two!') | ||||
|     else: | ||||
|         import matplotlib.pyplot as plt | ||||
|         import matplotlib.image as mpimg | ||||
|         fig = plt.figure(figsize=figsize) | ||||
|         plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)  | ||||
|         ax1 = fig.add_subplot(121) | ||||
|         ax2 = fig.add_subplot(122) | ||||
|         image_1 = mpimg.imread(image_path_array[0]) | ||||
|         image_2 = mpimg.imread(image_path_array[1]) | ||||
|         ax1.imshow(image_1) | ||||
|         ax2.imshow(image_2) | ||||
|         ax1.axis('off') | ||||
|         ax2.axis('off') | ||||
|         if show == 1: | ||||
|             plt.show() | ||||
|         if save == 1: | ||||
|             plt.savefig(filename+file_format, dpi=dpi) | ||||
|         plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 合并三个图片 | ||||
| def combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filename='a', file_format='.jpg', dpi=300): | ||||
|     import numpy as np | ||||
|     num = np.array(image_path_array).shape[0] | ||||
|     if num != 3: | ||||
|         print('Error: The number of images should be three!') | ||||
|     else: | ||||
|         import matplotlib.pyplot as plt | ||||
|         import matplotlib.image as mpimg | ||||
|         fig = plt.figure(figsize=figsize) | ||||
|         plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)  | ||||
|         ax1 = fig.add_subplot(131) | ||||
|         ax2 = fig.add_subplot(132) | ||||
|         ax3 = fig.add_subplot(133) | ||||
|         image_1 = mpimg.imread(image_path_array[0]) | ||||
|         image_2 = mpimg.imread(image_path_array[1]) | ||||
|         image_3 = mpimg.imread(image_path_array[2]) | ||||
|         ax1.imshow(image_1) | ||||
|         ax2.imshow(image_2) | ||||
|         ax3.imshow(image_3) | ||||
|         ax1.axis('off') | ||||
|         ax2.axis('off') | ||||
|         ax3.axis('off') | ||||
|         if show == 1: | ||||
|             plt.show() | ||||
|         if save == 1: | ||||
|             plt.savefig(filename+file_format, dpi=dpi) | ||||
|         plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 合并四个图片 | ||||
| def combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filename='a', file_format='.jpg', dpi=300): | ||||
|     import numpy as np | ||||
|     num = np.array(image_path_array).shape[0] | ||||
|     if num != 4: | ||||
|         print('Error: The number of images should be four!') | ||||
|     else: | ||||
|         import matplotlib.pyplot as plt | ||||
|         import matplotlib.image as mpimg | ||||
|         fig = plt.figure(figsize=figsize) | ||||
|         plt.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)  | ||||
|         ax1 = fig.add_subplot(221) | ||||
|         ax2 = fig.add_subplot(222) | ||||
|         ax3 = fig.add_subplot(223) | ||||
|         ax4 = fig.add_subplot(224) | ||||
|         image_1 = mpimg.imread(image_path_array[0]) | ||||
|         image_2 = mpimg.imread(image_path_array[1]) | ||||
|         image_3 = mpimg.imread(image_path_array[2]) | ||||
|         image_4 = mpimg.imread(image_path_array[3]) | ||||
|         ax1.imshow(image_1) | ||||
|         ax2.imshow(image_2) | ||||
|         ax3.imshow(image_3) | ||||
|         ax4.imshow(image_4) | ||||
|         ax1.axis('off') | ||||
|         ax2.axis('off') | ||||
|         ax3.axis('off') | ||||
|         ax4.axis('off') | ||||
|         if show == 1: | ||||
|             plt.show() | ||||
|         if save == 1: | ||||
|             plt.savefig(filename+file_format, dpi=dpi) | ||||
|         plt.close('all') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 对于某个目录中的txt文件,批量读取和画图 | ||||
| def batch_reading_and_plotting(directory, xlabel='x', ylabel='y'): | ||||
|     import re | ||||
|     import os | ||||
|     import guan | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for file in files: | ||||
|             if re.search('^txt.', file[::-1]): | ||||
|                 filename = file[:-4] | ||||
|                 x_array, y_array = guan.read_one_dimensional_data(filename=filename) | ||||
|                 guan.plot(x_array, y_array, xlabel=xlabel, ylabel=ylabel, title=filename, show=0, save=1, filename=filename) | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 制作GIF动画 | ||||
| def make_gif(image_path_array, filename='a', duration=0.1): | ||||
|     import imageio | ||||
|     images = [] | ||||
|     for image_path in image_path_array: | ||||
|         im = imageio.imread(image_path) | ||||
|         images.append(im) | ||||
|     imageio.mimsave(filename+'.gif', images, 'GIF', duration=duration) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 选取颜色 | ||||
| def color_matplotlib(): | ||||
|     color_array = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'] | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return color_array | ||||
| @@ -479,7 +479,7 @@ def calculate_scattering_matrix_and_get_information(fermi_energy, h00, h01, leng | ||||
|  | ||||
|     return number_of_active_channels, number_of_evanescent_channels, k_of_right_moving_active_channels, k_of_left_moving_active_channels, velocity_of_right_moving_active_channels, velocity_of_left_moving_active_channels, transmission_matrix_for_active_channels, reflection_matrix_for_active_channels, total_transmission_of_channels, total_conductance, total_reflection_of_channels, sum_of_transmission_and_reflection_of_channels | ||||
|  | ||||
| # 从散射矩阵中,打印出散射矩阵的信息 | ||||
| # 从散射矩阵中打印出散射矩阵的信息 | ||||
| def print_or_write_scattering_matrix_with_information_of_scattering_matrix(number_of_active_channels, number_of_evanescent_channels, k_of_right_moving_active_channels, k_of_left_moving_active_channels, velocity_of_right_moving_active_channels, velocity_of_left_moving_active_channels, transmission_matrix_for_active_channels, reflection_matrix_for_active_channels, total_transmission_of_channels, total_conductance, total_reflection_of_channels, sum_of_transmission_and_reflection_of_channels, print_show=1, write_file=0, filename='a', file_format='.txt'): | ||||
|     if print_show == 1: | ||||
|         print('\nActive channel (left or right) = ', number_of_active_channels) | ||||
|   | ||||
| @@ -1,263 +0,0 @@ | ||||
| # Module: read_and_write | ||||
|  | ||||
| # 将数据存到文件 | ||||
| def dump_data(data, filename, file_format='.txt'): | ||||
|     import pickle | ||||
|     with open(filename+file_format, 'wb') as f: | ||||
|         pickle.dump(data, f) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 从文件中恢复数据到变量 | ||||
| def load_data(filename, file_format='.txt'): | ||||
|     import pickle | ||||
|     with open(filename+file_format, 'rb') as f: | ||||
|         data = pickle.load(f) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return data | ||||
|  | ||||
| # 读取文件中的一维数据(每一行一组x和y) | ||||
| def read_one_dimensional_data(filename='a', file_format='.txt'):  | ||||
|     import numpy as np | ||||
|     f = open(filename+file_format, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x_array = np.array([]) | ||||
|     y_array = np.array([]) | ||||
|     for row in row_list: | ||||
|         column = np.array(row.split())  | ||||
|         if column.shape[0] != 0:   | ||||
|             x_array = np.append(x_array, [float(column[0])], axis=0)   | ||||
|             y_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 y_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(y_array).shape[0] == 0: | ||||
|                 y_array = [y_row] | ||||
|             else: | ||||
|                 y_array = np.append(y_array, [y_row], axis=0) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return x_array, y_array | ||||
|  | ||||
| # 读取文件中的一维数据(每一行一组x和y)(支持复数形式) | ||||
| def read_one_dimensional_complex_data(filename='a', file_format='.txt'):  | ||||
|     import numpy as np | ||||
|     f = open(filename+file_format, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x_array = np.array([]) | ||||
|     y_array = np.array([]) | ||||
|     for row in row_list: | ||||
|         column = np.array(row.split())  | ||||
|         if column.shape[0] != 0:   | ||||
|             x_array = np.append(x_array, [complex(column[0])], axis=0)   | ||||
|             y_row = np.zeros(dim_column-1, dtype=complex) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 y_row[dim0] = complex(column[dim0+1]) | ||||
|             if np.array(y_array).shape[0] == 0: | ||||
|                 y_array = [y_row] | ||||
|             else: | ||||
|                 y_array = np.append(y_array, [y_row], axis=0) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return x_array, y_array | ||||
|  | ||||
| # 读取文件中的二维数据(第一行和列分别为横纵坐标) | ||||
| def read_two_dimensional_data(filename='a', file_format='.txt'):  | ||||
|     import numpy as np | ||||
|     f = open(filename+file_format, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x_array = np.array([]) | ||||
|     y_array = np.array([]) | ||||
|     matrix = np.array([]) | ||||
|     for i0 in range(row_list.shape[0]): | ||||
|         column = np.array(row_list[i0].split())  | ||||
|         if i0 == 0: | ||||
|             x_str = column[1::]  | ||||
|             x_array = np.zeros(x_str.shape[0]) | ||||
|             for i00 in range(x_str.shape[0]): | ||||
|                 x_array[i00] = float(x_str[i00])  | ||||
|         elif column.shape[0] != 0:  | ||||
|             y_array = np.append(y_array, [float(column[0])], axis=0)   | ||||
|             matrix_row = np.zeros(dim_column-1) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 matrix_row[dim0] = float(column[dim0+1]) | ||||
|             if np.array(matrix).shape[0] == 0: | ||||
|                 matrix = [matrix_row] | ||||
|             else: | ||||
|                 matrix = np.append(matrix, [matrix_row], axis=0) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return x_array, y_array, matrix | ||||
|  | ||||
| # 读取文件中的二维数据(第一行和列分别为横纵坐标)(支持复数形式) | ||||
| def read_two_dimensional_complex_data(filename='a', file_format='.txt'):  | ||||
|     import numpy as np | ||||
|     f = open(filename+file_format, 'r') | ||||
|     text = f.read() | ||||
|     f.close() | ||||
|     row_list = np.array(text.split('\n'))  | ||||
|     dim_column = np.array(row_list[0].split()).shape[0]  | ||||
|     x_array = np.array([]) | ||||
|     y_array = np.array([]) | ||||
|     matrix = np.array([]) | ||||
|     for i0 in range(row_list.shape[0]): | ||||
|         column = np.array(row_list[i0].split())  | ||||
|         if i0 == 0: | ||||
|             x_str = column[1::]  | ||||
|             x_array = np.zeros(x_str.shape[0], dtype=complex) | ||||
|             for i00 in range(x_str.shape[0]): | ||||
|                 x_array[i00] = complex(x_str[i00])  | ||||
|         elif column.shape[0] != 0:  | ||||
|             y_array = np.append(y_array, [complex(column[0])], axis=0)   | ||||
|             matrix_row = np.zeros(dim_column-1, dtype=complex) | ||||
|             for dim0 in range(dim_column-1): | ||||
|                 matrix_row[dim0] = complex(column[dim0+1]) | ||||
|             if np.array(matrix).shape[0] == 0: | ||||
|                 matrix = [matrix_row] | ||||
|             else: | ||||
|                 matrix = np.append(matrix, [matrix_row], axis=0) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return x_array, y_array, matrix | ||||
|  | ||||
| # 读取文件中的二维数据(不包括x和y) | ||||
| def read_two_dimensional_data_without_xy_array(filename='a', file_format='.txt'): | ||||
|     import numpy as np | ||||
|     matrix = np.loadtxt(filename+file_format) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return matrix | ||||
|  | ||||
| # 打开文件用于新增内容 | ||||
| def open_file(filename='a', file_format='.txt'): | ||||
|     f = open(filename+file_format, 'a', encoding='UTF-8') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return f | ||||
|  | ||||
| # 在文件中写入一维数据(每一行一组x和y) | ||||
| def write_one_dimensional_data(x_array, y_array, filename='a', file_format='.txt'): | ||||
|     import guan | ||||
|     with open(filename+file_format, 'w', encoding='UTF-8') as f: | ||||
|         guan.write_one_dimensional_data_without_opening_file(x_array, y_array, f) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在文件中写入一维数据(每一行一组x和y)(需要输入文件) | ||||
| def write_one_dimensional_data_without_opening_file(x_array, y_array, f): | ||||
|     import numpy as np | ||||
|     x_array = np.array(x_array) | ||||
|     y_array = np.array(y_array) | ||||
|     i0 = 0 | ||||
|     for x0 in x_array: | ||||
|         f.write(str(x0)+'   ') | ||||
|         if len(y_array.shape) == 1: | ||||
|             f.write(str(y_array[i0])+'\n') | ||||
|         elif len(y_array.shape) == 2: | ||||
|             for j0 in range(y_array.shape[1]): | ||||
|                 f.write(str(y_array[i0, j0])+'   ') | ||||
|             f.write('\n') | ||||
|         i0 += 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在文件中写入二维数据(第一行和列分别为横纵坐标) | ||||
| def write_two_dimensional_data(x_array, y_array, matrix, filename='a', file_format='.txt'): | ||||
|     import guan | ||||
|     with open(filename+file_format, 'w', encoding='UTF-8') as f: | ||||
|         guan.write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f) | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在文件中写入二维数据(第一行和列分别为横纵坐标)(需要输入文件) | ||||
| def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f): | ||||
|     import numpy as np | ||||
|     x_array = np.array(x_array) | ||||
|     y_array = np.array(y_array) | ||||
|     matrix = np.array(matrix) | ||||
|     f.write('0   ') | ||||
|     for x0 in x_array: | ||||
|         f.write(str(x0)+'   ') | ||||
|     f.write('\n') | ||||
|     i0 = 0 | ||||
|     for y0 in y_array: | ||||
|         f.write(str(y0)) | ||||
|         j0 = 0 | ||||
|         for x0 in x_array: | ||||
|             f.write('   '+str(matrix[i0, j0])+'   ') | ||||
|             j0 += 1 | ||||
|         f.write('\n') | ||||
|         i0 += 1 | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在文件中写入二维数据(不包括x和y) | ||||
| def write_two_dimensional_data_without_xy_array(matrix, filename='a', file_format='.txt'): | ||||
|     import guan | ||||
|     with open(filename+file_format, 'w', encoding='UTF-8') as f: | ||||
|         guan.write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f) | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 在文件中写入二维数据(不包括x和y)(需要输入文件) | ||||
| def write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f): | ||||
|     for row in matrix: | ||||
|         for element in row: | ||||
|             f.write(str(element)+'   ') | ||||
|         f.write('\n') | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 以显示编号的样式,打印数组 | ||||
| def print_array_with_index(array, show_index=1, index_type=0): | ||||
|     if show_index==0: | ||||
|         for i0 in array: | ||||
|             print(i0) | ||||
|     else: | ||||
|         if index_type==0: | ||||
|             index = 0 | ||||
|             for i0 in array: | ||||
|                 print(index, i0) | ||||
|                 index += 1 | ||||
|         else: | ||||
|             index = 0 | ||||
|             for i0 in array: | ||||
|                 index += 1 | ||||
|                 print(index, i0) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|  | ||||
| # 读取文件夹中某个文件类型的所有文本文件 | ||||
| def read_text_files_in_directory(directory='./', file_format='.md'): | ||||
|     import os | ||||
|     file_list = [] | ||||
|     for root, dirs, files in os.walk(directory): | ||||
|         for i0 in range(len(files)): | ||||
|             if file_format in files[i0]: | ||||
|                 file_list.append(root+'/'+files[i0]) | ||||
|     content_array = [] | ||||
|     for file in file_list: | ||||
|         with open(file, 'r') as f: | ||||
|             content_array.append(f.read()) | ||||
|     import guan | ||||
|     guan.statistics_of_guan_package() | ||||
|     return file_list, content_array | ||||
|  | ||||
| # 在多个文本文件中查找关键词 | ||||
| def find_words_in_multiple_files(words, directory='./', file_format='.md'): | ||||
|     import guan | ||||
|     file_list, content_array = guan.read_text_files_in_directory(directory=directory, file_format=file_format) | ||||
|     num_files = len(file_list) | ||||
|     file_list_with_words = [] | ||||
|     for i0 in range(num_files): | ||||
|         if words in content_array[i0]: | ||||
|             file_list_with_words.append(file_list[i0]) | ||||
|     guan.statistics_of_guan_package() | ||||
|     return file_list_with_words | ||||
| @@ -1,6 +1,6 @@ | ||||
| ## Guan package | ||||
|  | ||||
| Guan is an open-source python package developed and maintained by https://www.guanjihuan.com/about (Ji-Huan Guan, 关济寰). With this package, you can calculate band structures, density of states, quantum transport and topological invariant of tight-binding models by invoking the functions you need. Other frequently used functions are also integrated in this package, such as figure plotting, file-reading/writing, file processing, data processing. | ||||
| Guan is an open-source python package developed and maintained by https://www.guanjihuan.com/about (Ji-Huan Guan, 关济寰). With this package, you can calculate band structures, density of states, quantum transport and topological invariant of tight-binding models by invoking the functions you need. Other frequently used functions are also integrated, such as figure-plotting and file-reading/writing. | ||||
|  | ||||
| The primary location of this package is on https://py.guanjihuan.com. | ||||
|  | ||||
| @@ -16,17 +16,14 @@ import guan | ||||
|  | ||||
| + basic functions | ||||
| + Fourier transform | ||||
| + Hamiltonian of finite size systems | ||||
| + Hamiltonian of models in reciprocal space | ||||
| + Hamiltonian of examples | ||||
| + band structures and wave functions | ||||
| + Green functions | ||||
| + density of states | ||||
| + quantum transport | ||||
| + topological invariant | ||||
| + plot figures | ||||
| + read and write | ||||
| + file processing | ||||
| + data processing | ||||
| + others | ||||
|  | ||||
| ## About this package | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user