Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Frequency.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_FREQUENCY_HPP
26 #define PHQ_UNIT_FREQUENCY_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 Frequency units.
53 enum class Frequency : int8_t {
54  /// \brief Hertz (Hz) frequency unit.
55  Hertz,
56 
57  /// \brief Kilohertz (kHz) frequency unit.
58  Kilohertz,
59 
60  /// \brief Megahertz (MHz) frequency unit.
61  Megahertz,
62 
63  /// \brief Gigahertz (GHz) frequency unit.
64  Gigahertz,
65 
66  /// \brief Per minute (/min) frequency unit.
67  PerMinute,
68 
69  /// \brief Per hour (/hr) frequency unit.
70  PerHour,
71 };
72 
73 } // namespace Unit
74 
75 /// \brief Standard frequency unit: hertz (Hz).
76 template <>
77 inline constexpr const Unit::Frequency Standard<Unit::Frequency>{Unit::Frequency::Hertz};
78 
79 /// \brief Physical dimension set of frequency units.
80 template <>
81 inline constexpr const Dimensions RelatedDimensions<Unit::Frequency>{
82  Dimensions{Dimension::Time{-1}, Dimension::Length{0}, Dimension::Mass{0},
84  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
85 };
86 
87 inline std::ostream& operator<<(std::ostream& stream, const Unit::Frequency unit) {
88  stream << Abbreviation(unit);
89  return stream;
90 }
91 
92 namespace Internal {
93 
94 template <>
95 inline const std::map<UnitSystem, Unit::Frequency> ConsistentUnits<Unit::Frequency>{
100 };
101 
102 template <>
103 inline const std::map<Unit::Frequency, UnitSystem> RelatedUnitSystems<Unit::Frequency>{};
104 
105 template <>
106 inline const std::map<Unit::Frequency, std::string_view> Abbreviations<Unit::Frequency>{
107  {Unit::Frequency::Hertz, "Hz" },
108  {Unit::Frequency::Kilohertz, "kHz" },
109  {Unit::Frequency::Megahertz, "MHz" },
110  {Unit::Frequency::Gigahertz, "GHz" },
111  {Unit::Frequency::PerMinute, "/min"},
112  {Unit::Frequency::PerHour, "/hr" },
113 };
114 
115 template <>
116 inline const std::unordered_map<std::string_view, Unit::Frequency> Spellings<Unit::Frequency>{
117  {"Hz", Unit::Frequency::Hertz },
118  {"/s", Unit::Frequency::Hertz },
119  {"1/s", Unit::Frequency::Hertz },
123  {"/min", Unit::Frequency::PerMinute},
124  {"1/min", Unit::Frequency::PerMinute},
125  {"/hr", Unit::Frequency::PerHour },
126  {"1/hr", Unit::Frequency::PerHour },
127 };
128 
129 template <>
130 template <typename NumericType>
131 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Hertz>::FromStandard(
132  NumericType& /*value*/) noexcept {}
133 
134 template <>
135 template <typename NumericType>
136 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Hertz>::ToStandard(
137  NumericType& /*value*/) noexcept {}
138 
139 template <>
140 template <typename NumericType>
141 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Kilohertz>::FromStandard(
142  NumericType& value) noexcept {
143  value *= static_cast<NumericType>(0.001L);
144 }
145 
146 template <>
147 template <typename NumericType>
148 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Kilohertz>::ToStandard(
149  NumericType& value) noexcept {
150  value *= static_cast<NumericType>(1000.0L);
151 }
152 
153 template <>
154 template <typename NumericType>
155 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Megahertz>::FromStandard(
156  NumericType& value) noexcept {
157  value *= static_cast<NumericType>(0.000001L);
158 }
159 
160 template <>
161 template <typename NumericType>
162 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Megahertz>::ToStandard(
163  NumericType& value) noexcept {
164  value *= static_cast<NumericType>(1000000.0L);
165 }
166 
167 template <>
168 template <typename NumericType>
169 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Gigahertz>::FromStandard(
170  NumericType& value) noexcept {
171  value *= static_cast<NumericType>(0.000000001L);
172 }
173 
174 template <>
175 template <typename NumericType>
176 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Gigahertz>::ToStandard(
177  NumericType& value) noexcept {
178  value *= static_cast<NumericType>(1000000000.0L);
179 }
180 
181 template <>
182 template <typename NumericType>
183 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerMinute>::FromStandard(
184  NumericType& value) noexcept {
185  value *= static_cast<NumericType>(60.0L);
186 }
187 
188 template <>
189 template <typename NumericType>
190 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerMinute>::ToStandard(
191  NumericType& value) noexcept {
192  value /= static_cast<NumericType>(60.0L);
193 }
194 
195 template <>
196 template <typename NumericType>
197 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerHour>::FromStandard(
198  NumericType& value) noexcept {
199  value *= static_cast<NumericType>(3600.0L);
200 }
201 
202 template <>
203 template <typename NumericType>
204 inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerHour>::ToStandard(
205  NumericType& value) noexcept {
206  value /= static_cast<NumericType>(3600.0L);
207 }
208 
209 template <typename NumericType>
210 inline const std::map<Unit::Frequency,
211  std::function<void(NumericType* values, const std::size_t size)>>
212  MapOfConversionsFromStandard<Unit::Frequency, NumericType>{
214  Conversions<Unit::Frequency, Unit::Frequency::Hertz>::FromStandard<NumericType> },
216  Conversions<Unit::Frequency, Unit::Frequency::Kilohertz>::FromStandard<NumericType>},
218  Conversions<Unit::Frequency, Unit::Frequency::Megahertz>::FromStandard<NumericType>},
220  Conversions<Unit::Frequency, Unit::Frequency::Gigahertz>::FromStandard<NumericType>},
222  Conversions<Unit::Frequency, Unit::Frequency::PerMinute>::FromStandard<NumericType>},
224  Conversions<Unit::Frequency, Unit::Frequency::PerHour>::FromStandard<NumericType> },
225 };
226 
227 template <typename NumericType>
228 inline const std::map<Unit::Frequency,
229  std::function<void(NumericType* const values, const std::size_t size)>>
230  MapOfConversionsToStandard<Unit::Frequency, NumericType>{
232  Conversions<Unit::Frequency, Unit::Frequency::Hertz>::ToStandard<NumericType> },
234  Conversions<Unit::Frequency, Unit::Frequency::Kilohertz>::ToStandard<NumericType>},
236  Conversions<Unit::Frequency, Unit::Frequency::Megahertz>::ToStandard<NumericType>},
238  Conversions<Unit::Frequency, Unit::Frequency::Gigahertz>::ToStandard<NumericType>},
240  Conversions<Unit::Frequency, Unit::Frequency::PerMinute>::ToStandard<NumericType>},
242  Conversions<Unit::Frequency, Unit::Frequency::PerHour>::ToStandard<NumericType> },
243 };
244 
245 } // namespace Internal
246 
247 } // namespace PhQ
248 
249 #endif // PHQ_UNIT_FREQUENCY_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
Frequency
Frequency units.
Definition: Frequency.hpp:53
@ Megahertz
Megahertz (MHz) frequency unit.
@ Gigahertz
Gigahertz (GHz) frequency unit.
@ PerMinute
Per minute (/min) frequency unit.
@ PerHour
Per hour (/hr) frequency unit.
@ Kilohertz
Kilohertz (kHz) frequency unit.
@ Hertz
Hertz (Hz) frequency unit.
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)