U
    L_X                     @   s   d dl Z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 G dd dZG dd dZdd	 ZG d
d dZdS )    N)Axes3D)CloughTocher2DInterpolatorc                   @   sf   e Zd ZddgdfddZdddZddd	Zdd
dZdd Zdd Zdd Z	dd Z
dddZdS )Alpha_T0_empirical      ?wheightsc                 C   s  | d\| _| _|| _ttjt	d }| j|j
|d | jk d krNnH| j|j
|d | jk d kr| j| j | _| _d| _ntd| d |j
|d | jk|d | jk@  | _t| jd  | _t| jd	  | _t| jd
  | _t| jt| jt| jt| jf| _t| j| _t| jt| j | _t| jt| j | _| jdkrld| _| jdkr~d| _|d | _|dkr| j| _n0|dkr| j| _n|dkr| j| _ntddS )a  
        Callable object, composition and components are set upon initialization
        __call__(temp): returns alpha_T0 for component 1 at given composition and temperature, approximated from experimental data

        :param comps: 'comp1,comp2'
        :param mole_fracs: mole fractions of components
        :param method: 'wheights' (default), 'ct': CloughTocher, 'plane': linear interpolation
        ,z/alpha_t0.xlsxComp1Comp2TzComponents z not found in database.c_avgT_avg	Alpha_T_0r      r   Zplanectz1method must be either 'wheights', 'plane' or 'ct'N) splitcomp1comp2compspd
read_excelospathdirname__file__locflip
ValueErrordatanparraytolistc_listT_list
alpha_listminmax	cT_rangeslen
num_pointsc_spanT_spancget_alpha_T0_wheightsget_alpha_T0get_alpha_T0_planeget_alpha_T0_CloughTocher)selfr   
mole_fracsmethoddf r3   [/home/ubuntu/Home/Documents/7_semester/irrev_prosjekt_renskriv/models/alpha_t0_empirical.py__init__   s:    
$&






zAlpha_T0_empirical.__init__Fc              
      s  || _ | jd }tt|| jd k || jd k|| jd k || jd kgrtdtt	|d d tt	|d d tt	| jd d d	 tt	| jd d d
 tt	| jd d d	 tt	| jd d d  | j
| | j d | j| | j d  }tdd tt|dd dD }tdd |D }d|d< t| j
|d  g}t| j|d  g}d}	| ||||sx|	| jk rx| j
||	  }
| j||	  }| |
| j || j || j || j || j || j }|	| jd k rB|sB|	d7 }	| j
||	  }
| j||	  }| |
|||||rd}qBq|rPd||	< | j
||  }| j||  }|	d7 }	qt| j||  }|||   t fddtt D  fddtt D }t|| }t|| d || d  || d  t| }|r&| |||||||| |dks<|	| jkrtdtt	|d d tt	|d d | j d tt	|d d  t|| gS )a  
        :param comps (str) : comp1,comp2 following Thermopack convention
        :param c (float) : Volumetric concentration at which to get kinetic_gas
        :param T (float) : Temperature (K) at which to get kinetic_gas
        :param plot (bool or '2d' or '3d') : display fitted data
        :return float : kinetic_gas at the specified conditions, approximated from experimental data
                        using the custom weighting scheme
        d   r   r         
c =  T =  is outside range 
c : (, ), T : )c                 S   s   g | ]\}}|qS r3   r3   .0i_r3   r3   r4   
<listcomp>W   s     z<Alpha_T0_empirical.get_alpha_T0_wheights.<locals>.<listcomp>c                 S   s   | d S Nr   r3   pairr3   r3   r4   <lambda>W       z:Alpha_T0_empirical.get_alpha_T0_wheights.<locals>.<lambda>keyc                 S   s   g | ]}d qS Fr3   r@   rA   r3   r3   r4   rC   X   s     Tc                    s4   g | ],}t  d | t  |d d   qS rD   r   prodrL   rr3   r4   rC   z   s     c                    s8   g | ]0}t  d | t  |d d    qS rD   rM   rL   rP   totr3   r4   rC   {   s     i  
Alpha_T0 at c =  for z# may be a bad approximation (R^2 = )plotr*   anyr   r   r%   warningswarnstrroundr    r(   r!   r)   sorted	enumeratecheck_insider'   check_validr"   sumranger&   plot_fitr   )r/   TrU   r*   distdist_inddist_ind_maskclose_cclose_TrA   new_cnew_Tvalid_pointclose_alphar   	fit_alphaR2r3   rQ   r4   r+   @   s    	
>*($   0&z(Alpha_T0_empirical.get_alpha_T0_wheightsc              	   C   s  || _ | jd }tt|| jd k || jd k|| jd k || jd kgrtdtt	|d d tt	|d d tt	| jd d d	 tt	| jd d d
 tt	| jd d d	 tt	| jd d d  t
t| j| jg | j||g}t|| gS )a  
        :param comps (str) : comp1,comp2 following Thermopack convention
        :param c (float) : Volumetric concentration at which to get kinetic_gas
        :param T (float) : Temperature (K) at which to get kinetic_gas
        :param plot (bool or '2d' or '3d') : display fitted data
        :return float : kinetic_gas at the specified conditions, approximated from experimental data
                        by CloughTocher-interpolation
        r6   r   r   r7   r8   r9   r:   r;   r<   r=   r>   )rU   r*   rV   r   r   r%   rW   rX   rY   rZ   interpolr    r!   	transposer"   )r/   rb   rU   r*   rl   r3   r3   r4   r.      s0    	
>*&z,Alpha_T0_empirical.get_alpha_T0_CloughTocherc              
      s  || _ | jd }tt|| jd k || jd k|| jd k || jd kgrtdtt	|d d tt	|d d tt	| jd d d	 tt	| jd d d
 tt	| jd d d	 tt	| jd d d  | j
| | j d | j| | j d  }tdd tt|dd dD }tdd |D }d}tg }t|dk r>|| jk r>tdd |D }d||< t| j
||  g}t| j||  g}	td|d }
| j
||
  }| j||
  }| || j || j || j |	| j || j || j }|
| jd k rj|sj|
d7 }
| j
||
  }| j||
  }| ||||	||rd}qjq|rxd||
< | j
||  }| j||  }	|
| jd k r|| jkr|
d7 }
| j
||
  }| j||
  }t||gf}t|	|gf}| ||||rd||
< qq| j
||  }| j||  }	|d7 }qJ| j||  }t|dkrt||	|g }t|d |d  |d |d  }t||d |d |  |d |  |d  }nR|||   t fddtt D  fddtt D }t|| }t|| d |	| d  || d  t| }|rd| |||	||||| |dkrtdtt	|d d tt	|d d | j d tt	|d d  t|| gS )a  
        :param comps (str) : comp1,comp2 following Thermopack convention
        :param c (float) : Volumetric concentration at which to get kinetic_gas
        :param T (float) : Temperature (K) at which to get kinetic_gas
        :param plot (bool or '2d' or '3d') : display fitted data
        :return float : kinetic_gas at the specified conditions, approximated from experimental data
                        by fitting a plane to the closest three data points that enclose the desired point
        r6   r   r   r7   r8   r9   r:   r;   r<   r=   r>   c                 S   s   g | ]\}}|qS r3   r3   r?   r3   r3   r4   rC      s     z9Alpha_T0_empirical.get_alpha_T0_plane.<locals>.<listcomp>c                 S   s   | d S rD   r3   rE   r3   r3   r4   rG      rH   z7Alpha_T0_empirical.get_alpha_T0_plane.<locals>.<lambda>rI   c                 S   s   g | ]}d qS rK   r3   rL   r3   r3   r4   rC      s     c                 S   s   g | ]}d qS rK   r3   rL   r3   r3   r4   rC      s     Tc                    s4   g | ],}t  d | t  |d d   qS rD   rM   rL   rO   r3   r4   rC      s     c                    s8   g | ]0}t  d | t  |d d    qS rD   rM   rL   rQ   r3   r4   rC      s     rS   rT   z may be a bad fit (R^2 = )rU   r*   rV   r   r   r%   rW   rX   rY   rZ   r    r(   r!   r)   r[   r\   r&   r'   SkipCounterr^   concatenater]   r"   ro   crossdotr_   r`   ra   r   )r/   rb   rU   r*   rc   rd   re   nrf   rg   rA   rh   ri   rj   Zbox_cZbox_Trk   Zclose_pointsZ
normal_vecrl   r   rm   r3   rQ   r4   r-      s    	
>*($
   $20
&z%Alpha_T0_empirical.get_alpha_T0_planec           	      C   s`  t |dk rdS t|| || g }tj|tdd |D fdd}dd tt |D }|dd D ]}t|d	 || d
 d	kr|d| ||d d  D ]D}t|d	 || d
 d	k rt|| || d
 d	kr  dS qqn|d| ||d d  D ]J}t|d	 || d
 d	krt|| || d
 d	k r  dS qqndS )a  
        :param x (ndarray) : list of x-points
        :param y (ndarray) : list of y-points
        :param c (float) : x-point
        :param T (float) : y-point

        :return bool : Is the point (c,T) inside the polygon spanned by (x1,y1),(x2,y2),...
        r8   Fc                 S   s   g | ]}d qS r   r3   rL   r3   r3   r4   rC     s     z3Alpha_T0_empirical.check_inside.<locals>.<listcomp>r   )axisc                 S   s   g | ]}|qS r3   r3   rL   r3   r3   r4   rC     s     Nr   T)r&   r   r   ro   rq   vstackr`   rr   )	r/   xyr*   rb   vecsindicesrA   jr3   r3   r4   r]     s    	" 8 <zAlpha_T0_empirical.check_insidec                 C   sb   t || || g }t || || g }t||D ]\}	}
t |
|	dk r> dS q>dS )a  
        :param new_x (float) : x-point
        :param new_y (float) : y-point
        :param x_list (ndarray) : list of x-points
        :param y_list (ndarray) : list of y-points
        :param c (float) : x-value at point that is being approximated
        :param T (float) : y-value at point that is being approximated

        :return bool : If the point (new_x,new_y) will improve the approximated value at (c, T)
        r   FT)r   r   ro   ziprs   )r/   Znew_xZnew_yx_listZy_listr*   rb   r{   Znew_point_vecsvecZnew_point_vecr3   r3   r4   r^     s    zAlpha_T0_empirical.check_validc	                    s  t d_tj jdks*jdkrtjjd jd  fddt	t
|D d tj||d	d
dd tj||ddd td td tdtt|d d tt|d  tdt|dd  d t|dd   t  jdksjdkrt }	|	jddd}
|
jjd jd jd dd
d |
j|||dd |
j|||dd |
d |
d |
d td tt|d  t  jd!krtd" dS )#a  
            Plots the fitted data, good for debugging and to view errors.
            Run __call__ with plot=True, plot='2d' or plot='3d' to activate
            NB: Does NOT display the plot. plt.show() must be called after function call to display
        Zplasma2dTr
   r   c                    s    g | ]} j|   qS r3   )cmapr"   rL   Z	max_alphar/   r3   r4   rC   ;  s     z/Alpha_T0_empirical.plot_fit.<locals>.<listcomp>colorZgreenry   zInterpolation points)r   markerlabelblackzPoint to approximate)r   r   zc [vol%]T [K]zplots/selected/r7   rB   r   Nr8   3do   
projectionr   Zblue)r   r   redz$\alpha_T^\circ$ [-]z$R^2 = $)r   r   TzArgument 'plot' can be:
'2d': plot 2d selection of data points
'3d': plot 3d fit and selection of data points
True: plot both the above)cmget_cmapr   r$   r"   rU   pltscatterr   r`   r&   xlabelylabelprintrY   rZ   savefigclosefigureadd_subplot
set_xlabel
set_ylabel
set_zlabeltitleshow)r/   rc   rf   rg   rk   r*   rb   rl   rm   figaxr3   r   r4   ra   0  s0    
4

(.&


zAlpha_T0_empirical.plot_fitc                 C   s,   |j | jd | jd | jd dddd dS )	z~
        scatter experimental data points in 3d
        :param ax: A_matr matplotlib.Axes3D instance on which to plot
        r
   r   r   r   ry   (   )r   r   sN)r   r   )r/   r   r3   r3   r4   plot_pointsZ  s    zAlpha_T0_empirical.plot_pointsc           
         s<  t d j\}}}}t||dd t||dt\ t }|jddd}	| |rt
fddttD }|jd	 |d
d nbt
 fddtt D }	jjkrt|	j |	 d  }	|jd	  |	d
dd tdj  |d |d |d dS )z
        Plot the data-set selected upon initialization and the interpolation with the method selected upon initialization.
        :param dim_1d: Set to True if data is 1d
        ignore   {Gz?r   r   r   c                    s$   g | ]} |  | d  qS ru   r,   rL   )	T_list_1d	c_list_1dr/   r3   r4   rC   q  s     z0Alpha_T0_empirical.plot_mesh.<locals>.<listcomp>r6   r   r   c                    s,   g | ]$  fd dt tD qS )c                    s,   g | ]$} |f  |f d  qS ru   r   rL   )r!   r    r}   r/   r3   r4   rC   u  s     z;Alpha_T0_empirical.plot_mesh.<locals>.<listcomp>.<listcomp>)r`   r&   )r@   )r!   r    r/   )r}   r4   rC   u  s   r   r   )r   alphaz)Approximated $\alpha_T^\circ$ values for zc [%vol]r   u   $\alpha_T^\circ$ [–]N)rW   filterwarningsr%   r   linspacemeshgridr   r   r   r   r   r`   r&   rU   r,   r.   r   shapero   Zplot_wireframer   r   r   r   r   )
r/   Zdim_1dmin_cmax_cmin_Tmax_Tr   r   Zalpha_vals_1dZ
alpha_valsr3   )r!   r   r    r   r/   r4   	plot_mesha  s,    

$



zAlpha_T0_empirical.plot_meshN)F)F)F)F)__name__
__module____qualname__r5   r+   r.   r-   r]   r^   ra   r   r   r3   r3   r3   r4   r   
   s   5
K

`*r   c                   @   s0   e Zd Zdd Zdd ZeZdd Zdd Zd	S )
rp   c                 C   s$   ||kr|d | _ n|| _ || _d S rD   valskip)r/   r   r   r3   r3   r4   r5     s    zSkipCounter.__init__c                 C   s6   | j | | jkr$|  j |d 7  _ n|  j |7  _ | S rD   r   r/   otherr3   r3   r4   __lADD__  s    zSkipCounter.__lADD__c                 C   s
   | j |k S Nr   r   r3   r3   r4   __lt__  s    zSkipCounter.__lt__c                 C   s   | j S r   r   )r/   r3   r3   r4   	__index__  s    zSkipCounter.__index__N)r   r   r   r5   r   __add__r   r   r3   r3   r3   r4   rp     s
   rp   c                     sH  g } g }g }g }g }t dddD ]<}t dddD ]&}t ddddg}t ddddg}|| d || d   t t   }	|	t|	 }
t fddtt D  fddtt D }|d | }	| t t   |t	  |t|	|
 |d |   |t|	| |d |   |t|	t|	 |d |   q6q"t
j| tt |dd	 t
j| tt |d
d	 t
  t
j|tt |dd	 t
j|tt |d
d	 t
  t
 }|jddd}|j| |tt |dd	 |j| |tt |d
d	 |d |d t
  dS )z~
        Testing procedure for different wheighted averages based on distance from point to different experimental points
    r   g?   r7   c                    s4   g | ],}t  d | t  |d d   qS rD   rM   rL   rO   r3   r4   rC     s     z!test_wheights.<locals>.<listcomp>c                    s8   g | ]0}t  d | t  |d d    qS rD   rM   rL   rQ   r3   r4   rC     s     br   rP   r   r   r   rm   Zr_minN)r   r   r   expsqrtr_   r`   r&   appendr#   r   r   absr   r   r   r   r   )ZR2_listmin_rZ
miss1_listZ
miss2_listZ
miss3_listZx_pZy_pry   rz   fw1w2r   r   r3   rQ   r4   test_wheights  sB    &

r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
DB_Builderc                 C   s
   || _ d S r   )filename)r/   r   r3   r3   r4   r5     s    zDB_Builder.__init__c           	      C   sf   t | j}|j|d |k|d |k@  }t|d }t|d }t|d }t|d }||||fS )Nr   r	   r
   r   )r   r   r   r   r#   r$   )	r/   r   r   r2   r   r   r   r   r   r3   r3   r4   get_cT_range  s    zDB_Builder.get_cT_rangec              
   C   sF  t | j}|d }|d }tdd t||D }i }|D ]6\}}|| krd|| d | q<d|gi||< q<| D ]}tt	|| d || d< tt	|| d || d< tt	|| d || d< tt	|| d || d	< t
|| d D ]^\}	}| ||\}
}}}|
|| d |	< ||| d |	< ||| d |	< ||| d	 |	< qq|t|d
}|d | D ]}t
|| d D ]\}	}||d | d tt|| d |	 d d tt|| d |	 d d t|| d |	  d t|| d	 |	  d  q|d qW 5 Q R X d S )Nr   r	   c                 s   s   | ]\}}||fV  qd S r   r3   )r@   c1c2r3   r3   r4   	<genexpr>  s     z&DB_Builder.build_db.<locals>.<genexpr>ZMixr   r   r   r   wz)Comp1, Comp2, min_c, max_c, min_T, max_T
r<   r   
)r   r   r   setr~   keysr   r   zerosr&   r\   r   openwriterY   rZ   )r/   Zoutfiler2   r   r   Zcomp_tuplesZ	comp_dictr   r   rA   r   r   r   r   filer3   r3   r4   build_db  sT    

zDB_Builder.build_dbN)r   r   r   r5   r   r   r3   r3   r3   r4   r     s   r   )pandasr   matplotlib.pyplotpyplotr   matplotlib.cmr   Zmpl_toolkits.mplot3dr   numpyr   rW   r   scipy.interpolater   rn   r   rp   r   r   r3   r3   r3   r4   <module>   s     |1