Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Mass units.
53enum class Mass : int8_t {
54 /// \brief Kilogram (kg) mass unit.
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).
73template <>
74inline constexpr const Unit::Mass Standard<Unit::Mass>{Unit::Mass::Kilogram};
75
76/// \brief Physical dimension set of mass units.
77template <>
78inline constexpr const Dimensions RelatedDimensions<Unit::Mass>{
79 Dimensions{Dimension::Time{0}, Dimension::Length{0}, Dimension::Mass{1},
80 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
81 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
82};
83
84inline std::ostream& operator<<(std::ostream& stream, const Unit::Mass unit) {
85 stream << Abbreviation(unit);
86 return stream;
87}
88
89namespace Internal {
90
91template <>
92inline const std::map<UnitSystem, Unit::Mass> ConsistentUnits<Unit::Mass>{
97};
98
99template <>
100inline const std::map<Unit::Mass, UnitSystem> RelatedUnitSystems<Unit::Mass>{
105};
106
107template <>
108inline 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
116template <>
117inline 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
126template <>
127template <typename NumericType>
128inline constexpr void Conversion<Unit::Mass, Unit::Mass::Kilogram>::FromStandard(
129 NumericType& /*value*/) noexcept {}
130
131template <>
132template <typename NumericType>
133inline constexpr void Conversion<Unit::Mass, Unit::Mass::Kilogram>::ToStandard(
134 NumericType& /*value*/) noexcept {}
135
136template <>
137template <typename NumericType>
138inline constexpr void Conversion<Unit::Mass, Unit::Mass::Gram>::FromStandard(
139 NumericType& value) noexcept {
140 value *= static_cast<NumericType>(1000.0L);
141}
142
143template <>
144template <typename NumericType>
145inline constexpr void Conversion<Unit::Mass, Unit::Mass::Gram>::ToStandard(
146 NumericType& value) noexcept {
147 value *= static_cast<NumericType>(0.001L);
148}
149
150template <>
151template <typename NumericType>
152inline 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
158template <>
159template <typename NumericType>
160inline 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
166template <>
167template <typename NumericType>
168inline 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
174template <>
175template <typename NumericType>
176inline 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
182template <>
183template <typename NumericType>
184inline constexpr void Conversion<Unit::Mass, Unit::Mass::Pound>::FromStandard(
185 NumericType& value) noexcept {
186 value /= static_cast<NumericType>(0.45359237L);
187}
188
189template <>
190template <typename NumericType>
191inline constexpr void Conversion<Unit::Mass, Unit::Mass::Pound>::ToStandard(
192 NumericType& value) noexcept {
193 value *= static_cast<NumericType>(0.45359237L);
194}
195
196template <typename NumericType>
197inline 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> },
203 {Unit::Mass::Slinch, Conversions<Unit::Mass, Unit::Mass::Slinch>::FromStandard<NumericType> },
204 {Unit::Mass::Pound, Conversions<Unit::Mass, Unit::Mass::Pound>::FromStandard<NumericType> },
205};
206
207template <typename NumericType>
208inline const std::
209 map<Unit::Mass, std::function<void(NumericType* const values, const std::size_t size)>>
210 MapOfConversionsToStandard<Unit::Mass, NumericType>{
212 Conversions<Unit::Mass, Unit::Mass::Kilogram>::ToStandard<NumericType>},
213 {Unit::Mass::Gram, Conversions<Unit::Mass, Unit::Mass::Gram>::ToStandard<NumericType> },
214 {Unit::Mass::Slug, Conversions<Unit::Mass, Unit::Mass::Slug>::ToStandard<NumericType> },
216 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
@ Pound
Pound (lbf) force unit.
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.
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
@ 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