Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
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"
34 #include "IsobaricHeatCapacity.hpp"
36 #include "Unit/HeatCapacity.hpp"
37 
38 namespace PhQ {
39 
40 // Forward declaration for class PhQ::GasConstant.
41 template <typename NumericType>
42 class SpecificGasConstant;
43 
44 /// \brief Gas constant of a gas. For the mass-specific gas constant, see PhQ::SpecificGasConstant.
45 template <typename NumericType = double>
46 class GasConstant : public DimensionalScalar<Unit::HeatCapacity, NumericType> {
47 public:
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.
96  constexpr GasConstant<NumericType>& operator=(const GasConstant<NumericType>& other) = default;
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 
143  constexpr SpecificGasConstant<NumericType> operator/(const Mass<NumericType>& mass) const;
144 
145  constexpr Mass<NumericType> operator/(
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 
168 private:
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 
175 template <typename NumericType>
176 inline constexpr bool operator==(
177  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
178  return left.Value() == right.Value();
179 }
180 
181 template <typename NumericType>
182 inline constexpr bool operator!=(
183  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
184  return left.Value() != right.Value();
185 }
186 
187 template <typename NumericType>
188 inline constexpr bool operator<(
189  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
190  return left.Value() < right.Value();
191 }
192 
193 template <typename NumericType>
194 inline constexpr bool operator>(
195  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
196  return left.Value() > right.Value();
197 }
198 
199 template <typename NumericType>
200 inline constexpr bool operator<=(
201  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
202  return left.Value() <= right.Value();
203 }
204 
205 template <typename NumericType>
206 inline constexpr bool operator>=(
207  const GasConstant<NumericType>& left, const GasConstant<NumericType>& right) noexcept {
208  return left.Value() >= right.Value();
209 }
210 
211 template <typename NumericType>
212 inline std::ostream& operator<<(
213  std::ostream& stream, const GasConstant<NumericType>& gas_constant) {
214  stream << gas_constant.Print();
215  return stream;
216 }
217 
218 template <typename NumericType>
220  const NumericType number, const GasConstant<NumericType>& gas_constant) {
221  return gas_constant * number;
222 }
223 
224 template <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 
231 template <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 
237 template <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 
243 template <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 
250 template <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 
256 template <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 
263 template <typename NumericType>
265  const GasConstant<NumericType>& gas_constant) const {
266  return IsobaricHeatCapacity<NumericType>{*this, gas_constant};
267 }
268 
269 template <typename NumericType>
271  const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity) const {
272  return GasConstant<NumericType>{*this, isochoric_heat_capacity};
273 }
274 
275 template <typename NumericType>
277  const GasConstant<NumericType>& gas_constant) const {
278  return IsochoricHeatCapacity<NumericType>{*this, gas_constant};
279 }
280 
281 } // namespace PhQ
282 
283 namespace std {
284 
285 template <typename NumericType>
286 struct 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...
double value
Value of this physical quantity expressed in its standard unit of measure.
constexpr double Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr Unit::HeatCapacity 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.
Definition: GasConstant.hpp:46
constexpr GasConstant< NumericType > & operator=(const GasConstant< NumericType > &other)=default
Copy assignment operator. Assigns this gas constant by copying another one.
~GasConstant() noexcept=default
Destructor. Destroys this gas constant.
constexpr IsobaricHeatCapacity< NumericType > operator+(const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity) 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 NumericType number) const
GasConstant()=default
Default constructor. Constructs a gas constant with an uninitialized value.
constexpr GasConstant< NumericType > & operator=(GasConstant< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this gas constant by moving another one.
constexpr void operator-=(const GasConstant< NumericType > &gas_constant) noexcept
constexpr NumericType operator/(const GasConstant< NumericType > &gas_constant) const noexcept
constexpr GasConstant< NumericType > & operator=(const GasConstant< OtherNumericType > &other)
Copy assignment operator. Assigns this gas constant by copying another one.
constexpr void operator/=(const NumericType number) noexcept
constexpr void operator+=(const GasConstant< NumericType > &gas_constant) 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< NumericType > operator-(const GasConstant< NumericType > &gas_constant) const
static constexpr GasConstant< NumericType > Zero()
Statically creates a gas constant of zero.
GasConstant(const NumericType value, const Unit::HeatCapacity unit)
Constructor. Constructs a gas constant with a given value expressed in a given heat capacity unit.
Definition: GasConstant.hpp:53
constexpr GasConstant< NumericType > operator*(const NumericType number) const
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...
Definition: GasConstant.hpp:71
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...
Definition: GasConstant.hpp:64
constexpr GasConstant< NumericType > operator+(const GasConstant< NumericType > &gas_constant) 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
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...
Definition: GasConstant.hpp:58
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...
constexpr IsobaricHeatCapacity< NumericType > operator-(const IsobaricHeatCapacity< NumericType > &isobaric_heat_capacity) const
IsobaricHeatCapacity()=default
Default constructor. Constructs an isobaric heat capacity with an uninitialized value.
Isochoric heat capacity, also known as heat capacity at constant volume. For the mass-specific isocho...
constexpr IsochoricHeatCapacity< NumericType > operator+(const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity) const
IsochoricHeatCapacity()=default
Default constructor. Constructs an isochoric heat capacity with an uninitialized value.
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.
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
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)