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
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"
39
40namespace PhQ {
41
42// Forward declaration for class PhQ::SpecificEnergy.
43template <typename NumericType>
44class SpecificPower;
45
46/// \brief Mass-specific energy. Energy per unit mass; see PhQ::Energy and PhQ::Mass.
47template <typename NumericType = double>
48class SpecificEnergy : public DimensionalScalar<Unit::SpecificEnergy, NumericType> {
49public:
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
133 constexpr SpecificPower<NumericType> operator*(const Frequency<NumericType>& frequency) const;
134
135 constexpr SpecificEnergy<NumericType> operator/(const NumericType number) const {
136 return SpecificEnergy<NumericType>{this->value / number};
137 }
138
139 constexpr SpecificPower<NumericType> operator/(const Time<NumericType>& time) const;
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
164private:
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
171template <typename NumericType>
172inline constexpr bool operator==(
173 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
174 return left.Value() == right.Value();
175}
176
177template <typename NumericType>
178inline constexpr bool operator!=(
179 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
180 return left.Value() != right.Value();
181}
182
183template <typename NumericType>
184inline constexpr bool operator<(
185 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
186 return left.Value() < right.Value();
187}
188
189template <typename NumericType>
190inline constexpr bool operator>(
191 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
192 return left.Value() > right.Value();
193}
194
195template <typename NumericType>
196inline constexpr bool operator<=(
197 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
198 return left.Value() <= right.Value();
199}
200
201template <typename NumericType>
202inline constexpr bool operator>=(
203 const SpecificEnergy<NumericType>& left, const SpecificEnergy<NumericType>& right) noexcept {
204 return left.Value() >= right.Value();
205}
206
207template <typename NumericType>
208inline std::ostream& operator<<(
209 std::ostream& stream, const SpecificEnergy<NumericType>& specific_energy) {
210 stream << specific_energy.Print();
211 return stream;
212}
213
214template <typename NumericType>
216 const NumericType number, const SpecificEnergy<NumericType>& specific_energy) {
217 return specific_energy * number;
218}
219
220template <typename NumericType>
221inline constexpr Mass<NumericType>::Mass(
222 const Energy<NumericType>& energy, const SpecificEnergy<NumericType>& specific_energy)
223 : Mass<NumericType>(energy.Value() / specific_energy.Value()) {}
224
225template <typename NumericType>
227 const SpecificEnergy<NumericType>& specific_energy, const Mass<NumericType>& mass)
228 : Energy<NumericType>(specific_energy.Value() * mass.Value()) {}
229
230template <typename NumericType>
232 const SpecificEnergy<NumericType>& specific_energy) const {
233 return Energy<NumericType>{specific_energy, *this};
234}
235
236template <typename NumericType>
238 const SpecificEnergy<NumericType>& specific_energy) const {
239 return Mass<NumericType>{*this, specific_energy};
240}
241
242template <typename NumericType>
244 const Mass<NumericType>& mass) const {
245 return SpecificEnergy<NumericType>{*this, mass};
246}
247
248} // namespace PhQ
249
250namespace std {
251
252template <typename NumericType>
253struct 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...
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...
Energy physical quantity. Can represent any kind of energy, such as kinetic energy,...
Definition Energy.hpp:73
constexpr Energy< NumericType > operator/(const NumericType number) const
Definition Energy.hpp:155
Energy()=default
Default constructor. Constructs an energy quantity with an uninitialized value.
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.
constexpr Mass< NumericType > operator*(const NumericType number) const
Definition Mass.hpp:192
Mass-specific energy. Energy per unit mass; see PhQ::Energy and PhQ::Mass.
constexpr SpecificEnergy< NumericType > & operator=(const SpecificEnergy< OtherNumericType > &other)
Copy assignment operator. Assigns this specific energy quantity by copying another one.
SpecificEnergy(const NumericType value, const Unit::SpecificEnergy unit)
Constructor. Constructs a specific energy quantity with a given value expressed in a given specific e...
static constexpr SpecificEnergy< NumericType > Zero()
Statically creates a specific energy quantity of zero.
constexpr SpecificEnergy< NumericType > operator*(const NumericType number) const
static constexpr SpecificEnergy< NumericType > Create(const NumericType value)
Statically creates a specific energy quantity with a given value expressed in a given specific energy...
constexpr void operator-=(const SpecificEnergy< NumericType > &specific_energy) noexcept
constexpr SpecificEnergy< NumericType > operator/(const NumericType number) const
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
~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 > &specific_energy) const
SpecificEnergy()=default
Default constructor. Constructs a specific energy quantity with an uninitialized value.
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 SpecificEnergy< NumericType > operator+(const SpecificEnergy< NumericType > &specific_energy) const
constexpr SpecificEnergy< NumericType > & operator=(const SpecificEnergy< NumericType > &other)=default
Copy assignment operator. Assigns this specific energy quantity by copying another one.
constexpr SpecificEnergy< NumericType > & operator=(SpecificEnergy< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this specific energy quantity by moving another one.
constexpr Energy< NumericType > operator*(const Mass< NumericType > &mass) 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
SpecificEnergy
Mass-specific energy units.
SpecificPower
Mass-specific power units.
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