Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
MachNumber.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_MACH_NUMBER_HPP
26 #define PHQ_MACH_NUMBER_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionlessScalar.hpp"
33 #include "SoundSpeed.hpp"
34 #include "Speed.hpp"
35 
36 namespace PhQ {
37 
38 /// \brief Mach number of a fluid flow. Measures the local compressibility of a fluid flow.
39 /// Represents the ratio of a fluid's local speed to its local speed of sound. See also PhQ::Speed
40 /// and PhQ::SoundSpeed.
41 template <typename NumericType = double>
42 class MachNumber : public DimensionlessScalar<NumericType> {
43 public:
44  /// \brief Default constructor. Constructs a Mach number with an uninitialized value.
45  MachNumber() = default;
46 
47  /// \brief Constructor. Constructs a Mach number with a given value.
48  explicit constexpr MachNumber(const NumericType value)
49  : DimensionlessScalar<NumericType>(value) {}
50 
51  /// \brief Constructor. Constructs a Mach number from a given speed and sound speed using the
52  /// definition of the Mach number.
53  constexpr MachNumber(const Speed<NumericType>& speed, const SoundSpeed<NumericType>& sound_speed)
54  : MachNumber<NumericType>(speed.Value() / sound_speed.Value()) {}
55 
56  /// \brief Destructor. Destroys this Mach number.
57  ~MachNumber() noexcept = default;
58 
59  /// \brief Copy constructor. Constructs a Mach number by copying another one.
60  constexpr MachNumber(const MachNumber<NumericType>& other) = default;
61 
62  /// \brief Copy constructor. Constructs a Mach number by copying another one.
63  template <typename OtherNumericType>
64  explicit constexpr MachNumber(const MachNumber<OtherNumericType>& other)
65  : MachNumber(static_cast<NumericType>(other.Value())) {}
66 
67  /// \brief Move constructor. Constructs a Mach number by moving another one.
68  constexpr MachNumber(MachNumber<NumericType>&& other) noexcept = default;
69 
70  /// \brief Copy assignment operator. Assigns this Mach number by copying another one.
71  constexpr MachNumber<NumericType>& operator=(const MachNumber<NumericType>& other) = default;
72 
73  /// \brief Copy assignment operator. Assigns this Mach number by copying another one.
74  template <typename OtherNumericType>
76  this->value = static_cast<NumericType>(other.Value());
77  return *this;
78  }
79 
80  /// \brief Move assignment operator. Assigns this Mach number by moving another one.
81  constexpr MachNumber<NumericType>& operator=(MachNumber<NumericType>&& other) noexcept = default;
82 
83  /// \brief Statically creates a Mach number of zero.
84  [[nodiscard]] static constexpr MachNumber<NumericType> Zero() {
85  return MachNumber<NumericType>{static_cast<NumericType>(0)};
86  }
87 
88  constexpr MachNumber<NumericType> operator+(const MachNumber<NumericType>& mach_number) const {
89  return MachNumber<NumericType>{this->value + mach_number.value};
90  }
91 
92  constexpr MachNumber<NumericType> operator-(const MachNumber<NumericType>& mach_number) const {
93  return MachNumber<NumericType>{this->value - mach_number.value};
94  }
95 
96  constexpr MachNumber<NumericType> operator*(const NumericType number) const {
97  return MachNumber<NumericType>{this->value * number};
98  }
99 
100  constexpr Speed<NumericType> operator*(const SoundSpeed<NumericType>& sound_speed) const {
101  return Speed<NumericType>{sound_speed, *this};
102  }
103 
104  constexpr MachNumber<NumericType> operator/(const NumericType number) const {
105  return MachNumber<NumericType>{this->value / number};
106  }
107 
108  constexpr NumericType operator/(const MachNumber<NumericType>& mach_number) const noexcept {
109  return this->value / mach_number.value;
110  }
111 
112  constexpr void operator+=(const MachNumber<NumericType>& mach_number) noexcept {
113  this->value += mach_number.value;
114  }
115 
116  constexpr void operator-=(const MachNumber<NumericType>& mach_number) noexcept {
117  this->value -= mach_number.value;
118  }
119 
120  constexpr void operator*=(const NumericType number) noexcept {
121  this->value *= number;
122  }
123 
124  constexpr void operator/=(const NumericType number) noexcept {
125  this->value /= number;
126  }
127 };
128 
129 template <typename NumericType>
130 inline constexpr bool operator==(
131  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
132  return left.Value() == right.Value();
133 }
134 
135 template <typename NumericType>
136 inline constexpr bool operator!=(
137  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
138  return left.Value() != right.Value();
139 }
140 
141 template <typename NumericType>
142 inline constexpr bool operator<(
143  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
144  return left.Value() < right.Value();
145 }
146 
147 template <typename NumericType>
148 inline constexpr bool operator>(
149  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
150  return left.Value() > right.Value();
151 }
152 
153 template <typename NumericType>
154 inline constexpr bool operator<=(
155  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
156  return left.Value() <= right.Value();
157 }
158 
159 template <typename NumericType>
160 inline constexpr bool operator>=(
161  const MachNumber<NumericType>& left, const MachNumber<NumericType>& right) noexcept {
162  return left.Value() >= right.Value();
163 }
164 
165 template <typename NumericType>
166 inline std::ostream& operator<<(std::ostream& stream, const MachNumber<NumericType>& mach_number) {
167  stream << mach_number.Print();
168  return stream;
169 }
170 
171 template <typename NumericType>
173  const NumericType number, const MachNumber<NumericType>& mach_number) {
174  return MachNumber<NumericType>{number * mach_number.Value()};
175 }
176 
177 template <typename NumericType>
179  const Speed<NumericType>& speed, const MachNumber<NumericType>& mach_number)
180  : SoundSpeed<NumericType>(speed.Value() / mach_number.Value()) {}
181 
182 template <typename NumericType>
183 constexpr Speed<NumericType>::Speed(
184  const SoundSpeed<NumericType>& sound_speed, const MachNumber<NumericType>& mach_number)
185  : Speed<NumericType>(sound_speed.Value() * mach_number.Value()) {}
186 
187 template <typename NumericType>
189  const MachNumber<NumericType>& mach_number) const {
190  return Speed<NumericType>{*this, mach_number};
191 }
192 
193 template <typename NumericType>
195  const SoundSpeed<NumericType>& sound_speed) const {
196  return MachNumber<NumericType>{*this, sound_speed};
197 }
198 
199 } // namespace PhQ
200 
201 namespace std {
202 
203 template <typename NumericType>
204 struct hash<PhQ::MachNumber<NumericType>> {
205  inline size_t operator()(const PhQ::MachNumber<NumericType>& mach_number) const {
206  return hash<NumericType>()(mach_number.Value());
207  }
208 };
209 
210 } // namespace std
211 
212 #endif // PHQ_MACH_NUMBER_HPP
Abstract base class that represents any dimensionless scalar physical quantity. Such a physical quant...
constexpr double Value() const noexcept
Value of this physical quantity.
double value
Value of this physical quantity.
std::string Print() const
Prints this physical quantity as a string.
Mach number of a fluid flow. Measures the local compressibility of a fluid flow. Represents the ratio...
Definition: MachNumber.hpp:42
constexpr MachNumber< NumericType > & operator=(const MachNumber< OtherNumericType > &other)
Copy assignment operator. Assigns this Mach number by copying another one.
Definition: MachNumber.hpp:75
constexpr MachNumber(const Speed< NumericType > &speed, const SoundSpeed< NumericType > &sound_speed)
Constructor. Constructs a Mach number from a given speed and sound speed using the definition of the ...
Definition: MachNumber.hpp:53
MachNumber()=default
Default constructor. Constructs a Mach number with an uninitialized value.
constexpr Speed< NumericType > operator*(const SoundSpeed< NumericType > &sound_speed) const
Definition: MachNumber.hpp:100
constexpr NumericType operator/(const MachNumber< NumericType > &mach_number) const noexcept
Definition: MachNumber.hpp:108
constexpr MachNumber< NumericType > operator/(const NumericType number) const
Definition: MachNumber.hpp:104
constexpr MachNumber(const NumericType value)
Constructor. Constructs a Mach number with a given value.
Definition: MachNumber.hpp:48
constexpr void operator*=(const NumericType number) noexcept
Definition: MachNumber.hpp:120
constexpr MachNumber< NumericType > operator+(const MachNumber< NumericType > &mach_number) const
Definition: MachNumber.hpp:88
constexpr void operator+=(const MachNumber< NumericType > &mach_number) noexcept
Definition: MachNumber.hpp:112
~MachNumber() noexcept=default
Destructor. Destroys this Mach number.
constexpr MachNumber(MachNumber< NumericType > &&other) noexcept=default
Move constructor. Constructs a Mach number by moving another one.
constexpr MachNumber< NumericType > & operator=(MachNumber< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this Mach number by moving another one.
constexpr void operator/=(const NumericType number) noexcept
Definition: MachNumber.hpp:124
constexpr MachNumber< NumericType > & operator=(const MachNumber< NumericType > &other)=default
Copy assignment operator. Assigns this Mach number by copying another one.
static constexpr MachNumber< NumericType > Zero()
Statically creates a Mach number of zero.
Definition: MachNumber.hpp:84
constexpr MachNumber< NumericType > operator*(const NumericType number) const
Definition: MachNumber.hpp:96
constexpr void operator-=(const MachNumber< NumericType > &mach_number) noexcept
Definition: MachNumber.hpp:116
constexpr MachNumber< NumericType > operator-(const MachNumber< NumericType > &mach_number) const
Definition: MachNumber.hpp:92
Speed of sound. Applies to any deformable material, including fluids and deformable solids....
Definition: SoundSpeed.hpp:56
constexpr SoundSpeed< NumericType > operator*(const NumericType number) const
Definition: SoundSpeed.hpp:145
SoundSpeed()=default
Default constructor. Constructs a sound speed with an uninitialized value.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
Speed()=default
Default constructor. Constructs a speed with an uninitialized value.
constexpr Speed< NumericType > operator/(const NumericType number) const
Definition: Speed.hpp:224
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)