Symbolic manipulation

This module defines the scalar linear perturbation equations for standard LCDM cosmology, using sympy. It uses the covariant perturbation notation, but includes functions to project into the Newtonian or synchronous gauge, as well as constructing general gauge invariant quantities. It uses “t” as the conformal time variable (=tau in the fortran code).

For a guide to usage and content see the ScalEqs notebook

As well as defining standard quantities, and how they map to CAMB variables, there are also functions for converting a symbolic expression to CAMB source code, and compiling custom sources for use with CAMB (as used by model.CAMBparams.set_custom_scalar_sources(), results.CAMBdata.get_time_evolution())

A Lewis July 2017

camb.symbolic.LinearPerturbation(name, species=None, camb_var=None, camb_sub=None, frame_dependence=None, description=None)[source]

Returns as linear perturbation variable, a function of conformal time t. Use help(x) to quickly view all quantities defined for the result.

  • name – sympy name for the Function

  • species – tag for the species if relevant (not used)

  • camb_var – relevant CAMB fortran variable

  • camb_sub – if not equal to camb_var, and string giving the expression in CAMB variables

  • frame_dependence – the change in the perturbation when the frame 4-velocity u change from u to u + delta_frame. Should be a numpy expression involving delta_frame.

  • description – string describing variable


sympy Function instance (function of t), with attributes set to the arguments above.

camb.symbolic.camb_fortran(expr, name='camb_function', frame='CDM', expand=False)[source]

Convert symbolic expression to CAMB fortran code, using CAMB variable notation. This is not completely general, but it will handle conversion of Newtonian gauge variables like Psi_N, and most derivatives up to second order.

  • expr – symbolic sympy expression using camb.symbolic variables and functions (plus any standard general functions that CAMB can convert to fortran).

  • name – lhs variable string to assign result to

  • frame – frame in which to interpret non gauge-invariant expressions. By default, uses CDM frame (synchronous gauge), as used natively by CAMB.

  • expand – do a sympy expand before generating code


fortran code snippet


Evaluates an expression in the CDM frame \((v_c=0, A=0)\). Equivalent to the synchronous gauge but using the covariant variable names.


x – expression


expression evaluated in CDM frame.

camb.symbolic.compile_source_function_code(code_body, file_path='', compiler=None, fflags=None, cache=True)[source]

Compile fortran code into function pointer in compiled shared library. The function is not intended to be called from python, but for passing back to compiled CAMB.

  • code_body – fortran code to do calculation and assign sources(i) output array. Can start with declarations of temporary variables if needed.

  • file_path – optional output path for generated f90 code

  • compiler – compiler, usually on path

  • fflags – options for compiler

  • cache – whether to cache the result


function pointer for compiled code

class camb.symbolic.f_K(*args)

Get Boltzmann hierarchies up to lmax for photons (J), E polarization and massless neutrinos (G).


lmax – maximum multipole


list of equations


Derives terms in line of sight source, after integration by parts so that only integrated against a Bessel function (no derivatives).


checks – True to do consistency checks on result


monopole_source, ISW, doppler, quadrupole_source

camb.symbolic.make_frame_invariant(expr, frame='CDM')[source]

Makes the quantity gauge invariant, assuming currently evaluated in frame ‘frame’. frame can either be a string frame name, or a variable that is zero in the current frame,

e.g. frame = Delta_g gives the constant photon density frame. So make_frame_invariant(sigma, frame=Delta_g) will return the combination of sigma and Delta_g that is frame invariant (and equal to just sigma when Delta_g=0).


Evaluates an expression in the Newtonian gauge (zero shear, sigma=0). Converts to using conventional metric perturbation variables for metric

\[ds^2 = a^2\left( (1+2\Psi_N)d t^2 - (1-2\Phi_N)\delta_{ij}dx^idx^j\right)\]

x – expression


expression evaluated in the Newtonian gauge


evaluates an expression in the synchronous gauge, using conventional synchronous-gauge variables.


x – expression


synchronous gauge variable expression