Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
735 views
�
�F�VB�@s3dZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZejdkr�dd�Z
ndd�Z
dd	�Zdd
d�Zfdd
d�Zdddd�Zdddd�Zd!dddd�Zdd�Zdd�Zedkr/e�e�ndS)"zLReplace symmetries of Kwant builders with momentum parameters to the
system.�N)�	herm_conj�cCst|tj�S)N)�
isinstance�collections�Hashable)�obj�r�../code/wraparound.py�	_hashablesr
cCs#t|tj�o"t|tj�S)N)rrr�np�ndarray)rrrr	r
scs��fdd�}i�|S)a�Decorator to memoize a function that works even with unhashable args.

    This decorator will even work with functions whose args are not hashable.
    The cache key is made up by the hashable arguments and the ids of the
    non-hashable args.  It is up to the user to make sure that non-hashable
    args do not change during the lifetime of the decorator.

    This decorator will keep reevaluating functions that return None.
    csOtdd�|D��}�j|�}|dkrK�|��|<}n|S)Ncss-|]#}t|�r|n	t|�VqdS)N)r
�id)�.0�argrrr	�	<genexpr>(sz+_memoize.<locals>.lookup.<locals>.<genexpr>)�tuple�get)�args�key�result)�cache�frr	�lookup's
z_memoize.<locals>.lookupr)rrr)rrr	�_memoizes
rcsZt�fdd��}t��fdd��}t��fdd��}tdd��}|d	kr�tj�}|j�nBt|jj�}tjtj|j|���}tj|��t�j��x?|j	�D]1\}}	t
|	�r||	�n|	g||<q�Wx�|j�D]�\}
}	|
\}}�j|�}
�j
|
|�}||kr�||j||
|	��q%||ks�t
|	�r�||
|	�}	n||f|kr�|||fj|	�q%|	g|||f<q%Wi}xXtj|j	�|j��D]8\}}t|�d
krB|dn	||�||<qW|S)a@Replace translational symmetries by momentum parameters.

    A new Builder instance is returned.  By default, each symmetry is replaced
    by one scalar momentum parameter that is appended to the already existing
    arguments of the system.  Optionally, one symmetry may be kept by using the
    `keep` argument.
    cs%t��st���fdd�S)Ncs�||d���S)Nr)�ar)�mnp�valrr	�<lambda>=sz/wraparound.<locals>.bind_site.<locals>.<lambda>)�callable�AssertionError)r)r)rr	�	bind_site:szwraparound.<locals>.bind_sitecs����fdd�}|S)Ncs{tjdtj�|�d���}t��r]�|�j�|�|d���n�}||}|t|�S)Ny�?)�cmath�exp�ta�dotr�actr)rr�phase�v�pv)�elemr�symrrr	rAs):
z3wraparound.<locals>.bind_hopping_as_site.<locals>.fr)r)rr)rr*)r)rr	�bind_hopping_as_site?sz(wraparound.<locals>.bind_hopping_as_sitecs����fdd�}|S)Ncsktjdtj�|�d���}t��r]�|�j�|�|d���n�}||S)Ny�?)r!r"r#r$rr%)r�brr&r')r)rr*rrr	rJs):z+wraparound.<locals>.bind_hopping.<locals>.fr)r)rr)rr*)r)rr	�bind_hoppingHsz wraparound.<locals>.bind_hoppingcs�fdd�S)Ncst�fdd��D��S)Nc3s-|]#}t|�r!|��n|VqdS)N)r)rr)rrr	rRszAwraparound.<locals>.bind_sum.<locals>.<lambda>.<locals>.<genexpr>)�sum)r)�vals)rr	rRsz.wraparound.<locals>.bind_sum.<locals>.<lambda>r)r/r)r/r	�bind_sumPszwraparound.<locals>.bind_sumN�r)r�kwant�Builder�symmetry�list�periods�TranslationalSymmetry�pop�len�site_value_pairsr�hopping_value_pairs�whichr%�append�	itertools�chain)�builder�keepr r+r-r0�retr6�siter�hoprr,Zb_domZb_waZsummed_valsZsite_or_hopr/r)rr*r	�
wraparound1s:		!)0rE�c	sLddlm}ddlm}t�tjj�sDtd��n|j	�}|j
dd�}tjtj
tj
|d�}tjtj
tj
|d�����fdd	�|D�}tj|�}tj|��\}	}
xSt|jd
�D]>}|j|	|
|d
d
�d
d
�|fdddd�q�W|j�d
S)z>Plot the bands of a system with two wrapped-around symmetries.r)�Axes3D)�pyplotz!Need a system without symmetries.�
projection�3dr1cs/g|]%����fdd��D��qS)csGg|]=}tjtjj�j��|fdd��j��qS)�sparseF)r�sort�linalg�eigvalsh�hamiltonian_submatrix�real)r�ky)r�kx�systrr	�
<listcomp>�s	z,plot_bands_2d.<locals>.<listcomp>.<listcomp>r)r)r�kysrS)rRr	rT�s	z!plot_bands_2d.<locals>.<listcomp>N�rstride�cstride�����)�mpl_toolkits.mplot3drG�
matplotlibrHrr2�system�FiniteSystem�	TypeError�figure�gcar�linspace�pi�array�meshgrid�range�shape�plot_wireframe�show)rSr�momentarGrH�fig�axZkxs�energiesZmesh_xZmesh_y�ir)rrUrSr	�
plot_bands_2d�s   
+rmr1cCsftj|jd�|jd��}tj|�}|||jdd�d�<|||jd�<|S)z.Create a builder for a simple infinite system.r1rcSsdS)NTr)�prrr	r�sz_simple_syst.<locals>.<lambda>)r1r)rr1)rr)r2r7�vecr3re�	neighbors)�lat�E�tr*rSrrr	�_simple_syst�s
$rtgffffff�?cs;tjtjtj|�}xtjj�tjj�gD]�}t|�}t|dd�j	�}t|�j	�}tj
j||f���fdd�tjdkr�|n	t
|�D�}g}xT|D]L}	|j||	fdd�}
tjtjj|
�j�}|j|�q�Wtjj||�q;WdS)NrAr1csg|]}�|��qSrr)rrQ)�bandsrr	rT�s	z/test_consistence_with_bands.<locals>.<listcomp>z1.0rKF)rr`rar2�lattice�	honeycomb�squarertrE�	finalized�physics�Bands�__version__�reversedrOrLrMrNrPr=�testing�assert_almost_equal)rRZnkysrUrqrSZ	wa_keep_1Zwa_keep_noneZ
energies_aZ
energies_brQ�HZevsr)rur	�test_consistence_with_bands�s%%
r�g�������?��?c		srxktjj�tjj�gD]K}tt|||��j�}|j|dd�}xt|�t|�ft	j
|ggt�t	j
dggt�ftj
|ggt�tj
dggt�fgD]�\��x���fdd�gD]o}xf��fdd�gD]L}tt|||��j�}|j|dd�}t	jj
||�qWq�Wq�WqWdS)NrKFr1cs�S)Nr)r)�E1rr	r�sz"test_value_types.<locals>.<lambda>cs�S)Nr)rr,)�t1rr	r�s)r2rvrwrxrErtryrO�floatrrbr#r~�assert_equal)	�krrrsrqrSr�ZE2�t2ZH_altr)r�r�r	�test_value_types�s%-=r�cCst�t�dS)N)r�r�rrrr	�test�sr�cCs5tjj�}tt|��j�}t|�dS)z2Calculate and plot the band structure of graphene.N)r2rvrwrErtryrm)rqrSrrr	�demo�sr��__main__)rr)rFrF皙�����)r�r�)�__doc__�sysr>rr!�numpyr�	tinyarrayr#r2Z
kwant.builderr�version_infor
rrErmrtr�r�r�r��__name__rrrr	�<module>s,P