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
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Force units.
53enum class Force : int8_t {
54 /// \brief Newton (N) force unit.
55 Newton,
56
57 /// \brief Kilonewton (kN) force unit.
59
60 /// \brief Meganewton (MN) force unit.
62
63 /// \brief Giganewton (GN) force unit.
65
66 /// \brief Millinewton (mN) force unit.
68
69 /// \brief Micronewton (μN) force unit.
71
72 /// \brief Nanonewton (nN) force unit.
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).
85template <>
86inline constexpr const Unit::Force Standard<Unit::Force>{Unit::Force::Newton};
87
88/// \brief Physical dimension set of force units.
89template <>
90inline constexpr const Dimensions RelatedDimensions<Unit::Force>{
91 Dimensions{Dimension::Time{-2}, Dimension::Length{1}, Dimension::Mass{1},
92 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
93 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
94};
95
96inline std::ostream& operator<<(std::ostream& stream, const Unit::Force unit) {
97 stream << Abbreviation(unit);
98 return stream;
99}
100
101namespace Internal {
102
103template <>
104inline const std::map<UnitSystem, Unit::Force> ConsistentUnits<Unit::Force>{
109};
110
111template <>
112inline const std::map<Unit::Force, UnitSystem> RelatedUnitSystems<Unit::Force>{
115};
116
117// clang-format off
118
119template <>
120inline const std::map<Unit::Force, std::string_view> Abbreviations<Unit::Force>{
121 {Unit::Force::Newton, "N" },
126 {Unit::Force::Micronewton, "μN" },
128 {Unit::Force::Dyne, "dyn"},
129 {Unit::Force::Pound, "lbf"},
130};
131
132template <>
133inline 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 },
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},
153 {"dyn", Unit::Force::Dyne },
154 {"lbf", Unit::Force::Pound },
155 {"lb", Unit::Force::Pound },
156};
157
158// clang-format on
159
160template <>
161template <typename NumericType>
162inline constexpr void Conversion<Unit::Force, Unit::Force::Newton>::FromStandard(
163 NumericType& /*value*/) noexcept {}
164
165template <>
166template <typename NumericType>
167inline constexpr void Conversion<Unit::Force, Unit::Force::Newton>::ToStandard(
168 NumericType& /*value*/) noexcept {}
169
170template <>
171template <typename NumericType>
172inline constexpr void Conversion<Unit::Force, Unit::Force::Kilonewton>::FromStandard(
173 NumericType& value) noexcept {
174 value *= static_cast<NumericType>(0.001L);
175}
176
177template <>
178template <typename NumericType>
179inline constexpr void Conversion<Unit::Force, Unit::Force::Kilonewton>::ToStandard(
180 NumericType& value) noexcept {
181 value *= static_cast<NumericType>(1000.0L);
182}
183
184template <>
185template <typename NumericType>
186inline constexpr void Conversion<Unit::Force, Unit::Force::Meganewton>::FromStandard(
187 NumericType& value) noexcept {
188 value *= static_cast<NumericType>(0.000001L);
189}
190
191template <>
192template <typename NumericType>
193inline constexpr void Conversion<Unit::Force, Unit::Force::Meganewton>::ToStandard(
194 NumericType& value) noexcept {
195 value *= static_cast<NumericType>(1000000.0L);
196}
197
198template <>
199template <typename NumericType>
200inline constexpr void Conversion<Unit::Force, Unit::Force::Giganewton>::FromStandard(
201 NumericType& value) noexcept {
202 value *= static_cast<NumericType>(0.000000001L);
203}
204
205template <>
206template <typename NumericType>
207inline constexpr void Conversion<Unit::Force, Unit::Force::Giganewton>::ToStandard(
208 NumericType& value) noexcept {
209 value *= static_cast<NumericType>(1000000000.0L);
210}
211
212template <>
213template <typename NumericType>
214inline constexpr void Conversion<Unit::Force, Unit::Force::Millinewton>::FromStandard(
215 NumericType& value) noexcept {
216 value *= static_cast<NumericType>(1000.0L);
217}
218
219template <>
220template <typename NumericType>
221inline constexpr void Conversion<Unit::Force, Unit::Force::Millinewton>::ToStandard(
222 NumericType& value) noexcept {
223 value *= static_cast<NumericType>(0.001L);
224}
225
226template <>
227template <typename NumericType>
228inline constexpr void Conversion<Unit::Force, Unit::Force::Micronewton>::FromStandard(
229 NumericType& value) noexcept {
230 value *= static_cast<NumericType>(1000000.0L);
231}
232
233template <>
234template <typename NumericType>
235inline constexpr void Conversion<Unit::Force, Unit::Force::Micronewton>::ToStandard(
236 NumericType& value) noexcept {
237 value *= static_cast<NumericType>(0.000001L);
238}
239
240template <>
241template <typename NumericType>
242inline constexpr void Conversion<Unit::Force, Unit::Force::Nanonewton>::FromStandard(
243 NumericType& value) noexcept {
244 value *= static_cast<NumericType>(1000000000.0L);
245}
246
247template <>
248template <typename NumericType>
249inline constexpr void Conversion<Unit::Force, Unit::Force::Nanonewton>::ToStandard(
250 NumericType& value) noexcept {
251 value *= static_cast<NumericType>(0.000000001L);
252}
253
254template <>
255template <typename NumericType>
256inline constexpr void Conversion<Unit::Force, Unit::Force::Dyne>::FromStandard(
257 NumericType& value) noexcept {
258 value *= static_cast<NumericType>(100000.0L);
259}
260
261template <>
262template <typename NumericType>
263inline constexpr void Conversion<Unit::Force, Unit::Force::Dyne>::ToStandard(
264 NumericType& value) noexcept {
265 value *= static_cast<NumericType>(0.00001L);
266}
267
268template <>
269template <typename NumericType>
270inline 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
275template <>
276template <typename NumericType>
277inline 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
282template <typename NumericType>
283inline 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> },
300 {Unit::Force::Pound, Conversions<Unit::Force, Unit::Force::Pound>::FromStandard<NumericType> },
301};
302
303template <typename NumericType>
304inline const std::map<Unit::Force,
305 std::function<void(NumericType* const values, const std::size_t size)>>
306 MapOfConversionsToStandard<Unit::Force, NumericType>{
307 {Unit::Force::Newton, Conversions<Unit::Force, Unit::Force::Newton>::ToStandard<NumericType> },
309 Conversions<Unit::Force, Unit::Force::Kilonewton>::ToStandard<NumericType> },
311 Conversions<Unit::Force, Unit::Force::Meganewton>::ToStandard<NumericType> },
313 Conversions<Unit::Force, Unit::Force::Giganewton>::ToStandard<NumericType> },
315 Conversions<Unit::Force, Unit::Force::Millinewton>::ToStandard<NumericType>},
317 Conversions<Unit::Force, Unit::Force::Micronewton>::ToStandard<NumericType>},
319 Conversions<Unit::Force, Unit::Force::Nanonewton>::ToStandard<NumericType> },
320 {Unit::Force::Dyne, Conversions<Unit::Force, Unit::Force::Dyne>::ToStandard<NumericType> },
321 {Unit::Force::Pound, Conversions<Unit::Force, Unit::Force::Pound>::ToStandard<NumericType> },
322};
323
324} // namespace Internal
325
326} // namespace PhQ
327
328#endif // PHQ_UNIT_FORCE_HPP
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.
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