Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
SoundSpeed.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_SOUND_SPEED_HPP
26 #define PHQ_SOUND_SPEED_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "HeatCapacityRatio.hpp"
35 #include "MassDensity.hpp"
36 #include "SpecificGasConstant.hpp"
37 #include "Speed.hpp"
38 #include "StaticPressure.hpp"
39 #include "Temperature.hpp"
40 
41 namespace PhQ {
42 
43 // Forward declaration for class PhQ::SoundSpeed.
44 template <typename NumericType>
45 class MachNumber;
46 
47 // Forward declaration for class PhQ::SoundSpeed.
48 template <typename NumericType>
49 class Speed;
50 
51 /// \brief Speed of sound. Applies to any deformable material, including fluids and deformable
52 /// solids. Defined as the ratio of a material's isentropic bulk modulus to its mass density; see
53 /// PhQ::IsentropicBulkModulus and PhQ::MassDensity. The speed of sound also appears in the
54 /// definition of the Mach number; see PhQ::MachNumber and PhQ::Speed.
55 template <typename NumericType = double>
56 class SoundSpeed : public DimensionalScalar<Unit::Speed, NumericType> {
57 public:
58  /// \brief Default constructor. Constructs a sound speed with an uninitialized value.
59  SoundSpeed() = default;
60 
61  /// \brief Constructs a sound speed from a given value and speed unit.
62  SoundSpeed(const NumericType value, const Unit::Speed unit)
63  : DimensionalScalar<Unit::Speed, NumericType>(value, unit) {}
64 
65  /// \brief Constructs a sound speed from an isentropic bulk modulus and a mass density. This is
66  /// the definition of the sound speed; this relation always holds true.
67  SoundSpeed(const IsentropicBulkModulus<NumericType>& isentropic_bulk_modulus,
68  const MassDensity<NumericType>& mass_density)
69  : SoundSpeed<NumericType>(std::sqrt(isentropic_bulk_modulus.Value() / mass_density.Value())) {}
70 
71  /// \brief Constructs a sound speed from a heat capacity ratio, a static pressure, and a mass
72  /// density. This relation applies only to an ideal gas.
73  SoundSpeed(const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
74  const StaticPressure<NumericType>& static_pressure,
75  const MassDensity<NumericType>& mass_density)
76  : SoundSpeed<NumericType>(
77  std::sqrt(heat_capacity_ratio.Value() * static_pressure.Value() / mass_density.Value())) {}
78 
79  /// \brief Constructs a sound speed from a heat capacity ratio, a specific gas constant, and a
80  /// temperature. This relation applies only to an ideal gas.
81  SoundSpeed(const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
82  const SpecificGasConstant<NumericType>& specific_gas_constant,
83  const Temperature<NumericType>& temperature)
84  : SoundSpeed<NumericType>(std::sqrt(
85  heat_capacity_ratio.Value() * specific_gas_constant.Value() * temperature.Value())) {}
86 
87  /// \brief Constructs a sound speed from a speed and a Mach number. This uses the definition of
88  /// the Mach number; this relation always holds true.
89  constexpr SoundSpeed(const Speed<NumericType>& speed, const MachNumber<NumericType>& mach_number);
90 
91  /// \brief Destructor. Destroys this sound speed.
92  ~SoundSpeed() noexcept = default;
93 
94  /// \brief Copy constructor. Constructs a sound speed by copying another one.
95  constexpr SoundSpeed(const SoundSpeed<NumericType>& other) = default;
96 
97  /// \brief Copy constructor. Constructs a sound speed by copying another one.
98  template <typename OtherNumericType>
99  explicit constexpr SoundSpeed(const SoundSpeed<OtherNumericType>& other)
100  : SoundSpeed(static_cast<NumericType>(other.Value())) {}
101 
102  /// \brief Move constructor. Constructs a sound speed by moving another one.
103  constexpr SoundSpeed(SoundSpeed<NumericType>&& other) noexcept = default;
104 
105  /// \brief Copy assignment operator. Assigns this sound speed by copying another one.
106  constexpr SoundSpeed<NumericType>& operator=(const SoundSpeed<NumericType>& other) = default;
107 
108  /// \brief Copy assignment operator. Assigns this sound speed by copying another one.
109  template <typename OtherNumericType>
111  this->value = static_cast<NumericType>(other.Value());
112  return *this;
113  }
114 
115  /// \brief Move assignment operator. Assigns this sound speed by moving another one.
116  constexpr SoundSpeed<NumericType>& operator=(SoundSpeed<NumericType>&& other) noexcept = default;
117 
118  [[nodiscard]] static constexpr SoundSpeed<NumericType> Zero() {
119  return SoundSpeed<NumericType>{static_cast<NumericType>(0)};
120  }
121 
122  /// \brief Creates a sound speed from a given value and speed unit.
123  template <Unit::Speed Unit>
124  [[nodiscard]] static constexpr SoundSpeed<NumericType> Create(const NumericType value) {
126  ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(value)};
127  }
128 
130  return SoundSpeed<NumericType>{this->value + speed.value};
131  }
132 
133  constexpr Speed<NumericType> operator+(const Speed<NumericType>& speed) const {
134  return Speed<NumericType>{this->value + speed.value};
135  }
136 
138  return SoundSpeed<NumericType>{this->value - speed.value};
139  }
140 
141  constexpr Speed<NumericType> operator-(const Speed<NumericType>& speed) const {
142  return Speed<NumericType>{this->value - speed.value};
143  }
144 
145  constexpr SoundSpeed<NumericType> operator*(const NumericType number) const {
146  return SoundSpeed<NumericType>{this->value * number};
147  }
148 
149  constexpr Speed<NumericType> operator*(const MachNumber<NumericType>& mach_number) const;
150 
151  constexpr SoundSpeed<NumericType> operator/(const NumericType number) const {
152  return SoundSpeed<NumericType>{this->value / number};
153  }
154 
155  constexpr NumericType operator/(const SoundSpeed<NumericType>& sound_speed) const noexcept {
156  return this->value / sound_speed.value;
157  }
158 
159  constexpr void operator+=(const SoundSpeed<NumericType>& sound_speed) noexcept {
160  this->value += sound_speed.value;
161  }
162 
163  constexpr void operator+=(const Speed<NumericType>& speed) noexcept {
164  this->value += speed.value;
165  }
166 
167  constexpr void operator-=(const SoundSpeed<NumericType>& sound_speed) noexcept {
168  this->value -= sound_speed.value;
169  }
170 
171  constexpr void operator-=(const Speed<NumericType>& speed) noexcept {
172  this->value -= speed.value;
173  }
174 
175  constexpr void operator*=(const NumericType number) noexcept {
176  this->value *= number;
177  }
178 
179  constexpr void operator/=(const NumericType number) noexcept {
180  this->value /= number;
181  }
182 
183 private:
184  /// \brief Constructor. Constructs a sound speed with a given value expressed in the standard
185  /// speed unit.
186  explicit constexpr SoundSpeed(const NumericType value)
187  : DimensionalScalar<Unit::Speed, NumericType>(value) {}
188 };
189 
190 template <typename NumericType>
191 inline constexpr bool operator==(
192  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
193  return left.Value() == right.Value();
194 }
195 
196 template <typename NumericType>
197 inline constexpr bool operator!=(
198  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
199  return left.Value() != right.Value();
200 }
201 
202 template <typename NumericType>
203 inline constexpr bool operator<(
204  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
205  return left.Value() < right.Value();
206 }
207 
208 template <typename NumericType>
209 inline constexpr bool operator>(
210  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
211  return left.Value() > right.Value();
212 }
213 
214 template <typename NumericType>
215 inline constexpr bool operator<=(
216  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
217  return left.Value() <= right.Value();
218 }
219 
220 template <typename NumericType>
221 inline constexpr bool operator>=(
222  const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
223  return left.Value() >= right.Value();
224 }
225 
226 template <typename NumericType>
227 inline std::ostream& operator<<(std::ostream& stream, const SoundSpeed<NumericType>& sound_speed) {
228  stream << sound_speed.Print();
229  return stream;
230 }
231 
232 template <typename NumericType>
234  const NumericType number, const SoundSpeed<NumericType>& sound_speed) {
235  return sound_speed * number;
236 }
237 
238 template <typename NumericType>
239 inline constexpr Speed<NumericType> Speed<NumericType>::operator+(
240  const SoundSpeed<NumericType>& sound_speed) const {
241  return Speed<NumericType>{this->value + sound_speed.Value()};
242 }
243 
244 template <typename NumericType>
245 inline constexpr Speed<NumericType> Speed<NumericType>::operator-(
246  const SoundSpeed<NumericType>& sound_speed) const {
247  return Speed<NumericType>{this->value - sound_speed.Value()};
248 }
249 
250 template <typename NumericType>
251 inline constexpr void Speed<NumericType>::operator+=(
252  const SoundSpeed<NumericType>& speed) noexcept {
253  this->value += speed.Value();
254 }
255 
256 template <typename NumericType>
257 inline constexpr void Speed<NumericType>::operator-=(
258  const SoundSpeed<NumericType>& speed) noexcept {
259  this->value -= speed.Value();
260 }
261 
262 template <typename NumericType>
264  const IsentropicBulkModulus<NumericType>& isentropic_bulk_modulus,
265  const SoundSpeed<NumericType>& sound_speed)
266  : MassDensity<NumericType>(isentropic_bulk_modulus.Value() / std::pow(sound_speed.Value(), 2)) {}
267 
268 template <typename NumericType>
270  const MassDensity<NumericType>& mass_density, const SoundSpeed<NumericType>& sound_speed)
271  : IsentropicBulkModulus<NumericType>(mass_density.Value() * std::pow(sound_speed.Value(), 2)) {}
272 
273 } // namespace PhQ
274 
275 namespace std {
276 
277 template <typename NumericType>
278 struct hash<PhQ::SoundSpeed<NumericType>> {
279  inline size_t operator()(const PhQ::SoundSpeed<NumericType>& sound_speed) const {
280  return hash<NumericType>()(sound_speed.Value());
281  }
282 };
283 
284 } // namespace std
285 
286 #endif // PHQ_SOUND_SPEED_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::Speed 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...
Heat capacity ratio, also known as ratio of specific heats, adiabatic index, or Laplace's coefficient...
Isentropic bulk modulus. Not to be confused with the isothermal bulk modulus; see PhQ::IsothermalBulk...
Mach number of a fluid flow. Measures the local compressibility of a fluid flow. Represents the ratio...
Definition: MachNumber.hpp:42
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Definition: MassDensity.hpp:83
MassDensity()=default
Default constructor. Constructs a mass density with an uninitialized value.
Speed of sound. Applies to any deformable material, including fluids and deformable solids....
Definition: SoundSpeed.hpp:56
constexpr void operator+=(const Speed< NumericType > &speed) noexcept
Definition: SoundSpeed.hpp:163
constexpr SoundSpeed< NumericType > operator*(const NumericType number) const
Definition: SoundSpeed.hpp:145
SoundSpeed(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const SpecificGasConstant< NumericType > &specific_gas_constant, const Temperature< NumericType > &temperature)
Constructs a sound speed from a heat capacity ratio, a specific gas constant, and a temperature....
Definition: SoundSpeed.hpp:81
constexpr void operator+=(const SoundSpeed< NumericType > &sound_speed) noexcept
Definition: SoundSpeed.hpp:159
SoundSpeed(const IsentropicBulkModulus< NumericType > &isentropic_bulk_modulus, const MassDensity< NumericType > &mass_density)
Constructs a sound speed from an isentropic bulk modulus and a mass density. This is the definition o...
Definition: SoundSpeed.hpp:67
constexpr SoundSpeed< NumericType > operator+(const SoundSpeed< NumericType > &speed) const
Definition: SoundSpeed.hpp:129
constexpr SoundSpeed< NumericType > operator-(const SoundSpeed< NumericType > &speed) const
Definition: SoundSpeed.hpp:137
constexpr void operator*=(const NumericType number) noexcept
Definition: SoundSpeed.hpp:175
constexpr void operator/=(const NumericType number) noexcept
Definition: SoundSpeed.hpp:179
constexpr void operator-=(const SoundSpeed< NumericType > &sound_speed) noexcept
Definition: SoundSpeed.hpp:167
constexpr Speed< NumericType > operator-(const Speed< NumericType > &speed) const
Definition: SoundSpeed.hpp:141
constexpr SoundSpeed(const NumericType value)
Constructor. Constructs a sound speed with a given value expressed in the standard speed unit.
Definition: SoundSpeed.hpp:186
static constexpr SoundSpeed< NumericType > Zero()
Definition: SoundSpeed.hpp:118
constexpr SoundSpeed(SoundSpeed< NumericType > &&other) noexcept=default
Move constructor. Constructs a sound speed by moving another one.
static constexpr SoundSpeed< NumericType > Create(const NumericType value)
Creates a sound speed from a given value and speed unit.
Definition: SoundSpeed.hpp:124
constexpr SoundSpeed< NumericType > & operator=(const SoundSpeed< OtherNumericType > &other)
Copy assignment operator. Assigns this sound speed by copying another one.
Definition: SoundSpeed.hpp:110
SoundSpeed()=default
Default constructor. Constructs a sound speed with an uninitialized value.
constexpr SoundSpeed< NumericType > operator/(const NumericType number) const
Definition: SoundSpeed.hpp:151
~SoundSpeed() noexcept=default
Destructor. Destroys this sound speed.
constexpr Speed< NumericType > operator+(const Speed< NumericType > &speed) const
Definition: SoundSpeed.hpp:133
constexpr NumericType operator/(const SoundSpeed< NumericType > &sound_speed) const noexcept
Definition: SoundSpeed.hpp:155
constexpr SoundSpeed< NumericType > & operator=(SoundSpeed< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this sound speed by moving another one.
SoundSpeed(const HeatCapacityRatio< NumericType > &heat_capacity_ratio, const StaticPressure< NumericType > &static_pressure, const MassDensity< NumericType > &mass_density)
Constructs a sound speed from a heat capacity ratio, a static pressure, and a mass density....
Definition: SoundSpeed.hpp:73
SoundSpeed(const NumericType value, const Unit::Speed unit)
Constructs a sound speed from a given value and speed unit.
Definition: SoundSpeed.hpp:62
constexpr void operator-=(const Speed< NumericType > &speed) noexcept
Definition: SoundSpeed.hpp:171
constexpr SoundSpeed< NumericType > & operator=(const SoundSpeed< NumericType > &other)=default
Copy assignment operator. Assigns this sound speed by copying another one.
Mass-specific gas constant of a gas. Gas constant per unit mass; see PhQ::GasConstant and PhQ::Mass....
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
constexpr void operator+=(const Speed< NumericType > &speed) noexcept
Definition: Speed.hpp:247
constexpr Speed< NumericType > operator-(const Speed< NumericType > &speed) const
Definition: Speed.hpp:199
constexpr void operator-=(const Speed< NumericType > &speed) noexcept
Definition: Speed.hpp:253
constexpr Speed< NumericType > operator+(const Speed< NumericType > &speed) const
Definition: Speed.hpp:193
Static pressure. Pressure of a fluid at rest. Not to be confused with dynamic pressure,...
Temperature. For a temperature difference, see PhQ::TemperatureDifference. For the gradient of temper...
Definition: Temperature.hpp:41
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
Speed
Speed units.
Definition: Speed.hpp:53
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)