Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Halo bias tests #73

Merged
merged 2 commits into from Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions numcosmo/lss/nc_halo_bias_st_spher.c
Expand Up @@ -29,7 +29,7 @@
* @short_description: Sheth-Tormen spherical halo bias function type.
*
* Object implementation to compute the halo bias function given
* the Sheth-Tormen mass function for spherical collapse. A description
* the Sheth-Tormen mass function for spherical collapse. A description
* of the function is given below. Check nc_halo_bias.c for a description
* of halo biases and nc_multiplicity_func_st.c for the Sheth-Tormen
* mass function.
Expand All @@ -41,9 +41,9 @@
* \begin{align}
* b(\nu)=1+\frac{a \nu^2-1}{\delta_c}+\frac{2 p}{\delta_c\left[1+\left(a_c \nu^2\right)^p\right]}
* \end{align}
* where $b(\nu)$ is the bias, $\delta_c$ is the critical threshold,
* where $b(\nu)$ is the bias, $\delta_c$ is the critical threshold,
* $\nu = \frac{\delta_c}{\sigma}$, and $(a, p)$ are free parameters
* determined by the number of massive halos in the simulations and
* determined by the number of massive halos in the simulations and
* by the shape of the mass function at the low-mass end, respectively.
*
* The user must provide input the values: @NcHaloMassFunction, @delta_c, @a, @p - nc_halo_bias_st_spher_new_full().
Expand Down Expand Up @@ -207,7 +207,7 @@ _nc_halo_bias_st_spher_eval (NcHaloBias *biasf, NcHICosmo *cosmo, gdouble sigma
*
* Returns: A new #NcHaloBias.
*/
NcHaloBias *
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the only error I found

NcHaloBiasSTSpher *
nc_halo_bias_st_spher_new (NcHaloMassFunction *mfp)
{
return g_object_new (NC_TYPE_HALO_BIAS_ST_SPHER,
Expand All @@ -226,7 +226,7 @@ nc_halo_bias_st_spher_new (NcHaloMassFunction *mfp)
*
* Returns: A new #NcHaloBias.
*/
NcHaloBias *
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

NcHaloBiasSTSpher *
nc_halo_bias_st_spher_new_full (NcHaloMassFunction *mfp, gdouble delta_c, gdouble a, gdouble p)
{
return g_object_new (NC_TYPE_HALO_BIAS_ST_SPHER,
Expand Down Expand Up @@ -322,4 +322,3 @@ nc_halo_bias_st_spher_get_p (const NcHaloBiasSTSpher *biasf_st_spher)
}

/* _NC_BIAS_FUNCTION_ST_SPHER_DATASET_9901122 = {1.686, 0.75, 0.3}; */

5 changes: 2 additions & 3 deletions numcosmo/lss/nc_halo_bias_st_spher.h
Expand Up @@ -60,8 +60,8 @@ struct _NcHaloBiasSTSpher

GType nc_halo_bias_st_spher_get_type (void) G_GNUC_CONST;

NcHaloBias *nc_halo_bias_st_spher_new (NcHaloMassFunction *mfp);
NcHaloBias *nc_halo_bias_st_spher_new_full (NcHaloMassFunction *mfp, gdouble delta_c, gdouble a, gdouble p);
NcHaloBiasSTSpher *nc_halo_bias_st_spher_new (NcHaloMassFunction *mfp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

NcHaloBiasSTSpher *nc_halo_bias_st_spher_new_full (NcHaloMassFunction *mfp, gdouble delta_c, gdouble a, gdouble p);

void nc_halo_bias_st_spher_set_delta_c (NcHaloBiasSTSpher *biasf_st_spher, gdouble delta_c);
gdouble nc_halo_bias_st_spher_get_delta_c (const NcHaloBiasSTSpher *biasf_st_spher);
Expand All @@ -73,4 +73,3 @@ gdouble nc_halo_bias_st_spher_get_p (const NcHaloBiasSTSpher *biasf_st_spher);
G_END_DECLS

#endif /* _NC_HALO_BIAS_ST_SPHER_H_ */

10 changes: 10 additions & 0 deletions tests/Makefile.am
Expand Up @@ -110,6 +110,9 @@ test_ncm_sf_spherical_harmonics_SOURCES = \

test_ncm_sphere_map_SOURCES = \
test_ncm_sphere_map.c

test_nc_halo_bias_SOURCES = \
test_nc_halo_bias.c

test_nc_hicosmo_de_SOURCES = \
test_nc_hicosmo_de.c
Expand Down Expand Up @@ -188,6 +191,7 @@ test_programs = \
test_ncm_fit_esmcmc \
test_ncm_sf_spherical_harmonics \
test_ncm_sphere_map \
test_nc_halo_bias \
test_nc_hiqg_1d \
test_nc_hicosmo_de \
test_nc_window \
Expand Down Expand Up @@ -374,6 +378,12 @@ test_ncm_sphere_map_LDADD = \
$(GLIB_LIBS) \
$(GSL_LIBS) \
$(COVLIBS)

test_nc_halo_bias_LDADD = \
$(top_builddir)/numcosmo/libnumcosmo.la \
$(GLIB_LIBS) \
$(GSL_LIBS) \
$(COVLIBS)

test_nc_hicosmo_de_LDADD = \
$(top_builddir)/numcosmo/libnumcosmo.la \
Expand Down
234 changes: 234 additions & 0 deletions tests/test_nc_halo_bias.c
@@ -0,0 +1,234 @@
/***************************************************************************
* test_nc_halo_bias.c
*
* Mon Jan 16/02 10:04:16 2023
* Copyright 2023 Eduardo José Barroso
* <eduardojsbarroso@gmail.com>
****************************************************************************/
/*
* numcosmo
* Copyright (C) Eduardo José Barroso 2023 <eduardojsbarroso@gmail.com>
* numcosmo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* numcosmo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#undef GSL_RANGE_CHECK_OFF
#endif /* HAVE_CONFIG_H */
#include <numcosmo/numcosmo.h>

#include <math.h>
#include <glib.h>
#include <glib-object.h>

typedef struct _TestNcHaloBias TestNcHaloBias;

struct _TestNcHaloBias
{
NcHaloBiasPS *ps;
NcHaloBiasSTEllip *bste;
NcHaloBiasSTSpher *bsts;
NcHaloBiasTinker *bt;
NcHICosmo *cosmo;
NcHaloMassFunction *mfp;
gdouble z;
gdouble R1, R2, R3;
gint ntests;
gboolean (*rng_params) (TestNcHaloBias *test);
};

void test_nc_halo_bias_new (TestNcHaloBias *test, gconstpointer pdata);
void test_nc_halo_bias_eval (TestNcHaloBias *test, gconstpointer pdata);
void test_nc_halo_bias_free(TestNcHaloBias *test, gconstpointer pdata);
void test_nc_halo_bias_set_get (TestNcHaloBias *test, gconstpointer pdata);

gint
main (gint argc, gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
ncm_cfg_init_full_ptr (&argc, &argv);
ncm_cfg_enable_gsl_err_handler ();

g_test_add ("/nc/halo_bias/eval", TestNcHaloBias, NULL,
&test_nc_halo_bias_new,
&test_nc_halo_bias_eval,
&test_nc_halo_bias_free);

g_test_add ("/nc/halo_bias/set_get", TestNcHaloBias, NULL,
&test_nc_halo_bias_new,
&test_nc_halo_bias_set_get,
&test_nc_halo_bias_free);
g_test_add ("/nc/halo_bias/integrand", TestNcHaloBias, NULL,
&test_nc_halo_bias_new,
&test_nc_halo_bias_set_get,
&test_nc_halo_bias_free);


g_test_run ();
}

void
test_nc_halo_bias_free (TestNcHaloBias *test, gconstpointer pdata)
{
NCM_TEST_FREE (nc_halo_bias_free, NC_HALO_BIAS (test->ps));



ncm_model_free (NCM_MODEL (test->cosmo));

nc_halo_mass_function_free (test->mfp);
}

void
test_nc_halo_bias_new (TestNcHaloBias *test, gconstpointer pdata)
{
NcHICosmo *cosmo = nc_hicosmo_new_from_name (NC_TYPE_HICOSMO, "NcHICosmoDEXcdm");
NcDistance *dist = nc_distance_new (3.0);
NcTransferFunc *tf = nc_transfer_func_new_from_name ("NcTransferFuncEH");
NcPowspecML *ps_ml = NC_POWSPEC_ML (nc_powspec_ml_transfer_new (tf));
NcmPowspecFilter *psf = ncm_powspec_filter_new (NCM_POWSPEC (ps_ml), NCM_POWSPEC_FILTER_TYPE_TOPHAT);
NcMultiplicityFunc *mulf = NC_MULTIPLICITY_FUNC (nc_multiplicity_func_tinker_new_full (NC_MULTIPLICITY_FUNC_MASS_DEF_CRITICAL, 500.0));
NcHaloMassFunction *mfp = nc_halo_mass_function_new (dist, psf, mulf);
NcHaloBiasPS *ps = nc_halo_bias_ps_new(mfp);
NcHaloBiasSTEllip *bste = nc_halo_bias_st_ellip_new(mfp);
NcHaloBiasSTSpher *bsts = nc_halo_bias_st_spher_new(mfp);
NcHaloBiasTinker *bt = nc_halo_bias_tinker_new(mfp);


test->cosmo = cosmo;
test->bsts = bsts;
test->bste = bste;
test->bt = bt;
test->ps = ps;
test->mfp = mfp;
test->z = 1.0;

{
NcHaloBiasPS *ps_test = nc_halo_bias_ps_new_full (mfp, 1.0);
g_assert_true(NC_IS_HALO_BIAS_PS (ps_test));
NCM_TEST_FREE (nc_halo_bias_free, NC_HALO_BIAS (ps_test));
}
{
NcHaloBiasTinker *bt_test = nc_halo_bias_tinker_new_full (mfp, 1.0, 2.0, 3.0, 4.0);
g_assert_true(NC_IS_HALO_BIAS_TINKER (bt_test));
NCM_TEST_FREE (nc_halo_bias_free, NC_HALO_BIAS (bt_test));
}
{
NcHaloBiasSTEllip *bste_test = nc_halo_bias_st_ellip_new_full (mfp, 1.0, 2.0, 3.0, 4.0);
g_assert_true(NC_IS_HALO_BIAS_ST_ELLIP (bste_test));
NCM_TEST_FREE (nc_halo_bias_free, NC_HALO_BIAS (bste_test));
}
{
NcHaloBiasSTSpher *bsts_test = nc_halo_bias_st_spher_new_full (mfp, 1.0, 2.0, 3.0);
g_assert_true(NC_IS_HALO_BIAS_ST_SPHER (bsts_test));
NCM_TEST_FREE (nc_halo_bias_free, NC_HALO_BIAS (bsts_test));
}

g_assert_true(NC_IS_HALO_BIAS_ST_SPHER (bsts));
g_assert_true(NC_IS_HALO_BIAS_ST_ELLIP (bste));
g_assert_true(NC_IS_HALO_BIAS_TINKER (bt));
g_assert_true(NC_IS_HALO_BIAS_PS (ps));


nc_multiplicity_func_free (mulf);
ncm_powspec_filter_free (psf);
nc_powspec_ml_free (ps_ml);
nc_transfer_func_free (tf);
nc_distance_free (dist);


}

void test_nc_halo_bias_set_get (TestNcHaloBias *test, gconstpointer pdata)
{
gdouble delta_c = g_test_rand_double_range(1.0, 10.0);
gdouble a = g_test_rand_double_range(1.0, 10.0);
gdouble b = g_test_rand_double_range(1.0, 10.0);
gdouble B = g_test_rand_double_range(1.0, 10.0);
gdouble c = g_test_rand_double_range(1.0, 10.0);
gdouble p = nc_halo_bias_st_spher_get_p(test->bsts);

{
nc_halo_bias_ps_set_delta_c(test->ps, delta_c);
g_assert_cmpfloat(nc_halo_bias_ps_get_delta_c(test->ps) , ==, delta_c);
}

{
nc_halo_bias_tinker_set_delta_c(test->bt, delta_c);
nc_halo_bias_tinker_set_B(test->bt, B);
nc_halo_bias_tinker_set_b(test->bt, b);
nc_halo_bias_tinker_set_c(test->bt, c);

g_assert_cmpfloat(nc_halo_bias_tinker_get_delta_c(test->bt) , ==, delta_c);
g_assert_cmpfloat(nc_halo_bias_tinker_get_B(test->bt) , ==, B);
g_assert_cmpfloat(nc_halo_bias_tinker_get_b(test->bt) , ==, b);
g_assert_cmpfloat(nc_halo_bias_tinker_get_c(test->bt) , ==, c);
}

{
nc_halo_bias_st_spher_set_delta_c(test->bsts, delta_c);
nc_halo_bias_st_spher_set_a(test->bsts, a);
nc_halo_bias_st_spher_set_p(test->bsts, p);

g_assert_cmpfloat(nc_halo_bias_st_spher_get_delta_c(test->bsts) , ==, delta_c);
g_assert_cmpfloat(nc_halo_bias_st_spher_get_a(test->bsts) , ==, a);
g_assert_cmpfloat(nc_halo_bias_st_spher_get_p(test->bsts) , ==, p);
}

{
nc_halo_bias_st_ellip_set_delta_c(test->bste, delta_c);
nc_halo_bias_st_ellip_set_a(test->bste, a);
nc_halo_bias_st_ellip_set_b(test->bste, b);
nc_halo_bias_st_ellip_set_c(test->bste, c);

g_assert_cmpfloat(nc_halo_bias_st_ellip_get_delta_c(test->bste) , ==, delta_c);
g_assert_cmpfloat(nc_halo_bias_st_ellip_get_a(test->bste) , ==, a);
g_assert_cmpfloat(nc_halo_bias_st_ellip_get_b(test->bste) , ==, b);
g_assert_cmpfloat(nc_halo_bias_st_ellip_get_c(test->bste) , ==, c);
}
}

void test_nc_halo_bias_eval (TestNcHaloBias *test, gconstpointer pdata)
{
gdouble eval_ps, eval_bste, eval_bsts, eval_bt;

eval_ps = nc_halo_bias_eval (NC_HALO_BIAS(test->ps), test->cosmo, 0.0, test->z);
eval_bste = nc_halo_bias_eval (NC_HALO_BIAS(test->bste ), test->cosmo, 5.0, test->z);
eval_bsts = nc_halo_bias_eval (NC_HALO_BIAS(test->bsts), test->cosmo, GSL_POSINF, test->z);
eval_bt = nc_halo_bias_eval (NC_HALO_BIAS(test->bt), test->cosmo, GSL_POSINF, test->z);

g_assert_cmpfloat(eval_ps, ==, GSL_POSINF);
g_assert_cmpfloat(eval_bt, ==, 1.0);
g_assert (gsl_finite (eval_bste));

{
gdouble delta_c = nc_halo_bias_st_spher_get_delta_c(test->bsts);
gdouble p = nc_halo_bias_st_spher_get_p(test->bsts);
gdouble bias_inf = 1.0 - 1.0 / delta_c + 2.0 * p / delta_c;
g_assert_cmpfloat(eval_bsts - bias_inf, >, -1.0e-8);
}

}

void test_nc_halo_bias_integrand (TestNcHaloBias *test, gconstpointer pdata)
{
gdouble lnM = g_test_rand_double_range(12.0, 15.0);
gdouble bias_integrand = nc_halo_bias_integrand(NC_HALO_BIAS(test->ps), test->cosmo, lnM,test->z);
gdouble d2n_dzdlnM = nc_halo_mass_function_d2n_dzdlnM (test->mfp, test->cosmo, lnM, test->z);
gdouble sigma = nc_halo_mass_function_sigma_lnM (test->mfp, test->cosmo, lnM, test->z);
gdouble bias = nc_halo_bias_eval (NC_HALO_BIAS(test->ps), test->cosmo, sigma, test->z);


g_assert_cmpfloat(d2n_dzdlnM * bias ,==, bias_integrand);
}