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
GasConstant.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_GAS_CONSTANT_HPP
26#define PHQ_GAS_CONSTANT_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "HeatCapacityRatio.hpp"
36#include "Unit/HeatCapacity.hpp"
37
38namespace PhQ {
39
40// Forward declaration for class PhQ::GasConstant.
41template <typename NumericType>
42class SpecificGasConstant;
43
44/// \brief Gas constant of a gas. For the mass-specific gas constant, see PhQ::SpecificGasConstant.
45template <typename NumericType = double>
46class GasConstant : public DimensionalScalar<Unit::HeatCapacity, NumericType> {
47public:
48 /// \brief Default constructor. Constructs a gas constant with an uninitialized value.
49 GasConstant() = default;
50
51 /// \brief Constructor. Constructs a gas constant with a given value expressed in a given heat
52 /// capacity unit.
53 GasConstant(const NumericType value, const Unit::HeatCapacity unit)
54 : DimensionalScalar<Unit::HeatCapacity, NumericType>(value, unit) {}
55
56 /// \brief Constructor. Constructs a gas constant from a given isobaric heat capacity and
57 /// isochoric heat capacity using Mayer's relation.
58 constexpr GasConstant(const IsobaricHeatCapacity<NumericType>& isobaric_heat_capacity,
59 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity)
60 : GasConstant<NumericType>(isobaric_heat_capacity.Value() - isochoric_heat_capacity.Value()) {}
61
62 /// \brief Constructor. Constructs a gas constant from a given isobaric heat capacity and heat
63 /// capacity ratio using the definition of the heat capacity ratio and Mayer's relation.
64 constexpr GasConstant(const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
65 const IsobaricHeatCapacity<NumericType>& isobaric_heat_capacity)
66 : GasConstant<NumericType>(
67 (1.0 - 1.0 / heat_capacity_ratio.Value()) * isobaric_heat_capacity.Value()) {}
68
69 /// \brief Constructor. Constructs a gas constant from a given isochoric heat capacity and heat
70 /// capacity ratio using the definition of the heat capacity ratio and Mayer's relation.
71 constexpr GasConstant(const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
72 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity)
73 : GasConstant<NumericType>(
74 (heat_capacity_ratio.Value() - 1.0) * isochoric_heat_capacity.Value()) {}
75
76 /// \brief Constructor. Constructs a gas constant from a given specific gas constant and mass
77 /// using the definition of the specific gas constant.
78 constexpr GasConstant(
79 const SpecificGasConstant<NumericType>& specific_gas_constant, const Mass<NumericType>& mass);
80
81 /// \brief Destructor. Destroys this gas constant.
82 ~GasConstant() noexcept = default;
83
84 /// \brief Copy constructor. Constructs a gas constant by copying another one.
85 constexpr GasConstant(const GasConstant<NumericType>& other) = default;
86
87 /// \brief Copy constructor. Constructs a gas constant by copying another one.
88 template <typename OtherNumericType>
89 explicit constexpr GasConstant(const GasConstant<OtherNumericType>& other)
90 : GasConstant(static_cast<NumericType>(other.Value())) {}
91
92 /// \brief Move constructor. Constructs a gas constant by moving another one.
93 constexpr GasConstant(GasConstant<NumericType>&& other) noexcept = default;
94
95 /// \brief Copy assignment operator. Assigns this gas constant by copying another one.
97
98 /// \brief Copy assignment operator. Assigns this gas constant by copying another one.
99 template <typename OtherNumericType>
101 this->value = static_cast<NumericType>(other.Value());
102 return *this;
103 }
104
105 /// \brief Move assignment operator. Assigns this gas constant by moving another one.
107 GasConstant<NumericType>&& other) noexcept = default;
108
109 /// \brief Statically creates a gas constant of zero.
110 [[nodiscard]] static constexpr GasConstant<NumericType> Zero() {
111 return GasConstant<NumericType>{static_cast<NumericType>(0)};
112 }
113
114 /// \brief Statically creates a gas constant with a given value expressed in a given heat capacity
115 /// unit.
116 template <Unit::HeatCapacity Unit>
117 [[nodiscard]] static constexpr GasConstant<NumericType> Create(const NumericType value) {
119 ConvertStatically<Unit::HeatCapacity, Unit, Standard<Unit::HeatCapacity>>(value)};
120 }
121
122 constexpr GasConstant<NumericType> operator+(const GasConstant<NumericType>& gas_constant) const {
123 return GasConstant<NumericType>{this->value + gas_constant.value};
124 }
125
127 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity) const {
128 return IsobaricHeatCapacity<NumericType>{isochoric_heat_capacity, *this};
129 }
130
131 constexpr GasConstant<NumericType> operator-(const GasConstant<NumericType>& gas_constant) const {
132 return GasConstant<NumericType>{this->value - gas_constant.value};
133 }
134
135 constexpr GasConstant<NumericType> operator*(const NumericType number) const {
136 return GasConstant<NumericType>{this->value * number};
137 }
138
139 constexpr GasConstant<NumericType> operator/(const NumericType number) const {
140 return GasConstant<NumericType>{this->value / number};
141 }
142
144
146 const SpecificGasConstant<NumericType>& specific_gas_constant) const;
147
148 constexpr NumericType operator/(const GasConstant<NumericType>& gas_constant) const noexcept {
149 return this->value / gas_constant.value;
150 }
151
152 constexpr void operator+=(const GasConstant<NumericType>& gas_constant) noexcept {
153 this->value += gas_constant.value;
154 }
155
156 constexpr void operator-=(const GasConstant<NumericType>& gas_constant) noexcept {
157 this->value -= gas_constant.value;
158 }
159
160 constexpr void operator*=(const NumericType number) noexcept {
161 this->value *= number;
162 }
163
164 constexpr void operator/=(const NumericType number) noexcept {
165 this->value /= number;
166 }
167
168private:
169 /// \brief Constructor. Constructs a gas constant with a given value expressed in the standard
170 /// heat capacity unit.
171 explicit constexpr GasConstant(const NumericType value)
172 : DimensionalScalar<Unit::HeatCapacity, NumericType>(value) {}
173};
174
175template <typename NumericType>
176inline constexpr bool operator==(
177 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
178 return left.Value() == right.Value();
179}
180
181template <typename NumericType>
182inline constexpr bool operator!=(
183 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
184 return left.Value() != right.Value();
185}
186
187template <typename NumericType>
188inline constexpr bool operator<(
189 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
190 return left.Value() < right.Value();
191}
192
193template <typename NumericType>
194inline constexpr bool operator>(
195 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
196 return left.Value() > right.Value();
197}
198
199template <typename NumericType>
200inline constexpr bool operator<=(
201 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
202 return left.Value() <= right.Value();
203}
204
205template <typename NumericType>
206inline constexpr bool operator>=(
207 const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
208 return left.Value() >= right.Value();
209}
210
211template <typename NumericType>
212inline std::ostream& operator<<(
213 std::ostream& stream, const GasConstant<NumericType>& gas_constant) {
214 stream << gas_constant.Print();
215 return stream;
216}
217
218template <typename NumericType>
220 const NumericType number, const GasConstant<NumericType>& gas_constant) {
221 return gas_constant * number;
222}
223
224template <typename NumericType>
226 const IsobaricHeatCapacity<NumericType>& isobaric_heat_capacity,
227 const GasConstant<NumericType>& gas_constant)
228 : HeatCapacityRatio<NumericType>(
229 isobaric_heat_capacity.Value() / (isobaric_heat_capacity.Value() - gas_constant.Value())) {}
230
231template <typename NumericType>
233 const GasConstant<NumericType>& gas_constant,
234 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity)
235 : HeatCapacityRatio<NumericType>(gas_constant.Value() / isochoric_heat_capacity.Value() + 1.0) {}
236
237template <typename NumericType>
239 const IsobaricHeatCapacity<NumericType>& isobaric_heat_capacity,
240 const GasConstant<NumericType>& gas_constant)
241 : IsochoricHeatCapacity<NumericType>(isobaric_heat_capacity.Value() - gas_constant.Value()) {}
242
243template <typename NumericType>
245 const GasConstant<NumericType>& gas_constant,
246 const HeatCapacityRatio<NumericType>& heat_capacity_ratio)
247 : IsochoricHeatCapacity<NumericType>(gas_constant.Value() / (heat_capacity_ratio.Value() - 1.0)) {
248}
249
250template <typename NumericType>
252 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity,
253 const GasConstant<NumericType>& gas_constant)
254 : IsobaricHeatCapacity<NumericType>(isochoric_heat_capacity.Value() + gas_constant.Value()) {}
255
256template <typename NumericType>
258 const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
259 const GasConstant<NumericType>& gas_constant)
260 : IsobaricHeatCapacity<NumericType>(
261 heat_capacity_ratio.Value() * gas_constant.Value() / (heat_capacity_ratio.Value() - 1.0)) {}
262
263template <typename NumericType>
265 const GasConstant<NumericType>& gas_constant) const {
266 return IsobaricHeatCapacity<NumericType>{*this, gas_constant};
267}
268
269template <typename NumericType>
271 const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity) const {
272 return GasConstant<NumericType>{*this, isochoric_heat_capacity};
273}
274
275template <typename NumericType>
280
281} // namespace PhQ
282
283namespace std {
284
285template <typename NumericType>
286struct hash<PhQ::GasConstant<NumericType>> {
287 inline size_t operator()(const PhQ::GasConstant<NumericType>& gas_constant) const {
288 return hash<NumericType>()(gas_constant.Value());
289 }
290};
291
292} // namespace std
293
294#endif // PHQ_GAS_CONSTANT_HPP
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
Gas constant of a gas. For the mass-specific gas constant, see PhQ::SpecificGasConstant.
constexpr GasConstant< NumericType > & operator=(const GasConstant< OtherNumericType > &other)
Copy assignment operator. Assigns this gas constant by copying another one.
constexpr GasConstant< NumericType > operator+(const GasConstant< NumericType > &gas_constant) const
static constexpr GasConstant< NumericType > Zero()
Statically creates a gas constant of zero.
~GasConstant() noexcept=default
Destructor. Destroys this gas constant.
constexpr GasConstant< NumericType > operator/(const NumericType number) const
constexpr GasConstant(const NumericType value)
Constructor. Constructs a gas constant with a given value expressed in the standard heat capacity uni...
constexpr GasConstant< NumericType > & operator=(const GasConstant< NumericType > &other)=default
Copy assignment operator. Assigns this gas constant by copying another one.
GasConstant()=default
Default constructor. Constructs a gas constant with an uninitialized value.
constexpr void operator-=(const GasConstant< NumericType > &gas_constant) noexcept
constexpr IsobaricHeatCapacity< NumericType > operator+(const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity) const
constexpr NumericType operator/(const GasConstant< NumericType > &gas_constant) const noexcept
constexpr GasConstant< NumericType > & operator=(GasConstant< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this gas constant by moving another one.
constexpr GasConstant< NumericType > operator-(const GasConstant< NumericType > &gas_constant) const
constexpr void operator/=(const NumericType number) noexcept
constexpr void operator+=(const GasConstant< NumericType > &gas_constant) noexcept
GasConstant(const NumericType value, const Unit::HeatCapacity unit)
Constructor. Constructs a gas constant with a given value expressed in a given heat capacity unit.
constexpr GasConstant(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity)
Constructor. Constructs a gas constant from a given isochoric heat capacity and heat capacity ratio u...
constexpr GasConstant(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const IsobaricHeatCapacity< NumericType > &isobaric_heat_capacity)
Constructor. Constructs a gas constant from a given isobaric heat capacity and heat capacity ratio us...
constexpr GasConstant< NumericType > operator*(const NumericType number) const
constexpr GasConstant(GasConstant< NumericType > &&other) noexcept=default
Move constructor. Constructs a gas constant by moving another one.
constexpr void operator*=(const NumericType number) noexcept
static constexpr GasConstant< NumericType > Create(const NumericType value)
Statically creates a gas constant with a given value expressed in a given heat capacity unit.
constexpr GasConstant(const IsobaricHeatCapacity< NumericType > &isobaric_heat_capacity, const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity)
Constructor. Constructs a gas constant from a given isobaric heat capacity and isochoric heat capacit...
Heat capacity ratio, also known as ratio of specific heats, adiabatic index, or Laplace's coefficient...
HeatCapacityRatio()=default
Default constructor. Constructs a heat capacity ratio ratio with an uninitialized value.
Isobaric heat capacity, also known as heat capacity at constant pressure. For the mass-specific isoba...
IsobaricHeatCapacity()=default
Default constructor. Constructs an isobaric heat capacity with an uninitialized value.
constexpr IsobaricHeatCapacity< NumericType > operator-(const IsobaricHeatCapacity< NumericType > &isobaric_heat_capacity) const
Isochoric heat capacity, also known as heat capacity at constant volume. For the mass-specific isocho...
IsochoricHeatCapacity()=default
Default constructor. Constructs an isochoric heat capacity with an uninitialized value.
constexpr IsochoricHeatCapacity< NumericType > operator+(const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity) const
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition Mass.hpp:100
Mass-specific gas constant of a gas. Gas constant per unit mass; see PhQ::GasConstant and PhQ::Mass....
HeatCapacity
Heat capacity units.
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
constexpr bool operator<(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator<=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator==(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept