Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
SpecificGasConstant.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_SPECIFIC_GAS_CONSTANT_HPP
26 #define PHQ_SPECIFIC_GAS_CONSTANT_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "GasConstant.hpp"
34 #include "HeatCapacityRatio.hpp"
35 #include "Mass.hpp"
39 
40 namespace PhQ {
41 
42 /// \brief Mass-specific gas constant of a gas. Gas constant per unit mass; see PhQ::GasConstant and
43 /// PhQ::Mass. PhQ::Mass.
44 template <typename NumericType = double>
45 class SpecificGasConstant : public DimensionalScalar<Unit::SpecificHeatCapacity, NumericType> {
46 public:
47  /// \brief Default constructor. Constructs a specific gas constant with an uninitialized value.
48  SpecificGasConstant() = default;
49 
50  /// \brief Constructor. Constructs a specific gas constant with a given value expressed in a given
51  /// specific heat capacity unit.
52  SpecificGasConstant(const NumericType value, const Unit::SpecificHeatCapacity unit)
53  : DimensionalScalar<Unit::SpecificHeatCapacity, NumericType>(value, unit) {}
54 
55  /// \brief Constructor. Constructs a specific gas constant from a given specific isobaric heat
56  /// capacity and specific isochoric heat capacity using Mayer's relation.
58  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity,
59  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity)
60  : SpecificGasConstant<NumericType>(
61  specific_isobaric_heat_capacity.Value() - specific_isochoric_heat_capacity.Value()) {}
62 
63  /// \brief Constructor. Constructs a specific gas constant from a given specific isobaric heat
64  /// capacity and heat capacity ratio using the definition of the heat capacity ratio and Mayer's
65  /// relation.
67  const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
68  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity)
69  : SpecificGasConstant<NumericType>(
70  (1.0 - 1.0 / heat_capacity_ratio.Value()) * specific_isobaric_heat_capacity.Value()) {}
71 
72  /// \brief Constructor. Constructs a specific gas constant from a given specific isochoric heat
73  /// capacity and heat capacity ratio using the definition of the heat capacity ratio and Mayer's
74  /// relation.
76  const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
77  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity)
78  : SpecificGasConstant<NumericType>(
79  (heat_capacity_ratio.Value() - 1.0) * specific_isochoric_heat_capacity.Value()) {}
80 
81  /// \brief Constructor. Constructs a specific gas constant from a given gas constant and mass
82  /// using the definition of the specific gas constant.
84  const GasConstant<NumericType>& gas_constant, const Mass<NumericType>& mass)
85  : SpecificGasConstant<NumericType>(gas_constant.Value() / mass.Value()) {}
86 
87  /// \brief Destructor. Destroys this specific gas constant.
88  ~SpecificGasConstant() noexcept = default;
89 
90  /// \brief Copy constructor. Constructs a specific gas constant by copying another one.
91  constexpr SpecificGasConstant(const SpecificGasConstant<NumericType>& other) = default;
92 
93  /// \brief Copy constructor. Constructs a specific gas constant by copying another one.
94  template <typename OtherNumericType>
95  explicit constexpr SpecificGasConstant(const SpecificGasConstant<OtherNumericType>& other)
96  : SpecificGasConstant(static_cast<NumericType>(other.Value())) {}
97 
98  /// \brief Move constructor. Constructs a specific gas constant by moving another one.
99  constexpr SpecificGasConstant(SpecificGasConstant<NumericType>&& other) noexcept = default;
100 
101  /// \brief Copy assignment operator. Assigns this specific gas constant by copying another one.
103  const SpecificGasConstant<NumericType>& other) = default;
104 
105  /// \brief Copy assignment operator. Assigns this specific gas constant by copying another one.
106  template <typename OtherNumericType>
109  this->value = static_cast<NumericType>(other.Value());
110  return *this;
111  }
112 
113  /// \brief Move assignment operator. Assigns this specific gas constant by moving another one.
115  SpecificGasConstant<NumericType>&& other) noexcept = default;
116 
117  /// \brief Statically creates a specific gas constant of zero.
118  [[nodiscard]] static constexpr SpecificGasConstant<NumericType> Zero() {
119  return SpecificGasConstant<NumericType>{static_cast<NumericType>(0)};
120  }
121 
122  /// \brief Statically creates a specific gas constant with a given value expressed in a given
123  /// specific heat capacity unit.
124  template <Unit::SpecificHeatCapacity Unit>
125  [[nodiscard]] static constexpr SpecificGasConstant<NumericType> Create(const NumericType value) {
127  ConvertStatically<Unit::SpecificHeatCapacity, Unit, Standard<Unit::SpecificHeatCapacity>>(
128  value)};
129  }
130 
132  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
133  return SpecificGasConstant<NumericType>{this->value + specific_gas_constant.value};
134  }
135 
137  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity) const {
138  return SpecificIsobaricHeatCapacity<NumericType>{specific_isochoric_heat_capacity, *this};
139  }
140 
142  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
143  return SpecificGasConstant<NumericType>{this->value - specific_gas_constant.value};
144  }
145 
146  constexpr SpecificGasConstant<NumericType> operator*(const NumericType number) const {
147  return SpecificGasConstant<NumericType>{this->value * number};
148  }
149 
150  constexpr GasConstant<NumericType> operator*(const Mass<NumericType>& mass) const {
151  return GasConstant<NumericType>{*this, mass};
152  }
153 
154  constexpr SpecificGasConstant<NumericType> operator/(const NumericType number) const {
155  return SpecificGasConstant<NumericType>{this->value / number};
156  }
157 
158  constexpr NumericType operator/(
159  const SpecificGasConstant<NumericType>& specific_gas_constant) const noexcept {
160  return this->value / specific_gas_constant.value;
161  }
162 
163  constexpr void operator+=(
164  const SpecificGasConstant<NumericType>& specific_gas_constant) noexcept {
165  this->value += specific_gas_constant.value;
166  }
167 
168  constexpr void operator-=(
169  const SpecificGasConstant<NumericType>& specific_gas_constant) noexcept {
170  this->value -= specific_gas_constant.value;
171  }
172 
173  constexpr void operator*=(const NumericType number) noexcept {
174  this->value *= number;
175  }
176 
177  constexpr void operator/=(const NumericType number) noexcept {
178  this->value /= number;
179  }
180 
181 private:
182  /// \brief Constructor. Constructs a specific gas constant with a given value expressed in the
183  /// standard specific heat capacity unit.
184  explicit constexpr SpecificGasConstant(const NumericType value)
185  : DimensionalScalar<Unit::SpecificHeatCapacity, NumericType>(value) {}
186 };
187 
188 template <typename NumericType>
189 inline constexpr bool operator==(const SpecificGasConstant<NumericType>& left,
190  const SpecificGasConstant<NumericType>& right) noexcept {
191  return left.Value() == right.Value();
192 }
193 
194 template <typename NumericType>
195 inline constexpr bool operator!=(const SpecificGasConstant<NumericType>& left,
196  const SpecificGasConstant<NumericType>& right) noexcept {
197  return left.Value() != right.Value();
198 }
199 
200 template <typename NumericType>
201 inline constexpr bool operator<(const SpecificGasConstant<NumericType>& left,
202  const SpecificGasConstant<NumericType>& right) noexcept {
203  return left.Value() < right.Value();
204 }
205 
206 template <typename NumericType>
207 inline constexpr bool operator>(const SpecificGasConstant<NumericType>& left,
208  const SpecificGasConstant<NumericType>& right) noexcept {
209  return left.Value() > right.Value();
210 }
211 
212 template <typename NumericType>
213 inline constexpr bool operator<=(const SpecificGasConstant<NumericType>& left,
214  const SpecificGasConstant<NumericType>& right) noexcept {
215  return left.Value() <= right.Value();
216 }
217 
218 template <typename NumericType>
219 inline constexpr bool operator>=(const SpecificGasConstant<NumericType>& left,
220  const SpecificGasConstant<NumericType>& right) noexcept {
221  return left.Value() >= right.Value();
222 }
223 
224 template <typename NumericType>
225 inline std::ostream& operator<<(
226  std::ostream& stream, const SpecificGasConstant<NumericType>& specific_gas_constant) {
227  stream << specific_gas_constant.Print();
228  return stream;
229 }
230 
231 template <typename NumericType>
233  const NumericType number, const SpecificGasConstant<NumericType>& specific_gas_constant) {
234  return specific_gas_constant * number;
235 }
236 
237 template <typename NumericType>
239  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity,
240  const SpecificGasConstant<NumericType>& specific_gas_constant)
241  : HeatCapacityRatio<NumericType>(
242  specific_isobaric_heat_capacity.Value()
243  / (specific_isobaric_heat_capacity.Value() - specific_gas_constant.Value())) {}
244 
245 template <typename NumericType>
247  const SpecificGasConstant<NumericType>& specific_gas_constant,
248  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity)
249  : HeatCapacityRatio<NumericType>(
250  specific_gas_constant.Value() / specific_isochoric_heat_capacity.Value() + 1.0) {}
251 
252 template <typename NumericType>
253 inline constexpr Mass<NumericType>::Mass(
254  const GasConstant<NumericType>& gas_constant,
255  const SpecificGasConstant<NumericType>& specific_gas_constant)
256  : Mass<NumericType>(gas_constant.Value() / specific_gas_constant.Value()) {}
257 
258 template <typename NumericType>
260  const SpecificGasConstant<NumericType>& specific_gas_constant, const Mass<NumericType>& mass)
261  : GasConstant<NumericType>(specific_gas_constant.Value() * mass.Value()) {}
262 
263 template <typename NumericType>
265  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity,
266  const SpecificGasConstant<NumericType>& specific_gas_constant)
267  : SpecificIsochoricHeatCapacity<NumericType>(
268  specific_isobaric_heat_capacity.Value() - specific_gas_constant.Value()) {}
269 
270 template <typename NumericType>
272  const SpecificGasConstant<NumericType>& specific_gas_constant,
273  const HeatCapacityRatio<NumericType>& heat_capacity_ratio)
274  : SpecificIsochoricHeatCapacity<NumericType>(
275  specific_gas_constant.Value() / (heat_capacity_ratio.Value() - 1.0)) {}
276 
277 template <typename NumericType>
279  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity,
280  const SpecificGasConstant<NumericType>& specific_gas_constant)
281  : SpecificIsobaricHeatCapacity<NumericType>(
282  specific_isochoric_heat_capacity.Value() + specific_gas_constant.Value()) {}
283 
284 template <typename NumericType>
286  const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
287  const SpecificGasConstant<NumericType>& specific_gas_constant)
288  : SpecificIsobaricHeatCapacity<NumericType>(
289  heat_capacity_ratio.Value() * specific_gas_constant.Value()
290  / (heat_capacity_ratio.Value() - 1.0)) {}
291 
292 template <typename NumericType>
295  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
296  return SpecificIsobaricHeatCapacity<NumericType>{*this, specific_gas_constant};
297 }
298 
299 template <typename NumericType>
300 inline constexpr SpecificGasConstant<NumericType>
302  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity) const {
303  return SpecificGasConstant<NumericType>{*this, specific_isochoric_heat_capacity};
304 }
305 
306 template <typename NumericType>
309  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
310  return SpecificIsochoricHeatCapacity<NumericType>{*this, specific_gas_constant};
311 }
312 
313 template <typename NumericType>
315  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
316  return GasConstant<NumericType>{specific_gas_constant, *this};
317 }
318 
319 template <typename NumericType>
321  const Mass<NumericType>& mass) const {
322  return SpecificGasConstant<NumericType>{*this, mass};
323 }
324 
325 template <typename NumericType>
327  const SpecificGasConstant<NumericType>& specific_gas_constant) const {
328  return Mass<NumericType>{*this, specific_gas_constant};
329 }
330 
331 } // namespace PhQ
332 
333 namespace std {
334 
335 template <typename NumericType>
336 struct hash<PhQ::SpecificGasConstant<NumericType>> {
337  inline size_t operator()(
338  const PhQ::SpecificGasConstant<NumericType>& specific_gas_constant) const {
339  return hash<NumericType>()(specific_gas_constant.Value());
340  }
341 };
342 
343 } // namespace std
344 
345 #endif // PHQ_SPECIFIC_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::SpecificHeatCapacity 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 NumericType number) const
GasConstant()=default
Default constructor. Constructs a gas constant with an uninitialized value.
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.
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition: Mass.hpp:100
Mass()=default
Default constructor. Constructs a mass with an uninitialized value.
constexpr Mass< NumericType > operator*(const NumericType number) const
Definition: Mass.hpp:192
Mass-specific gas constant of a gas. Gas constant per unit mass; see PhQ::GasConstant and PhQ::Mass....
constexpr void operator+=(const SpecificGasConstant< NumericType > &specific_gas_constant) noexcept
constexpr SpecificGasConstant< NumericType > & operator=(const SpecificGasConstant< OtherNumericType > &other)
Copy assignment operator. Assigns this specific gas constant by copying another one.
constexpr GasConstant< NumericType > operator*(const Mass< NumericType > &mass) const
constexpr SpecificGasConstant(const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity, const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity)
Constructor. Constructs a specific gas constant from a given specific isobaric heat capacity and spec...
constexpr SpecificGasConstant(const GasConstant< NumericType > &gas_constant, const Mass< NumericType > &mass)
Constructor. Constructs a specific gas constant from a given gas constant and mass using the definiti...
constexpr SpecificGasConstant< NumericType > operator*(const NumericType number) const
constexpr SpecificGasConstant< NumericType > operator+(const SpecificGasConstant< NumericType > &specific_gas_constant) const
static constexpr SpecificGasConstant< NumericType > Zero()
Statically creates a specific gas constant of zero.
static constexpr SpecificGasConstant< NumericType > Create(const NumericType value)
Statically creates a specific gas constant with a given value expressed in a given specific heat capa...
constexpr SpecificGasConstant< NumericType > operator/(const NumericType number) const
constexpr SpecificGasConstant(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity)
Constructor. Constructs a specific gas constant from a given specific isobaric heat capacity and heat...
constexpr void operator*=(const NumericType number) noexcept
constexpr void operator/=(const NumericType number) noexcept
SpecificGasConstant()=default
Default constructor. Constructs a specific gas constant with an uninitialized value.
constexpr SpecificGasConstant< NumericType > & operator=(SpecificGasConstant< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this specific gas constant by moving another one.
constexpr void operator-=(const SpecificGasConstant< NumericType > &specific_gas_constant) noexcept
constexpr SpecificGasConstant< NumericType > & operator=(const SpecificGasConstant< NumericType > &other)=default
Copy assignment operator. Assigns this specific gas constant by copying another one.
~SpecificGasConstant() noexcept=default
Destructor. Destroys this specific gas constant.
constexpr SpecificIsobaricHeatCapacity< NumericType > operator+(const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity) const
constexpr SpecificGasConstant(SpecificGasConstant< NumericType > &&other) noexcept=default
Move constructor. Constructs a specific gas constant by moving another one.
constexpr SpecificGasConstant(const NumericType value)
Constructor. Constructs a specific gas constant with a given value expressed in the standard specific...
constexpr NumericType operator/(const SpecificGasConstant< NumericType > &specific_gas_constant) const noexcept
constexpr SpecificGasConstant(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity)
Constructor. Constructs a specific gas constant from a given specific isochoric heat capacity and hea...
SpecificGasConstant(const NumericType value, const Unit::SpecificHeatCapacity unit)
Constructor. Constructs a specific gas constant with a given value expressed in a given specific heat...
constexpr SpecificGasConstant< NumericType > operator-(const SpecificGasConstant< NumericType > &specific_gas_constant) const
Mass-specific isobaric heat capacity, also known as mass-specific heat capacity at constant pressure,...
SpecificIsobaricHeatCapacity()=default
Default constructor. Constructs a specific isobaric heat capacity with an uninitialized value.
constexpr SpecificIsobaricHeatCapacity< NumericType > operator-(const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity) const
Mass-specific isochoric heat capacity, also known as mass-specific heat capacity at constant volume,...
constexpr SpecificIsochoricHeatCapacity< NumericType > operator+(const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity) const
SpecificIsochoricHeatCapacity()=default
Default constructor. Constructs a specific isochoric heat capacity with an uninitialized value.
SpecificHeatCapacity
Mass-specific 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)