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