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
Time.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_DURATION_HPP
26#define PHQ_DURATION_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "Unit/Time.hpp"
34
35namespace PhQ {
36
37// Forward declaration for class PhQ::Time.
38template <typename NumericType>
39class Acceleration;
40
41// Forward declaration for class PhQ::Time.
42template <typename NumericType>
43class Angle;
44
45// Forward declaration for class PhQ::Time.
46template <typename NumericType>
47class AngularSpeed;
48
49// Forward declaration for class PhQ::Time.
50template <typename NumericType>
51class Displacement;
52
53// Forward declaration for class PhQ::Time.
54template <typename NumericType>
55class DisplacementGradient;
56
57// Forward declaration for class PhQ::Time.
58template <typename NumericType>
59class ElectricCharge;
60
61// Forward declaration for class PhQ::Time.
62template <typename NumericType>
63class ElectricCurrent;
64
65// Forward declaration for class PhQ::Time.
66template <typename NumericType>
67class Energy;
68
69// Forward declaration for class PhQ::Time.
70template <typename NumericType>
71class Frequency;
72
73// Forward declaration for class PhQ::Time.
74template <typename NumericType>
75class Length;
76
77// Forward declaration for class PhQ::Time.
78template <typename NumericType>
79class Mass;
80
81// Forward declaration for class PhQ::Time.
82template <typename NumericType>
83class MassRate;
84
85// Forward declaration for class PhQ::Time.
86template <typename NumericType>
87class Memory;
88
89// Forward declaration for class PhQ::Time.
90template <typename NumericType>
91class MemoryRate;
92
93// Forward declaration for class PhQ::Time.
94template <typename NumericType>
95class PlanarAcceleration;
96
97// Forward declaration for class PhQ::Time.
98template <typename NumericType>
99class PlanarDisplacement;
100
101// Forward declaration for class PhQ::Time.
102template <typename NumericType>
103class PlanarVelocity;
104
105// Forward declaration for class PhQ::Time.
106template <typename NumericType>
107class Power;
108
109// Forward declaration for class PhQ::Time.
110template <typename NumericType>
111class ScalarAcceleration;
112
113// Forward declaration for class PhQ::Time.
114template <typename NumericType>
115class ScalarAngularAcceleration;
116
117// Forward declaration for class PhQ::Time.
118template <typename NumericType>
119class ScalarDisplacementGradient;
120
121// Forward declaration for class PhQ::Time.
122template <typename NumericType>
123class ScalarStrain;
124
125// Forward declaration for class PhQ::Time.
126template <typename NumericType>
127class ScalarStrainRate;
128
129// Forward declaration for class PhQ::Time.
130template <typename NumericType>
131class ScalarVelocityGradient;
132
133// Forward declaration for class PhQ::Time.
134template <typename NumericType>
135class SpecificEnergy;
136
137// Forward declaration for class PhQ::Time.
138template <typename NumericType>
139class SpecificPower;
140
141// Forward declaration for class PhQ::Time.
142template <typename NumericType>
143class Speed;
144
145// Forward declaration for class PhQ::Time.
146template <typename NumericType>
147class Strain;
148
149// Forward declaration for class PhQ::Time.
150template <typename NumericType>
151class StrainRate;
152
153// Forward declaration for class PhQ::Time.
154template <typename NumericType>
155class Velocity;
156
157// Forward declaration for class PhQ::Time.
158template <typename NumericType>
159class VelocityGradient;
160
161// Forward declaration for class PhQ::Time.
162template <typename NumericType>
163class Volume;
164
165// Forward declaration for class PhQ::Time.
166template <typename NumericType>
167class VolumeRate;
168
169/// \brief Time. Can represent either a point in time, a time duration, or a period. For the inverse
170/// of time, see PhQ::Frequency.
171template <typename NumericType = double>
172class Time : public DimensionalScalar<Unit::Time, NumericType> {
173public:
174 /// \brief Default constructor. Constructs a time quantity with an uninitialized value.
175 Time() = default;
176
177 /// \brief Constructor. Constructs a time quantity with a given value expressed in a given time
178 /// unit.
179 Time(const NumericType value, const Unit::Time unit)
180 : DimensionalScalar<Unit::Time, NumericType>(value, unit) {}
181
182 /// \brief Constructor. Constructs a time quantity from a given frequency using the definition of
183 /// frequency.
184 explicit constexpr Time(const PhQ::Frequency<NumericType>& frequency);
185
186 /// \brief Constructor. Constructs a time quantity from a given speed and scalar acceleration
187 /// using the definition of acceleration.
188 constexpr Time(
189 const Speed<NumericType>& speed, const ScalarAcceleration<NumericType>& scalar_acceleration);
190
191 /// \brief Constructor. Constructs a time quantity from a given angular speed and scalar angular
192 /// acceleration using the definition of angular acceleration.
193 constexpr Time(const AngularSpeed<NumericType>& angular_speed,
194 const ScalarAngularAcceleration<NumericType>& scalar_angular_acceleration);
195
196 /// \brief Constructor. Constructs a time quantity from a given angle and angular speed using the
197 /// definition of angular speed.
198 constexpr Time(const Angle<NumericType>& angle, const AngularSpeed<NumericType>& angular_speed);
199
200 /// \brief Constructor. Constructs a time quantity from a given electric charge and electric
201 /// current using the definition of electric current.
202 constexpr Time(const ElectricCharge<NumericType>& electric_charge,
203 const ElectricCurrent<NumericType>& electric_current);
204
205 /// \brief Constructor. Constructs a time quantity from a given mass and mass rate using the
206 /// definition of mass rate.
207 constexpr Time(const Mass<NumericType>& mass, const MassRate<NumericType>& mass_rate);
208
209 /// \brief Constructor. Constructs a time quantity from a given memory and memory rate using the
210 /// definition of memory rate.
211 constexpr Time(const Memory<NumericType>& memory, const MemoryRate<NumericType>& memory_rate);
212
213 /// \brief Constructor. Constructs a time quantity from a given energy and power using the
214 /// definition of power.
215 constexpr Time(const Energy<NumericType>& energy, const Power<NumericType>& power);
216
217 /// \brief Constructor. Constructs a time quantity from a given specific energy and specific power
218 /// using the definition of specific power.
219 constexpr Time(const SpecificEnergy<NumericType>& specific_energy,
220 const SpecificPower<NumericType>& specific_power);
221
222 /// \brief Constructor. Constructs a time quantity from a given length and speed using the
223 /// definition of speed.
224 constexpr Time(const Length<NumericType>& length, const Speed<NumericType>& speed);
225
226 /// \brief Constructor. Constructs a time quantity from a given volume and volume rate using the
227 /// definition of volume rate.
228 constexpr Time(const Volume<NumericType>& volume, const VolumeRate<NumericType>& volume_rate);
229
230 /// \brief Destructor. Destroys this time quantity.
231 ~Time() noexcept = default;
232
233 /// \brief Copy constructor. Constructs a time quantity by copying another one.
234 constexpr Time(const Time<NumericType>& other) = default;
235
236 /// \brief Copy constructor. Constructs a time quantity by copying another one.
237 template <typename OtherNumericType>
238 explicit constexpr Time(const Time<OtherNumericType>& other)
239 : Time(static_cast<NumericType>(other.Value())) {}
240
241 /// \brief Move constructor. Constructs a time quantity by moving another one.
242 constexpr Time(Time<NumericType>&& other) noexcept = default;
243
244 /// \brief Copy assignment operator. Assigns this time quantity by copying another one.
245 constexpr Time<NumericType>& operator=(const Time<NumericType>& other) = default;
246
247 /// \brief Copy assignment operator. Assigns this time quantity by copying another one.
248 template <typename OtherNumericType>
250 this->value = static_cast<NumericType>(other.Value());
251 return *this;
252 }
253
254 /// \brief Move assignment operator. Assigns this time quantity by moving another one.
255 constexpr Time<NumericType>& operator=(Time<NumericType>&& other) noexcept = default;
256
257 /// \brief Statically creates a time quantity of zero.
258 [[nodiscard]] static constexpr Time<NumericType> Zero() {
259 return Time<NumericType>{static_cast<NumericType>(0)};
260 }
261
262 /// \brief Statically creates a time quantity with a given value expressed in a given time unit.
263 template <Unit::Time Unit>
264 [[nodiscard]] static constexpr Time<NumericType> Create(const NumericType value) {
265 return Time<NumericType>{ConvertStatically<Unit::Time, Unit, Standard<Unit::Time>>(value)};
266 }
267
268 [[nodiscard]] constexpr PhQ::Frequency<NumericType> Frequency() const;
269
270 constexpr Time<NumericType> operator+(const Time<NumericType>& time) const {
271 return Time<NumericType>{this->value + time.value};
272 }
273
274 constexpr Time<NumericType> operator-(const Time<NumericType>& time) const {
275 return Time<NumericType>{this->value - time.value};
276 }
277
278 constexpr Time<NumericType> operator*(const NumericType number) const {
279 return Time<NumericType>{this->value * number};
280 }
281
282 constexpr NumericType operator*(const PhQ::Frequency<NumericType>& frequency) const noexcept;
283
285 const PlanarAcceleration<NumericType>& planar_acceleration) const;
286
287 constexpr Angle<NumericType> operator*(const AngularSpeed<NumericType>& angular_speed) const;
288
290 const ScalarAngularAcceleration<NumericType>& scalar_angular_acceleration) const;
291
293 const ElectricCurrent<NumericType>& electric_current) const;
294
295 constexpr Velocity<NumericType> operator*(const Acceleration<NumericType>& acceleration) const;
296
298 const ScalarAcceleration<NumericType>& scalar_acceleration) const;
299
300 constexpr Mass<NumericType> operator*(const MassRate<NumericType>& mass_rate) const;
301
302 constexpr Energy<NumericType> operator*(const Power<NumericType>& power) const;
303
305 const SpecificPower<NumericType>& specific_power) const;
306
307 constexpr Strain<NumericType> operator*(const StrainRate<NumericType>& strain_rate) const;
308
310 const ScalarStrainRate<NumericType>& scalar_strain_rate) const;
311
313 const PlanarVelocity<NumericType>& planar_velocity) const;
314
315 constexpr Displacement<NumericType> operator*(const Velocity<NumericType>& velocity) const;
316
318 const VelocityGradient<NumericType>& velocity_gradient) const;
319
321 const ScalarVelocityGradient<NumericType>& scalar_velocity_gradient) const;
322
323 constexpr Volume<NumericType> operator*(const VolumeRate<NumericType>& volume_rate) const;
324
325 constexpr Time<NumericType> operator/(const NumericType number) const {
326 return Time<NumericType>{this->value / number};
327 }
328
329 constexpr NumericType operator/(const Time<NumericType>& time) const noexcept {
330 return this->value / time.value;
331 }
332
333 constexpr void operator+=(const Time<NumericType>& time) noexcept {
334 this->value += time.value;
335 }
336
337 constexpr void operator-=(const Time<NumericType>& time) noexcept {
338 this->value -= time.value;
339 }
340
341 constexpr void operator*=(const NumericType number) noexcept {
342 this->value *= number;
343 }
344
345 constexpr void operator/=(const NumericType number) noexcept {
346 this->value /= number;
347 }
348
349private:
350 /// \brief Constructor. Constructs a time quantity with a given value expressed in the standard
351 /// time unit.
352 explicit constexpr Time(const NumericType value) : DimensionalScalar<Unit::Time>(value) {}
353};
354
355template <typename NumericType>
356inline constexpr bool operator==(
357 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
358 return left.Value() == right.Value();
359}
360
361template <typename NumericType>
362inline constexpr bool operator!=(
363 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
364 return left.Value() != right.Value();
365}
366
367template <typename NumericType>
368inline constexpr bool operator<(
369 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
370 return left.Value() < right.Value();
371}
372
373template <typename NumericType>
374inline constexpr bool operator>(
375 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
376 return left.Value() > right.Value();
377}
378
379template <typename NumericType>
380inline constexpr bool operator<=(
381 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
382 return left.Value() <= right.Value();
383}
384
385template <typename NumericType>
386inline constexpr bool operator>=(
387 const Time<NumericType>& left, const Time<NumericType>& right) noexcept {
388 return left.Value() >= right.Value();
389}
390
391template <typename NumericType>
392inline std::ostream& operator<<(std::ostream& stream, const Time<NumericType>& time) {
393 stream << time.Print();
394 return stream;
395}
396
397template <typename NumericType>
399 const NumericType number, const Time<NumericType>& time) {
400 return time * number;
401}
402
403} // namespace PhQ
404
405namespace std {
406
407template <typename NumericType>
408struct hash<PhQ::Time<NumericType>> {
409 inline size_t operator()(const PhQ::Time<NumericType>& time) const {
410 return hash<NumericType>()(time.Value());
411 }
412};
413
414} // namespace std
415
416#endif // PHQ_DURATION_HPP
Three-dimensional Euclidean acceleration vector. Contains three components in Cartesian coordinates: ...
Plane angle between two lines or dihedral angle between two planes.
Definition Angle.hpp:130
Angular speed, also known as angular rate or angular frequency. Represents the time rate of change of...
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...
Three-dimensional Euclidean displacement gradient dyadic tensor. Gradient of the displacement vector....
Three-dimensional Euclidean displacement vector. Contains three components in Cartesian coordinates: ...
Electric charge.
Electric current, also known as amperage. Represents a flow of electric charge or a time rate of chan...
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
Length, distance, or physical size. Can also represent a scalar component or magnitude of a position ...
Definition Length.hpp:111
Mass rate. Can represent the time rate of change of a mass or a mass flow rate; see PhQ::Mass,...
Definition MassRate.hpp:51
Mass. For the time rate of change of mass, see PhQ::MassRate; see also PhQ::Time and PhQ::Frequency.
Definition Mass.hpp:100
Computer memory rate. Can represent the time rate of change of memory or a memory transfer speed; see...
Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see also PhQ::T...
Definition Memory.hpp:52
Two-dimensional Euclidean acceleration vector in the XY plane. Contains two components in Cartesian c...
Two-dimensional Euclidean displacement vector in the XY plane. Contains two components in Cartesian c...
Two-dimensional Euclidean velocity vector in the XY plane. Contains two components in Cartesian coord...
Power. Time rate of change of energy or energy transfer rate; see PhQ::Energy, PhQ::Time,...
Definition Power.hpp:51
Scalar acceleration component or magnitude of an acceleration vector. For a three-dimensional Euclide...
Scalar angular acceleration. Represents the time rate of change of an angular speed; see also PhQ::An...
Scalar component or resultant of a three-dimensional Euclidean displacement gradient dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean strain rate symmetric dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean strain symmetric dyadic tensor....
Scalar component or resultant of a three-dimensional Euclidean velocity gradient dyadic tensor....
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.
Scalar velocity component or magnitude of a velocity vector. For a three-dimensional Euclidean veloci...
Definition Speed.hpp:100
Three-dimensional Euclidean strain rate symmetric dyadic tensor. Time rate of change of strain....
Three-dimensional Euclidean strain symmetric dyadic tensor. Contains six components in Cartesian coor...
Definition Strain.hpp:68
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
constexpr void operator/=(const NumericType number) noexcept
Definition Time.hpp:345
constexpr Time< NumericType > & operator=(const Time< OtherNumericType > &other)
Copy assignment operator. Assigns this time quantity by copying another one.
Definition Time.hpp:249
constexpr Time< NumericType > operator+(const Time< NumericType > &time) const
Definition Time.hpp:270
constexpr Time< NumericType > operator-(const Time< NumericType > &time) const
Definition Time.hpp:274
constexpr NumericType operator/(const Time< NumericType > &time) const noexcept
Definition Time.hpp:329
static constexpr Time< NumericType > Zero()
Statically creates a time quantity of zero.
Definition Time.hpp:258
constexpr void operator-=(const Time< NumericType > &time) noexcept
Definition Time.hpp:337
Time(const NumericType value, const Unit::Time unit)
Constructor. Constructs a time quantity with a given value expressed in a given time unit.
Definition Time.hpp:179
static constexpr Time< NumericType > Create(const NumericType value)
Statically creates a time quantity with a given value expressed in a given time unit.
Definition Time.hpp:264
constexpr Time< NumericType > operator*(const NumericType number) const
Definition Time.hpp:278
~Time() noexcept=default
Destructor. Destroys this time quantity.
constexpr Time< NumericType > & operator=(const Time< NumericType > &other)=default
Copy assignment operator. Assigns this time quantity by copying another one.
constexpr void operator+=(const Time< NumericType > &time) noexcept
Definition Time.hpp:333
constexpr Time(const NumericType value)
Constructor. Constructs a time quantity with a given value expressed in the standard time unit.
Definition Time.hpp:352
constexpr PhQ::Frequency< NumericType > Frequency() const
constexpr Time< NumericType > operator/(const NumericType number) const
Definition Time.hpp:325
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
constexpr Time< NumericType > & operator=(Time< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this time quantity by moving another one.
constexpr void operator*=(const NumericType number) noexcept
Definition Time.hpp:341
constexpr Time(Time< NumericType > &&other) noexcept=default
Move constructor. Constructs a time quantity by moving another one.
Three-dimensional Euclidean velocity gradient dyadic tensor. Gradient of the velocity vector....
Three-dimensional Euclidean velocity vector. Contains three components in Cartesian coordinates: x,...
Definition Velocity.hpp:55
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
ElectricCurrent
Electric current units.
VolumeRate
Volume rate units. Can represent a time rate of change of a volume or a volume flow rate.
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.
MassRate
Mass rate units. Can represent the time rate of change of a mass or a mass flow rate.
Definition MassRate.hpp:53
Angle
Angle units.
Definition Angle.hpp:53
Volume
Volume units.
Definition Volume.hpp:53
Memory
Computer memory units.
Definition Memory.hpp:53
MemoryRate
Computer memory rate unit. Can represent the time rate of change of memory or a memory transfer speed...
SpecificPower
Mass-specific power units.
Acceleration
Acceleration units.
ElectricCharge
Electric charge units.
Time
Time units.
Definition Time.hpp:53
Speed
Speed units.
Definition Speed.hpp:53
AngularSpeed
Angular speed 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