https://github.com/steven-murray/pycamb
Raw File
Tip revision: 6db4aea707d6594d166ced8ce1202245b0bc53f7 authored by Steven Murray on 16 November 2015, 02:44:22 UTC
more formatting
Tip revision: 6db4aea
README.rst
This code is a python scripts that generates python bindings for the main functions of CAMB,
the Code for Anisotropies in the Microwave Background.  See http://camb.info for more details on CAMB.

The python language can let you more flexibly and easily vary the parameters of CAMB, exposed by this code.
You can easily modify which variables are accessible.
For example, if you have modified CAMB to add a new parameter, you can easily add it to pycamb.



Installation
--------
The simplest way is to use ``pip``: ``CAMBURL=<url.of.camb.download> pip install git+git://github.com/steven-murray/pycamb.git``.

The url at which to download CAMB can be gotten by filling out a form at http://camb.info.

**NOTE: this version of pycamb is only known to work up to the March 2013 version
of ``camb``. Please use that version until further notice.**

Check if the installation has worked correctly using:
> python
>>> import pycamb

If no error messages are reported, pycamb built correctly (there will be several
warning messages).


Modifying
---------
There is no point modifying either pycamb.py or py_camb_wrap.f90 directly; they
are generated by generatePyCamb.py and any changes will be lost.

To simply include new parameters that you have already added to camb, read the
top of generatePyCamb.py, there are instructions there.

Other modifications will be a little more difficult, as the code is rather messy.
Basically you need to write a piece of fortran code for python to output, and
the a piece of python code for python to output, and add both to the generating
code at the foot of the file.  Also, add your new python functions to the ``__all__``
variable generated in the python header.


Usage
-----
The functions currently available in the code are::

    pycamb.camb             #Get CMB power spectra; returns T,E,B,X
    pycamb.matter_power     #Get P(k); returns k,P(k)
    pycamb.transfers        #Get transfer functions; returns k,T_i(k,z), sigma8(z)
    pycamb.age              #Get age of universe at z=0
    pycamb.angular_diameter #Get angular diameter distance to given redshift

All of them are called in a similar way; read the documentation string for
``pycamb.camb`` to find out more, using::
    import pycamb
    help(pycamb.camb)

The two methods are explicitly passing the parameters when calling the functions,
and constructing a dictionary to pass, e.g.:

**method 1**::

    import pycamb
    T,E,B,X = pycamb.camb(2000,H0=72, scalar_index=0.95)

**method 2**::

    params = {"H0":72.0, scalar_index:0.95}
    T,E,B,X = pycamb.camb(2000,**params)


Interactive Mode
================
You can use this module either interactively or in a script.  If in interactive
mode you probably want to install matplotlib (pylab) to plot your results.
The ipython module is also very useful for making interactive mode easier -
it has a pylab mode where mathematical and plotting tools are already imported.

To start most simply in interactive mode, run python at the command line and import everything from the module::

    from pylab import *
    from pycamb import *
    lmax=2000
    T,E,B,X = camb(lmax, H0=70., omegab=0.04,omegav=0.6,omegac=0.36)
    ell = arange(1,lmax)
    loglog(ell,T,label="TT")
    loglog(ell,E, label="EE")
    legend()
    show() #May not be necessary


Scripts
=======
You can put more complex sets of commands in a script ending in ``.py``, e.g. ``plot.py``::

    import pycamb
    import pylab
    lmax=2000
    ns_values = [0.8,0.9,1.0,1.1]
    ell = pylab.arange(1,lmax)
    for ns in ns_values:
        T,E,B,X = pycamb.camb(lmax, scalar_index=ns)
        pylab.semilogx(ell,T,label="%.2f"%ns)
    pylab.legend()
    pylab.xlabel("$\ell$", fontsize=20)
    pylab.ylabel("$\ell (\ell+1) C_\ell / 2\pi \quad [\mu K^2]$", fontsize=20)
    pylab.title("Varying Spectral Index $n_s$")
    pylab.xlim(1,2000)
    pylab.savefig("spectral_index.eps")

Then run with ``python plot.py``.


Bugs & Issues
-------------
Anything error which causes CAMB to exit at a "STOP" statement will also cause
python to exit.  In particular, setting lmax too low does this.

If you get any weird errors in your scripts about indentation, check whether you
have mixed tabs and spaces somewhere.


Joe Zuntz
jaz@astro.ox.ac.uk
September 2010
back to top