# Source code notes on AnalyticDensityProf¶

This section of the documentation provides background material and detailed implementation notes on the functions and methods of the primary base class used to model the spatial distribution of matter and galaxies within halos, AnalyticDensityProf. This as an abstract base class and so it cannot itself be instantiated; only concrete sub-classes can be used to directly model the spatial profile of halos.

The purpose of the AnalyticDensityProf class is to provide a template for any Halotools model of the spatial distribution of points within a halo. So in Halotools, any analytical model for how either matter or galaxies are spatially distributed within their halos will subclass from the AnalyticDensityProf class.

This tutorial is organized as follows. The Halo Mass and Radius Definitions section reviews how halo boundaries and masses are defined with respect to a cosmologically evolving reference density. The Spatial Profiles of Halos section covers the mathematics of halo density profiles, including explicit derivations of the exact form of all equations are they are implemented in code. The tutorial concludes with the Constructor of the AnalyticDensityProf class section by describing how the __init__ constructor standardizes the attributes and behavior of the class to facilitate mock-making with a uniform syntax.

## Halo Mass and Radius Definitions¶

### Basic equations¶

The AnalyticDensityProf class models a dark matter halo to be a spherically symmetric overdensity relative to some reference density, $$\rho_{\rm ref}(z)$$. The reference density is typically either the critical energy density of the universe, $$\rho_{\rm crit}(z)$$, or the mean matter density $$\rho_{\rm m}(z) = \Omega_{m}(z)\rho_{\rm crit}(z)$$. In the spherical-overdensity definition of a halo, the spherical boundary of a halo is defined such that the region inside the spherical shell has a fixed density

$\rho_{\rm thresh}(z) \equiv \Delta_{\rm ref}(z)\rho_{\rm ref}(z);$

the redshift-dependence of $$\rho_{\rm thresh}$$ reflects both the evolution of the reference density $$\rho_{\rm ref}$$ as well as any possible redshift-dependence in the scalar multiple $$\Delta_{\rm ref}$$.

The cosmological model determines $$\rho_{\rm ref}(z)$$; the choice of a halo mass definition is determined by how one chooses $$\Delta_{\rm ref}(z)$$. Typically, one chooses $$\Delta_{\rm ref}$$ to be some redshift-independent multiple of the reference density. In the conventional notation for this choice, $$\Delta_{\rm ref}(z) = 500c$$ refers to the case where

$\rho_{\rm thresh}(z) = 500\rho_{\rm crit}(z),$

and $$\Delta_{\rm ref}(z) = 200m$$ is shorthand for

$\rho_{\rm thresh}(z) = 200\rho_{\rm m}(z).$

The other common choice for $$\Delta_{\rm ref}(z)$$ is $$\Delta_{\rm vir}(z)$$, which defined by the solution of the gravitational collapse of a top-hat overdensity evolving in an expanding background.

Once a choice is made for $$\Delta_{\rm ref}(z)$$, the mass of a spherically symmetric halo is defined by:

$M_{\Delta}(z) \equiv \frac{4\pi}{3}R_{\Delta}^{3}\Delta_{\rm ref}(z)\rho_{\rm ref}(z)$

This equation defines the relationship between the total mass of a halo $$M_{\Delta}$$ and the halo boundary $$R_{\Delta}$$.

### Computing the relevant quantities¶

In Halotools, the reference densities are computed using the cosmology sub-package of Astropy, and the remaining quantities are computed in the profile_models sub-package, specifically the profile_helpers module.

Quantity Source Code
$$\rho_{\rm thresh}(z)$$ density_threshold
$$\Delta_{\rm vir}(z)$$ delta_vir
$$M_{\Delta}(z)$$ halo_radius_to_halo_mass
$$R_{\Delta}(z)$$ halo_mass_to_halo_radius
$$\rho_{\rm crit}(z)$$ critical_density
$$\Omega_{\rm m}(z)$$ Om

## Spatial Profiles of Halos¶

### Basic equations¶

For a given choice of $$\Delta_{\rm ref}(z)$$, the mass of a spherically symmetric halo is is related to the spatial profile of the matter in its interior via:

$M_{\Delta}(z) \equiv 4\pi\int_{0}^{R_{\Delta}}dr' r'^{2}\rho_{\rm prof}(r')$

This equation defines the normalization of the halo profile $$\rho_{\rm prof}(r)$$, which for any sub-class of AnalyticDensityProf is computed with the mass_density method.

For numerical stability, it is always preferable to work with order-unity quantities rather than astronomical numbers. So throughout the profile_models sub-package, most methods work with the scaled radius, $$\tilde{r}$$, defined as:

$\tilde{r} \equiv r/R_{\Delta},$

and the dimensionless_mass_density, $$\tilde{\rho}_{\rm prof}$$, defined as:

$\tilde{\rho}_{\rm prof}(\tilde{r}) \equiv \rho_{\rm prof}(\tilde{r})/\rho_{\rm thresh}$

In the implementation of AnalyticDensityProf, for reasons of numerical stability a profile is actually defined by $$\tilde{\rho}_{\rm prof}(\tilde{r})$$, and $$\rho_{\rm prof}(r)$$ is a derived quantity.

In fact, in order to define a new profile model, one only need define a sub-class AnalyticDensityProf and provide an implementation of the dimensionless_mass_density method, as all other profile quantities can be computed from this function.

### Convenience functions¶

In addition to the dimensionless_mass_density method that defines the profile, instances of the AnalyticDensityProf class have a number of other useful bound methods:

#### Enclosed mass¶

The mass enclosed within a given radius is defined as:

$M_{\Delta}(<r) \equiv 4\pi\int_{0}^{r}dr' r'^{2}\rho_{\rm prof}(r'),$

which can be computed via the enclosed_mass method of the AnalyticDensityProf class, or any of its sub-classes.

#### Cumulative mass PDF¶

One particularly important quantity in making mocks is $$P_{\rm prof}(<\tilde{r})$$, the cumulative probability of finding a randomly selected particle at a scaled-radius position less than $$\tilde{r}$$:

$P_{\rm prof}(<\tilde{r}) \equiv M_{\Delta}(<\tilde{r}) / M_{\Delta}.$

This function is computed by the cumulative_mass_PDF method of the AnalyticDensityProf class. The $$P_{\rm prof}(<\tilde{r})$$ is used by MonteCarloGalProf to help generate Monte Carlo realizations of halo density profiles.

For reasons of numerical stability, in the Halotools implementation of the enclosed_mass method the quantity $$M_{\Delta}(<r)$$ is computed as $$M_{\Delta}(<r) = P_{\rm prof}(<\tilde{r})M_{\Delta}$$.

#### Virial velocity¶

A halo’s virial velocity $$V_{\rm vir}$$ is defined as:

$V^{2}_{\rm vir} \equiv GM_{\Delta}/R_{\Delta}$

Intuitively, the virial velocity is the speed of a tracer particle on a circular orbit at a distance $$R_{\Delta}$$ from the center of a halo in virial equilibrium. You can compute $$V_{\rm vir}$$ via the virial_velocity method of the AnalyticDensityProf class, or any of its subclasses.

#### Circular velocity profile¶

The circular velocity profile, $$V_{\rm circ}(r)$$, is defined as:

$V^{2}_{\rm circ}(r) \equiv GM_{\Delta}(<r)/r,$

where G is Newton’s constant. Intuitively, $$V_{\rm circ}(r)$$ is the speed of a tracer particle on a bound circular orbit at a distance r from the center of a virialized halo. You can compute $$V_{\rm circ}(r)$$ with the circular_velocity method of the AnalyticDensityProf class, or any of its sub-classes.

For reasons of numerical stability, when computing $$V_{\rm circ}(r)$$ it is useful to use the dimensionless-circular velocity, $$\tilde{V}_{\rm circ}(r)$$, defined as

$\tilde{V}_{\rm circ}(r) \equiv V_{\rm circ}(r) / V_{\rm vir},$

so that $$V_{\rm circ}(r) = \tilde{V}_{\rm circ}(r)V_{\rm vir}$$.

In the actual Halotools implementation $$\tilde{V}_{\rm circ}(r)$$ is computed using

$\tilde{V}^{2}_{\rm circ}(\tilde{r}) = \frac{P_{\rm prof}(<\tilde{r})}{\tilde{r}}$

To see that this alternative method of calculation is correct:

\begin{align}\begin{aligned}\begin{split}\tilde{V}_{\rm circ}(r) \equiv \frac{V_{\rm circ}(r)}{V_{\rm vir}} \\\end{split}\\\begin{split}\tilde{V}_{\rm circ}(r) = \frac{GM_{\Delta}(<r)/r}{GM_{\Delta}/R_{\Delta}} \\\end{split}\\\tilde{V}_{\rm circ}(r) = \frac{M_{\Delta}(<r)/M_{\Delta}}{r/R_{\Delta}},\end{aligned}\end{align}

where the second equality follows from the definition of $$V_{\rm circ}$$. Since the numerator in the final expression is $$P_{\rm prof}(<r)$$ and the denominator is $$\tilde{r}$$, we arrive at

$\tilde{V}^{2}_{\rm circ}(\tilde{r}) = \frac{P_{\rm prof}(<\tilde{r})}{\tilde{r}}$

This is why in the source code for the dimensionless_circular_velocity method, the returned quantity is $$\sqrt{P_{\rm prof}(<\tilde{r})/\tilde{r}}$$. Then the source code for the circular_velocity method simply multiplies the returned value of dimensionless_circular_velocity by $$V_{\rm vir}$$.

#### Maximum circular velocity¶

The maximum circular velocity $$V_{\rm max}$$ is defined as the maximum value attained by $$V_{\rm circ}(r)$$ over the entire profile of the halo. Halotools computes $$V_{\rm max}$$ by using Scipy’s zero-finder minimize. You can compute $$V_{\rm max}$$ using the vmax method of the AnalyticDensityProf class, or any of its sub-classes.

### Computing the relevant quantities¶

Quantity Source Code
$$\rho_{\rm prof}(r)$$ mass_density
$$\tilde{\rho}_{\rm prof}(\tilde{r})$$ dimensionless_mass_density
$$M_{\Delta}(<r)$$ enclosed_mass
$$P_{\rm prof}(<\tilde{r})$$ cumulative_mass_PDF
$$V_{\rm vir}$$ virial_velocity
$$V_{\rm circ}(r)$$ circular_velocity
$$\tilde{V}_{\rm circ}(r)$$ dimensionless_circular_velocity

## Constructor of the AnalyticDensityProf class¶

In this final section of the tutorial, we will look closely at the __init__ constructor to see how it creates a standardized interface for the purpose of making mock catalogs with the Halotools factories.

The AnalyticDensityProf constructor has three required arguments: cosmology, redshift and mdef. Binding these attributes to the instance accomplishes several things:

1. When an instance of an AnalyticDensityProf sub-class is incorporated into a composite model, these attributes will be compared against the corresponding attributes of other component models so that composite model consistency is ensured.
1. A fixed value for the density_threshold attribute can be bound to the instance that is consistent with the returned value of the profile_helpers.density_threshold function.
2. The string mdef is parsed with the model_defaults.get_halo_boundary_key function and the returned value is bound to the halo_boundary_key instance attribute. This guarantees that during mock-making, the appropriate column of the CachedHaloCatalog halo_table will be automatically chosen for all methods of the AnalyticDensityProf sub-class instance requiring this knowledge.
3. Likewise, mdef is parsed with the model_defaults.get_halo_mass_key function and the returned value is bound to the prim_haloprop_key instance attribute. This guarantees that the AnalyticDensityProf sub-class instance will use the halo_table column that is consistent with the input mass definition.

At the conclusion of the constructor, a few empty sequences are bound to the instance. These are documented in the The param_dict mechanism and Explanation of the prof_param_keys mechanism.