Celeritas 0.6.0-dev.115+3b60a5fd
Loading...
Searching...
No Matches
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
 
using unit_value_type = decltype(UnitT::value())
 
using common_type = decltype(std::declval< value_type >() *std::declval< unit_value_type >())
 

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 constdata () const
 Access the underlying data for more efficient loading from memory.
 
CELER_CONSTEXPR_FUNCTION value_type & value () &noexcept
 
CELER_CONSTEXPR_FUNCTION value_type constvalue () const &noexcept
 

Detailed Description

template<class UnitT, class ValueT>
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, real_type>;
A numerical value tagged with a unit.
Definition Quantity.hh:80

Note the use of the RealQuantity type alias (below).

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

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

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:95

When using a Quantity from another part of the code, e.g. an imported unit system, use the value_as 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
The collection owns the data.

The label is used solely for diagnostic purposes.

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.

Member Function Documentation

◆ value() [1/2]

template<class UnitT , class ValueT >
CELER_CONSTEXPR_FUNCTION value_type & celeritas::Quantity< UnitT, ValueT >::value ( ) &
inlinenoexcept

Access the underlying numeric value, discarding units

◆ value() [2/2]

template<class UnitT , class ValueT >
CELER_CONSTEXPR_FUNCTION value_type const & celeritas::Quantity< UnitT, ValueT >::value ( ) const &
inlinenoexcept

Access the underlying numeric value, discarding units


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