Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
MassDensity.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_MASS_DENSITY_HPP
26 #define PHQ_MASS_DENSITY_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Mass.hpp"
34 #include "MassRate.hpp"
35 #include "Unit/MassDensity.hpp"
36 #include "Volume.hpp"
37 #include "VolumeRate.hpp"
38 
39 namespace PhQ {
40 
41 // Forward declaration for class PhQ::MassDensity.
42 template <typename NumericType>
43 class DynamicPressure;
44 
45 // Forward declaration for class PhQ::MassDensity.
46 template <typename NumericType>
47 class DynamicViscosity;
48 
49 // Forward declaration for class PhQ::MassDensity.
50 template <typename NumericType>
51 class IsentropicBulkModulus;
52 
53 // Forward declaration for class PhQ::MassDensity.
54 template <typename NumericType>
55 class KinematicViscosity;
56 
57 // Forward declaration for class PhQ::MassDensity.
58 template <typename NumericType>
59 class ReynoldsNumber;
60 
61 // Forward declaration for class PhQ::MassDensity.
62 template <typename NumericType>
63 class ScalarThermalConductivity;
64 
65 // Forward declaration for class PhQ::MassDensity.
66 template <typename NumericType>
67 class SoundSpeed;
68 
69 // Forward declaration for class PhQ::MassDensity.
70 template <typename NumericType>
71 class SpecificIsobaricHeatCapacity;
72 
73 // Forward declaration for class PhQ::MassDensity.
74 template <typename NumericType>
75 class Speed;
76 
77 // Forward declaration for class PhQ::MassDensity.
78 template <typename NumericType>
79 class ThermalDiffusivity;
80 
81 /// \brief Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
82 template <typename NumericType = double>
83 class MassDensity : public DimensionalScalar<Unit::MassDensity, NumericType> {
84 public:
85  /// \brief Default constructor. Constructs a mass density with an uninitialized value.
86  MassDensity() = default;
87 
88  /// \brief Constructor. Constructs a mass density with a given value expressed in a given mass
89  /// density unit.
90  MassDensity(const NumericType value, const Unit::MassDensity unit)
91  : DimensionalScalar<Unit::MassDensity, NumericType>(value, unit) {}
92 
93  /// \brief Constructor. Constructs a mass density from a given mass and volume using the
94  /// definition of mass density.
95  constexpr MassDensity(const Mass<NumericType>& mass, const Volume<NumericType>& volume)
96  : MassDensity<NumericType>(mass.Value() / volume.Value()) {}
97 
98  /// \brief Constructor. Constructs a mass density from a given mass rate and volume rate using the
99  /// definition of mass density.
100  constexpr MassDensity(
101  const MassRate<NumericType>& mass_rate, const VolumeRate<NumericType>& volume_rate)
102  : MassDensity<NumericType>(mass_rate.Value() / volume_rate.Value()) {}
103 
104  /// \brief Constructor. Constructs a mass density from a given dynamic viscosity and kinematic
105  /// viscosity using the definition of kinematic viscosity.
106  constexpr MassDensity(const DynamicViscosity<NumericType>& dynamic_viscosity,
107  const KinematicViscosity<NumericType>& kinematic_viscosity);
108 
109  /// \brief Constructor. Constructs a mass density from a given scalar thermal conductivity,
110  /// thermal diffusivity, and specific isobaric heat capacity using the definition of thermal
111  /// diffusivity.
112  constexpr MassDensity(
113  const ScalarThermalConductivity<NumericType>& scalar_thermal_conductivity,
114  const ThermalDiffusivity<NumericType>& thermal_diffusivity,
115  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity);
116 
117  /// \brief Constructor. Constructs a mass density from a given dynamic pressure and speed using
118  /// the definition of dynamic pressure.
119  constexpr MassDensity(
120  const DynamicPressure<NumericType>& dynamic_pressure, const Speed<NumericType>& speed);
121 
122  /// \brief Constructor. Constructs a mass density from a given Reynolds number, dynamic viscosity,
123  /// speed, and length using the definition of the Reynolds number.
124  constexpr MassDensity(const ReynoldsNumber<NumericType>& reynolds_number,
125  const DynamicViscosity<NumericType>& dynamic_viscosity,
126  const Speed<NumericType>& speed, const Length<NumericType>& length);
127 
128  /// \brief Constructor. Constructs a mass density from a given isentropic bulk modulus and sound
129  /// speed using the definition of the isentropic bulk modulus.
130  constexpr MassDensity(const IsentropicBulkModulus<NumericType>& isentropic_bulk_modulus,
131  const SoundSpeed<NumericType>& sound_speed);
132 
133  /// \brief Destructor. Destroys this mass density.
134  ~MassDensity() noexcept = default;
135 
136  /// \brief Copy constructor. Constructs a mass density by copying another one.
137  constexpr MassDensity(const MassDensity<NumericType>& other) = default;
138 
139  /// \brief Copy constructor. Constructs a mass density by copying another one.
140  template <typename OtherNumericType>
141  explicit constexpr MassDensity(const MassDensity<OtherNumericType>& other)
142  : MassDensity(static_cast<NumericType>(other.Value())) {}
143 
144  /// \brief Move constructor. Constructs a mass density by moving another one.
145  constexpr MassDensity(MassDensity<NumericType>&& other) noexcept = default;
146 
147  /// \brief Copy assignment operator. Assigns this mass density by copying another one.
148  constexpr MassDensity<NumericType>& operator=(const MassDensity<NumericType>& other) = default;
149 
150  /// \brief Copy assignment operator. Assigns this mass density by copying another one.
151  template <typename OtherNumericType>
153  this->value = static_cast<NumericType>(other.Value());
154  return *this;
155  }
156 
157  /// \brief Move assignment operator. Assigns this mass density by moving another one.
159  MassDensity<NumericType>&& other) noexcept = default;
160 
161  /// \brief Statically creates a mass density of zero.
162  [[nodiscard]] static constexpr MassDensity<NumericType> Zero() {
163  return MassDensity<NumericType>{static_cast<NumericType>(0)};
164  }
165 
166  /// \brief Statically creates a mass density with a given value expressed in a given mass density
167  /// unit.
168  template <Unit::MassDensity Unit>
169  [[nodiscard]] static constexpr MassDensity<NumericType> Create(const NumericType value) {
171  ConvertStatically<Unit::MassDensity, Unit, Standard<Unit::MassDensity>>(value)};
172  }
173 
174  constexpr MassDensity<NumericType> operator+(const MassDensity<NumericType>& mass_density) const {
175  return MassDensity<NumericType>{this->value + mass_density.value};
176  }
177 
178  constexpr MassDensity<NumericType> operator-(const MassDensity<NumericType>& mass_density) const {
179  return MassDensity<NumericType>{this->value - mass_density.value};
180  }
181 
182  constexpr MassDensity<NumericType> operator*(const NumericType number) const {
183  return MassDensity<NumericType>{this->value * number};
184  }
185 
186  constexpr Mass<NumericType> operator*(const Volume<NumericType>& volume) const {
187  return Mass<NumericType>{*this, volume};
188  }
189 
190  constexpr MassRate<NumericType> operator*(const VolumeRate<NumericType>& volume_rate) const {
191  return MassRate<NumericType>{*this, volume_rate};
192  }
193 
195  const KinematicViscosity<NumericType>& kinematic_viscosity) const;
196 
197  constexpr MassDensity<NumericType> operator/(const NumericType number) const {
198  return MassDensity<NumericType>{this->value / number};
199  }
200 
201  constexpr NumericType operator/(const MassDensity<NumericType>& mass_density) const noexcept {
202  return this->value / mass_density.value;
203  }
204 
205  constexpr void operator+=(const MassDensity<NumericType>& mass_density) noexcept {
206  this->value += mass_density.value;
207  }
208 
209  constexpr void operator-=(const MassDensity<NumericType>& mass_density) noexcept {
210  this->value -= mass_density.value;
211  }
212 
213  constexpr void operator*=(const NumericType number) noexcept {
214  this->value *= number;
215  }
216 
217  constexpr void operator/=(const NumericType number) noexcept {
218  this->value /= number;
219  }
220 
221 private:
222  /// \brief Constructor. Constructs a mass density with a given value expressed in the standard
223  /// mass density unit.
224  explicit constexpr MassDensity(const NumericType value)
225  : DimensionalScalar<Unit::MassDensity, NumericType>(value) {}
226 };
227 
228 template <typename NumericType>
229 inline constexpr bool operator==(
230  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
231  return left.Value() == right.Value();
232 }
233 
234 template <typename NumericType>
235 inline constexpr bool operator!=(
236  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
237  return left.Value() != right.Value();
238 }
239 
240 template <typename NumericType>
241 inline constexpr bool operator<(
242  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
243  return left.Value() < right.Value();
244 }
245 
246 template <typename NumericType>
247 inline constexpr bool operator>(
248  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
249  return left.Value() > right.Value();
250 }
251 
252 template <typename NumericType>
253 inline constexpr bool operator<=(
254  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
255  return left.Value() <= right.Value();
256 }
257 
258 template <typename NumericType>
259 inline constexpr bool operator>=(
260  const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
261  return left.Value() >= right.Value();
262 }
263 
264 template <typename NumericType>
265 inline std::ostream& operator<<(
266  std::ostream& stream, const MassDensity<NumericType>& mass_density) {
267  stream << mass_density.Print();
268  return stream;
269 }
270 
271 template <typename NumericType>
273  const NumericType number, const MassDensity<NumericType>& mass_density) {
274  return mass_density * number;
275 }
276 
277 template <typename NumericType>
278 inline constexpr Volume<NumericType>::Volume(
279  const Mass<NumericType>& mass, const MassDensity<NumericType>& mass_density)
280  : Volume<NumericType>(mass.Value() / mass_density.Value()) {}
281 
282 template <typename NumericType>
283 inline constexpr Mass<NumericType>::Mass(
284  const MassDensity<NumericType>& mass_density, const Volume<NumericType>& volume)
285  : Mass<NumericType>(mass_density.Value() * volume.Value()) {}
286 
287 template <typename NumericType>
288 inline constexpr MassRate<NumericType>::MassRate(
289  const MassDensity<NumericType>& mass_density, const VolumeRate<NumericType>& volume_rate)
290  : MassRate<NumericType>(mass_density.Value() * volume_rate.Value()) {}
291 
292 template <typename NumericType>
293 inline constexpr VolumeRate<NumericType>::VolumeRate(
294  const MassRate<NumericType>& mass_rate, const MassDensity<NumericType>& mass_density)
295  : VolumeRate<NumericType>(mass_rate.Value() / mass_density.Value()) {}
296 
297 template <typename NumericType>
298 inline constexpr Mass<NumericType> Volume<NumericType>::operator*(
299  const MassDensity<NumericType>& mass_density) const {
300  return Mass<NumericType>{mass_density, *this};
301 }
302 
303 template <typename NumericType>
304 inline constexpr MassRate<NumericType> VolumeRate<NumericType>::operator*(
305  const MassDensity<NumericType>& mass_density) const {
306  return MassRate<NumericType>{mass_density, *this};
307 }
308 
309 template <typename NumericType>
310 inline constexpr MassDensity<NumericType> Mass<NumericType>::operator/(
311  const Volume<NumericType>& volume) const {
312  return MassDensity<NumericType>{*this, volume};
313 }
314 
315 template <typename NumericType>
316 inline constexpr Volume<NumericType> Mass<NumericType>::operator/(
317  const MassDensity<NumericType>& mass_density) const {
318  return Volume<NumericType>{*this, mass_density};
319 }
320 
321 template <typename NumericType>
322 inline constexpr MassDensity<NumericType> MassRate<NumericType>::operator/(
323  const VolumeRate<NumericType>& volume_rate) const {
324  return MassDensity<NumericType>{*this, volume_rate};
325 }
326 
327 template <typename NumericType>
328 inline constexpr VolumeRate<NumericType> MassRate<NumericType>::operator/(
329  const MassDensity<NumericType>& mass_density) const {
330  return VolumeRate<NumericType>{*this, mass_density};
331 }
332 
333 } // namespace PhQ
334 
335 namespace std {
336 
337 template <typename NumericType>
338 struct hash<PhQ::MassDensity<NumericType>> {
339  inline size_t operator()(const PhQ::MassDensity<NumericType>& mass_density) const {
340  return hash<NumericType>()(mass_density.Value());
341  }
342 };
343 
344 } // namespace std
345 
346 #endif // PHQ_MASS_DENSITY_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::MassDensity 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 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...
Isentropic bulk modulus. Not to be confused with the isothermal bulk modulus; see PhQ::IsothermalBulk...
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
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Definition: MassDensity.hpp:83
constexpr NumericType operator/(const MassDensity< NumericType > &mass_density) const noexcept
constexpr MassDensity(const IsentropicBulkModulus< NumericType > &isentropic_bulk_modulus, const SoundSpeed< NumericType > &sound_speed)
Constructor. Constructs a mass density from a given isentropic bulk modulus and sound speed using the...
constexpr void operator/=(const NumericType number) noexcept
constexpr MassDensity(const DynamicViscosity< NumericType > &dynamic_viscosity, const KinematicViscosity< NumericType > &kinematic_viscosity)
Constructor. Constructs a mass density from a given dynamic viscosity and kinematic viscosity using t...
constexpr MassDensity< NumericType > & operator=(MassDensity< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this mass density by moving another one.
constexpr MassDensity(const MassRate< NumericType > &mass_rate, const VolumeRate< NumericType > &volume_rate)
Constructor. Constructs a mass density from a given mass rate and volume rate using the definition of...
constexpr MassDensity< NumericType > operator*(const NumericType number) const
constexpr MassDensity(const DynamicPressure< NumericType > &dynamic_pressure, const Speed< NumericType > &speed)
Constructor. Constructs a mass density from a given dynamic pressure and speed using the definition o...
static constexpr MassDensity< NumericType > Zero()
Statically creates a mass density of zero.
MassDensity()=default
Default constructor. Constructs a mass density with an uninitialized value.
constexpr MassDensity< NumericType > operator+(const MassDensity< NumericType > &mass_density) const
constexpr MassDensity(const Mass< NumericType > &mass, const Volume< NumericType > &volume)
Constructor. Constructs a mass density from a given mass and volume using the definition of mass dens...
Definition: MassDensity.hpp:95
constexpr void operator-=(const MassDensity< NumericType > &mass_density) noexcept
constexpr Mass< NumericType > operator*(const Volume< NumericType > &volume) const
~MassDensity() noexcept=default
Destructor. Destroys this mass density.
static constexpr MassDensity< NumericType > Create(const NumericType value)
Statically creates a mass density with a given value expressed in a given mass density unit.
constexpr DynamicViscosity< NumericType > operator*(const KinematicViscosity< NumericType > &kinematic_viscosity) const
constexpr void operator+=(const MassDensity< NumericType > &mass_density) noexcept
MassDensity(const NumericType value, const Unit::MassDensity unit)
Constructor. Constructs a mass density with a given value expressed in a given mass density unit.
Definition: MassDensity.hpp:90
constexpr MassDensity(MassDensity< NumericType > &&other) noexcept=default
Move constructor. Constructs a mass density by moving another one.
constexpr MassDensity(const ScalarThermalConductivity< NumericType > &scalar_thermal_conductivity, const ThermalDiffusivity< NumericType > &thermal_diffusivity, const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity)
Constructor. Constructs a mass density from a given scalar thermal conductivity, thermal diffusivity,...
constexpr MassRate< NumericType > operator*(const VolumeRate< NumericType > &volume_rate) const
constexpr void operator*=(const NumericType number) noexcept
constexpr MassDensity(const NumericType value)
Constructor. Constructs a mass density with a given value expressed in the standard mass density unit...
constexpr MassDensity< NumericType > & operator=(const MassDensity< NumericType > &other)=default
Copy assignment operator. Assigns this mass density by copying another one.
constexpr MassDensity< NumericType > operator/(const NumericType number) const
constexpr MassDensity< NumericType > & operator=(const MassDensity< OtherNumericType > &other)
Copy assignment operator. Assigns this mass density by copying another one.
constexpr MassDensity(const ReynoldsNumber< NumericType > &reynolds_number, const DynamicViscosity< NumericType > &dynamic_viscosity, const Speed< NumericType > &speed, const Length< NumericType > &length)
Constructor. Constructs a mass density from a given Reynolds number, dynamic viscosity,...
constexpr MassDensity< NumericType > operator-(const MassDensity< NumericType > &mass_density) const
Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see PhQ::Mass,...
Definition: MassRate.hpp:51
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition: Mass.hpp:100
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 ...
Speed of sound. Applies to any deformable material, including fluids and deformable solids....
Definition: SoundSpeed.hpp:56
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
Thermal diffusivity of a material. Measures the rate of heat transfer inside a material....
Volume rate. Can represent a time rate of change of a volume or a volume flow rate....
Definition: VolumeRate.hpp:51
Volume. For the time rate of change of volume, see PhQ::VolumeRate; see also PhQ::Time and PhQ::Frequ...
Definition: Volume.hpp:62
Volume()=default
Default constructor. Constructs a volume with an uninitialized value.
VolumeRate
Volume rate units. Can represent a time rate of change of a volume or a volume flow rate.
Definition: VolumeRate.hpp:53
DynamicViscosity
Dynamic viscosity units.
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
Mass
Mass units.
Definition: Mass.hpp:53
MassRate
Mass rate units. Can represent the time rate of change of a mass or a mass flow rate.
Definition: MassRate.hpp:53
Volume
Volume units.
Definition: Volume.hpp:53
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 MassDensity< NumericType > operator*(const NumericType number, const MassDensity< NumericType > &mass_density)
constexpr Dyad< NumericType > operator/(const Dyad< NumericType > &dyad, const OtherNumericType number)
Definition: Dyad.hpp:696
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)