Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Force.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_FORCE_HPP
26 #define PHQ_UNIT_FORCE_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 Force units.
53 enum class Force : int8_t {
54  /// \brief Newton (N) force unit.
55  Newton,
56 
57  /// \brief Kilonewton (kN) force unit.
58  Kilonewton,
59 
60  /// \brief Meganewton (MN) force unit.
61  Meganewton,
62 
63  /// \brief Giganewton (GN) force unit.
64  Giganewton,
65 
66  /// \brief Millinewton (mN) force unit.
68 
69  /// \brief Micronewton (μN) force unit.
71 
72  /// \brief Nanonewton (nN) force unit.
73  Nanonewton,
74 
75  /// \brief Dyne (dyn) force unit.
76  Dyne,
77 
78  /// \brief Pound (lbf) force unit.
79  Pound,
80 };
81 
82 } // namespace Unit
83 
84 /// \brief Standard force unit: newton (N).
85 template <>
86 inline constexpr const Unit::Force Standard<Unit::Force>{Unit::Force::Newton};
87 
88 /// \brief Physical dimension set of force units.
89 template <>
90 inline constexpr const Dimensions RelatedDimensions<Unit::Force>{
91  Dimensions{Dimension::Time{-2}, Dimension::Length{1}, Dimension::Mass{1},
93  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
94 };
95 
96 inline std::ostream& operator<<(std::ostream& stream, const Unit::Force unit) {
97  stream << Abbreviation(unit);
98  return stream;
99 }
100 
101 namespace Internal {
102 
103 template <>
104 inline const std::map<UnitSystem, Unit::Force> ConsistentUnits<Unit::Force>{
109 };
110 
111 template <>
112 inline const std::map<Unit::Force, UnitSystem> RelatedUnitSystems<Unit::Force>{
115 };
116 
117 // clang-format off
118 
119 template <>
120 inline const std::map<Unit::Force, std::string_view> Abbreviations<Unit::Force>{
121  {Unit::Force::Newton, "N" },
122  {Unit::Force::Kilonewton, "kN" },
123  {Unit::Force::Meganewton, "MN" },
124  {Unit::Force::Giganewton, "GN" },
125  {Unit::Force::Millinewton, "mN" },
126  {Unit::Force::Micronewton, "μN" },
127  {Unit::Force::Nanonewton, "nN" },
128  {Unit::Force::Dyne, "dyn"},
129  {Unit::Force::Pound, "lbf"},
130 };
131 
132 template <>
133 inline const std::unordered_map<std::string_view, Unit::Force> Spellings<Unit::Force>{
134  {"N", Unit::Force::Newton },
135  {"J/m", Unit::Force::Newton },
136  {"kJ/km", Unit::Force::Newton },
137  {"kg·m/s^2", Unit::Force::Newton },
138  {"kg*m/s^2", Unit::Force::Newton },
139  {"kg·m/s2", Unit::Force::Newton },
140  {"kg*m/s2", Unit::Force::Newton },
141  {"kN", Unit::Force::Kilonewton },
142  {"MN", Unit::Force::Meganewton },
143  {"GN", Unit::Force::Giganewton },
144  {"mN", Unit::Force::Millinewton},
145  {"μN", Unit::Force::Micronewton},
146  {"uN", Unit::Force::Micronewton},
147  {"nJ/mm", Unit::Force::Micronewton},
148  {"g·mm/s^2", Unit::Force::Micronewton},
149  {"g*mm/s^2", Unit::Force::Micronewton},
150  {"g·mm/s2", Unit::Force::Micronewton},
151  {"g*mm/s2", Unit::Force::Micronewton},
152  {"nN", Unit::Force::Nanonewton },
153  {"dyn", Unit::Force::Dyne },
154  {"lbf", Unit::Force::Pound },
155  {"lb", Unit::Force::Pound },
156 };
157 
158 // clang-format on
159 
160 template <>
161 template <typename NumericType>
162 inline constexpr void Conversion<Unit::Force, Unit::Force::Newton>::FromStandard(
163  NumericType& /*value*/) noexcept {}
164 
165 template <>
166 template <typename NumericType>
167 inline constexpr void Conversion<Unit::Force, Unit::Force::Newton>::ToStandard(
168  NumericType& /*value*/) noexcept {}
169 
170 template <>
171 template <typename NumericType>
172 inline constexpr void Conversion<Unit::Force, Unit::Force::Kilonewton>::FromStandard(
173  NumericType& value) noexcept {
174  value *= static_cast<NumericType>(0.001L);
175 }
176 
177 template <>
178 template <typename NumericType>
179 inline constexpr void Conversion<Unit::Force, Unit::Force::Kilonewton>::ToStandard(
180  NumericType& value) noexcept {
181  value *= static_cast<NumericType>(1000.0L);
182 }
183 
184 template <>
185 template <typename NumericType>
186 inline constexpr void Conversion<Unit::Force, Unit::Force::Meganewton>::FromStandard(
187  NumericType& value) noexcept {
188  value *= static_cast<NumericType>(0.000001L);
189 }
190 
191 template <>
192 template <typename NumericType>
193 inline constexpr void Conversion<Unit::Force, Unit::Force::Meganewton>::ToStandard(
194  NumericType& value) noexcept {
195  value *= static_cast<NumericType>(1000000.0L);
196 }
197 
198 template <>
199 template <typename NumericType>
200 inline constexpr void Conversion<Unit::Force, Unit::Force::Giganewton>::FromStandard(
201  NumericType& value) noexcept {
202  value *= static_cast<NumericType>(0.000000001L);
203 }
204 
205 template <>
206 template <typename NumericType>
207 inline constexpr void Conversion<Unit::Force, Unit::Force::Giganewton>::ToStandard(
208  NumericType& value) noexcept {
209  value *= static_cast<NumericType>(1000000000.0L);
210 }
211 
212 template <>
213 template <typename NumericType>
214 inline constexpr void Conversion<Unit::Force, Unit::Force::Millinewton>::FromStandard(
215  NumericType& value) noexcept {
216  value *= static_cast<NumericType>(1000.0L);
217 }
218 
219 template <>
220 template <typename NumericType>
221 inline constexpr void Conversion<Unit::Force, Unit::Force::Millinewton>::ToStandard(
222  NumericType& value) noexcept {
223  value *= static_cast<NumericType>(0.001L);
224 }
225 
226 template <>
227 template <typename NumericType>
228 inline constexpr void Conversion<Unit::Force, Unit::Force::Micronewton>::FromStandard(
229  NumericType& value) noexcept {
230  value *= static_cast<NumericType>(1000000.0L);
231 }
232 
233 template <>
234 template <typename NumericType>
235 inline constexpr void Conversion<Unit::Force, Unit::Force::Micronewton>::ToStandard(
236  NumericType& value) noexcept {
237  value *= static_cast<NumericType>(0.000001L);
238 }
239 
240 template <>
241 template <typename NumericType>
242 inline constexpr void Conversion<Unit::Force, Unit::Force::Nanonewton>::FromStandard(
243  NumericType& value) noexcept {
244  value *= static_cast<NumericType>(1000000000.0L);
245 }
246 
247 template <>
248 template <typename NumericType>
249 inline constexpr void Conversion<Unit::Force, Unit::Force::Nanonewton>::ToStandard(
250  NumericType& value) noexcept {
251  value *= static_cast<NumericType>(0.000000001L);
252 }
253 
254 template <>
255 template <typename NumericType>
256 inline constexpr void Conversion<Unit::Force, Unit::Force::Dyne>::FromStandard(
257  NumericType& value) noexcept {
258  value *= static_cast<NumericType>(100000.0L);
259 }
260 
261 template <>
262 template <typename NumericType>
263 inline constexpr void Conversion<Unit::Force, Unit::Force::Dyne>::ToStandard(
264  NumericType& value) noexcept {
265  value *= static_cast<NumericType>(0.00001L);
266 }
267 
268 template <>
269 template <typename NumericType>
270 inline constexpr void Conversion<Unit::Force, Unit::Force::Pound>::FromStandard(
271  NumericType& value) noexcept {
272  value /= (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
273 }
274 
275 template <>
276 template <typename NumericType>
277 inline constexpr void Conversion<Unit::Force, Unit::Force::Pound>::ToStandard(
278  NumericType& value) noexcept {
279  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L);
280 }
281 
282 template <typename NumericType>
283 inline const std::map<Unit::Force, std::function<void(NumericType* values, const std::size_t size)>>
284  MapOfConversionsFromStandard<Unit::Force, NumericType>{
286  Conversions<Unit::Force, Unit::Force::Newton>::FromStandard<NumericType> },
288  Conversions<Unit::Force, Unit::Force::Kilonewton>::FromStandard<NumericType> },
290  Conversions<Unit::Force, Unit::Force::Meganewton>::FromStandard<NumericType> },
292  Conversions<Unit::Force, Unit::Force::Giganewton>::FromStandard<NumericType> },
294  Conversions<Unit::Force, Unit::Force::Millinewton>::FromStandard<NumericType>},
296  Conversions<Unit::Force, Unit::Force::Micronewton>::FromStandard<NumericType>},
298  Conversions<Unit::Force, Unit::Force::Nanonewton>::FromStandard<NumericType> },
299  {Unit::Force::Dyne, Conversions<Unit::Force, Unit::Force::Dyne>::FromStandard<NumericType> },
301  Conversions<Unit::Force, Unit::Force::Pound>::FromStandard<NumericType> },
302 };
303 
304 template <typename NumericType>
305 inline const std::map<Unit::Force,
306  std::function<void(NumericType* const values, const std::size_t size)>>
307  MapOfConversionsToStandard<Unit::Force, NumericType>{
309  Conversions<Unit::Force, Unit::Force::Newton>::ToStandard<NumericType> },
311  Conversions<Unit::Force, Unit::Force::Kilonewton>::ToStandard<NumericType> },
313  Conversions<Unit::Force, Unit::Force::Meganewton>::ToStandard<NumericType> },
315  Conversions<Unit::Force, Unit::Force::Giganewton>::ToStandard<NumericType> },
317  Conversions<Unit::Force, Unit::Force::Millinewton>::ToStandard<NumericType>},
319  Conversions<Unit::Force, Unit::Force::Micronewton>::ToStandard<NumericType>},
321  Conversions<Unit::Force, Unit::Force::Nanonewton>::ToStandard<NumericType> },
322  {Unit::Force::Dyne, Conversions<Unit::Force, Unit::Force::Dyne>::ToStandard<NumericType> },
323  {Unit::Force::Pound, Conversions<Unit::Force, Unit::Force::Pound>::ToStandard<NumericType> },
324 };
325 
326 } // namespace Internal
327 
328 } // namespace PhQ
329 
330 #endif // PHQ_UNIT_FORCE_HPP
ElectricCurrent
Electric current units.
Force
Force units.
Definition: Force.hpp:53
@ Millinewton
Millinewton (mN) force unit.
@ Nanonewton
Nanonewton (nN) force unit.
@ Newton
Newton (N) force unit.
@ Pound
Pound (lbf) force unit.
@ Giganewton
Giganewton (GN) force unit.
@ Meganewton
Meganewton (MN) force unit.
@ Dyne
Dyne (dyn) force unit.
@ Kilonewton
Kilonewton (kN) force unit.
@ Micronewton
Micronewton (μN) force 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)