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
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Power units.
53enum class Power : int8_t {
54 /// \brief Watt (W) power unit.
55 Watt,
56
57 /// \brief Milliwatt (mW) power unit.
59
60 /// \brief Microwatt (μW) power unit.
62
63 /// \brief Nanowatt (nW) power unit.
65
66 /// \brief Kilowatt (kW) power unit.
68
69 /// \brief Megawatt (MW) power unit.
71
72 /// \brief Gigawatt (GW) power unit.
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).
85template <>
86inline constexpr const Unit::Power Standard<Unit::Power>{Unit::Power::Watt};
87
88/// \brief Physical dimension set of power units.
89template <>
90inline constexpr const Dimensions RelatedDimensions<Unit::Power>{
91 Dimensions{Dimension::Time{-3}, Dimension::Length{2}, 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::Power unit) {
97 stream << Abbreviation(unit);
98 return stream;
99}
100
101namespace Internal {
102
103template <>
104inline const std::map<UnitSystem, Unit::Power> ConsistentUnits<Unit::Power>{
109};
110
111template <>
112inline const std::map<Unit::Power, UnitSystem> RelatedUnitSystems<Unit::Power>{
117};
118
119// clang-format off
120
121template <>
122inline 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
134template <>
135inline 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},
170 {"in·lbf/s", Unit::Power::InchPoundPerSecond},
174};
175
176// clang-format on
177
178template <>
179template <typename NumericType>
180inline constexpr void Conversion<Unit::Power, Unit::Power::Watt>::FromStandard(
181 NumericType& /*value*/) noexcept {}
182
183template <>
184template <typename NumericType>
185inline constexpr void Conversion<Unit::Power, Unit::Power::Watt>::ToStandard(
186 NumericType& /*value*/) noexcept {}
187
188template <>
189template <typename NumericType>
190inline constexpr void Conversion<Unit::Power, Unit::Power::Milliwatt>::FromStandard(
191 NumericType& value) noexcept {
192 value *= static_cast<NumericType>(1000.0L);
193}
194
195template <>
196template <typename NumericType>
197inline constexpr void Conversion<Unit::Power, Unit::Power::Milliwatt>::ToStandard(
198 NumericType& value) noexcept {
199 value *= static_cast<NumericType>(0.001L);
200}
201
202template <>
203template <typename NumericType>
204inline constexpr void Conversion<Unit::Power, Unit::Power::Microwatt>::FromStandard(
205 NumericType& value) noexcept {
206 value *= static_cast<NumericType>(1000000.0L);
207}
208
209template <>
210template <typename NumericType>
211inline constexpr void Conversion<Unit::Power, Unit::Power::Microwatt>::ToStandard(
212 NumericType& value) noexcept {
213 value *= static_cast<NumericType>(0.000001L);
214}
215
216template <>
217template <typename NumericType>
218inline constexpr void Conversion<Unit::Power, Unit::Power::Nanowatt>::FromStandard(
219 NumericType& value) noexcept {
220 value *= static_cast<NumericType>(1000000000.0L);
221}
222
223template <>
224template <typename NumericType>
225inline constexpr void Conversion<Unit::Power, Unit::Power::Nanowatt>::ToStandard(
226 NumericType& value) noexcept {
227 value *= static_cast<NumericType>(0.000000001L);
228}
229
230template <>
231template <typename NumericType>
232inline constexpr void Conversion<Unit::Power, Unit::Power::Kilowatt>::FromStandard(
233 NumericType& value) noexcept {
234 value *= static_cast<NumericType>(0.001L);
235}
236
237template <>
238template <typename NumericType>
239inline constexpr void Conversion<Unit::Power, Unit::Power::Kilowatt>::ToStandard(
240 NumericType& value) noexcept {
241 value *= static_cast<NumericType>(1000.0L);
242}
243
244template <>
245template <typename NumericType>
246inline constexpr void Conversion<Unit::Power, Unit::Power::Megawatt>::FromStandard(
247 NumericType& value) noexcept {
248 value *= static_cast<NumericType>(0.000001L);
249}
250
251template <>
252template <typename NumericType>
253inline constexpr void Conversion<Unit::Power, Unit::Power::Megawatt>::ToStandard(
254 NumericType& value) noexcept {
255 value *= static_cast<NumericType>(1000000.0L);
256}
257
258template <>
259template <typename NumericType>
260inline constexpr void Conversion<Unit::Power, Unit::Power::Gigawatt>::FromStandard(
261 NumericType& value) noexcept {
262 value *= static_cast<NumericType>(0.000000001L);
263}
264
265template <>
266template <typename NumericType>
267inline constexpr void Conversion<Unit::Power, Unit::Power::Gigawatt>::ToStandard(
268 NumericType& value) noexcept {
269 value *= static_cast<NumericType>(1000000000.0L);
270}
271
272template <>
273template <typename NumericType>
274inline 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
280template <>
281template <typename NumericType>
282inline 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
288template <>
289template <typename NumericType>
290inline 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
296template <>
297template <typename NumericType>
298inline 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
304template <typename NumericType>
305inline 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
326template <typename NumericType>
327inline const std::
328 map<Unit::Power, 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
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.
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