Example #1
0
def test_instantiate(modeling_data):
    """ Unit tests for modeling objects' instantiation """

    m = theo.Modeling()
    m.set_concentration(4.0)
    m.set_mass(1.0e15)
    assert m.backend == theo.be_nick

    assert_raises(ValueError, m.set_cosmo, 3.0)
    assert_raises(ValueError,
                  m.set_halo_density_profile,
                  halo_profile_model='bla')
    assert_raises(ValueError, m.set_halo_density_profile, massdef='blu')

    if theo.be_nick == 'nc':
        import gi
        gi.require_version("NumCosmoMath", "1.0")
        gi.require_version("NumCosmo", "1.0")

        import gi.repository.NumCosmoMath as Ncm
        import gi.repository.NumCosmo as Nc

        mset = m.get_mset()
        assert isinstance(mset, Ncm.MSet)

        m.set_mset(mset)
        assert_raises(AttributeError, m.set_mset, 3)

    r_proj = np.logspace(-2, 2, 100)
    z_cl = 0.5
    z_src = 0.85
    Sigma = m.eval_surface_density(r_proj, z_cl)
    Sigma_mean = m.eval_mean_surface_density(r_proj, z_cl)
    Sigma_excess = m.eval_excess_surface_density(r_proj, z_cl)

    assert_allclose(Sigma_excess, (Sigma_mean - Sigma), rtol=5.0e-15)

    shear = m.eval_tangential_shear(r_proj, z_cl, z_src)
    convergence = m.eval_convergence(r_proj, z_cl, z_src)
    reduced_shear = m.eval_reduced_tangential_shear(r_proj, z_cl, z_src)
    magnification = m.eval_magnification(r_proj, z_cl, z_src)

    assert_allclose(reduced_shear, shear / (1.0 - convergence), rtol=8.0e-15)
    assert_allclose(magnification,
                    1.0 / ((1.0 - convergence)**2 - np.abs(shear)**2),
                    rtol=5.0e-15)

    reduced_shear = m.eval_reduced_tangential_shear(
        r_proj, z_cl, np.repeat(z_src, len(r_proj)))
    assert_allclose(reduced_shear, shear / (1.0 - convergence), rtol=8.0e-15)
Example #2
0
def test_profiles(modeling_data):
    """ Tests for profile functions, get_3d_density, predict_surface_density,
    and predict_excess_surface_density """
    helper_profiles(theo.compute_3d_density)
    helper_profiles(theo.compute_surface_density)
    helper_profiles(theo.compute_excess_surface_density)

    reltol = modeling_data['theory_reltol']

    # Validation tests
    # NumCosmo makes different choices for constants (Msun). We make this conversion
    # by passing the ratio of SOLAR_MASS in kg from numcosmo and CLMM
    cfg = load_validation_config()
    cosmo = cfg['cosmo']

    assert_allclose(theo.compute_3d_density(cosmo=cosmo, **cfg['RHO_PARAMS']),
                    cfg['numcosmo_profiles']['rho'], reltol)
    assert_allclose(
        theo.compute_surface_density(cosmo=cosmo, **cfg['SIGMA_PARAMS']),
        cfg['numcosmo_profiles']['Sigma'], reltol)
    assert_allclose(
        theo.compute_excess_surface_density(cosmo=cosmo,
                                            **cfg['SIGMA_PARAMS']),
        cfg['numcosmo_profiles']['DeltaSigma'], reltol)

    # Object Oriented tests
    m = theo.Modeling()
    m.set_cosmo(cosmo)
    m.set_halo_density_profile(
        halo_profile_model=cfg['SIGMA_PARAMS']['halo_profile_model'])
    m.set_concentration(cfg['SIGMA_PARAMS']['cdelta'])
    m.set_mass(cfg['SIGMA_PARAMS']['mdelta'])

    assert_allclose(
        m.eval_3d_density(cfg['SIGMA_PARAMS']['r_proj'],
                          cfg['SIGMA_PARAMS']['z_cl']),
        cfg['numcosmo_profiles']['rho'], reltol)
    assert_allclose(
        m.eval_surface_density(cfg['SIGMA_PARAMS']['r_proj'],
                               cfg['SIGMA_PARAMS']['z_cl']),
        cfg['numcosmo_profiles']['Sigma'], reltol)
    assert_allclose(
        m.eval_excess_surface_density(cfg['SIGMA_PARAMS']['r_proj'],
                                      cfg['SIGMA_PARAMS']['z_cl']),
        cfg['numcosmo_profiles']['DeltaSigma'], reltol)
    if m.backend == 'ct':
        assert_raises(ValueError, m.eval_excess_surface_density, 1e-12,
                      cfg['SIGMA_PARAMS']['z_cl'])
Example #3
0
def test_compute_critical_surface_density(modeling_data):
    """ Validation test for critical surface density """
    
    reltol = modeling_data['theory_reltol']
    
    cfg = load_validation_config()
    assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'],
                                                    z_cluster=cfg['TEST_CASE']['z_cluster'],
                                                    z_source=cfg['TEST_CASE']['z_source']),
                    cfg['TEST_CASE']['nc_Sigmac'], reltol)
    # Check errors for z<0
    assert_raises(ValueError, theo.compute_critical_surface_density, cfg['cosmo'], z_cluster=-0.2, z_source=0.3)
    assert_raises(ValueError, theo.compute_critical_surface_density, cfg['cosmo'], z_cluster=0.2, z_source=-0.3)
    # Check behaviour when sources are in front of the lens
    z_cluster = 0.3
    z_source = 0.2
    assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'], z_cluster=z_cluster, z_source=z_source),
                    np.inf, 1.0e-10)
    z_source = [0.2, 0.12, 0.25]
    assert_allclose(theo.compute_critical_surface_density(cfg['cosmo'], z_cluster=z_cluster, z_source=z_source),
                    [np.inf, np.inf, np.inf], 1.0e-10)
    # Check usage with cluster object function
    z_src = np.array([cfg['TEST_CASE']['z_source']])
    cluster = GalaxyCluster(unique_id='blah', ra=0, dec=0, z=cfg['TEST_CASE']['z_cluster'],
                                 galcat=GCData([0*z_src, 0*z_src, z_src],
                                               names=('ra', 'dec', 'z')))
    cluster.add_critical_surface_density(cfg['cosmo'])
    assert_allclose(cluster.galcat['sigma_c'],
                    cfg['TEST_CASE']['nc_Sigmac'], reltol)

    # Object Oriented tests
    m = theo.Modeling()
    m.set_cosmo(cfg['cosmo'])
    assert_allclose(m.eval_critical_surface_density(cfg['TEST_CASE']['z_cluster'],
                                      cfg['TEST_CASE']['z_source']),
                cfg['TEST_CASE']['nc_Sigmac'], reltol)
    # Check behaviour when sources are in front of the lens
    z_cluster = 0.3
    z_source = 0.2
    assert_allclose(m.eval_critical_surface_density(z_cluster, z_source),
                np.inf, 1.0e-10)
    z_source = [0.2, 0.12, 0.25]
    assert_allclose(m.eval_critical_surface_density(z_cluster, z_source),
                [np.inf, np.inf, np.inf], 1.0e-10)
Example #4
0
def test_shear_convergence_unittests(modeling_data):
    """ Unit and validation tests for the shear and convergence calculations """
    helper_physics_functions(theo.compute_tangential_shear)
    helper_physics_functions(theo.compute_convergence)
    helper_physics_functions(theo.compute_reduced_tangential_shear)
    helper_physics_functions(theo.compute_magnification)

    reltol = modeling_data['theory_reltol']

    # Validation Tests -------------------------
    # NumCosmo makes different choices for constants (Msun). We make this conversion
    # by passing the ratio of SOLAR_MASS in kg from numcosmo and CLMM
    cfg = load_validation_config()
    constants_conversion = clc.SOLAR_MASS.value / cfg['TEST_CASE']['Msun[kg]']

    # First compute SigmaCrit to correct cosmology changes
    cosmo = cfg['cosmo']
    sigma_c = theo.compute_critical_surface_density(
        cosmo, cfg['GAMMA_PARAMS']['z_cluster'], cfg['z_source'])

    # Compute sigma_c in the new cosmology and get a correction factor
    sigma_c_undo = theo.compute_critical_surface_density(
        cosmo, cfg['GAMMA_PARAMS']['z_cluster'], cfg['z_source'])
    sigmac_corr = (sigma_c_undo / sigma_c)

    # Chech error is raised if too small radius
    assert_raises(ValueError, theo.compute_tangential_shear, 1.e-12, 1.e15, 4,
                  0.2, 0.45, cosmo)

    # Validate tangential shear
    gammat = theo.compute_tangential_shear(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(gammat * sigmac_corr, cfg['numcosmo_profiles']['gammat'],
                    reltol)

    # Validate convergence
    kappa = theo.compute_convergence(cosmo=cosmo, **cfg['GAMMA_PARAMS'])
    assert_allclose(kappa * sigmac_corr, cfg['numcosmo_profiles']['kappa'],
                    reltol)

    # Validate reduced tangential shear
    assert_allclose(
        theo.compute_reduced_tangential_shear(cosmo=cosmo,
                                              **cfg['GAMMA_PARAMS']),
        gammat / (1.0 - kappa), 1.0e-10)
    assert_allclose(gammat * sigmac_corr / (1. - (kappa * sigmac_corr)),
                    cfg['numcosmo_profiles']['gt'], 1.e2 * reltol)

    # Validate magnification
    assert_allclose(
        theo.compute_magnification(cosmo=cosmo, **cfg['GAMMA_PARAMS']),
        1. / ((1 - kappa)**2 - abs(gammat)**2), 1.0e-10)
    assert_allclose(1. / ((1 - kappa)**2 - abs(gammat)**2),
                    cfg['numcosmo_profiles']['mu'], 1.e2 * reltol)

    # Check that shear, reduced shear and convergence return zero and magnification returns one if source is in front of the cluster
    # First, check for a array of radius and single source z
    r = np.logspace(-2, 2, 10)
    z_cluster = 0.3
    z_source = 0.2

    assert_allclose(
        theo.compute_convergence(r,
                                 mdelta=1.e15,
                                 cdelta=4.,
                                 z_cluster=z_cluster,
                                 z_source=z_source,
                                 cosmo=cosmo), np.zeros(len(r)), 1.0e-10)
    assert_allclose(
        theo.compute_tangential_shear(r,
                                      mdelta=1.e15,
                                      cdelta=4.,
                                      z_cluster=z_cluster,
                                      z_source=z_source,
                                      cosmo=cosmo), np.zeros(len(r)), 1.0e-10)
    assert_allclose(
        theo.compute_reduced_tangential_shear(r,
                                              mdelta=1.e15,
                                              cdelta=4.,
                                              z_cluster=z_cluster,
                                              z_source=z_source,
                                              cosmo=cosmo), np.zeros(len(r)),
        1.0e-10)
    assert_allclose(
        theo.compute_magnification(r,
                                   mdelta=1.e15,
                                   cdelta=4.,
                                   z_cluster=z_cluster,
                                   z_source=z_source,
                                   cosmo=cosmo), np.ones(len(r)), 1.0e-10)

    # Second, check a single radius and array of source z
    r = 1.
    z_source = [0.25, 0.1, 0.14, 0.02]
    assert_allclose(
        theo.compute_convergence(r,
                                 mdelta=1.e15,
                                 cdelta=4.,
                                 z_cluster=z_cluster,
                                 z_source=z_source,
                                 cosmo=cosmo), np.zeros(len(z_source)),
        1.0e-10)
    assert_allclose(
        theo.compute_tangential_shear(r,
                                      mdelta=1.e15,
                                      cdelta=4.,
                                      z_cluster=z_cluster,
                                      z_source=z_source,
                                      cosmo=cosmo), np.zeros(len(z_source)),
        1.0e-10)
    assert_allclose(
        theo.compute_reduced_tangential_shear(r,
                                              mdelta=1.e15,
                                              cdelta=4.,
                                              z_cluster=z_cluster,
                                              z_source=z_source,
                                              cosmo=cosmo),
        np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(
        theo.compute_magnification(r,
                                   mdelta=1.e15,
                                   cdelta=4.,
                                   z_cluster=z_cluster,
                                   z_source=z_source,
                                   cosmo=cosmo), np.ones(len(z_source)),
        1.0e-10)

    # Object Oriented tests
    m = theo.Modeling()
    m.set_cosmo(cosmo)
    m.set_halo_density_profile(
        halo_profile_model=cfg['GAMMA_PARAMS']['halo_profile_model'])
    m.set_concentration(cfg['GAMMA_PARAMS']['cdelta'])
    m.set_mass(cfg['GAMMA_PARAMS']['mdelta'])
    # First compute SigmaCrit to correct cosmology changes
    sigma_c = m.eval_critical_surface_density(cfg['GAMMA_PARAMS']['z_cluster'],
                                              cfg['GAMMA_PARAMS']['z_source'])

    # Compute sigma_c in the new cosmology and get a correction factor
    sigma_c_undo = m.eval_critical_surface_density(
        cfg['GAMMA_PARAMS']['z_cluster'], cfg['GAMMA_PARAMS']['z_source'])
    sigmac_corr = (sigma_c_undo / sigma_c)

    # Validate tangential shear
    profile_pars = (cfg['GAMMA_PARAMS']['r_proj'],
                    cfg['GAMMA_PARAMS']['z_cluster'],
                    cfg['GAMMA_PARAMS']['z_source'])
    gammat = m.eval_tangential_shear(*profile_pars)
    assert_allclose(gammat * sigmac_corr, cfg['numcosmo_profiles']['gammat'],
                    reltol)

    # Validate convergence
    kappa = m.eval_convergence(*profile_pars)
    assert_allclose(kappa * sigmac_corr, cfg['numcosmo_profiles']['kappa'],
                    reltol)

    # Validate reduced tangential shear
    assert_allclose(m.eval_reduced_tangential_shear(*profile_pars),
                    gammat / (1.0 - kappa), 1.0e-10)
    assert_allclose(gammat * sigmac_corr / (1. - (kappa * sigmac_corr)),
                    cfg['numcosmo_profiles']['gt'], 1.e2 * reltol)

    # Validate magnification
    assert_allclose(m.eval_magnification(*profile_pars),
                    1. / ((1 - kappa)**2 - abs(gammat)**2), 1.0e-10)
    assert_allclose(1. / ((1 - kappa)**2 - abs(gammat)**2),
                    cfg['numcosmo_profiles']['mu'], 1.e2 * reltol)

    # Check that shear, reduced shear and convergence return zero and magnification returns one if source is in front of the cluster
    # First, check for a array of radius and single source z
    r = np.logspace(-2, 2, 10)
    z_cluster = 0.3
    z_source = 0.2

    assert_allclose(m.eval_convergence(r, z_cluster, z_source),
                    np.zeros(len(r)), 1.0e-10)
    assert_allclose(m.eval_tangential_shear(r, z_cluster, z_source),
                    np.zeros(len(r)), 1.0e-10)
    assert_allclose(m.eval_reduced_tangential_shear(r, z_cluster, z_source),
                    np.zeros(len(r)), 1.0e-10)
    assert_allclose(m.eval_magnification(r, z_cluster, z_source),
                    np.ones(len(r)), 1.0e-10)

    # Second, check a single radius and array of source z
    r = 1.
    z_source = [0.25, 0.1, 0.14, 0.02]

    assert_allclose(m.eval_convergence(r, z_cluster, z_source),
                    np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(m.eval_tangential_shear(r, z_cluster, z_source),
                    np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(m.eval_reduced_tangential_shear(r, z_cluster, z_source),
                    np.zeros(len(z_source)), 1.0e-10)
    assert_allclose(m.eval_magnification(r, z_cluster, z_source),
                    np.ones(len(z_source)), 1.0e-10)