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
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
39namespace PhQ {
40
41// Forward declaration for class PhQ::MassDensity.
42template <typename NumericType>
43class DynamicPressure;
44
45// Forward declaration for class PhQ::MassDensity.
46template <typename NumericType>
48
49// Forward declaration for class PhQ::MassDensity.
50template <typename NumericType>
51class IsentropicBulkModulus;
52
53// Forward declaration for class PhQ::MassDensity.
54template <typename NumericType>
55class KinematicViscosity;
56
57// Forward declaration for class PhQ::MassDensity.
58template <typename NumericType>
59class ReynoldsNumber;
60
61// Forward declaration for class PhQ::MassDensity.
62template <typename NumericType>
63class ScalarThermalConductivity;
64
65// Forward declaration for class PhQ::MassDensity.
66template <typename NumericType>
67class SoundSpeed;
68
69// Forward declaration for class PhQ::MassDensity.
70template <typename NumericType>
71class SpecificIsobaricHeatCapacity;
72
73// Forward declaration for class PhQ::MassDensity.
74template <typename NumericType>
75class Speed;
76
77// Forward declaration for class PhQ::MassDensity.
78template <typename NumericType>
79class ThermalDiffusivity;
80
81/// \brief Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
82template <typename NumericType = double>
83class MassDensity : public DimensionalScalar<Unit::MassDensity, NumericType> {
84public:
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.
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
221private:
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
228template <typename NumericType>
229inline constexpr bool operator==(
230 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
231 return left.Value() == right.Value();
232}
233
234template <typename NumericType>
235inline constexpr bool operator!=(
236 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
237 return left.Value() != right.Value();
238}
239
240template <typename NumericType>
241inline constexpr bool operator<(
242 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
243 return left.Value() < right.Value();
244}
245
246template <typename NumericType>
247inline constexpr bool operator>(
248 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
249 return left.Value() > right.Value();
250}
251
252template <typename NumericType>
253inline constexpr bool operator<=(
254 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
255 return left.Value() <= right.Value();
256}
257
258template <typename NumericType>
259inline constexpr bool operator>=(
260 const MassDensity<NumericType>& left, const MassDensity<NumericType>& right) noexcept {
261 return left.Value() >= right.Value();
262}
263
264template <typename NumericType>
265inline std::ostream& operator<<(
266 std::ostream& stream, const MassDensity<NumericType>& mass_density) {
267 stream << mass_density.Print();
268 return stream;
269}
270
271template <typename NumericType>
273 const NumericType number, const MassDensity<NumericType>& mass_density) {
274 return mass_density * number;
275}
276
277template <typename NumericType>
279 const Mass<NumericType>& mass, const MassDensity<NumericType>& mass_density)
280 : Volume<NumericType>(mass.Value() / mass_density.Value()) {}
281
282template <typename NumericType>
283inline constexpr Mass<NumericType>::Mass(
284 const MassDensity<NumericType>& mass_density, const Volume<NumericType>& volume)
285 : Mass<NumericType>(mass_density.Value() * volume.Value()) {}
286
287template <typename NumericType>
289 const MassDensity<NumericType>& mass_density, const VolumeRate<NumericType>& volume_rate)
290 : MassRate<NumericType>(mass_density.Value() * volume_rate.Value()) {}
291
292template <typename NumericType>
294 const MassRate<NumericType>& mass_rate, const MassDensity<NumericType>& mass_density)
295 : VolumeRate<NumericType>(mass_rate.Value() / mass_density.Value()) {}
296
297template <typename NumericType>
299 const MassDensity<NumericType>& mass_density) const {
300 return Mass<NumericType>{mass_density, *this};
301}
302
303template <typename NumericType>
305 const MassDensity<NumericType>& mass_density) const {
306 return MassRate<NumericType>{mass_density, *this};
307}
308
309template <typename NumericType>
311 const Volume<NumericType>& volume) const {
312 return MassDensity<NumericType>{*this, volume};
313}
314
315template <typename NumericType>
317 const MassDensity<NumericType>& mass_density) const {
318 return Volume<NumericType>{*this, mass_density};
319}
320
321template <typename NumericType>
323 const VolumeRate<NumericType>& volume_rate) const {
324 return MassDensity<NumericType>{*this, volume_rate};
325}
326
327template <typename NumericType>
329 const MassDensity<NumericType>& mass_density) const {
330 return VolumeRate<NumericType>{*this, mass_density};
331}
332
333} // namespace PhQ
334
335namespace std {
336
337template <typename NumericType>
338struct 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...
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...
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.
constexpr NumericType operator/(const MassDensity< NumericType > &mass_density) const noexcept
constexpr void operator/=(const NumericType number) noexcept
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
MassDensity()=default
Default constructor. Constructs a mass density with an uninitialized value.
constexpr MassDensity< NumericType > operator*(const NumericType number) const
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 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...
constexpr void operator-=(const MassDensity< NumericType > &mass_density) noexcept
~MassDensity() noexcept=default
Destructor. Destroys this mass density.
constexpr MassRate< NumericType > operator*(const VolumeRate< NumericType > &volume_rate) 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.
constexpr Mass< NumericType > operator*(const Volume< NumericType > &volume) const
constexpr MassDensity(MassDensity< NumericType > &&other) noexcept=default
Move constructor. Constructs a mass density by moving another one.
constexpr MassDensity< NumericType > operator-(const MassDensity< NumericType > &mass_density) const
constexpr MassDensity< NumericType > & operator=(const MassDensity< OtherNumericType > &other)
Copy assignment operator. Assigns this mass density by copying another one.
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...
static constexpr MassDensity< NumericType > Zero()
Statically creates a mass density of zero.
constexpr MassDensity< NumericType > operator+(const MassDensity< NumericType > &mass_density) const
constexpr MassDensity< NumericType > & operator=(const MassDensity< NumericType > &other)=default
Copy assignment operator. Assigns this mass density by copying another one.
constexpr MassDensity< NumericType > & operator=(MassDensity< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this mass density by moving another one.
Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see PhQ::Mass,...
Definition MassRate.hpp:51
MassRate()=default
Default constructor. Constructs a mass rate with an uninitialized value.
constexpr MassRate< NumericType > operator/(const NumericType number) const
Definition MassRate.hpp:131
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition Mass.hpp:100
Mass()=default
Default constructor. Constructs a mass with an uninitialized value.
constexpr Mass< NumericType > operator/(const NumericType number) const
Definition Mass.hpp:211
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....
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....
constexpr VolumeRate< NumericType > operator*(const NumericType number) const
VolumeRate()=default
Default constructor. Constructs a volume rate with an uninitialized value.
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.
constexpr Volume< NumericType > operator*(const NumericType number) const
Definition Volume.hpp:135
DynamicViscosity
Dynamic viscosity units.
MassDensity
Mass density 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