def T_BBN(eta, Xn, dN_eff=0, xi_nu=0):
    a = 2.23
    b = 223
    g_eff = lambda T: ge.g_eff_e_star(T, dN_eff=dN_eff, xi_nu=xi_nu)
    vec_res = np.array(
        [brentq(BBN_func2, a, b, args=(eta_i, Xn, g_eff)) for eta_i in eta])
    print(2.23 / vec_res)
    return 2.23 / vec_res
def solve_Boltz(T_range=(2, 1e-2),
                dN_eff=0,
                xi_nu=0,
                create_spline=False,
                H_z_pn_rate=None,
                H_z_np_rate=None,
                rtol=1e-6,
                atol=1e-20,
                y0=None):
    g_eff = lambda T: ge.g_eff_e_star(T, dN_eff=dN_eff, xi_nu=xi_nu)
    start_time = time.time()
    m = 0.511
    z_range = (m / T_range[0], m / T_range[1])
    rtol = rtol
    atol = atol

    if y0 is None:
        y0 = X_n_eq(z_range[0], xi_nu=xi_nu)
        if type(y0) != type(np.array([0])):
            y0 = np.array([y0])
    if create_spline:
        filename_np = 'gamma_np'
        filename_pn = 'gamma_pn'
        filename_Hz_np = 'gamma_H_z_np'
        filename_Hz_pn = 'gamma_H_z_pn'
        z_range_2 = (np.log10(z_range[0]) - 1, np.log10(z_range[1]) + 1)
        gamma_pn_spline(filename_pn, z_range=z_range_2)
        gamma_np_spline(filename_np, z_range=z_range_2)
        gamma_H_z_np(filename_Hz_np, z_range_2, g_eff)
        gamma_H_z_pn(filename_Hz_pn, z_range_2, g_eff)
    if not (H_z_pn_rate is None or H_z_np_rate is None):
        Boltzmann2 = ODE(Boltzmann_for_n,
                         y0,
                         z_range,
                         rtol=rtol,
                         atol=atol,
                         params=(H_z_pn_rate, H_z_np_rate),
                         dense_output=True,
                         verbose=True,
                         vectorized=True)
    else:
        Boltzmann2 = ODE(Boltzmann_for_n,
                         y0,
                         z_range,
                         rtol=rtol,
                         atol=atol,
                         dense_output=True,
                         verbose=True,
                         vectorized=True)
    print("--- %s seconds ---" % (time.time() - start_time))
    return Boltzmann2
def gamma_H_z_pn(filename, z_range, dN_eff=0, xi_nu=0, return_spline=False):
    zi = np.logspace(np.log10(z_range[0]) - 1, np.log10(z_range[1]) + 1, 1000)
    m = 0.511
    T = m / zi
    """
    In order to use the time temperature relation by Laine et al just uncomment these two lines
    """
    # dT_dt=ge.dT_dt(T)
    # func=-m/zi**2*1/dT_dt(T)*gamma_p_to_n(zi,xi_nu)
    g_eff = lambda t: ge.g_eff_e_star(t, dN_eff=dN_eff, xi_nu=xi_nu)
    geffs = ge.g_eff_s_tot(T, return_spline=True)
    dgeffs = lambda T: geffs.derivative(1)(T)
    func = gamma_p_to_n(zi, xi_nu) / (1.66 * np.sqrt(g_eff(0.511 / zi)) *
                                      (0.511)**2 / 1.221e22) * zi * (
                                          1 + 1 / 3 * T / geffs(T) * dgeffs(T))
    f2 = interp1d(zi, func, k=3)
    if return_spline:
        return f2
    func_to_pkl(f2, filename)