TBTK

Need a break? Support the development by playing Polarity Puzzles 
Handles conversion between different units. More...
#include <UnitHandler.h>
Static Public Member Functions  
static double  getConstantInBaseUnits (const std::string &name) 
static double  getConstantInNaturalUnits (const std::string &name) 
static void  setScales (const std::vector< std::string > &scales) 
template<typename Quantity >  
static double  convertNaturalToBase (double value) 
template<typename Quantity >  
static double  convertBaseToNatural (double value) 
static std::string  getUnitString (const std::string &constantName) 
Convert arbitrary to base  
Convert from arbitrary units to base units.  
template<typename Quantity >  
static std::enable_if< Quantity::IsBaseQuantity::value, double >::type  convertArbitraryToBase (double value, typename Quantity::Unit unit) 
template<typename Quantity >  
static std::enable_if< !Quantity::IsBaseQuantity::value, double >::type  convertArbitraryToBase (double value, typename Quantity::Unit unit) 
Convert base to arbitrary  
Convert from base units to arbitrary units.  
template<typename Quantity >  
static std::enable_if< Quantity::IsBaseQuantity::value, double >::type  convertBaseToArbitrary (double value, typename Quantity::Unit unit) 
template<typename Quantity >  
static std::enable_if< !Quantity::IsBaseQuantity::value, double >::type  convertBaseToArbitrary (double value, typename Quantity::Unit unit) 
Convert arbitrary to natural  
Convert from arbitrary units to natural units.  
template<typename Quantity >  
static std::enable_if< Quantity::IsBaseQuantity::value, double >::type  convertArbitraryToNatural (double value, typename Quantity::Unit unit) 
template<typename Quantity >  
static std::enable_if< !Quantity::IsBaseQuantity::value, double >::type  convertArbitraryToNatural (double value, typename Quantity::Unit unit) 
Convert natural to arbitrary  
Convert from natural units to arbitrary units.  
template<typename Quantity >  
static std::enable_if< Quantity::IsBaseQuantity::value, double >::type  convertNaturalToArbitrary (double value, typename Quantity::Unit unit) 
template<typename Quantity >  
static std::enable_if< !Quantity::IsBaseQuantity::value, double >::type  convertNaturalToArbitrary (double value, typename Quantity::Unit unit) 
Get unit string for quantity  
Get the unit string for the given Quantity in the currently set base units.
 
template<typename Quantity >  
static std::enable_if< Quantity::IsBaseQuantity::value, std::string >::type  getUnitString () 
template<typename Quantity >  
static std::enable_if< !Quantity::IsBaseQuantity::value, std::string >::type  getUnitString () 
Friends  
void  Initialize () 
Handles conversion between different units.
TBTK does not enforce the use of a specific set of units. Instead it defines the seven base quantities angle, charge, count, energy, length, temperature, and time. The UnitHandler allows for seven corresponding base and natural units to be set for these quantities.
For example, in the following example the base units are set to radians (rad), Colomb (C), pieces (pcs), milli electron Volt (meV), meter (m), Kelvin (K), and femtosecond (fs).
The natural units are similar to the base units, but also take into account the scale factor in front of the unit symbols. This means that 3 Colomb corresponds to three base units, but only two natural units.
In TBTK, all numbers are interpreted in terms of natural units. This means that TBTK assumes that any number passed to one of its functions is given in natural units. The default natural units are 1 rad, 1 C, 1 pcs, 1 eV, 1 m, 1 K, and 1 s. If another set of natural units is preferred, the call to UnitHandler::setScales() should occur at the start of the program, just after TBTK has been initialized.
TBTK defines a number of base units for the six Base Qauntities. Each can be refered to using its string representation as above, or using an enum class variable. The later is used in functions that are potentially performance critical. For exampel, it is possible to use either Joule (J) or (eV) as base unit for the Base Quantity Energy. The string representations for these are "J" and "eV", while the corresponding enum class representations are Quantity::Energy::Unit::J and Quantity::Energy::Unit::eV. For a complete list of base units, see the documentation for Quantity::Base.
In addition to the base units, TBTK also defines derived units for Derived Quantities such as mass and voltage. The units are called derived since they can be defined in terms of base units. For example, kg ~ eV m^2 s^2 and V = eV/e. Some derived units also have string and enum class representations, such as Quantity::Mass::Unit::kg and "kg" for mass or Quantity::Voltage::Unit::V and "V" for voltage. A full list of derived quantities and their corresponding units, see the documentation for Quantity::Derived.
It is possible to request the value of constants in the currently set base and natural units using
The value in base and natural units will differ by a product of scale factors that accounts for the difference between the two types of units. The currently available constants are
Constant  Symbol  Source* 

Unit charge (positive)  "e"  1 
Speed of light  "c"  1 
Avogadros number  "N_A"  1 
Bohr radius  "a_0"  1 
Planck constant  "h"  1 
Boltzmann constant  "k_B"  1 
Electron mass  "m_e"  2 
Proton mass  "m_p"  2 
Vacuum permeability  "mu_0"  2 
Vacuum permittivity  "epsilon_0"  2 
Reduced Planck constant  "hbar"  3 
Bohr magneton  "mu_B"  3 
Nuclear magneton  "mu_N"  3 
*The sources for the constants are:
Assume that the scales have been set such that the base unit is 1 m and the natural unit is 1 foot (0.3048 m). It is then possible to convert between the two units as follows.
Similar calls can be used to convert between other quantities between base and natural units by replacing Quantity::Length with the corresponding Quantity.
It is also posible to convert between an arbitrary (not currently set) unit and the (currently set) base and natural units. For example, from "nm" to "ft", or "V" to C^1 meV. The functions for this are
The UnitHandler also allows for a string representation of the base unit for constants and Quantities to be extracted. Since natural units can come with an arbitrary numerical factor, unit strings can not be obtained for natural units. This is on of the reasons why conversion to and from base units is of interest. All calculations are done in natural units, but automatic generation of unit strings can only be done for base units. Note, however, that as long as no scale factor is used, the base and natural units are the same.
For example, the unit string for Boltzmann constant can be obtained using
The unit string for voltage can be obtained using

static 
Convert from base units to natural units.

static 
Convert from natural units to base units.

static 
Get physical constant in base units.

static 
Get physical constant in natural units.

static 
Get the unit string for the given constant.
constantName  The name of the constant. 

inlinestatic 
Set scales.

friend 
The Context is a friend of the UnitHandler to allow it to initialize the UnitHandler.