Advanced Multi-Physics (AMP)
On-Line Documentation
Units.h
Go to the documentation of this file.
1#ifndef included_AMP_Units
2#define included_AMP_Units
3
4#include <array>
5#include <math.h>
6#include <string>
7#include <string_view>
8#include <utility>
9#include <vector>
10
11
12namespace AMP {
13
15enum class UnitPrefix : int8_t {
16 quecto = 0,
17 ronto = 1,
18 yocto = 2,
19 zepto = 3,
20 atto = 4,
21 femto = 5,
22 pico = 6,
23 nano = 7,
24 micro = 8,
25 milli = 9,
26 centi = 10,
27 deci = 11,
28 none = 12,
29 deca = 13,
30 hecto = 14,
31 kilo = 15,
32 mega = 16,
33 giga = 17,
34 tera = 18,
35 peta = 19,
36 exa = 20,
37 zetta = 21,
38 yotta = 22,
39 ronna = 23,
40 quetta = 24,
41 unknown = -1
42};
43
44
46enum class UnitType : int8_t {
47 unitless = 0,
48 time = 1,
49 length = 2,
50 mass = 3,
51 current = 4,
52 temperature = 5,
53 mole = 6,
54 intensity = 7,
55 angle = 8,
56 solidAngle = 9,
57 energy = 10,
58 power = 11,
59 frequency = 12,
60 force = 13,
61 pressure = 14,
62 electricCharge = 15,
64 capacitance = 17,
65 resistance = 18,
67 magneticFlux = 20,
69 inductance = 22,
70 luminousFlux = 23,
71 illuminance = 24,
72 unknown = -1
73};
74
75
100class alignas( 8 ) Units final
101{
102public:
103 using unit_type = std::array<char, 31>;
104 using SI_type = std::array<int8_t, 9>;
105
106public:
108 constexpr Units() = default;
109
116 constexpr Units( const char *unit );
117
124 constexpr Units( const std::string_view &unit );
125
133 constexpr Units( const std::string_view &unit, double value );
134
141 explicit constexpr Units( const SI_type &unit, double scale = 1.0 );
142
149 explicit constexpr Units( const UnitType &unit, double scale = 1.0 );
150
156 constexpr UnitType getType() const noexcept;
157
163 constexpr static UnitPrefix getUnitPrefix( const std::string_view &str ) noexcept;
164
171 constexpr bool compatible( const Units &unit ) noexcept;
172
180 constexpr double convert( const Units &unit ) const;
181
187 constexpr static double convert( UnitPrefix x ) noexcept;
188
190 constexpr bool operator==( const Units &rhs ) const noexcept;
191
193 constexpr bool operator!=( const Units &rhs ) const noexcept { return !operator==( rhs ); }
194
196 constexpr void operator*=( const Units &rhs ) noexcept;
197
199 constexpr void operator*=( double x ) noexcept { d_scale *= x; }
200
202 constexpr void operator/=( const Units &rhs ) noexcept;
203
205 constexpr Units pow( int p ) const noexcept;
206
208 constexpr bool isNull() const { return d_scale == 0; }
209
210
211public:
213 std::string str() const;
214
216 static constexpr std::string_view getPrefixStr( UnitPrefix ) noexcept;
217
219 std::string printSI() const;
220
222 std::string printUnit() const;
223
225 std::string printFull() const;
226
227
228public:
230 static inline std::vector<std::string> getAllPrefixes();
231
233 static inline std::vector<std::string> getAllUnits();
234
235 // Convert string to int
236 static constexpr int atoi( std::string_view, bool = true );
237
238 // Convert string to double
239 static constexpr double strtod( std::string_view );
240
241
242protected:
244 SI_type d_SI = { 0 };
245 double d_scale = { 0.0 };
246
247protected:
248 std::string printSIBase() const;
249 static constexpr Units read( std::string_view str );
250 static constexpr Units read2( std::string_view str );
251 static constexpr Units readUnit( const std::string_view &str, bool throwErr = true );
252 static constexpr SI_type combine( const SI_type &a, const SI_type &b );
253 static constexpr SI_type getSI( UnitType );
254 static constexpr std::pair<size_t, char> findToken( const std::string_view &, size_t );
255 static constexpr size_t findPar( const std::string_view &, size_t );
256
257protected:
258 friend constexpr Units pow( Units base, int exponent );
259};
260
261
262inline std::ostream &operator<<( std::ostream &os, const Units &unit )
263{
264 os << unit.str();
265 return os;
266}
267
268
269} // namespace AMP
270
271
272#include "AMP/utils/Units.hpp"
273
274
275#endif
Provides a class for storing units.
Definition Units.h:101
constexpr bool isNull() const
Check if unit is null.
Definition Units.h:208
friend constexpr Units pow(Units base, int exponent)
constexpr Units(const char *unit)
Construct the units from a const char array.
static constexpr Units read(std::string_view str)
std::array< int8_t, 9 > SI_type
Definition Units.h:104
static constexpr std::string_view getPrefixStr(UnitPrefix) noexcept
Get a string representation for the prefix.
constexpr Units()=default
Empty constructor.
constexpr bool operator==(const Units &rhs) const noexcept
Operator ==.
unit_type d_unit
Definition Units.h:243
constexpr void operator*=(const Units &rhs) noexcept
Operator *=.
constexpr Units pow(int p) const noexcept
Raise the unit to the given power.
static constexpr int atoi(std::string_view, bool=true)
static constexpr double strtod(std::string_view)
constexpr Units(const SI_type &unit, double scale=1.0)
Construct the units.
constexpr Units(const std::string_view &unit, double value)
Construct the units from a const char array.
static constexpr SI_type getSI(UnitType)
static constexpr SI_type combine(const SI_type &a, const SI_type &b)
static constexpr std::pair< size_t, char > findToken(const std::string_view &, size_t)
constexpr bool compatible(const Units &unit) noexcept
Check if two units are compatible.
constexpr void operator*=(double x) noexcept
Operator *=.
Definition Units.h:199
static constexpr size_t findPar(const std::string_view &, size_t)
constexpr UnitType getType() const noexcept
Get the unit type.
double d_scale
Definition Units.h:245
constexpr double convert(const Units &unit) const
Convert the unit to a new type.
SI_type d_SI
Definition Units.h:244
std::string printSIBase() const
constexpr Units(const UnitType &unit, double scale=1.0)
Construct the units.
std::string printSI() const
Get a string representation of the units in SI units (with scaling factor)
static constexpr Units readUnit(const std::string_view &str, bool throwErr=true)
static std::vector< std::string > getAllUnits()
Get all supported units.
constexpr Units(const std::string_view &unit)
Construct the units from a const char array.
static constexpr Units read2(std::string_view str)
std::string printUnit() const
Get a string representation of the units in SI units (with scaling factor)
std::array< char, 31 > unit_type
Definition Units.h:103
std::string printFull() const
Get the full unit and conversion string.
constexpr void operator/=(const Units &rhs) noexcept
Operator /=.
std::string str() const
Get a string representation of the units.
static constexpr UnitPrefix getUnitPrefix(const std::string_view &str) noexcept
Get the prefix from a string.
static std::vector< std::string > getAllPrefixes()
Get all supported unit prefixes.
std::ostream & operator<<(std::ostream &out, const AMP::ArraySize &s)
Definition Array.h:861
UnitPrefix
Enum to hold prefix.
Definition Units.h:15
UnitType
Enum to hold type.
Definition Units.h:46



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:41.
Comments on this page