From 88b35643449589ca138e73f3ac468bbd6dabf7e5 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Fri, 24 Feb 2023 07:06:09 +0800 Subject: [PATCH] =?UTF-8?q?0.0.165=5F=E5=AF=B9=E5=87=BD=E6=95=B0=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E4=B8=AD=E6=96=87=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_Reference.py | 150 ++++++++++++++++++++++++++++ PyPI/setup.cfg | 2 +- PyPI/src/guan.egg-info/PKG-INFO | 2 +- PyPI/src/guan/__init__.py | 168 +++++++++++++++++++++++++++++--- 4 files changed, 304 insertions(+), 18 deletions(-) diff --git a/API_Reference.py b/API_Reference.py index fe5ffcb..0383bee 100644 --- a/API_Reference.py +++ b/API_Reference.py @@ -50,260 +50,372 @@ sigma_zz = guan.sigma_zz() # Module 2: Fourier transform +# 通过元胞和跃迁项得到一维的哈密顿量(需要输入k值) hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell, hopping) +# 通过元胞和跃迁项得到二维方格子的哈密顿量(需要输入k值) hamiltonian = guan.two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell, hopping_1, hopping_2) +# 通过元胞和跃迁项得到三维立方格子的哈密顿量(需要输入k值) hamiltonian = guan.three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell, hopping_1, hopping_2, hopping_3) +# 通过元胞和跃迁项得到一维的哈密顿量(返回的哈密顿量为携带k的函数) hamiltonian_function = guan.one_dimensional_fourier_transform_with_k(unit_cell, hopping) +# 通过元胞和跃迁项得到二维方格子的哈密顿量(返回的哈密顿量为携带k的函数) hamiltonian_function = guan.two_dimensional_fourier_transform_for_square_lattice_with_k1_k2(unit_cell, hopping_1, hopping_2) +# 通过元胞和跃迁项得到三维立方格子的哈密顿量(返回的哈密顿量为携带k的函数) hamiltonian_function = guan.three_dimensional_fourier_transform_for_cubic_lattice_with_k1_k2_k3(unit_cell, hopping_1, hopping_2, hopping_3) +# 由实空间格矢得到倒空间格矢(一维) b1 = guan.calculate_one_dimensional_reciprocal_lattice_vector(a1) +# 由实空间格矢得到倒空间格矢(二维) b1, b2 = guan.calculate_two_dimensional_reciprocal_lattice_vectors(a1, a2) +# 由实空间格矢得到倒空间格矢(三维) b1, b2, b3 = guan.calculate_three_dimensional_reciprocal_lattice_vectors(a1, a2, a3) +# 由实空间格矢得到倒空间格矢(一维),这里为符号运算 b1 = guan.calculate_one_dimensional_reciprocal_lattice_vector_with_sympy(a1) +# 由实空间格矢得到倒空间格矢(二维),这里为符号运算 b1, b2 = guan.calculate_two_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2) +# 由实空间格矢得到倒空间格矢(三维),这里为符号运算 b1, b2, b3 = guan.calculate_three_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2, a3) # Module 3: Hamiltonian of finite size systems +# 构建一维的有限尺寸体系哈密顿量(可设置是否为周期边界条件) hamiltonian = guan.hamiltonian_of_finite_size_system_along_one_direction(N, on_site=0, hopping=1, period=0) +# 构建二维的方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) hamiltonian = guan.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) +# 构建三维的立方格子有限尺寸体系哈密顿量(可设置是否为周期边界条件) hamiltonian = guan.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) +# 构建有限尺寸的SSH模型哈密顿量 hamiltonian = guan.hamiltonian_of_finite_size_ssh_model(N, v=0.6, w=1, onsite_1=0, onsite_2=0, period=1) +# 获取Zigzag边的石墨烯条带的元胞间跃迁 hopping = guan.get_hopping_term_of_graphene_ribbon_along_zigzag_direction(N, eta=0) +# 构建有限尺寸的石墨烯哈密顿量(可设置是否为周期边界条件) hamiltonian = guan.hamiltonian_of_finite_size_system_along_two_directions_for_graphene(N1, N2, period_1=0, period_2=0) +# 获取石墨烯有效模型沿着x方向的在位能和跃迁项(其中,动量qy为参数) h00, h01 = guan.get_onsite_and_hopping_terms_of_2d_effective_graphene_along_one_direction(qy, t=1, staggered_potential=0, eta=0, valley_index=0) +# 获取BHZ模型的在位能和跃迁项 H0, H1, H2 = guan.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) +# 获取半个BHZ模型的在位能和跃迁项(自旋向上) H0, H1, H2 = guan.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) +# 获取半个BHZ模型的在位能和跃迁项(自旋向下) H0, H1, H2 = guan.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) # Module 4: Hamiltonian of models in the reciprocal space +# 一维链的哈密顿量 hamiltonian = guan.hamiltonian_of_simple_chain(k) +# 二维方格子的哈密顿量 hamiltonian = guan.hamiltonian_of_square_lattice(k1, k2) +# 准一维方格子条带的哈密顿量 hamiltonian = guan.hamiltonian_of_square_lattice_in_quasi_one_dimension(k, N=10, period=0) +# 三维立方格子的哈密顿量 hamiltonian = guan.hamiltonian_of_cubic_lattice(k1, k2, k3) +# SSH模型的哈密顿量 hamiltonian = guan.hamiltonian_of_ssh_model(k, v=0.6, w=1) +# 石墨烯的哈密顿量 hamiltonian = guan.hamiltonian_of_graphene(k1, k2, staggered_potential=0, t=1, a=1/math.sqrt(3)) +# 石墨烯有效模型的哈密顿量 hamiltonian = guan.effective_hamiltonian_of_graphene(qx, qy, t=1, staggered_potential=0, valley_index=0) +# 石墨烯有效模型离散化后的哈密顿量 hamiltonian = guan.effective_hamiltonian_of_graphene_after_discretization(qx, qy, t=1, staggered_potential=0, valley_index=0) +# 准一维Zigzag边石墨烯条带的哈密顿量 hamiltonian = guan.hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension(k, N=10, M=0, t=1, period=0) +# Haldane模型的哈密顿量 hamiltonian = guan.hamiltonian_of_haldane_model(k1, k2, M=2/3, t1=1, t2=1/3, phi=math.pi/4, a=1/math.sqrt(3)) +# 准一维Haldane模型条带的哈密顿量 hamiltonian = guan.hamiltonian_of_haldane_model_in_quasi_one_dimension(k, N=10, M=2/3, t1=1, t2=1/3, phi=math.pi/4, period=0) +# 一个量子反常霍尔效应的哈密顿量 hamiltonian = guan.hamiltonian_of_one_QAH_model(k1, k2, t1=1, t2=1, t3=0.5, m=-1) +# BHZ模型的哈密顿量 hamiltonian = guan.hamiltonian_of_bhz_model(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, M=-0.01) +# 半BHZ模型的哈密顿量(自旋向上) hamiltonian = guan.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) +# 半BHZ模型的哈密顿量(自旋向下) hamiltonian = guan.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) +# BBH模型的哈密顿量 hamiltonian = guan.hamiltonian_of_bbh_model(kx, ky, gamma_x=0.5, gamma_y=0.5, lambda_x=1, lambda_y=1) +# Kagome模型的哈密顿量 hamiltonian = guan.hamiltonian_of_kagome_lattice(kx, ky, t=1) # Module 5: band structures and wave functions +# 计算哈密顿量的本征值 eigenvalue = guan.calculate_eigenvalue(hamiltonian) +# 输入哈密顿量函数(带一组参数),计算一组参数下的本征值,返回本征值向量组 eigenvalue_array = guan.calculate_eigenvalue_with_one_parameter(x_array, hamiltonian_function, print_show=0) +# 输入哈密顿量函数(带两组参数),计算两组参数下的本征值,返回本征值向量组 eigenvalue_array = guan.calculate_eigenvalue_with_two_parameters(x_array, y_array, hamiltonian_function, print_show=0, print_show_more=0) +# 计算哈密顿量的本征矢 eigenvector = guan.calculate_eigenvector(hamiltonian) +# 通过二分查找的方法获取和相邻波函数一样规范的波函数 vector_target = guan.find_vector_with_the_same_gauge_with_binary_search(vector_target, vector_ref, show_error=1, show_times=0, show_phase=0, n_test=1000, precision=1e-6) +# 通过使得波函数的一个非零分量为实数,得到固定规范的波函数 vector = guan.find_vector_with_fixed_gauge_by_making_one_component_real(vector, precision=0.005, index=None) +# 通过使得波函数的一个非零分量为实数,得到固定规范的波函数(在一组波函数中选取最大的那个分量) vector_array = guan.find_vector_array_with_fixed_gauge_by_making_one_component_real(vector_array, precision=0.005) +# 旋转两个简并的波函数(说明:参数比较多,效率不高) vector1, vector2 = guan.rotation_of_degenerate_vectors(vector1, vector2, index1=None, index2=None, precision=0.01, criterion=0.01, show_theta=0) +# 旋转两个简并的波函数向量组(说明:参数比较多,效率不高) vector1_array, vector2_array = guan.rotation_of_degenerate_vectors_array(vector1_array, vector2_array, precision=0.01, criterion=0.01, show_theta=0) # Module 6: Green functions +# 输入哈密顿量,得到格林函数 green = guan.green_function(fermi_energy, hamiltonian, broadening, self_energy=0) +# 在Dyson方程中的一个中间格林函数G_{nn}^{n} green_nn_n = guan.green_function_nn_n(fermi_energy, h00, h01, green_nn_n_minus, broadening, self_energy=0) +# 在Dyson方程中的一个中间格林函数G_{in}^{n} green_in_n = guan.green_function_in_n(green_in_n_minus, h01, green_nn_n) +# 在Dyson方程中的一个中间格林函数G_{ni}^{n} green_ni_n = guan.green_function_ni_n(green_nn_n, h01, green_ni_n_minus) +# 在Dyson方程中的一个中间格林函数G_{ii}^{n} green_ii_n = guan.green_function_ii_n(green_ii_n_minus, green_in_n_minus, h01, green_nn_n, green_ni_n_minus) +# 计算转移矩阵(该矩阵可以用来计算表面格林函数) transfer = guan.transfer_matrix(fermi_energy, h00, h01) +# 计算电极的表面格林函数 right_lead_surface, left_lead_surface = guan.surface_green_function_of_lead(fermi_energy, h00, h01) +# 计算电极的自能(基于Dyson方程的小矩阵形式) right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) +# 计算电极的自能(基于中心区整体的大矩阵形式) right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead_with_h_LC_and_h_CR(fermi_energy, h00, h01, h_LC, h_CR) +# 计算电极的自能(基于中心区整体的大矩阵形式,可适用于多端电导的计算) self_energy, gamma = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00, h01, h_lead_to_center) +# 计算考虑电极自能后的中心区的格林函数 green, gamma_right, gamma_left = guan.green_function_with_leads(fermi_energy, h00, h01, h_LC, h_CR, center_hamiltonian) +# 计算用于计算局域电流的格林函数G_n G_n = guan.electron_correlation_function_green_n_for_local_current(fermi_energy, h00, h01, h_LC, h_CR, center_hamiltonian) # Module 7: density of states +# 计算体系的总态密度 total_dos = guan.total_density_of_states(fermi_energy, hamiltonian, broadening=0.01) +# 对于不同费米能,计算体系的总态密度 total_dos_array = guan.total_density_of_states_with_fermi_energy_array(fermi_energy_array, hamiltonian, broadening=0.01, print_show=0) +# 计算方格子的局域态密度(其中,哈密顿量的维度为:dim_hamiltonian = N1*N2*internal_degree) local_dos = guan.local_density_of_states_for_square_lattice(fermi_energy, hamiltonian, N1, N2, internal_degree=1, broadening=0.01) +# 计算立方格子的局域态密度(其中,哈密顿量的维度为:dim_hamiltonian = N1*N2*N3*internal_degree) local_dos = guan.local_density_of_states_for_cubic_lattice(fermi_energy, hamiltonian, N1, N2, N3, internal_degree=1, broadening=0.01) +# 利用Dyson方程,计算方格子的局域态密度(其中,h00的维度为:dim_h00 = N2*internal_degree) local_dos = guan.local_density_of_states_for_square_lattice_using_dyson_equation(fermi_energy, h00, h01, N2, N1, internal_degree=1, broadening=0.01) +# 利用Dyson方程,计算立方格子的局域态密度(其中,h00的维度为:dim_h00 = N2*N3*internal_degree) local_dos = guan.local_density_of_states_for_cubic_lattice_using_dyson_equation(fermi_energy, h00, h01, N3, N2, N1, internal_degree=1, broadening=0.01) +# 利用Dyson方程,计算方格子条带(考虑了电极自能)的局域态密度(其中,h00的维度为:dim_h00 = N2*internal_degree) local_dos = guan.local_density_of_states_for_square_lattice_with_self_energy_using_dyson_equation(fermi_energy, h00, h01, N2, N1, right_self_energy, left_self_energy, internal_degree=1, broadening=0.01) # Module 8: quantum transport +# 计算电导 conductance = guan.calculate_conductance(fermi_energy, h00, h01, length=100) +# 计算不同费米能下的电导 conductance_array = guan.calculate_conductance_with_fermi_energy_array(fermi_energy_array, h00, h01, length=100, print_show=0) +# 计算在势垒散射下的电导 conductance = guan.calculate_conductance_with_barrier(fermi_energy, h00, h01, length=100, barrier_length=20, barrier_potential=1) +# 计算在无序散射下的电导 conductance = guan.calculate_conductance_with_disorder(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100, calculation_times=1) +# 计算在无序垂直切片的散射下的电导 conductance = guan.calculate_conductance_with_slice_disorder(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100) +# 计算在无序水平切片的散射下的电导 conductance = guan.calculate_conductance_with_disorder_inside_unit_cell_which_keeps_translational_symmetry(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100) +# 计算在随机空位的散射下的电导 conductance = guan.calculate_conductance_with_random_vacancy(fermi_energy, h00, h01, vacancy_concentration=0.5, vacancy_potential=1e9, length=100) +# 计算在不同无序散射强度下的电导 conductance_array = guan.calculate_conductance_with_disorder_intensity_array(fermi_energy, h00, h01, disorder_intensity_array, disorder_concentration=1.0, length=100, calculation_times=1, print_show=0) +# 计算在不同无序浓度下的电导 conductance_array = guan.calculate_conductance_with_disorder_concentration_array(fermi_energy, h00, h01, disorder_concentration_array, disorder_intensity=2.0, length=100, calculation_times=1, print_show=0) +# 计算在不同无序散射长度下的电导 conductance_array = guan.calculate_conductance_with_scattering_length_array(fermi_energy, h00, h01, length_array, disorder_intensity=2.0, disorder_concentration=1.0, calculation_times=1, print_show=0) +# 计算得到Gamma矩阵和格林函数,用于计算六端口的量子输运 gamma_array, green = guan.get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10) +# 计算六端口的透射矩阵 transmission_matrix = guan.calculate_six_terminal_transmission_matrix(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10) +# 计算从电极1出发的透射系数 transmission_12, transmission_13, transmission_14, transmission_15, transmission_16 = guan.calculate_six_terminal_transmissions_from_lead_1(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10) +# 通过动量k的虚部,判断通道为传播通道还是衰减通道 if_active = guan.if_active_channel(k_of_channel) +# 获取通道的动量和速度,用于计算散射矩阵 k_of_channel, velocity_of_channel, eigenvalue, eigenvector = guan.get_k_and_velocity_of_channel(fermi_energy, h00, h01) +# 获取分类后的动量和速度,以及U和F,用于计算散射矩阵 k_right, k_left, velocity_right, velocity_left, f_right, f_left, u_right, u_left, ind_right_active = guan.get_classified_k_velocity_u_and_f(fermi_energy, h00, h01) +# 计算散射矩阵 transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active = guan.calculate_scattering_matrix(fermi_energy, h00, h01, length=100) +# 从散射矩阵中,获取散射矩阵的信息 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 = guan.information_of_scattering_matrix(transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active) +# 已知h00和h01,计算散射矩阵并获得散射矩阵的信息 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 = guan.calculate_scattering_matrix_and_get_information(fermi_energy, h00, h01, length=100) +# 从散射矩阵中,打印出散射矩阵的信息 guan.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') +# 已知h00和h01,计算散射矩阵并打印出散射矩阵的信息 guan.print_or_write_scattering_matrix(fermi_energy, h00, h01, length=100, print_show=1, write_file=0, filename='a', file_format='.txt') +# 在无序下,计算散射矩阵 transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active = guan.calculate_scattering_matrix_with_disorder(fermi_energy, h00, h01, length=100, disorder_intensity=2.0, disorder_concentration=1.0) +# 在无序下,计算散射矩阵,并获取散射矩阵多次计算的平均信息 transmission_matrix_for_active_channels_averaged, reflection_matrix_for_active_channels_averaged = guan.calculate_scattering_matrix_with_disorder_and_get_averaged_information(fermi_energy, h00, h01, length=100, disorder_intensity=2.0, disorder_concentration=1.0, calculation_times=1) # Module 9: topological invariant +# 通过高效法计算方格子的陈数 chern_number = guan.calculate_chern_number_for_square_lattice_with_efficient_method(hamiltonian_function, precision=100, print_show=0) +# 通过高效法计算方格子的陈数(可计算简并的情况) chern_number = guan.calculate_chern_number_for_square_lattice_with_efficient_method_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], precision=100, print_show=0) +# 通过Wilson loop方法计算方格子的陈数 chern_number = guan.calculate_chern_number_for_square_lattice_with_wilson_loop(hamiltonian_function, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0) +# 通过Wilson loop方法计算方格子的陈数(可计算简并的情况) chern_number = guan.calculate_chern_number_for_square_lattice_with_wilson_loop_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0) +# 通过高效法计算贝利曲率 k_array, berry_curvature_array = guan.calculate_berry_curvature_with_efficient_method(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision=100, print_show=0) +# 通过高效法计算贝利曲率(可计算简并的情况) k_array, berry_curvature_array = guan.calculate_berry_curvature_with_efficient_method_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], k_min=-math.pi, k_max=math.pi, precision=100, print_show=0) +# 通过Wilson loop方法计算贝里曲率 k_array, berry_curvature_array = guan.calculate_berry_curvature_with_wilson_loop(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0) +# 通过Wilson loop方法计算贝里曲率(可计算简并的情况) k_array, berry_curvature_array = guan.calculate_berry_curvature_with_wilson_loop_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], k_min=-math.pi, k_max=math.pi, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0) +# 计算蜂窝格子的陈数(高效法) chern_number = guan.calculate_chern_number_for_honeycomb_lattice(hamiltonian_function, a=1, precision=300, print_show=0) +# 计算Wilson loop wilson_loop_array = guan.calculate_wilson_loop(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision=100, print_show=0) # Module 10: read and write +# 读取文件中的一维数据(每一行一组x和y) x_array, y_array = guan.read_one_dimensional_data(filename='a', file_format='.txt') +# 读取文件中的一维数据(每一行一组x和y)(支持复数形式) x_array, y_array = guan.read_one_dimensional_complex_data(filename='a', file_format='txt') +# 读取文件中的二维数据(第一行和列分别为横纵坐标) x_array, y_array, matrix = guan.read_two_dimensional_data(filename='a', file_format='.txt') +# 读取文件中的二维数据(第一行和列分别为横纵坐标)(支持复数形式) x_array, y_array, matrix = guan.read_two_dimensional_complex_data(filename='a', file_format='.txt') +# 打开文件用于新增内容 f = guan.open_file(filename='a', file_format='.txt') +# 在文件中写入一维数据(每一行一组x和y) guan.write_one_dimensional_data(x_array, y_array, filename='a', file_format='.txt') +# 在文件中写入一维数据(每一行一组x和y)(需要输入文件) guan.write_one_dimensional_data_without_opening_file(x_array, y_array, f) +# 在文件中写入二维数据(第一行和列分别为横纵坐标) guan.write_two_dimensional_data(x_array, y_array, matrix, filename='a', file_format='.txt') +# 在文件中写入二维数据(第一行和列分别为横纵坐标)(需要输入文件) guan.write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f) +# 在文件中写入二维数据(不包括x和y) guan.write_two_dimensional_data_without_xy_array(matrix, filename='a', file_format='.txt') +# 在文件中写入二维数据(不包括x和y)(需要输入文件) guan.write_two_dimensional_data_without_xy_array_and_without_opening_file(matrix, f) +# 以显示编号的样式,打印数组 guan.print_array_with_index(array, show_index=1, index_type=0) @@ -311,90 +423,128 @@ guan.print_array_with_index(array, show_index=1, index_type=0) # Module 11: plot figures +# 导入plt, fig, ax plt, fig, ax = guan.import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20) +# 基于plt, fig, ax开始画图 guan.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) +# 画图 guan.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) +# 一组横坐标数据,两组纵坐标数据画图 guan.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) +# 两组横坐标数据,两组纵坐标数据画图 guan.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) +# 一组横坐标数据,三组纵坐标数据画图 guan.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) +# 三组横坐标数据,三组纵坐标数据画图 guan.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) +# 画三维图 guan.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) +# 画Contour图 guan.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) +# 画棋盘图/伪彩色图 guan.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) +# 通过坐标画点和线 guan.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) +# 合并两个图片 guan.combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filename='a', file_format='.jpg', dpi=300) +# 合并三个图片 guan.combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filename='a', file_format='.jpg', dpi=300) +# 合并四个图片 guan.combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filename='a', file_format='.jpg', dpi=300) +# 制作GIF动画 guan.make_gif(image_path_array, filename='a', duration=0.1) # Module 12: data processing +# 并行计算前的预处理,把参数分成多份 parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, cpus=1, task_index=0) +# 在一组数据中找到数值相近的数 new_array = guan.find_close_values_in_one_array(array, precision=1e-2) +# 寻找能带的简并点 degenerate_k_array, degenerate_eigenvalue_array = guan.find_degenerate_points(k_array, eigenvalue_array, precision=1e-2) +# 对于某个目录中的txt文件,批量读取和画图 guan.batch_reading_and_plotting(directory, xlabel='x', ylabel='y') +# 将RGB转成HEX hex = guan.rgb_to_hex(rgb, pound=1) +# 将HEX转成RGB rgb = guan.hex_to_rgb(hex) # # Module 13: file processing +# 将文件目录结构写入Markdown文件 guan.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) +# 查找文件名相同的文件 repeated_file = guan.find_repeated_file_with_same_filename(directory='./', ignored_directory_with_words=[], ignored_file_with_words=[], num=1000): +# 统计各个子文件夹中的文件数量 guan.count_file_in_sub_directory(directory='./', smaller_than_num=None) +# 产生必要的文件,例如readme.md guan.creat_necessary_file(directory, filename='readme', file_format='.md', content='', overwrite=None, ignored_directory_with_words=[]) +# 删除特定文件名的文件 guan.delete_file_with_specific_name(directory, filename='readme', file_format='.md') +# 所有文件移到根目录(慎用) guan.move_all_files_to_root_directory(directory) +# 改变当前的目录位置 guan.change_directory_by_replacement(current_key_word='code', new_key_word='data') # Module 14: others +# 通过Sci-Hub网站下载文献 guan.download_with_scihub(address=None, num=1) +# 获取PDF文献中的链接。例如: link_starting_form='https://doi.org' links = guan.get_links_from_pdf(pdf_path, link_starting_form='') +# 将PDF文件转成文本 content = guan.pdf_to_text(pdf_path) +# 将文本转成音频 guan.str_to_audio(str='hello world', filename='str', rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0) +# 将txt文件转成音频 guan.txt_to_audio(txt_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0) +# 将PDF文件转成音频 guan.pdf_to_audio(pdf_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0) +# 将wav音频文件压缩成MP3音频文件 guan.compress_wav_to_mp3(wav_path, output_filename='a.mp3', bitrate='16k') +# 播放学术单词 guan.play_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_translation=1, show_link=1, translation_time=2, rest_time=1) +# 播放挑选过后的学术单词 guan.play_selected_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_link=1, rest_time=3) +# 播放元素周期表上的单词 guan.play_element_words(random_on=0, show_translation=1, show_link=1, translation_time=2, rest_time=1) \ No newline at end of file diff --git a/PyPI/setup.cfg b/PyPI/setup.cfg index 3312d31..6c68a20 100644 --- a/PyPI/setup.cfg +++ b/PyPI/setup.cfg @@ -1,7 +1,7 @@ [metadata] # replace with your username: name = guan -version = 0.0.163 +version = 0.0.165 author = guanjihuan author_email = guanjihuan@163.com description = An open source python package diff --git a/PyPI/src/guan.egg-info/PKG-INFO b/PyPI/src/guan.egg-info/PKG-INFO index 4ca6a8a..4311b7d 100644 --- a/PyPI/src/guan.egg-info/PKG-INFO +++ b/PyPI/src/guan.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: guan -Version: 0.0.163 +Version: 0.0.165 Summary: An open source python package Home-page: https://py.guanjihuan.com Author: guanjihuan diff --git a/PyPI/src/guan/__init__.py b/PyPI/src/guan/__init__.py index 79e3e7b..3494763 100644 --- a/PyPI/src/guan/__init__.py +++ b/PyPI/src/guan/__init__.py @@ -2,7 +2,7 @@ # 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 file reading/writing, figure plotting, data processing. -# The current version is guan-0.0.163, updated on February 21, 2023. +# The current version is guan-0.0.165, updated on February 24, 2023. # Installation: pip install --upgrade guan @@ -119,12 +119,14 @@ def sigma_zz(): # Fourier transform for discrete lattices +# 通过元胞和跃迁项得到一维的哈密顿量(需要输入k值) def one_dimensional_fourier_transform(k, unit_cell, hopping): unit_cell = np.array(unit_cell) hopping = np.array(hopping) hamiltonian = unit_cell+hopping*cmath.exp(1j*k)+hopping.transpose().conj()*cmath.exp(-1j*k) return hamiltonian +# 通过元胞和跃迁项得到二维方格子的哈密顿量(需要输入k值) def two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell, hopping_1, hopping_2): unit_cell = np.array(unit_cell) hopping_1 = np.array(hopping_1) @@ -132,6 +134,7 @@ def two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell, hopp hamiltonian = unit_cell+hopping_1*cmath.exp(1j*k1)+hopping_1.transpose().conj()*cmath.exp(-1j*k1)+hopping_2*cmath.exp(1j*k2)+hopping_2.transpose().conj()*cmath.exp(-1j*k2) return hamiltonian +# 通过元胞和跃迁项得到三维立方格子的哈密顿量(需要输入k值) def three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell, hopping_1, hopping_2, hopping_3): unit_cell = np.array(unit_cell) hopping_1 = np.array(hopping_1) @@ -140,16 +143,19 @@ def three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell, hamiltonian = unit_cell+hopping_1*cmath.exp(1j*k1)+hopping_1.transpose().conj()*cmath.exp(-1j*k1)+hopping_2*cmath.exp(1j*k2)+hopping_2.transpose().conj()*cmath.exp(-1j*k2)+hopping_3*cmath.exp(1j*k3)+hopping_3.transpose().conj()*cmath.exp(-1j*k3) return hamiltonian +# 通过元胞和跃迁项得到一维的哈密顿量(返回的哈密顿量为携带k的函数) def one_dimensional_fourier_transform_with_k(unit_cell, hopping): import functools hamiltonian_function = functools.partial(guan.one_dimensional_fourier_transform, unit_cell=unit_cell, hopping=hopping) return hamiltonian_function +# 通过元胞和跃迁项得到二维方格子的哈密顿量(返回的哈密顿量为携带k的函数) def two_dimensional_fourier_transform_for_square_lattice_with_k1_k2(unit_cell, hopping_1, hopping_2): import functools hamiltonian_function = functools.partial(guan.two_dimensional_fourier_transform_for_square_lattice, unit_cell=unit_cell, hopping_1=hopping_1, hopping_2=hopping_2) return hamiltonian_function +# 通过元胞和跃迁项得到三维立方格子的哈密顿量(返回的哈密顿量为携带k的函数) def three_dimensional_fourier_transform_for_cubic_lattice_with_k1_k2_k3(unit_cell, hopping_1, hopping_2, hopping_3): import functools hamiltonian_function = functools.partial(guan.three_dimensional_fourier_transform_for_cubic_lattice, unit_cell=unit_cell, hopping_1=hopping_1, hopping_2=hopping_2, hopping_3=hopping_3) @@ -157,10 +163,12 @@ def three_dimensional_fourier_transform_for_cubic_lattice_with_k1_k2_k3(unit_cel ## calculate reciprocal lattice vectors +# 由实空间格矢得到倒空间格矢(一维) def calculate_one_dimensional_reciprocal_lattice_vector(a1): b1 = 2*np.pi/a1 return b1 +# 由实空间格矢得到倒空间格矢(二维) def calculate_two_dimensional_reciprocal_lattice_vectors(a1, a2): a1 = np.array(a1) a2 = np.array(a2) @@ -173,6 +181,7 @@ def calculate_two_dimensional_reciprocal_lattice_vectors(a1, a2): b2 = np.delete(b2, 2) return b1, b2 +# 由实空间格矢得到倒空间格矢(三维) def calculate_three_dimensional_reciprocal_lattice_vectors(a1, a2, a3): a1 = np.array(a1) a2 = np.array(a2) @@ -182,11 +191,13 @@ def calculate_three_dimensional_reciprocal_lattice_vectors(a1, a2, a3): b3 = 2*np.pi*np.cross(a1, a2)/np.dot(a1, np.cross(a2, a3)) return b1, b2, b3 +# 由实空间格矢得到倒空间格矢(一维),这里为符号运算 def calculate_one_dimensional_reciprocal_lattice_vector_with_sympy(a1): import sympy b1 = 2*sympy.pi/a1 return b1 +# 由实空间格矢得到倒空间格矢(二维),这里为符号运算 def calculate_two_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2): import sympy a1 = sympy.Matrix(1, 3, [a1[0], a1[1], 0]) @@ -200,6 +211,7 @@ def calculate_two_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2): b2 = sympy.Matrix(1, 2, [b2[0], b2[1]]) return b1, b2 +# 由实空间格矢得到倒空间格矢(三维),这里为符号运算 def calculate_three_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2, a3): import sympy cross_a2_a3 = a2.cross(a3) @@ -221,6 +233,7 @@ def calculate_three_dimensional_reciprocal_lattice_vectors_with_sympy(a1, a2, a3 # Module 3: Hamiltonian of finite size systems +# 构建一维的有限尺寸体系哈密顿量(可设置是否为周期边界条件) def hamiltonian_of_finite_size_system_along_one_direction(N, on_site=0, hopping=1, period=0): on_site = np.array(on_site) hopping = np.array(hopping) @@ -239,6 +252,7 @@ def hamiltonian_of_finite_size_system_along_one_direction(N, on_site=0, hopping= hamiltonian[0:dim, (N-1)*dim+0:(N-1)*dim+dim] = hopping.transpose().conj() 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): on_site = np.array(on_site) hopping_1 = np.array(hopping_1) @@ -269,6 +283,7 @@ def hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1 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() 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): on_site = np.array(on_site) hopping_1 = np.array(hopping_1) @@ -315,6 +330,7 @@ def hamiltonian_of_finite_size_system_along_three_directions_for_cubic_lattice(N 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() return hamiltonian +# 构建有限尺寸的SSH模型哈密顿量 def hamiltonian_of_finite_size_ssh_model(N, v=0.6, w=1, onsite_1=0, onsite_2=0, period=1): hamiltonian = np.zeros((2*N, 2*N)) for i in range(N): @@ -330,6 +346,7 @@ def hamiltonian_of_finite_size_ssh_model(N, v=0.6, w=1, onsite_1=0, onsite_2=0, hamiltonian[2*N-1, 0] = w return hamiltonian +# 获取Zigzag边的石墨烯条带的元胞间跃迁 def get_hopping_term_of_graphene_ribbon_along_zigzag_direction(N, eta=0): hopping = np.zeros((4*N, 4*N), dtype=complex) for i0 in range(N): @@ -341,6 +358,7 @@ def get_hopping_term_of_graphene_ribbon_along_zigzag_direction(N, eta=0): hopping[4*i0+2, 4*i0+3] = 1 return hopping +# 构建有限尺寸的石墨烯哈密顿量(可设置是否为周期边界条件) def hamiltonian_of_finite_size_system_along_two_directions_for_graphene(N1, N2, period_1=0, period_2=0): 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) @@ -349,6 +367,7 @@ def hamiltonian_of_finite_size_system_along_two_directions_for_graphene(N1, N2, 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) 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): constant = -np.sqrt(3)/2 h00 = np.zeros((2, 2), dtype=complex) @@ -367,6 +386,7 @@ def get_onsite_and_hopping_terms_of_2d_effective_graphene_along_one_direction(qy h01[1, 0] = constant*t*(1j/2) 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): E_s = C+M-4*(D+B)/(a**2) E_p = C-M-4*(D-B)/(a**2) @@ -398,6 +418,7 @@ def get_onsite_and_hopping_terms_of_bhz_model(A=0.3645/5, B=-0.686/25, C=0, D=-0 H2[3, 2] = -1j*V_sp 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): E_s = C+M-4*(D+B)/(a**2) E_p = C-M-4*(D-B)/(a**2) @@ -419,6 +440,7 @@ def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_up(A=0.3645/5, B=-0. H2[1, 0] = 1j*np.conj(V_sp) 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): E_s = C+M-4*(D+B)/(a**2) E_p = C-M-4*(D-B)/(a**2) @@ -453,14 +475,17 @@ def get_onsite_and_hopping_terms_of_half_bhz_model_for_spin_down(A=0.3645/5, B=- # Module 4: Hamiltonian of models in the reciprocal space +# 一维链的哈密顿量 def hamiltonian_of_simple_chain(k): hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=0, hopping=1) return hamiltonian +# 二维方格子的哈密顿量 def hamiltonian_of_square_lattice(k1, k2): hamiltonian = guan.two_dimensional_fourier_transform_for_square_lattice(k1, k2, unit_cell=0, hopping_1=1, hopping_2=1) return hamiltonian +# 准一维方格子条带的哈密顿量 def hamiltonian_of_square_lattice_in_quasi_one_dimension(k, N=10, period=0): h00 = np.zeros((N, N), dtype=complex) # hopping in a unit cell h01 = np.zeros((N, N), dtype=complex) # hopping between unit cells @@ -475,16 +500,19 @@ def hamiltonian_of_square_lattice_in_quasi_one_dimension(k, N=10, period=0): hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) return hamiltonian +# 三维立方格子的哈密顿量 def hamiltonian_of_cubic_lattice(k1, k2, k3): hamiltonian = guan.three_dimensional_fourier_transform_for_cubic_lattice(k1, k2, k3, unit_cell=0, hopping_1=1, hopping_2=1, hopping_3=1) return hamiltonian +# SSH模型的哈密顿量 def hamiltonian_of_ssh_model(k, v=0.6, w=1): 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) return hamiltonian +# 石墨烯的哈密顿量 def hamiltonian_of_graphene(k1, k2, staggered_potential=0, t=1, a=1/math.sqrt(3)): h0 = np.zeros((2, 2), dtype=complex) # mass term h1 = np.zeros((2, 2), dtype=complex) # nearest hopping @@ -495,6 +523,7 @@ def hamiltonian_of_graphene(k1, k2, staggered_potential=0, t=1, a=1/math.sqrt(3) hamiltonian = h0 + h1 return hamiltonian +# 石墨烯有效模型的哈密顿量 def effective_hamiltonian_of_graphene(qx, qy, t=1, staggered_potential=0, valley_index=0): hamiltonian = np.zeros((2, 2), dtype=complex) hamiltonian[0, 0] = staggered_potential @@ -508,6 +537,7 @@ def effective_hamiltonian_of_graphene(qx, qy, t=1, staggered_potential=0, valley hamiltonian[1, 0] = constant*t*(-qx+1j*qy) return hamiltonian +# 石墨烯有效模型离散化后的哈密顿量 def effective_hamiltonian_of_graphene_after_discretization(qx, qy, t=1, staggered_potential=0, valley_index=0): hamiltonian = np.zeros((2, 2), dtype=complex) hamiltonian[0, 0] = staggered_potential @@ -521,6 +551,7 @@ def effective_hamiltonian_of_graphene_after_discretization(qx, qy, t=1, staggere hamiltonian[1, 0] = constant*t*(-np.sin(qx)+1j*np.sin(qy)) return hamiltonian +# 准一维Zigzag边石墨烯条带的哈密顿量 def hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension(k, N=10, M=0, t=1, period=0): 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 @@ -547,6 +578,7 @@ def hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension(k, N=10, M=0, t=1 hamiltonian = guan.one_dimensional_fourier_transform(k, unit_cell=h00, hopping=h01) return hamiltonian +# Haldane模型的哈密顿量 def hamiltonian_of_haldane_model(k1, k2, M=2/3, t1=1, t2=1/3, phi=math.pi/4, a=1/math.sqrt(3)): h0 = np.zeros((2, 2), dtype=complex) # mass term h1 = np.zeros((2, 2), dtype=complex) # nearest hopping @@ -560,6 +592,7 @@ def hamiltonian_of_haldane_model(k1, k2, M=2/3, t1=1, t2=1/3, phi=math.pi/4, a=1 hamiltonian = h0 + h1 + h2 + h2.transpose().conj() return hamiltonian +# 准一维Haldane模型条带的哈密顿量 def hamiltonian_of_haldane_model_in_quasi_one_dimension(k, N=10, M=2/3, t1=1, t2=1/3, phi=math.pi/4, period=0): 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 @@ -608,6 +641,7 @@ def hamiltonian_of_haldane_model_in_quasi_one_dimension(k, N=10, M=2/3, t1=1, t2 hamiltonian = h00 + h01*cmath.exp(1j*k) + h01.transpose().conj()*cmath.exp(-1j*k) return hamiltonian +# 一个量子反常霍尔效应的哈密顿量 def hamiltonian_of_one_QAH_model(k1, k2, t1=1, t2=1, t3=0.5, m=-1): hamiltonian = np.zeros((2, 2), dtype=complex) hamiltonian[0, 1] = 2*t1*math.cos(k1)-1j*2*t1*math.cos(k2) @@ -616,6 +650,7 @@ def hamiltonian_of_one_QAH_model(k1, k2, t1=1, t2=1, t3=0.5, m=-1): hamiltonian[1, 1] = -(m+2*t3*math.sin(k1)+2*t3*math.sin(k2)+2*t2*math.cos(k1+k2)) 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): hamiltonian = np.zeros((4, 4), dtype=complex) varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) @@ -631,6 +666,7 @@ def hamiltonian_of_bhz_model(kx, ky, A=0.3645/5, B=-0.686/25, C=0, D=-0.512/25, hamiltonian[3, 2] = -np.conj(d1_d2) 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): hamiltonian = np.zeros((2, 2), dtype=complex) varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) @@ -642,6 +678,7 @@ def hamiltonian_of_half_bhz_model_for_spin_up(kx, ky, A=0.3645/5, B=-0.686/25, C hamiltonian[1, 0] = d1_d2 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): hamiltonian = np.zeros((2, 2), dtype=complex) varepsilon = C-2*D*(2-math.cos(kx)-math.cos(ky)) @@ -653,6 +690,7 @@ def hamiltonian_of_half_bhz_model_for_spin_down(kx, ky, A=0.3645/5, B=-0.686/25, hamiltonian[1, 0] = -np.conj(d1_d2) return hamiltonian +# BBH模型的哈密顿量 def hamiltonian_of_bbh_model(kx, ky, gamma_x=0.5, gamma_y=0.5, lambda_x=1, lambda_y=1): # label of atoms in a unit cell # (2) —— (0) @@ -669,6 +707,7 @@ def hamiltonian_of_bbh_model(kx, ky, gamma_x=0.5, gamma_y=0.5, lambda_x=1, lambd hamiltonian[2, 1] = np.conj(hamiltonian[1, 2]) return hamiltonian +# Kagome模型的哈密顿量 def hamiltonian_of_kagome_lattice(kx, ky, t=1): k1_dot_a1 = kx k2_dot_a2 = kx/2+ky*math.sqrt(3)/2 @@ -693,6 +732,7 @@ def hamiltonian_of_kagome_lattice(kx, ky, t=1): ## band structures +# 计算哈密顿量的本征值 def calculate_eigenvalue(hamiltonian): if np.array(hamiltonian).shape==(): eigenvalue = np.real(hamiltonian) @@ -700,6 +740,7 @@ def calculate_eigenvalue(hamiltonian): eigenvalue, eigenvector = np.linalg.eigh(hamiltonian) return eigenvalue +# 输入哈密顿量函数(带一组参数),计算一组参数下的本征值,返回本征值向量组 def calculate_eigenvalue_with_one_parameter(x_array, hamiltonian_function, print_show=0): dim_x = np.array(x_array).shape[0] i0 = 0 @@ -721,6 +762,7 @@ def calculate_eigenvalue_with_one_parameter(x_array, hamiltonian_function, print i0 += 1 return eigenvalue_array +# 输入哈密顿量函数(带两组参数),计算两组参数下的本征值,返回本征值向量组 def calculate_eigenvalue_with_two_parameters(x_array, y_array, hamiltonian_function, print_show=0, print_show_more=0): dim_x = np.array(x_array).shape[0] dim_y = np.array(y_array).shape[0] @@ -754,12 +796,14 @@ def calculate_eigenvalue_with_two_parameters(x_array, y_array, hamiltonian_funct ## wave functions +# 计算哈密顿量的本征矢 def calculate_eigenvector(hamiltonian): eigenvalue, eigenvector = np.linalg.eigh(hamiltonian) return eigenvector ## find vector with the same gauge +# 通过二分查找的方法获取和相邻波函数一样规范的波函数 def find_vector_with_the_same_gauge_with_binary_search(vector_target, vector_ref, show_error=1, show_times=0, show_phase=0, n_test=1000, precision=1e-6): phase_1_pre = 0 phase_2_pre = np.pi @@ -796,6 +840,7 @@ def find_vector_with_the_same_gauge_with_binary_search(vector_target, vector_ref print('Phase=', phase) return vector_target +# 通过使得波函数的一个非零分量为实数,得到固定规范的波函数 def find_vector_with_fixed_gauge_by_making_one_component_real(vector, precision=0.005, index=None): vector = np.array(vector) if index == None: @@ -811,6 +856,7 @@ def find_vector_with_fixed_gauge_by_making_one_component_real(vector, precision= vector = -vector return vector +# 通过使得波函数的一个非零分量为实数,得到固定规范的波函数(在一组波函数中选取最大的那个分量) def find_vector_array_with_fixed_gauge_by_making_one_component_real(vector_array, precision=0.005): vector_sum = 0 Num_k = np.array(vector_array).shape[0] @@ -821,6 +867,7 @@ def find_vector_array_with_fixed_gauge_by_making_one_component_real(vector_array vector_array[i0] = guan.find_vector_with_fixed_gauge_by_making_one_component_real(vector_array[i0], precision=precision, index=index) return vector_array +# 旋转两个简并的波函数(说明:参数比较多,效率不高) def rotation_of_degenerate_vectors(vector1, vector2, index1=None, index2=None, precision=0.01, criterion=0.01, show_theta=0): vector1 = np.array(vector1) vector2 = np.array(vector2) @@ -846,6 +893,7 @@ def rotation_of_degenerate_vectors(vector1, vector2, index1=None, index2=None, p break return vector1, vector2 +# 旋转两个简并的波函数向量组(说明:参数比较多,效率不高) def rotation_of_degenerate_vectors_array(vector1_array, vector2_array, precision=0.01, criterion=0.01, show_theta=0): Num_k = np.array(vector1_array).shape[0] vector1_sum = 0 @@ -868,6 +916,7 @@ def rotation_of_degenerate_vectors_array(vector1_array, vector2_array, precision # Module 6: Green functions +# 输入哈密顿量,得到格林函数 def green_function(fermi_energy, hamiltonian, broadening, self_energy=0): if np.array(hamiltonian).shape==(): dim = 1 @@ -876,6 +925,7 @@ def green_function(fermi_energy, hamiltonian, broadening, self_energy=0): green = np.linalg.inv((fermi_energy+broadening*1j)*np.eye(dim)-hamiltonian-self_energy) return green +# 在Dyson方程中的一个中间格林函数G_{nn}^{n} def green_function_nn_n(fermi_energy, h00, h01, green_nn_n_minus, broadening, self_energy=0): h01 = np.array(h01) if np.array(h00).shape==(): @@ -885,19 +935,23 @@ def green_function_nn_n(fermi_energy, h00, h01, green_nn_n_minus, broadening, se green_nn_n = np.linalg.inv((fermi_energy+broadening*1j)*np.identity(dim)-h00-np.dot(np.dot(h01.transpose().conj(), green_nn_n_minus), h01)-self_energy) return green_nn_n +# 在Dyson方程中的一个中间格林函数G_{in}^{n} def green_function_in_n(green_in_n_minus, h01, green_nn_n): green_in_n = np.dot(np.dot(green_in_n_minus, h01), green_nn_n) return green_in_n +# 在Dyson方程中的一个中间格林函数G_{ni}^{n} def green_function_ni_n(green_nn_n, h01, green_ni_n_minus): h01 = np.array(h01) green_ni_n = np.dot(np.dot(green_nn_n, h01.transpose().conj()), green_ni_n_minus) return green_ni_n +# 在Dyson方程中的一个中间格林函数G_{ii}^{n} def green_function_ii_n(green_ii_n_minus, green_in_n_minus, h01, green_nn_n, green_ni_n_minus): green_ii_n = green_ii_n_minus+np.dot(np.dot(np.dot(np.dot(green_in_n_minus, h01), green_nn_n), h01.transpose().conj()),green_ni_n_minus) return green_ii_n +# 计算转移矩阵(该矩阵可以用来计算表面格林函数) def transfer_matrix(fermi_energy, h00, h01): h01 = np.array(h01) if np.array(h00).shape==(): @@ -911,6 +965,7 @@ def transfer_matrix(fermi_energy, h00, h01): transfer[dim:2*dim, dim:2*dim] = 0 return transfer +# 计算电极的表面格林函数 def surface_green_function_of_lead(fermi_energy, h00, h01): h01 = np.array(h01) if np.array(h00).shape==(): @@ -934,6 +989,7 @@ def surface_green_function_of_lead(fermi_energy, h00, h01): left_lead_surface = np.linalg.inv(fermi_energy*np.identity(dim)-h00-np.dot(np.dot(h01.transpose().conj(), s3), np.linalg.inv(s4))) return right_lead_surface, left_lead_surface +# 计算电极的自能(基于Dyson方程的小矩阵形式) def self_energy_of_lead(fermi_energy, h00, h01): h01 = np.array(h01) right_lead_surface, left_lead_surface = guan.surface_green_function_of_lead(fermi_energy, h00, h01) @@ -943,6 +999,7 @@ def self_energy_of_lead(fermi_energy, h00, h01): gamma_left = (left_self_energy - left_self_energy.transpose().conj())*1j return right_self_energy, left_self_energy, gamma_right, gamma_left +# 计算电极的自能(基于中心区整体的大矩阵形式) def self_energy_of_lead_with_h_LC_and_h_CR(fermi_energy, h00, h01, h_LC, h_CR): h_LC = np.array(h_LC) h_CR = np.array(h_CR) @@ -953,6 +1010,7 @@ def self_energy_of_lead_with_h_LC_and_h_CR(fermi_energy, h00, h01, h_LC, h_CR): gamma_left = (left_self_energy - left_self_energy.transpose().conj())*1j return right_self_energy, left_self_energy, gamma_right, gamma_left +# 计算电极的自能(基于中心区整体的大矩阵形式,可适用于多端电导的计算) def self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00, h01, h_lead_to_center): h_lead_to_center = np.array(h_lead_to_center) right_lead_surface, left_lead_surface = guan.surface_green_function_of_lead(fermi_energy, h00, h01) @@ -960,12 +1018,14 @@ def self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00, h01, h_lead_to_ gamma = (self_energy - self_energy.transpose().conj())*1j return self_energy, gamma +# 计算考虑电极自能后的中心区的格林函数 def green_function_with_leads(fermi_energy, h00, h01, h_LC, h_CR, center_hamiltonian): dim = np.array(center_hamiltonian).shape[0] right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead_with_h_LC_and_h_CR(fermi_energy, h00, h01, h_LC, h_CR) green = np.linalg.inv(fermi_energy*np.identity(dim)-center_hamiltonian-left_self_energy-right_self_energy) return green, gamma_right, gamma_left +# 计算用于计算局域电流的格林函数G_n def electron_correlation_function_green_n_for_local_current(fermi_energy, h00, h01, h_LC, h_CR, center_hamiltonian): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead_with_h_LC_and_h_CR(fermi_energy, h00, h01, h_LC, h_CR) green = guan.green_function(fermi_energy, center_hamiltonian, broadening=0, self_energy=left_self_energy+right_self_energy) @@ -983,11 +1043,13 @@ def electron_correlation_function_green_n_for_local_current(fermi_energy, h00, h # Module 7: density of states +# 计算体系的总态密度 def total_density_of_states(fermi_energy, hamiltonian, broadening=0.01): green = guan.green_function(fermi_energy, hamiltonian, broadening) total_dos = -np.trace(np.imag(green))/math.pi return total_dos +# 对于不同费米能,计算体系的总态密度 def total_density_of_states_with_fermi_energy_array(fermi_energy_array, hamiltonian, broadening=0.01, print_show=0): dim = np.array(fermi_energy_array).shape[0] total_dos_array = np.zeros(dim) @@ -999,8 +1061,8 @@ def total_density_of_states_with_fermi_energy_array(fermi_energy_array, hamilton i0 += 1 return total_dos_array +# 计算方格子的局域态密度(其中,哈密顿量的维度为:dim_hamiltonian = N1*N2*internal_degree) def local_density_of_states_for_square_lattice(fermi_energy, hamiltonian, N1, N2, internal_degree=1, broadening=0.01): - # dim_hamiltonian = N1*N2*internal_degree green = guan.green_function(fermi_energy, hamiltonian, broadening) local_dos = np.zeros((N2, N1)) for i1 in range(N1): @@ -1009,8 +1071,8 @@ def local_density_of_states_for_square_lattice(fermi_energy, hamiltonian, N1, N2 local_dos[i2, i1] = local_dos[i2, i1]-np.imag(green[i1*N2*internal_degree+i2*internal_degree+i, i1*N2*internal_degree+i2*internal_degree+i])/math.pi return local_dos +# 计算立方格子的局域态密度(其中,哈密顿量的维度为:dim_hamiltonian = N1*N2*N3*internal_degree) def local_density_of_states_for_cubic_lattice(fermi_energy, hamiltonian, N1, N2, N3, internal_degree=1, broadening=0.01): - # dim_hamiltonian = N1*N2*N3*internal_degree green = guan.green_function(fermi_energy, hamiltonian, broadening) local_dos = np.zeros((N3, N2, N1)) for i1 in range(N1): @@ -1020,8 +1082,8 @@ def local_density_of_states_for_cubic_lattice(fermi_energy, hamiltonian, N1, N2, local_dos[i3, i2, i1] = local_dos[i3, i2, i1]-np.imag(green[i1*N2*N3*internal_degree+i2*N3*internal_degree+i3*internal_degree+i, i1*N2*N3*internal_degree+i2*N3*internal_degree+i3*internal_degree+i])/math.pi return local_dos +# 利用Dyson方程,计算方格子的局域态密度(其中,h00的维度为:dim_h00 = N2*internal_degree) def local_density_of_states_for_square_lattice_using_dyson_equation(fermi_energy, h00, h01, N2, N1, internal_degree=1, broadening=0.01): - # dim_h00 = N2*internal_degree local_dos = np.zeros((N2, N1)) green_11_1 = guan.green_function(fermi_energy, h00, broadening) for i1 in range(N1): @@ -1050,8 +1112,8 @@ def local_density_of_states_for_square_lattice_using_dyson_equation(fermi_energy local_dos[i2, i1] = local_dos[i2, i1] - np.imag(green_ii_n_minus[i2*internal_degree+i, i2*internal_degree+i])/math.pi return local_dos +# 利用Dyson方程,计算立方格子的局域态密度(其中,h00的维度为:dim_h00 = N2*N3*internal_degree) def local_density_of_states_for_cubic_lattice_using_dyson_equation(fermi_energy, h00, h01, N3, N2, N1, internal_degree=1, broadening=0.01): - # dim_h00 = N2*N3*internal_degree local_dos = np.zeros((N3, N2, N1)) green_11_1 = guan.green_function(fermi_energy, h00, broadening) for i1 in range(N1): @@ -1081,8 +1143,8 @@ def local_density_of_states_for_cubic_lattice_using_dyson_equation(fermi_energy, local_dos[i3, i2, i1] = local_dos[i3, i2, i1] -np.imag(green_ii_n_minus[i2*N3*internal_degree+i3*internal_degree+i, i2*N3*internal_degree+i3*internal_degree+i])/math.pi return local_dos +# 利用Dyson方程,计算方格子条带(考虑了电极自能)的局域态密度(其中,h00的维度为:dim_h00 = N2*internal_degree) def local_density_of_states_for_square_lattice_with_self_energy_using_dyson_equation(fermi_energy, h00, h01, N2, N1, right_self_energy, left_self_energy, internal_degree=1, broadening=0.01): - # dim_h00 = N2*internal_degree local_dos = np.zeros((N2, N1)) green_11_1 = guan.green_function(fermi_energy, h00+left_self_energy, broadening) for i1 in range(N1): @@ -1130,6 +1192,7 @@ def local_density_of_states_for_square_lattice_with_self_energy_using_dyson_equa ## conductance +# 计算电导 def calculate_conductance(fermi_energy, h00, h01, length=100): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) for ix in range(length): @@ -1145,6 +1208,7 @@ def calculate_conductance(fermi_energy, h00, h01, length=100): conductance = np.trace(np.dot(np.dot(np.dot(gamma_left, green_0n_n), gamma_right), green_0n_n.transpose().conj())) return conductance +# 计算不同费米能下的电导 def calculate_conductance_with_fermi_energy_array(fermi_energy_array, h00, h01, length=100, print_show=0): dim = np.array(fermi_energy_array).shape[0] conductance_array = np.zeros(dim) @@ -1156,6 +1220,7 @@ def calculate_conductance_with_fermi_energy_array(fermi_energy_array, h00, h01, i0 += 1 return conductance_array +# 计算在势垒散射下的电导 def calculate_conductance_with_barrier(fermi_energy, h00, h01, length=100, barrier_length=20, barrier_potential=1): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) dim = np.array(h00).shape[0] @@ -1175,6 +1240,7 @@ def calculate_conductance_with_barrier(fermi_energy, h00, h01, length=100, barri conductance = np.trace(np.dot(np.dot(np.dot(gamma_left, green_0n_n), gamma_right), green_0n_n.transpose().conj())) return conductance +# 计算在无序散射下的电导 def calculate_conductance_with_disorder(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100, calculation_times=1): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) dim = np.array(h00).shape[0] @@ -1199,6 +1265,7 @@ def calculate_conductance_with_disorder(fermi_energy, h00, h01, disorder_intensi conductance_averaged = conductance_averaged/calculation_times return conductance +# 计算在无序垂直切片的散射下的电导 def calculate_conductance_with_slice_disorder(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) dim = np.array(h00).shape[0] @@ -1218,6 +1285,7 @@ def calculate_conductance_with_slice_disorder(fermi_energy, h00, h01, disorder_i conductance = np.trace(np.dot(np.dot(np.dot(gamma_left, green_0n_n), gamma_right), green_0n_n.transpose().conj())) return conductance +# 计算在无序水平切片的散射下的电导 def calculate_conductance_with_disorder_inside_unit_cell_which_keeps_translational_symmetry(fermi_energy, h00, h01, disorder_intensity=2.0, disorder_concentration=1.0, length=100): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) dim = np.array(h00).shape[0] @@ -1238,6 +1306,7 @@ def calculate_conductance_with_disorder_inside_unit_cell_which_keeps_translation conductance = np.trace(np.dot(np.dot(np.dot(gamma_left, green_0n_n), gamma_right), green_0n_n.transpose().conj())) return conductance +# 计算在随机空位的散射下的电导 def calculate_conductance_with_random_vacancy(fermi_energy, h00, h01, vacancy_concentration=0.5, vacancy_potential=1e9, length=100): right_self_energy, left_self_energy, gamma_right, gamma_left = guan.self_energy_of_lead(fermi_energy, h00, h01) dim = np.array(h00).shape[0] @@ -1258,6 +1327,7 @@ def calculate_conductance_with_random_vacancy(fermi_energy, h00, h01, vacancy_co conductance = np.trace(np.dot(np.dot(np.dot(gamma_left, green_0n_n), gamma_right), green_0n_n.transpose().conj())) return conductance +# 计算在不同无序散射强度下的电导 def calculate_conductance_with_disorder_intensity_array(fermi_energy, h00, h01, disorder_intensity_array, disorder_concentration=1.0, length=100, calculation_times=1, print_show=0): dim = np.array(disorder_intensity_array).shape[0] conductance_array = np.zeros(dim) @@ -1271,6 +1341,7 @@ def calculate_conductance_with_disorder_intensity_array(fermi_energy, h00, h01, conductance_array = conductance_array/calculation_times return conductance_array +# 计算在不同无序浓度下的电导 def calculate_conductance_with_disorder_concentration_array(fermi_energy, h00, h01, disorder_concentration_array, disorder_intensity=2.0, length=100, calculation_times=1, print_show=0): dim = np.array(disorder_concentration_array).shape[0] conductance_array = np.zeros(dim) @@ -1284,6 +1355,7 @@ def calculate_conductance_with_disorder_concentration_array(fermi_energy, h00, h conductance_array = conductance_array/calculation_times return conductance_array +# 计算在不同无序散射长度下的电导 def calculate_conductance_with_scattering_length_array(fermi_energy, h00, h01, length_array, disorder_intensity=2.0, disorder_concentration=1.0, calculation_times=1, print_show=0): dim = np.array(length_array).shape[0] conductance_array = np.zeros(dim) @@ -1299,13 +1371,12 @@ def calculate_conductance_with_scattering_length_array(fermi_energy, h00, h01, l ## multi-terminal transmission +# 计算得到Gamma矩阵和格林函数,用于计算六端口的量子输运 def get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10): # ---------------- Geometry ---------------- # lead2 lead3 # lead1(L) lead4(R) # lead6 lead5 - - # h00 and h01 in leads h00_for_lead_1 = h00_for_lead_4 h00_for_lead_2 = h00_for_lead_2 h00_for_lead_3 = h00_for_lead_2 @@ -1318,7 +1389,6 @@ def get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_fo h01_for_lead_4 = h01_for_lead_4 h01_for_lead_5 = h01_for_lead_2.transpose().conj() h01_for_lead_6 = h01_for_lead_2.transpose().conj() - # hopping matrix from lead to center h_lead1_to_center = np.zeros((internal_degree*width, internal_degree*width*length), dtype=complex) h_lead2_to_center = np.zeros((internal_degree*width, internal_degree*width*length), dtype=complex) h_lead3_to_center = np.zeros((internal_degree*width, internal_degree*width*length), dtype=complex) @@ -1334,8 +1404,7 @@ def get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_fo h_lead2_to_center[begin_index:end_index, internal_degree*(width*(move+i0)+(width-1))+0:internal_degree*(width*(move+i0)+(width-1))+internal_degree] = h01_for_lead_2.transpose().conj()[begin_index:end_index, begin_index:end_index] h_lead3_to_center[begin_index:end_index, internal_degree*(width*(length-move-1-i0)+(width-1))+0:internal_degree*(width*(length-move-1-i0)+(width-1))+internal_degree] = h01_for_lead_3.transpose().conj()[begin_index:end_index, begin_index:end_index] h_lead5_to_center[begin_index:end_index, internal_degree*(width*(length-move-1-i0)+0)+0:internal_degree*(width*(length-move-1-i0)+0)+internal_degree] = h01_for_lead_5.transpose().conj()[begin_index:end_index, begin_index:end_index] - h_lead6_to_center[begin_index:end_index, internal_degree*(width*(i0+move)+0)+0:internal_degree*(width*(i0+move)+0)+internal_degree] = h01_for_lead_6.transpose().conj()[begin_index:end_index, begin_index:end_index] - # self energy + h_lead6_to_center[begin_index:end_index, internal_degree*(width*(i0+move)+0)+0:internal_degree*(width*(i0+move)+0)+internal_degree] = h01_for_lead_6.transpose().conj()[begin_index:end_index, begin_index:end_index] self_energy1, gamma1 = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00_for_lead_1, h01_for_lead_1, h_lead1_to_center) self_energy2, gamma2 = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00_for_lead_2, h01_for_lead_1, h_lead2_to_center) self_energy3, gamma3 = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00_for_lead_3, h01_for_lead_1, h_lead3_to_center) @@ -1343,13 +1412,12 @@ def get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_fo self_energy5, gamma5 = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00_for_lead_5, h01_for_lead_1, h_lead5_to_center) self_energy6, gamma6 = guan.self_energy_of_lead_with_h_lead_to_center(fermi_energy, h00_for_lead_6, h01_for_lead_1, h_lead6_to_center) gamma_array = [gamma1, gamma2, gamma3, gamma4, gamma5, gamma6] - # Green function green = np.linalg.inv(fermi_energy*np.eye(internal_degree*width*length)-center_hamiltonian-self_energy1-self_energy2-self_energy3-self_energy4-self_energy5-self_energy6) return gamma_array, green +# 计算六端口的透射矩阵 def calculate_six_terminal_transmission_matrix(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10): gamma_array, green = guan.get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width, length, internal_degree, moving_step_of_leads) - # Transmission transmission_matrix = np.zeros((6, 6), dtype=complex) channel_lead_4 = guan.calculate_conductance(fermi_energy, h00_for_lead_4, h01_for_lead_4, length=3) channel_lead_2 = guan.calculate_conductance(fermi_energy, h00_for_lead_2, h01_for_lead_2, length=3) @@ -1369,9 +1437,9 @@ def calculate_six_terminal_transmission_matrix(fermi_energy, h00_for_lead_4, h01 transmission_matrix = np.real(transmission_matrix) return transmission_matrix +# 计算从电极1出发的透射系数 def calculate_six_terminal_transmissions_from_lead_1(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width=10, length=50, internal_degree=1, moving_step_of_leads=10): gamma_array, green = guan.get_gamma_array_and_green_for_six_terminal_transmission(fermi_energy, h00_for_lead_4, h01_for_lead_4, h00_for_lead_2, h01_for_lead_2, center_hamiltonian, width, length, internal_degree, moving_step_of_leads) - # Transmission transmission_12 = np.real(np.trace(np.dot(np.dot(np.dot(gamma_array[0], green), gamma_array[1]), green.transpose().conj()))) transmission_13 = np.real(np.trace(np.dot(np.dot(np.dot(gamma_array[0], green), gamma_array[2]), green.transpose().conj()))) transmission_14 = np.real(np.trace(np.dot(np.dot(np.dot(gamma_array[0], green), gamma_array[3]), green.transpose().conj()))) @@ -1381,6 +1449,7 @@ def calculate_six_terminal_transmissions_from_lead_1(fermi_energy, h00_for_lead_ ## scattering matrix +# 通过动量k的虚部,判断通道为传播通道还是衰减通道 def if_active_channel(k_of_channel): if np.abs(np.imag(k_of_channel))<1e-6: if_active = 1 @@ -1388,6 +1457,7 @@ def if_active_channel(k_of_channel): if_active = 0 return if_active +# 获取通道的动量和速度,用于计算散射矩阵 def get_k_and_velocity_of_channel(fermi_energy, h00, h01): if np.array(h00).shape==(): dim = 1 @@ -1419,6 +1489,7 @@ def get_k_and_velocity_of_channel(fermi_energy, h00, h01): eigenvector = copy.deepcopy(temp) return k_of_channel, velocity_of_channel, eigenvalue, eigenvector +# 获取分类后的动量和速度,以及U和F,用于计算散射矩阵 def get_classified_k_velocity_u_and_f(fermi_energy, h00, h01): if np.array(h00).shape==(): dim = 1 @@ -1468,6 +1539,7 @@ def get_classified_k_velocity_u_and_f(fermi_energy, h00, h01): f_left = np.dot(np.dot(u_left, lambda_matrix_left), np.linalg.inv(u_left)) return k_right, k_left, velocity_right, velocity_left, f_right, f_left, u_right, u_left, ind_right_active +# 计算散射矩阵 def calculate_scattering_matrix(fermi_energy, h00, h01, length=100): h01 = np.array(h01) if np.array(h00).shape==(): @@ -1508,6 +1580,7 @@ def calculate_scattering_matrix(fermi_energy, h00, h01, length=100): print('Error Alert: scattering matrix is not normalized!') return transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active +# 从散射矩阵中,获取散射矩阵的信息 def information_of_scattering_matrix(transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active): if np.array(transmission_matrix).shape==(): dim = 1 @@ -1527,6 +1600,7 @@ def information_of_scattering_matrix(transmission_matrix, reflection_matrix, k_r sum_of_transmission_and_reflection_of_channels = np.sum(np.square(np.abs(transmission_matrix[0:ind_right_active, 0:ind_right_active])), axis=0) + np.sum(np.square(np.abs(reflection_matrix[0:ind_right_active, 0:ind_right_active])), axis=0) 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 +# 已知h00和h01,计算散射矩阵并获得散射矩阵的信息 def calculate_scattering_matrix_and_get_information(fermi_energy, h00, h01, length=100): transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active = guan.calculate_scattering_matrix(fermi_energy, h00, h01, length=length) @@ -1534,6 +1608,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) @@ -1577,6 +1652,7 @@ def print_or_write_scattering_matrix_with_information_of_scattering_matrix(numbe f.write('Total transmission of channels:\n'+str(total_transmission_of_channels)+'\n') f.write('Total conductance = '+str(total_conductance)+'\n') +# 已知h00和h01,计算散射矩阵并打印出散射矩阵的信息 def print_or_write_scattering_matrix(fermi_energy, h00, h01, length=100, print_show=1, write_file=0, filename='a', file_format='.txt'): transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active = guan.calculate_scattering_matrix(fermi_energy, h00, h01, length=length) @@ -1584,6 +1660,7 @@ def print_or_write_scattering_matrix(fermi_energy, h00, h01, length=100, print_s guan.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=print_show, write_file=write_file, filename=filename, file_format=file_format) +# 在无序下,计算散射矩阵 def calculate_scattering_matrix_with_disorder(fermi_energy, h00, h01, length=100, disorder_intensity=2.0, disorder_concentration=1.0): import copy import math @@ -1630,6 +1707,7 @@ def calculate_scattering_matrix_with_disorder(fermi_energy, h00, h01, length=100 print('Error Alert: scattering matrix is not normalized!') return transmission_matrix, reflection_matrix, k_right, k_left, velocity_right, velocity_left, ind_right_active +# 在无序下,计算散射矩阵,并获取散射矩阵多次计算的平均信息 def calculate_scattering_matrix_with_disorder_and_get_averaged_information(fermi_energy, h00, h01, length=100, disorder_intensity=2.0, disorder_concentration=1.0, calculation_times=1): transmission_matrix_for_active_channels_averaged = 0 reflection_matrix_for_active_channels_averaged = 0 @@ -1651,6 +1729,7 @@ def calculate_scattering_matrix_with_disorder_and_get_averaged_information(fermi # Module 9: topological invariant +# 通过高效法计算方格子的陈数 def calculate_chern_number_for_square_lattice_with_efficient_method(hamiltonian_function, precision=100, print_show=0): if np.array(hamiltonian_function(0, 0)).shape==(): dim = 1 @@ -1684,6 +1763,7 @@ def calculate_chern_number_for_square_lattice_with_efficient_method(hamiltonian_ chern_number = chern_number/(2*math.pi*1j) return chern_number +# 通过高效法计算方格子的陈数(可计算简并的情况) def calculate_chern_number_for_square_lattice_with_efficient_method_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], precision=100, print_show=0): delta = 2*math.pi/precision chern_number = 0 @@ -1749,6 +1829,7 @@ def calculate_chern_number_for_square_lattice_with_efficient_method_for_degenera chern_number = chern_number/(2*math.pi*1j) return chern_number +# 通过Wilson loop方法计算方格子的陈数 def calculate_chern_number_for_square_lattice_with_wilson_loop(hamiltonian_function, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0): delta = 2*math.pi/precision_of_plaquettes chern_number = 0 @@ -1790,6 +1871,7 @@ def calculate_chern_number_for_square_lattice_with_wilson_loop(hamiltonian_funct chern_number = chern_number/(2*math.pi) return chern_number +# 通过Wilson loop方法计算方格子的陈数(可计算简并的情况) def calculate_chern_number_for_square_lattice_with_wilson_loop_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0): delta = 2*math.pi/precision_of_plaquettes chern_number = 0 @@ -1850,7 +1932,7 @@ def calculate_chern_number_for_square_lattice_with_wilson_loop_for_degenerate_ca chern_number = chern_number/(2*math.pi) return chern_number - +# 通过高效法计算贝利曲率 def calculate_berry_curvature_with_efficient_method(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision=100, print_show=0): if np.array(hamiltonian_function(0, 0)).shape==(): dim = 1 @@ -1888,6 +1970,7 @@ def calculate_berry_curvature_with_efficient_method(hamiltonian_function, k_min= i0 += 1 return k_array, berry_curvature_array +# 通过高效法计算贝利曲率(可计算简并的情况) def calculate_berry_curvature_with_efficient_method_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], k_min=-math.pi, k_max=math.pi, precision=100, print_show=0): delta = (k_max-k_min)/precision k_array = np.arange(k_min, k_max, delta) @@ -1958,6 +2041,7 @@ def calculate_berry_curvature_with_efficient_method_for_degenerate_case(hamilton i00 += 1 return k_array, berry_curvature_array +# 通过Wilson loop方法计算贝里曲率 def calculate_berry_curvature_with_wilson_loop(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0): if np.array(hamiltonian_function(0, 0)).shape==(): dim = 1 @@ -2007,6 +2091,7 @@ def calculate_berry_curvature_with_wilson_loop(hamiltonian_function, k_min=-math i00 += 1 return k_array, berry_curvature_array +# 通过Wilson loop方法计算贝里曲率(可计算简并的情况) def calculate_berry_curvature_with_wilson_loop_for_degenerate_case(hamiltonian_function, index_of_bands=[0, 1], k_min=-math.pi, k_max=math.pi, precision_of_plaquettes=20, precision_of_wilson_loop=5, print_show=0): delta = (k_max-k_min)/precision_of_plaquettes k_array = np.arange(k_min, k_max, delta) @@ -2071,6 +2156,7 @@ def calculate_berry_curvature_with_wilson_loop_for_degenerate_case(hamiltonian_f i000 += 1 return k_array, berry_curvature_array +# 计算蜂窝格子的陈数(高效法) def calculate_chern_number_for_honeycomb_lattice(hamiltonian_function, a=1, precision=300, print_show=0): if np.array(hamiltonian_function(0, 0)).shape==(): dim = 1 @@ -2109,6 +2195,7 @@ def calculate_chern_number_for_honeycomb_lattice(hamiltonian_function, a=1, prec chern_number = chern_number/(2*math.pi*1j) return chern_number +# 计算Wilson loop def calculate_wilson_loop(hamiltonian_function, k_min=-math.pi, k_max=math.pi, precision=100, print_show=0): k_array = np.linspace(k_min, k_max, precision) dim = np.array(hamiltonian_function(0)).shape[0] @@ -2136,6 +2223,7 @@ def calculate_wilson_loop(hamiltonian_function, k_min=-math.pi, k_max=math.pi, p # Module 10: read and write +# 读取文件中的一维数据(每一行一组x和y) def read_one_dimensional_data(filename='a', file_format='.txt'): f = open(filename+file_format, 'r') text = f.read() @@ -2157,6 +2245,7 @@ def read_one_dimensional_data(filename='a', file_format='.txt'): y_array = np.append(y_array, [y_row], axis=0) return x_array, y_array +# 读取文件中的一维数据(每一行一组x和y)(支持复数形式) def read_one_dimensional_complex_data(filename='a', file_format='.txt'): f = open(filename+file_format, 'r') text = f.read() @@ -2178,6 +2267,7 @@ def read_one_dimensional_complex_data(filename='a', file_format='.txt'): y_array = np.append(y_array, [y_row], axis=0) return x_array, y_array +# 读取文件中的二维数据(第一行和列分别为横纵坐标) def read_two_dimensional_data(filename='a', file_format='.txt'): f = open(filename+file_format, 'r') text = f.read() @@ -2205,6 +2295,7 @@ def read_two_dimensional_data(filename='a', file_format='.txt'): matrix = np.append(matrix, [matrix_row], axis=0) return x_array, y_array, matrix +# 读取文件中的二维数据(第一行和列分别为横纵坐标)(支持复数形式) def read_two_dimensional_complex_data(filename='a', file_format='.txt'): f = open(filename+file_format, 'r') text = f.read() @@ -2232,6 +2323,7 @@ def read_two_dimensional_complex_data(filename='a', file_format='.txt'): matrix = np.append(matrix, [matrix_row], axis=0) return x_array, y_array, matrix +# 打开文件用于新增内容 def open_file(filename='a', file_format='.txt'): try: f = open(filename+file_format, 'a', encoding='UTF-8') @@ -2239,10 +2331,12 @@ def open_file(filename='a', file_format='.txt'): f = open(filename+file_format, 'w', encoding='UTF-8') return f +# 在文件中写入一维数据(每一行一组x和y) def write_one_dimensional_data(x_array, y_array, filename='a', file_format='.txt'): with open(filename+file_format, 'w', encoding='UTF-8') as f: guan.write_one_dimensional_data_without_opening_file(x_array, y_array, f) +# 在文件中写入一维数据(每一行一组x和y)(需要输入文件) def write_one_dimensional_data_without_opening_file(x_array, y_array, f): x_array = np.array(x_array) y_array = np.array(y_array) @@ -2257,10 +2351,12 @@ def write_one_dimensional_data_without_opening_file(x_array, y_array, f): f.write('\n') i0 += 1 +# 在文件中写入二维数据(第一行和列分别为横纵坐标) def write_two_dimensional_data(x_array, y_array, matrix, filename='a', file_format='.txt'): 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) +# 在文件中写入二维数据(第一行和列分别为横纵坐标)(需要输入文件) def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f): x_array = np.array(x_array) y_array = np.array(y_array) @@ -2279,16 +2375,19 @@ def write_two_dimensional_data_without_opening_file(x_array, y_array, matrix, f) f.write('\n') i0 += 1 +# 在文件中写入二维数据(不包括x和y) def write_two_dimensional_data_without_xy_array(matrix, filename='a', file_format='.txt'): 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) +# 在文件中写入二维数据(不包括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') +# 以显示编号的样式,打印数组 def print_array_with_index(array, show_index=1, index_type=0): if show_index==0: for i0 in array: @@ -2315,6 +2414,7 @@ def print_array_with_index(array, show_index=1, index_type=0): # Module 11: 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() @@ -2325,6 +2425,7 @@ def import_plt_and_start_fig_ax(adjust_bottom=0.2, adjust_left=0.2, labelsize=20 [label.set_fontname('Times New Roman') for label in labels] 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) @@ -2340,6 +2441,7 @@ def plot_without_starting_fig(plt, fig, ax, x_array, y_array, xlabel='x', ylabel y_max=max(y_array) ax.set_ylim(y_min, y_max) +# 画图 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): 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) @@ -2358,6 +2460,7 @@ def plot(x_array, y_array, xlabel='x', ylabel='y', title='', fontsize=20, labels plt.show() plt.close('all') +# 一组横坐标数据,两组纵坐标数据画图 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): 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) @@ -2381,6 +2484,7 @@ def plot_two_array(x_array, y1_array, y2_array, xlabel='x', ylabel='y', title='' plt.show() plt.close('all') +# 两组横坐标数据,两组纵坐标数据画图 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): 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) @@ -2404,6 +2508,7 @@ def plot_two_array_with_two_horizontal_array(x1_array, x2_array, y1_array, y2_ar plt.show() plt.close('all') +# 一组横坐标数据,三组纵坐标数据画图 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): 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) @@ -2430,6 +2535,7 @@ def plot_three_array(x_array, y1_array, y2_array, y3_array, xlabel='x', ylabel=' plt.show() plt.close('all') +# 三组横坐标数据,三组纵坐标数据画图 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): 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) @@ -2456,6 +2562,7 @@ def plot_three_array_with_three_horizontal_array(x1_array, x2_array, x3_array, y plt.show() plt.close('all') +# 画三维图 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 matplotlib.pyplot as plt from matplotlib import cm @@ -2495,6 +2602,7 @@ def plot_3d_surface(x_array, y_array, matrix, xlabel='x', ylabel='y', zlabel='z' plt.show() plt.close('all') +# 画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 matplotlib.pyplot as plt fig, ax = plt.subplots() @@ -2518,6 +2626,7 @@ def plot_contour(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', fon plt.show() plt.close('all') +# 画棋盘图/伪彩色图 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 matplotlib.pyplot as plt fig, ax = plt.subplots() @@ -2541,6 +2650,7 @@ def plot_pcolor(x_array, y_array, matrix, xlabel='x', ylabel='y', title='', font plt.show() plt.close('all') +# 通过坐标画点和线 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 matplotlib.pyplot as plt coordinate_array = np.array(coordinate_array) @@ -2566,6 +2676,7 @@ def draw_dots_and_lines(coordinate_array, draw_dots=1, draw_lines=1, max_distanc else: plt.savefig(filename+file_format, dpi=dpi) +# 合并两个图片 def combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filename='a', file_format='.jpg', dpi=300): num = np.array(image_path_array).shape[0] if num != 2: @@ -2589,6 +2700,7 @@ def combine_two_images(image_path_array, figsize=(16,8), show=0, save=1, filenam plt.savefig(filename+file_format, dpi=dpi) plt.close('all') +# 合并三个图片 def combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filename='a', file_format='.jpg', dpi=300): num = np.array(image_path_array).shape[0] if num != 3: @@ -2616,6 +2728,7 @@ def combine_three_images(image_path_array, figsize=(16,5), show=0, save=1, filen plt.savefig(filename+file_format, dpi=dpi) plt.close('all') +# 合并四个图片 def combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filename='a', file_format='.jpg', dpi=300): num = np.array(image_path_array).shape[0] if num != 4: @@ -2647,6 +2760,7 @@ def combine_four_images(image_path_array, figsize=(16,16), show=0, save=1, filen plt.savefig(filename+file_format, dpi=dpi) plt.close('all') +# 制作GIF动画 def make_gif(image_path_array, filename='a', duration=0.1): import imageio images = [] @@ -2667,6 +2781,7 @@ def make_gif(image_path_array, filename='a', duration=0.1): # Module 12: data processing +# 并行计算前的预处理,把参数分成多份 def preprocess_for_parallel_calculations(parameter_array_all, cpus=1, task_index=0): num_all = np.array(parameter_array_all).shape[0] if num_all%cpus == 0: @@ -2680,6 +2795,7 @@ def preprocess_for_parallel_calculations(parameter_array_all, cpus=1, task_index parameter_array = parameter_array_all[task_index*num_parameter:num_all] return parameter_array +# 在一组数据中找到数值相近的数 def find_close_values_in_one_array(array, precision=1e-2): new_array = [] i0 = 0 @@ -2692,6 +2808,7 @@ def find_close_values_in_one_array(array, precision=1e-2): i0 += 1 return new_array +# 寻找能带的简并点 def find_degenerate_points(k_array, eigenvalue_array, precision=1e-2): degenerate_k_array = [] degenerate_eigenvalue_array = [] @@ -2704,6 +2821,7 @@ def find_degenerate_points(k_array, eigenvalue_array, precision=1e-2): i0 += 1 return degenerate_k_array, degenerate_eigenvalue_array +# 对于某个目录中的txt文件,批量读取和画图 def batch_reading_and_plotting(directory, xlabel='x', ylabel='y'): import re import os @@ -2714,12 +2832,14 @@ def batch_reading_and_plotting(directory, xlabel='x', ylabel='y'): 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) +# 将RGB转成HEX def rgb_to_hex(rgb, pound=1): if pound==0: return '%02x%02x%02x' % rgb else: return '#%02x%02x%02x' % rgb +# 将HEX转成RGB def hex_to_rgb(hex): hex = hex.lstrip('#') length = len(hex) @@ -2738,6 +2858,7 @@ def hex_to_rgb(hex): # # Module 13: file processing +# 将文件目录结构写入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") @@ -2840,6 +2961,7 @@ def write_file_list_in_markdown(directory='./', filename='a', reverse_positive_o f.write('###### '+str(filename6)+'\n\n') f.close() +# 查找文件名相同的文件 def find_repeated_file_with_same_filename(directory='./', ignored_directory_with_words=[], ignored_file_with_words=[], num=1000): import os from collections import Counter @@ -2863,6 +2985,7 @@ def find_repeated_file_with_same_filename(directory='./', ignored_directory_with repeated_file.append(item) return repeated_file +# 统计各个子文件夹中的文件数量 def count_file_in_sub_directory(directory='./', smaller_than_num=None): import os from collections import Counter @@ -2887,6 +3010,7 @@ def count_file_in_sub_directory(directory='./', smaller_than_num=None): print(count_file) print() +# 产生必要的文件,例如readme.md def creat_necessary_file(directory, filename='readme', file_format='.md', content='', overwrite=None, ignored_directory_with_words=[]): import os directory_with_file = [] @@ -2915,6 +3039,7 @@ def creat_necessary_file(directory, filename='readme', file_format='.md', conten f.write(content) f.close() +# 删除特定文件名的文件 def delete_file_with_specific_name(directory, filename='readme', file_format='.md'): import os for root, dirs, files in os.walk(directory): @@ -2922,6 +3047,7 @@ def delete_file_with_specific_name(directory, filename='readme', file_format='.m if files[i0] == filename+file_format: os.remove(root+'/'+files[i0]) +# 所有文件移到根目录(慎用) def move_all_files_to_root_directory(directory): import os import shutil @@ -2935,6 +3061,7 @@ def move_all_files_to_root_directory(directory): except: pass +# 改变当前的目录位置 def change_directory_by_replacement(current_key_word='code', new_key_word='data'): import os code_path = os.getcwd() @@ -2958,6 +3085,7 @@ def change_directory_by_replacement(current_key_word='code', new_key_word='data' ## download +# 通过Sci-Hub网站下载文献 def download_with_scihub(address=None, num=1): from bs4 import BeautifulSoup import re @@ -2996,8 +3124,8 @@ def download_with_scihub(address=None, num=1): ## PDF +# 获取PDF文献中的链接。例如: link_starting_form='https://doi.org' def get_links_from_pdf(pdf_path, link_starting_form=''): - # Example: link_starting_form='https://doi.org' import PyPDF2 import re pdfReader = PyPDF2.PdfFileReader(pdf_path) @@ -3020,6 +3148,7 @@ def get_links_from_pdf(pdf_path, link_starting_form=''): old = u['/A']['/URI'] return links +# 将PDF文件转成文本 def pdf_to_text(pdf_path): from pdfminer.pdfparser import PDFParser, PDFDocument from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter @@ -3052,6 +3181,7 @@ def pdf_to_text(pdf_path): ## audio +# 将文本转成音频 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 if print_text==1: @@ -3073,6 +3203,7 @@ def str_to_audio(str='hello world', filename='str', rate=125, voice=1, read=1, s engine.say(str) engine.runAndWait() +# 将txt文件转成音频 def txt_to_audio(txt_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0): import pyttsx3 f = open(txt_path, 'r', encoding ='utf-8') @@ -3098,6 +3229,7 @@ def txt_to_audio(txt_path, rate=125, voice=1, read=1, save=0, compress=0, bitrat engine.say(text) engine.runAndWait() +# 将PDF文件转成音频 def pdf_to_audio(pdf_path, rate=125, voice=1, read=1, save=0, compress=0, bitrate='16k', print_text=0): import pyttsx3 text = guan.pdf_to_text(pdf_path) @@ -3123,6 +3255,7 @@ def pdf_to_audio(pdf_path, rate=125, voice=1, read=1, save=0, compress=0, bitrat engine.say(text) engine.runAndWait() +# 将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 @@ -3131,6 +3264,7 @@ def compress_wav_to_mp3(wav_path, output_filename='a.mp3', bitrate='16k'): ## words +# 播放学术单词 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 @@ -3190,6 +3324,7 @@ def play_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_translati pygame.mixer.music.stop() print() +# 播放挑选过后的学术单词 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 @@ -3245,6 +3380,7 @@ def play_selected_academic_words(reverse=0, random_on=0, bre_or_ame='ame', show_ pygame.mixer.music.stop() print() +# 播放元素周期表上的单词 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