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