Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Temperature.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_TEMPERATURE_HPP
26 #define PHQ_UNIT_TEMPERATURE_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 Temperature units. Not to be confused with temperature difference units. For example, a
53 /// temperature of 20 °C corresponds to a temperature of 68 °F, whereas a temperature difference of
54 /// +20 °C corresponds to a temperature difference of +36 °F.
55 enum class Temperature : int8_t {
56  /// \brief Kelvin (K) temperature unit.
57  Kelvin,
58 
59  /// \brief Degree Celsius (°C) temperature unit.
60  Celsius,
61 
62  /// \brief Degree Rankine (°R) temperature unit.
63  Rankine,
64 
65  /// \brief Degree Fahrenheit (°F) temperature unit.
66  Fahrenheit,
67 };
68 
69 } // namespace Unit
70 
71 /// \brief Standard temperature unit: kelvin (K).
72 template <>
73 inline constexpr const Unit::Temperature Standard<Unit::Temperature>{Unit::Temperature::Kelvin};
74 
75 /// \brief Physical dimension set of temperature units.
76 template <>
77 inline constexpr const Dimensions RelatedDimensions<Unit::Temperature>{
80  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
81 };
82 
83 inline std::ostream& operator<<(std::ostream& stream, const Unit::Temperature unit) {
84  stream << Abbreviation(unit);
85  return stream;
86 }
87 
88 namespace Internal {
89 
90 template <>
91 inline const std::map<UnitSystem, Unit::Temperature> ConsistentUnits<Unit::Temperature>{
96 };
97 
98 template <>
99 inline const std::map<Unit::Temperature, UnitSystem> RelatedUnitSystems<Unit::Temperature>{};
100 
101 // clang-format off
102 
103 template <>
104 inline const std::map<Unit::Temperature, std::string_view> Abbreviations<Unit::Temperature>{
109 };
110 
111 template <>
112 inline const std::unordered_map<std::string_view, Unit::Temperature> Spellings<Unit::Temperature>{
114  {"°K", Unit::Temperature::Kelvin },
115  {"degK", Unit::Temperature::Kelvin },
116  {"°C", Unit::Temperature::Celsius },
118  {"degC", Unit::Temperature::Celsius },
119  {"°R", Unit::Temperature::Rankine },
121  {"degR", Unit::Temperature::Rankine },
125 };
126 
127 // clang-format on
128 
129 template <>
130 template <typename NumericType>
131 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Kelvin>::FromStandard(
132  NumericType& /*value*/) noexcept {}
133 
134 template <>
135 template <typename NumericType>
136 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Kelvin>::ToStandard(
137  NumericType& /*value*/) noexcept {}
138 
139 template <>
140 template <typename NumericType>
141 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Celsius>::FromStandard(
142  NumericType& value) noexcept {
143  value -= static_cast<NumericType>(273.15L);
144 }
145 
146 template <>
147 template <typename NumericType>
148 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Celsius>::ToStandard(
149  NumericType& value) noexcept {
150  value += static_cast<NumericType>(273.15L);
151 }
152 
153 template <>
154 template <typename NumericType>
155 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Rankine>::FromStandard(
156  NumericType& value) noexcept {
157  value *= static_cast<NumericType>(1.8L);
158 }
159 
160 template <>
161 template <typename NumericType>
162 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Rankine>::ToStandard(
163  NumericType& value) noexcept {
164  value /= static_cast<NumericType>(1.8L);
165 }
166 
167 template <>
168 template <typename NumericType>
169 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Fahrenheit>::FromStandard(
170  NumericType& value) noexcept {
171  value = static_cast<NumericType>(1.8L) * value - static_cast<NumericType>(459.67L);
172 }
173 
174 template <>
175 template <typename NumericType>
176 inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Fahrenheit>::ToStandard(
177  NumericType& value) noexcept {
178  value = (value + static_cast<NumericType>(459.67L)) / static_cast<NumericType>(1.8L);
179 }
180 
181 template <typename NumericType>
182 inline const std::map<Unit::Temperature,
183  std::function<void(NumericType* values, const std::size_t size)>>
184  MapOfConversionsFromStandard<Unit::Temperature, NumericType>{
186  Conversions<Unit::Temperature, Unit::Temperature::Kelvin>::FromStandard<NumericType> },
188  Conversions<Unit::Temperature, Unit::Temperature::Celsius>::FromStandard<NumericType> },
190  Conversions<Unit::Temperature, Unit::Temperature::Rankine>::FromStandard<NumericType> },
192  Conversions<Unit::Temperature, Unit::Temperature::Fahrenheit>::FromStandard<NumericType>},
193 };
194 
195 template <typename NumericType>
196 inline const std::map<Unit::Temperature,
197  std::function<void(NumericType* const values, const std::size_t size)>>
198  MapOfConversionsToStandard<Unit::Temperature, NumericType>{
200  Conversions<Unit::Temperature, Unit::Temperature::Kelvin>::ToStandard<NumericType> },
202  Conversions<Unit::Temperature, Unit::Temperature::Celsius>::ToStandard<NumericType> },
204  Conversions<Unit::Temperature, Unit::Temperature::Rankine>::ToStandard<NumericType> },
206  Conversions<Unit::Temperature, Unit::Temperature::Fahrenheit>::ToStandard<NumericType>},
207 };
208 
209 } // namespace Internal
210 
211 } // namespace PhQ
212 
213 #endif // PHQ_UNIT_TEMPERATURE_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
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
@ Celsius
Degree Celsius (°C) temperature unit.
@ Rankine
Degree Rankine (°R) temperature unit.
@ Kelvin
Kelvin (K) temperature unit.
@ Fahrenheit
Degree Fahrenheit (°F) temperature unit.
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)