Celeritas  0.5.0-86+4a8eea4
Public Member Functions | List of all members
celeritas::Quantity< UnitT, ValueT > Class Template Reference

A numerical value tagged with a unit. More...

#include <Quantity.hh>

Public Types

Type aliases
using value_type = ValueT
 
using unit_type = UnitT
 

Public Member Functions

constexpr Quantity ()=default
 Construct with default (zero)
 
CELER_CONSTEXPR_FUNCTION Quantity (value_type value) noexcept
 Construct with value in celeritas native units.
 
template<detail::QConstant QC>
CELER_CONSTEXPR_FUNCTION Quantity (detail::UnitlessQuantity< QC >) noexcept
 Construct implicitly from a unitless quantity.
 
CELER_CONSTEXPR_FUNCTION value_type const * data () const
 Access the underlying data for more efficient loading from memory.
 
CELER_CONSTEXPR_FUNCTION value_type & value () &noexcept
 
CELER_CONSTEXPR_FUNCTION value_type const & value () const &noexcept
 

Detailed Description

template<class UnitT, class ValueT = decltype(UnitT::value())>
class celeritas::Quantity< UnitT, ValueT >

A numerical value tagged with a unit.

Template Parameters
UnitTunit tag class
ValueTvalue type

A quantity is a value expressed in terms of the given unit. Storing values in a different unit system can help with some calculations (e.g. operating in natural unit systems) by avoiding numerical multiplications and divisions by large constants. It can also make debugging easier (numeric values are obvious).

Example usage by physics class, where charge is in units of q_e+, and mass and momentum are expressed in atomic natural units (where m_e = 1 and c = 1).

using MevEnergy = Quantity<Mev>;
using MevMass = Quantity<UnitDivide<Mev, CLightSq>>;
using MevMomentum = Quantity<UnitDivide<Mev, CLight>>;

A relativistic equation that operates on these quantities can do so without unnecessary floating point operations involving the speed of light:

real_type eval = value_as<MevEnergy>(energy); // Natural units
MevMomentum momentum{std::sqrt(eval * eval
+ 2 * value_as<MevMass>(mass) * eval)};
double real_type
Numerical type for real numbers.
Definition: corecel/Types.hh:35

The resulting quantity can be converted to the native Celeritas unit system with native_value_from, which multiplies in the constant value of ElMomentumUnit:

real_type mom = native_value_from(momentum);
double native_value_from(UnitSystem sys, ImportUnits q)
Get the native value from a quantity of this type.
Definition: ImportUnits.cc:96

When using a Quantity from another part of the code, e.g. an imported unit system, use the quantity free function rather than .value() in order to guarantee consistency of units between source and destination.

An example unit class would be:

struct DozenUnit
{
static constexpr int value() { return 12; }
static constexpr char const* label() { return "dozen"; }
};
@ value
Ownership of the data, only on host.

The label is used solely for outputting to JSON.

Note
The Quantity is designed to be a simple "strong type" class, not a complex mathematical class. To operate on quantities, you must use value_as (to operate within the Quantity's unit system) or native_value_from (to operate in the Celeritas native unit system), use the resulting numeric values in your mathematical expressions, then return a new Quantity class with the resulting value and correct type.

The documentation for this class was generated from the following file: