TBTK
Need a break? Support the development by playing Polarity Puzzles
TBTK::Quantity::Quantity< Units, Exponents > Class Template Reference

Base class for Quantitis. More...

#include <Quantity.h>

Inheritance diagram for TBTK::Quantity::Quantity< Units, Exponents >:
TBTK::Real TBTK::PseudoSerializable TBTK::Quantity::Base< Units, Exponents > TBTK::Quantity::Derived< Units, Exponents >

Classes

class  ConversionTable
 

Public Types

using Unit = Units
 
using Exponent = Exponents
 

Public Member Functions

 Quantity ()
 
 Quantity (double value)
 
- Public Member Functions inherited from TBTK::Real
 Real ()
 
constexpr Real (double value)
 
 Real (const std::string &serialization, Serializable::Mode mode)
 
constexpr operator double () const
 
Realoperator= (double rhs)
 
Realoperator+= (const Real &rhs)
 
Realoperator-= (const Real &rhs)
 
Realoperator*= (const Real &rhs)
 
Realoperator/= (const Real &rhs)
 
std::string serialize (Serializable::Mode mode) const
 

Static Public Member Functions

static std::string getUnitString (Unit unit)
 
static Unit getUnit (const std::string &unit)
 
static double getConversionFactor (Unit unit)
 

Static Protected Attributes

static class TBTK::Quantity::Quantity::ConversionTable conversionTable
 

Detailed Description

template<typename Units, typename Exponents>
class TBTK::Quantity::Quantity< Units, Exponents >

Base class for Quantitis.

A Quantity is a Real number that carries compile time information about units. While it is possible to create Quantity object, their main purpose is to act as template parameter for the UnitHandler functions.

Defining new Quantities

Note: The Quantity template is not meant to be instatiated directly. Instead the Base and Derived classes that inherit from Quantity are meant to be instatiated. However, the syntax for instantiating such Quantities is the same as instantiating a Quantity directly. The instantiation procedure is therefore outlined here.

Units and exponents

To instantiate a Quantity, we first need to define a set of units for the Quantity. This is done with the help of an enum class.

enum class VoltageUnit {V, mV};

We also need to define the exponents for the base units Angle, Charge, Count, Energy, Length, Temperature, and Time. In the case of voltage, this is V = J/C, or Energy/Charge.

enum class Voltage{
Angle = 0,
Charge = -1,
Count = 0,
Energy = 1,
Length = 0,
Temperature = 0,
Time = 0
};

Having defined these two enum classes, we can define Voltage using

typedef Quantity<VoltageUnit, VoltageExponent> Voltage;

Note that when instantiating Base and Derived units, Quantity in the line above is replaced by Base and Derived, respectively.

With these definitions, the following symbols that are relevant for UnitHandler calls are defined:

  • Quantity::Voltage
  • Quantity::Voltage::Unit::V
  • Quantity::Voltage::Unit::mV

Creating and initializing a conversion table

We also need to define a table that can be used to convert between string and enum class representations of the units. The conversion table should also contain a conversion factor that specifies the amount in the given unit that corresponds to a single unit of the Quantity in the default base units. The default base units are rad, C, pcs, eV, m, K, and s.

The unit for voltage is V = J/C = 1.602176634e-19. We can therefore define the conversion table as follows.

VoltageUnit,
VoltageExponent
>::ConversionTable Quantity<
VoltageUnit,
VoltageExponent
>::conversionTable({
{Quantity::Voltage::Unit::V, {"V", 1.602176634e-19}},
{Quantity::Voltage::Unit::mV, {"mV", 1.602176634e-19*1e3}},
});

Notes:

  • This definition has to be made in a .cpp-file to avoid multiple definitions.
  • Even though Quantities should be instantiated from the Base and Derived classes, this definition should always contain the keyword Quantity. Note the difference with the typedef above, where Quantity is to be replaced by Base or Derived.
  • It is only possible to initialize the conversion table as is done above if the numerical constants are specified as numbers or using variables defined in the same translation unit (source file). It can be used when defining custom Derived units. However, the Base and Derived Quantities defined in TBTK uses constants from Constants instead of specifying numerical values directly. These values are not available until Constants have been initialized. The conversion table definition and initialization is therefore separated into two parts as described below.

The conversionTable is defined as follows. (Note the empty curly brace, these are necessary to ensure that the conversion table is created.)

VoltageUnit,
VoltageExponent
>::ConversionTable Quantity<
VoltageUnit,
VoltageExponent
>::conversionTable({});

The following code is then put into the function initializeDerivedQuantities() to initialize the conversion table.

Voltage::conversionTable = Voltage::ConversionTable({
{Voltage::Unit::V, {"V", 1.602176634e-19}},
{Voltage::Unit::mV, {"mV", 1.602176634e-19*1e3}},
});

The conversion tables for Base units are similarly initialized in initializeBaseQuantities().

It is not possible to define custom Derived units and initialize them from another function that initializeDerivedQuantities(). Custom defined Derived units therefore either have to be defined using hard coded constants or be defined by extending the Derived.h and Derived.cpp files directly.

Conversion

Unit string to unit

Quantity::Voltage::Unit unit = Quantity::Voltage::getUnit("V");

Unit to unit string

std::string unitString
= Quantity::Voltage::getUnitString(Quantity::Voltage::Unit::V);

Get conversion factor from the default base units to the given unit

double conversionFactor
= Quantity::Voltage::getConversionFactor(Quantity::Voltage::Unit::V);

Constructor & Destructor Documentation

◆ Quantity() [1/2]

template<typename Units , typename Exponents >
TBTK::Quantity::Quantity< Units, Exponents >::Quantity ( )
inline

Default constructor.

◆ Quantity() [2/2]

template<typename Units , typename Exponents >
TBTK::Quantity::Quantity< Units, Exponents >::Quantity ( double  value)
inline

Constructs a Quantity from a double.

Parameters
valueThe value fo the Quantity.

Member Function Documentation

◆ getConversionFactor()

template<typename Units , typename Exponents >
double TBTK::Quantity::Quantity< Units, Exponents >::getConversionFactor ( Unit  unit)
static

Get the conversion factor for converting from the reference unit to the given unit.

Parameters
unitThe unit to get the conversion factor for.
Returns
The conversion factor for converting from the reference unit to the given unit.

◆ getUnit()

template<typename Units , typename Exponents >
Quantity< Units, Exponents >::Unit TBTK::Quantity::Quantity< Units, Exponents >::getUnit ( const std::string &  unit)
static

Convert a string to a Unit.

Parameters
unitA unit string.
Returns
The unit corresponding to the string.

◆ getUnitString()

template<typename Units , typename Exponents >
std::string TBTK::Quantity::Quantity< Units, Exponents >::getUnitString ( Unit  unit)
static

Get unit string for the given Unit.

Parameters
unitThe unit to get the string for.
Returns
String representation of the unit.

The documentation for this class was generated from the following file:
TBTK::Quantity::Quantity::Quantity
Quantity()
Definition: Quantity.h:174