python binding of CLASS for large-scale structure calculations
A python binding of the CMB Boltzmann code CLASS, designed for large-scale structure calculations.
The package mainly uses CLASS in order to compute linear power spectra, but the python binding also supports computing nonlinear power spectra and the Cls spectra.
Some of the package features:
The required external Python dependencies are:
And the necessary compilation tools are:
- g++ (>= 4.8, for c++11 support)
- swig (>= 3.0)
Note that swig can be installed using the anaconda package manager:
conda install "swig>=3.0"
The CLASS code will automatically be downloaded and compiled, and is thus, not an external dependency for the user. The version of CLASS compiled by the code is stored in the variable classylss.version.class_version.
The package should be compiled using the GNU compilers for C++ and fortran, g++ and gfortran.
If these are not the default compilers on your system (or if a specific version should be used), they should be
explicitly set via environment variables. Then, the package can be installed via the
# set compilers explicitly, if they are not the default compilers export CXX=g++ export F90=gfortran # install the package pip install classylss
The above procedure has been tested successfully on Mac and Linux machines. However, if installation fails, it is likely due to a failure while compiling either CLASS or the underlying C++ library. In this case, the package should be installed from source and the compilation procedure customized.
The package can be downloaded from github as
git clone https://github.com/nickhand/classylss.git cd classylss
If CLASS is not built succesfully, the user can edit the default configuration variables in depends/class.cfg, which are used when building the CLASS library.
To verify that the installation has succeeded, run:
To compute power spectra for the Planck 2015 cosmology:
from astropy.cosmology import Planck15 from classylss import power import numpy # desired wavenumbers (in h/Mpc) k = numpy.logspace(-3, 0, 500) # desired redshift z = 0 # linear power spectrum in [Mpc/h]^3 Plin = power.linear(k, z, verbose=True, cosmo=Planck15) # nonlinear power spectrum in [Mpc/h]^3 Pnl = power.nonlinear(k, z, verbose=True, cosmo=Planck15) # Zeldovich power spectrum in [Mpc/h]^3 Pzel = power.zeldovich(k, z, verbose=True, cosmo=Planck15)
and similarly, correlation functions can be computed:
from classylss import correlation # desired separation (in Mpc/h) r = numpy.logspace(0, numpy.log10(150), 500) # desired redshift z = 0 # linear 2PCF cf_lin = correlation.linear(r, z, verbose=True, cosmo=Planck15) # nonlinear 2PCF cf_nl = correlation.nonlinear(r, z, verbose=True, cosmo=Planck15) # Zeldovich power spectrum in [Mpc/h]^3 cf_zel = correlation.zeldovich(r, z, smoothing=1.0, verbose=True, cosmo=Planck15)
All of the above functions accept a class_kwargs keyword, which allows the user to pass any valid CLASS parameter to the CLASS code. The class_kwargs parameter is a dictionary that will be passed to the ClassEngine instance, which is responsible for running CLASS.