Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
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 
38 namespace PhQ {
39 
40 // Forward declaration for class PhQ::MassRate.
41 template <typename NumericType>
42 class MassDensity;
43 
44 // Forward declaration for class PhQ::MassRate.
45 template <typename NumericType>
46 class 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.
50 template <typename NumericType = double>
51 class MassRate : public DimensionalScalar<Unit::MassRate, NumericType> {
52 public:
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) {
111  return MassRate<NumericType>{
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 
135  constexpr Frequency<NumericType> operator/(const Mass<NumericType>& mass) const {
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 
167 private:
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 
174 template <typename NumericType>
175 inline constexpr bool operator==(
176  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
177  return left.Value() == right.Value();
178 }
179 
180 template <typename NumericType>
181 inline constexpr bool operator!=(
182  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
183  return left.Value() != right.Value();
184 }
185 
186 template <typename NumericType>
187 inline constexpr bool operator<(
188  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
189  return left.Value() < right.Value();
190 }
191 
192 template <typename NumericType>
193 inline constexpr bool operator>(
194  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
195  return left.Value() > right.Value();
196 }
197 
198 template <typename NumericType>
199 inline constexpr bool operator<=(
200  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
201  return left.Value() <= right.Value();
202 }
203 
204 template <typename NumericType>
205 inline constexpr bool operator>=(
206  const MassRate<NumericType>& left, const MassRate<NumericType>& right) noexcept {
207  return left.Value() >= right.Value();
208 }
209 
210 template <typename NumericType>
211 inline std::ostream& operator<<(std::ostream& stream, const MassRate<NumericType>& mass_rate) {
212  stream << mass_rate.Print();
213  return stream;
214 }
215 
216 template <typename NumericType>
218  const NumericType number, const MassRate<NumericType>& mass_rate) {
219  return mass_rate * number;
220 }
221 
222 template <typename NumericType>
223 inline constexpr Time<NumericType>::Time(
224  const Mass<NumericType>& mass, const MassRate<NumericType>& mass_rate)
225  : Time<NumericType>(mass.Value() / mass_rate.Value()) {}
226 
227 template <typename NumericType>
228 inline constexpr Frequency<NumericType>::Frequency(
229  const MassRate<NumericType>& mass_rate, const Mass<NumericType>& mass)
230  : Frequency<NumericType>(mass_rate.Value() / mass.Value()) {}
231 
232 template <typename NumericType>
233 inline constexpr Mass<NumericType>::Mass(
234  const MassRate<NumericType>& mass_rate, const Time<NumericType>& time)
235  : Mass<NumericType>(mass_rate.Value() * time.Value()) {}
236 
237 template <typename NumericType>
238 inline constexpr Mass<NumericType>::Mass(
239  const MassRate<NumericType>& mass_rate, const Frequency<NumericType>& frequency)
240  : Mass<NumericType>(mass_rate.Value() / frequency.Value()) {}
241 
242 template <typename NumericType>
243 inline constexpr Mass<NumericType> Time<NumericType>::operator*(
244  const MassRate<NumericType>& mass_rate) const {
245  return Mass<NumericType>{mass_rate, *this};
246 }
247 
248 template <typename NumericType>
249 inline constexpr MassRate<NumericType> Mass<NumericType>::operator*(
250  const Frequency<NumericType>& frequency) const {
251  return MassRate<NumericType>{*this, frequency};
252 }
253 
254 template <typename NumericType>
255 inline constexpr MassRate<NumericType> Frequency<NumericType>::operator*(
256  const Mass<NumericType>& mass) const {
257  return MassRate<NumericType>{mass, *this};
258 }
259 
260 template <typename NumericType>
261 inline constexpr MassRate<NumericType> Mass<NumericType>::operator/(
262  const Time<NumericType>& time) const {
263  return MassRate<NumericType>{*this, time};
264 }
265 
266 template <typename NumericType>
267 inline constexpr Time<NumericType> Mass<NumericType>::operator/(
268  const MassRate<NumericType>& mass_rate) const {
269  return Time<NumericType>{*this, mass_rate};
270 }
271 
272 } // namespace PhQ
273 
274 namespace std {
275 
276 template <typename NumericType>
277 struct 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...
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::MassRate 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
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
constexpr MassRate(const MassDensity< NumericType > &mass_density, const VolumeRate< NumericType > &volume_rate)
Constructor. Constructs a mass rate from a given mass density and volume rate using the definition of...
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 void operator-=(const MassRate< NumericType > &mass_rate) noexcept
Definition: MassRate.hpp:155
~MassRate() noexcept=default
Destructor. Destroys this mass rate.
constexpr MassRate< NumericType > operator/(const NumericType number) const
Definition: MassRate.hpp:131
constexpr MassRate< NumericType > operator*(const NumericType number) const
Definition: MassRate.hpp:123
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
constexpr MassRate< NumericType > operator-(const MassRate< NumericType > &mass_rate) const
Definition: MassRate.hpp:119
constexpr MassRate< NumericType > & operator=(MassRate< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this mass rate by moving another one.
constexpr VolumeRate< NumericType > operator/(const MassDensity< NumericType > &mass_density) const
MassRate()=default
Default constructor. Constructs a mass rate with an uninitialized value.
constexpr Mass< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition: MassRate.hpp:139
constexpr Frequency< NumericType > operator/(const Mass< NumericType > &mass) const
Definition: MassRate.hpp:135
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 void operator+=(const MassRate< NumericType > &mass_rate) noexcept
Definition: MassRate.hpp:151
constexpr MassRate< NumericType > operator+(const MassRate< NumericType > &mass_rate) const
Definition: MassRate.hpp:115
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
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
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(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
static constexpr MassRate< NumericType > Zero()
Statically creates a mass rate of zero.
Definition: MassRate.hpp:104
constexpr Mass< NumericType > operator*(const Time< NumericType > &time) const
Definition: MassRate.hpp:127
constexpr void operator*=(const NumericType number) noexcept
Definition: MassRate.hpp:159
constexpr MassRate< NumericType > & operator=(const MassRate< NumericType > &other)=default
Copy assignment operator. Assigns this mass rate by copying another one.
constexpr MassDensity< NumericType > operator/(const VolumeRate< NumericType > &volume_rate) const
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition: Mass.hpp:100
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition: Time.hpp:172
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....
Definition: VolumeRate.hpp:51
VolumeRate
Volume rate units. Can represent a time rate of change of a volume or a volume flow rate.
Definition: VolumeRate.hpp:53
MassDensity
Mass density units.
Definition: MassDensity.hpp:54
Mass
Mass units.
Definition: Mass.hpp:53
Frequency
Frequency units.
Definition: Frequency.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
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 MassRate< NumericType > operator*(const NumericType number, const MassRate< NumericType > &mass_rate)
Definition: MassRate.hpp:217
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 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)