Little Helpers

Units

class units

Class to represent physical unit systems.

This class describes a system of units, by storing base units for time, length and mass, and computes derived units. The unit system in which to specify the new units is not fixed, so that this class may be used to convert between any two systems of units. The unit object can also be used to absolutely specify a unit system. In that case, the convention is that the units are expressed in SI units. There are predefined unit objects which express different geometric unit systems (and SI units) in terms of the SI units.

Public Functions

constexpr units() = default

Default constructor results in SI units.

inline constexpr units(double ulength_, double utime_, double umass_)

Constructor for direct specification of base units.

inline constexpr units operator/(const units &base) const

Express units in terms of other units.

Parameters

base – ..in terms of those units

inline constexpr double length() const

Unit of length.

inline constexpr double time() const

Unit of time.

inline constexpr double freq() const

Unit of frequency.

inline constexpr double mass() const

Unit of mass.

inline constexpr double velocity() const

Unit of velocity.

inline constexpr double accel() const

Unit of acceleration.

inline constexpr double force() const

Unit of force.

inline constexpr double area() const

Unit of area.

inline constexpr double volume() const

Unit of volume.

inline constexpr double density() const

Unit of mass density.

inline constexpr double pressure() const

Unit of pressure.

inline constexpr double mom_inertia() const

Unit for moment of inertia.

Public Static Functions

static inline constexpr units si()

Get SI units.

static inline constexpr units cgs()

Get CGS units.

static inline constexpr units geom_ulength(double ulength, double g_si = G_SI)

Compute units with G=c=1 and given length unit.

Parameters

g_si – Gravitational constant \( G \) in SI units

Returns

units object

static inline constexpr units geom_meter(double g_si = G_SI)

Compute units with G=c=1 where length unit is meter.

Parameters

g_si – Gravitational constant \( G \) in SI units

Returns

units object

static inline units geom_udensity(double udensity, double g_si = G_SI)

Compute units with G=c=1 and given density unit.

Parameters
  • udensity – Target density unit in SI units

  • g_si – Gravitational constant \( G \) in SI units

Returns

units object

static inline constexpr units geom_umass(double umass, double g_si = G_SI)

Compute units with G=c=1 and given mass unit.

Parameters
  • umass – Target mass unit in SI units

  • g_si – Gravitational constant \( G \) in SI units

Returns

units object

static inline constexpr units geom_solar(double m_sun_si = M_sun_SI, double g_si = G_SI)

Compute units with G=c=1 and the mass unit is the solar mass.

Parameters
  • m_sun_si – Solar mass in SI units

  • g_si – Gravitational constant \( G \) in SI units

Returns

units object

Public Static Attributes

static constexpr double PDG2021_c_SI = 299792458.0

Speed of light from particle physics data group (2021)

static constexpr double PDG2021_G_SI = 6.67430E-11

Newtonian graviational constant from particle physics data group (2021)

static constexpr double PDG2021_M_sun_SI = 1.98841E30

Solar mass from particle physics data group (2021)

static constexpr double c_SI = PDG2021_c_SI

Speed of light.

static constexpr double G_SI = PDG2021_G_SI

Default value used for Newtonian graviational constant.

static constexpr double M_sun_SI = PDG2021_M_sun_SI

Default value used for solar mass.

static constexpr double FORMAL_BARYON_MASS_SI = 1.66e-27

Convention for formal value of baryon mass.

This is the proportionality in the definition of the baryonic mass density based on baryon number density. Different sources use different values, this value is the RePrimAnd convention.

Tensors

The library contains classes representing fixed-size tensors (not tensor fields). The functionality is limited mostly to the needs of the primitive recovery algorithm, but might also be useful elsewhere. The objects used by the library itself are:

  • sm_vec3u: a 3-dimensional vector (upper index)

  • sm_vec3l: a 3-dimensional vector (lower index)

  • sm_symt3u: a 3-dimensional rank-2 symmetric tensor (upper index)

  • sm_symt3l: a 3-dimensional rank-2 symmetric tensor (lower index)

  • sm_metric3: a 3-metric containing upper+lower components and determinant

Standard arithmetic operations are supported via operator overloading. Contraction operations between tensors are supported via the multiplication operator. Indices can be raised and lowered via the metric. Consistent use of co- and contra-variant indices is enforced at compile time, e.g. one cannot add an upper index and a lower index vector.

template<class T, int N, bool UP>
class sm_tensor1

Class representing rank-1 tensor.

Template Parameters
  • T – Underlying scalar data type

  • N – Dimensionality

  • UP – If tensor has upper (true) or lower (false) indices

Public Functions

sm_tensor1() noexcept = default

Default constructor leaves elements uninitialized.

sm_tensor1(const me_t &that) noexcept = default

Copy from rank-1 tensor of same dimension and data type.

sm_tensor1 &operator=(const me_t &that) noexcept = default

Assign from rank-1 tensor of same dimension and data type.

template<class T2>
inline explicit sm_tensor1(const sim_t<T2> &that) noexcept

Copy from tensor with compatible data type.

template<class ...An, class Na = detail::fix_num_args<N, An...>>
inline sm_tensor1(An&&... an)

Create from single components.

inline sm_tensor1(zero_literal)

Create zero tensor.

inline T &operator()(int j)

Access i-th component (counting starts at 0).

inline const T &operator()(int j) const

Get i-th component (counting starts at 0)

inline vec_t &as_vector()

Access underlying raw vector.

inline const vec_t &as_vector() const

Get underlying raw vector.

inline void negate()

Replace tensor by its negative.

template<class T2>
inline void operator+=(const sim_t<T2> &a)

Add other compatible tensor.

template<class T2>
inline void operator-=(const sim_t<T2> &a)

Subtract other compatible tensor.

template<class T2>
inline void operator*=(T2 z)

Multiply with scalar.

template<class T2>
inline void operator/=(T2 z)

Divide by scalar.

inline me_t operator+(const me_t &a) const

Compute sum of two tensors.

inline me_t operator-(const me_t &a) const

Compute difference of two tensors.

inline me_t operator-() const

Compute negative of tensor.

inline me_t operator*(T z) const

Compute product of tensor with scalar.

inline me_t operator/(T z) const

Compute tensor divided by scalar.

template<class T, int N, bool UP1, bool UP2>
class sm_tensor2_sym

Class representing symmetric rank-2 tensor.

Template Parameters
  • T – Data type of components

  • N – Dimensionality

  • UP1 – If first index is upper (true) or lower (false)

  • UP2 – If second index is upper (true) or lower (false)

Public Functions

sm_tensor2_sym() noexcept = default

Default contructor leaves components uninitialized.

sm_tensor2_sym(const me_t &that) noexcept = default

Copy from rank-2 tensor of same dimension and data type.

sm_tensor2_sym &operator=(const me_t &that) noexcept = default

Assignment from rank-2 tensor of same dimension and data type.

template<class T2>
inline explicit sm_tensor2_sym(const sim_t<T2> &that)

Copy from rank-2 tensor of same dimension and compatible data type.

template<class ...An, class Na = detail::fix_num_args<FLAT_SIZE, An...>>
inline explicit sm_tensor2_sym(An&&... an)

Construct from single components.

The order is \(00, 10,11, \ldots , m0, \dots , mm \), where \( m=N-1 \).

inline sm_tensor2_sym(zero_literal)

Set to zero.

inline sm_tensor2_sym(one_literal)

Set to Kronecker delta.

inline T &operator()(int i, int j)

Access component \( (i,j) \) (counting starts at 0)

inline const T &operator()(int i, int j) const

Get component \( (i,j) \) (counting starts at 0)

inline mat_t &as_matrix()

Access underlying raw matrix of components.

inline const mat_t &as_matrix() const

Get underlying raw matrix of components.

inline void negate()

Replace tensor by its negative.

template<class T2>
inline void operator+=(const sim_t<T2> &a)

Add other compatible tensor.

template<class T2>
inline void operator-=(const sim_t<T2> &a)

Subtract other compatible tensor.

template<class T2>
inline void operator*=(T2 z)

Multiply with scalar.

template<class T2>
inline void operator/=(T2 z)

Divide by scalar.

inline me_t operator+(const me_t &a) const

Compute sum of two tensors.

inline me_t operator-(const me_t &a) const

Compute difference of two tensors.

inline me_t operator-() const

Compute negative of tensor.

inline me_t operator*(T z) const

Compute product of tensor with scalar.

inline me_t operator/(T z) const

Compute tensor divided by scalar.

inline void diag(const T &d)

Set component matrix to diagonal matrix.

inline T contract(const sm_tensor1<T, N, !UP1> &v, const sm_tensor1<T, N, !UP2> &w) const

Compute contraction with two rank-1 tensors.

Parameters
  • v – Rank-1 tensor to contract first index with

  • w – Rank-1 tensor to contract second index with

inline T quadratic(const sm_tensor1<T, N, !UP1> &v) const

Contract twice with same rank-1 tensor.

template<class T, int N>
class sm_metric

Class representing a metric.

This allows raising, lowering, and contraction of vectors. Consists of upper- and lower-index metric tensors and the metric determinant.

Template Parameters
  • T – Data type of metric components

  • N – Dimensionality

Public Functions

template<class T2>
inline explicit sm_metric(const sm_metric<T2, N> &that)

Copy from metric with compatible component data type.

inline sm_metric(lo_t lo_, up_t up_, T det_)

Construct metric.

Parameters
  • lo_ – Lower-index metric tensor

  • up_ – Upper-index metric tensor

  • det_ – Determinent of lower-index metric tensor

inline explicit sm_metric(lo_t lo_)

Construct metric.

Upper-index metric tensor and determinant will be computed from lower-index metric tensor.

Note

Only implemented for N=2,3

Parameters

lo_ – Lower-index metric tensor

inline void raise(sm_tensor1<T, N, true> &erg, const sm_tensor1<T, N, false> &v) const

Raise index of vector.

Parameters
  • erg – Reference to upper-index vector for storing result

  • v – Lower-index vector to be raised.

inline sm_tensor1<T, N, true> raise(const sm_tensor1<T, N, false> &v) const

Raise index of vector.

Parameters

v – Lower-index vector to be raised.

Returns

Upper-index vector

inline void lower(sm_tensor1<T, N, false> &erg, const sm_tensor1<T, N, true> &v) const

Lower index of vector.

Parameters
  • erg – Reference to lower-index vector for storing result

  • v – Upper-index vector to be lowered.

inline sm_tensor1<T, N, false> lower(const sm_tensor1<T, N, true> &v) const

Lower index of vector.

Parameters

v – Upper-index vector to be lowered.

Returns

Lower-index vector

inline T contract(const sm_tensor1<T, N, true> &v, const sm_tensor1<T, N, true> &w) const

Contract two upper-index vectors with metric.

inline T contract(const sm_tensor1<T, N, false> &v, const sm_tensor1<T, N, false> &w) const

Contract two lower-index vectors with metric.

inline T norm2(const sm_tensor1<T, N, true> &v) const

Contract upper-index vector with itself using metric.

inline T norm2(const sm_tensor1<T, N, false> &v) const

Contract lower-index vector with itself using metric.

template<bool UP>
inline T norm(const sm_tensor1<T, N, UP> &v) const

Compute vector norm defined by metric

inline auto cross_product(const sm_tensor1<T, 3, true> &a, const sm_tensor1<T, 3, true> &b) const -> sm_tensor1<T, 3, false>

Compute cross-product of 3-vectors.

This is ony defined if N=3, i.e. 3-metric.

Returns

\( \epsilon_{ijk} a^j b^k \) where \( \epsilon \) is the Levi-Civita tensor

inline void minkowski()

Set to spatial part of Minkowski metric.

using EOS_Toolkit::sm_vec3u = sm_tensor1<real_t, 3, true>

3-vector with upper indices

using EOS_Toolkit::sm_vec3l = sm_tensor1<real_t, 3, false>

3-vector with lower indices

using EOS_Toolkit::sm_symt3u = sm_tensor2_sym<real_t, 3, true, true>

3-dimensional symmetric rank-2 tensor with upper indices

using EOS_Toolkit::sm_symt3l = sm_tensor2_sym<real_t, 3, false, false>

3-dimensional symmetric rank-2 tensor with lower indices

using EOS_Toolkit::sm_metric3 = sm_metric<real_t, 3>

3-dimensional positive definite metric

Other

template<class T>
class interval

Class representing a closed interval

Template Parameters

T – the underlying numerical type, e.g. double

Public Functions

interval() noexcept = default

Default constructor yields empty range \( [0,0] \).

inline interval(T min, T max)

Construct from minimum and maximum.

Parameters
  • min – Lower boundary of interval

  • max – Upper boundary of interval

Pre

Aborts unless max >= min

Pre

Aborts unless max and min are both finite values

inline bool contains(const T &x) const

Note

Returns false if x is NAN or INF.

Parameters

x – Value to test

Returns

If value is contained in closed interval \([a,b]\).

inline bool contains(const interval<T> &r) const
Parameters

r – Interval to test

Returns

If Interval is contained in closed interval \([a,b]\).

inline T limit_to(const T &x) const

Limit value to interval.

Parameters

x – Value to constrain

Returns

x if it inside interval, else the closest boundary

inline T min() const noexcept
Returns

Lower boundary

inline T max() const noexcept
Returns

Upper boundary

inline T length() const
Returns

Length of interval