Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
287 views
ubuntu2404
�

�8�h�4����dZddlZddlmZddlmZejjd�Gd�d�Z
Gd�d�ZGd	�d
�Zd�Z
d�Zd
�Zd�Zd�Zedk(r�e
�Ze�\ZZZe�Ze�Zee�Zed�ed�ededdj6�d��edee��d��edeed��d��edee��d��yy)a*
Condensed Matter Physics Simulations
=====================================

This module implements fundamental condensed matter physics calculations:
- Electronic band structure using tight-binding models
- Fermi surface calculations and visualization
- Phonon dispersion relations
- Many-body systems (Hubbard model)

Keywords: band structure calculation python, tight binding model, fermi surface calculation,
phonon dispersion python, hubbard model simulation, electronic properties condensed matter

Author: CoCalc Scientific Templates
License: MIT
�N)�eigh�*c�8�eZdZdZdd�Zd	d�Zd
d�Zdd�Zdd�Zy)
�TightBindingModelz�
    Tight-binding model for electronic band structure calculations.

    Supports various lattice geometries and can calculate:
    - Energy dispersion relations E(k)
    - Density of states
    - Fermi surfaces
    c��||_y)z�
        Parameters:
        -----------
        lattice_type : str
            Type of lattice ('square', 'triangular', 'honeycomb')
        N��lattice_type��selfr	s  �code/condensed_matter.py�__init__zTightBindingModel.__init__$���)���c�j�|d|ztj|�tj|�zzz
S)u1
        Energy dispersion for 2D square lattice.

        E(k) = ε₀ - 2t(cos(kₓ) + cos(kᵧ))

        Parameters:
        -----------
        kx, ky : array
            k-space coordinates
        t : float
            Hopping parameter
        epsilon_0 : float
            On-site energy
        ���np�cos)r�kx�ky�t�	epsilon_0s     r�dispersion_square_2dz&TightBindingModel.dispersion_square_2d-s-���1�Q�3����r�
�R�V�V�B�Z� 7�8�8�8rc���dtjd|z�ztjd|dztjd�|zdzzz�z}|tj|�z}||fS)a	
        Energy dispersion for graphene (honeycomb lattice).

        Returns both conduction and valence bands.

        Parameters:
        -----------
        kx, ky : array
            k-space coordinates
        t : float
            Hopping parameter
        �y�?r�)r�exp�sqrt�abs)rrrr�f�energy_magnitudes      r�dispersion_graphenez%TightBindingModel.dispersion_graphene>sg��
����r�B�w���"�&�&��r�!�t�b�g�g�a�j��m�A�o�/E�)F�"G�G���r�v�v�a�y�=���"2�!2�2�2rc�>�|j�}|j�d|zz
|j�d|zz}}tj|||�}|dkDr�tj
|�}|D]*}	|tj||	z
dzd|dzzz�z
}�,|tjdtjz�|zt|�zz}||fStj||��\}
}|
|d|dz
zt|�z}|dd|ddzdz}||fS)a:
        Calculate density of states with optional Gaussian broadening.

        Parameters:
        -----------
        energies : array
            Energy values from band calculation
        n_bins : int
            Number of energy bins
        broadening : float
            Gaussian broadening width
        rrr)�binsrN�����)�flatten�min�maxr�linspace�
zeros_likerr�pi�len�	histogram)r�energies�n_bins�
broadening�E_flat�E_min�E_max�E_grid�dos�E�hist�	bin_edgess            r�density_of_statesz#TightBindingModel.density_of_statesQs%���!�!�#���z�z�|�a�
�l�2�F�J�J�L�1�Z�<�4O�u�����U�E�6�2����>��-�-��'�C����r�v�v���
�Q��.�!�J��M�/�B�C�C����B�G�G�A�b�e�e�G�$�z�1�C��K�?�@�C��s�{��	!�l�l�6��?�O�D�)��&��)�f�Q�i�/�0�3�v�;�>�C����n�y���}�4��9�F��s�{�rc��|j||�}|dkDr%ddtj||z
|z�zz}||fS||kjt�}||fS)a�
        Calculate Fermi surface and electron occupation.

        Parameters:
        -----------
        kx, ky : array
            k-space grid
        mu : float
            Chemical potential
        temperature : float
            Temperature for Fermi-Dirac distribution

        Returns:
        --------
        energy : array
            Energy dispersion
        occupation : array
            Fermi-Dirac occupation function
        r��?)rrr�astype�float)rrr�mu�temperature�energy�
occupations       r�
fermi_surfacezTightBindingModel.fermi_surfacepsn��(�*�*�2�r�2����?���b�f�f�f�r�k�[�-H�&I� I�J�J��z�!�!�!�2�+�-�-�e�4�J��z�!�!rN)�square)r;�)r;)���{�G�z�?)rF)	�__name__�
__module__�__qualname__�__doc__r
rr"r9rB�rrrrs ���)�9�"3�&�>"rrc�4�eZdZdZedd��Zedd��Zy)�PhononModelz�
    Phonon dispersion calculations for various lattice models.

    Implements:
    - 1D monoatomic chain
    - 1D diatomic chain
    - Density of phonon states
    c��tjd|z|z�tjtj|dz��zS)u
        Phonon frequency for 1D monoatomic chain.

        ω(k) = √(4K/M) |sin(k/2)|

        Parameters:
        -----------
        k : array
            Wave vector
        K : float
            Spring constant
        M : float
            Atomic mass
        �r)rrr�sin)�k�K�Ms   r�monoatomic_chain_1dzPhononModel.monoatomic_chain_1d�s4�� �w�w�q��s�1�u�~����r�v�v�a��c�{� 3�3�3rc�6�||z}||z}|dzd|ztj|�dzzz
}tj||tj|�zzd|zz�}tj||tj|�z
zd|zz�}||fS)a,
        Phonon frequencies for 1D diatomic chain.

        Returns both optical and acoustic branches.

        Parameters:
        -----------
        k : array
            Wave vector
        K : float
            Spring constant
        M1, M2 : float
            Masses of the two atoms
        rrO)rrr)	rQrR�M1�M2�M_sum�M_prod�discriminant�
omega_optical�omega_acoustics	         r�diatomic_chain_1dzPhononModel.diatomic_chain_1d�s��� �R����b����a�x�!�F�(�2�6�6�!�9�a�<�"7�7������U�R�W�W�\�-B�%B� C�q��x� P�Q�
�����e�b�g�g�l�.C�&C�!D��&��!Q�R���n�,�,rN)r;r;)r;r;�@)rGrHrIrJ�staticmethodrTr]rKrrrMrM�s/����4��4�"�-��-rrMc�&�eZdZdZdd�Zd�Zdd�Zy)�HubbardModelu�
    Mean-field solution of the Hubbard model.

    H = -t Σ⟨i,j⟩σ (c†ᵢσ cⱼσ + h.c.) + U Σᵢ nᵢ↑ nᵢ↓

    Implements self-consistent mean-field approximation to study
    magnetic instabilities and correlation effects.
    c��||_y)zi
        Parameters:
        -----------
        lattice_type : str
            Lattice geometry
        Nrr
s  rr
zHubbardModel.__init__�rrc��d|ztj|�tj|�zz}||z
||zdzz}||z
||zdzz
}	||	fS)ar
        Mean-field energy bands for 2D square lattice.

        Parameters:
        -----------
        kx, ky : array
            k-space coordinates
        t : float
            Hopping parameter
        mu : float
            Chemical potential
        U : float
            On-site interaction
        m : float
            Magnetization (order parameter)
        �����rr)
rrrrr>�U�m�	epsilon_k�E_up�E_downs
          r�mean_field_bandszHubbardModel.mean_field_bands�s_��$�q�D�"�&�&��*�r�v�v�b�z�1�2�	��2�~��!��A��%���R��!�A�#�a�%�'���V�|�rc	��||zdz}d}d}	tjtjtj|	�}
tjtjtj|	�}tj|
|�\}}
t	|�D]�}|j||
||||�\}}ddtj||z�zz}ddtj||z�zz}tj||z�}tj||z
�}t||z
�|kr||dfcSd|zd|zz}|d||z
zz}��||dfS)	as
        Solve mean-field equations self-consistently.

        Parameters:
        -----------
        t : float
            Hopping parameter
        U : float
            Interaction strength
        n_target : float
            Target electron density
        T : float
            Temperature
        max_iter : int
            Maximum iterations
        tol : float
            Convergence tolerance

        Returns:
        --------
        mu : float
            Converged chemical potential
        m : float
            Converged magnetization
        converged : bool
            Whether solution converged
        r皙�����?�@r;Tgffffff�?g333333�?F)	rr)r+�meshgrid�rangerjr�meanr)rrre�n_target�T�max_iter�tolr>rf�k_meshrr�KX�KY�	iterationrhri�f_up�f_down�n_new�m_news                     r�self_consistent_solutionz%HubbardModel.self_consistent_solution�sL��:��\�A�
������
�[�[�"�%�%������
/��
�[�[�"�%�%������
/�����R��$���B��x��I��0�0��R��B��1�E�L�D�&��#����t�a�x� 0�0�1�D��C�"�&�&��!��"4�4�5�F��G�G�D�6�M�*�E��G�G�D�6�M�*�E��5�1�9�~��#��5�$��&��a��#��+�%�A��c�X��-�.�.�B�))�,�1�e�|�rN)�	square_2d)rli�g�����ư>)rGrHrIrJr
rjr}rKrrrara�s���)��4<rrac��td�t�}d}tjtjtj|�}tjtjtj|�}tj
||�\}}|j
||d��}|j||d��\}}|j|d��\}	}
|jtj|j�|j�g�d��\}}td|j�|j�z
d���td	|j�|j�z
d���||	|
d
�||||d�||d�S)
z=
    Compare band structures of different lattice types.
    z!=== Band Structure Comparison ===�dr;)rg�������?)r0zSquare lattice bandwidth: �.3fzGraphene bandwidth: )r@�dos_Er5)�energy_c�energy_vr�r5)rC�graphenerr)
�printrrr)r+rnrr"r9�concatenater&r(r')
�tb�k_pointsrrrvrw�E_square�E_graphene_c�E_graphene_v�E_dos_square�
dos_square�E_dos_graphene�dos_graphenes
             r�band_structure_comparisonr�1st��
�
-�.�	�	�B��H�	���b�e�e�V�R�U�U�H�	-�B�	���b�e�e�V�R�U�U�H�	-�B�
�[�[��R�
 �F�B���&�&�r�2��&�5�H�!#�!7�!7��B�#�!7�!F��L�,� "�3�3�H��3�N��L�*�#%�#7�#7�
����,�,�.��0D�0D�0F�G�H��$8�$� �N�L�

�&�x�|�|�~�����'F�s�&K�
L�M�	� ��!1�!1�!3�l�6F�6F�6H�!H�� M�
N�O�&��Z�P�!-�<�+�L�B���	�rc���td�t�}d}tjtjtj|�}tjtjtj|�}tj
||�\}}gd�}g}|D]/}|j
|||d��\}	}
|j||	|
d���1td|���|||fS)zC
    Study evolution of Fermi surface with chemical potential.
    z=== Fermi Surface Evolution ===rE)g�g�rDr^rl)r?)r>r@rAu#Calculated Fermi surfaces for μ = )r�rrr)r+rnrB�append)r��k_resrrrvrw�	mu_values�
fermi_datar>r@rAs           r�fermi_surface_evolutionr�Ts���
�
+�,�	�	�B�
�E�	���b�e�e�V�R�U�U�E�	*�B�	���b�e�e�V�R�U�U�E�	*�B�
�[�[��R�
 �F�B��'�I��J����-�-�b�"�b�c�-�J���
�����v�Z�P�Q��
�/�	�{�
;�<��r�2��rc��td�t�}tjtjtjd�}|j|dd��}|j
|ddd��\}}td|j�d���td	|j�d���td
|j�d���||||d�S)z?
    Compare phonon dispersions for different chain types.
    z$=== Phonon Dispersion Comparison ===i�r;)rRrSr^)rRrVrWzMonoatomic max frequency: r�zDiatomic optical max: zDiatomic acoustic max: )rQ�
monoatomic�optical�acoustic)r�rMrr)r+rTr]r()�phononrQ�
omega_mono�	omega_opt�omega_acs     r�phonon_comparisonr�ms���
�
0�1�
�]�F�	���R�U�U�F�B�E�E�3�'�A��+�+�A���+�<�J� �2�2�1����2�L��I�x�	�&�z�~�~�'7��&<�
=�>�	�"�9�=�=�?�3�"7�
8�9�	�#�H�L�L�N�3�#7�
8�9�� ���	�rc� �td�t�}d}d}gd�}g}|D]i}|j|||d��\}}}||t|�|d�}	|j	|	�|rdnd	}
td
||zd�d|d
�dt|�d�d|
�d�	��k|S)z5
    Study magnetic transition in Hubbard model.
    z)=== Hubbard Model Magnetic Transition ===r;g�������?)rDr^g@g@rl)rr)rer>�
magnetization�	convergedr�z
not convergedzU/t = z.1fu: μ = r�z, |m| = z.4fz (�))r�rar}rr�)�hubbardr�density�U_values�resultsrer>rfr��result�statuss           r�hubbard_magnetic_transitionr��s���
�
5�6��n�G�	�A��G�#�H��G�
��"�;�;�A�q�'�S�;�Q���A�y��� ��V�"�	
��	���v�� )����
��q��s�3�i�w�r�#�h�h�s�1�v�c�l�"�V�H�A�N�O���Nrc�|�td�|dd}|d}|d}|d|dz
}tj||d�	�}tj||d
�	�}tj|dz|dzz�}td|j	�d
���tdtj
|�d
���|||d�S)z�
    Calculate basic transport properties from band structure.

    This is a simplified demonstration - real transport calculations
    require more sophisticated methods.
    z(=== Transport Properties Calculation ===rCr@rr)rr)rrr)�axisrrzMaximum group velocity: r�zAverage group velocity: )�vx�vy�speed)r�r�gradientrr(rp)�	band_datar@rr�dkr�r�r�s        r�calculate_transport_propertiesr��s���
�
4�5��x�
 ��
*�F�	�4��B�	�4��B�
�D��B�t�H�	�B�
�+�+�f�b�q�
)�	)�B�
�+�+�f�b�q�
)�	)�B�
�G�G�B��E�B��E�M�"�E�	�$�U�Y�Y�[��$5�
6�7�	�$�R�W�W�U�^�C�$8�
9�:��B��/�/r�__main__z
=== Summary ===z3Completed all condensed matter physics calculationszBand structure: rCr@z	 k-pointszFermi surfaces: z chemical potentialszPhonon modes: rQzHubbard model: z interaction strengths)rJ�numpyr�matplotlib.pyplot�pyplot�plt�scipy.linalgr�random�seedrrMrar�r�r�r�r�rGr�r�rvrw�phonon_data�hubbard_results�	transportr��shaper,rKrr�<module>r�s'���"����	�	���r��r"�r"�h6-�6-�pi�i�V!�F�2�4�<0�:�z��)�+�I�0�2��J��B�#�%�K�1�3�O�.�y�9�I�	�
��	�?�A�	��Y�x�0��:�@�@�A��
K�L�	��S��_�-�-A�
B�C�	�N�3�{�3�/�0�1��
;�<�	�O�C��0�1�1G�
H�I�r