Advanced Multi-Physics (AMP)
On-Line Documentation
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
AMP::Units Class Referencefinal

Provides a class for storing units. More...

#include <Units.h>

Public Types

using SI_type = std::array< int8_t, 9 >
 
using unit_type = std::array< char, 31 >
 

Public Member Functions

constexpr bool compatible (const Units &unit) noexcept
 Check if two units are compatible.
 
constexpr double convert (const Units &unit) const
 Convert the unit to a new type.
 
constexpr UnitType getType () const noexcept
 Get the unit type.
 
constexpr bool isNull () const
 Check if unit is null.
 
constexpr bool operator!= (const Units &rhs) const noexcept
 Operator !=.
 
constexpr void operator*= (const Units &rhs) noexcept
 Operator *=.
 
constexpr void operator*= (double x) noexcept
 Operator *=.
 
constexpr void operator/= (const Units &rhs) noexcept
 Operator /=.
 
constexpr bool operator== (const Units &rhs) const noexcept
 Operator ==.
 
constexpr Units pow (int p) const noexcept
 Raise the unit to the given power.
 
std::string printFull () const
 Get the full unit and conversion string.
 
std::string printSI () const
 Get a string representation of the units in SI units (with scaling factor)
 
std::string printUnit () const
 Get a string representation of the units in SI units (with scaling factor)
 
std::string str () const
 Get a string representation of the units.
 
constexpr Units ()=default
 Empty constructor.
 
constexpr Units (const char *unit)
 Construct the units from a const char array.
 
constexpr Units (const SI_type &unit, double scale=1.0)
 Construct the units.
 
constexpr Units (const std::string_view &unit)
 Construct the units from a const char array.
 
constexpr Units (const std::string_view &unit, double value)
 Construct the units from a const char array.
 
constexpr Units (const UnitType &unit, double scale=1.0)
 Construct the units.
 

Static Public Member Functions

static constexpr int atoi (std::string_view, bool=true)
 
static constexpr double convert (UnitPrefix x) noexcept
 Convert the prefix to a double.
 
static std::vector< std::string > getAllPrefixes ()
 Get all supported unit prefixes.
 
static std::vector< std::string > getAllUnits ()
 Get all supported units.
 
static constexpr std::string_view getPrefixStr (UnitPrefix) noexcept
 Get a string representation for the prefix.
 
static constexpr UnitPrefix getUnitPrefix (const std::string_view &str) noexcept
 Get the prefix from a string.
 
static constexpr double strtod (std::string_view)
 

Protected Member Functions

std::string printSIBase () const
 

Static Protected Member Functions

static constexpr SI_type combine (const SI_type &a, const SI_type &b)
 
static constexpr size_t findPar (const std::string_view &, size_t)
 
static constexpr std::pair< size_t, char > findToken (const std::string_view &, size_t)
 
static constexpr SI_type getSI (UnitType)
 
static constexpr Units read (std::string_view str)
 
static constexpr Units read2 (std::string_view str)
 
static constexpr Units readUnit (const std::string_view &str, bool throwErr=true)
 

Protected Attributes

double d_scale = { 0.0 }
 
SI_type d_SI = { 0 }
 
unit_type d_unit = { 0 }
 

Friends

constexpr Units pow (Units base, int exponent)
 

Detailed Description

Provides a class for storing units.

This class provides routines for creating and storing units.
A user can specify a unit and get the scaling factor to a compatible unit.
All user-provided units are converted to SI base units with a scaling factor.
Currently we support all major SI units and some additional units with prefixes.
Note: currently the majority of the routines are constexpr so that the creation
   of a unit can be done at compile-time.
Note: To prevent ambiguity some unit abbreviations are not supported:
   min (minute) - ambiguous with min (milli-inch)
Note: To prevent ambiguity some units are not supported:
   year: could be 365 days, 365.25 days, 365.2425 days
   horsepower:
       Mechanical horsepower - 745.6998715822702 W
       Metric horsepower - 735.49875 W
       Electric horsepower - 746 W
       Boiler horsepower - 9,812.5 W
       Hydraulic horsepower - 745.69987 W
       Air horsepower - 745.69987 W

Definition at line 100 of file Units.h.

Member Typedef Documentation

◆ SI_type

using AMP::Units::SI_type = std::array<int8_t, 9>

Definition at line 104 of file Units.h.

◆ unit_type

using AMP::Units::unit_type = std::array<char, 31>

Definition at line 103 of file Units.h.

Constructor & Destructor Documentation

◆ Units() [1/6]

constexpr AMP::Units::Units ( )
constexprdefault

Empty constructor.

◆ Units() [2/6]

constexpr AMP::Units::Units ( const char *  unit)
constexpr

Construct the units from a const char array.

This is the default constructor for a string view. It can create a unit from a string of the format "W/(m^2)"

Parameters
unitInput string

◆ Units() [3/6]

constexpr AMP::Units::Units ( const std::string_view &  unit)
constexpr

Construct the units from a const char array.

This is the default constructor for a string view. It can create a unit from a string of the format "W/(m^2)"

Parameters
unitInput string

◆ Units() [4/6]

constexpr AMP::Units::Units ( const std::string_view &  unit,
double  value 
)
constexpr

Construct the units from a const char array.

This is the default constructor for a string view. It can create a unit from a string of the format "W/(m^2)"

Parameters
unitInput string
valueValue of constant scalar

◆ Units() [5/6]

constexpr AMP::Units::Units ( const SI_type unit,
double  scale = 1.0 
)
explicitconstexpr

Construct the units.

Construct units directly by specifying the SI units and scale

Parameters
unitInput string
scaleValue of constant scalar

◆ Units() [6/6]

constexpr AMP::Units::Units ( const UnitType unit,
double  scale = 1.0 
)
explicitconstexpr

Construct the units.

Construct units directly by specifying the SI units and scale

Parameters
unitInput string
scaleValue of constant scalar

Member Function Documentation

◆ atoi()

static constexpr int AMP::Units::atoi ( std::string_view  ,
bool  = true 
)
staticconstexpr

◆ combine()

static constexpr SI_type AMP::Units::combine ( const SI_type a,
const SI_type b 
)
staticconstexprprotected

◆ compatible()

constexpr bool AMP::Units::compatible ( const Units unit)
constexprnoexcept

Check if two units are compatible.

Check if two units are compatible with each other. I.e. if convert will succeed.

Parameters
unitUnit to compare

◆ convert() [1/2]

constexpr double AMP::Units::convert ( const Units unit) const
constexpr

Convert the unit to a new type.

This function returns the scaling factor to convert between two different units. For example, converting "J" to "ergs" returns 1e7. If the two units are not compatible an exception will be thrown.

Parameters
unitDesired unit

Referenced by AMP::Materials::Property::set_default().

◆ convert() [2/2]

static constexpr double AMP::Units::convert ( UnitPrefix  x)
staticconstexprnoexcept

Convert the prefix to a double.

This returns the value of the given prefix

Parameters
xEnum for the prefix

◆ findPar()

static constexpr size_t AMP::Units::findPar ( const std::string_view &  ,
size_t   
)
staticconstexprprotected

◆ findToken()

static constexpr std::pair< size_t, char > AMP::Units::findToken ( const std::string_view &  ,
size_t   
)
staticconstexprprotected

◆ getAllPrefixes()

static std::vector< std::string > AMP::Units::getAllPrefixes ( )
inlinestatic

Get all supported unit prefixes.

◆ getAllUnits()

static std::vector< std::string > AMP::Units::getAllUnits ( )
inlinestatic

Get all supported units.

◆ getPrefixStr()

static constexpr std::string_view AMP::Units::getPrefixStr ( UnitPrefix  )
staticconstexprnoexcept

Get a string representation for the prefix.

◆ getSI()

static constexpr SI_type AMP::Units::getSI ( UnitType  )
staticconstexprprotected

◆ getType()

constexpr UnitType AMP::Units::getType ( ) const
constexprnoexcept

Get the unit type.

This returns the type of unit (if it is reducible). e.g. time, length, energy, power, etc.

◆ getUnitPrefix()

static constexpr UnitPrefix AMP::Units::getUnitPrefix ( const std::string_view &  str)
staticconstexprnoexcept

Get the prefix from a string.

This returns an enum representing the given input string

Parameters
strInput string

◆ isNull()

constexpr bool AMP::Units::isNull ( ) const
inlineconstexpr

Check if unit is null.

Definition at line 208 of file Units.h.

References d_scale.

Referenced by AMP::Materials::Property::set_default().

◆ operator!=()

constexpr bool AMP::Units::operator!= ( const Units rhs) const
inlineconstexprnoexcept

Operator !=.

Definition at line 193 of file Units.h.

References operator==().

◆ operator*=() [1/2]

constexpr void AMP::Units::operator*= ( const Units rhs)
constexprnoexcept

Operator *=.

◆ operator*=() [2/2]

constexpr void AMP::Units::operator*= ( double  x)
inlineconstexprnoexcept

Operator *=.

Definition at line 199 of file Units.h.

References d_scale.

◆ operator/=()

constexpr void AMP::Units::operator/= ( const Units rhs)
constexprnoexcept

Operator /=.

◆ operator==()

constexpr bool AMP::Units::operator== ( const Units rhs) const
constexprnoexcept

Operator ==.

Referenced by operator!=().

◆ pow()

constexpr Units AMP::Units::pow ( int  p) const
constexprnoexcept

Raise the unit to the given power.

◆ printFull()

std::string AMP::Units::printFull ( ) const

Get the full unit and conversion string.

◆ printSI()

std::string AMP::Units::printSI ( ) const

Get a string representation of the units in SI units (with scaling factor)

◆ printSIBase()

std::string AMP::Units::printSIBase ( ) const
protected

◆ printUnit()

std::string AMP::Units::printUnit ( ) const

Get a string representation of the units in SI units (with scaling factor)

◆ read()

static constexpr Units AMP::Units::read ( std::string_view  str)
staticconstexprprotected

◆ read2()

static constexpr Units AMP::Units::read2 ( std::string_view  str)
staticconstexprprotected

◆ readUnit()

static constexpr Units AMP::Units::readUnit ( const std::string_view &  str,
bool  throwErr = true 
)
staticconstexprprotected

◆ str()

std::string AMP::Units::str ( ) const

Get a string representation of the units.

Referenced by AMP::operator<<().

◆ strtod()

static constexpr double AMP::Units::strtod ( std::string_view  )
staticconstexpr

Friends And Related Symbol Documentation

◆ pow

constexpr Units pow ( Units  base,
int  exponent 
)
friend

Member Data Documentation

◆ d_scale

double AMP::Units::d_scale = { 0.0 }
protected

Definition at line 245 of file Units.h.

Referenced by isNull(), and operator*=().

◆ d_SI

SI_type AMP::Units::d_SI = { 0 }
protected

Definition at line 244 of file Units.h.

◆ d_unit

unit_type AMP::Units::d_unit = { 0 }
protected

Definition at line 243 of file Units.h.


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



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:42.
Comments on this page