Beispiel #1
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)
Beispiel #2
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)