Table of contents
Back to the main page Back to the documentation 1. Overview: Utils 2. Timing 3. Units 4. Plotting 5. Math 6. Making Python wrapper 7. KD-tree 8. Potential gradient descent (PGD) 9. Performance cases and counters
Overview: Utils
Here we provide some random stuff that is useful to have.
Timing
We provide a simple interface for timing the code (FML/Timing/Timings.h):
// Set up timer
FML::UTILS::Timings 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 (EndTiming also returns the time in seconds)
t.StartTiming("New label");
// ...
double seconds = t.EndTiming("New label", true);
// ...
// Print all the timings we have made
t.PrintAllTimings();
Units
A simple class (FML/Units/Units.h) 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, temperature and charge of your unit in SI. It also keeps common physical constants and provides these in the units you have chosen.
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 Coulomb
And contains the following physical constants and units:
// Physical constants double k_b; // Boltzmann constant double k_e; // Coulomb 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; // Thomson 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
It's possible to produce plots directly from C++ using matplotlib-cpp which is included in the library (FML/Plotting/Matplotlibcpp.h, compile with USE_PYTHON):
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 (FML/Math/Math.h, namespace FML::MATH):
// Python linspace. Generate a linearly 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(
const std::function<double(int)> &a,
const std::function<double(int)> &b,
double epsilon,
int maxsteps);
// WKB approximation for the hyperspherical 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 (and HaloModel/PythonWrapper). This is really easy with SWIG: one just has to define a simple interface, compile the C++ code as a shared library, run SWIG and that's 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.
KD-tree
KDTree/KDTree.h is a kd-tree for nearest-neighbour, $k$-nearest-neighbour and range searches in any dimension, optionally in a periodic box (minimum-image distances, each point returned once for any radius). The queries are const and can run in parallel; it is used by the kNN and counts-in-cells statistics, the density-split clustering and baryonification.
Potential gradient descent (PGD)
PGD (Dai, Feng & Seljak 2018) sharpens the halos of a cheap PM/COLA simulation by moving the particles towards the minima of a filtered potential, $$\vec S = -\alpha\,{\rm norm}\,\nabla\hat O_l\hat O_s\nabla^{-2}\delta,\qquad \hat O_l = e^{-k_l^2/k^2},\quad \hat O_s = e^{-k^4/k_s^4},$$ with the fitted $\alpha(a)$ (pgd_fitting_formula_alpha), see PGD/PotentialGradientDecent.h.
Performance cases and counters
FML/Performance is a suite of reproducible benchmark cases of representative operations (FFT round trips, grid copies, Fourier resampling, particle migration and polyspectra) with explicit provenance: make -C FML benchmark and python3 FML/Performance/run.py --exe ... --ranks 1 2 4 --threads 1 2 run every case in a fresh process for each rank/thread combination and record JSON reports, raw logs, the build and host information. Opt-in counters (FML_ENABLE_PERFORMANCE_COUNTERS) record FFTs, grid allocations and traced communication payloads alongside wall time and RSS; ordinary builds are unaffected. The reports embed the compiler, flags, library versions, repository revision and a hash of the sources. Python users: see the Python page.