B
    d                 @   s  d Z ddlZddlmZ yddlZW n ek
r@   ed Y nX dd Z	dd Z
dd	 Zd
d Zdd ZG dd dZG dd dZdVddZdd Zdd Zdd Zdd Zdd Zdd  ZdWd"d#ZdXd$d%Zd&d' ZdYd(d)Zd*d+ ZdZd,d-Zejfd.d/Zejfd0d1Zd2d3 Z d4d5 Z!d6d7 Z"d[d9d:Z#d\d;d<Z$d=d> Z%d?d@ Z&d]dBdCZ'd^dDdEZ(G dFdG dGZ)dHdI Z*dJdK Z+dLdM Z,dNdO Z-dPdQ Z.dRdS Z/dTdU Z0dS )_z~
Various fitting formulas provided by numerical relativity

Archisman Ghosh, Nathan K. Johnson-McDaniel, P. Ajith, 2015-04-09
    NzCannot import lal SWIG bindingsc             C   s0   t | dkrtdt |dkr,tdd S )Nr   zm1 has to be positivezm2 has to be positive)npany
ValueError)m1m2 r   h/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/lalinference/imrtgr/nrutils.py_check_m   s    r	   c             C   s\   t | dkrtdt |dkr,tdt | dk rBtdt |dk rXtdd S )N   zchi1 has to be <= 1zchi2 has to be <= 1r   zchi1 has to be nonnegativezchi2 has to be nonnegative)r   r   r   )chi1chi2r   r   r   
_check_chi   s    r   c             C   s8   t t| dkrtdt t|dkr4tdd S )Nr
   zchi1 has to be <= 1zchi2 has to be <= 1)r   r   absr   )r   r   r   r   r   _check_chi_aligned    s    r   c             C   s   t | | t|| d S )N)r	   r   )r   r   r   r   r   r   r   _check_mchi&   s    
r   c             C   s   t | | t|| d S )N)r	   r   )r   r   r   r   r   r   r   _check_mchi_aligned*   s    
r   c               @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	bbh_final_state_fitsZPan2011HLZ2014ZPhenomDZ	UIB2016v1UIB2016ZHBR2016HL2016N)
__name__
__module____qualname__pan2011hlz2014phenD	uib2016v1uib2016hbr2016hl2016r   r   r   r   r   3   s   r   c               @   s    e Zd ZdZdZdZdZdZdS )bbh_Kerr_trunc_optsZTRUNCATEZTRUNCATESILENTZKEEPZIGNOREERRORN)r   r   r   trunctrunc_silentkeepignerrr   r   r   r   r    =   s
   r    thisc             C   s|  |t tj krtd| |tjkrxtt| dkrxt	| t tj
frtt| dk}|tjkst|tjkrt| | d }|tjkrtdt||f  || |< nB|tjkrtd|t|f  n |tjkrtd|t|f n|tjks|tjkr>t| d }|tjkr8td| ||f  |} n:|tjkr\td|| f  n|tjkrxtd|| f | S )	Nz6Unknown user option '%s' for Kerr truncation behavior.g      ?zFTruncating %d excessive chif values from %s fit to Kerr limit of +-1.0zANote: %s fit predicts %d chif values in excess of the Kerr limit.z;%s fit predicts %d chif values in excess of the Kerr limit.z?Truncating excessive chif of %f from %s fit to Kerr limit of %fz=Note: %s fit predicts chif of %f in excess of the Kerr limit.z7%s fit predicts chif of %f in excess of the Kerr limit.)listr    __dict__valuesr   r%   r   r   r   
isinstanceZndarraywherer"   r#   signprintsizer$   r&   )chifZbehaviorfitnameZidx_overZ
chif_truncr   r   r   _truncate_at_Kerr_limitF   s0     



r2   c             C   sv   t tt | } t tt |}| | }| | | | d  }|dt dd |  d|d   d|d    S )a  
    Calculate the mass of the final BH resulting from the merger of two non-spinning black holes using eqn. 29a of from Pan et al, Phys Rev D 84, 124052 (2011).

    Parameters
    ----------
    m1, m2 : component masses

    Returns
    ------
    final mass, mf
    g       @g      ?gqq?gV/?gQ?   )r   	vectorizefloatarraysqrt)r   r   metar   r   r   #bbh_final_mass_non_spinning_Panetalc   s
    r:   c             C   sb   t tt | } t tt |}| | | | d  }t d| d|d   d|d   S )a  
    Calculate the spin of the final BH resulting from the merger of two non-spinning black holes using eqn. 29b of Pan et al, Phys Rev D 84, 124052 (2011).

    Parameters
    ----------
    m1, m2 : component masses

    Returns
    ------
    final dimensionless spin, chif
    g       @g      (@g+@g&1@r3   )r   r4   r5   r6   r7   )r   r   r9   r   r   r   #bbh_final_spin_non_spinning_Panetalv   s    r;   c             C   s   t t | d} dd| d  d d|  d d|  d    }t d| d  |d  }t | }d| t d| d| d|   |  S )z
    Calculate the ISCO radius of a Kerr BH as a function of the Kerr parameter using eqns. 2.5 and 2.8 from Ori and Thorne, Phys Rev D 62, 24022 (2000)

    Parameters
    ----------
    a : Kerr parameter

    Returns
    -------
    ISCO radius
    g      ?g       @gUUUUUU?g      @   r3   )r   minimumr6   r7   r-   )aZz1Zz2Za_signr   r   r   calc_isco_radius   s
    ,
r?   c       	      C   sr   t | ||| | | }| | | | }| | | }| |  | || |  |d  }|| | |  | }||||fS )za Internal function to compute the combinations of masses and spins that are used in the RIT fits r<   )r   )	r   r   r   r   r8   r9   delta_mSDeltar   r   r   
_RIT_setup   s     rC   c       	      C   s@  || }|| }|| }|| }d|  |  |d |d |  |d |  |d |  |d |  |d |  |d | |  |d	 | |  |d
 | |  |d | |  |d | |  |d | |  |d | |  |d | |  |d | |  |d | |  |d | |  |d | |  |d | |   S )a8  
    Internal function to return the basic even-in-interchange-of-particles expression used in the RIT fits for the final mass, final spin, and peak luminosity

    The coefficients in the expression are passed as a vector as [0 1 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 4e 4f 4g 4h 4i] (giving the subscripts).
    g      0@r   r
   r<   r3                  	   
                           r   )	r9   r@   rA   rB   coeffsS2dm2ZDeltadmZDelta2r   r   r   _RIT_symm_express   s    
rV   c             C   s^  t | }dt| d|   d td|  }|dkrd}d}	d}
d}d	}d
}d}d}d}d}d}d}d}d}d}d}d}d}d}n^|dkrd}d}	d}
d}d}d}d}d }d!}d"}d#}d$}d%}d&}d'}d(}d)}d*}d+}ntd,|| }|| }|| }t||||||	|
||||||||||||||||g|d-d.|   |  || |  }t| | S )/zh Internal function: the final spin with the Healy et al. fits is determined by minimizing this function r3   r<   g       @2014g??g+,?gIJzZ¿gǁW˝g\7ugV*?g!Z+粿g1`U,~s?g3ۃgK=By_?gHPhgDXPhgyTq?g{h+g?/gZ?g01"Qhg'jinZ?gC2016g?gga?gxLÿg;#g<jg+O?g2[FgV+&U?gxoRgpIX?g2zcwg<[?x?gu[g%g<E܄gAw+F?gvCbgDra&{g)1z洿z3Unknown version--should be either "2014" or "2016".g      ?g       @)r?   r   r7   r   rV   r   )Za_fr9   r@   rA   rB   versionr_iscoZJ_iscoL0ZL1ZL2aZL2bZL2cZL2dZL3aZL3bZL3cZL3dZL4aZL4bZL4cZL4dZL4eZL4fZL4gZL4hZL4irU   Zdm4dm6Za_f_newr   r   r   _final_spin_diff_Healyetal   s`    (Vr]   rW   c             C   s   t tt | } t tt |}t tt |}t tt |}t | t | t | t | dkrt t| ||||S t| |||\}}}}tjt	d|||||fd\}	}
t
|	dr|	d }n|	}|S )a  
    Calculate the spin of the final BH resulting from the merger of two black holes with non-precessing spins using fit from Healy et al Phys Rev D 90, 104004 (2014) (version == "2014") or the small update from Healy and Lousto arXiv:1610.09713 (version == "2016")

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs

    Returns
    -------
    dimensionless final spin, chif
    r
   g        )args__len__r   )r   r4   r5   r6   r/   'bbh_final_spin_non_precessing_HealyetalrC   soZleastsqr]   hasattr)r   r   r   r   rY   r9   r@   rA   rB   xZcov_xr0   r   r   r   r`      s    ,

r`   c       #      C   s  t tt | } t tt |}t tt |}t tt |}t| |||\}}}}	|dkrt| ||||}n
t |}t|}
|dkrd}d}d}d}d}d}d	}d
}d}d}d}d}d}d}d}d}d}d}d}n`|dkr@d}d}d}d}d}d}d}d}d}d }d!}d"}d#}d$}d%}d&}d'}d(}d)}ntd*d+d,|
  ||
d-   t d+d.|
  d,| |
d-    }|| }|| | } | | }!t	||||	|||||||||||||||||||gd/||d0   |   }"|"|! S )1a  
    Calculate the mass of the final BH resulting from the merger of two black holes with non-precessing spins using fit from Healy et al Phys Rev D 90, 104004 (2014) (version == "2014") or the small update from Healy and Lousto arXiv:1610.09713 (version == "2016")

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    chif: final spin (optional), if already calculated

    Returns
    -------
    final mass, mf
    NrW   gEHξr?gg)YNg_=[sg4ӽN꫿g&|g٘?g/$|?gWxgn4@g?gÝ#g.eOgm)bg)H4ſgoIaޓ?gӟH@g#~j?gSspg`?rX   g%fs?gU>-go]w2Wwgg^gBhg?gzILqt?gL|?낿gLֿ#޾ggHaϐ?g"1gsuv]gsgqE?gpOĿg?g|o@gx<F%0?gA$<D?gԅT?z3Unknown version--should be either "2014" or "2016".g      ?g       @g      ?g      @r
   g      &@)
r   r4   r5   r6   rC   r`   r?   r   r7   rV   )#r   r   r   r   rY   r0   r9   r@   rA   rB   rZ   ZM0ZK1ZK2aZK2bZK2cZK2dZK3aZK3bZK3cZK3dZK4aZK4bZK4cZK4dZK4eZK4fZK4gZK4hZK4iZE_iscorU   r\   r8   mfr   r   r   'bbh_final_mass_non_precessing_Healyetal%  sr    

:4re   c             C   s   t | |||||dS )zQ
    wrapper to bbh_final_spin_projected_spins() for backwards compatibility
    r   )bbh_final_spin_projected_spins)r   r   r   r   tilt1tilt2r   r   r   'bbh_final_spin_projected_spin_Healyetal~  s    ri   c          	   C   s   t | |||||d|S )zQ
    wrapper to bbh_final_mass_projected_spins() for backwards compatibility
    r   )bbh_final_mass_projected_spins)r   r   r   r   rg   rh   r0   r   r   r   'bbh_final_mass_projected_spin_Healyetal  s    rk   c          	   C   s   t | ||||||dS )zJ
   wrapper to bbh_final_spin_precessing() for backwards compatibility
   r   )bbh_final_spin_precessing)r   r   r   r   rg   rh   phi12r   r   r   3bbh_final_spin_precessing_Healyetal_extension_Minit  s    rn   c             C   s  t tt | } t tt |}t tt |}t tt |}t tt |}t tt |}|dk	rt tt |}t| ||| |t | }|t | }	|tjkr0t |dks
t |dks
t |dks
t |dkrt	d |dk	r$t	d t
| |}
n|tjkrRt| |||	d|d}
n|tjkrtt| |||	d|d}
n|tjkr|dk	rt	d t| |||	}
nl|tjkr|dk	rt	d t| |||	dd	}
n:|tjkr|dk	rt	d t| |||	d
d	}
ntd|
S )ak  
    Calculate the mass of the final BH resulting from the merger of two black holes,
    only using the projected spins along the total angular momentum
    and some aligned-spin fit from the literature

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    tilt1, tilt2 : tilts (in radians) in the new spin convention
    fitname: fit selection currently supports Pan2011 (non-spinning), HLZ2014, PhenomD, UIB2016, HL2016
    chif: final spin (optional, only used for HLZ2014 and HL2016), if already calculated

    Returns
    -------
    final mass, mf
    Nr   z%Note: Pan2011 fit does not use spins.z,Note: Precomputed chif not used by this fit.rW   )rY   r0   rX   v2)rY   v1zUnrecognized fit name.)r   r4   r5   r6   r   cosr   r   r   r.   r:   r   re   r   r   &bbh_final_mass_non_precessing_Husaetalr   %bbh_final_mass_non_precessing_UIB2016r   r   )r   r   r   r   rg   rh   r1   r0   chi1projchi2projrd   r   r   r   rj     sF    @



rj   c             C   s  t tt | } t tt |}t tt |}t tt |}t tt |}t tt |}t| ||| |t | }|t | }	|tjkrt |dkst |dkst |dkst |dkrt	d t
| |}
n|tjkrt| |||	dd}
n|tjkr8t| |||	dd}
n|tjkrTt| |||	}
nd|tjkrtt| |||	dd}
nD|tjkrt| |||	dd}
n$|tjkrt| |||	}
ntdt|
||}
|
S )	a_  
    Calculate the (signed) dimensionless spin parameter of the final BH resulting from the merger of two black holes,
    only using the projected spins along the total angular momentum
    and some aligned-spin fit from the literature

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    tilt1, tilt2 : tilts (in radians) in the new spin convention
    fitname: fit selection currently supports Pan2011 (non-spinning), HLZ2014, PhenomD, UIB2016, HBR2016, HL2016

    Returns
    -------
    (signed) dimensionless final spin parameter, chif
    r   z%Note: Pan2011 fit does not use spins.rW   )rY   rX   ro   rp   zUnrecognized fit name.)r   r4   r5   r6   r   rq   r   r   r   r.   r;   r   r`   r   r   &bbh_final_spin_non_precessing_Husaetalr   %bbh_final_spin_non_precessing_UIB2016r   r   %bbh_final_spin_non_precessing_HBR2016r   r2   )r   r   r   r   rg   rh   r1   truncatert   ru   r0   r   r   r   rf     s8    
8rf   c	          	   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jks|tjks|tjks|tj	ks|tj
ks|tjkrd}	n.|tjkrd}	t| ||||||}
ntd|	st| |||||||}| |  | t | }|| | t | }|| ||  d| | t |  }|| || | d   d }
t|
|d| }
|
S )a7  
    Calculate the magnitude of the dimensionless spin parameter of the final BH resulting from the merger of two black holes,
    including the in-plane spin components;
    by either using a precessing fit from the literature;
    or by first projecting the spins along the angular momentum and using an aligned-spin fit from the literature,
    then adding in quadrature the in-plane dimensionful spin scaled by the initial mass squared.

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    tilt1, tilt2 : tilts (in radians) in the new spin convention
    phi12: angle (in radians) between in-plane spin components
    fitname: fit selection currently supports Pan2011 (non-spinning), HLZ2014 (aligned+augmentation),
                                              PhenomD (aligned+augmentation), UIB2016 (aligned+augmentation),
                                              HL2016 (aligned+augmentation), HBR2016 (precessing)

    Returns
    -------
    magnitude of the dimensionless final spin parameter, chif
    FTzUnrecognized fit name.g       @g      @g      ?z
augmented )r   r4   r5   r6   r   r   r   r   r   r   r   r   r   !bbh_final_spin_precessing_HBR2016r   rf   sinrq   r2   )r   r   r   r   rg   rh   rm   r1   ry   Zprecfitr0   ZchifalignedZ	S1perpmagZ	S2perpmagZ	Sperpmag2r   r   r   rl     s,    <&rl   c             C   sT  t tt | } t tt |}t tt |}t tt |}t t|dkrjtdt t|dkrtd| | }|| }| | | }|| }|| }|| }	|| d  | }
||d  | }|
| }|dd|   }|dd| d|  d	|  d
|	  ddd|  d|  |   ddd|  d|  |     }|S )a}  
    Calculate the mass of the final BH resulting from the
    merger of two black holes with non-precessing spins using the fits
    used by IMRPhenomD, given in Eqs. (3.6) and (3.8) of Husa et al.
    arXiv:1508.07250. Note that Eq. (3.8) gives the radiated energy, not
    the final mass directly

    m1, m2: component masses
    chi1, chi2: dimensionless spins of two BHs
    r
   zchi1 has to be in [-1, 1]zchi2 has to be in [-1, 1]r<   g      ?g       @g
\?g*&?g}r^Խ?gD
@g4Ehg$ @ghГg@g&ap|gF4p?ge3
7@)r   r4   r5   r6   r   r   r   )r   r   r   r   r8   msqr9   eta2eta3eta4S1rT   rA   ShMfr   r   r   rr   :  s*    ">rr   c             C   s  t tt | } t tt |}t tt |}t tt |}t t|dkrjtdt t|dkrtd| | }|| }| | | }|| }|| }|| }	|| d  | }
||d  | }|
| }|dd|   }|| }|| }|| }d| d|  d	|  d
|	  dd|  d|  |  d| d|  |  d| d|  |  d| d|  |  }|S )a}  
    Calculate the spin of the final BH resulting from the
    merger of two black holes with non-precessing spins using the fits
    used by IMRPhenomD, given in Eqs. (3.6) and (3.8) of Husa et al.
    arXiv:1508.07250. Note that Eq. (3.8) gives the radiated energy, not
    the final mass directly

    m1, m2: component masses
    chi1, chi2: dimensionless spins of two BHs
    r
   zchi1 has to be in [-1, 1]zchi2 has to be in [-1, 1]r<   g      ?g       @gLXz@gVHԘ@gy.i"@g9+w\*@g,u5ȵ?gYf6Y@g&~˵?gQ1 @g2.Ŭgo޽o@gb\,gOO* @)r   r4   r5   r6   r   r   r   )r   r   r   r   r8   r|   r9   r}   r~   r   r   rT   rA   r   ZSsqZScuZSqur0   r   r   r   rv   d  s.    trv   c             C   s  t tt | } t tt |}t tt |}t tt |}t | dk rftdt |dk r|tdt t|dkrtdt t|dkrtd| | }t |dkrtd|| }| |  }|| }| | | }t |dkrtd	 t |d}t |d
k r:td t 	|d
}|| }	|	| }
|	|	 }|| | }|| | }|| }|| ||  ||  }|| }|| }|| }|| }t || krt 
| | | }|| }d}d}dd|  d }|||	|
|||||||||||fS )zP
    common setup function for UIB final-state and luminosity fit functions
    r   zm1 must not be negativezm2 must not be negativer
   zchi1 has to be in [-1, 1]zchi2 has to be in [-1, 1]zm1+m2 must be positiveg      ?zTruncating eta from above to 0.25. This should only be necessary in some rounding corner cases, but better check your m1 and m2 inputs...g        zTruncating negative eta to 0.0. This should only be necessary in some rounding corner cases, but better check your m1 and m2 inputs...g;f?gLXz?g      ?g      @g      ?)r   r4   r5   r6   r   r   r   r.   r=   maximumr-   )r   r   r   r   r8   r|   Zm1sqZm2sqr9   r}   r~   r   r   rT   StotShatShat2Shat3Shat4chidiffchidiff2sqrt2sqrt3
sqrt1m4etar   r   r   bbh_UIBfits_setup  sV    r   ro   c       .      C   sR  t | |||\}}}}}	}
}}}}}}}}}|dkrd}d}d}d}d}d}d}d	}d
}d}d}d}d} d}!d}"d}#d}$d}%d}&d}'d}(d})d}*d}+nt|dkrd}d}d}d}d}d }d!}d"}d#}d$}d%}d&}d'} d(}!d)}"d}#d*}$d+}%d,}&d-}'d.}(d/})d0}*d1}+ntd2d3d4|  | ||  ||  ||	  d3|| | |"|)|  d5d5|"  d6|)  |    || | ||#|  d5d5|  d6|#  |    || | | |*|  d5d5|   d6|*  |     d3|| | |!|+|  d5d5|!  d6|+  |     |$| | d3|%|   |  |'| | | d3|(|   |  |&| |  },|d3|,  }-|-S )7a  
    Calculate the final mass with the aligned-spin NR fit
    by Xisco Jimenez Forteza, David Keitel, Sascha Husa et al.
    [LIGO-P1600270] [https://arxiv.org/abs/1611.00332]
    versions v1 and v2 use the same ansatz,
    with v2 calibrated to additional SXS and RIT data

    m1, m2: component masses
    chi1, chi2: dimensionless spins of two BHs
    Results are symmetric under simultaneous exchange of m1<->m2 and chi1<->chi2.
    rp   g^I+?gzG?g(\?g	cZgfa?g#{gfӌ˝t	@gkG^9ĿgvH=>ĿgdM¿g
J!@gΉt@g9@gTSn?g y<5?g        gMe(qgx
g?gsw/g%wg2V,@gB3=mg|$5ro   g/$?g5^I?gMb?gA`"˿gO*?gg-;A)	@gqhʿg'0J:ɿgV*TĿgM@gq'@gAA?@gWl?gA?gppgYYZ	g! R?gKetAg~g{#y/@gUSZ(tngq/vz1Unknown version -- should be either "v1" or "v2".g      ?gUUUUUUg      0@g      @)r   r   ).r   r   r   r   rY   r8   r9   r}   r~   r   r   r   r   r   r   r   r   r   r   r   b10b20b30b50a2a3a4b1b2b3b5f20f30f50f10f21d10d11d20d30d31f11f31f51ZEradr   r   r   r   rs     sp    ,
 9rs   c       2      C   s  t | |||\}}}}}	}
}}}}}}}}}|dkrd}d}d}d}d}d}d}d	}d
}d}d}d}d} d}!d}"d}#d}$d}%d}&d}'d}(d})d}*d}+d},d}-d}.d}/n|dkr"d}d }d!}d}d"}d#}d$}d%}d&}d'}d(}d)}d*} d+}!d,}"d-}#d.}$d/}%d}&d0}'d1}(d2})d3}*d4}+d5},d6}-d7}.d8}/ntd9d:| | || |  || |  d;|| |   || | |%| |,|  d<d=|%  d>|,  |   || | |"| |-|  d<d=|"  d>|-  |    ||  | |#| |.|  d<d=|#  d>|.  |    d;||! | |$|/|  |&|  d<d<|$  d=|/  d>|&  |      |'| | d;|(|   |  |*| | | d;|+|   |  |)| |  }0|0|
 }1|1S )?a  
    Calculate the final spin with the aligned-spin NR fit
    by Xisco Jimenez Forteza, David Keitel, Sascha Husa et al.
    [LIGO-P1600270] [https://arxiv.org/abs/1611.00332]
    versions v1 and v2 use the same ansatz,
    with v2 calibrated to additional SXS and RIT data

    m1, m2: component masses
    chi1, chi2: dimensionless spins of two BHs
    Results are symmetric under simultaneous exchange of m1<->m2 and chi1<->chi2.
    rp   gQ@gRQ?gQ@gEȿg333333?g	?gw/gd-@gFpA#g#5@g++?gU)<?g]i\?g|B3>?gE	5+!@g;V7@ga^?gL3@g        gy1D?guCu'@g&gg@gJI?5g[YO?gB-Dg3.Yfg

S:ro   g(\@g?g
ףp=
@gӼɵ?gD ?gI+gB$v<@gTkˇ"gW@g?gntA5?g o?gXon?g!@gIg!}6@g`i?gz|@gC=?gcPeG"@gJ2g #L@gLP>
gz2`?gR@g&ˠ:cgz1Unknown version -- should be either "v1" or "v2".g       @g      ?g      P@g      0@g      @)r   r   )2r   r   r   r   rY   r8   r9   r}   r~   r   r   r   r   r   r   r   r   r   r   r   Za20Za30Za50r   r   r   r   r   r   a5r   r   r   r   r   r   r   r   Zf52r   r   r   r   r   Zf12Zf22Zf32r   ZLorbr0   r   r   r   rw   *  s    ,
 ]rw   c             C   sb   t tt | } t tt |}t tt |}t tt |}| |||||  fS )z
    Setup function for the Hofmann, Barausse, and Rezzolla final spin fits to vectorize the masses and spins and calculate the mass ratio.
    )r   r4   r5   r6   )r   r   r   r   r   r   r   _bbh_HBR2016_setup  s
    r   c       *      C   s  |dkr.d}d}d}d}d}	d}
d}d	}d
}n|dkrd}d}d}d}d}	d}d}
d}d}d}d}d}d}d}d}d}d}d}d}nn|dkrd}d}d }d!}d"}	d#}d$}d%}
d&}d'}d(}d)}d*}d+}d,}d-}d.}d/}d0}d1}d2}d3}d4}nt d5| | }||  }| | ||  }||| |  d6| d6|   }||| ||   } t| }!d6d7|!  d8 }"d9d6d:d;|! d: d8    }#|dkr| |  }$|||   |	|$  ||
||   ||$    }%|dkr.|| }&|&| }'|$|  }(|%|||  |(  |&|||   ||$  ||(    |'|||   ||$  ||(    }%|dkr`|%|||  |&|  |'|  |( |   }%t|#d:| |"d6   ||%  })|)S )<aq  
    Compute the orbital angular momentum ell used by the Hofmann, Barausse, and Rezzolla final spin fit [from ApJL 825, L19 (2016)], henceforth HBR.
    The three versions available correspond to the three choices of fit coefficients given in Table 1 of that paper, with 6, 16, and 20 coefficients, respectively.

    version can thus be "M1J2", "M3J3", or "M3J4"
    ZM1J2r
   r<   gRDUg~:gvR~RgV@g镲?gt^c@gy]?M3J3r3   g.oSgʡE@gd]Kȇg&OgZt@@gǘ~Og9#J{$@gԚL@gvagQt@gGx$h*gq=
ףogfffffBj@g
g@egGzw@gp?ZM3J4rD   g)*g3P>#@gd`@gL;gm4*g6[A@g6;Rg ~:Uglxz,oW@g33333i@gMZbgm2x@g/${@g㥛 6}gʡEgx&k@gtDg#~jɅgCl獇@g(\;@g,V?z<Unknown version--should be either "M1J2", "M3J3", or "M3J4".g      ?gUUUUUU?g      ?g&D\4?g       @g      @)r   r?   r   )*r   r   chi1zchi2zrY   ZnMZnJZk00Zk01Zk02Zk10Zk11Zk12xiZk03Zk13Zk20Zk21Zk22Zk23Zk30Zk31Zk32Zk33Zk04Zk14Zk24Zk34r8   qr9   atotZaeffrZ   Ze_iscoZl_iscoZaeff2Zksumr}   r~   Zaeff3ellr   r   r   _bbh_HBR2016_ell  s    
 
,
T
( r   r   c             C   s`   t | |||\} }}}}||| |  d| d|   }t| ||||}||d| d |   S )a\  
        Calculate the (signed) dimensionless spin of the final BH resulting from the
        merger of two black holes with aligned spins using the fit from Hofmann, Barausse, and Rezzolla ApJL 825, L19 (2016), henceforth HBR.

        The three versions available correspond to the three choices of fit coefficients given in Table 1 of that paper, with 6, 16, and 20 coefficients, respectively.

        version can thus be "M1J2", "M3J3", or "M3J4"

        m1, m2: component masses
        chi1z, chi2z: components of the dimensionless spins of the two BHs along the orbital angular momentum
        g      ?g       @)r   r   )r   r   r   r   rY   r   r   r   r   r   r   rx     s     rx   c          	   C   s  t | t | t | t | t | t | t | dkrft t| |||||||S t| |||\} }}}}t tt |}t tt |}t tt |}d}	t |}
t ||	t |  }t |}t ||	t |  }d|
|
  d||   d t | |
|  }|| }t	| ||| || |}|| || | |  d| | | |  d|| || |   | |  || |  }|dk rt
d|  d}|d d| d|   S )a  
        Calculate the dimensionless spin of the final BH resulting from the
        merger of two black holes with precessing spins using the fit from Hofmann, Barausse, and Rezzolla ApJL 825, L19 (2016), henceforth HBR.

        The three versions available correspond to the three choices of fit coefficients given in Table 1 of that paper, with 6, 16, and 20 coefficients, respectively.

        version can thus be "M1J2", "M3J3", or "M3J4"

        m1, m2: component masses
        chi1, chi2: dimensionless spins of two BHs
        tilt1, tilt2: tilt angles of the spins from the orbital angular momentum
        phi12: angle between in-plane spin components
        r
   g~jt?g      ?g       @g        zbbbh_final_spin_precessing_HBR2016(): The argument of the square root is %f; truncating it to zero.g      ?)r   r/   r4   rz   r   r5   r6   rq   r{   r   r.   )r   r   r   r   rg   rh   rm   rY   r   ZepsZcos_betaZ	cos_betasZ	cos_gammaZ
cos_gammasZ	cos_alphaq2r   Zsqrt_argr   r   r   rz     s&    J

.X
rz   c               @   s   e Zd ZdZdZdZdS )bbh_Lpeak_fitsZT1600018r   r   N)r   r   r   t1600018r   r   r   r   r   r   r   R  s   r   c             C   s   t jd }||  S )zm
    convert from geometric units ("Planck luminosity" of c^5/G) to multiples of 10^56 erg/s = 10^49 J/s
    gn5)lalZLUMPL_SI)LpeakZLumPl_ergs_per_secr   r   r   _rescale_LpeakW  s    
r   c             C   sf   t tt | } t | dkr*tdt | dkr@tddd|   }| d|   }t||||S )a9  
    wrapper to bbh_peak_luminosity_non_precessing_T1600018() for backwards compatibility

    q: mass ratio (here m2/m1, where m1>m2)
    chi1: the component of the dimensionless spin of m1 along the angular momentum (z)
    chi2: the component of the dimensionless spin of m2 along the angular momentum (z)
    g        zq has to be > 0.g      ?zq has to be <= 1.)r   r4   r5   r6   r   r   +bbh_peak_luminosity_non_precessing_T1600018)r   r   r   r   r   r   r   r   bbh_aligned_Lpeak_6mode_SHXJDK^  s    	r   c             C   sT  t tt | } t tt |}t tt |}t tt |}t| |||\}}}}}}	}
}}}}}}}}dd|  }| | ||  | }|| }|| }|| }|| }dd|  d|  d|  d|  d|  | d	d
|  d|  d|  d|  d|  |  d|d  |d  |  d|d  |d  |  }t|S )a5  
    Calculate the peak luminosity (using modes 22, 21, 33, 32, 44, and 43) of a binary black hole with aligned spins using the fit made by Sascha Husa, Xisco Jimenez Forteza, David Keitel [LIGO-T1500598] using 5th order in chieff and return results in units of 10^56 ergs/s

    m1, m2: component masses
    chi1: the component of the dimensionless spin of m1 along the angular momentum (z)
    chi2: the component of the dimensionless spin of m2 along the angular momentum (z)
    Results are symmetric under simultaneous exchange of m1<->m2 and chi1<->chi2.
    g      ?g      @gtQ?g"t?gQ3V2?g;)ES/?glk#R?g4J5?guȩ7;?g \?g]?g?gn5w?gQjg?g*Uʚ?g&qV?g,üM	@gvovI?gh?g {?)r   r4   r5   r6   r   r   )r   r   r   r   r8   r9   r}   r~   r   r   r   r   r   r   r   r   r   r   r   rU   Zchi_effZchi_eff2Zchi_eff3Zchi_eff4Zchi_eff5r   r   r   r   r   w  s    ,r   c       .      C   s  t | |||\}}}}}}	}
}}}}}}}}|| }d}d}d}d}d}d}d}d}d	}d
}d}d}d} d}!d}"d}#d}$d}%d}&d}'d}(d})d}*d}+|||  ||  ||  ||  ||  d| |
 | |!|  dd|   d|!  |   d| | |"|#|  dd|"  d|#  |    ||$|%|  d|$ d|%  |    ||&|'|  d|& d|'  |    dd| |
 |(|)|  dd|(  d|)  |    ||*|+|  d|* d|+  |      ||||
  | | ||    },d }-|,| |- },t|,S )!a  
    Calculate the peak luminosity with the aligned-spin NR fit
    by David Keitel, Xisco Jimenez Forteza, Sascha Husa, Lionel London et al.
    [LIGO-P1600279-v5] [https://arxiv.org/abs/1612.09566v1]
    using modes up to lmax=6,
    and return results in units of 10^56 ergs/s

    m1, m2: component masses
    chi1, chi2: dimensionless spins of two BHs
    Results are symmetric under simultaneous exchange of m1<->m2 and chi1<->chi2.
    gڐו?gh gBImgA@g`ngۻyh@g3Rg"4aS\?g?N1ƿgi!?g1P@g&Z?g*D@gp}?g䛈*g$?@gSHqgᱥ
οg?Aʰ@g	}?gh?g 0@g&0gD|gd3?g        g(\?g      0@g      @g1Zd?g      0g      ?gˡE?g=IQŐ?)r   r   ).r   r   r   r   r8   r9   r}   r~   r   r   r   r   r   r   r   r   r   r   r   Zeta5Za0Za1r   r   r   r   r   r   Zb4r   r   r   r   r   r   r   r   r   Zf40Zf41Zf60Zf61Zf70Zf71r   r[   r   r   r   *bbh_peak_luminosity_non_precessing_UIB2016  s>    , Mr   c       
      C   s   t tt | } t tt |}t tt |}t tt |}t| |||\}}}}ddddddddd	d
dddddddddg}t|||||}	t|	S )a  
    Calculate the peak luminosity of an aligned-spin binary black hole coalescence using the fit from Healy and Lousto arXiv:1610.09713 (henceforth HL)

    Parameters
    ----------
    m1, m2 : component masses
    chi1z, chi2z : components of the dimensionless spins of the two BHs along their orbital angular momentum

    Returns
    -------
    peak luminosity in units of 10^56 ergs/s

    g7{uP?gl-hM?gثgX,N?g[G>g(59gĚDog[|i#?gV?gmK-@g5-L?gY!d>gg`cX>g&X?g7qbu#g!?g8%?g\p6gDgP)r   r4   r5   r6   rC   rV   r   )
r   r   r   r   r9   r@   rA   rB   rS   r   r   r   r   ,bbh_peak_luminosity_non_precessing_Healyetal  s    *r   c       
      C   s   t tt | } t tt |}t tt |}t tt |}t tt |}t tt |}t| ||| |t | }|t | }|tjkrt| |||}	n<|tj	krt
| |||}	n"|tjkrt| |||}	ntd|	S )a  
    Calculate the peak luminosity of the merger of two black holes,
    only using the projected spins along the total angular momentum
    and some aligned-spin fit from the literature

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    tilt1, tilt2 : tilts (in radians) in the new spin convention
    fitname: fit selection currently supports T1600018, UIB2016, HL2016

    Returns
    -------
    peak luminosity, Lpeak, in units of 10^56 ergs/s
    zUnrecognized fit name.)r   r4   r5   r6   r   rq   r   r   r   r   r   r   r   r   )
r   r   r   r   rg   rh   r1   rt   ru   r   r   r   r   #bbh_peak_luminosity_projected_spins  s"    


r   c	          
      s   dkr&t tt|dkr&td  dkr:td   fdd}	dd	 }
t|}|jsftd
t|}t |
| |
||
||
||
||
||
|}t||g}t	| dr| j
}nd}x8t|D ],\}}|	| |||||||}|d||< qW tj|dd}||S )aD  
    Calculate the average of the predictions of various fits, currently just for the final mass, final spin, or peak luminosity of a quasicircular
    binary black hole coalescence. The final spin calculation returns the magnitude of the final spin, including the contribution
    from in-plane spins; the other two cases just apply aligned-spin fits to the components of the spins along the orbital angular momentum.

    Parameters
    ----------
    m1, m2 : component masses
    chi1, chi2 : dimensionless spins of two BHs
    tilt1, tilt2 : tilts (in radians) in the new spin convention
    phi12: angle (in radians) between in-plane spin components (only used for the final spin)
    quantity: "Mf", "af", "afz", or "Lpeak"
    fits: An array of fit names to be used. The possible fit names are those known by bbh_final_mass_projected_spins, bbh_final_spin_precessing,
          and bbh_peak_luminosity_projected_spins

    The shape of m1 is used to determine the shape of the output.

    Returns
    -------
    Average of the results for the given fits for the chosen quantity
    afr   z=Note: phi12 is only used for the full final spin calculation.)r   r   afzr   zUnknown quantity: %sc          	      sv    dkrt | ||||||S  dkr:t| |||||||S  dkrVt| ||||||S  dkrrt| ||||||S d S )Nr   r   r   r   )rj   rl   rf   r   )r   r   r   r   rg   rh   rm   r1   )quantityr   r   _return_quantity:  s    z5bbh_average_fits_precessing.<locals>._return_quantityc             S   s   t | drt| S dS d S )Nr_   r
   )rb   len)rc   r   r   r   
_len_smartE  s    
z/bbh_average_fits_precessing.<locals>._len_smartz1The list of fits passed cannot be an empty array.shape)Zaxis)maxr   r   Z
atleast_1dr.   r   r/   r   Zzerosrb   r   	enumerateZreshapeZmean)r   r   r   r   rg   rh   rm   r   Zfitsr   r   Znum_fitsZnum_datadataZ
data_shapekfitZdata_portionZdata_avgr   )r   r   bbh_average_fits_precessing  s(    
0
r   )r'   )rW   )rW   N)N)N)ro   )ro   )r   )r   )1__doc__numpyr   Zscipy.optimizeoptimizera   r   ImportErrorr.   r	   r   r   r   r   r   r    r2   r:   r;   r?   rC   rV   r]   r`   re   ri   rk   rn   rj   r"   rf   rl   rr   rv   r   rs   rw   r   r   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sZ   
	
	
>
&
Y

<59*.A
W
_h

;!8-