a
     ɝh5#                     @   s   d dl Z d dlZd dlZd dlZd dlZe je jeZ	e je	Z
e
ejvr^eje
 d dlmZ d dlmZ d dlmZ d dlZd dlZd dlZd dlmZ d dlmZ dd Zdd ZdS )    N)	Structure)SpacegroupAnalyzerc                 C   s   d}zd| d d  d| d d  d| d d  d	| d
 d  d| d d  d| d d  d| d d  d| d d  d| d| d d  d}W n. t y } zt| W Y d}~n
d}~0 0 |S )z
    Display calculation results
    Args:
        df: DataFrame containing calculation results
    Returns:
        template: HTML template string containing results
    z@$$\kappa_L=A\frac{M V^{\frac{1}{3}} \theta_a^3}{\gamma^2 T n} $$zA
                The number of atoms of the crystal structure is Number of Atomsr   z=.<br>
                The volume of the crystal structure is    Volume (Å3)uO    Å$^3$.<br>
                The total atomic mass of the crystal structure is the total atomic mass (amu)zG amu.<br>
                The Bulk modulus of the crystal structure is Bulk modulus (GPa)zH GPa.<br>
                The Shear modulus of the crystal structure is Shear modulus (GPa)zI GPa.<br>
                The sound velocity of the crystal structure is Speed of sound (m s-1)zV m/s.<br>
                The Acoustic Debye Temperature  of the crystal structure is Acoustic Debye Temperature (K)uM    K.<br>
                The Grüneisen parameter of the crystal structure is    Grüneisen parameterzJ.<br>
                The formula of the lattice thermal conductivity is: zE.<br>
                The calculated lattice thermal conductivity is Kappa_Slack (W m-1 K-1)u    W/(m·K).<br>
                N)	Exceptionstwrite)dfZformulatemplatee r   &/var/www/html/AI4Kappa/Pages/KappaP.pydisplay_results   s4    	







	

 r   c                   C   st  t d tjd} t jj}tj| d}t jjrTt		tj|d}i }|D ]}zxtj
|}tt jdr|t jjv rt jj| ||< td|  n0t|}t|}| }	|	||< td|  W qR ty( }
 z@t | dt|
  t| t d	| d
 W Y d}
~
qRd}
~
0 0 qR|s@t d dS t|\}}t|dkr^|d }tj||}t|}tj| d}t|\}}z8t|d |  t| t ||d }W t!|  nt!|  0 t"|dd |dd D ]Z\}}zBt||  t| t ||}t#j$||ddd}W t!|  nt!|  0 qzzt d t%|}|j&rt d W W t'| t(|  dS t#j$||ddd}|j&rt d W W t'| t(|  dS t)*|}t)+|}t),|d}t)-|}g d}|j.dd|f }|j&rPt d W W t'| t(|  dS t /| t d t|j0dkr~|j0d nd}t d|  t d t j|dd t d |j&st1|}t j2|dd t d W nD ty" }
 z*t dt|
  t d W Y d}
~
n
d}
~
0 0 W t'| t(|  nt'| t(|  0 n
t 3d d}t j2|dd dS )zMain application functionKappaP.modelz*.cifprimitive_structuresz%Using cached primitive structure for z$Created new primitive structure for z Invalid CIF file: zInvalid CIF file z has been deleted.NzNo valid CIF files found.r   ztest_results.csv   T)
left_indexright_indexz---z.Failed to extract crystal data from CIF files.z.Failed to merge crystal data with predictions.)r   zDensity (g cm-3)r   r   r   r   z-Sound velocity of the transverse wave (m s-1)z,Sound velocity of the longitude wave (m s-1)r	   zPoisson ratior   r
   r   z5No data was generated. Please check your input files.zNo filez,The file name of displaying crystalline is: z1The information of uploaded crystal structure is:)unsafe_allow_htmlzAn error occurred: z,Please check your input files and try again.z-Please upload CIF files in the sidebar first.aP  <p style='font-size: 22px;'>We strive to have clear documentation and examples to help everyone with using KappaP on their own. 
        We will happily fix issues in the documentation and examples should you find any, 
        however, we will not be able to offer extensive user support and training, except for our collaborators.</p>)4r   titleospathabspathsession_stateroot_dir_pathjoinuploaded_filesglobbasenamehasattrr   printr   	from_filer   Z get_primitive_standard_structurer   r   strremoveerrorfoZcreate_id_proplenZget_crystalline_contentcmZget_model_pathZ
copy_modelpredictmainZget_pre_dataframeZclean_modelzippdmergeZget_dir_crystalline_dataemptyZdel_cif_fileZdel_temp_filecalkZcal_Debye_TZ	cal_gammaZcal_AZcal_K_Slackloc	dataframeindexr   markdowninfo) Z	sour_pathr#   Z
model_pathZcif_path_listZvalid_structuresZcif_path	file_name	structureZanalyzerZprimitive_structurer   Zcif_name_listZfirst_cif_nameZfirst_cif_pathZcry_contentZresults_csv_pathZmodel_path_listZmodel_name_listZpre_dfZ
model_nameZpre_df1Z
all_cry_dfZwhole_info_dfZDebye_dfZgamma_dfZA_dfZ
K_slack_dflsZfinal_dffirst_indexr   declarationr   r   r   app<   s    



*


"



1


+











"


rB   ) r   warningsr&   iosysr    dirnamer!   __file__Zcurrent_dirZ
parent_dirappendstreamlit_scripts.file_opfile_opr.   Zstreamlit_scripts.chang_modelZchang_modelr0   Zstreamlit_scripts.calculate_KZcalculate_Kr7   r1   	streamlitr   pandasr4   Zpymatgen.core.structurer   Zpymatgen.symmetry.analyzerr   r   rB   r   r   r   r   <module>   s$   
