Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Pressure.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_PRESSURE_HPP
26 #define PHQ_UNIT_PRESSURE_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 Pressure units.
53 enum class Pressure : int8_t {
54  /// \brief Pascal (Pa) pressure unit.
55  Pascal,
56 
57  /// \brief Kilopascal (kPa) pressure unit.
58  Kilopascal,
59 
60  /// \brief Megapascal (MPa) pressure unit.
61  Megapascal,
62 
63  /// \brief Gigapascal (GPa) pressure unit.
64  Gigapascal,
65 
66  /// \brief Bar (bar) pressure unit.
67  Bar,
68 
69  /// \brief Atmosphere (atm) pressure unit.
70  Atmosphere,
71 
72  /// \brief Pound per square foot (lbf/ft^2) pressure unit.
74 
75  /// \brief Pound per square inch (lbf/in^2) pressure unit.
77 };
78 
79 } // namespace Unit
80 
81 /// \brief Standard pressure unit: pascal (Pa).
82 template <>
83 inline constexpr const Unit::Pressure Standard<Unit::Pressure>{Unit::Pressure::Pascal};
84 
85 /// \brief Physical dimension set of pressure units.
86 template <>
87 inline constexpr const Dimensions RelatedDimensions<Unit::Pressure>{
88  Dimensions{Dimension::Time{-2}, Dimension::Length{-1}, Dimension::Mass{1},
90  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
91 };
92 
93 inline std::ostream& operator<<(std::ostream& stream, const Unit::Pressure unit) {
94  stream << Abbreviation(unit);
95  return stream;
96 }
97 
98 namespace Internal {
99 
100 template <>
101 inline const std::map<UnitSystem, Unit::Pressure> ConsistentUnits<Unit::Pressure>{
106 };
107 
108 template <>
109 inline const std::map<Unit::Pressure, UnitSystem> RelatedUnitSystems<Unit::Pressure>{
112 };
113 
114 template <>
115 inline const std::map<Unit::Pressure, std::string_view> Abbreviations<Unit::Pressure>{
116  {Unit::Pressure::Pascal, "Pa" },
117  {Unit::Pressure::Kilopascal, "kPa" },
118  {Unit::Pressure::Megapascal, "MPa" },
119  {Unit::Pressure::Gigapascal, "GPa" },
120  {Unit::Pressure::Bar, "bar" },
121  {Unit::Pressure::Atmosphere, "atm" },
124 };
125 
126 // clang-format off
127 
128 template <>
129 inline const std::unordered_map<std::string_view, Unit::Pressure> Spellings<Unit::Pressure>{
130  {"Pa", Unit::Pressure::Pascal },
131  {"N/m^2", Unit::Pressure::Pascal },
132  {"N/m2", Unit::Pressure::Pascal },
133  {"kg/(m·s^2)", Unit::Pressure::Pascal },
134  {"kg/(m·s2)", Unit::Pressure::Pascal },
135  {"kg/(m*s^2)", Unit::Pressure::Pascal },
136  {"kg/(m*s2)", Unit::Pressure::Pascal },
137  {"kg/m/s^2", Unit::Pressure::Pascal },
138  {"kg/m/s2", Unit::Pressure::Pascal },
139  {"kPa", Unit::Pressure::Kilopascal },
140  {"kN/m^2", Unit::Pressure::Kilopascal },
141  {"kN/m2", Unit::Pressure::Kilopascal },
142  {"MPa", Unit::Pressure::Megapascal },
143  {"N/mm^2", Unit::Pressure::Megapascal },
144  {"N/mm2", Unit::Pressure::Megapascal },
145  {"MN/m^2", Unit::Pressure::Megapascal },
146  {"MN/m2", Unit::Pressure::Megapascal },
147  {"GPa", Unit::Pressure::Gigapascal },
148  {"GN/m^2", Unit::Pressure::Gigapascal },
149  {"GN/m2", Unit::Pressure::Gigapascal },
150  {"kN/mm^2", Unit::Pressure::Gigapascal },
151  {"kN/mm2", Unit::Pressure::Gigapascal },
152  {"bar", Unit::Pressure::Bar },
153  {"atm", Unit::Pressure::Atmosphere },
154  {"atmosphere", Unit::Pressure::Atmosphere },
165 };
166 
167 // clang-format on
168 
169 template <>
170 template <typename NumericType>
171 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Pascal>::FromStandard(
172  NumericType& /*value*/) noexcept {}
173 
174 template <>
175 template <typename NumericType>
176 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Pascal>::ToStandard(
177  NumericType& /*value*/) noexcept {}
178 
179 template <>
180 template <typename NumericType>
181 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Kilopascal>::FromStandard(
182  NumericType& value) noexcept {
183  value *= static_cast<NumericType>(0.001L);
184 }
185 
186 template <>
187 template <typename NumericType>
188 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Kilopascal>::ToStandard(
189  NumericType& value) noexcept {
190  value *= static_cast<NumericType>(1000.0L);
191 }
192 
193 template <>
194 template <typename NumericType>
195 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Megapascal>::FromStandard(
196  NumericType& value) noexcept {
197  value *= static_cast<NumericType>(0.000001L);
198 }
199 
200 template <>
201 template <typename NumericType>
202 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Megapascal>::ToStandard(
203  NumericType& value) noexcept {
204  value *= static_cast<NumericType>(1000000.0L);
205 }
206 
207 template <>
208 template <typename NumericType>
209 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Gigapascal>::FromStandard(
210  NumericType& value) noexcept {
211  value *= static_cast<NumericType>(0.000000001L);
212 }
213 
214 template <>
215 template <typename NumericType>
216 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Gigapascal>::ToStandard(
217  NumericType& value) noexcept {
218  value *= static_cast<NumericType>(1000000000.0L);
219 }
220 
221 template <>
222 template <typename NumericType>
223 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Bar>::FromStandard(
224  NumericType& value) noexcept {
225  value *= static_cast<NumericType>(0.00001L);
226 }
227 
228 template <>
229 template <typename NumericType>
230 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Bar>::ToStandard(
231  NumericType& value) noexcept {
232  value *= static_cast<NumericType>(100000.0L);
233 }
234 
235 template <>
236 template <typename NumericType>
237 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Atmosphere>::FromStandard(
238  NumericType& value) noexcept {
239  value /= static_cast<NumericType>(101325.0L);
240 }
241 
242 template <>
243 template <typename NumericType>
244 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::Atmosphere>::ToStandard(
245  NumericType& value) noexcept {
246  value *= static_cast<NumericType>(101325.0L);
247 }
248 
249 template <>
250 template <typename NumericType>
251 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::PoundPerSquareFoot>::FromStandard(
252  NumericType& value) noexcept {
253  value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
254  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
255 }
256 
257 template <>
258 template <typename NumericType>
259 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::PoundPerSquareFoot>::ToStandard(
260  NumericType& value) noexcept {
261  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
262  / (static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L));
263 }
264 
265 template <>
266 template <typename NumericType>
267 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::PoundPerSquareInch>::FromStandard(
268  NumericType& value) noexcept {
269  value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
270  / (static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L));
271 }
272 
273 template <>
274 template <typename NumericType>
275 inline constexpr void Conversion<Unit::Pressure, Unit::Pressure::PoundPerSquareInch>::ToStandard(
276  NumericType& value) noexcept {
277  value *= static_cast<NumericType>(0.45359237L) * static_cast<NumericType>(9.80665L)
278  / (static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L));
279 }
280 
281 template <typename NumericType>
282 inline const std::map<Unit::Pressure,
283  std::function<void(NumericType* values, const std::size_t size)>>
284  MapOfConversionsFromStandard<Unit::Pressure, NumericType>{
286  Conversions<Unit::Pressure, Unit::Pressure::Pascal>::FromStandard<NumericType> },
288  Conversions<Unit::Pressure, Unit::Pressure::Kilopascal>::FromStandard<NumericType>},
290  Conversions<Unit::Pressure, Unit::Pressure::Megapascal>::FromStandard<NumericType>},
292  Conversions<Unit::Pressure, Unit::Pressure::Gigapascal>::FromStandard<NumericType>},
294  Conversions<Unit::Pressure, Unit::Pressure::Bar>::FromStandard<NumericType> },
296  Conversions<Unit::Pressure, Unit::Pressure::Atmosphere>::FromStandard<NumericType>},
298  Conversions<Unit::Pressure,
299  Unit::Pressure::PoundPerSquareFoot>::FromStandard<NumericType> },
301  Conversions<Unit::Pressure,
302  Unit::Pressure::PoundPerSquareInch>::FromStandard<NumericType> },
303 };
304 
305 template <typename NumericType>
306 inline const std::map<Unit::Pressure,
307  std::function<void(NumericType* const values, const std::size_t size)>>
308  MapOfConversionsToStandard<Unit::Pressure, NumericType>{
310  Conversions<Unit::Pressure, Unit::Pressure::Pascal>::ToStandard<NumericType> },
312  Conversions<Unit::Pressure, Unit::Pressure::Kilopascal>::ToStandard<NumericType> },
314  Conversions<Unit::Pressure, Unit::Pressure::Megapascal>::ToStandard<NumericType> },
316  Conversions<Unit::Pressure, Unit::Pressure::Gigapascal>::ToStandard<NumericType> },
318  Conversions<Unit::Pressure, Unit::Pressure::Bar>::ToStandard<NumericType> },
320  Conversions<Unit::Pressure, Unit::Pressure::Atmosphere>::ToStandard<NumericType> },
322  Conversions<Unit::Pressure, Unit::Pressure::PoundPerSquareFoot>::ToStandard<NumericType>},
324  Conversions<Unit::Pressure, Unit::Pressure::PoundPerSquareInch>::ToStandard<NumericType>},
325 };
326 
327 } // namespace Internal
328 
329 } // namespace PhQ
330 
331 #endif // PHQ_UNIT_PRESSURE_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
Pressure
Pressure units.
Definition: Pressure.hpp:53
@ PoundPerSquareInch
Pound per square inch (lbf/in^2) pressure unit.
@ Megapascal
Megapascal (MPa) pressure unit.
@ Kilopascal
Kilopascal (kPa) pressure unit.
@ Atmosphere
Atmosphere (atm) pressure unit.
@ Gigapascal
Gigapascal (GPa) pressure unit.
@ Pascal
Pascal (Pa) pressure unit.
@ PoundPerSquareFoot
Pound per square foot (lbf/ft^2) pressure unit.
@ Bar
Bar (bar) pressure unit.
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)