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
Speed.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_SPEED_HPP
26#define PHQ_SPEED_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "Frequency.hpp"
34#include "Length.hpp"
35#include "Time.hpp"
36#include "Unit/Speed.hpp"
37
38namespace PhQ {
39
40// Forward declaration for class PhQ::Speed.
41template <typename NumericType>
42class Direction;
43
44// Forward declaration for class PhQ::Speed.
45template <typename NumericType>
46class DynamicKinematicPressure;
47
48// Forward declaration for class PhQ::Speed.
49template <typename NumericType>
50class DynamicPressure;
51
52// Forward declaration for class PhQ::Speed.
53template <typename NumericType>
55
56// Forward declaration for class PhQ::Speed.
57template <typename NumericType>
58class KinematicViscosity;
59
60// Forward declaration for class PhQ::Speed.
61template <typename NumericType>
62class MachNumber;
63
64// Forward declaration for class PhQ::Speed.
65template <typename NumericType>
66class MassDensity;
67
68// Forward declaration for class PhQ::Speed.
69template <typename NumericType>
70class PlanarVelocity;
71
72// Forward declaration for class PhQ::Speed.
73template <typename NumericType>
74class Power;
75
76// Forward declaration for class PhQ::Speed.
77template <typename NumericType>
78class ReynoldsNumber;
79
80// Forward declaration for class PhQ::Speed.
81template <typename NumericType>
82class ScalarAcceleration;
83
84// Forward declaration for class PhQ::Speed.
85template <typename NumericType>
86class SoundSpeed;
87
88// Forward declaration for class PhQ::Speed.
89template <typename NumericType>
91
92// Forward declaration for class PhQ::Speed.
93template <typename NumericType>
94class Velocity;
95
96/// \brief Scalar velocity component or magnitude of a velocity vector. For a three-dimensional
97/// Euclidean velocity vector, see PhQ::Velocity. For a two-dimensional Euclidean velocity vector in
98/// the XY plane, see PhQ::PlanarVelocity.
99template <typename NumericType = double>
100class Speed : public DimensionalScalar<Unit::Speed, NumericType> {
101public:
102 /// \brief Default constructor. Constructs a speed with an uninitialized value.
103 Speed() = default;
104
105 /// \brief Constructor. Constructs a speed with a given value expressed in a given speed unit.
106 Speed(const NumericType value, const Unit::Speed unit)
107 : DimensionalScalar<Unit::Speed, NumericType>(value, unit) {}
108
109 /// \brief Constructor. Constructs a speed from a given length and time duration using the
110 /// definition of speed.
111 constexpr Speed(const Length<NumericType>& length, const Time<NumericType>& time)
112 : Speed<NumericType>(length.Value() / time.Value()) {}
113
114 /// \brief Constructor. Constructs a speed from a given length and frequency using the definition
115 /// of speed.
116 constexpr Speed(const Length<NumericType>& length, const Frequency<NumericType>& frequency)
117 : Speed<NumericType>(length.Value() * frequency.Value()) {}
118
119 /// \brief Constructor. Constructs a speed from a given scalar acceleration and time duration
120 /// using the definition of acceleration.
121 constexpr Speed(
122 const ScalarAcceleration<NumericType>& scalar_acceleration, const Time<NumericType>& time);
123
124 /// \brief Constructor. Constructs a speed from a given scalar acceleration and frequency using
125 /// the definition of acceleration.
126 constexpr Speed(const ScalarAcceleration<NumericType>& scalar_acceleration,
127 const Frequency<NumericType>& frequency);
128
129 /// \brief Constructor. Constructs a speed from a given dynamic pressure and mass density using
130 /// the definition of dynamic pressure.
131 Speed(const DynamicPressure<NumericType>& dynamic_pressure,
132 const MassDensity<NumericType>& mass_density);
133
134 /// \brief Constructor. Constructs a speed from a given dynamic kinematic pressure using the
135 /// definition of dynamic kinematic pressure.
136 explicit Speed(const DynamicKinematicPressure<NumericType>& dynamic_kinematic_pressure);
137
138 /// \brief Constructor. Constructs a speed from a given Reynolds number, dynamic viscosity, mass
139 /// density, and length using the definition of the Reynolds number.
140 constexpr Speed(const ReynoldsNumber<NumericType>& reynolds_number,
141 const DynamicViscosity<NumericType>& dynamic_viscosity,
142 const MassDensity<NumericType>& mass_density, const Length<NumericType>& length);
143
144 /// \brief Constructor. Constructs a speed from a given Reynolds number, kinematic viscosity, and
145 /// length using the definition of the Reynolds number.
146 constexpr Speed(const ReynoldsNumber<NumericType>& reynolds_number,
147 const KinematicViscosity<NumericType>& kinematic_viscosity,
148 const Length<NumericType>& length);
149
150 /// \brief Constructor. Constructs a speed from a given sound speed and Mach number using the
151 /// definition of the Mach number.
152 constexpr Speed(
153 const SoundSpeed<NumericType>& sound_speed, const MachNumber<NumericType>& mach_number);
154
155 /// \brief Destructor. Destroys this speed.
156 ~Speed() noexcept = default;
157
158 /// \brief Copy constructor. Constructs a speed by copying another one.
159 constexpr Speed(const Speed<NumericType>& other) = default;
160
161 /// \brief Copy constructor. Constructs a speed by copying another one.
162 template <typename OtherNumericType>
163 explicit constexpr Speed(const Speed<OtherNumericType>& other)
164 : Speed(static_cast<NumericType>(other.Value())) {}
165
166 /// \brief Move constructor. Constructs a speed by moving another one.
167 constexpr Speed(Speed<NumericType>&& other) noexcept = default;
168
169 /// \brief Copy assignment operator. Assigns this speed by copying another one.
170 constexpr Speed<NumericType>& operator=(const Speed<NumericType>& other) = default;
171
172 /// \brief Copy assignment operator. Assigns this speed by copying another one.
173 template <typename OtherNumericType>
175 this->value = static_cast<NumericType>(other.Value());
176 return *this;
177 }
178
179 /// \brief Move assignment operator. Assigns this speed by moving another one.
180 constexpr Speed<NumericType>& operator=(Speed<NumericType>&& other) noexcept = default;
181
182 /// \brief Statically creates a speed of zero.
183 [[nodiscard]] static constexpr Speed<NumericType> Zero() {
184 return Speed<NumericType>{static_cast<NumericType>(0)};
185 }
186
187 /// \brief Statically creates a speed with a given value expressed in a given speed unit.
188 template <Unit::Speed Unit>
189 [[nodiscard]] static constexpr Speed<NumericType> Create(const NumericType value) {
190 return Speed<NumericType>{ConvertStatically<Unit::Speed, Unit, Standard<Unit::Speed>>(value)};
191 }
192
193 constexpr Speed<NumericType> operator+(const Speed<NumericType>& speed) const {
194 return Speed<NumericType>{this->value + speed.value};
195 }
196
197 constexpr Speed<NumericType> operator+(const SoundSpeed<NumericType>& sound_speed) const;
198
199 constexpr Speed<NumericType> operator-(const Speed<NumericType>& speed) const {
200 return Speed<NumericType>{this->value - speed.value};
201 }
202
203 constexpr Speed<NumericType> operator-(const SoundSpeed<NumericType>& sound_speed) const;
204
205 constexpr Speed<NumericType> operator*(const NumericType number) const {
206 return Speed<NumericType>{this->value * number};
207 }
208
209 constexpr Length<NumericType> operator*(const Time<NumericType>& time) const {
210 return Length<NumericType>{*this, time};
211 }
212
214 const Frequency<NumericType>& frequency) const;
215
217 const PlanarDirection<NumericType>& direction) const;
218
219 constexpr Velocity<NumericType> operator*(const Direction<NumericType>& direction) const;
220
222 const TransportEnergyConsumption<NumericType>& transport_energy_consumption) const;
223
224 constexpr Speed<NumericType> operator/(const NumericType number) const {
225 return Speed<NumericType>{this->value / number};
226 }
227
228 constexpr Length<NumericType> operator/(const Frequency<NumericType>& frequency) const {
229 return Length<NumericType>{*this, frequency};
230 }
231
232 constexpr Frequency<NumericType> operator/(const Length<NumericType>& length) const {
233 return Frequency<NumericType>{*this, length};
234 }
235
237
239 const ScalarAcceleration<NumericType>& scalar_acceleration) const;
240
241 constexpr MachNumber<NumericType> operator/(const SoundSpeed<NumericType>& sound_speed) const;
242
243 constexpr NumericType operator/(const Speed<NumericType>& speed) const noexcept {
244 return this->value / speed.value;
245 }
246
247 constexpr void operator+=(const Speed<NumericType>& speed) noexcept {
248 this->value += speed.value;
249 }
250
251 constexpr void operator+=(const SoundSpeed<NumericType>& speed) noexcept;
252
253 constexpr void operator-=(const Speed<NumericType>& speed) noexcept {
254 this->value -= speed.value;
255 }
256
257 constexpr void operator-=(const SoundSpeed<NumericType>& speed) noexcept;
258
259 constexpr void operator*=(const NumericType number) noexcept {
260 this->value *= number;
261 }
262
263 constexpr void operator/=(const NumericType number) noexcept {
264 this->value /= number;
265 }
266
267private:
268 /// \brief Constructor. Constructs a speed with a given value expressed in the standard speed
269 /// unit.
270 explicit constexpr Speed(const NumericType value)
271 : DimensionalScalar<Unit::Speed, NumericType>(value) {}
272
273 template <typename OtherNumericType>
274 friend class SoundSpeed;
275
276 template <typename OtherNumericType>
277 friend class PlanarVelocity;
278
279 template <typename OtherNumericType>
280 friend class Velocity;
281};
282
283template <typename NumericType>
284inline constexpr bool operator==(
285 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
286 return left.Value() == right.Value();
287}
288
289template <typename NumericType>
290inline constexpr bool operator!=(
291 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
292 return left.Value() != right.Value();
293}
294
295template <typename NumericType>
296inline constexpr bool operator<(
297 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
298 return left.Value() < right.Value();
299}
300
301template <typename NumericType>
302inline constexpr bool operator>(
303 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
304 return left.Value() > right.Value();
305}
306
307template <typename NumericType>
308inline constexpr bool operator<=(
309 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
310 return left.Value() <= right.Value();
311}
312
313template <typename NumericType>
314inline constexpr bool operator>=(
315 const Speed<NumericType>& left, const Speed<NumericType>& right) noexcept {
316 return left.Value() >= right.Value();
317}
318
319template <typename NumericType>
320inline std::ostream& operator<<(std::ostream& stream, const Speed<NumericType>& speed) {
321 stream << speed.Print();
322 return stream;
323}
324
325template <typename NumericType>
327 const NumericType number, const Speed<NumericType>& speed) {
328 return speed * number;
329}
330
331template <typename NumericType>
333 const Speed<NumericType>& speed, const Time<NumericType>& time)
334 : Length<NumericType>(speed.Value() * time.Value()) {}
335
336template <typename NumericType>
338 const Speed<NumericType>& speed, const Frequency<NumericType>& frequency)
339 : Length<NumericType>(speed.Value() / frequency.Value()) {}
340
341template <typename NumericType>
342inline constexpr Time<NumericType>::Time(
343 const Length<NumericType>& length, const Speed<NumericType>& speed)
344 : Time<NumericType>(length.Value() / speed.Value()) {}
345
346template <typename NumericType>
348 const Speed<NumericType>& speed, const Length<NumericType>& length)
349 : Frequency<NumericType>(speed.Value() / length.Value()) {}
350
351template <typename NumericType>
353 const Frequency<NumericType>& frequency) const {
354 return Speed<NumericType>{*this, frequency};
355}
356
357template <typename NumericType>
359 const Speed<NumericType>& speed) const {
360 return Time<NumericType>{*this, speed};
361}
362
363template <typename NumericType>
365 const Time<NumericType>& time) const {
366 return Speed<NumericType>{*this, time};
367}
368
369template <typename NumericType>
371 const Length<NumericType>& length) const {
372 return Speed<NumericType>{length, *this};
373}
374
375} // namespace PhQ
376
377namespace std {
378
379template <typename NumericType>
380struct hash<PhQ::Speed<NumericType>> {
381 inline size_t operator()(const PhQ::Speed<NumericType>& speed) const {
382 return hash<NumericType>()(speed.Value());
383 }
384};
385
386} // namespace std
387
388#endif // PHQ_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...
constexpr const PhQ::Vector< NumericType > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
Three-dimensional Euclidean direction vector. Contains three components in Cartesian coordinates: x,...
Dynamic kinematic pressure, which is dynamic pressure divided by mass density; see PhQ::DynamicPressu...
Dynamic pressure, which is the additional pressure arising from a flowing fluid's kinetic energy....
Dynamic viscosity, also known as molecular dynamic viscosity. Dynamic viscosity is the relationship b...
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition Frequency.hpp:40
constexpr Frequency< NumericType > operator*(const NumericType number) const
Frequency()=default
Default constructor. Constructs a frequency with an uninitialized value.
Kinematic viscosity, also known as molecular kinematic viscosity. Defined as dynamic viscosity divide...
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition Length.hpp:111
Length()=default
Default constructor. Constructs a length with an uninitialized value.
constexpr Length< NumericType > operator*(const NumericType number) const
Definition Length.hpp:198
constexpr Length< NumericType > operator/(const NumericType number) const
Definition Length.hpp:216
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.
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in Cartesian coord...
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition Power.hpp:51
Reynolds number of a fluid flow. Measures the local turbulence of a fluid flow. Represents the ratio ...
Scalar acceleration component or magnitude of an acceleration vector. For a three-dimensional Euclide...
Speed of sound. Applies to any deformable material, including fluids and deformable solids....
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 void operator+=(const Speed< NumericType > &speed) noexcept
Definition Speed.hpp:247
constexpr void operator-=(const Speed< NumericType > &speed) noexcept
Definition Speed.hpp:253
constexpr Length< NumericType > operator*(const Time< NumericType > &time) const
Definition Speed.hpp:209
constexpr Speed< NumericType > & operator=(const Speed< NumericType > &other)=default
Copy assignment operator. Assigns this speed by copying another one.
constexpr Speed(const Length< NumericType > &length, const Time< NumericType > &time)
Constructor. Constructs a speed from a given length and time duration using the definition of speed.
Definition Speed.hpp:111
constexpr Speed< NumericType > operator*(const NumericType number) const
Definition Speed.hpp:205
constexpr Frequency< NumericType > operator/(const Length< NumericType > &length) const
Definition Speed.hpp:232
constexpr Speed< NumericType > operator-(const Speed< NumericType > &speed) const
Definition Speed.hpp:199
constexpr Speed< NumericType > operator/(const NumericType number) const
Definition Speed.hpp:224
constexpr Speed(const Length< NumericType > &length, const Frequency< NumericType > &frequency)
Constructor. Constructs a speed from a given length and frequency using the definition of speed.
Definition Speed.hpp:116
constexpr void operator/=(const NumericType number) noexcept
Definition Speed.hpp:263
static constexpr Speed< NumericType > Zero()
Statically creates a speed of zero.
Definition Speed.hpp:183
constexpr Speed< NumericType > & operator=(Speed< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this speed by moving another one.
~Speed() noexcept=default
Destructor. Destroys this speed.
constexpr Speed(const NumericType value)
Constructor. Constructs a speed with a given value expressed in the standard speed unit.
Definition Speed.hpp:270
constexpr NumericType operator/(const Speed< NumericType > &speed) const noexcept
Definition Speed.hpp:243
constexpr Speed< NumericType > operator+(const Speed< NumericType > &speed) const
Definition Speed.hpp:193
constexpr Speed< NumericType > & operator=(const Speed< OtherNumericType > &other)
Copy assignment operator. Assigns this speed by copying another one.
Definition Speed.hpp:174
constexpr Speed(Speed< NumericType > &&other) noexcept=default
Move constructor. Constructs a speed by moving another one.
Speed(const NumericType value, const Unit::Speed unit)
Constructor. Constructs a speed with a given value expressed in a given speed unit.
Definition Speed.hpp:106
constexpr Length< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition Speed.hpp:228
static constexpr Speed< NumericType > Create(const NumericType value)
Statically creates a speed with a given value expressed in a given speed unit.
Definition Speed.hpp:189
constexpr void operator*=(const NumericType number) noexcept
Definition Speed.hpp:259
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
Transport energy consumption, also known as energy consumption in transport. A measure of energy use ...
Three-dimensional Euclidean velocity vector. Contains three components in Cartesian coordinates: x,...
Definition Velocity.hpp:55
DynamicViscosity
Dynamic viscosity units.
MassDensity
Mass density units.
Power
Power units.
Definition Power.hpp:53
TransportEnergyConsumption
Energy consumption in transport units.
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