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
MassRate.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_RATE_HPP
26#define PHQ_MASS_RATE_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "Frequency.hpp"
34#include "Mass.hpp"
35#include "Time.hpp"
36#include "Unit/MassRate.hpp"
37
38namespace PhQ {
39
40// Forward declaration for class PhQ::MassRate.
41template <typename NumericType>
42class MassDensity;
43
44// Forward declaration for class PhQ::MassRate.
45template <typename NumericType>
46class VolumeRate;
47
48/// \brief Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see
49/// PhQ::Mass, PhQ::Time, and PhQ::Frequency.
50template <typename NumericType = double>
51class MassRate : public DimensionalScalar<Unit::MassRate, NumericType> {
52public:
53 /// \brief Default constructor. Constructs a mass rate with an uninitialized value.
54 MassRate() = default;
55
56 /// \brief Constructor. Constructs a mass rate with a given value expressed in a given mass rate
57 /// unit.
58 MassRate(const NumericType value, const Unit::MassRate unit)
59 : DimensionalScalar<Unit::MassRate, NumericType>(value, unit) {}
60
61 /// \brief Constructor. Constructs a mass rate from a given mass and time using the definition of
62 /// mass rate.
63 constexpr MassRate(const Mass<NumericType>& mass, const Time<NumericType>& time)
64 : MassRate<NumericType>(mass.Value() / time.Value()) {}
65
66 /// \brief Constructor. Constructs a mass rate from a given mass and frequency using the
67 /// definition of mass rate.
68 constexpr MassRate(const Mass<NumericType>& mass, const Frequency<NumericType>& frequency)
69 : MassRate<NumericType>(mass.Value() * frequency.Value()) {}
70
71 /// \brief Constructor. Constructs a mass rate from a given mass density and volume rate using the
72 /// definition of mass density.
73 constexpr MassRate(
74 const MassDensity<NumericType>& mass_density, const VolumeRate<NumericType>& volume_rate);
75
76 /// \brief Destructor. Destroys this mass rate.
77 ~MassRate() noexcept = default;
78
79 /// \brief Copy constructor. Constructs a mass rate by copying another one.
80 constexpr MassRate(const MassRate<NumericType>& other) = default;
81
82 /// \brief Copy constructor. Constructs a mass rate by copying another one.
83 template <typename OtherNumericType>
84 explicit constexpr MassRate(const MassRate<OtherNumericType>& other)
85 : MassRate(static_cast<NumericType>(other.Value())) {}
86
87 /// \brief Move constructor. Constructs a mass rate by moving another one.
88 constexpr MassRate(MassRate<NumericType>&& other) noexcept = default;
89
90 /// \brief Copy assignment operator. Assigns this mass rate by copying another one.
91 constexpr MassRate<NumericType>& operator=(const MassRate<NumericType>& other) = default;
92
93 /// \brief Copy assignment operator. Assigns this mass rate by copying another one.
94 template <typename OtherNumericType>
96 this->value = static_cast<NumericType>(other.Value());
97 return *this;
98 }
99
100 /// \brief Move assignment operator. Assigns this mass rate by moving another one.
101 constexpr MassRate<NumericType>& operator=(MassRate<NumericType>&& other) noexcept = default;
102
103 /// \brief Statically creates a mass rate of zero.
104 [[nodiscard]] static constexpr MassRate<NumericType> Zero() {
105 return MassRate<NumericType>{static_cast<NumericType>(0)};
106 }
107
108 /// \brief Statically creates a mass rate with a given value expressed in a given mass rate unit.
109 template <Unit::MassRate Unit>
110 [[nodiscard]] static constexpr MassRate<NumericType> Create(const NumericType value) {
112 ConvertStatically<Unit::MassRate, Unit, Standard<Unit::MassRate>>(value)};
113 }
114
115 constexpr MassRate<NumericType> operator+(const MassRate<NumericType>& mass_rate) const {
116 return MassRate<NumericType>{this->value + mass_rate.value};
117 }
118
119 constexpr MassRate<NumericType> operator-(const MassRate<NumericType>& mass_rate) const {
120 return MassRate<NumericType>{this->value - mass_rate.value};
121 }
122
123 constexpr MassRate<NumericType> operator*(const NumericType number) const {
124 return MassRate<NumericType>{this->value * number};
125 }
126
127 constexpr Mass<NumericType> operator*(const Time<NumericType>& time) const {
128 return Mass<NumericType>{*this, time};
129 }
130
131 constexpr MassRate<NumericType> operator/(const NumericType number) const {
132 return MassRate<NumericType>{this->value / number};
133 }
134
136 return Frequency<NumericType>{*this, mass};
137 }
138
139 constexpr Mass<NumericType> operator/(const Frequency<NumericType>& frequency) const {
140 return Mass<NumericType>{*this, frequency};
141 }
142
143 constexpr MassDensity<NumericType> operator/(const VolumeRate<NumericType>& volume_rate) const;
144
145 constexpr VolumeRate<NumericType> operator/(const MassDensity<NumericType>& mass_density) const;
146
147 constexpr NumericType operator/(const MassRate<NumericType>& mass_rate) const noexcept {
148 return this->value / mass_rate.value;
149 }
150
151 constexpr void operator+=(const MassRate<NumericType>& mass_rate) noexcept {
152 this->value += mass_rate.value;
153 }
154
155 constexpr void operator-=(const MassRate<NumericType>& mass_rate) noexcept {
156 this->value -= mass_rate.value;
157 }
158
159 constexpr void operator*=(const NumericType number) noexcept {
160 this->value *= number;
161 }
162
163 constexpr void operator/=(const NumericType number) noexcept {
164 this->value /= number;
165 }
166
167private:
168 /// \brief Constructor. Constructs a mass rate with a given value expressed in the standard mass
169 /// rate unit.
170 explicit constexpr MassRate(const NumericType value)
171 : DimensionalScalar<Unit::MassRate, NumericType>(value) {}
172};
173
174template <typename NumericType>
175inline constexpr bool operator==(
176 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
177 return left.Value() == right.Value();
178}
179
180template <typename NumericType>
181inline constexpr bool operator!=(
182 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
183 return left.Value() != right.Value();
184}
185
186template <typename NumericType>
187inline constexpr bool operator<(
188 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
189 return left.Value() < right.Value();
190}
191
192template <typename NumericType>
193inline constexpr bool operator>(
194 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
195 return left.Value() > right.Value();
196}
197
198template <typename NumericType>
199inline constexpr bool operator<=(
200 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
201 return left.Value() <= right.Value();
202}
203
204template <typename NumericType>
205inline constexpr bool operator>=(
206 const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
207 return left.Value() >= right.Value();
208}
209
210template <typename NumericType>
211inline std::ostream& operator<<(std::ostream& stream, const MassRate<NumericType>& mass_rate) {
212 stream << mass_rate.Print();
213 return stream;
214}
215
216template <typename NumericType>
218 const NumericType number, const MassRate<NumericType>& mass_rate) {
219 return mass_rate * number;
220}
221
222template <typename NumericType>
223inline constexpr Time<NumericType>::Time(
224 const Mass<NumericType>& mass, const MassRate<NumericType>& mass_rate)
225 : Time<NumericType>(mass.Value() / mass_rate.Value()) {}
226
227template <typename NumericType>
229 const MassRate<NumericType>& mass_rate, const Mass<NumericType>& mass)
230 : Frequency<NumericType>(mass_rate.Value() / mass.Value()) {}
231
232template <typename NumericType>
233inline constexpr Mass<NumericType>::Mass(
234 const MassRate<NumericType>& mass_rate, const Time<NumericType>& time)
235 : Mass<NumericType>(mass_rate.Value() * time.Value()) {}
236
237template <typename NumericType>
238inline constexpr Mass<NumericType>::Mass(
239 const MassRate<NumericType>& mass_rate, const Frequency<NumericType>& frequency)
240 : Mass<NumericType>(mass_rate.Value() / frequency.Value()) {}
241
242template <typename NumericType>
244 const MassRate<NumericType>& mass_rate) const {
245 return Mass<NumericType>{mass_rate, *this};
246}
247
248template <typename NumericType>
250 const Frequency<NumericType>& frequency) const {
251 return MassRate<NumericType>{*this, frequency};
252}
253
254template <typename NumericType>
256 const Mass<NumericType>& mass) const {
257 return MassRate<NumericType>{mass, *this};
258}
259
260template <typename NumericType>
262 const Time<NumericType>& time) const {
263 return MassRate<NumericType>{*this, time};
264}
265
266template <typename NumericType>
268 const MassRate<NumericType>& mass_rate) const {
269 return Time<NumericType>{*this, mass_rate};
270}
271
272} // namespace PhQ
273
274namespace std {
275
276template <typename NumericType>
277struct hash<PhQ::MassRate<NumericType>> {
278 inline size_t operator()(const PhQ::MassRate<NumericType>& mass_rate) const {
279 return hash<NumericType>()(mass_rate.Value());
280 }
281};
282
283} // namespace std
284
285#endif // PHQ_MASS_RATE_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...
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.
Mass density. Mass per unit volume; see PhQ::Mass and PhQ::Volume.
Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see PhQ::Mass,...
Definition MassRate.hpp:51
constexpr Mass< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition MassRate.hpp:139
MassRate(const NumericType value, const Unit::MassRate unit)
Constructor. Constructs a mass rate with a given value expressed in a given mass rate unit.
Definition MassRate.hpp:58
constexpr MassRate< NumericType > operator+(const MassRate< NumericType > &mass_rate) const
Definition MassRate.hpp:115
constexpr void operator-=(const MassRate< NumericType > &mass_rate) noexcept
Definition MassRate.hpp:155
static constexpr MassRate< NumericType > Create(const NumericType value)
Statically creates a mass rate with a given value expressed in a given mass rate unit.
Definition MassRate.hpp:110
~MassRate() noexcept=default
Destructor. Destroys this mass rate.
constexpr MassRate< NumericType > operator*(const NumericType number) const
Definition MassRate.hpp:123
constexpr MassRate< NumericType > & operator=(MassRate< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this mass rate by moving another one.
constexpr void operator/=(const NumericType number) noexcept
Definition MassRate.hpp:163
constexpr MassRate(const NumericType value)
Constructor. Constructs a mass rate with a given value expressed in the standard mass rate unit.
Definition MassRate.hpp:170
MassRate()=default
Default constructor. Constructs a mass rate with an uninitialized value.
constexpr MassRate(MassRate< NumericType > &&other) noexcept=default
Move constructor. Constructs a mass rate by moving another one.
constexpr NumericType operator/(const MassRate< NumericType > &mass_rate) const noexcept
Definition MassRate.hpp:147
constexpr Frequency< NumericType > operator/(const Mass< NumericType > &mass) const
Definition MassRate.hpp:135
constexpr void operator+=(const MassRate< NumericType > &mass_rate) noexcept
Definition MassRate.hpp:151
constexpr Mass< NumericType > operator*(const Time< NumericType > &time) const
Definition MassRate.hpp:127
constexpr MassRate< NumericType > operator-(const MassRate< NumericType > &mass_rate) const
Definition MassRate.hpp:119
constexpr MassRate(const Mass< NumericType > &mass, const Time< NumericType > &time)
Constructor. Constructs a mass rate from a given mass and time using the definition of mass rate.
Definition MassRate.hpp:63
constexpr MassRate< NumericType > operator/(const NumericType number) const
Definition MassRate.hpp:131
constexpr MassRate(const Mass< NumericType > &mass, const Frequency< NumericType > &frequency)
Constructor. Constructs a mass rate from a given mass and frequency using the definition of mass rate...
Definition MassRate.hpp:68
constexpr void operator*=(const NumericType number) noexcept
Definition MassRate.hpp:159
constexpr MassRate< NumericType > & operator=(const MassRate< OtherNumericType > &other)
Copy assignment operator. Assigns this mass rate by copying another one.
Definition MassRate.hpp:95
constexpr MassRate< NumericType > & operator=(const MassRate< NumericType > &other)=default
Copy assignment operator. Assigns this mass rate by copying another one.
static constexpr MassRate< NumericType > Zero()
Statically creates a mass rate of zero.
Definition MassRate.hpp:104
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
constexpr Mass< NumericType > operator*(const NumericType number) const
Definition Mass.hpp:192
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
constexpr Time< NumericType > operator*(const NumericType number) const
Definition Time.hpp:278
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
Volume rate. Can represent a time rate of change of a volume or a volume flow rate....
VolumeRate
Volume rate units. Can represent a time rate of change of a volume or a volume flow rate.
MassDensity
Mass density units.
MassRate
Mass rate units. Can represent the time rate of change of a mass or a mass flow rate.
Definition MassRate.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