Overview: Utils

Here we provide some random stuff that is useful to have.

Timing

We provide a simple interface for timing the code:

// Set up timer
Timer t;

// Make a timing
t.StartTiming("Label");
// ...
t.EndTiming("Label");

// New timing using same label = time adds up
t.StartTiming("Label");
// ...
t.EndTiming("Label");

// Make a timing and print the result straight away
t.StartTiming("New label");
// ...
t.EndTiming("New label", true);

// ...

// Print all the timings we have made
t.PrintAllTimings();


Units

A simple class for dealing with arbitrary physical units. You can either choose between common units like SI, Planck, ParticlePhysics, Cosmology or define your own units by specifying the length, mass, time and temperature of your unit to SI. It also keep common physical constants and provide these in the units you have choosen.

The class contains 5 numbers defining the units:

double m   = 1.0;  // Size of the unit length in meters
double s   = 1.0;  // Size of the unit time in seconds
double kg  = 1.0;  // Size of the unit mass in kg
double K   = 1.0;  // Size of the unit temperature in Kelvin
double Co  = 1.0;  // Size of the unit charge in Coloumb


And contains the following physical constants and units:

// Physical constants
double k_b;             // Boltzmann constant
double k_e;             // Coloumb constant
double G;               // Gravitational constant
double hbar;            // Plancks constant
double c;               // Speed of light
double m_e;             // Mass of electron
double m_H;             // Mass of hydrogen
double sigma_T;         // Thompson cross section
double lambda_2s1s;     // Decay time 2s to 1s
double epsilon_0;       // Binding energy of hydrogen groundstate
double H0_over_h;       // Hubble constant without h: 100km/s/Mpc
double xhi0;            // Helium ground state
double xhi1;            // Helium first excited state

// Derived units
double yr;              // 1 year
double Gyr;             // 1 gigayear
double mm;              // 1 millimeter
double cm;              // 1 centimeter
double km;              // 1 kilometer
double N;               // 1 Newton
double J;               // 1 Joule
double W;               // 1 Watt
double eV;              // 1 electronvolt
double MeV;             // 1 megaelectronvolt
double Mpc;             // 1 Megaparsec
double kpc;             // 1 kiloparsec
double Gpc;             // 1 Gigaparsec
double Msun;            // 1 Solar mass
double velocity;        // Unit length over unit time
double density;         // Unit mass over unit volume


Example use:

// Standard SI units
auto SI = FML::UTILS::ConstantsAndUnits("SI");
SI.info();

// Electron mass energy me c^2
double emass_in_eV = SI.m_e * SI.c * SI.c / SI.eV;

// Planck units
auto Planck = FML::UTILS::ConstantsAndUnits("Planck");
Planck.info();

// How many Planck lengths in a Mpc
auto planck_length_in_mpc = Planck.Mpc / Planck.m;


Plotting

Its possible to produce plots directly from C++ using matplotlib-cpp which is included in the library:

  namespace plt = FML::UTILS::Matplotlib;

// Call matplotlib
plt::xlim(0.0, 1.0);
plt::plot(x_array, y_array);
plt::title("y = 2 + sin(2 pi x)");

// Save plot to file
plt::save("plot.png");


Math

Some useful math functions:

// Python linspace. Generate a lineary spaced array
DVector linspace(double xmin, double xmax, int num);

// Find roots f(x) = 0
double find_root_bisection(
const std::function<double(double)>& function,
std::pair<double, double> xrange,
double epsilon = 1e-7);

// Airy function (first time called it generates a spline that is used later on)
double Airy_Ai(double z);

// Hyperspherical bessel functions using recursion formula
DVector Hyperspherical_j_ell_array(int lmax, const double nu, const double chi, double K);

// Spherical bessel functions using recursion formula
DVector j_ell_array(int lmax, const double x);

//  Spherical bessel functions from CXX or GSL with fix for very small or large arguments
double j_ell(const int ell, const double arg);

// General method for evaluating continued fraction. Gives back the result and if it converged
std::pair<double,bool> GeneralizedLentzMethod(
std::function<double(int)> &a,
std::function<double(int)> &b,
double epsilon,
int maxsteps);

// WKB approximation for The hyper spherical bessel functions (for a curved Universe)
// For a flat Universe call with nu = 1.0, chi = k*(eta0-eta) and K = 0.0
double HyperSphericalBesselWKB(int ell, double nu, double chi, double K);


Making Python wrapper

If you for some reason want to call the library (or any other C++ code) from Python there is an example of how to do this in the MakePythonWrapper folder. This is really easy with Swig, one just have to define a simple interface, compile the C++ code as a shared library, run Swig and thats it you can now run C++ code and pass the results back to python (or the other way around). This works well with threads, but I have never used it with MPI so have no idea how that would work.