3
__i                 @   sZ   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dZdd Z	dd Z
dS )a  
***************************************************        
***   Program for the computation of the shock  ***
***     in adiabatic colliding wind binaries    ***    
***************************************************
This code is part of the LIFELINE program.

Copyright (C) 2020-2021 University of Liege (Belgium)
Enmanuelle Mossoux (STAR Institute)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.

Run: from ashock import ashock
     rien=ashock(Mdot1,Mdot2,vinf1,vinf2, mass_ratio,ex,omega, a, Per, R1, R2, Teff1, Teff2, nbr_points_shock_2D, nbr_points_shock_3D, nbr_points_width, nbr_bin_profile, direct, cut_lim, wind_prim, wind_sec, phase, incl,ipar, mu)

Use of the Canto et al. (1996) formalism

# Parameters 
# ==========
Mdot - Mass loss rate [Msun/yr]
vinf - terminal velocity of the winds [cm/s]
ex - eccentricity
omega - argument of the periapsis [radian]
a - semi-major axis [Rsun]
R - stellar radius [Rsun]
Teff - Effective temperature of the star [K]
nbr_points_shock_2D - number of points to create the 2D shape of the shock
nbr_points_shock_3D - number of points to create the 3D shape of the shock
nstep_width - number of points to discretize the inside of the shock
nbr_bin_profile - number of points in the line profile
direct - directory where saving files
cut_lim - Limit of the tangential velocity to see the emission
wind_prim - h5 file containing the wind distribution of the primary 
wind_sec - h5 file containing the wind distribution of the secondary 
phase - phase of the system (not corrected from omega)
incl - inclination of the orbit
ipar - index of the system parameter
mu - Mean molecular weight

# Versions
# ========
v1 - 16/03/18
c             C   sP  dd l }d}d}| d |d  |d k rd}|jd||  || gg\}}|d }|j|
j| d d |
j| d d  }|j|dkrd}||kr|}||  d |d  |d k r$d}|j|| || gg\}}|d }|j|	j| d d |	j| d d  }|	j| d dkr|	j| d dkr|d	 }|d	 }d
}|	j| d }|	j| d }|d	 }x~|dkr|dkr|d
kr|	j| d }|	j| d }|d	 }|}d}n,d
}|	j| d }|	j| d }|d	 }|}qnW |j|	j| d d |	j| d d  }||kr$|}| d |d  |d kr@||  d |d  |d kr@|j| | || gg\}}|d }|j|	j| d d |	j| d d  }|	j| d dkr|	j| d dkr|d	 }|d	 }d
}|	j| d }|	j| d }|d	 }x~|dkr|dkr|d
krX|	j| d }|	j| d }|d	 }|}d}n,d
}|	j| d }|	j| d }|d	 }|}qW |j|	j| d d |	j| d d  }|jd| |  || gg\}}|d }|j|
j| d d |
j| d d  }|
j| d dkr|
j| d dkr|d	 }|d	 }d
}|
j| d }|
j| d }|d	 }x~|dkr|dkr|d
kr|
j| d }|
j| d }|d	 }|}d}n,d
}|
j| d }|
j| d }|d	 }|}qhW |j|
j| d d |
j| d d  }|j|dkr$d}||kr2|}||kr@|}|| ||  S )N    g    cA   g      ?uxuyFg           updown)numpyquerysqrtilocisfinite)xyR1R2Mdot1Mdot2dtree_prim_windtree_sec_wind
dwind_prim	dwind_secvinf1vinf2npvv1vv2rien	ind_closeZ
ind_closepZ
ind_closemZstadeZdw1Zdw2 r   //local/network/htdocs/Lifeline/codes3/ashock.pycomp_eta5   s     **(

*
8*(

* *(

*

r!   c             C   sN   dd l }t| d|||||||||	|
|}|dkr4dS | |d|j|   S d S )Nr   g        g      ?)mathr!   r
   )r   r   r   r   r   r   r   r   r   r   r   r   r"   etar   r   r    find_x0   s
     r$   c             C   s&   dd l }|j|d ||  d  | S )Nr   r   )r   r
   )ZcenterxZcenteryradiusr   r   r   r   r    	plot_star   s    r&   c             C   s:   dd l }|}| }|j|d |d  }|j|| || S )Nr   r   )r   r
   arctan2)r   zr   ZoppZadjZhypor   r   r    angleyz   s
    r)   c             C   s   dd l }| |j| S )Nr   )r"   cos)vecangler"   r   r   r    projx   s    r-   c             C   s    dd l }| |j| |j| S )Nr   )r"   sinr*   )r+   angle1angle2r"   r   r   r    projy   s    r1   c             C   s    dd l }| |j| |j| S )Nr   )r"   r.   )r+   r/   r0   r"   r   r   r    projz   s    r2   皙?c       	      C   sx   dd l }| j}|| |j|| ddd }|| |j|| ddd }|j|j|d |d  dd}| j||k d S )Nr   r   )axisr   )r   	trianglesrollmaxr
   set_mask)	triangr   r   alphar   r5   ZxtriZytriZmaxir   r   r    
apply_mask   s     r;   c             C   s   dd l }| \}}}|\}}}	g }
g }xTtt|D ]D}|j|j|| ||f||f |
j|j|| ||f||	f q2W ||
fS )Nr   )r   rangelenappendinterp)p1p2r   r   x1y1z1x2y2z2ZreszZresyir   r   r    find_xy   s    

 $rI   c         +   C   st"  ddl m} ddlm} ddlm} dd l}dd l}ddlm	} dd l
j}dd l}dd l}	dd l}
ddlm} ddlm} ddlm} dd lj}| \}}}}}}}}}}}}}}}}}} }!}"}#}$}%}&}'}(|j})|
j|"jd	d
}*|
j|#jd	d
}+||j|j|*jd d  d |*jd d  d fdlt|*jd d  d fjdd},||j|j|+jd d  d |+jd d  d fdmt|+jd d  d fjdd}-|| ||  }.|d}/|d}0|d}1|dd }2||0 dn }||0 do }||/ }||/ }||/ }t||g}3|j ||j!dd }4|4|j"t!|d d  |3 |j"t!|d  }5|)d }6|.dkr
|)d|.  }7|)d }6d|)d |7 |j#|)d   }8|8|7|6  }9|6|9 }6xLt$|9dkrt$|8dkrd|6|7 |j#|6  }8|8|7|6  }9|6|9 }6qW |6|)d k r
|)d |6 }6|$}:|:};|:|; ||j%|;  d||j&|;   }<xDt$|<dkr~|;|< };|:|; ||j%|;  d||j&|;   }<q<W |j&|;| d||j&|;   }=|j'd||  |j%|; d||j&|;   }>|j(|>|=}?|?d|) krd}?|d|d   d||j&|?   }@|j)||@ }A|t*||@ | d |@|||||,|-|*|+||fd}B|Bd }Bt+| d t,|& d}C|Cj-t,|Bd   |Cj-t,|B| d!  |Cj-t,|B|@ d"  |Cj.  t|B|g}B|$|( }$|$dk rd|) |$ }$d#d$ t/d%D \}D}E}F}G}H}I}J}K}L}M}N}O}P}Q}Rd&d$ t/d'D \}S}T}U}V}W}X}Y}Z}[}\}]}^}_d(d$ t/d)D \}`}a}b}c}d}e|j |d* |) d+|  }fd*|) d+|  }g|j0| |@ ||@ d,}ht1dd||@ |h}i|j0| |@ ||@ d,}jt1dd||@ |j}k|jd }j|j ||6 | d-|) d  }l|j |d. d |)|6  |d. d  }m|m|j#|m }ndd|. |l|j#|l d   }og }pxH|oD ]@}q|j2t$|n|q |j3t$|n|q kd }r|pj4|m|rd   qTW |j5|p}p|j%|p|j%|p|l  }s|s|j%|l |@ }t|j2|+d d/kd }u|+j|u }v|B}wd}xg }yg }zx|tD ]}{|j'|wd |xd  }||j'|@|w d |xd  }}t6|w|x|||||@|,|-|*|+||}~|~dkr|{|w|@||d  |j'|~ ||d |j'|~ |}d     }d| }||{|x  |w }w|{}x|yj4|w |zj4|~ qW |j5|z}z|j5|y|@ }y|t|@ }tdddg}d}xXt/|D ]J}t!|d d0 dkrLd1t,|d  d2 t,| d3 }t7| |y|@ }|t|@ |j&|f|  }|t|@ |j%|f|  }xt/t|yD ]}|| dk 	rl|j)|j'|| d || d  |@||   |Ak 	rl|j#|A|@||   }|j8|| |j'|| d || d   }t$||j&| t!|j9||  ||< t$||j%| t!|j9||  ||< |j:|j;|| || || g||j<j=|| || || gdd4|j<j=|dd4  }t>|| || }|)|j:|j;|| |@ || || g||j<j=|| |@ || || gdd4|j<j=|dd4   }d}d}|)d }|dkr|j:|j;||d  ||d  ||d  g||j<j=||d  ||d  ||d  gdd4|j<j=|dd4  }|)|j:|j;||d  |@ ||d  ||d  g||j<j=||d  |@ ||d  ||d  gdd4|j<j=|dd4   }|j:|j;|| ||d   || ||d   || ||d   g||j<j=|| ||d   || ||d   || ||d   gdd4|j<j=|dd4  }t$|| }t$|| }|j'|| d || d  || d  }|j'|@||  d || d  || d  }|,j?|| |@ |j'|| d || d  |@ gg\}}|d }|j'|*j| d5 d |*j| d6 d  }t@||g}td7|g}|-j?d|| |@  |j'|| d || d  |@ gg\}}|d }|j'|+j| d5 d |+j| d6 d  }t@||g}td7|g}|d|) |z|  |@ |  }|| }|z| ||j%||j&|   }|||j%||j&|   }|| }|z| |j%| |j%| }||j%||j%|  }|| }|z| d|j&|  |d|j&|   }|j%|| |j%| }||j%| | | }|| |j'||  }d8| d8|) || d || d  || d   |  }d8| d8|) |@||  d || d  || d   |  }|| d }|| }|| }|}|}|dkr(|| |k r|}|| |k r(|}t|||}|j |d| d | d|   |d  |d|   }|j |d| d | d|   |d  |d|   }|j |}|j|V|||| |  d  |j%|)d |  | |g t$|j%|| |  f}V|j|V|||| |  d  |j%|)d |  | |g t$|j%|| |  f}VtA|| |}tB|| ||}tC|| ||}tA|| |)| |@ }tB|| |)| |}tC|| |)| |}tA|| |}tB|| ||}tC|| ||}tA|| |)| |@ }tB|| |)| |}tC|| |)| |}||j&|)d |  }||j&|)d |  }|dkr0|j:|j;|| || || g||j<j=|| || || gdd4|j<j=|dd4  }||j&||  }|j:|j;|| || || g||j<j=|| || || gdd4|j<j=|dd4  }||j&|)| |  }|}|}|}|}|}|}|| }|| }|}|}|j'|d |d  }|j'|d |d  }|j |d dp|2 |d  d;|1   }|j |d dq|2 |d  d;|1   }|j|X|f}X|j|Y|f}Y|j|Z|f}Z|j|J|gf}J|j|K|gf}K|j|L|gf}L|j|M|gf}M|j|N|gf}N|j|O|gf}O|j|[|f}[|j|\|f}\|j|]|f}]|j|[|f}[|j|\|f}\|j|]|f}]|)d }|dkrN|j:|j;|Jdr |Jds  |Kdt |Kdu  |Ldv |Ldw  g||j<j=|Jdx |Jdy  |Kdz |Kd{  |Ld| |Ld}  gdd4|j<j=|dd4  }t>|Kd~ |Ld }tA|j |d | |}tB|j |d | ||}tC|j |d | ||}|j|W||j%|% |j&|$ ||j&|%  ||j%|% |j%|$  f}WtA|j |d | |}tB|j |d | ||}tC|j |d | ||}|j|W||j%|% |j&|$ ||j&|%  ||j%|% |j%|$  f}W|j|P|f}P|j|^dd|j |  f}^|j|Q|j |d | f}Q|j|R|j |d | f}R|j|P|f}P|j|^dd|j |  f}^|j|Q|j |d | f}Q|j|R|j |d | f}RqW qW |j|X|Sf}y|j|Y|Tf}t|j|Z|Uf}|j|J|Df}D|j|K|Ef}E|j|L|Ff}F|j|M|Gf}G|j|N|Hf}H|j|O|If}I|j5|[d d  }|j5|\d d  }|j5|Pd d  }|j5|Qd d  }tDt|Ƀ| }|j|d|ͅ |tDtE|| d/ tDtE|| d/ |  f}|j|d|ͅ |tDtE|| d/ tDtE|| d/ |  f}|j|d|ͅ |tDtE|| d/ tDtE|| d/ |  f}|j|d|ͅ |tDtE|| d/ tDtE|| d/ |  f}|jFd||@ d<d=}|jFd||@ d<d=}|j d>|jGt|Ѓ d? }|jH||@ ||@ }tI|||@ ||@ d@dA |jJd |jK }|jL }|jM|Ӄ |jN|||dB |jM|҃ |jO|h|idCdD |jO|j|kdCdD |jO|h|i dCdD |jO|j|k dCdD |jPdEdF}|jQdG |jRdt|P |jSdH |jTdI |jUdd |jVdd |jW| dJ t,|& dK  |j.  d}|jFd||@ d<d=}|jFd||@ d<d=}dL}x t|у| dk r|d> }ِqW |jX|j dM|jGt|у| |j"t@|у|   dN |j"t@|у|  }|jH||@ ||@ }tI|||@ ||@ d@dA |jJd |jK }|jL }|jM|Ӄ |jN||| |dB |jM|҃ |jO|h|idCdD |jO|j|kdCdD |jO|h|i dCdD |jO|j|k dCdD |jPdEdF}|jQdOt,|ك dP  |jRt@|Q| t|Q|  |jSdH |jTdI |jUdd |jVdd |jW| dQ t,|& dK  |j.  |[|\|]|P|QdR}|j-|| dS t,|& dT  t|[}dU| }t@dV|jG|j't|yt@|y d t|tt@|t d  t|ȃt@|ȃ d  | g}t@|D}dWt|G }t@|H}dWt|H }||j&|$ |j%|% }||j&|% }||j%|$ |j%|% }|j5|||g|j<j=|j5|||g }|j|j|[|@ |\|@ |]|@ fdt|[fj}~[~\~]||dd}||j|j|D|@ |E|@ |F|@ fdt|Dfjdd}||j|j|G|@ |H|@ |I|@ fdt|Gfjdd}||j|j|y|@ |t|@ ||@ fdt|yfjdd}~D~E~F~G~H~Ig }g }x^t/|D ]R}|j4g  |j4g  t+| dX t,|& dY t,| dZ d}|j-d[ |j.  qW t7d\ t7d] xft/|ۃD ]X}t!|d d0 dkrJd1t,|d  d2 t,|ۃ d3 }t7| ||df |@ }||df |@ }||df |@ }|P| }|Q| }t$|V| }|W| }|j%|$| |j&|$|  }|j&|$|j%|% | |j&|%|  |j%|$|j%|% |  }|j&|% |j&|$ | |j%|%|  |j&|%|j%|$ |  }|j%|$|d  |j&|$|  }|j&|$|j%|% |d  |j&|%|  |j%|$|j%|% |  }|j&|% |j&|$ |d  |j%|%|  |j&|%|j%|$ |  }d}|dk  r|j'|d |d  |k  rd}d}|dk  r:|j'|d |d  |k  r:d}|| dkr|j2|5|kd }t|dkr|dkr||d  j4| ||d  j4d^d_|d  d`|2d  d   | da  ||||||||||y|t||@|^|P|R|||||||,|-|*|+||2||||||||||||||)}t+| dX t,|& dY t,|d  dZ db}|j-dcjYtZt,|dd  |j.  qW t+| de t,|& dZ d}|j-df t+| dg t,|& dZ d} | j-dh t+| di t,|& dZ d}|j-dj |j-dk xnt/|D ]b}|j-dcjYtZt,|| dd  | j-dcjYtZt,|| dd  |j-t,|5|  dV dd  !qW |j.  | j.  |j.  d S )Nr   )KDTree)fsolve)spatial)	constante)Axes3D)RT)asciiT)Zdropr   r   r   i  )leafsizeRsunMsunkbmpg     @@gp=
v@g     @)dtypeg      ?g       @g:0yE>g        r   )argsz/histograms/xstag_parwz cm
z R1
z d
c             s   s   | ]
}g V  qd S )Nr   ).0_r   r   r    	<genexpr>*  s    zashock.<locals>.<genexpr>   c             s   s   | ]
}g V  qd S )Nr   )rY   rZ   r   r   r    r[   +  s       c             s   s   | ]
}g V  qd S )Nr   )rY   rZ   r   r   r    r[   ,  s       g     v@g     f@(   g-C6?g     @@g      ?g      Y@z* /z *)r4   r   r   g    cAg      @ghh!w>g      @g      0@white)colorg      $@g      "@g333333?)r:   )levelsblack)cz%.2f)formatzTemperature [keV]zy/Dzx/Dz/plots/temperature_xy_parz.pdfgvIh%<=g      4@g      3@z	Density [z g/cm^3]z/plots/density_par)r   r   r(   kTrhoz/plots/char_shock_parz.datg?g     j@g?z/histograms/ray_tracing_parZ_binz.dataz# Temperature / Factor
 z*** Ray tracing ***gQ@-(;gQ?gq=
ףp?gqىEa 
z/histograms/bin_parz# Tangential velocity (km/s)
z /histograms/emiss_part_shock_parz:# Part of the emissivity which depends only on the shock.
z/histograms/temp_emiss_parz9# Temperature of the cell where the emission is created.
z*# Each line of a bin of the line profile.
rm   g    H~Ag    H~Ag9UY>g9UY>rm   rm   rn   rm   rn   rm   rn   rm   rn   rm   rn   rm   rm   )r   r   )r   r   rm   rn   )r   r   )r   r   rm   rn   rm   rm   rm   rm   rm   rm   rm   )[Zscipy.spatialrJ   Zscipy.optimizerK   scipyrL   r   r"   
constantesrM   matplotlib.pyplotpyplot
matplotlibosZpandasZmpl_toolkits.mplot3drN   Z
raytracingrO   Z
astropy.iorP   matplotlib.tritripiZread_hdfZreset_indexreshapeconcatenater   r=   Tr7   arangefloatfloortanabsr.   r*   r
   atan2atanr$   openstrwritecloser<   linspacer&   wherenanminr>   arrayr!   printacossignarccosdotlinalgnormr)   r	   minr-   r1   r2   introundCircleceilTriangulationr;   figuregcfgca
add_artisttricontourfplotcolorbar	set_labelset_climylabelxlabelxlimylimsavefigsortjoinmap(  liste_paramrJ   rK   rL   r   r"   rM   pltrs   rt   pdrN   rO   rP   rv   r   r   r   r   
mass_ratioexomegarj   perr   r   ZTeff_1ZTeff_2nbr_points_shock_2Dnbr_points_shock_3DZnstep_widthnbr_bin_profiledirectcut_lim	wind_primwind_secphaseincliparmuM_conjrw   r   r   r   r   betarR   rS   rT   rU   vmaxr+   ZvvvZthetainfasympZdeltafZ
deltathetaMEdEcoisii
phase_truer   Zouverture_minxstagZfxstagZx_cdw1Zy_cdw1Zz_cdw1Zx_cdw2Zy_cdw2Zz_cdw2Zx_cdw1gZy_cdw1gZz_cdw1gZx_cdw2gZy_cdw2gZz_cdw2gZkT_allZrho_allZ	sigma_allZx_cdZy_cdZz_cdZvol_cdZvrad_cdZx_skewZy_skewZz_skewZxx_allZyy_allZzz_allZcote_cdZvt_vecZvt1_vecZvp1_vecZvt2_vecZvp2_vecZ
slope_vec1Z
slope_vec2Zphi_vecdphiZxstar1Zystar1Zxstar2Zystar2Z	theta_vecZ
theta1_vecZThetaCotThetapointZthe1_vecZ	pointhereindZr_testZy_vecZind_halfZdwind_sec_halfZx_oldZy_oldZx_vecZeta_vecZ	y_vecherer1r2r#   ZpentexaxislmaxZicutsentenceZxcutZycutZzcutZcutZRprimer,   ZthetahlatZ	the1_vechZthetahm1Zthe1_vechm1Z
slope_vechdthetaZdthe1Zr1_vechZr2_vechr   r   r   r   Zsig0r:   Zterm1Zterm2Zterm3Zterm4Z	sigma_vecZrho1Zrho2rh   Z
thickness1Z
thickness2Zl0_1Zl0_2Zrl1Zl011Zrl2Zl022Zl1_widthZl2_widthZincreasevolZx_cdw1g1Zy_cdw1g1Zz_cdw1g1Zx_cdw2g1Zy_cdw2g1Zz_cdw2g1Z	x_cdw1g1aZ	y_cdw1g1aZ	z_cdw1g1aZ	x_cdw2g1aZ	y_cdw2g1aZ	z_cdw2g1aZvt1Zvt2Z	x_cdw1g11Z	y_cdw1g11Z	z_cdw1g11Z	x_cdw2g11Z	y_cdw2g11Z	z_cdw2g11Zvp1Zvp2ZkT1ZkT2ZvxZvyZvzZz_vecZx_capZy_capZkT_capZrho_capZ	nbr_layerZx_layerZy_layerZkT_layerZ	rho_layerZcircle1Zcircle2rc   Ztriang2figaxcbarfactortableZ
nbr_pt_totZdeltaZZZndeltaZZxmin1Zxmax2ymin1Zymax2ZdeltaXZdeltaYZdeltaZZdir_obsZensemble_pointstreeZtreecdw1Ztreecdw2ZtreevecZ
temp_emissZemiss_part1rH   ZfRTxxyyzzrg   ZvolZvradyt1Zzt1Zpt1yt2Zzt2Zpt2Zvis1Zvis2Zind_vradZray_tracingZfbinZfhistoZftempr   r   r    ashock   s$   

8RR0
(. ."0
0,&
(


 
8


 D,((Vb
nz*.:*>* (8<
66
RR
VV&&
HH(BBBB





J




T(.666

&
 >@ BD&&6X*
  "

r   N)r3   )__doc__r!   r$   r&   r)   r-   r1   r2   r;   rI   r   r   r   r   r    <module>1   s   g

