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