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