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