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
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Frequency units.
53enum class Frequency : int8_t {
54 /// \brief Hertz (Hz) frequency unit.
55 Hertz,
56
57 /// \brief Kilohertz (kHz) frequency unit.
59
60 /// \brief Megahertz (MHz) frequency unit.
62
63 /// \brief Gigahertz (GHz) frequency unit.
65
66 /// \brief Per minute (/min) frequency unit.
68
69 /// \brief Per hour (/hr) frequency unit.
70 PerHour,
71};
72
73} // namespace Unit
74
75/// \brief Standard frequency unit: hertz (Hz).
76template <>
77inline constexpr const Unit::Frequency Standard<Unit::Frequency>{Unit::Frequency::Hertz};
78
79/// \brief Physical dimension set of frequency units.
80template <>
81inline constexpr const Dimensions RelatedDimensions<Unit::Frequency>{
82 Dimensions{Dimension::Time{-1}, Dimension::Length{0}, Dimension::Mass{0},
83 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
84 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
85};
86
87inline std::ostream& operator<<(std::ostream& stream, const Unit::Frequency unit) {
88 stream << Abbreviation(unit);
89 return stream;
90}
91
92namespace Internal {
93
94template <>
95inline const std::map<UnitSystem, Unit::Frequency> ConsistentUnits<Unit::Frequency>{
100};
101
102template <>
103inline const std::map<Unit::Frequency, UnitSystem> RelatedUnitSystems<Unit::Frequency>{};
104
105template <>
106inline const std::map<Unit::Frequency, std::string_view> Abbreviations<Unit::Frequency>{
107 {Unit::Frequency::Hertz, "Hz" },
112 {Unit::Frequency::PerHour, "/hr" },
113};
114
115template <>
116inline 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 },
125 {"/hr", Unit::Frequency::PerHour },
126 {"1/hr", Unit::Frequency::PerHour },
127};
128
129template <>
130template <typename NumericType>
131inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Hertz>::FromStandard(
132 NumericType& /*value*/) noexcept {}
133
134template <>
135template <typename NumericType>
136inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Hertz>::ToStandard(
137 NumericType& /*value*/) noexcept {}
138
139template <>
140template <typename NumericType>
141inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Kilohertz>::FromStandard(
142 NumericType& value) noexcept {
143 value *= static_cast<NumericType>(0.001L);
144}
145
146template <>
147template <typename NumericType>
148inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Kilohertz>::ToStandard(
149 NumericType& value) noexcept {
150 value *= static_cast<NumericType>(1000.0L);
151}
152
153template <>
154template <typename NumericType>
155inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Megahertz>::FromStandard(
156 NumericType& value) noexcept {
157 value *= static_cast<NumericType>(0.000001L);
158}
159
160template <>
161template <typename NumericType>
162inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Megahertz>::ToStandard(
163 NumericType& value) noexcept {
164 value *= static_cast<NumericType>(1000000.0L);
165}
166
167template <>
168template <typename NumericType>
169inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Gigahertz>::FromStandard(
170 NumericType& value) noexcept {
171 value *= static_cast<NumericType>(0.000000001L);
172}
173
174template <>
175template <typename NumericType>
176inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::Gigahertz>::ToStandard(
177 NumericType& value) noexcept {
178 value *= static_cast<NumericType>(1000000000.0L);
179}
180
181template <>
182template <typename NumericType>
183inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerMinute>::FromStandard(
184 NumericType& value) noexcept {
185 value *= static_cast<NumericType>(60.0L);
186}
187
188template <>
189template <typename NumericType>
190inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerMinute>::ToStandard(
191 NumericType& value) noexcept {
192 value /= static_cast<NumericType>(60.0L);
193}
194
195template <>
196template <typename NumericType>
197inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerHour>::FromStandard(
198 NumericType& value) noexcept {
199 value *= static_cast<NumericType>(3600.0L);
200}
201
202template <>
203template <typename NumericType>
204inline constexpr void Conversion<Unit::Frequency, Unit::Frequency::PerHour>::ToStandard(
205 NumericType& value) noexcept {
206 value /= static_cast<NumericType>(3600.0L);
207}
208
209template <typename NumericType>
210inline const std::
211 map<Unit::Frequency, 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
227template <typename NumericType>
228inline const std::
229 map<Unit::Frequency, 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
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.
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