AlmaBTE  1.3
A solver of the space- and time-dependent Boltzmann transport equation for phonons
utilities.hpp File Reference

Miscellaneous convenience resources. More...

#include <tuple>
#include <vector>
#include <string>
#include <memory>
#include <limits>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <boost/math/common_factor.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/mpi.hpp>
#include <constants.hpp>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <unsupported/Eigen/Splines>
Include dependency graph for utilities.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  alma::Container_comparator< T >
 Comparator function object template for a container of comparable objects. More...
 
class  alma::Min_keeper< T >
 Convenience class for updating a minimum value and keeping track of all the objects associated to it. More...
 

Functions

template<typename T , typename... Args>
std::unique_ptr< T > alma::make_unique (Args &&... args)
 Emulation of C++14's make_unique.
 
template<typename T >
std::tuple< std::vector< typename T::key_type >, std::vector< typename T::mapped_type > > alma::split_keys_and_values (const T &input)
 Put the keys and values of a map (or similar) into separate vectors. More...
 
void alma::flush_istream (std::istream &f)
 Read from f until '
' is found. More...
 
template<typename T >
std::vector< T > alma::tokenize_homogeneous_line (std::string &line, const std::string sep=" \)
 Try to tokenize a line into homogeneous tokens. More...
 
bool alma::starts_with_character (const std::string &line, const std::string &chars)
 Check whether a line starts with a character from a given set. More...
 
bool alma::almost_equal (const double a, const double b, const double abstol=1e-8, const double reltol=1e-5)
 Approximate comparation between doubles. More...
 
double alma::ssqrt (double x)
 "Signed square root" of real numbers, that converts purely imaginary numbers to negative values. More...
 
template<typename T >
int alma::signum (const T &arg)
 Compute the sign of an argument. More...
 
std::string alma::get_timestamp ()
 Build a timestamp in our preferred format. More...
 
std::array< std::size_t, 2 > alma::my_jobs (std::size_t njobs, std::size_t nprocs, std::size_t my_id)
 Compute the minimum and maximum indices of the jobs that must be executed in the current process. More...
 
template<typename T , typename U >
alma::python_mod (const T &n, const U &d)
 Alternative modulus operation with a behavior similar to the % operator in Python. More...
 
double alma::bose_einstein (double omega, double T)
 Bose-Einstein distribution. More...
 
double alma::bose_einstein_kernel (double omega, double T)
 Integration kernel for the specific heat, thermal conductivity and other integrals. More...
 
Eigen::VectorXi alma::reduce_integers (const Eigen::Ref< const Eigen::VectorXi > &vector)
 Divide a vector of integers by the GCD of their coefficients. More...
 
std::string alma::engineer_format (double x, bool insert_space=false)
 Convert a numerical value to a string that uses engineering prefix.
 
Eigen::VectorXd alma::logSpace (double min, double max, int N)
 Construct a vector with N logarithmically spaced values from min to max. More...
 
Eigen::VectorXd alma::splineInterpolation (const Eigen::Ref< const Eigen::VectorXd > &X, const Eigen::Ref< const Eigen::VectorXd > &Y, const Eigen::Ref< const Eigen::VectorXd > &Xtarget)
 Perform spline interpolation of a tabulated function Y(X) at Xtarget. More...
 
Eigen::VectorXd alma::linearInterpolation (const Eigen::Ref< const Eigen::VectorXd > &X, const Eigen::Ref< const Eigen::VectorXd > &Y, const Eigen::Ref< const Eigen::VectorXd > &Xtarget)
 Perform linear interpolation of a tabulated function Y(X) at Xtarget. More...
 
template<class Archive , typename S >
void boost::serialization::save (Archive &ar, const Eigen::Triplet< S > &t, const unsigned int version)
 Allow boost to serialize an Eigen::Triplet.
 
template<class Archive , typename S >
void boost::serialization::load (Archive &ar, Eigen::Triplet< S > &t, const unsigned int version)
 Allow boost to unserialize an Eigen::Triplet.
 
template<class Archive , typename S >
void boost::serialization::serialize (Archive &ar, Eigen::Triplet< S > &t, const unsigned int version)
 Dummy function allowing us to implement save() and load() separately. More...
 

Detailed Description

Miscellaneous convenience resources.

Function Documentation

◆ almost_equal()

bool alma::almost_equal ( const double  a,
const double  b,
const double  abstol = 1e-8,
const double  reltol = 1e-5 
)
inline

Approximate comparation between doubles.

Parameters
[in]a- first value
[in]b- second value
[in]abstol- Maximum absolute tolerance
[in]reltol- Maximum relative tolerance
Returns
- true if the two values are almost equal

◆ bose_einstein()

double alma::bose_einstein ( double  omega,
double  T 
)
inline

Bose-Einstein distribution.

Parameters
[in]omega- angular frequency in rad/ps
[in]T- temperature in K
Returns
the average occupation number at equilibrium

◆ bose_einstein_kernel()

double alma::bose_einstein_kernel ( double  omega,
double  T 
)
inline

Integration kernel for the specific heat, thermal conductivity and other integrals.

This function implements the calculation of (x / sinh(x))^2, where x = hbar * omega / (2 * kB *T).

Parameters
[in]omega- angular frequency in rad/ps
[in]T- temperature in K
Returns
the value of the aforementioned function

◆ flush_istream()

void alma::flush_istream ( std::istream &  f)
inline

Read from f until '
' is found.

This function is meant to be called after reading data from f using >> and before using std::getline() on it.

Parameters
[in]f- istream from which to read

◆ get_timestamp()

std::string alma::get_timestamp ( )
inline

Build a timestamp in our preferred format.

Returns
a string containing the current time.

◆ linearInterpolation()

Eigen::VectorXd alma::linearInterpolation ( const Eigen::Ref< const Eigen::VectorXd > &  X,
const Eigen::Ref< const Eigen::VectorXd > &  Y,
const Eigen::Ref< const Eigen::VectorXd > &  Xtarget 
)
inline

Perform linear interpolation of a tabulated function Y(X) at Xtarget.

X must be strictly ascending (no duplicate entries).

◆ logSpace()

Eigen::VectorXd alma::logSpace ( double  min,
double  max,
int  N 
)
inline

Construct a vector with N logarithmically spaced values from min to max.

◆ my_jobs()

std::array<std::size_t, 2> alma::my_jobs ( std::size_t  njobs,
std::size_t  nprocs,
std::size_t  my_id 
)
inline

Compute the minimum and maximum indices of the jobs that must be executed in the current process.

Split njobs jobs among nproc processes and return the minimum and maximum indices of the jobs that must be assigned to the current process, bundled in an array. More specifically, return jmin and jmax so that we can write a for loop like for (auto j = jmin; j < jmax; ++j)

Parameters
[in]njobs- number of jobs
[in]nprocs- number of process
[in]my_id- id of the current process

◆ python_mod()

template<typename T , typename U >
U alma::python_mod ( const T &  n,
const U &  d 
)
inline

Alternative modulus operation with a behavior similar to the % operator in Python.

Parameters
[in]n- numerator
[in]d- denominator
Returns
the remainder of the integer division n / d with the same sign as d.

◆ reduce_integers()

Eigen::VectorXi alma::reduce_integers ( const Eigen::Ref< const Eigen::VectorXi > &  vector)
inline

Divide a vector of integers by the GCD of their coefficients.

Parameters
[in]vector- the original vector
Returns
the reduced vector

◆ serialize()

template<class Archive , typename S >
void boost::serialization::serialize ( Archive &  ar,
Eigen::Triplet< S > &  t,
const unsigned int  version 
)

Dummy function allowing us to implement save() and load() separately.

◆ signum()

template<typename T >
int alma::signum ( const T &  arg)
inline

Compute the sign of an argument.

Parameters
[in]arg- anything that can be constructed passing 0 as the only argument and that supports operator -
Returns
-1, 0 or 1 depending on the sign of arg

◆ splineInterpolation()

Eigen::VectorXd alma::splineInterpolation ( const Eigen::Ref< const Eigen::VectorXd > &  X,
const Eigen::Ref< const Eigen::VectorXd > &  Y,
const Eigen::Ref< const Eigen::VectorXd > &  Xtarget 
)
inline

Perform spline interpolation of a tabulated function Y(X) at Xtarget.

◆ split_keys_and_values()

template<typename T >
std::tuple<std::vector<typename T::key_type>, std::vector<typename T::mapped_type> > alma::split_keys_and_values ( const T &  input)

Put the keys and values of a map (or similar) into separate vectors.

◆ ssqrt()

double alma::ssqrt ( double  x)
inline

"Signed square root" of real numbers, that converts purely imaginary numbers to negative values.

Parameters
[in]x- argument of the square root
Returns
a real number with the absolute value of sqrt(|x|) and the sign of x

◆ starts_with_character()

bool alma::starts_with_character ( const std::string &  line,
const std::string &  chars 
)
inline

Check whether a line starts with a character from a given set.

Parameters
[in]line- string containing the line
[in]chars- string containing the set of characters
Returns
true if line starts with a character from chars

◆ tokenize_homogeneous_line()

template<typename T >
std::vector<T> alma::tokenize_homogeneous_line ( std::string &  line,
const std::string  sep = " \t" 
)
inline

Try to tokenize a line into homogeneous tokens.

This function tries to split a line into fields that can all be cast into the same type and return a vector of tokens. boost::bad_lexical_cast will be thrown in case of failure.

Parameters
[in]line- line of text to tokenize
[in]sep- field separators
Returns
a vector of tokens