Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
MassDensity.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_DENSITY_HPP
26 #define PHQ_UNIT_MASS_DENSITY_HPP
27 
28 #include <cmath>
29 #include <cstddef>
30 #include <cstdint>
31 #include <functional>
32 #include <map>
33 #include <ostream>
34 #include <string_view>
35 #include <unordered_map>
36 
37 #include "../Base.hpp"
38 #include "../Dimension/ElectricCurrent.hpp"
39 #include "../Dimension/Length.hpp"
40 #include "../Dimension/LuminousIntensity.hpp"
41 #include "../Dimension/Mass.hpp"
42 #include "../Dimension/SubstanceAmount.hpp"
43 #include "../Dimension/Temperature.hpp"
44 #include "../Dimension/Time.hpp"
45 #include "../Dimensions.hpp"
46 #include "../Unit.hpp"
47 #include "../UnitSystem.hpp"
48 
49 namespace PhQ {
50 
51 namespace Unit {
52 
53 /// \brief Mass density units.
54 enum class MassDensity : int8_t {
55  /// \brief Kilogram per cubic metre (kg/m^3) mass density unit.
57 
58  /// \brief Gram per cubic millimetre (g/mm^3) mass density unit.
60 
61  /// \brief Slug per cubic foot (slug/ft^3) mass density unit.
63 
64  /// \brief Slinch per cubic inch (slinch/in^3) mass density unit.
66 
67  /// \brief Pound per cubic foot (lbm/ft^3) mass density unit.
69 
70  /// \brief Pound per cubic inch (lbm/in^3) mass density unit.
72 };
73 
74 } // namespace Unit
75 
76 /// \brief Standard mass density unit: kilogram per cubic metre (kg/m^3).
77 template <>
78 inline constexpr const Unit::MassDensity Standard<Unit::MassDensity>{
80 
81 /// \brief Physical dimension set of mass density units.
82 template <>
83 inline constexpr const Dimensions RelatedDimensions<Unit::MassDensity>{
84  Dimensions{Dimension::Time{0}, Dimension::Length{-3}, Dimension::Mass{1},
86  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
87 };
88 
89 inline std::ostream& operator<<(std::ostream& stream, const Unit::MassDensity unit) {
90  stream << Abbreviation(unit);
91  return stream;
92 }
93 
94 namespace Internal {
95 
96 template <>
97 inline const std::map<UnitSystem, Unit::MassDensity> ConsistentUnits<Unit::MassDensity>{
102 };
103 
104 template <>
105 inline const std::map<Unit::MassDensity, UnitSystem> RelatedUnitSystems<Unit::MassDensity>{
110 };
111 
112 template <>
113 inline const std::map<Unit::MassDensity, std::string_view> Abbreviations<Unit::MassDensity>{
116  {Unit::MassDensity::SlugPerCubicFoot, "slug/ft^3" },
117  {Unit::MassDensity::SlinchPerCubicInch, "slinch/in^3"},
120 };
121 
122 template <>
123 inline const std::unordered_map<std::string_view, Unit::MassDensity> Spellings<Unit::MassDensity>{
130  {"slug/ft^3", Unit::MassDensity::SlugPerCubicFoot },
132  {"slug/ft/ft/ft", Unit::MassDensity::SlugPerCubicFoot },
133  {"slinch/in^3", Unit::MassDensity::SlinchPerCubicInch },
134  {"slinch/in3", Unit::MassDensity::SlinchPerCubicInch },
135  {"slinch/in/in/in", Unit::MassDensity::SlinchPerCubicInch },
138  {"lbm/ft/ft/ft", Unit::MassDensity::PoundPerCubicFoot },
141  {"lb/ft/ft/ft", Unit::MassDensity::PoundPerCubicFoot },
144  {"lbm/in/in/in", Unit::MassDensity::PoundPerCubicInch },
147  {"lb/in/in/in", Unit::MassDensity::PoundPerCubicInch },
148 };
149 
150 template <>
151 template <typename NumericType>
152 inline constexpr void
153 Conversion<Unit::MassDensity, Unit::MassDensity::KilogramPerCubicMetre>::FromStandard(
154  NumericType& /*value*/) noexcept {}
155 
156 template <>
157 template <typename NumericType>
158 inline constexpr void
159 Conversion<Unit::MassDensity, Unit::MassDensity::KilogramPerCubicMetre>::ToStandard(
160  NumericType& /*value*/) noexcept {}
161 
162 template <>
163 template <typename NumericType>
164 inline constexpr void
165 Conversion<Unit::MassDensity, Unit::MassDensity::GramPerCubicMillimetre>::FromStandard(
166  NumericType& value) noexcept {
167  value *= static_cast<NumericType>(0.000001L);
168 }
169 
170 template <>
171 template <typename NumericType>
172 inline constexpr void
173 Conversion<Unit::MassDensity, Unit::MassDensity::GramPerCubicMillimetre>::ToStandard(
174  NumericType& value) noexcept {
175  value *= static_cast<NumericType>(1000000.0L);
176 }
177 
178 template <>
179 template <typename NumericType>
180 inline constexpr void
181 Conversion<Unit::MassDensity, Unit::MassDensity::SlugPerCubicFoot>::FromStandard(
182  NumericType& value) noexcept {
183  value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
184  * static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
185  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
186 }
187 
188 template <>
189 template <typename NumericType>
190 inline constexpr void
191 Conversion<Unit::MassDensity, Unit::MassDensity::SlugPerCubicFoot>::ToStandard(
192  NumericType& value) noexcept {
193  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
194  / std::pow(static_cast<NumericType>(0.3048L), 4);
195 }
196 
197 template <>
198 template <typename NumericType>
199 inline constexpr void
200 Conversion<Unit::MassDensity, Unit::MassDensity::SlinchPerCubicInch>::FromStandard(
201  NumericType& value) noexcept {
202  value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
203  * static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
204  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
205 }
206 
207 template <>
208 template <typename NumericType>
209 inline constexpr void
210 Conversion<Unit::MassDensity, Unit::MassDensity::SlinchPerCubicInch>::ToStandard(
211  NumericType& value) noexcept {
212  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
213  / std::pow(static_cast<NumericType>(0.0254L), 4);
214 }
215 
216 template <>
217 template <typename NumericType>
218 inline constexpr void
219 Conversion<Unit::MassDensity, Unit::MassDensity::PoundPerCubicFoot>::FromStandard(
220  NumericType& value) noexcept {
221  value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
222  * static_cast<NumericType>(0.3048L) / static_cast<NumericType>(0.45359237L);
223 }
224 
225 template <>
226 template <typename NumericType>
227 inline constexpr void
228 Conversion<Unit::MassDensity, Unit::MassDensity::PoundPerCubicFoot>::ToStandard(
229  NumericType& value) noexcept {
230  value *= static_cast<NumericType>(0.45359237L) / std::pow(static_cast<NumericType>(0.3048L), 3);
231 }
232 
233 template <>
234 template <typename NumericType>
235 inline constexpr void
236 Conversion<Unit::MassDensity, Unit::MassDensity::PoundPerCubicInch>::FromStandard(
237  NumericType& value) noexcept {
238  value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
239  * static_cast<NumericType>(0.0254L) / static_cast<NumericType>(0.45359237L);
240 }
241 
242 template <>
243 template <typename NumericType>
244 inline constexpr void
245 Conversion<Unit::MassDensity, Unit::MassDensity::PoundPerCubicInch>::ToStandard(
246  NumericType& value) noexcept {
247  value *= static_cast<NumericType>(0.45359237L) / std::pow(static_cast<NumericType>(0.0254L), 3);
248 }
249 
250 template <typename NumericType>
251 inline const std::map<Unit::MassDensity,
252  std::function<void(NumericType* values, const std::size_t size)>>
253  MapOfConversionsFromStandard<Unit::MassDensity, NumericType>{
255  Conversions<Unit::MassDensity,
256  Unit::MassDensity::KilogramPerCubicMetre>::FromStandard<NumericType> },
258  Conversions<Unit::MassDensity,
259  Unit::MassDensity::GramPerCubicMillimetre>::FromStandard<NumericType>},
261  Conversions<Unit::MassDensity,
262  Unit::MassDensity::SlugPerCubicFoot>::FromStandard<NumericType> },
264  Conversions<Unit::MassDensity,
265  Unit::MassDensity::SlinchPerCubicInch>::FromStandard<NumericType> },
267  Conversions<Unit::MassDensity,
268  Unit::MassDensity::PoundPerCubicFoot>::FromStandard<NumericType> },
270  Conversions<Unit::MassDensity,
271  Unit::MassDensity::PoundPerCubicInch>::FromStandard<NumericType> },
272 };
273 
274 template <typename NumericType>
275 inline const std::map<Unit::MassDensity,
276  std::function<void(NumericType* const values, const std::size_t size)>>
277  MapOfConversionsToStandard<Unit::MassDensity, NumericType>{
279  Conversions<Unit::MassDensity,
280  Unit::MassDensity::KilogramPerCubicMetre>::ToStandard<NumericType> },
282  Conversions<Unit::MassDensity,
283  Unit::MassDensity::GramPerCubicMillimetre>::ToStandard<NumericType>},
285  Conversions<Unit::MassDensity,
286  Unit::MassDensity::SlugPerCubicFoot>::ToStandard<NumericType> },
288  Conversions<Unit::MassDensity,
289  Unit::MassDensity::SlinchPerCubicInch>::ToStandard<NumericType> },
291  Conversions<Unit::MassDensity,
292  Unit::MassDensity::PoundPerCubicFoot>::ToStandard<NumericType> },
294  Conversions<Unit::MassDensity,
295  Unit::MassDensity::PoundPerCubicInch>::ToStandard<NumericType> },
296 };
297 
298 } // namespace Internal
299 
300 } // namespace PhQ
301 
302 #endif // PHQ_UNIT_MASS_DENSITY_HPP
ElectricCurrent
Electric current units.
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
@ SlugPerCubicFoot
Slug per cubic foot (slug/ft^3) mass density unit.
@ PoundPerCubicInch
Pound per cubic inch (lbm/in^3) mass density unit.
@ SlinchPerCubicInch
Slinch per cubic inch (slinch/in^3) mass density unit.
@ GramPerCubicMillimetre
Gram per cubic millimetre (g/mm^3) mass density unit.
@ PoundPerCubicFoot
Pound per cubic foot (lbm/ft^3) mass density unit.
@ KilogramPerCubicMetre
Kilogram per cubic metre (kg/m^3) mass density unit.
Mass
Mass units.
Definition: Mass.hpp:53
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)