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
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
48namespace PhQ {
49
50namespace 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.
55enum 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.
67};
68
69} // namespace Unit
70
71/// \brief Standard temperature unit: kelvin (K).
72template <>
73inline constexpr const Unit::Temperature Standard<Unit::Temperature>{Unit::Temperature::Kelvin};
74
75/// \brief Physical dimension set of temperature units.
76template <>
77inline constexpr const Dimensions RelatedDimensions<Unit::Temperature>{
78 Dimensions{Dimension::Time{0}, Dimension::Length{0}, Dimension::Mass{0},
79 Dimension::ElectricCurrent{0}, Dimension::Temperature{1},
80 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
81};
82
83inline std::ostream& operator<<(std::ostream& stream, const Unit::Temperature unit) {
84 stream << Abbreviation(unit);
85 return stream;
86}
87
88namespace Internal {
89
90template <>
91inline const std::map<UnitSystem, Unit::Temperature> ConsistentUnits<Unit::Temperature>{
96};
97
98template <>
99inline const std::map<Unit::Temperature, UnitSystem> RelatedUnitSystems<Unit::Temperature>{};
100
101// clang-format off
102
103template <>
104inline const std::map<Unit::Temperature, std::string_view> Abbreviations<Unit::Temperature>{
109};
110
111template <>
112inline const std::unordered_map<std::string_view, Unit::Temperature> Spellings<Unit::Temperature>{
115 {"degK", Unit::Temperature::Kelvin },
125};
126
127// clang-format on
128
129template <>
130template <typename NumericType>
131inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Kelvin>::FromStandard(
132 NumericType& /*value*/) noexcept {}
133
134template <>
135template <typename NumericType>
136inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Kelvin>::ToStandard(
137 NumericType& /*value*/) noexcept {}
138
139template <>
140template <typename NumericType>
141inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Celsius>::FromStandard(
142 NumericType& value) noexcept {
143 value -= static_cast<NumericType>(273.15L);
144}
145
146template <>
147template <typename NumericType>
148inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Celsius>::ToStandard(
149 NumericType& value) noexcept {
150 value += static_cast<NumericType>(273.15L);
151}
152
153template <>
154template <typename NumericType>
155inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Rankine>::FromStandard(
156 NumericType& value) noexcept {
157 value *= static_cast<NumericType>(1.8L);
158}
159
160template <>
161template <typename NumericType>
162inline constexpr void Conversion<Unit::Temperature, Unit::Temperature::Rankine>::ToStandard(
163 NumericType& value) noexcept {
164 value /= static_cast<NumericType>(1.8L);
165}
166
167template <>
168template <typename NumericType>
169inline 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
174template <>
175template <typename NumericType>
176inline 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
181template <typename NumericType>
182inline const std::
183 map<Unit::Temperature, 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>::
193 FromStandard<NumericType> },
194};
195
196template <typename NumericType>
197inline const std::
198 map<Unit::Temperature, std::function<void(NumericType* const values, const std::size_t size)>>
199 MapOfConversionsToStandard<Unit::Temperature, NumericType>{
201 Conversions<Unit::Temperature, Unit::Temperature::Kelvin>::ToStandard<NumericType> },
203 Conversions<Unit::Temperature, Unit::Temperature::Celsius>::ToStandard<NumericType> },
205 Conversions<Unit::Temperature, Unit::Temperature::Rankine>::ToStandard<NumericType> },
207 Conversions<Unit::Temperature, Unit::Temperature::Fahrenheit>::ToStandard<NumericType>},
208};
209
210} // namespace Internal
211
212} // namespace PhQ
213
214#endif // PHQ_UNIT_TEMPERATURE_HPP
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
@ Celsius
Degree Celsius (°C) temperature unit.
@ Rankine
Degree Rankine (°R) temperature unit.
@ Kelvin
Kelvin (K) temperature unit.
@ Fahrenheit
Degree Fahrenheit (°F) temperature 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