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
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"
37#include "Speed.hpp"
38#include "StaticPressure.hpp"
39#include "Temperature.hpp"
40
41namespace PhQ {
42
43// Forward declaration for class PhQ::SoundSpeed.
44template <typename NumericType>
45class MachNumber;
46
47// Forward declaration for class PhQ::SoundSpeed.
48template <typename NumericType>
49class 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.
55template <typename NumericType = double>
56class SoundSpeed : public DimensionalScalar<Unit::Speed, NumericType> {
57public:
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
80 /// \brief Constructs a sound speed from a heat capacity ratio, a specific gas constant, and a
81 /// temperature. This relation applies only to an ideal gas.
82 SoundSpeed(const HeatCapacityRatio<NumericType>& heat_capacity_ratio,
83 const SpecificGasConstant<NumericType>& specific_gas_constant,
84 const Temperature<NumericType>& temperature)
85 : SoundSpeed<NumericType>(std::sqrt(
86 heat_capacity_ratio.Value() * specific_gas_constant.Value() * temperature.Value())) {}
87
88 /// \brief Constructs a sound speed from a speed and a Mach number. This uses the definition of
89 /// the Mach number; this relation always holds true.
90 constexpr SoundSpeed(const Speed<NumericType>& speed, const MachNumber<NumericType>& mach_number);
91
92 /// \brief Destructor. Destroys this sound speed.
93 ~SoundSpeed() noexcept = default;
94
95 /// \brief Copy constructor. Constructs a sound speed by copying another one.
96 constexpr SoundSpeed(const SoundSpeed<NumericType>& other) = default;
97
98 /// \brief Copy constructor. Constructs a sound speed by copying another one.
99 template <typename OtherNumericType>
100 explicit constexpr SoundSpeed(const SoundSpeed<OtherNumericType>& other)
101 : SoundSpeed(static_cast<NumericType>(other.Value())) {}
102
103 /// \brief Move constructor. Constructs a sound speed by moving another one.
104 constexpr SoundSpeed(SoundSpeed<NumericType>&& other) noexcept = default;
105
106 /// \brief Copy assignment operator. Assigns this sound speed by copying another one.
108
109 /// \brief Copy assignment operator. Assigns this sound speed by copying another one.
110 template <typename OtherNumericType>
112 this->value = static_cast<NumericType>(other.Value());
113 return *this;
114 }
115
116 /// \brief Move assignment operator. Assigns this sound speed by moving another one.
117 constexpr SoundSpeed<NumericType>& operator=(SoundSpeed<NumericType>&& other) noexcept = default;
118
119 [[nodiscard]] static constexpr SoundSpeed<NumericType> Zero() {
120 return SoundSpeed<NumericType>{static_cast<NumericType>(0)};
121 }
122
123 /// \brief Creates a sound speed from a given value and speed unit.
124 template <Unit::Speed Unit>
125 [[nodiscard]] static constexpr SoundSpeed<NumericType> Create(const NumericType value) {
127 ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(value)};
128 }
129
131 return SoundSpeed<NumericType>{this->value + speed.value};
132 }
133
134 constexpr Speed<NumericType> operator+(const Speed<NumericType>& speed) const {
135 return Speed<NumericType>{this->value + speed.value};
136 }
137
139 return SoundSpeed<NumericType>{this->value - speed.value};
140 }
141
142 constexpr Speed<NumericType> operator-(const Speed<NumericType>& speed) const {
143 return Speed<NumericType>{this->value - speed.value};
144 }
145
146 constexpr SoundSpeed<NumericType> operator*(const NumericType number) const {
147 return SoundSpeed<NumericType>{this->value * number};
148 }
149
150 constexpr Speed<NumericType> operator*(const MachNumber<NumericType>& mach_number) const;
151
152 constexpr SoundSpeed<NumericType> operator/(const NumericType number) const {
153 return SoundSpeed<NumericType>{this->value / number};
154 }
155
156 constexpr NumericType operator/(const SoundSpeed<NumericType>& sound_speed) const noexcept {
157 return this->value / sound_speed.value;
158 }
159
160 constexpr void operator+=(const SoundSpeed<NumericType>& sound_speed) noexcept {
161 this->value += sound_speed.value;
162 }
163
164 constexpr void operator+=(const Speed<NumericType>& speed) noexcept {
165 this->value += speed.value;
166 }
167
168 constexpr void operator-=(const SoundSpeed<NumericType>& sound_speed) noexcept {
169 this->value -= sound_speed.value;
170 }
171
172 constexpr void operator-=(const Speed<NumericType>& speed) noexcept {
173 this->value -= speed.value;
174 }
175
176 constexpr void operator*=(const NumericType number) noexcept {
177 this->value *= number;
178 }
179
180 constexpr void operator/=(const NumericType number) noexcept {
181 this->value /= number;
182 }
183
184private:
185 /// \brief Constructor. Constructs a sound speed with a given value expressed in the standard
186 /// speed unit.
187 explicit constexpr SoundSpeed(const NumericType value)
188 : DimensionalScalar<Unit::Speed, NumericType>(value) {}
189};
190
191template <typename NumericType>
192inline constexpr bool operator==(
193 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
194 return left.Value() == right.Value();
195}
196
197template <typename NumericType>
198inline constexpr bool operator!=(
199 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
200 return left.Value() != right.Value();
201}
202
203template <typename NumericType>
204inline constexpr bool operator<(
205 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
206 return left.Value() < right.Value();
207}
208
209template <typename NumericType>
210inline constexpr bool operator>(
211 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
212 return left.Value() > right.Value();
213}
214
215template <typename NumericType>
216inline constexpr bool operator<=(
217 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
218 return left.Value() <= right.Value();
219}
220
221template <typename NumericType>
222inline constexpr bool operator>=(
223 const SoundSpeed<NumericType>& left, const SoundSpeed<NumericType>& right) noexcept {
224 return left.Value() >= right.Value();
225}
226
227template <typename NumericType>
228inline std::ostream& operator<<(std::ostream& stream, const SoundSpeed<NumericType>& sound_speed) {
229 stream << sound_speed.Print();
230 return stream;
231}
232
233template <typename NumericType>
235 const NumericType number, const SoundSpeed<NumericType>& sound_speed) {
236 return sound_speed * number;
237}
238
239template <typename NumericType>
241 const SoundSpeed<NumericType>& sound_speed) const {
242 return Speed<NumericType>{this->value + sound_speed.Value()};
243}
244
245template <typename NumericType>
247 const SoundSpeed<NumericType>& sound_speed) const {
248 return Speed<NumericType>{this->value - sound_speed.Value()};
249}
250
251template <typename NumericType>
252inline constexpr void Speed<NumericType>::operator+=(
253 const SoundSpeed<NumericType>& speed) noexcept {
254 this->value += speed.Value();
255}
256
257template <typename NumericType>
258inline constexpr void Speed<NumericType>::operator-=(
259 const SoundSpeed<NumericType>& speed) noexcept {
260 this->value -= speed.Value();
261}
262
263template <typename NumericType>
265 const IsentropicBulkModulus<NumericType>& isentropic_bulk_modulus,
266 const SoundSpeed<NumericType>& sound_speed)
267 : MassDensity<NumericType>(isentropic_bulk_modulus.Value() / std::pow(sound_speed.Value(), 2)) {}
268
269template <typename NumericType>
271 const MassDensity<NumericType>& mass_density, const SoundSpeed<NumericType>& sound_speed)
272 : IsentropicBulkModulus<NumericType>(mass_density.Value() * std::pow(sound_speed.Value(), 2)) {}
273
274} // namespace PhQ
275
276namespace std {
277
278template <typename NumericType>
279struct hash<PhQ::SoundSpeed<NumericType>> {
280 inline size_t operator()(const PhQ::SoundSpeed<NumericType>& sound_speed) const {
281 return hash<NumericType>()(sound_speed.Value());
282 }
283};
284
285} // namespace std
286
287#endif // PHQ_SOUND_SPEED_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...
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...
IsentropicBulkModulus()=default
Default constructor. Constructs an isentropic bulk modulus with an uninitialized value.
Mach number of a fluid flow. Measures the local compressibility of a fluid flow. Represents the ratio...
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
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....
constexpr void operator+=(const Speed< NumericType > &speed) noexcept
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....
constexpr SoundSpeed< NumericType > operator*(const NumericType number) const
constexpr void operator+=(const SoundSpeed< NumericType > &sound_speed) noexcept
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...
static constexpr SoundSpeed< NumericType > Zero()
constexpr void operator*=(const NumericType number) noexcept
constexpr Speed< NumericType > operator+(const Speed< NumericType > &speed) const
constexpr SoundSpeed< NumericType > operator-(const SoundSpeed< NumericType > &speed) const
static constexpr SoundSpeed< NumericType > Create(const NumericType value)
Creates a sound speed from a given value and speed unit.
constexpr void operator/=(const NumericType number) noexcept
constexpr SoundSpeed< NumericType > & operator=(const SoundSpeed< NumericType > &other)=default
Copy assignment operator. Assigns this sound speed by copying another one.
constexpr void operator-=(const SoundSpeed< NumericType > &sound_speed) noexcept
constexpr Speed< NumericType > operator-(const Speed< NumericType > &speed) const
constexpr SoundSpeed(const NumericType value)
Constructor. Constructs a sound speed with a given value expressed in the standard speed unit.
constexpr SoundSpeed(SoundSpeed< NumericType > &&other) noexcept=default
Move constructor. Constructs a sound speed by moving another one.
SoundSpeed()=default
Default constructor. Constructs a sound speed with an uninitialized value.
~SoundSpeed() noexcept=default
Destructor. Destroys this sound speed.
constexpr SoundSpeed< NumericType > & operator=(const SoundSpeed< OtherNumericType > &other)
Copy assignment operator. Assigns this sound speed by copying another one.
constexpr NumericType operator/(const SoundSpeed< NumericType > &sound_speed) const noexcept
constexpr SoundSpeed< NumericType > & operator=(SoundSpeed< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this sound speed by moving another one.
constexpr SoundSpeed< NumericType > operator/(const NumericType number) const
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....
constexpr SoundSpeed< NumericType > operator+(const SoundSpeed< NumericType > &speed) const
SoundSpeed(const NumericType value, const Unit::Speed unit)
Constructs a sound speed from a given value and speed unit.
constexpr void operator-=(const Speed< NumericType > &speed) noexcept
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 void operator-=(const Speed< NumericType > &speed) noexcept
Definition Speed.hpp:253
constexpr Speed< NumericType > operator-(const Speed< NumericType > &speed) const
Definition Speed.hpp:199
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...
Speed
Speed units.
Definition Speed.hpp:53
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