a
    7b                    @   sl   d dl mZ d dlZd dlT d dlmZ d dlZddlm	Z	m
Z
mZ e
drTeZneZG dd	 d	eZdS )
    )print_functionN)*)path   )	plotutilsutilsplatform_specifics   c                
   @   sX  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dddZdddZdddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zdd-d.Zdd/d0Zdd1d2Zdd4d5Zdd6d7Zdd8d9Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"dDdE Z#dFdG Z$ddHdIZ%ddJdKZ&ddLdMZ'dNdO Z(ddPdQZ)ddSdTZ*ddUdVZ+ddWdXZ,ddYdZZ-dd[d\Z.dd]d^Z/dd_d`Z0ddadbZ1ddcddZ2dedf Z3dgdh Z4didj Z5dkdl Z6ddndoZ7ddsdtZ8dudv Z9ddydzZ:dd|d}Z;dddZ<dddZ=dddZ>dddZ?dddZ@dd ZAdd ZBdd ZCdddZDdS )
thermopackz!
    Interface to thermopack
    c                 C   s  t  }|d | _|d | _|d | _|d | _ttt	|d }t
|| _| jj| _|   t| j| dd| _t| j| dd| _t| j| dd	| _t| j| dd
| _t| j| dd| _t| j| ddj| _d| _t| j| dd| _t| j| dd| _t| j| dd| _t| j| dd| _t| j| dd| _ t| j| dd| _!t| j| dd| _"t| j| dd| _#t| j| dd| _$t| j| dd| _%t| j| dd| _&t| j| dd| _'t| j| dd| _(t| j| dd | _)t| j| dd!| _*t| j| dd"| _+t| j| dd#| _,t| j| d$d%| _-t| j| d&d'| _.t| j| d&d(| _/t| j| d)d*| _0t| j| d+d,| _1t| j| d-d.| _2t| j| d/d0| _3t| j| d)d1| _4t| j| d2d3| _5t| j| d4d5| _6t| j| d4d6| _7t| j| d4d7| _8t| j| d4d8| _9t| j| d4d9| _:t| j| d4d:| _;t| j| d4d;| _<t| j| d4d<| _=t| j| d4d=| _>t| j| d4d>| _?t| j| d?d@| _@t| j| d?dA| _At| j| d?dB| _Bt| j| d?dC| _Ct| j| dDdE| _Dt| j| dFdG| _Et| j| dFdH| _Ft| j| dFdI| _Gt| j| dJdK| _Ht| j| dLdM| _It| j| dLdN| _Jt| j| dLdO| _Kt| j| dLdP| _Lt| j| dQdR| _Mt| j| d4dS| _Nt| j| d4dT| _Ot| j| d4dU| _Pt| j| dVdW| _Q| R  dS )XzN
        Load libthermopack.(so/dll) and initialize function pointers
        prefixmodulepostfixZpostfix_no_moduleZdyn_libZthermopack_varadd_eos
delete_eosZactivate_modelZget_eos_identificationZ
eoslibinitinit_thermoZthermopack_constantsZrgasNZtptminZtppminZsolideosZ
solid_initZinit_volume_translationredefine_critical_parameterseosZspecificvolumezfacthermoentropyenthalpycompmoleweightZgetcriticalparamZidealidealenthalpysingleZget_entropy_reference_valueZset_entropy_reference_valueZget_enthalpy_reference_valueZset_enthalpy_reference_valuespeed_of_soundZsound_velocity_2phZcompdataZcomp_index_activeZcomp_name_activeZ	ph_solverZsetphtoleranceZ	tp_solverZtwophasetpflashZ	ps_solverZtwophasepsflashZ	uv_solverZtwophaseuvflashZtwophasephflashZthermo_utilsZ
guessphaseZeostvinternal_energy_tv
entropy_tvZpressureZ	thermo_tventhalpy_tvZfree_energy_tvchemical_potential_tventropy_tvp
thermo_tvpenthalpy_tvpZ
saturationZ	safe_bubtZ	safe_bubpZ	safe_dewtZ	safe_dewpZsaturation_curveZenvelopeplotZ
binaryplotZvllebinaryxyglobal_binary_plotget_bp_termZsolid_saturationZsolidenvelopeplotZisolinesZisothermZisobarZ	isenthalpZ	isentropecriticalZcalccriticaltvZvirial_coefficientsZsecondvirialcoeffmatrixZbinarythirdvirialcoeffmatrixjoule_thompson_inversionZmap_jt_inversion)Sr   Zget_platform_specificsr   r   r   
postfix_nmr   joindirname__file__cdllLoadLibrarytpZget_phase_flags_cs_get_phase_flagsget_phase_flagsgetattrget_export_name	s_add_eoss_delete_eoss_activate_models_get_model_ideoslibinit_init_thermoc_doublein_dllvalueZRgasncminimum_temperature_cminimum_pressure_csolideos_solid_init"eoslibinit_init_volume_translation'eoslibinit_redefine_critical_parameterss_eos_specificvolume
s_eos_zfacs_eos_thermos_eos_entropys_eos_enthalpys_eos_compmoleweights_eos_getCriticalParams_ideal_idealenthalpysingle#s_ideal_get_entropy_reference_value#s_ideal_set_entropy_reference_value$s_ideal_get_enthalpy_reference_value$s_ideal_set_enthalpy_reference_values_sos_sound_velocity_2phs_compdata_compindexs_compdata_compnames_set_ph_tolerances_twophasetpflashs_psflash_twophases_uvflash_twophases_phflash_twophases_guess_phases_internal_energy_tvs_entropy_tvs_pressure_tv
s_lnphi_tvs_enthalpy_tvs_helmholtz_energy	s_chempots_entropy_tvps_thermo_tvps_enthalpy_tvp
s_bubble_t
s_bubble_ps_dew_ts_dew_ps_envelope_plots_binary_plots_global_binary_plots_get_bp_terms_solid_envelope_plot
s_isotherms_isobars_isenthalps_isentrope	s_crit_tvs_virial_coeffcientss_second_virial_matrixs_binary_third_virial_matrixs_joule_thompson_inversionr   )selfZpf_specificsZdyn_lib_path rp   \/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/pyctp/thermo.py__init__   s   












zthermopack.__init__c                 C   s   |    dS )z1Delete FORTRAN memory allocated for this instanceN)r   ro   rp   rp   rq   __del__   s    zthermopack.__del__c                 C   s&   t tg| j_d| j_| | j dS )z?Activate this instance of thermopack parameters for calculationN)POINTERc_intr2   argtypesrestypemodel_index_crs   rp   rp   rq   activate   s    zthermopack.activatec                 C   s"   d| j _t| j _t|   | _dS )z/Allocate FORTRAN memory for this class instanceN)r0   rw   rv   rx   ry   rs   rp   rp   rq   r      s    zthermopack.add_eosc                 C   s0   t tg| j_d| j_| | j td| _dS )z2de-allocate FORTRAN memory for this class instanceNr   )ru   rv   r1   rw   rx   ry   rs   rp   rp   rq   r      s    zthermopack.delete_eosc                 C   sT   |    d}td| }t|}ttg| j_d| j_| || |jd }|S )zMGet model identification

        Returns:
            str: Eos name
        (       Nascii)	rz   c_char_p
c_len_typer3   rw   rx   r7   decodestrip)ro   Z	eosid_lenZeosid_cZeosid_len_cZeosidrp   rp   rq   get_model_id   s    zthermopack.get_model_idc                 C   s6   t |dkr(| j| | j | | j }n
|| j }|S )zGenerate library export name based on module and method name

        Args:
            module (str): Name of module
            method (str): Name of method

        Returns:
            str: Library export name
        r   )lenr   r   r   r%   )ro   r   methodZexport_namerp   rp   rq   r/      s    

zthermopack.get_export_nameNDefaultc           +      C   s  |    tt|dt|d| _tt }t|d}t	t|}t|d}t	t|}t|d}t	t|}t|d}t	t|}t|}|du r|}nttt|}|du rt }t	d}nt|d}t	t|}|du rt }t	d}nt|d}t	t|}t	t|	}t|	d} t	t|
}!t|
d}"t	t|}#t|d}$|du rtt
 }%ntt
t
|}%|du rt }&t	d}'nt|d}&t	t|}'|du r|}(nt| j | }(|du r|})n |rd}*nd}*ttt|*})ttttttttttttttt
tttttt	t	t	t	t	t	t	t	t	t	g| j_d| j_| ||||t||||| |"|$|%|&|(|)||||||||!|#|' dS )a"  Initialize thermopack

        Args:
            eos (str): Equation of state
            mixing (str): Mixture model for cubic eos
            alpha (str): Alpha formulations for cubic EOS
            comps (string): Comma separated list of components
            nphases (int): Maximum number of phases considered during multi-phase flash calculations
            liq_vap_discr_method (int, optional): Method to discriminate between liquid and vapor in case of an undefined single phase. Defaults to None.
            csp_eos (str, optional): Corrensponding state equation. Defaults to None.
            csp_ref_comp (str, optional): CSP reference component. Defaults to None.
            kij_ref (str, optional): Data set identifiers. Defaults to "Default".
            alpha_ref (str, optional): Data set identifiers. Defaults to "Default".
            saft_ref (str, optional): Data set identifiers. Defaults to "Default".
            b_exponent (float, optional): Exponent used in co-volume mixing. Defaults to None.
            TrendEosForCp (str, optional): Option to init trend for ideal gas properties. Defaults to None.
            cptype (int array, optional): Equation type number for Cp. Defaults to None.
            silent (bool, optional): Supress messages during init?. Defaults to None.
         ,r}   Nr   r   )rz   maxr   splitr8   ru   rv   r~   encoder   r5   r4   rw   rx   byref)+ro   r   ZmixingalphacompsnphasesZliq_vap_discr_methodZcsp_eosZcsp_ref_compZkij_refZ	alpha_refZsaft_refZ
b_exponentZTrendEosForCpZcptypesilentnull_pointerZeos_cZeos_lenZmixing_cZ
mixing_lenZalpha_cZ	alpha_lenZcomp_string_cZcomp_string_lenZ	nphases_cZliq_vap_discr_method_cZ	csp_eos_cZcsp_eos_lenZcsp_ref_comp_cZcsp_ref_comp_lenZkij_ref_lenZ	kij_ref_cZalpha_ref_lenZalpha_ref_cZsaft_ref_lenZ
saft_ref_cZb_exponent_cZTrendEosForCp_cZTrendEosForCp_lenZcptype_csilent_cZ
silent_intrp   rp   rq   r      s     








zthermopack.init_thermoc                 C   sl   |    d}t|d}tt|}t|d}tt|}ttttg| j_d| j_| |||| dS )zInitialialize Peneloux volume translations

        Args:
            parameter_reference (str): String defining parameter set, Defaults to "Default"
        ZPENELOUXr}   N)rz   r~   r   r   r   r<   rw   rx   )ro   Zparameter_referenceZvolume_trans_modelZvolume_trans_model_cZvolume_trans_model_lenZref_string_cZref_string_lenrp   rp   rq    init_peneloux_volume_translation  s"    z+thermopack.init_peneloux_volume_translationTc                 C   s   |    |rtd}ntd}tt }|du r6|}ntt| | }|du rT|}ntt| | }ttttttg| j_d| j_| t||| dS )a  Recalculate critical properties of pure fluids

        Args:
            silent (bool): Ignore warnings? Defaults to True
            Tc_initials (array_like): Initial value for pure fluid critical temperatures (K). Negative values will trigger use of SRK values from data base.
            vc_initials (array_like): Initial value for pure fluid critical volumes (m3/mol). Negative values will trigger use of SRK values from data base.
        r   r   N)	rz   rv   ru   r5   r   r=   rw   rx   r   )ro   r   ZTc_initialsZvc_initialsr   r   ZTc_initials_cZvc_initials_crp   rp   rq   r     s*    


z'thermopack.redefine_critical_parametersc                 C   sF   |    t|d}tt|}ttg| j_d| j_| || dS )zUInitialize pure solid

        Args:
            scomp (str): Component name
        r}   N)rz   r~   r   r   r   r;   rw   rx   )ro   ZscompZscomp_cZ	scomp_lenrp   rp   rq   
init_solid  s    zthermopack.init_solidc                 C   sF   |    t|d}tt|}ttg| j_t| j_| ||}|S )zGet component index

        Args:
            comp (str): Component name

        Returns:
            int: Component FORTRAN index
        r}   )	rz   r~   r   r   r   rK   rw   rv   rx   )ro   compcomp_ccomp_lenidxrp   rp   rq   getcompindex  s    	zthermopack.getcompindexc                 C   sh   |    d}td| }t|}t|}ttttg| j_d| j_| t||| |j	
d }|S )zGet component name

        Args:
            int: Component FORTRAN index

        Returns:
            comp (str): Component name
        r{   r|   Nr}   )rz   r~   r   rv   ru   rL   rw   rx   r   r7   r   r   )ro   indexr   r   Z
comp_len_cZindex_cZcompnamerp   rp   rq   get_comp_name  s    	
zthermopack.get_comp_namec                 C   s8   |    t|}ttg| j_t| j_| t|}|S )zGet component mole weight (g/mol)

        Args:
            comp (int): Component FORTRAN index

        Returns:
            float: Component mole weight (g/mol)
        )rz   rv   ru   rC   rw   r5   rx   r   )ro   r   r   Zmw_irp   rp   rq   r     s    	zthermopack.compmoleweightc              	   C   s   |    t|}ttttttttttttg| j_d| j_td}td}td}td}td}| t|t|t|t|t|t| |jS )z
        Get acentric factor of component i
        Args:
            i (int) component FORTRAN index
        returns:
            float: acentric factor
        N        )	rz   rv   ru   r5   rD   rw   rx   r   r7   )ro   ir   wZtciZpciZvciZtnbirp   rp   rq   acentric_factor  s.    
zthermopack.acentric_factorc              
   C   s   t  }t  }t  }t  }t  }t  }t  }tt tt tt tt tt tt tt g| j_d| j_| t|t|t|t|t|t|t| |j| _|j| _|j| _	|j| _
|j| _|j| _|j| _dS )zkGet phase identifiers used by thermopack

        Returns:
            int: Phase int  identifiers
        N)rv   ru   r,   rw   rx   r   r7   ZTWOPHLIQPHVAPPHZ
MINGIBBSPHZSINGLEPHZSOLIDPHZFAKEPH)ro   ZiTWOPHZiLIQPHZiVAPPHZiMINGIBBSPHZ	iSINGLEPHZiSOLIDPHZiFAKEPHrp   rp   rq   r-   &  s>    
zthermopack.get_phase_flagsc                 C   s   g d}|| S )zGet phase type

        Args:
            i_phase (int): Phase flag returned by thermopack

        Returns:
            str: Phase type
        )Z	TWO_PHASEZLIQUIDZVAPORZMINIMUM_GIBBSZSINGLEZSOLIDZFAKErp   )ro   Zi_phaseZphase_string_listrp   rp   rq   get_phase_typeK  s    	zthermopack.get_phase_typec                 C   s   || j _dS )zSet minimum temperature in Thermopack. Used to limit search
        domain for numerical solvers.

        Args:
            temp (float): Temperature (K)
        Nr9   r7   ro   temprp   rp   rq   set_tminX  s    zthermopack.set_tminc                 C   s   | j j}|S )zGet minimum temperature in Thermopack. Used to limit search
        domain for numerical solvers.

        Returns:
            float: Temperature (K)
        r   r   rp   rp   rq   get_tmina  s    zthermopack.get_tminc                 C   s   || j _dS )zGet minimum pressure in Thermopack. Used to limit search
        domain for numerical solvers.

        Args:
            press (float): Pressure (Pa)
        N)r:   r7   )ro   pressrp   rp   rq   set_pmink  s    zthermopack.set_pminc              
   C   sZ  |    tt }t|}	t|}
tt| | }t|}td}|du rP|}ntttd}|du rn|}ntttd}|du r|}ntt| d}ttttttttttttttttg| j_d| j_| t|	t|
|t|t|||| |j	f}|dur$||d f7 }|dur<||d f7 }|durV|t
|f7 }|S )a   Calculate single-phase specific volume
            Note that the order of the output match the default order of input for the differentials.
            Note further that dvdt, dvdp and dvdn only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Molar composition
            phase (int): Calcualte root for specified phase
            dvdt (logical, optional): Calculate molar volume differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dvdp (logical, optional): Calculate molar volume differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dvdn (logical, optional): Calculate molar volume differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.

        Returns:
            float: Specific volume (m3/mol), and optionally differentials
        r   Nr   )rz   ru   r5   r   rv   r>   rw   rx   r   r7   nparray)ro   r   r   xphaseZdvdtZdvdpZdvdnr   temp_cpress_cx_cphase_cv_cZdvdt_cZdvdp_cZdvdn_creturn_tuplerp   rp   rq   specific_volumex  sV    
	



zthermopack.specific_volumec              
   C   sZ  |    tt }t|}	t|}
tt| | }t|}td}|du rP|}ntttd}|du rn|}ntttd}|du r|}ntt| d}ttttttttttttttttg| j_d| j_| t|	t|
|t|t|||| |j	f}|dur$||d f7 }|dur<||d f7 }|durV|t
|f7 }|S )a   Calculate single-phase compressibility
            Note that the order of the output match the default order of input for the differentials.
            Note further that dzdt, dzdp and dzdn only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Molar composition
            phase (int): Calcualte root for specified phase
            dzdt (logical, optional): Calculate compressibility differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dzdp (logical, optional): Calculate compressibility differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dzdn (logical, optional): Calculate compressibility differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.

        Returns:
            float: Compressibility (-), and optionally differentials
        r   Nr   )rz   ru   r5   r   rv   r?   rw   rx   r   r7   r   r   )ro   r   r   r   r   ZdzdtZdzdpZdzdnr   r   r   r   r   z_cZdzdt_cZdzdp_cZdzdn_cr   rp   rp   rq   r     sV    
	



zthermopack.zfacc
                 C   sR  |    tt }
t|}t|}tt| | }t|}tt| d}|du rX|
}ntt| d}|du rv|
}ntt| d}|du r|
}ntt|d  d}|du rtt }ntttd}tt }|	du r|
}ntttd}ttttttttttttttttttttttg| j_d| j_| t|t||t|||||||| t	
|f}|dur|t	
|f7 }|dur|t	
|f7 }|durt	t|t|f}tt|D ]6}tt|D ]"}|||t|   || |< qq||f7 }|dur6||d f7 }|	durN||d f7 }|S )a   Calculate logarithm of fugacity coefficient given composition,
        temperature and pressure.
        Note that the order of the output match the default order of input for the differentials.
        Note further that dlnfugdt, dlnfugdp, dlnfugdn and ophase only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Molar composition (.)
            phase (int): Calcualte root for specified phase
            dlnfugdt (logical, optional): Calculate fugacity coefficient differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dlnfugdp (logical, optional): Calculate fugacity coefficient differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dlnfugdn (logical, optional): Calculate fugacity coefficient differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.
            ophase (int, optional): Phase flag. Only set when phase=MINGIBBSPH.
            v (float, optional): Specific volume (m3/mol)
        Returns:
            ndarray: fugacity coefficient (-), and optionally differentials
        r   N   r   )rz   ru   r5   r   rv   r@   rw   rx   r   r   r   zerosrange)ro   r   r   r   r   dlnfugdtdlnfugdpdlnfugdnZophasevr   r   r   r   r   lnfug_c
dlnfugdt_c
dlnfugdp_c
dlnfugdn_cZophase_cZmetaExtremum_cr   r   
dlnfugdn_rr   jrp   rp   rq   r      s    





$


zthermopack.thermoFc	                 C   s  |    tt }	t|}
t|}tt| | }t|}td}|du rP|	}ntttd}|du rn|	}ntttd}|du r|	}ntt| d}|rtttd}ntttd}ttttttttttttttttttg	| j_d| j_| t|
t||t|t|||||	 |j	f}|durR||d f7 }|durj||d f7 }|dur|t
|f7 }|S )ah   Calculate specific single-phase enthalpy
            Note that the order of the output match the default order of input for the differentials.
            Note further that dhdt, dhdp and dhdn only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Molar composition
            phase (int): Calcualte root for specified phase
            dhdt (logical, optional): Calculate enthalpy differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dhdp (logical, optional): Calculate enthalpy differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dhdn (logical, optional): Calculate enthalpy differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.
            residual (logical, optional): Calculate residual enthalpy. Defaults to False.

        Returns:
            float: Specific enthalpy (J/mol), and optionally differentials
        r   Nr   r   )rz   ru   r5   r   rv   rB   rw   rx   r   r7   r   r   )ro   r   r   r   r   dhdtdhdpdhdnresidualr   r   r   r   r   h_cdhdt_cdhdp_cdhdn_c
residual_cr   rp   rp   rq   r   _  s`    






zthermopack.enthalpyc	                 C   s  |    tt }	t|}
t|}tt| | }t|}td}|du rP|	}ntttd}|du rn|	}ntttd}|du r|	}ntt| d}|rtttd}ntttd}ttttttttttttttttttg	| j_d| j_| t|
t||t|t|||||	 |j	f}|durR||d f7 }|durj||d f7 }|dur|t
|f7 }|S )ad   Calculate specific single-phase entropy
            Note that the order of the output match the default order of input for the differentials.
            Note further that dsdt, dhsp and dsdn only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Molar composition
            phase (int): Calcualte root for specified phase
            dsdt (logical, optional): Calculate entropy differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dsdp (logical, optional): Calculate entropy differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dsdn (logical, optional): Calculate entropy differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.
            residual (logical, optional): Calculate residual entropy. Defaults to False.

        Returns:
            float: Specific entropy (J/mol/K), and optionally differentials
        r   Nr   r   )rz   ru   r5   r   rv   rA   rw   rx   r   r7   r   r   )ro   r   r   r   r   dsdtdsdpdsdnr   r   r   r   r   r   s_cdsdt_cdsdp_cdsdn_cr   r   rp   rp   rq   r     s`    


	


zthermopack.entropyc           
      C   s   |    tt }t|}t|}td}|du r8|}ntttd}ttttttttg| j_d| j_| t|t|t|| |jf}	|dur|	|d f7 }	|	S )a   Calculate specific ideal enthalpy
            Note that the order of the output match the default order of input for the differentials.
            Note further that dhdt, and dhdp only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            j (array_like): Component index
            dhdt (logical, optional): Calculate ideal enthalpy differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dhdp (logical, optional): Calculate ideal enthalpy differentials with respect to pressure while temperature and composition are held constant. Defaults to None.

        Returns:
            float: Specific ideal enthalpy (J/mol), and optionally differentials
        r   Nr   )	rz   ru   r5   rv   rE   rw   rx   r   r7   )
ro   r   r   r   r   r   j_cr   r   r   rp   rp   rq   r     s.    

zthermopack.idealenthalpysinglec                 C   sL   |    t|}t|}ttttg| j_d| j_| t|t| dS )z Set specific ideal entropy reference value

        Args:
            j (integer): Component index
            s0 (float): Ideal entropy reference (J/mol/K)
        N)rz   rv   r5   ru   rG   rw   rx   r   )ro   r   s0r   s0_crp   rp   rq   !set_ideal_entropy_reference_value#  s    
z,thermopack.set_ideal_entropy_reference_valuec                 C   sN   |    t|}td}ttttg| j_d| j_| t|t| |jS )z Get specific ideal entropy reference value

        Args:
            j (integer): Component index

        Returns:
            float: Specific ideal entropy (J/mol/K)
        r   N)	rz   rv   r5   ru   rF   rw   rx   r   r7   )ro   r   r   r   rp   rp   rq   !get_ideal_entropy_reference_value7  s    	
z,thermopack.get_ideal_entropy_reference_valuec                 C   sL   |    t|}t|}ttttg| j_d| j_| t|t| dS )z Set specific ideal enthalpy reference value

        Args:
            j (integer): Component index
            h0 (float): Ideal enthalpy reference (J/mol)
        N)rz   rv   r5   ru   rI   rw   rx   r   )ro   r   Zh0r   h0_crp   rp   rq   "set_ideal_enthalpy_reference_valueO  s    
z-thermopack.set_ideal_enthalpy_reference_valuec                 C   sN   |    t|}td}ttttg| j_d| j_| t|t| |jS )z Get specific ideal enthalpy reference value

        Args:
            j (integer): Component index

        Returns:
            float: Specific ideal enthalpy (J/mol)
        r   N)	rz   rv   r5   ru   rH   rw   rx   r   r7   )ro   r   r   r   rp   rp   rq   "get_ideal_enthalpy_reference_valuec  s    	
z-thermopack.get_ideal_enthalpy_reference_valuec	                 C   s   |    t|}	t|}
tt| | }tt| | }tt| | }t|}t|}t|}tt }ttttttttttttttttttg	| j_t| j_| t|	t|
|||t|t|t||	}|S )af  Calculate speed of sound for single phase or two phase mixture assuming
        mechanical, thermal and chemical equilibrium.

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (array_like): Liquid molar composition
            y (array_like): Gas molar composition
            z (array_like): Overall molar composition
            betaV (float): Molar gas phase fraction
            betaL (float): Molar liquid phase fraction
            phase (int): Calcualte root for specified phase

        Returns:
            float: Speed of sound (m/s)
        )	rz   r5   r   rv   ru   rJ   rw   rx   r   )ro   r   r   r   yzZbetaVZbetaLr   r   r   r   y_cr   betaV_cbetaL_cr   Zph_cZsosrp   rp   rq   r   {  s@    



zthermopack.speed_of_soundc                 C   s0   t |}tt g| j_d| j_| t| dS )zjSet tolerance of isobaric-isentalpic (PH) flash

        Args:
            tol (float): Tolerance
        N)r5   ru   rM   rw   rx   r   )ro   toltol_crp   rp   rq   set_ph_tolerance  s    zthermopack.set_ph_tolerancec              
   C   s   |    t|}t|}tt| | }tt| d}tt| d}td}	td}
td}ttttttttttttttttg| j_d| j_| t|t||t|	t|
t||| t	
|}t	
|}|||	j|
j|jfS )a  Do isothermal-isobaric (TP) flash

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            z (array_like): Overall molar composition

        Returns:
            x (ndarray): Liquid molar composition
            y (ndarray): Gas molar composition
            betaV (float): Molar gas phase fraction
            betaL (float): Molar liquid phase fraction
            phase (int): Phase identifier (iTWOPH/iLIQPH/iVAPPH)
        r   r   N)rz   r5   r   rv   ru   rN   rw   rx   r   r   r   r7   )ro   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   rp   rq   two_phase_tpflash  s>    	
	

zthermopack.two_phase_tpflashc                 C   sT  |    t|}tt| | }t|}|durBttt|}ntttd}tt| d}	tt| d}
td}td}td}td}ttttttttttttttttttttg
| j_d| j_| |t||t|t||	|
t|t|t|
 |j	dkr&t
dt|	}t|
}|d |||j	|j	|j	fS )a_  Do isentropic-isobaric (SP) flash

        Args:
            press (float): Pressure (Pa)
            z (array_like): Overall molar composition
            entropy (float): Specific entropy (J/mol/K)
            temp (float, optional): Initial guess for temperature (K)

        Returns:
            temp (float): Temperature (K)
            x (ndarray): Liquid molar composition
            y (ndarray): Gas molar composition
            betaV (float): Molar gas phase fraction
            betaL (float): Molar liquid phase fraction
            phase (int): Phase identifier (iTWOPH/iLIQPH/iVAPPH)
        Nr   r   zPS flash calclualtion failed)rz   r5   r   ru   rv   rO   rw   rx   r   r7   	Exceptionr   r   )ro   r   r   r   r   r   r   r   r   r   r   r   r   r   ierr_cr   r   rp   rp   rq   two_phase_psflash  sR    

zthermopack.two_phase_psflashc                 C   sT  |    t|}tt| | }t|}|durBttt|}ntttd}tt| d}	tt| d}
td}td}td}td}ttttttttttttttttttttg
| j_d| j_| |t||t|t||	|
t|t|t|
 |j	dkr&t
dt|	}t|
}|d |||j	|j	|j	fS )a`  Do isenthalpic-isobaric (HP) flash

        Args:
            press (float): Pressure (Pa)
            z (array_like): Overall molar composition
            enthalpy (float): Specific enthalpy (J/mol)
            temp (float, optional): Initial guess for temperature (K)

        Returns:
            temp (float): Temperature (K)
            x (ndarray): Liquid molar composition
            y (ndarray): Gas molar composition
            betaV (float): Molar gas phase fraction
            betaL (float): Molar liquid phase fraction
            phase (int): Phase identifier (iTWOPH/iLIQPH/iVAPPH)
        Nr   r   zPH flash calclualtion failed)rz   r5   r   ru   rv   rQ   rw   rx   r   r7   r   r   r   )ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   rp   rq   two_phase_phflash2  sR    

zthermopack.two_phase_phflashc                 C   s\  |    tt| | }t|}t|}|durBttt|}	ntttd}	|durlttt|}
ntttd}
tt| d}tt| d}td}td}td}ttttttttttttttttttttg
| j_| |	|
|t|t|||t|t|t|
 t	|}t	|}|	d |
d |||j
|j
|j
fS )a  Do isoenergetic-isochoric (UV) flash

        Args:
            press (float): Pressure (Pa)
            z (array_like): Overall molar composition
            specific_energy (float): Specific energy (J/mol)
            specific_volume (float): Specific volume (m3/mol)
            temp (float, optional): Initial guess for temperature (K)
            press (float, optional): Initial guess for pressure (Pa)

        Returns:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)
            x (ndarray): Liquid molar composition
            y (ndarray): Gas molar composition
            betaV (float): Molar gas phase fraction
            betaL (float): Molar liquid phase fraction
            phase (int): Phase identifier (iTWOPH/iLIQPH/iVAPPH)
        Nr   r   )rz   r5   r   ru   rv   rP   rw   r   r   r   r7   )ro   r   Zspecific_energyr   r   r   r   e_cr   r   r   r   r   r   r   r   r   r   rp   rp   rq   two_phase_uvflasht  sP    

zthermopack.two_phase_uvflashc                 C   s   |    t|}t|}tt| | }tt }|}|}	|}
ttttttttttttg| j_t| j_| t|t||||	|
}|S )aL  If only one root exsist for the equation of state the phase type can be
        determined from either the psedo-critical volume or a volume ratio to the co-volume

        Args:
            temp (float): Temperature (K)
            press (float): Pressure (Pa)

        Returns:
            int: Phase int (VAPPH or LIQPH)
        )	rz   r5   r   ru   rR   rw   rv   rx   r   )ro   r   r   r   r   r   r   r   Ztemp_comp_cZpress_comp_cZ
vb_ratio_cr   rp   rp   rq   guess_phase  s0    

zthermopack.guess_phasec              
   C   sT  |    t|}t|}tt| | }	tt }
|du r@|
}ntttd}|du r^|
}ntttd}|
}|du r|
}ntt| d}tttd}ttttttttttttttttg| j_t| j_| t|t||	|||||}|f}|dur||d f7 }|dur6||d f7 }|durP|t	
|f7 }|S )a8  Calculate pressure given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dpdt (No type, optional): Flag to activate calculation. Defaults to None.
            dpdv (No type, optional): Flag to activate calculation. Defaults to None.
            dpdn (No type, optional): Flag to activate calculation. Defaults to None.

        Returns:
            float: Pressure (Pa)
            Optionally pressure differentials
        Nr   r   r   )rz   r5   r   ru   rv   rU   rw   rx   r   r   r   )ro   r   volumenZdpdtZdpdvZdpdnr   r   n_cr   Zdpdt_cZdpdv_cZd2pdv2_cZdpdn_cZrecalculate_cPr   rp   rp   rq   pressure_tv  sV    
	
	


zthermopack.pressure_tvIRc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate internal energy given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dedt (No type, optional): Flag to activate calculation. Defaults to None.
            dedv (No type, optional): Flag to activate calculation. Defaults to None.
            dedn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Energy (J)
            Optionally energy differentials
        r   Nr   )rz   r5   r   ru   r   get_contribution_flagrv   rS   rw   rx   r   r7   r   r   )ro   r   r   r   ZdedtZdedvZdednproperty_flagr   r   r   r   r   Zdedt_cZdedv_cZdedn_ccontribution_cr   rp   rp   rq   r   %  sV    

	
	


zthermopack.internal_energy_tvc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate entropy given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dsdt (No type, optional): Flag to activate calculation. Defaults to None.
            dsdv (No type, optional): Flag to activate calculation. Defaults to None.
            dsdn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Entropy (J/K)
            Optionally entropy differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   rT   rw   rx   r   r7   r   r   )ro   r   r   r   r   Zdsdvr   r   r   r   r   r   r   r   Zdsdv_cr   r   r   rp   rp   rq   r   j  sV    

	
	


zthermopack.entropy_tvc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate enthalpy given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dhdt (No type, optional): Flag to activate calculation. Defaults to None.
            dhdv (No type, optional): Flag to activate calculation. Defaults to None.
            dhdn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Enthalpy (J)
            Optionally enthalpy differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   rW   rw   rx   r   r7   r   r   )ro   r   r   r   r   Zdhdvr   r   r   r   r   r   r   r   Zdhdv_cr   r   r   rp   rp   rq   r     sV    

	
	


zthermopack.enthalpy_tvc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate Helmholtz energy given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dadt (No type, optional): Flag to activate calculation. Defaults to None.
            dadv (No type, optional): Flag to activate calculation. Defaults to None.
            dadn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.
        Returns:
            float: Helmholtz energy (J)
            Optionally energy differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   rX   rw   rx   r   r7   r   r   )ro   r   r   r   ZdadtZdadvZdadnr   r   r   Za_cr   r   Zdadt_cZdadv_cZdadn_cr   r   rp   rp   rq   helmholtz_tv  sV    

	
	


zthermopack.helmholtz_tvc              
   C   s  |    t|}t|}	tt| d}
tt| | }tt }|du rP|}ntt| d}|du rn|}ntt| d}|du r|}ntt|d  d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	||
|||| t|
f}|dur0|t|f7 }|durJ|t|f7 }|durtt|t|f}tt|D ]6}tt|D ]"}|||t|   || |< qqv|t|f7 }|S )a  Calculate chemical potential given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dmudt (No type, optional): Flag to activate calculation. Defaults to None.
            dmudv (No type, optional): Flag to activate calculation. Defaults to None.
            dmudn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Chemical potential (J/mol)
            Optionally chemical potential differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   rY   rw   rx   r   r   r   r   r   )ro   r   r   r   ZdmudtZdmudvZdmudnr   r   r   Zmu_cr   r   Zdmudt_cZdmudv_cZdmudn_cr   r   r   r   rp   rp   rq   r   8  s^    

	
	


$z thermopack.chemical_potential_tvc              	   C   s  |    t|}t|}tt| d}	tt| | }
tt }|du rP|}ntt| d}|du rn|}ntt| d}|du r|}ntt|d  d}ttttttttttttttg| j_d| j_| t|t||
|	||| t	|	f}|dur|t	|f7 }|dur8|t	|f7 }|durt
t|t|f}tt|D ]6}tt|D ]"}|||t|   || |< qtqd||f7 }|S )aa  Calculate natural logarithm of fugacity given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dlnphidt (No type, optional): Flag to activate calculation. Defaults to None.
            dlnphidv (No type, optional): Flag to activate calculation. Defaults to None.
            dlnphidn (No type, optional): Flag to activate calculation. Defaults to None.

        Returns:
            ndarry: Natural logarithm of fugacity
            Optionally differentials
        r   Nr   )rz   r5   r   ru   rV   rw   rx   r   r   r   r   r   )ro   r   r   r   ZdlnphidtZdlnphidvZdlnphidnr   r   Zlnphi_cr   r   Z
dlnphidt_cZ
dlnphidv_cZ
dlnphidn_cr   r   r   rp   rp   rq   fugacity_tv  sX    




$
zthermopack.fugacity_tvc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate entropy given temperature, pressure and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dsdt (No type, optional): Flag to activate calculation. Defaults to None.
            dsdp (No type, optional): Flag to activate calculation. Defaults to None.
            dsdn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Entropy (J/K)
            Optionally entropy differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   rZ   rw   rx   r   r7   r   r   )ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   rp   rq   r     sV    

	
	


zthermopack.entropy_tvpc              
   C   sX  |    t|}t|}	td}
tt| | }tt }|du rH|}ntttd}|du rf|}ntttd}|du r|}ntt| d}t|}ttttttttttttttttg| j_d| j_	| t
|t
|	|t
|
|||| |
jf}|dur"||d f7 }|dur:||d f7 }|durT|t|f7 }|S )a  Calculate enthalpy given temperature, volume and mol numbers.

        Args:
            temp (float): Temperature (K)
            volume (float): Volume (m3)
            n (array_like): Mol numbers (mol)
            dhdt (No type, optional): Flag to activate calculation. Defaults to None.
            dhdp (No type, optional): Flag to activate calculation. Defaults to None.
            dhdn (No type, optional): Flag to activate calculation. Defaults to None.
            property_flag (integer, optional): Calculate residual (R) and/or ideal (I) entropy. Defaults to IR.

        Returns:
            float: Enthalpy (J)
            Optionally enthalpy differentials
        r   Nr   )rz   r5   r   ru   r   r   rv   r\   rw   rx   r   r7   r   r   )ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   rp   rq   r      sV    

	
	


zthermopack.enthalpy_tvpc              	   C   s  |    tt }t|}	t|}
tt| | }tt| d}|du rP|}ntt| d}|du rn|}ntt| d}|du r|}ntt|d  d}ttttttttttttttg| j_d| j_| t|	t|
||||| t	|f}|dur|t	|f7 }|dur8|t	|f7 }|durt
t|t|f}tt|D ]6}tt|D ]"}|||t|   || |< qtqd||f7 }|S )aD   Calculate logarithm of fugacity coefficient given molar numbers,
        temperature and pressure.
        Note that the order of the output match the default order of input for the differentials.
        Note further that dlnfugdt, dlnfugdp, dlnfugdn and ophase only are flags to enable calculation.

        Args:
            temp (float): Temperature (K)
            v (float): Volume (m3)
            n (array_like): Molar numbers (mol)
            dlnfugdt (logical, optional): Calculate fugacity coefficient differentials with respect to temperature while pressure and composition are held constant. Defaults to None.
            dlnfugdp (logical, optional): Calculate fugacity coefficient differentials with respect to pressure while temperature and composition are held constant. Defaults to None.
            dlnfugdn (logical, optional): Calculate fugacity coefficient differentials with respect to mol numbers while pressure and temperature are held constant. Defaults to None.
        Returns:
            ndarray: fugacity coefficient (-), and optionally differentials
        r   Nr   )rz   ru   r5   r   r[   rw   rx   r   r   r   r   r   )ro   r   r   r   r   r   r   r   r   r   vol_cr   r   r   r   r   r   r   r   r   rp   rp   rq   r   Q  sX    




$
zthermopack.thermo_tvpc           	      C   s   |    t|}tt| d}tt| | }td}ttttttttg| j_t| j_| t|||t|}t	
|}|jdkrtd||fS )aI  Calculate bubble temperature given pressure and composition

        Args:
            press (float): Pressure (Pa)
            z (array_like): Composition (-)

        Raises:
            Exception: Faild to calculate

        Returns:
            float: Temperature (K)
            ndarray: Incipient phase composition
        r   r   z&bubble_temperature calclualtion failed)rz   r5   r   rv   ru   r]   rw   rx   r   r   r   r7   r   )	ro   r   r   r   r   r   r   r   r   rp   rp   rq   bubble_temperature  s(    


zthermopack.bubble_temperaturec           	      C   s   |    t|}tt| d}tt| | }td}ttttttttg| j_t| j_| t|||t|}t	
|}|jdkrtd||fS )aH  Calculate bubble pressure given temperature and composition

        Args:
            temp (float): Temperature (K)
            z (array_like): Composition (-)

        Raises:
            Exception: Faild to calculate

        Returns:
            float: Pressure (Pa)
            ndarray: Incipient phase composition
        r   r   #bubble_pressure calclualtion failed)rz   r5   r   rv   ru   r^   rw   rx   r   r   r   r7   r   )	ro   r   r   r   r   r   r   r   r   rp   rp   rq   bubble_pressure  s(    


zthermopack.bubble_pressurec           	      C   s   |    t|}tt| d}tt| | }td}ttttttttg| j_t| j_| t|||t|}t	
|}|jdkrtd||fS )aR  Calculate dew temperature given pressure and composition

        Args:
            temp (float): Pressure (Pa)
            z (float): Compositon (-)

        Raises:
            Exception: Not able to solve for dew point

        Returns:
            float : Temperature (K)
            ndarray : Incipient phase composition (-)
        r   r   z#dew_temperature calclualtion failed)rz   r5   r   rv   ru   r_   rw   rx   r   r   r   r7   r   )	ro   r   r   r   r   r   r   r   r   rp   rp   rq   dew_temperature  s(    


zthermopack.dew_temperaturec           	      C   s   |    t|}tt| d}tt| | }td}ttttttttg| j_t| j_| t|||t|}t	
|}|jdkrtd||fS )aR  Calculate dew pressure given temperature and composition

        Args:
            temp (float): Temperature (K)
            z (float): Compositon (-)

        Raises:
            Exception: Not able to solve for dew point

        Returns:
            float : Pressure (Pa)
            ndarray : Incipient phase composition (-)
        r   r   r   )rz   r5   r   rv   ru   r`   rw   rx   r   r   r   r7   r   )	ro   r   r   r   r   r   r   r   r   rp   rp   rq   dew_pressure		  s(    


zthermopack.dew_pressure    8lAc           #      C   s  |    d}tt| | }td}	t|}
td}td}t|}t|}t| d}t| d}t|t|  d}t| d}td}tt }|}|}|du r|}nttt|}tt }|du r|}nttt|}ttttttttttttttttttttttttttttttttttg| j_d| j_| |t|	t|
t|t|t|t|||||t|||||| t	
|d|j }t	
|d|j }||f}|rt	|dkrt	d|j }t	d|j }t	|}t|jD ]} ||  || < ||  ||  d < ||  || < ||  ||  d < | ||  ||  || j\|| < | ||  ||  || j\||  d < q|||f}n`t	|}!t|jD ]@} ||  dkr| j}"n| j}"| ||  ||  ||"\|!| < q||!f7 }|S )	a  Get the phase-envelope

        Args:
            initial_pressure (float): Start mapping form dew point at initial pressure (Pa).
            z (array_like): Composition (-)
            maximum_pressure (float , optional): Exit on maximum pressure (Pa). Defaults to 1.5e7.
            minimum_temperature (float , optional): Exit on minimum pressure (Pa). Defaults to None.
            step_size (float , optional): Tune step size of envelope trace. Defaults to None.
            calc_v (bool, optional): Calculate specifc volume of saturated phase? Defaults to False
        Returns:
            ndarray: Temperature values (K)
            ndarray: Pressure values (Pa)
            ndarray (optional): Specific volume (m3/mol)
          r   r   g      ?r   Nr   g      ?)rz   r5   r   rv   ru   ra   rw   rx   r   r   r   r7   Zamaxr   Z
zeros_liker   r   r   r   )#ro   initial_pressurer   maximum_pressureminimum_temperatureZ	step_sizeZcalc_vnmaxr   r   r   Zspec_cZ	beta_in_cmax_press_cnmax_cZTa_cZPa_cZKi_cZbeta_cr   r   Zcriconden_cZcrit_cZds_cZexitOnTriplePoint_cZtme_ct_valsp_valsr   Zt_vals_singleZp_vals_singleZv_vals_singler   v_valsr   rp   rp   rq   get_envelope_twophase.	  s    





z thermopack.get_envelope_twophase     j@~jth?{Gz?c           %      C   s  |    d}t|}td}td}	td}
t|}t|}t|}t|}d}t|d}tt|}t|d  d}td d}td}tttttttttttttttttttttttttg| j_	d	| j_
| t|t|
t|	t|t|t||t|||t|t|| |d }|d }|d
 }|dkrt|}t|}t|}t|D ]>}||d  ||< ||d d  ||< ||d d
  ||< qv|||f}nd}|dkrFt|}t|}t|}t|D ]B}||d d  ||< ||d d  ||< ||d d  ||< q|||f} nd} |dkrt|}!t|}"t|}#t|D ]B}||d d  |!|< ||d d  |"|< ||d d  |#|< qz|!|"|#f}$nd}$|| |$fS )aS  Calculate binary three phase envelope

        Args:
            temp (float): Temperature (K)
            maximum_pressure (float, optional): Exit on maximum pressure (Pa). Defaults to 1.5e7.
            minimum_pressure (float, optional): Exit on minimum pressure (Pa). Defaults to 1.0e5.
            maximum_dz (float, optional): [description]. Defaults to 0.003.
            maximum_dlns (float, optional): [description]. Defaults to 0.01.

        Returns:
            tuple of arrays: LLE, L1VE, L2VE

            LLE : Liquid 1 - Liquid 2 Equilibrium
                LLE[0] : Liquid 1 composition (mole fraction of component 1)
                LLE[1] : Liquid 2 composition (mole fraction of component 1)
                LLE[2] : Pressure [Pa]
            L1VE : Liquid 1 - Vapour Equilibrium
                L1VE[0] : Bubble line composition (mole fraction of component 1) 
                L1VE[1] : Dew line composition (mole fraction of component 1)
                L1VE[2] : Pressure [Pa]
            L2VE : Liquid 2 - Vapour Equilibrium
                L2VE[0] : Bubble line composition (mole fraction of component 1) 
                L2VE[1] : Dew line composition (mole fraction of component 1)
                L2VE[2] : Pressure [Pa]

            If one or more of the equilibria are not found the corresponding tuple is (None, None, None)
        i'  r   r   zbinaryVLLE.datr}   	      r   Nr   )NNN         r	      )rz   r5   rv   r~   r   r   r   ru   rb   rw   rx   r   r   r   r   )%ro   r   r   minimum_pressureZ
maximum_dzZmaximum_dlnsr   r   
min_temp_cZispec_cr   r  min_press_cZdz_max_cZ
dlns_max_cfilename
filename_cfilename_lenZres_cZnres_cZwsf_cZnLLEZnL1VEZnL2VEZxLLEZwLLEZpLLEr   ZLLEZxL1VEZwL1VEZpL1VEZL1VEZxL2VEZwL2VEZpL2VEZL2VErp   rp   rq   get_binary_pxy	  s    "












zthermopack.get_binary_pxyc                 C   s\   d}t d| }t|}t|}ttt tg| j_d| j_| t||| |j	d}|S )zGet error description for binary plot error

        Args:
            i_term (int): binary plot error identifyer

        Returns:
            str: Error message
        2   r|   Nr}   )
r~   r   rv   ru   rd   rw   rx   r   r7   r   )ro   Zi_termZmessage_lenZ	message_ci_term_cmessagerp   rp   rq   r"   
  s    

zthermopack.get_bp_term     b@     @@c              
   C   s   |    t|}t|}t|}t|}	t|r2dnd}
d}t|d}tt|}td}ttttttttttttttg| j_	d| j_
| t|t|t|	|t|t|t|
| |jdks| |j}t| t|S )a  Calculate global binary phase envelope

        Args:
            maximum_pressure (float, optional): Exit on maximum pressure (Pa). Defaults to 1.5e7.
            minimum_pressure (float, optional): Exit on minimum pressure (Pa). Defaults to 1.0e5.
            minimum_temperature (float, optional): Terminate phase line traceing at minimum temperature. Defaults to 150.0 K.
            maximum_temperature (float, optional): Terminate phase line traceing at maximum temperature. Defaults to 500.0 K.
            include_azeotropes (bool, optional): Include azeotropic lines. Defaults to False.

        Returns:
            tuple of arrays
        r   r   zglobal_binary.datr}   N)rz   r5   rv   r~   r   r   r   ru   rc   rw   rx   r   r7   r"   printr   Zget_globa_binary_data)ro   r   r  r   maximum_temperatureZinclude_azeotropesr  r  Z
max_temp_cr  Z	az_bool_cr  r  r  r  r  rp   rp   rq   r!   :
  sB    	
	
zthermopack.global_binary_plot     @e@c              
   C   s   |    tt| | }td}t|}t|}	d}
t|
d}tt|
}td}t|r`dnd}|  }| | t	tt	tt	tt	tt	ttt	ttg| j
_d| j
_| 
|t|t|t|t|	|t|| | | t|
S )a  Calculate phase envelope including solid lines

        Args:
            initial_pressure (float): Start mapping from initial pressure (Pa).
            z (array_like): Composition (-)
            maximum_pressure (float , optional): Exit on maximum pressure (Pa). Defaults to 1.5e7.
            calc_esv (bool, optional): Calculate specifc volume of saturated phase? Defaults to False

        Returns:
            tuple of arrays
        r   zsolid_envelope.datr}   r   r   N)rz   r5   r   r~   r   r   rv   r   r   ru   re   rw   rx   r   r   Zget_solid_envelope_data)ro   r   r   r   r   Zcalc_esvr   r   r   r  r  r  r  Zi_spec_cZ
esv_bool_cZmin_trp   rp   rq   solid_envelope_plotr
  sB    
		
zthermopack.solid_envelope_plotd   c                 C   sH  |    t|}t|}t|}tt| | }	t| d}
t| d}t| d}t| d}t|}td}ttttttttttttttttttttg
| j_d| j_| t|t|t||	t|||
||t|
 t	
|d|j }t	
|
d|j }t	
|d|j }t	
|d|j }||||fS )a  Get iso-therm at specified temperature

        Args:
            temp (float): Temperature (K)
            z (array_like): Composition (-)
            minimum_pressure (float, optional): Map to minimum pressure. Defaults to 1.0e5. (Pa)
            maximum_pressure (float, optional): Map to maximum pressure. Defaults to 1.5e7. (Pa)
            nmax (int, optional): Maximum number of points on iso-therm. Defaults to 100.

        Returns:
           Multiple numpy arrays.
        r   r   N)rz   r5   r   rv   ru   rf   rw   rx   r   r   r   r7   )ro   r   r   r  r   r   r   r:   maximum_pressure_cr   va_cpa_csa_cha_cr  na_cr  r  s_valsh_valsrp   rp   rq   get_isotherm
  sN    
zthermopack.get_isotherm      i@c                 C   sH  |    t|}t|}t|}tt| | }	t| d}
t| d}t| d}t| d}t|}td}ttttttttttttttttttttg
| j_d| j_| t|t|t||	t|||
||t|
 t	
|d|j }t	
|
d|j }t	
|d|j }t	
|d|j }||||fS )a  Get isobar at specified pressure.

        Args:
            press (float): Pressure (Pa)
            z (array_like): Composition (-)
            minimum_temperature (float, optional): Minimum temperature. Defaults to 200.0. (K)
            maximum_temperature (float, optional): Maximum temperature. Defaults to 500.0. (K)
            nmax (int, optional): Maximum number of points on iso-bar. Defaults to 100.

        Returns:
            Multiple numpy arrays.
        r   r   N)rz   r5   r   rv   ru   rg   rw   rx   r   r   r   r7   )ro   r   r   r   r  r   r   r9   maximum_temperature_cr   r"  ta_cr$  r%  r  r&  r  r  r'  r(  rp   rp   rq   
get_isobar
  sN    
zthermopack.get_isobarc                 C   sp  |    t|}t|}	t|}
t|}t|}tt| | }t| d}t| d}t| d}t| d}t|}td}ttttttttttttttttttttttttg| j_d| j_| t|t|	t|
t|t||t|||||t| t	
|d|j }t	
|d|j }t	
|d|j }t	
|d|j }||||fS )a  Get isenthalpy given specified enthalpy.

        Args:
            enthalpy (float): Enthalpy (J/mol)
            z (array_like): Composition (-)
            minimum_pressure (float, optional): Minimum pressure. Defaults to 1.0e5. (Pa)
            maximum_pressure (float, optional): Maximum pressure. Defaults to 1.5e7. (Pa)
            minimum_temperature (float, optional): Minimum temperature. Defaults to 200.0. (K)
            maximum_temperature (float, optional): Maximum temperature. Defaults to 500.0. (K)
            nmax (int, optional): Maximum number of points on isenthalp. Defaults to 100.

        Returns:
            Multiple numpy arrays.
        r   r   N)rz   r5   r   rv   ru   rh   rw   rx   r   r   r   r7   )ro   r   r   r  r   r   r  r   Z
enthalpy_cr:   r!  r9   r+  r   r"  r,  r$  r#  r  r&  r  r  r'  r  rp   rp   rq   get_isenthalp$  sZ    
zthermopack.get_isenthalpc                 C   sp  |    t|}t|}	t|}
t|}t|}tt| | }t| d}t| d}t| d}t| d}t|}td}ttttttttttttttttttttttttg| j_d| j_| t|t|	t|
t|t||t|||||t| t	
|d|j }t	
|d|j }t	
|d|j }t	
|d|j }||||fS )a  Get isentrope at specified entropy.

        Args:
            entropy (float): Entropy (J/mol/K)
            z (array_like): Composition (-)
            minimum_pressure (float, optional): Minimum pressure. Defaults to 1.0e5. (Pa)
            maximum_pressure (float, optional): Maximum pressure. Defaults to 1.5e7. (Pa)
            minimum_temperature (float, optional): Minimum temperature. Defaults to 200.0. (K)
            maximum_temperature (float, optional): Maximum temperature. Defaults to 500.0. (K)
            nmax (int, optional): Maximum number of points on isentrope. Defaults to 100.

        Returns:
            Multiple numpy arrays.
        r   r   N)rz   r5   r   rv   ru   ri   rw   rx   r   r   r   r7   )ro   r   r   r  r   r   r  r   Z	entropy_cr:   r!  r9   r+  r   r"  r,  r%  r#  r  r&  r  r  r(  r  rp   rp   rq   get_isentropek  sZ    
zthermopack.get_isentroper   Hz>c              	   C   s   |    t|}t|}tt| | }td}td}	t|}
ttttttttttttg| j_d| j_| t|t||t|t|
t|	 |j	dkrt
d|j	|j	|	j	fS )a(  Calculate critical point in variables T and V

        Args:
            n (array_like): Mol numbers (mol)
            temp (float, optional): Initial guess for temperature (K). Defaults to 0.0.
            v (float, optional): Initial guess for volume (m3). Defaults to 0.0.
            tol (float, optional): Error tolerance (-). Defaults to 1.0e-8.

        Raises:
            Exception: Failure to solve for critcal point

        Returns:
            float: Temperature (K)
            float: Volume (m3)
            float: Pressure (Pa)
        r   r   Nzcritical calclualtion failed)rz   r5   r   rv   ru   rj   rw   rx   r   r7   r   )ro   r   r   r   r   r   r   r   r   ZP_cr   rp   rp   rq   r#     s2    

zthermopack.criticalc                 C   s   |    ttt|}tt| | }td}td}ttttttttg| j_d| j_| ||t|t| |j|jfS )aD  Calculate (composition-dependent) virial coefficients B and C,
        defined as P/RT = rho + B*rho**2 + C*rho**3 + O(rho**4) as rho->0.

        Args:
            temp (float): Temperature
            n (array_like): Mol numbers (mol)

        Returns:
            float: B (m3/mol)
            float: C (m6/mol2)
        r   N)	rz   ru   r5   r   rk   rw   rx   r   r7   )ro   r   r   r   r   ZB_cZC_crp   rp   rq   virial_coeffcients  s"    zthermopack.virial_coeffcientsc                 C   s   |    ttt|}t| jd  d}ttttg| j_d| j_| || t| j| jf}t	| jD ].}t	| jD ]}|||| j   || |< q|qn|S )a@  Calculate composition-independent virial coefficients B,
        defined as P = RT*rho + B*rho**2 + C*rho**3 + O(rho**4) as rho->0.
        Including cross coefficients.

        Args:
            temp (float): Temperature (K)

        Returns:
            ndarray: B - Second virial coefficient matrix (m3/mol)
        r   r   N)
rz   ru   r5   r8   rl   rw   rx   r   r   r   )ro   r   r   Zbmat_cZbmatr   r   rp   rp   rq   second_virial_matrix  s    zthermopack.second_virial_matrixc                 C   s   |    | jdksJ ttt|}t| jd  d}ttttg| j_d| j_| || t| j| jf}t	| jD ].}t	| jD ]}|||| j   || |< qq||S )aw  Calculate composition-independent virial coefficients C,
        defined as P = RT*rho + B*rho**2 + C*rho**3 + O(rho**4) as rho->0.
        Including cross coefficients
        Currently the code only support binary mixtures

        Args:
            temp (float): Temperature (K)

        Returns:
            ndarray: C - Third virial coefficient matrix (m6/mol2)
        r   r   N)
rz   r8   ru   r5   rm   rw   rx   r   r   r   )ro   r   r   Zcmat_cZcmatr   r   rp   rp   rq   binary_third_virial_matrix#  s    z%thermopack.binary_third_virial_matrixr   c              
   C   s  |    tt| | }t|}t| d}t| d}t| d}td}td}	ttttttttttttttg| j_d| j_| ||||t|t|	t| |j	dkrt
dt|d|	j	 }
t|d|	j	 }t|d|	j	 }|
||fS )a!  Calculate Joule-Thompson inversion curve

        Args:
            temp (float): Temperature (K)
            nmax (int): Array size

        Returns:
            ndarray: temp - Temperature (K)
            ndarray: press - Pressure (Pa)
            ndarray: vol - Volume (m3/mol)
        r   r   Nz-Joule-Thompson inversion curve mapping failed)rz   r5   r   rv   ru   rn   rw   rx   r   r7   r   r   r   )ro   r   r   r   r  r   r   r   r   r   r  r  r  rp   rp   rq   r$   F  s>    	
z#thermopack.joule_thompson_inversion)
NNNr   r   r   NNNN)r   )TNN)NNN)NNN)NNNNN)NNNF)NNNF)N)N)N)NN)NNN)NNNr   )NNNr   )NNNr   )NNNr   )NNNr   )NNN)NNNr   )NNNr   )NNN)r   NNF)r   r  r  r	  )r   r  r  r  F)r   r  F)r  r   r   )r*  r  r   )r  r   r*  r  r   )r  r   r*  r  r   )r   r   r0  )r   )E__name__
__module____qualname____doc__rr   rt   rz   r   r   r   r/   r   r   r   r   r   r   r   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r  r  r"   r!   r  r)  r-  r.  r/  r#   r1  r2  r3  r$   rp   rp   rp   rq   r
      s    *    
 

&!%	

D
D  
_
M
L
+83
A
B
D*
C  
E  
E  
E  
D  
I
G  
E
D  
I%%%%   
v    
}     
8  
;   
@   
@     
J     
K
1#r
   )
__future__r   sysctypesosr   Znumpyr    r   r   r   Zgcc_major_version_greater_thanc_size_tr   rv   objectr
   rp   rp   rp   rq   <module>   s   
