def test_tk3d_smoke(): """Make sure it works once.""" (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() tsp1 = ccl.Tk3D(a_arr, lk_arr, pk1_arr=fka1_arr, pk2_arr=fka2_arr) tsp2 = ccl.Tk3D(a_arr, lk_arr, tkk_arr=tkka_arr) assert_(not np.isnan(tsp1.eval(1E-2, 0.5))) assert_(not np.isnan(tsp2.eval(1E-2, 0.5)))
def test_tk3d_eval(is_product): (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() if is_product: tsp = ccl.Tk3D(a_arr, lk_arr, pk1_arr=fka1_arr, pk2_arr=fka2_arr) else: tsp = ccl.Tk3D(a_arr, lk_arr, tkk_arr=tkka_arr) # Test at single point ktest = 0.7 atest = 0.5 ptrue = tkkaf(ktest, ktest, atest) phere = tsp.eval(ktest, atest) assert_almost_equal(phere / ptrue, 1., 6) ktest = 5E-5 atest = 0.5 ptrue = tkkaf(ktest, ktest, atest) phere = tsp.eval(ktest, atest) assert_almost_equal(phere / ptrue, 1., 6) # Test at array of points ktest = np.logspace(-3, 1, 10) ptrue = tkkaf(ktest[None, :], ktest[:, None], atest) phere = tsp.eval(ktest, atest) assert_allclose(phere.flatten(), ptrue.flatten(), rtol=1E-6)
def test_tk3d_delete(): """Check that ccl.Tk3D.__del__ works.""" (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() tsp = ccl.Tk3D(a_arr, lk_arr, pk1_arr=fka1_arr, pk2_arr=fka2_arr) # This should not cause an ignored exception del tsp
def test_tk3d_spline_arrays(is_product): (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() if is_product: tsp = ccl.Tk3D(a_arr, lk_arr, pk1_arr=fka1_arr, pk2_arr=fka2_arr) else: tsp = ccl.Tk3D(a_arr, lk_arr, tkk_arr=tkka_arr) a_get, lk_get1, lk_get2, out = tsp.get_spline_arrays() assert np.allclose(a_get, a_arr, rtol=1e-15) assert np.allclose(lk_get1, lk_arr, rtol=1e-15) assert np.allclose(lk_get2, lk_arr, rtol=1e-15) if is_product: assert np.allclose(np.log(out[0]), fka1_arr, rtol=1e-15) assert np.allclose(np.log(out[1]), fka2_arr, rtol=1e-15) else: assert np.allclose(np.log(out[0]), tkka_arr, rtol=1e-15)
def get_tk3d(alpha=1, beta=1): a_arr = np.linspace(0.1, 1., 10) k_arr = np.geomspace(1E-4, 1E3, 10) tkka_arr = np.array( [tkkaf(k_arr[None, :], k_arr[:, None], a, alpha, beta) for a in a_arr]) return ccl.Tk3D(a_arr, np.log(k_arr), tkk_arr=np.log(tkka_arr), is_logt=True)
def test_tk3d_spline_arrays_raises(): (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() tsp = ccl.Tk3D(a_arr, lk_arr, tkk_arr=tkka_arr) # PR923 aims to change this bit of code; the assertion is there to remind # us to uncomment what is commented out. assert not hasattr(tsp.__class__, "has_tsp") # ccl.lib.f3d_t_free(tsp.tsp) # delattr(tsp, "tsp") # fool `Tk3D` into believing it doesn't have a `tsp` tsp.has_tsp = False with pytest.raises(ValueError): tsp.get_spline_arrays()
def test_tk3d_eval_errors(): (a_arr, lk_arr, fka1_arr, fka2_arr, tkka_arr) = get_arrays() tsp = ccl.Tk3D(a_arr, lk_arr, pk1_arr=fka1_arr, pk2_arr=fka2_arr) assert_raises(TypeError, tsp.eval, 1E-2, np.array([0.1]))