Skip to content

classy:classy.pyx_cclassy.pxd

ardok-m edited this page Jun 30, 2017 · 2 revisions

classy: classy.pyx and cclassy.pxd

As you already know, classy is the python wrapper of CLASS. This means it serves as interface between python and C codes. In order to acomplish that, classy is written using cython. Cython's source files have .pyx extension and the C-header equivalent files are the .pxd files. In them cython declarations are stored.

After this brief introduction, we can dive into classy.pyx.

classy.pyx

First thing to remark is the definition of the two Error classes:

  • CosmoSevereError which will be used if init_input() fails.
  • CosmoComputationError if the failure comes from any other computational stage.
    This distinction is made to work with MontePython. In case first error were raised, MontePython would halt the execution but, in the other case, it would continue running having assigned a low proability value to the specific model that caused the error.

Secondly, the userspace class is Class, that have the following methods defined:

  • set(). It updates the parameters passed to input_init().
  • compute(). Computes the cosmology (calls all xxx_init() until specified computational level is arrived (default is lensing)) if it fails at some stage, reaise the corresponding error and call struct_cleanup(); elsewise set ready = True.
  • struct_cleanup(). Call xxx_free() for the computed levels if ready is not False (freeing a non-allocated memory causes a segementation_fault).
  • empty(). Remove set parameters and set ready = False.
  • get_curent_derived_parameters. Return the computed value of the parameters given as input (e.g. ['h', '100*theta_s', ...]). This method is used by MontePython to obtain the value of the 'derived' parameters. If you want MontePython to understand a new parameter (e.g. w0_smg()), you must add it to this method.
  • get_xxx(). Returns a dictionary with the computed tables in the xxx structure, where the keywords are the column titles.
  • A buch of methods to return different physical values. To see them all look in classy.pyx.

cclassy.pxd

As we said classy.pxd is the Cython header file with all cython specific declarations used in classy.pyx. We will remark just a few things:

  • cdef extern from "class.h": tells cython that all its member definitions are found in class.h.
  • In its block we declare those C-objects we need (structures, functions e.g. as xxx_init(), constants, ...).
  • In a lower level we declare their subobjects if any; for example, the structure keywords.
[...]

cdef extern from "class.h":

    ctypedef char FileArg[40]

    [...]

    cdef enum file_format:
         class_format
         camb_format

    [...]

    cdef struct background:
        ErrorMsg error_message
        int bg_size
        int index_bg_ang_distance

    [...]

    void lensing_free(void*)
    void spectra_free(void*)

    [...]

    cdef int _FAILURE_
    cdef int _FALSE_
    cdef int _TRUE_

    [...]

    int nonlinear_k_nl_at_z(void* pba, void* pnl, double z, double* k_nl)

    int spectra_firstline_and_ic_suffix(void *ppt, int index_ic, char first_line[_LINE_LENGTH_MAX_], FileName ic_suffix)