Skip to content

Code1:philosophy and structure

ardok-m edited this page Sep 13, 2017 · 7 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
    • 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, thermodynamics.c and output.c (all these are mandatory). The following ./class output is really informative:

Running CLASS version v2.4.5
Computing background
 -> age = 13.795359 Gyr
 -> conformal age = 14165.045412 Mpc
Computing thermodynamics with Y_He=0.2477
 -> recombination at z = 1089.267451
    corresponding to conformal time = 280.576042 Mpc
    with comoving sound horizon = 144.695940 Mpc
    angular diameter distance = 12.734921 Mpc
    and sound horizon angle 100*theta_s = 1.042142
 -> baryon drag stops at z = 1059.171266
    corresponding to conformal time = 286.488461 Mpc
    with comoving sound horizon rs = 147.376600 Mpc
 -> reionization with optical depth = 0.092473
    corresponding to conformal time = 4255.316282 Mpc
 -> free-streaming approximation can be turned on as soon as tau=364.763 Mpc
No sources requested. Perturbation module skipped.
No perturbations requested. Primordial module skipped.
No non-linear spectra requested. Nonlinear module skipped.
No harmonic space transfer functions to compute. Transfer module skipped.
No spectra requested. Spectra module skipped.
No lensing requested. Lensing module skipped.
No output files requested. Output module skipped.

The ini-file was just:

background_verbose = 10
thermodynamics_verbose = 10
perturbations_verbose = 10
bessels_verbose = 10
transfer_verbose = 10
primordial_verbose = 10
spectra_verbose = 10
nonlinear_verbose = 10
lensing_verbose = 10
output_verbose = 10

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(...)

A remark on units:

CLASS uses the following convention:

  • c = 1.
  • The Planck Mass, M_P = 1/\sqrt{8\piG} = 1.
  • All quantities are in some power of Mpc..
  • The energy densities and pressures are scaled by 1/(3*M_P^2) = 1/3; e.g. rho_CLASS = rho_physical/3. Then H^2 = \sum_i rho_i - K/a^2.

Important remark: in hi_class, for the time being, modified gravity models have K = 0.