Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
DynamicViscosity.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_DYNAMIC_VISCOSITY_HPP
26 #define PHQ_DYNAMIC_VISCOSITY_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "KinematicViscosity.hpp"
34 #include "MassDensity.hpp"
36 
37 namespace PhQ {
38 
39 // Forward declaration for class PhQ::DynamicViscosity.
40 template <typename NumericType>
41 class ReynoldsNumber;
42 
43 // Forward declaration for class PhQ::DynamicViscosity.
44 template <typename NumericType>
45 class ScalarThermalConductivity;
46 
47 // Forward declaration for class PhQ::DynamicViscosity.
48 template <typename NumericType>
49 class SpecificIsobaricHeatCapacity;
50 
51 /// \brief Dynamic viscosity, also known as molecular dynamic viscosity. Dynamic viscosity is the
52 /// relationship between the viscous stress of a material and its corresponding strain rate. Not to
53 /// be confused with kinematic viscosity, which is dynamic viscosity divided by mass density; see
54 /// PhQ::KinematicViscosity and PhQ::MassDensity. Also not to be confused with bulk dynamic
55 /// viscosity; see PhQ::BulkDynamicViscosity.
56 template <typename NumericType = double>
57 class DynamicViscosity : public DimensionalScalar<Unit::DynamicViscosity, NumericType> {
58 public:
59  /// \brief Default constructor. Constructs a dynamic viscosity with an uninitialized value.
60  DynamicViscosity() = default;
61 
62  /// \brief Constructor. Constructs a dynamic viscosity with a given value expressed in a given
63  /// dynamic viscosity unit.
64  DynamicViscosity(const NumericType value, const Unit::DynamicViscosity unit)
65  : DimensionalScalar<Unit::DynamicViscosity, NumericType>(value, unit) {}
66 
67  /// \brief Constructor. Constructs a dynamic viscosity from a given mass density and kinematic
68  /// viscosity using the definition of kinematic viscosity.
69  constexpr DynamicViscosity(const MassDensity<NumericType>& mass_density,
70  const KinematicViscosity<NumericType>& kinematic_viscosity)
71  : DynamicViscosity<NumericType>(mass_density.Value() * kinematic_viscosity.Value()) {}
72 
73  /// \brief Constructor. Constructs a dynamic viscosity from a given mass density, speed, length,
74  /// and Reynolds number using the definition of the Reynolds number.
75  constexpr DynamicViscosity(
76  const MassDensity<NumericType>& mass_density, const Speed<NumericType>& speed,
77  const Length<NumericType>& length, const ReynoldsNumber<NumericType>& reynolds_number);
78 
79  /// \brief Constructor. Constructs a dynamic viscosity from a given Prandtl number, scalar thermal
80  /// conductivity, and specific isobaric heat capacity using the definition of the Prandtl number.
81  constexpr DynamicViscosity(
82  const PrandtlNumber<NumericType>& prandtl_number,
83  const ScalarThermalConductivity<NumericType>& scalar_thermal_conductivity,
84  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity);
85 
86  /// \brief Destructor. Destroys this dynamic viscosity.
87  ~DynamicViscosity() noexcept = default;
88 
89  /// \brief Copy constructor. Constructs a dynamic viscosity by copying another one.
90  constexpr DynamicViscosity(const DynamicViscosity<NumericType>& other) = default;
91 
92  /// \brief Copy constructor. Constructs a dynamic viscosity by copying another one.
93  template <typename OtherNumericType>
94  explicit constexpr DynamicViscosity(const DynamicViscosity<OtherNumericType>& other)
95  : DynamicViscosity(static_cast<NumericType>(other.Value())) {}
96 
97  /// \brief Move constructor. Constructs a dynamic viscosity by moving another one.
98  constexpr DynamicViscosity(DynamicViscosity<NumericType>&& other) noexcept = default;
99 
100  /// \brief Copy assignment operator. Assigns this dynamic viscosity by copying another one.
102  const DynamicViscosity<NumericType>& other) = default;
103 
104  /// \brief Copy assignment operator. Assigns this dynamic viscosity by copying another one.
105  template <typename OtherNumericType>
107  const DynamicViscosity<OtherNumericType>& other) {
108  this->value = static_cast<NumericType>(other.Value());
109  return *this;
110  }
111 
112  /// \brief Move assignment operator. Assigns this dynamic viscosity by moving another one.
114  DynamicViscosity<NumericType>&& other) noexcept = default;
115 
116  /// \brief Statically creates a dynamic viscosity of zero.
117  [[nodiscard]] static constexpr DynamicViscosity<NumericType> Zero() {
118  return DynamicViscosity<NumericType>{static_cast<NumericType>(0)};
119  }
120 
121  /// \brief Statically creates a dynamic viscosity with a given value expressed in a given dynamic
122  /// viscosity unit.
123  template <Unit::DynamicViscosity Unit>
124  [[nodiscard]] static constexpr DynamicViscosity<NumericType> Create(const NumericType value) {
126  ConvertStatically<Unit::DynamicViscosity, Unit, Standard<Unit::DynamicViscosity>>(value)};
127  }
128 
130  const DynamicViscosity<NumericType>& dynamic_viscosity) const {
131  return DynamicViscosity<NumericType>{this->value + dynamic_viscosity.value};
132  }
133 
135  const DynamicViscosity<NumericType>& dynamic_viscosity) const {
136  return DynamicViscosity<NumericType>{this->value - dynamic_viscosity.value};
137  }
138 
139  constexpr DynamicViscosity<NumericType> operator*(const NumericType number) const {
140  return DynamicViscosity<NumericType>{this->value * number};
141  }
142 
143  constexpr DynamicViscosity<NumericType> operator/(const NumericType number) const {
144  return DynamicViscosity<NumericType>{this->value / number};
145  }
146 
148  const MassDensity<NumericType>& mass_density) const {
149  return KinematicViscosity<NumericType>{*this, mass_density};
150  }
151 
153  const KinematicViscosity<NumericType>& kinematic_viscosity) const {
154  return MassDensity<NumericType>{*this, kinematic_viscosity};
155  }
156 
157  constexpr NumericType operator/(
158  const DynamicViscosity<NumericType>& dynamic_viscosity) const noexcept {
159  return this->value / dynamic_viscosity.value;
160  }
161 
162  constexpr void operator+=(const DynamicViscosity<NumericType>& dynamic_viscosity) noexcept {
163  this->value += dynamic_viscosity.value;
164  }
165 
166  constexpr void operator-=(const DynamicViscosity<NumericType>& dynamic_viscosity) noexcept {
167  this->value -= dynamic_viscosity.value;
168  }
169 
170  constexpr void operator*=(const NumericType number) noexcept {
171  this->value *= number;
172  }
173 
174  constexpr void operator/=(const NumericType number) noexcept {
175  this->value /= number;
176  }
177 
178 private:
179  /// \brief Constructor. Constructs a dynamic viscosity with a given value expressed in the
180  /// standard dynamic viscosity unit.
181  explicit constexpr DynamicViscosity(const NumericType value)
182  : DimensionalScalar<Unit::DynamicViscosity, NumericType>(value) {}
183 };
184 
185 template <typename NumericType>
186 inline constexpr bool operator==(const DynamicViscosity<NumericType>& left,
187  const DynamicViscosity<NumericType>& right) noexcept {
188  return left.Value() == right.Value();
189 }
190 
191 template <typename NumericType>
192 inline constexpr bool operator!=(const DynamicViscosity<NumericType>& left,
193  const DynamicViscosity<NumericType>& right) noexcept {
194  return left.Value() != right.Value();
195 }
196 
197 template <typename NumericType>
198 inline constexpr bool operator<(const DynamicViscosity<NumericType>& left,
199  const DynamicViscosity<NumericType>& right) noexcept {
200  return left.Value() < right.Value();
201 }
202 
203 template <typename NumericType>
204 inline constexpr bool operator>(const DynamicViscosity<NumericType>& left,
205  const DynamicViscosity<NumericType>& right) noexcept {
206  return left.Value() > right.Value();
207 }
208 
209 template <typename NumericType>
210 inline constexpr bool operator<=(const DynamicViscosity<NumericType>& left,
211  const DynamicViscosity<NumericType>& right) noexcept {
212  return left.Value() <= right.Value();
213 }
214 
215 template <typename NumericType>
216 inline constexpr bool operator>=(const DynamicViscosity<NumericType>& left,
217  const DynamicViscosity<NumericType>& right) noexcept {
218  return left.Value() >= right.Value();
219 }
220 
221 template <typename NumericType>
222 inline std::ostream& operator<<(
223  std::ostream& stream, const DynamicViscosity<NumericType>& dynamic_viscosity) {
224  stream << dynamic_viscosity.Print();
225  return stream;
226 }
227 
228 template <typename NumericType>
230  const NumericType number, const DynamicViscosity<NumericType>& dynamic_viscosity) {
231  return dynamic_viscosity * number;
232 }
233 
234 template <typename NumericType>
236  const DynamicViscosity<NumericType>& dynamic_viscosity,
237  const KinematicViscosity<NumericType>& kinematic_viscosity)
238  : MassDensity<NumericType>(dynamic_viscosity.Value() / kinematic_viscosity.Value()) {}
239 
240 template <typename NumericType>
242  const DynamicViscosity<NumericType>& dynamic_viscosity,
243  const MassDensity<NumericType>& mass_density)
244  : KinematicViscosity<NumericType>(dynamic_viscosity.Value() / mass_density.Value()) {}
245 
246 template <typename NumericType>
248  const MassDensity<NumericType>& mass_density) const {
249  return DynamicViscosity<NumericType>{mass_density, *this};
250 }
251 
252 template <typename NumericType>
254  const KinematicViscosity<NumericType>& kinematic_viscosity) const {
255  return DynamicViscosity<NumericType>{*this, kinematic_viscosity};
256 }
257 
258 } // namespace PhQ
259 
260 namespace std {
261 
262 template <typename NumericType>
263 struct hash<PhQ::DynamicViscosity<NumericType>> {
264  inline size_t operator()(const PhQ::DynamicViscosity<NumericType>& dynamic_viscosity) const {
265  return hash<NumericType>()(dynamic_viscosity.Value());
266  }
267 };
268 
269 } // namespace std
270 
271 #endif // PHQ_DYNAMIC_VISCOSITY_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::DynamicViscosity 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...
Dynamic viscosity, also known as molecular dynamic viscosity. Dynamic viscosity is the relationship b...
constexpr DynamicViscosity(DynamicViscosity< NumericType > &&other) noexcept=default
Move constructor. Constructs a dynamic viscosity by moving another one.
constexpr void operator*=(const NumericType number) noexcept
constexpr DynamicViscosity< NumericType > operator-(const DynamicViscosity< NumericType > &dynamic_viscosity) const
constexpr NumericType operator/(const DynamicViscosity< NumericType > &dynamic_viscosity) const noexcept
constexpr DynamicViscosity< NumericType > operator/(const NumericType number) const
constexpr void operator/=(const NumericType number) noexcept
DynamicViscosity(const NumericType value, const Unit::DynamicViscosity unit)
Constructor. Constructs a dynamic viscosity with a given value expressed in a given dynamic viscosity...
constexpr DynamicViscosity(const NumericType value)
Constructor. Constructs a dynamic viscosity with a given value expressed in the standard dynamic visc...
constexpr DynamicViscosity(const PrandtlNumber< NumericType > &prandtl_number, const ScalarThermalConductivity< NumericType > &scalar_thermal_conductivity, const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity)
Constructor. Constructs a dynamic viscosity from a given Prandtl number, scalar thermal conductivity,...
constexpr MassDensity< NumericType > operator/(const KinematicViscosity< NumericType > &kinematic_viscosity) const
constexpr void operator-=(const DynamicViscosity< NumericType > &dynamic_viscosity) noexcept
constexpr DynamicViscosity< NumericType > operator*(const NumericType number) const
constexpr DynamicViscosity< NumericType > & operator=(const DynamicViscosity< OtherNumericType > &other)
Copy assignment operator. Assigns this dynamic viscosity by copying another one.
constexpr DynamicViscosity< NumericType > & operator=(const DynamicViscosity< NumericType > &other)=default
Copy assignment operator. Assigns this dynamic viscosity by copying another one.
constexpr DynamicViscosity(const MassDensity< NumericType > &mass_density, const KinematicViscosity< NumericType > &kinematic_viscosity)
Constructor. Constructs a dynamic viscosity from a given mass density and kinematic viscosity using t...
static constexpr DynamicViscosity< NumericType > Create(const NumericType value)
Statically creates a dynamic viscosity with a given value expressed in a given dynamic viscosity unit...
constexpr KinematicViscosity< NumericType > operator/(const MassDensity< NumericType > &mass_density) const
static constexpr DynamicViscosity< NumericType > Zero()
Statically creates a dynamic viscosity of zero.
constexpr DynamicViscosity< NumericType > & operator=(DynamicViscosity< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dynamic viscosity by moving another one.
~DynamicViscosity() noexcept=default
Destructor. Destroys this dynamic viscosity.
DynamicViscosity()=default
Default constructor. Constructs a dynamic viscosity with an uninitialized value.
constexpr void operator+=(const DynamicViscosity< NumericType > &dynamic_viscosity) noexcept
constexpr DynamicViscosity(const MassDensity< NumericType > &mass_density, const Speed< NumericType > &speed, const Length< NumericType > &length, const ReynoldsNumber< NumericType > &reynolds_number)
Constructor. Constructs a dynamic viscosity from a given mass density, speed, length,...
constexpr DynamicViscosity< NumericType > operator+(const DynamicViscosity< NumericType > &dynamic_viscosity) const
Kinematic viscosity, also known as molecular kinematic viscosity. Defined as dynamic viscosity divide...
constexpr KinematicViscosity< NumericType > operator*(const NumericType number) const
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition: Length.hpp:111
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Definition: MassDensity.hpp:83
constexpr MassDensity< NumericType > operator*(const NumericType number) const
MassDensity()=default
Default constructor. Constructs a mass density with an uninitialized value.
Prandtl number of a fluid. Represents the ratio of the momentum diffusivity to the thermal diffusivit...
Reynolds number of a fluid flow. Measures the local turbulence of a fluid flow. Represents the ratio ...
Scalar component or resultant of a three-dimensional Euclidean thermal conductivity symmetric dyadic ...
Mass-specific isobaric heat capacity, also known as mass-specific heat capacity at constant pressure,...
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition: Speed.hpp:100
DynamicViscosity
Dynamic viscosity units.
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
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)