Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Mass.hpp
Go to the documentation of this file.
1 // Copyright © 2020-2024 Alexandre Coderre-Chabot
2 //
3 // This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4 // models, and units of measure for scientific computing.
5 //
6 // Physical Quantities is hosted at:
7 // https://github.com/acodcha/phq
8 //
9 // Physical Quantities is licensed under the MIT License:
10 // https://mit-license.org
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13 // associated documentation files (the "Software"), to deal in the Software without restriction,
14 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
15 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // - The above copyright notice and this permission notice shall be included in all copies or
18 // substantial portions of the Software.
19 // - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 #ifndef PHQ_UNIT_MASS_HPP
26 #define PHQ_UNIT_MASS_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <map>
32 #include <ostream>
33 #include <string_view>
34 #include <unordered_map>
35 
36 #include "../Base.hpp"
37 #include "../Dimension/ElectricCurrent.hpp"
38 #include "../Dimension/Length.hpp"
39 #include "../Dimension/LuminousIntensity.hpp"
40 #include "../Dimension/Mass.hpp"
41 #include "../Dimension/SubstanceAmount.hpp"
42 #include "../Dimension/Temperature.hpp"
43 #include "../Dimension/Time.hpp"
44 #include "../Dimensions.hpp"
45 #include "../Unit.hpp"
46 #include "../UnitSystem.hpp"
47 
48 namespace PhQ {
49 
50 namespace Unit {
51 
52 /// \brief Mass units.
53 enum class Mass : int8_t {
54  /// \brief Kilogram (kg) mass unit.
55  Kilogram,
56 
57  /// \brief Gram (g) mass unit.
58  Gram,
59 
60  /// \brief Slug (slug) mass unit.
61  Slug,
62 
63  /// \brief Slinch (slinch) mass unit.
64  Slinch,
65 
66  /// \brief Pound (lbm) mass unit.
67  Pound,
68 };
69 
70 } // namespace Unit
71 
72 /// \brief Standard mass unit: kilogram (kg).
73 template <>
74 inline constexpr const Unit::Mass Standard<Unit::Mass>{Unit::Mass::Kilogram};
75 
76 /// \brief Physical dimension set of mass units.
77 template <>
78 inline constexpr const Dimensions RelatedDimensions<Unit::Mass>{
81  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
82 };
83 
84 inline std::ostream& operator<<(std::ostream& stream, const Unit::Mass unit) {
85  stream << Abbreviation(unit);
86  return stream;
87 }
88 
89 namespace Internal {
90 
91 template <>
92 inline const std::map<UnitSystem, Unit::Mass> ConsistentUnits<Unit::Mass>{
97 };
98 
99 template <>
100 inline const std::map<Unit::Mass, UnitSystem> RelatedUnitSystems<Unit::Mass>{
105 };
106 
107 template <>
108 inline const std::map<Unit::Mass, std::string_view> Abbreviations<Unit::Mass>{
109  {Unit::Mass::Kilogram, "kg" },
110  {Unit::Mass::Gram, "g" },
111  {Unit::Mass::Slug, "slug" },
112  {Unit::Mass::Slinch, "slinch"},
113  {Unit::Mass::Pound, "lbm" },
114 };
115 
116 template <>
117 inline const std::unordered_map<std::string_view, Unit::Mass> Spellings<Unit::Mass>{
118  {"kg", Unit::Mass::Kilogram},
119  {"g", Unit::Mass::Gram },
120  {"slug", Unit::Mass::Slug },
121  {"slinch", Unit::Mass::Slinch },
122  {"lbm", Unit::Mass::Pound },
123  {"lb", Unit::Mass::Pound },
124 };
125 
126 template <>
127 template <typename NumericType>
128 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Kilogram>::FromStandard(
129  NumericType& /*value*/) noexcept {}
130 
131 template <>
132 template <typename NumericType>
133 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Kilogram>::ToStandard(
134  NumericType& /*value*/) noexcept {}
135 
136 template <>
137 template <typename NumericType>
138 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Gram>::FromStandard(
139  NumericType& value) noexcept {
140  value *= static_cast<NumericType>(1000.0L);
141 }
142 
143 template <>
144 template <typename NumericType>
145 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Gram>::ToStandard(
146  NumericType& value) noexcept {
147  value *= static_cast<NumericType>(0.001L);
148 }
149 
150 template <>
151 template <typename NumericType>
152 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Slug>::FromStandard(
153  NumericType& value) noexcept {
154  value *= static_cast<NumericType>(0.3048L)
155  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
156 }
157 
158 template <>
159 template <typename NumericType>
160 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Slug>::ToStandard(
161  NumericType& value) noexcept {
162  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
163  / static_cast<NumericType>(0.3048L);
164 }
165 
166 template <>
167 template <typename NumericType>
168 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Slinch>::FromStandard(
169  NumericType& value) noexcept {
170  value *= static_cast<NumericType>(0.0254L)
171  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
172 }
173 
174 template <>
175 template <typename NumericType>
176 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Slinch>::ToStandard(
177  NumericType& value) noexcept {
178  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
179  / static_cast<NumericType>(0.0254L);
180 }
181 
182 template <>
183 template <typename NumericType>
184 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Pound>::FromStandard(
185  NumericType& value) noexcept {
186  value /= static_cast<NumericType>(0.45359237L);
187 }
188 
189 template <>
190 template <typename NumericType>
191 inline constexpr void Conversion<Unit::Mass, Unit::Mass::Pound>::ToStandard(
192  NumericType& value) noexcept {
193  value *= static_cast<NumericType>(0.45359237L);
194 }
195 
196 template <typename NumericType>
197 inline const std::map<Unit::Mass, std::function<void(NumericType* values, const std::size_t size)>>
198  MapOfConversionsFromStandard<Unit::Mass, NumericType>{
200  Conversions<Unit::Mass, Unit::Mass::Kilogram>::FromStandard<NumericType>},
201  {Unit::Mass::Gram, Conversions<Unit::Mass, Unit::Mass::Gram>::FromStandard<NumericType> },
202  {Unit::Mass::Slug, Conversions<Unit::Mass, Unit::Mass::Slug>::FromStandard<NumericType> },
204  Conversions<Unit::Mass, Unit::Mass::Slinch>::FromStandard<NumericType> },
205  {Unit::Mass::Pound, Conversions<Unit::Mass, Unit::Mass::Pound>::FromStandard<NumericType> },
206 };
207 
208 template <typename NumericType>
209 inline const std::map<Unit::Mass,
210  std::function<void(NumericType* const values, const std::size_t size)>>
211  MapOfConversionsToStandard<Unit::Mass, NumericType>{
213  Conversions<Unit::Mass, Unit::Mass::Kilogram>::ToStandard<NumericType>},
214  {Unit::Mass::Gram, Conversions<Unit::Mass, Unit::Mass::Gram>::ToStandard<NumericType> },
215  {Unit::Mass::Slug, Conversions<Unit::Mass, Unit::Mass::Slug>::ToStandard<NumericType> },
216  {Unit::Mass::Slinch, Conversions<Unit::Mass, Unit::Mass::Slinch>::ToStandard<NumericType> },
217  {Unit::Mass::Pound, Conversions<Unit::Mass, Unit::Mass::Pound>::ToStandard<NumericType> },
218 };
219 
220 } // namespace Internal
221 
222 } // namespace PhQ
223 
224 #endif // PHQ_UNIT_MASS_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
@ Slug
Slug (slug) mass unit.
@ Slinch
Slinch (slinch) mass unit.
@ Pound
Pound (lbm) mass unit.
@ Gram
Gram (g) mass unit.
@ Kilogram
Kilogram (kg) mass unit.
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Definition: Temperature.hpp:55
Time
Time units.
Definition: Time.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.
@ FootPoundSecondRankine
Foot-pound-second-rankine (ft·lbf·s·°R) system.
@ MillimetreGramSecondKelvin
Millimetre-gram-second-kelvin (mm·g·s·K) system.
@ MetreKilogramSecondKelvin
Metre-kilogram-second-kelvin (m·kg·s·K) system.
@ InchPoundSecondRankine
Inch-pound-second-rankine (in·lbf·s·°R) system.
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition: Base.hpp:89
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)