a
     ɝh;B                     @   s   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rNe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Zdd Zdd Zdd Zd	d
 ZdS )    N)	Structurec                 C   s   d}zd| d j d  d| d j d  d| d j d  d	| d
 j d  d| d j d  d| d j d  d| d j d  d| d j d  d| d| d j d  d}W n8 ty } z td|  d}W Y d }~n
d }~0 0 |S )Nz@$$\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>
                z!Error in display_results_kappap: zError displaying results)iloc	Exceptionstwrite)dfformulatemplatee r   +/var/www/html/AI4Kappa/Pages/CustomKappa.pydisplay_results_kappap   s6    	
r   c                 C   st   d}d| d j d  d| d j d  d| d j d  d	| d
 j d  d| d j d  d| d| d j d  d}|S )NzG$$\kappa_L=\frac{G \upsilon_s V^{\frac{1}{3}}}{N T} \cdot e^{-\gamma}$$z4
            The volume of the crystal structure is r   r   uF    Å$^3$.<br>
            The Bulk modulus of the crystal structure is r   zD GPa.<br>
            The Shear modulus of the crystal structure is r   zE GPa.<br>
            The sound velocity of the crystal structure is r   zI m/s.<br>
            The Grunisen parameter of the crystal structure is r
   zF.<br>
            The formula of the lattice thermal conductivity is: zA.<br>
            The calculated lattice thermal conductivity is Kappa_cal (W m-1 K-1)u    W/(m·K).<br>
            )r   )r   r   r   r   r   r   display_results_ai4kappa.   s"    	r   c                 C   s   | dkrg d}ng d}|S )NKappaP)r   Density (g cm-3)r   r   r   r   -Sound velocity of the transverse wave (m s-1),Sound velocity of the longitude wave (m s-1)r   Poisson ratior
   r	   r   )r   r   r   r   r   r   r   r   r   r   r
   r	   r   r   )methodlsr   r   r   display_columns:   s    
r    c            2      C   s6	  t d tjd} t jj}tj| d}t jdddgdd}t jj		rt

tj|d	}t|d
krxt d d S t d t d i }|D ]}tj|}t d| d t d\}}	}
zFtttj||}|d}|d u st|ttfsd}W n> tyL } z$td| d|  d}W Y d }~n
d }~0 0 |( t jdd| d dd}W d    n1 s0    Y  |	( t jdd| d dd}W d    n1 s0    Y  d }|d urh|d urh|d urhz|d }|d }|d }|d| d  | d }|| d }|dkrNt d | d! d }n|| }t|d"d" d"t|d" d"  }td"d|  d#k rt d | d$ d }nTdd%|  d"d"d|    }d|  k rd&k sn t d | d'|d(d) d }W nl ty$   t d | d* d }Y nD tyf } z*t d | d+t |  d }W Y d }~n
d }~0 0 |
Z | d,| }d-| t jvr|t jd-| < |t jd-|  krd.| t jv rt jd.| = |t jd-| < t j!d/d0| t jd.| d1d2d}zL|" r4t|}|t jd.| < n$|}d.| t jv rXt jd.| = W n" t#y|   t d3 d }Y n0 |d urt d4|d( |" rt d5 n
t d6 W d    n1 s0    Y  i }|d ur||d< |d ur||d< |d ur||d/< |r|||< |||< qt $d7}|	r i }i }|% D ]\}}g }g } d|vrp|&d8 n|d dkr| &d9 d|vr|&d: n|d dkr| &d; d/|vr|&d/ n|d/ dkr| &d< |r|||< | rJ| ||< qJ|s|rt d= |rZt d> |% D ]&\}}t d?| dd@|  q2|rt dA |% D ]&\}}!t d?| dd@|!  qrd S z"t'|}"t(j)|"j*g dBdC}#dDD ]}$|$|"jv r|"|$ |#|$< q|% D ]H\}}tj+|d }%|%|#j*v r|% D ]\}&}'|'|#j,|%|&f< qq|#d/ }(t-.|#})|dkr|t-/|)|(}*t-0|*d%|(}+t-1|+},t2}-nt-/|)|(}*t-3|*},t4}-t d t dE t dF t 5|,j,d d t6|f  t d t dG |7 D ]}tj+|d }%t 8dH|  t9tj||}.t j|.ddI z&|,j,|%g }/|-|/}0t j:|0ddI W n@ ty } z&t dJ| dt |  W Y d }~n
d }~0 0 W d    n1 s0    Y  qW n< ty } z"t dKt |  W Y d }~d S d }~0 0 t;| t<|  n
t =dL dM}1t j:|1ddI d S )NNzCustom Kappa Calculator.modelzSelect calculation method:r   PINKT)
horizontalz*.cif   z3Maximum 5 files allowed. Please upload fewer files.z---zCustom Parameters Inputz**Parameters for z:**   r   g      @zError reading density for z: r   Zbulk_zEnter value...)keyvalueplaceholderr   Zshear_g    eAi     g      ?r   zWarning for uW   : Cannot calculate Grüneisen parameter - Sound velocity of the transverse wave is zero   g|=u?   : Cannot calculate Grüneisen parameter - Invalid Poisson ratio   
   u#   : Calculated Grüneisen parameter (z.4fz#) is out of reasonable range (0-10)u@   : Cannot calculate Grüneisen parameter - Division by zero erroru+   : Error calculating Grüneisen parameter - _Zprev_bulk_shear_Z
user_grun_r
   Zgrun_input_ z&Enter value or leave empty for defaultzPlease enter a valid numberzCurrent value: z(User defined)z(Default calculated)Z	CalculatezBulk moduluszBulk modulus must be positivezShear moduluszShear modulus must be positiveu%   Grüneisen parameter must be positivez$Please correct the following errors:zMissing parameters:z- z, zInvalid parameters:)r   r   r   r   r   r   r
   )indexcolumns)r   r   r   r   ResultszCombined Results:zCrystal Structure InformationzStructure details for )unsafe_allow_htmlzError displaying results for z&An error occurred during calculation: z9Please upload CIF files (maximum 5) in the sidebar first.aK  <p style='font-size: 22px;'>We strive to have clear documentation and examples to help everyone with using our calculator. 
        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>)>r   titleospathabspathsession_stateroot_dir_pathjoinradiouploaded_filesgloblenerrorr   	subheaderbasenamer1   foget_crystalline_datar   	from_fileget
isinstanceintfloatr   printnumber_inputwarningpowabsZeroDivisionErrorstr
text_inputstrip
ValueErrorbuttonitemsappendget_dir_crystalline_datapd	DataFramer0   splitextloccalkcal_Debye_T	cal_gammacal_Acal_K_Slackr   by_MTPr   	dataframer    keysexpanderget_crystalline_contentmarkdowndel_cif_filedel_temp_fileinfo)2	sour_pathr9   
model_pathr   Z	cif_filesZfile_paramscif_file	file_namecol1col2Zcol3Zstructure_dfdensityr   ZbulkZshearZdefault_gruneisenZbulk_paZshear_paZ
density_siVlVtapoissonZcurrent_bulk_shearZ
user_inputZ	gruneisenparams	calculateZmissing_paramsZinvalid_paramsmissinginvaliderrors
all_cry_dfwhole_info_dfcol	base_nameparamr(   custom_gammaDebye_dfgamma_dfA_dffinal_dfZdisplay_funccry_contentZfile_resultsr   declarationr   r   r   appG   sx   






&&
 




*












 
 









X

r   ) r5   r=   sysr6   dirnamer7   __file__current_dir
parent_dirrU   streamlit_scripts.file_opfile_oprB   streamlit_scripts.chang_modelchang_modelcmstreamlit_scripts.calculate_Kcalculate_Kr[   predict	streamlitr   pandasrW   pymatgen.corer   numpynpr   r   r    r   r   r   r   r   <module>   s$   
