Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Mass.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_HPP
26 #define PHQ_MASS_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "DimensionalScalar.hpp"
33 #include "Unit/Mass.hpp"
34 
35 namespace PhQ {
36 
37 // Forward declaration for class PhQ::Mass.
38 template <typename NumericType>
39 class Time;
40 
41 // Forward declaration for class PhQ::Mass.
42 template <typename NumericType>
43 class Energy;
44 
45 // Forward declaration for class PhQ::Mass.
46 template <typename NumericType>
47 class Frequency;
48 
49 // Forward declaration for class PhQ::Mass.
50 template <typename NumericType>
51 class GasConstant;
52 
53 // Forward declaration for class PhQ::Mass.
54 template <typename NumericType>
55 class IsobaricHeatCapacity;
56 
57 // Forward declaration for class PhQ::Mass.
58 template <typename NumericType>
59 class IsochoricHeatCapacity;
60 
61 // Forward declaration for class PhQ::Mass.
62 template <typename NumericType>
63 class MassDensity;
64 
65 // Forward declaration for class PhQ::Mass.
66 template <typename NumericType>
67 class MassRate;
68 
69 // Forward declaration for class PhQ::Mass.
70 template <typename NumericType>
71 class Power;
72 
73 // Forward declaration for class PhQ::Mass.
74 template <typename NumericType>
75 class SpecificEnergy;
76 
77 // Forward declaration for class PhQ::Mass.
78 template <typename NumericType>
79 class SpecificGasConstant;
80 
81 // Forward declaration for class PhQ::Mass.
82 template <typename NumericType>
83 class SpecificIsobaricHeatCapacity;
84 
85 // Forward declaration for class PhQ::Mass.
86 template <typename NumericType>
87 class SpecificIsochoricHeatCapacity;
88 
89 // Forward declaration for class PhQ::Mass.
90 template <typename NumericType>
91 class SpecificPower;
92 
93 // Forward declaration for class PhQ::Mass.
94 template <typename NumericType>
95 class Volume;
96 
97 /// \brief Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and
98 /// PhQ::Frequency.
99 template <typename NumericType = double>
100 class Mass : public DimensionalScalar<Unit::Mass, NumericType> {
101 public:
102  /// \brief Default constructor. Constructs a mass with an uninitialized value.
103  Mass() = default;
104 
105  /// \brief Constructor. Constructs a mass with a given value expressed in a given mass unit.
106  Mass(const NumericType value, const Unit::Mass unit)
107  : DimensionalScalar<Unit::Mass, NumericType>(value, unit) {}
108 
109  /// \brief Constructor. Constructs a mass from a given mass density and volume using the
110  /// definition of mass density.
111  constexpr Mass(const MassDensity<NumericType>& mass_density, const Volume<NumericType>& volume);
112 
113  /// \brief Constructor. Constructs a mass from a given mass rate and time using the definition of
114  /// mass rate.
115  constexpr Mass(const MassRate<NumericType>& mass_rate, const Time<NumericType>& time);
116 
117  /// \brief Constructor. Constructs a mass from a given mass rate and frequency using the
118  /// definition of mass rate.
119  constexpr Mass(const MassRate<NumericType>& mass_rate, const Frequency<NumericType>& frequency);
120 
121  /// \brief Constructor. Constructs a mass from a given energy and specific energy using the
122  /// definition of specific energy.
123  constexpr Mass(
124  const Energy<NumericType>& energy, const SpecificEnergy<NumericType>& specific_energy);
125 
126  /// \brief Constructor. Constructs a mass from a given power and specific power using the
127  /// definition of specific power.
128  constexpr Mass(const Power<NumericType>& power, const SpecificPower<NumericType>& specific_power);
129 
130  /// \brief Constructor. Constructs a mass from a given gas constant and specific gas constant
131  /// using the definition of the specific gas constant.
132  constexpr Mass(const GasConstant<NumericType>& gas_constant,
133  const SpecificGasConstant<NumericType>& specific_gas_constant);
134 
135  /// \brief Constructor. Constructs a mass from a given isobaric heat capacity and specific
136  /// isobaric heat capacity using the definition of the specific isobaric heat capacity.
137  constexpr Mass(const IsobaricHeatCapacity<NumericType>& isobaric_heat_capacity,
138  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity);
139 
140  /// \brief Constructor. Constructs a mass from a given isochoric heat capacity and specific
141  /// isochoric heat capacity using the definition of the specific isochoric heat capacity.
142  constexpr Mass(
143  const IsochoricHeatCapacity<NumericType>& isochoric_heat_capacity,
144  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity);
145 
146  /// \brief Destructor. Destroys this mass.
147  ~Mass() noexcept = default;
148 
149  /// \brief Copy constructor. Constructs a mass by copying another one.
150  constexpr Mass(const Mass<NumericType>& other) = default;
151 
152  /// \brief Copy constructor. Constructs a mass by copying another one.
153  template <typename OtherNumericType>
154  explicit constexpr Mass(const Mass<OtherNumericType>& other)
155  : Mass(static_cast<NumericType>(other.Value())) {}
156 
157  /// \brief Move constructor. Constructs a mass by moving another one.
158  constexpr Mass(Mass<NumericType>&& other) noexcept = default;
159 
160  /// \brief Copy assignment operator. Assigns this mass by copying another one.
161  constexpr Mass<NumericType>& operator=(const Mass<NumericType>& other) = default;
162 
163  /// \brief Copy assignment operator. Assigns this mass by copying another one.
164  template <typename OtherNumericType>
166  this->value = static_cast<NumericType>(other.Value());
167  return *this;
168  }
169 
170  /// \brief Move assignment operator. Assigns this mass by moving another one.
171  constexpr Mass<NumericType>& operator=(Mass<NumericType>&& other) noexcept = default;
172 
173  /// \brief Statically creates a mass of zero.
174  [[nodiscard]] static constexpr Mass<NumericType> Zero() {
175  return Mass<NumericType>{static_cast<NumericType>(0)};
176  }
177 
178  /// \brief Statically creates a mass with a given value expressed in a given mass unit.
179  template <Unit::Mass Unit>
180  [[nodiscard]] static constexpr Mass<NumericType> Create(const NumericType value) {
181  return Mass<NumericType>{ConvertStatically<Unit::Mass, Unit, Standard<Unit::Mass>>(value)};
182  }
183 
184  constexpr Mass<NumericType> operator+(const Mass<NumericType>& mass) const {
185  return Mass<NumericType>{this->value + mass.value};
186  }
187 
188  constexpr Mass<NumericType> operator-(const Mass<NumericType>& mass) const {
189  return Mass<NumericType>{this->value - mass.value};
190  }
191 
192  constexpr Mass<NumericType> operator*(const NumericType number) const {
193  return Mass<NumericType>{this->value * number};
194  }
195 
196  constexpr MassRate<NumericType> operator*(const Frequency<NumericType>& frequency) const;
197 
198  constexpr Energy<NumericType> operator*(const SpecificEnergy<NumericType>& specific_energy) const;
199 
200  constexpr Power<NumericType> operator*(const SpecificPower<NumericType>& specific_power) const;
201 
203  const SpecificIsobaricHeatCapacity<NumericType>& specific_isobaric_heat_capacity) const;
204 
206  const SpecificIsochoricHeatCapacity<NumericType>& specific_isochoric_heat_capacity) const;
207 
209  const SpecificGasConstant<NumericType>& specific_gas_constant) const;
210 
211  constexpr Mass<NumericType> operator/(const NumericType number) const {
212  return Mass<NumericType>{this->value / number};
213  }
214 
216 
217  constexpr Volume<NumericType> operator/(const MassDensity<NumericType>& mass_density) const;
218 
219  constexpr MassRate<NumericType> operator/(const Time<NumericType>& time) const;
220 
221  constexpr Time<NumericType> operator/(const MassRate<NumericType>& mass_rate) const;
222 
223  constexpr NumericType operator/(const Mass<NumericType>& mass) const noexcept {
224  return this->value / mass.value;
225  }
226 
227  constexpr void operator+=(const Mass<NumericType>& mass) noexcept {
228  this->value += mass.value;
229  }
230 
231  constexpr void operator-=(const Mass<NumericType>& mass) noexcept {
232  this->value -= mass.value;
233  }
234 
235  constexpr void operator*=(const NumericType number) noexcept {
236  this->value *= number;
237  }
238 
239  constexpr void operator/=(const NumericType number) noexcept {
240  this->value /= number;
241  }
242 
243 private:
244  /// \brief Constructor. Constructs a mass with a given value expressed in the standard mass unit.
245  explicit constexpr Mass(const NumericType value)
246  : DimensionalScalar<Unit::Mass, NumericType>(value) {}
247 };
248 
249 template <typename NumericType>
250 inline constexpr bool operator==(
251  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
252  return left.Value() == right.Value();
253 }
254 
255 template <typename NumericType>
256 inline constexpr bool operator!=(
257  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
258  return left.Value() != right.Value();
259 }
260 
261 template <typename NumericType>
262 inline constexpr bool operator<(
263  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
264  return left.Value() < right.Value();
265 }
266 
267 template <typename NumericType>
268 inline constexpr bool operator>(
269  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
270  return left.Value() > right.Value();
271 }
272 
273 template <typename NumericType>
274 inline constexpr bool operator<=(
275  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
276  return left.Value() <= right.Value();
277 }
278 
279 template <typename NumericType>
280 inline constexpr bool operator>=(
281  const Mass<NumericType>& left, const Mass<NumericType>& right) noexcept {
282  return left.Value() >= right.Value();
283 }
284 
285 template <typename NumericType>
286 inline std::ostream& operator<<(std::ostream& stream, const Mass<NumericType>& mass) {
287  stream << mass.Print();
288  return stream;
289 }
290 
291 template <typename NumericType>
292 inline constexpr Mass<NumericType> operator*(
293  const NumericType number, const Mass<NumericType>& mass) {
294  return mass * number;
295 }
296 
297 } // namespace PhQ
298 
299 namespace std {
300 
301 template <typename NumericType>
302 struct hash<PhQ::Mass<NumericType>> {
303  inline size_t operator()(const PhQ::Mass<NumericType>& mass) const {
304  return hash<NumericType>()(mass.Value());
305  }
306 };
307 
308 } // namespace std
309 
310 #endif // PHQ_MASS_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::Mass 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...
Energy physical quantity. Can represent any kind of energy, such as kinetic energy,...
Definition: Energy.hpp:73
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition: Frequency.hpp:40
Gas constant of a gas. For the mass-specific gas constant, see PhQ::SpecificGasConstant.
Definition: GasConstant.hpp:46
Isobaric heat capacity, also known as heat capacity at constant pressure. For the mass-specific isoba...
Isochoric heat capacity, also known as heat capacity at constant volume. For the mass-specific isocho...
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Definition: MassDensity.hpp:83
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
constexpr Mass< NumericType > & operator=(const Mass< NumericType > &other)=default
Copy assignment operator. Assigns this mass by copying another one.
constexpr NumericType operator/(const Mass< NumericType > &mass) const noexcept
Definition: Mass.hpp:223
constexpr void operator*=(const NumericType number) noexcept
Definition: Mass.hpp:235
constexpr Mass< NumericType > & operator=(const Mass< OtherNumericType > &other)
Copy assignment operator. Assigns this mass by copying another one.
Definition: Mass.hpp:165
constexpr MassRate< NumericType > operator*(const Frequency< NumericType > &frequency) const
constexpr Mass(const MassRate< NumericType > &mass_rate, const Frequency< NumericType > &frequency)
Constructor. Constructs a mass from a given mass rate and frequency using the definition of mass rate...
Mass()=default
Default constructor. Constructs a mass with an uninitialized value.
constexpr Mass(const MassRate< NumericType > &mass_rate, const Time< NumericType > &time)
Constructor. Constructs a mass from a given mass rate and time using the definition of mass rate.
constexpr IsobaricHeatCapacity< NumericType > operator*(const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity) const
constexpr Power< NumericType > operator*(const SpecificPower< NumericType > &specific_power) const
constexpr MassRate< NumericType > operator/(const Time< NumericType > &time) const
constexpr Mass< NumericType > & operator=(Mass< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this mass by moving another one.
constexpr Time< NumericType > operator/(const MassRate< NumericType > &mass_rate) const
constexpr GasConstant< NumericType > operator*(const SpecificGasConstant< NumericType > &specific_gas_constant) const
constexpr void operator/=(const NumericType number) noexcept
Definition: Mass.hpp:239
constexpr Mass(const Energy< NumericType > &energy, const SpecificEnergy< NumericType > &specific_energy)
Constructor. Constructs a mass from a given energy and specific energy using the definition of specif...
constexpr Mass< NumericType > operator-(const Mass< NumericType > &mass) const
Definition: Mass.hpp:188
constexpr void operator-=(const Mass< NumericType > &mass) noexcept
Definition: Mass.hpp:231
constexpr Mass(const MassDensity< NumericType > &mass_density, const Volume< NumericType > &volume)
Constructor. Constructs a mass from a given mass density and volume using the definition of mass dens...
Mass(const NumericType value, const Unit::Mass unit)
Constructor. Constructs a mass with a given value expressed in a given mass unit.
Definition: Mass.hpp:106
static constexpr Mass< NumericType > Zero()
Statically creates a mass of zero.
Definition: Mass.hpp:174
constexpr Mass< NumericType > operator*(const NumericType number) const
Definition: Mass.hpp:192
constexpr Mass(const NumericType value)
Constructor. Constructs a mass with a given value expressed in the standard mass unit.
Definition: Mass.hpp:245
constexpr void operator+=(const Mass< NumericType > &mass) noexcept
Definition: Mass.hpp:227
constexpr IsochoricHeatCapacity< NumericType > operator*(const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity) const
constexpr MassDensity< NumericType > operator/(const Volume< NumericType > &volume) const
constexpr Energy< NumericType > operator*(const SpecificEnergy< NumericType > &specific_energy) const
constexpr Mass(const IsochoricHeatCapacity< NumericType > &isochoric_heat_capacity, const SpecificIsochoricHeatCapacity< NumericType > &specific_isochoric_heat_capacity)
Constructor. Constructs a mass from a given isochoric heat capacity and specific isochoric heat capac...
static constexpr Mass< NumericType > Create(const NumericType value)
Statically creates a mass with a given value expressed in a given mass unit.
Definition: Mass.hpp:180
constexpr Mass< NumericType > operator+(const Mass< NumericType > &mass) const
Definition: Mass.hpp:184
constexpr Mass< NumericType > operator/(const NumericType number) const
Definition: Mass.hpp:211
constexpr Volume< NumericType > operator/(const MassDensity< NumericType > &mass_density) const
constexpr Mass(const IsobaricHeatCapacity< NumericType > &isobaric_heat_capacity, const SpecificIsobaricHeatCapacity< NumericType > &specific_isobaric_heat_capacity)
Constructor. Constructs a mass from a given isobaric heat capacity and specific isobaric heat capacit...
constexpr Mass(const Power< NumericType > &power, const SpecificPower< NumericType > &specific_power)
Constructor. Constructs a mass from a given power and specific power using the definition of specific...
constexpr Mass(Mass< NumericType > &&other) noexcept=default
Move constructor. Constructs a mass by moving another one.
~Mass() noexcept=default
Destructor. Destroys this mass.
constexpr Mass(const GasConstant< NumericType > &gas_constant, const SpecificGasConstant< NumericType > &specific_gas_constant)
Constructor. Constructs a mass from a given gas constant and specific gas constant using the definiti...
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition: Power.hpp:51
Mass-specific energy. Energy per unit mass; see PhQ::Energy and PhQ::Mass.
Mass-specific gas constant of a gas. Gas constant per unit mass; see PhQ::GasConstant and PhQ::Mass....
Mass-specific isobaric heat capacity, also known as mass-specific heat capacity at constant pressure,...
Mass-specific isochoric heat capacity, also known as mass-specific heat capacity at constant volume,...
Mass-specific power. Power per unit mass; see PhQ::Power and PhQ::Mass.
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
Volume. For the time rate of change of volume, see PhQ::VolumeRate; see also PhQ::Time and PhQ::Frequ...
Definition: Volume.hpp:62
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
Mass
Mass units.
Definition: Mass.hpp:53
Power
Power units.
Definition: Power.hpp:53
Frequency
Frequency units.
Definition: Frequency.hpp:53
Energy
Energy units.
Definition: Energy.hpp:53
SpecificEnergy
Mass-specific energy units.
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
SpecificPower
Mass-specific power units.
Time
Time units.
Definition: Time.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 bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)