Skip to content

Code1:philosophy and structure

ardok-m edited this page Jun 27, 2017 · 6 revisions

Philosophy of the code

This part is based on https://lesgourg.github.io/class-tour/Tokyo2014/lecture1_philosophy.pdf, https://lesgourg.github.io/class-tour/Tokyo2014/lecture2_structure.pdf and https://workshops.ift.uam-csic.es/files/208/hi_class_course_IFT.pdf

The CLASS Commandments

  • Notation from Ma & Bertschinger (astro-ph/9506072)

  • Distinct modules with separate physical tasks: 1. input.c, 2. background.c, 3. thermodynamics.c, 4. perturbations.c, 5. primordial.c, 6. nonlinear.c, 7. transfer.c, 8. spectra.c, 9. lensing.c, 10. output.c

  • Each module associated with a structure xx, containing all what other modules need to know, and nothing else.

  • Documentation + Version history

  • Structure for approximations, always chosen in terms of dimensionless ratios

  • All precision variables grouped in one single place (input.c)

  • No duplicate equations

  • No hard-coding: dynamical indexing (ρ_b->vec[index_bg_rho_b])

  • Component-specific blocks

  • if (has xxx) { (xxx physics) }

  • Easy to add new components:

    • Search for inspirational ingredient
    • Copy, paste & adapt to:
    • interpret parameters (input.c)
    • implement equations (background.c, perturbations.c)

Structure

This part is extracted from https://lesgourg.github.io/class-tour/Tokyo2014/lecture2_structure.pdf

CLASS modules

A CLASS module is:

  • a file include/xxx.h containing some declarations
  • a file source/xxx.c containing some functions each module is a associated with a structure xx
  • each module is a associated with a structure xx, containing all what other modules need to know, and nothing else some fields in this structure are filled in the input.c module
  • some fields in this structure are filled in the input.c module (input parameters relevant for this module)
  • all other fields are filled by a function xxx_init(...)
  • "executing a module” ≡ calling xxx_init(...)

Schematically:

module structure ab. * main content
input.c precision pr ppr all precision parameters
background.c background ba pba background quantities as funct. of conformal time (tau).
thermodynamics.c thermodynamics th pth thermo. quantities as funct. of conformal time (tau).
perturbation.c perturb pt ppt source functions S(k,t)
primordial.c primordial pm ppm primordial spectra P(k)
nonlinear.c nonlinear nl pnl non-linear corrections α_NL(k,tau)
transfer.c tranfers tr ptr transfer functions ∆_l(k)
spectra.c spectra sp psp linear and/or non-linear P(k,z), cl's
lensing.c lensing le ple lensed cl's
output.c output op pop description of output format

Note: "*" stands for "pointer". See that all pointers have the same name as the structure abreviated names with a prefixed "p".

It is important to remark that each module depends on the previous one and needs to be sourced the associated strctures (as pointers); except input.c that needs to be called with all the pointer to structures in order to fill them with the input/default values. main/class.c is a good example.

Another important issue is that CLASS runs as less modules as possible; i.e. if you only request background output, it will only run input.c, background.c and output.c

CLASS modules insight:

Each module contains, in this order:

  • some functions xxx_external_1(...), ..., xxx_external_n(...) that can be called from other modules (e.g. to read correctly or interpolate the content of the structure xx)

  • a function xxx_init(...) filling the structure xx

  • a function xxx_free(...) freeing all the memory all the memory allocated to this structure

  • some functions xxx_internal_1(...), ..., xxx_internal_m(...) that are called only inside the module, within xxx_init(...)