Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
SubstanceAmount.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_SUBSTANCE_AMOUNT_HPP
26 #define PHQ_UNIT_SUBSTANCE_AMOUNT_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 Amount of substance units.
53 enum class SubstanceAmount : int8_t {
54  /// \brief Mole (mol) substance amount unit.
55  Mole,
56 
57  /// \brief Kilomole (kmol) substance amount unit.
58  Kilomole,
59 
60  /// \brief Megamole (Mmol) substance amount unit.
61  Megamole,
62 
63  /// \brief Gigamole (Gmol) substance amount unit.
64  Gigamole,
65 
66  /// \brief Particles (particles) substance amount unit.
67  Particles,
68 };
69 
70 } // namespace Unit
71 
72 /// \brief Standard amount of substance unit: mole (mol).
73 template <>
74 inline constexpr const Unit::SubstanceAmount Standard<Unit::SubstanceAmount>{
76 
77 /// \brief Physical dimension set of amount of substance units.
78 template <>
79 inline constexpr const Dimensions RelatedDimensions<Unit::SubstanceAmount>{
82  Dimension::SubstanceAmount{1}, Dimension::LuminousIntensity{0}}
83 };
84 
85 inline std::ostream& operator<<(std::ostream& stream, const Unit::SubstanceAmount unit) {
86  stream << Abbreviation(unit);
87  return stream;
88 }
89 
90 namespace Internal {
91 
92 template <>
93 inline const std::map<UnitSystem, Unit::SubstanceAmount> ConsistentUnits<Unit::SubstanceAmount>{
98 };
99 
100 template <>
101 inline const std::map<Unit::SubstanceAmount, UnitSystem>
102  RelatedUnitSystems<Unit::SubstanceAmount>{};
103 
104 template <>
105 inline const std::map<Unit::SubstanceAmount, std::string_view> Abbreviations<Unit::SubstanceAmount>{
106  {Unit::SubstanceAmount::Mole, "mol" },
110  {Unit::SubstanceAmount::Particles, "particles"},
111 };
112 
113 template <>
114 inline const std::unordered_map<std::string_view, Unit::SubstanceAmount>
115  Spellings<Unit::SubstanceAmount>{
116  {"mol", Unit::SubstanceAmount::Mole },
120  {"particles", Unit::SubstanceAmount::Particles},
121 };
122 
123 template <>
124 template <typename NumericType>
125 inline constexpr void Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Mole>::FromStandard(
126  NumericType& /*value*/) noexcept {}
127 
128 template <>
129 template <typename NumericType>
130 inline constexpr void Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Mole>::ToStandard(
131  NumericType& /*value*/) noexcept {}
132 
133 template <>
134 template <typename NumericType>
135 inline constexpr void
136 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Kilomole>::FromStandard(
137  NumericType& value) noexcept {
138  value *= static_cast<NumericType>(0.001L);
139 }
140 
141 template <>
142 template <typename NumericType>
143 inline constexpr void
144 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Kilomole>::ToStandard(
145  NumericType& value) noexcept {
146  value *= static_cast<NumericType>(1000.0L);
147 }
148 
149 template <>
150 template <typename NumericType>
151 inline constexpr void
152 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Megamole>::FromStandard(
153  NumericType& value) noexcept {
154  value *= static_cast<NumericType>(1.0E-6L);
155 }
156 
157 template <>
158 template <typename NumericType>
159 inline constexpr void
160 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Megamole>::ToStandard(
161  NumericType& value) noexcept {
162  value *= static_cast<NumericType>(1.0E6L);
163 }
164 
165 template <>
166 template <typename NumericType>
167 inline constexpr void
168 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Gigamole>::FromStandard(
169  NumericType& value) noexcept {
170  value *= static_cast<NumericType>(1.0E-9L);
171 }
172 
173 template <>
174 template <typename NumericType>
175 inline constexpr void
176 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Gigamole>::ToStandard(
177  NumericType& value) noexcept {
178  value *= static_cast<NumericType>(1.0E9L);
179 }
180 
181 template <>
182 template <typename NumericType>
183 inline constexpr void
184 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Particles>::FromStandard(
185  NumericType& value) noexcept {
186  value *= static_cast<NumericType>(6.02214076E23L);
187 }
188 
189 template <>
190 template <typename NumericType>
191 inline constexpr void
192 Conversion<Unit::SubstanceAmount, Unit::SubstanceAmount::Particles>::ToStandard(
193  NumericType& value) noexcept {
194  value /= static_cast<NumericType>(6.02214076E23L);
195 }
196 
197 template <typename NumericType>
198 inline const std::map<Unit::SubstanceAmount,
199  std::function<void(NumericType* values, const std::size_t size)>>
200  MapOfConversionsFromStandard<Unit::SubstanceAmount, NumericType>{
202  Conversions<Unit::SubstanceAmount,
203  Unit::SubstanceAmount::Mole>::FromStandard<NumericType> },
205  Conversions<Unit::SubstanceAmount,
206  Unit::SubstanceAmount::Kilomole>::FromStandard<NumericType> },
208  Conversions<Unit::SubstanceAmount,
209  Unit::SubstanceAmount::Megamole>::FromStandard<NumericType> },
211  Conversions<Unit::SubstanceAmount,
212  Unit::SubstanceAmount::Gigamole>::FromStandard<NumericType> },
214  Conversions<Unit::SubstanceAmount,
215  Unit::SubstanceAmount::Particles>::FromStandard<NumericType>},
216 };
217 
218 template <typename NumericType>
219 inline const std::map<Unit::SubstanceAmount,
220  std::function<void(NumericType* const values, const std::size_t size)>>
221  MapOfConversionsToStandard<Unit::SubstanceAmount, NumericType>{
223  Conversions<Unit::SubstanceAmount, Unit::SubstanceAmount::Mole>::ToStandard<NumericType>},
225  Conversions<Unit::SubstanceAmount,
226  Unit::SubstanceAmount::Kilomole>::ToStandard<NumericType> },
228  Conversions<Unit::SubstanceAmount,
229  Unit::SubstanceAmount::Megamole>::ToStandard<NumericType> },
231  Conversions<Unit::SubstanceAmount,
232  Unit::SubstanceAmount::Gigamole>::ToStandard<NumericType> },
234  Conversions<Unit::SubstanceAmount,
235  Unit::SubstanceAmount::Particles>::ToStandard<NumericType> },
236 };
237 
238 } // namespace Internal
239 
240 } // namespace PhQ
241 
242 #endif // PHQ_UNIT_SUBSTANCE_AMOUNT_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
@ Kilomole
Kilomole (kmol) substance amount unit.
@ Megamole
Megamole (Mmol) substance amount unit.
@ Gigamole
Gigamole (Gmol) substance amount unit.
@ Particles
Particles (particles) substance amount unit.
@ Mole
Mole (mol) substance amount unit.
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)