a
    }Cb'/                     @   sh   d Z ddlZddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ G dd deZdS )	an  
Author: Vegard Gjeldvik Jervell
Date: December 2021
Purpose: Implementation of the model proposed by Kempers (J. Chem. Phys. 115, 6330, 2001) for prediction of the Soret coefficient
         doi : http://dx.doi.org/10.1063/1.1398315
Requires: numpy, scipy, ThermoPack (https://github.com/SINTEF/thermopack), KineticGas (https://github.com/vegardjervell/Kineticgas)
    N)gas_constantAvogadro	Boltzmann)root)cubic)
KineticGasc                   @   sX   e Zd ZdddZdddZddd	Zdd
dZdddZdddZdd Z	dd Z
dS )Kempers   Nc           	         s0  |_ d_ fddj dD }d|v r\td| d  t _jj d n~tt	
|tt	
|krd fd	d
t|D }td| d j  d | d  t _jj d n _|du rt	
fdd|dD _nt	|_t|j||d_|_dS )a  
        :param comps (str): comma separated list of components
        :param eos (ThermoPack): Initialized Equation of State object, initialized with components 'comp'
        :param alpha_t0_N (int, optional): Order of approximation of Enskog solutions
        :param sigma (array, optional): Hard sphere diameters [m]
        :param eps_div_k (array, optional): Interaction potential well depth [k_B]
        :param mole_weights (array, optional): Molar masses [g / mol]
           c                    s   g | ]}  |qS  )getcompindex.0compeosr   c/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/kempers01/kempers01.py
<listcomp>       z$Kempers.__init__.<locals>.<listcomp>,zfEquation of state and model must be initialized with same components.
I'm initializing using SRK with z now to avoid crashingZSRKc                 3   s   | ]}  |V  qd S )N)Zget_comp_name)r   ir   r   r   	<genexpr>%   r   z#Kempers.__init__.<locals>.<genexpr>zpEquation of state and model must be initialized with same components in the same order
but are initialized with z and z".
I'm initializing using SRK with Nc                    s    g | ]} j  j |qS r   )r   Zcompmoleweightr   r   )selfr   r   r   0   r   )mole_weightssigma	eps_div_k)compstotal_molessplitwarningswarnr   r   initanynparraysortedjoinr   copyr   kinetic_gas
alpha_t0_N)	r   r   r   r*   r   r   r   Zeoscomp_indsZeoscompsr   )r   r   r   __init__   sD    



"zKempers.__init__Fc                 C   s,  t }| jj||||dd\}}	| jj||||dd\}
}| jj|d|ddd\}}| ||||}| jj||||| jd}|	\}}|\}}|\}}|\}}|d }|| || ||   || | || |   ||  || |d  ||   }t	|| g}|| }|du r$|| }||fS |S d	S )
a  
            Get soret coefficients at specified phase point, for binary mixture, center of volume frame of reference
            :param T (float): Temperature [K]
            :param p (float): Pressure [Pa]
            :param x (ndarray): Composition [mole fraction]
            :param phase (int): ThermoPack phase key
            :param kin (bool, optional): Return kinetic gas value?
            :param BH (bool, optional): Use Barker-Henderson diameters?
            :return: (ndarray) soret coefficients
        Tdvdndhdnh㈵>   BHNr   r   r   N)
r   r   specific_volumeenthalpydmudx_TPr)   alpha_T0r*   r$   r%   )r   Tpxphasekinr3   Rvr-   hr/   h0dh0dndmudxr9   v1v2h1h2h10h20x1x2dmu1dx1alpha_1alphasoretkin_contribr   r   r   get_binary_soret_cov7   s$    P
zKempers.get_binary_soret_covc              	      sP  t dkr | j|||dS t| jj||dd\}| jj||dd\}| jjdddd\}	| ||| jj||| j	d}
t | j
  f	dd	}t||
}|jd
u r tdt d t d t|d   t|j
tj}|j
}n|j
}| }|du rH }||fS |S dS )a  
        Get soret coefficients at specified phase point, center of volume frame of reference
            :param T (float): Temperature [K]
            :param p (float): Pressure [Pa]
            :param x (ndarray): Composition [mole fraction]
            :param phase (int): ThermoPack phase key
            :param kin (bool, optional): Return kinetic gas value?
            :param BH (bool, optional): Use Barker-Henderson diameters?
        :return: (ndarray) soret coefficients
        r1   )r>   Tr,   r.   r0   r2   c                    s   t }td D ]d d  	d     	     d
   	  d d
d   	d     t 	
fddtd D  |< qt
d
    |d < |S )Nr
   r   c                 3   sR   | ]J}|f   d |f d    |  d|    |  V  qdS r   r
   Nr   r   j)rO   rD   r-   r   r<   r   r   r      s   z8Kempers.get_soret_cov.<locals>.eq_set.<locals>.<genexpr>r$   ZzerosrangesumrO   Zeqs	r4   r?   r:   r9   rC   r/   rD   r-   r<   rO   r   r   eq_sety   s    
. 

z%Kempers.get_soret_cov.<locals>.eq_setF+Solution did not converge for composition :, Temperature :z, Pressure : g     j@N)lenrR   r   r   r6   r7   r8   r)   r9   r*   r<   r   successr    r!   strr$   	full_likenanr   r:   r;   r<   r=   r>   r3   r@   rA   rB   Zinitial_guessr\   ZsolvedrO   rP   rQ   r   rZ   r   get_soret_cov]   s,    

.
zKempers.get_soret_covc                 C   s(  t }| j||||\}| jj||||dd\}	}
| jj|d|ddd\}}| ||||}| jj||||| jd}| j\}}|
\}}|\}}|\}}|d }|| || ||   || | || |   ||  || |d  ||   }t	
|| g}|| }|du r || }||fS |S dS )	a  
        Get soret coefficients at specified phase point, for binary mixture, center of mass frame of reference
            :param T (float): Temperature [K]
            :param p (float): Pressure [Pa]
            :param x (ndarray): Composition [mole fraction]
            :param phase (int): ThermoPack phase key
            :param kin (bool, optional): Return kinetic gas value?
            :param BH (bool, optional): Use Barker-Henderson diameters?
        :return: (ndarray) soret coefficients
        Tr.   r0   r1   r2   r5   r   N)r   r   r6   r7   r8   r)   r9   r*   r   r$   r%   )r   r:   r;   r<   r=   r>   r3   r?   r@   rA   r/   rB   rC   rD   r9   m1m2rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   r   r   r   get_binary_soret_com   s(    
6
zKempers.get_binary_soret_comc              	      s<  t dkr"| j||||dS t| j | j||\}| jj||dd\}| jjdddd\}	| ||| jj	||| j
d}
t | j f	dd}t||
}|jd	u rtd
t d t  t|jtj}n|j}| }|du r4 }||fS |S dS )a  
        Get soret coefficients at specified phase point, center of mass frame of reference
            :param T (float): Temperature [K]
            :param p (float): Pressure [Pa]
            :param x (ndarray): Composition [mole fraction]
            :param phase (int): ThermoPack phase key
            :param kin (bool, optional): Return kinetic gas value?
            :param BH (bool, optional): Use Barker-Henderson diameters?
        :return: (ndarray) soret coefficients
        r1   r>   r3   Tr.   r0   r2   c                    s   t }td D ]d d  d          d
     d d
d   d     t 	
fddtd D  |< qt
d
    |d < |S )Nr
   r   c                 3   sR   | ]J}|f    d |f  d    |  d|   |  V  qdS rS   r   rT   )MrO   rD   r   r<   r   r   r      s   z8Kempers.get_soret_com.<locals>.eq_set.<locals>.<genexpr>rV   rY   	rj   r4   r?   r:   r9   rC   r/   rD   r<   r[   r   r\      s    
. 

z%Kempers.get_soret_com.<locals>.eq_setFr]   r^   N)r_   rh   r   r   r   r6   r7   r8   r)   r9   r*   r<   r   r`   r    r!   ra   r$   rb   rc   rd   r   rk   r   get_soret_com   s,    


zKempers.get_soret_comc                 C   s@   |dkr| j ||||||dS |dkr<| j||||||dS dS )a  
        Get soret coefficients at specified phase point
            :param T (float): Temperature [K]
            :param p (float): Pressure [Pa]
            :param x (ndarray): Composition [mole fraction]
            :param phase (int): ThermoPack phase key
            :param mode (str): 'com' or 'cov' to determine mode
            :param kin (bool, optional): Return kinetic gas value?
            :param BH (bool, optional): Use Barker-Henderson diameters?
        :return: (ndarray) Soret coefficients
        Zcovri   comN)re   rl   )r   r:   r;   r<   r=   moder>   r3   r   r   r   	get_soret   s    zKempers.get_soretc                 C   sv   | j j||||dd\}}| j j||| j || j dd\}}| j j||| j || j dd\}	}
|tj|
|dd S )z
        Calculate chemical potential derivative with respect to number of moles at constant temperature and pressure
        :return: ndarray, dmudn[i,j] = dmu_i / dn_j
        Tr,   )dmudn)dpdnr   )Zaxes)r   r6   Zchemical_potential_tvr   Zpressure_tvr$   Z	tensordot)r   r:   r;   r<   r=   r@   r-   muZdmudn_TVZpresrq   r   r   r   dmudn_TP  s    

$zKempers.dmudn_TPc                 C   s$   t |}| ||||}|| j S )z
        Calculate chemical potential derivative with respect to mole fraction of components
        at constant temperature and pressure
        :return: ndarray, dmudx[i,j] = dmu_i / dx_j
        )r$   r%   rs   r   )r   r:   r;   r<   r=   rp   r   r   r   r8     s    
zKempers.dmudx_TP)r	   NNN)FF)FF)FF)FF)FF)__name__
__module____qualname__r+   rR   re   rh   rl   ro   rs   r8   r   r   r   r   r      s   
&
&
8
&
:
r   )__doc__r    platformZnumpyr$   Zscipy.constantsr   r   r   Zscipy.optimizer   Zpyctpr   Zpykingasr   objectr   r   r   r   r   <module>   s   