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