生活, 其他记录

GUAN软件包学习示例

Guan软件包官网:https://py.guanjihuan.com。安装或更新命令:pip install --upgrade guan。如果无法获取最新版本,可以指定默认源安装,安装命令为:pip install --upgrade guan -i https://pypi.python.org/simple。使用方法:import guan。个人常用的函数会不定期添加进去。

这是之前的一篇:Python开源项目Guan。本篇给出一些的学习示例,如果你需要实现相同的功能,那么可以安装软件包后进行调用或者直接复制函数源码到你自身的项目中。

一、测试例子

# GUAN软件包官网: https://py.guanjihuan.com
import guan
guan.test()

二、泡利矩阵

# 泡利矩阵
import guan
sigma_x = guan.sigma_x()
print(sigma_x)

三、函数的计时器

# 函数的计时器
import guan

@guan.timer_decorator
def my_function():
    import time
    time.sleep(2)
    print('Run finished!')

for _ in range(3):
    my_function()

四、实空间哈密顿量的示例

# 实空间哈密顿量的示例
import guan
print('\n', guan.hamiltonian_of_finite_size_system_along_one_direction(3), '\n')
print(guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(2, 2), '\n')
print(guan.hamiltonian_of_finite_size_system_along_three_directions_for_cubic_lattice(2, 2, 2), '\n')

五、能带图计算示例

# 能带图计算示例
import guan
import numpy as np
k_array = np.linspace(-np.pi, np.pi, 100)
 # one dimensional chain
hamiltonian_function = guan.one_dimensional_fourier_transform_with_k(unit_cell=0, hopping=1)
eigenvalue_array = guan.calculate_eigenvalue_with_one_parameter(k_array, hamiltonian_function)
guan.plot(k_array, eigenvalue_array, xlabel='k', ylabel='E', style='-k', fontfamily=None)
# square lattice ribbon
eigenvalue_array = guan.calculate_eigenvalue_with_one_parameter(k_array, guan.hamiltonian_of_square_lattice_in_quasi_one_dimension)
guan.plot(k_array, eigenvalue_array, xlabel='k', ylabel='E', style='-k', fontfamily=None)
# graphene ribbon
eigenvalue_array = guan.calculate_eigenvalue_with_one_parameter(k_array, guan.hamiltonian_of_graphene_with_zigzag_in_quasi_one_dimension)
guan.plot(k_array, eigenvalue_array, xlabel='k', ylabel='E', style='-k', fontfamily=None)

六、陈数和Wilson loop计算示例

# 陈数和Wilson loop计算示例
import guan
import numpy as np
chern_number = guan.calculate_chern_number_for_square_lattice_with_efficient_method(guan.hamiltonian_of_one_QAH_model, precision=100)
print('\nChern number=', chern_number, '\n')
wilson_loop_array = guan.calculate_wilson_loop(guan.hamiltonian_of_ssh_model)
print('Wilson loop =', wilson_loop_array)
p = np.log(wilson_loop_array)/2/np.pi/1j
print('\np =', p, '\n')

七、使用格林函数计算态密度示例

# 使用格林函数计算态密度示例
import guan
import numpy as np

hamiltonian = guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(2,2)
fermi_energy_array = np.linspace(-4, 4, 400)
total_dos_array = guan.total_density_of_states_with_fermi_energy_array(fermi_energy_array, hamiltonian, broadening=0.1)
guan.plot(fermi_energy_array, total_dos_array, xlabel='E', ylabel='Total DOS', style='-', fontfamily=None)

fermi_energy = 0
N1 = 3
N2 = 4
hamiltonian = guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N1,N2)
LDOS = guan.local_density_of_states_for_square_lattice(fermi_energy, hamiltonian, N1=N1, N2=N2)
print('square lattice:\n', LDOS, '\n')
h00 = guan.hamiltonian_of_finite_size_system_along_one_direction(N2)
h01 = np.identity(N2)
LDOS = guan.local_density_of_states_for_square_lattice_using_dyson_equation(fermi_energy, h00=h00, h01=h01, N2=N2, N1=N1)
print(LDOS, '\n\n')
LDOS2 = guan.local_density_of_states_for_square_lattice_using_dyson_equation_with_second_method(fermi_energy, h00, h01, N2, N1, internal_degree=1, broadening=0.01)
print(LDOS2, '\n\n')
guan.plot_contour(range(N1), range(N2), LDOS, fontfamily=None)
guan.plot_contour(range(N1), range(N2), LDOS2, fontfamily=None)

N1 = 3
N2 = 4
N3 = 5
hamiltonian = guan.hamiltonian_of_finite_size_system_along_three_directions_for_cubic_lattice(N1, N2, N3)
LDOS = guan.local_density_of_states_for_cubic_lattice(fermi_energy, hamiltonian, N1=N1, N2=N2, N3=N3)
print('cubic lattice:\n', LDOS, '\n')
h00 = guan.hamiltonian_of_finite_size_system_along_two_directions_for_square_lattice(N2, N3)
h01 = np.identity(N2*N3)
LDOS = guan.local_density_of_states_for_cubic_lattice_using_dyson_equation(fermi_energy, h00, h01, N3=N3, N2=N2, N1=N1)
print(LDOS)

八、电导和散射矩阵的计算示例

# 电导和散射矩阵的计算示例
import guan
import numpy as np

fermi_energy_array = np.linspace(-4, 4, 400)
h00 = guan.hamiltonian_of_finite_size_system_along_one_direction(4)
h01 = np.identity(4)
conductance_array = guan.calculate_conductance_with_fermi_energy_array(fermi_energy_array, h00, h01)
guan.plot(fermi_energy_array, conductance_array, xlabel='E', ylabel='Conductance', style='-', fontfamily=None)

fermi_energy = 0
guan.print_or_write_scattering_matrix(fermi_energy, h00, h01)

九、波函数规范的选取示例

# 波函数规范的选取示例
import numpy as np
import cmath
import guan

# Fixed gauge example 1
vector = np.array([np.sqrt(0.5), np.sqrt(0.5)])*cmath.exp(np.random.uniform(0, 1)*1j)
print('\nExample 1\n', vector)
print(np.dot(vector.transpose().conj(), vector), '\n')

vector = guan.find_vector_with_fixed_gauge_by_making_one_component_real(vector)
print(vector)
print(np.dot(vector.transpose().conj(), vector), '\n')

# Fixed gauge example 2
vector = np.array([1, 0])*cmath.exp(np.random.uniform(0, 1)*1j)
print('\nExample 2\n', vector)
print(np.dot(vector.transpose().conj(), vector), '\n')

vector = guan.find_vector_with_fixed_gauge_by_making_one_component_real(vector)
print(vector)
print(np.dot(vector.transpose().conj(), vector), '\n')

十、数组分割示例

# 数组分割示例
import numpy as np
import guan
task_num = 4
parameter_array_all = np.arange(0, 17, 1) 
for task_index in range(task_num):
    parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, task_num, task_index)
    print(parameter_array)
    print()
337 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code