Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
ScalarForce.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_SCALAR_FORCE_HPP
26 #define PHQ_SCALAR_FORCE_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 
32 #include "Area.hpp"
33 #include "DimensionalScalar.hpp"
34 #include "Unit/Force.hpp"
35 
36 namespace PhQ {
37 
38 // Forward declaration for class PhQ::ScalarForce.
39 template <typename NumericType>
40 class Direction;
41 
42 // Forward declaration for class PhQ::ScalarForce.
43 template <typename NumericType>
44 class Force;
45 
46 // Forward declaration for class PhQ::ScalarForce.
47 template <typename NumericType>
48 class PlanarDirection;
49 
50 // Forward declaration for class PhQ::ScalarForce.
51 template <typename NumericType>
52 class PlanarForce;
53 
54 // Forward declaration for class PhQ::ScalarForce.
55 template <typename NumericType>
56 class ScalarTraction;
57 
58 // Forward declaration for class PhQ::ScalarForce.
59 template <typename NumericType>
60 class StaticPressure;
61 
62 /// \brief Scalar force component or magnitude of a force vector. For a three-dimensional Euclidean
63 /// force vector, see PhQ::Force. For a two-dimensional Euclidean force vector in the XY plane, see
64 /// PhQ::PlanarForce.
65 template <typename NumericType = double>
66 class ScalarForce : public DimensionalScalar<Unit::Force, NumericType> {
67 public:
68  /// \brief Default constructor. Constructs a scalar force with an uninitialized value.
69  ScalarForce() = default;
70 
71  /// \brief Constructor. Constructs a scalar force with a given value expressed in a given force
72  /// unit.
73  ScalarForce(const NumericType value, const Unit::Force unit)
74  : DimensionalScalar<Unit::Force, NumericType>(value, unit) {}
75 
76  /// \brief Constructor. Constructs a scalar force from a given scalar traction and area using the
77  /// definition of traction.
78  constexpr ScalarForce(
79  const ScalarTraction<NumericType>& scalar_traction, const Area<NumericType>& area);
80 
81  /// \brief Constructor. Constructs a scalar force from a given static pressure and area using the
82  /// definition of pressure.
83  constexpr ScalarForce(
84  const StaticPressure<NumericType>& static_pressure, const Area<NumericType>& area);
85 
86  /// \brief Destructor. Destroys this scalar force.
87  ~ScalarForce() noexcept = default;
88 
89  /// \brief Copy constructor. Constructs a scalar force by copying another one.
90  constexpr ScalarForce(const ScalarForce<NumericType>& other) = default;
91 
92  /// \brief Copy constructor. Constructs a scalar force by copying another one.
93  template <typename OtherNumericType>
94  explicit constexpr ScalarForce(const ScalarForce<OtherNumericType>& other)
95  : ScalarForce(static_cast<NumericType>(other.Value())) {}
96 
97  /// \brief Move constructor. Constructs a scalar force by moving another one.
98  constexpr ScalarForce(ScalarForce<NumericType>&& other) noexcept = default;
99 
100  /// \brief Copy assignment operator. Assigns this scalar force by copying another one.
101  constexpr ScalarForce<NumericType>& operator=(const ScalarForce<NumericType>& other) = default;
102 
103  /// \brief Copy assignment operator. Assigns this scalar force by copying another one.
104  template <typename OtherNumericType>
106  this->value = static_cast<NumericType>(other.Value());
107  return *this;
108  }
109 
110  /// \brief Move assignment operator. Assigns this scalar force by moving another one.
112  ScalarForce<NumericType>&& other) noexcept = default;
113 
114  /// \brief Statically creates a scalar force of zero.
115  [[nodiscard]] static constexpr ScalarForce<NumericType> Zero() {
116  return ScalarForce<NumericType>{static_cast<NumericType>(0)};
117  }
118 
119  /// \brief Statically creates a scalar force with a given value expressed in a given force unit.
120  template <Unit::Force Unit>
121  [[nodiscard]] static constexpr ScalarForce<NumericType> Create(const NumericType value) {
123  ConvertStatically<Unit::Force, Unit, Standard<Unit::Force>>(value)};
124  }
125 
126  constexpr ScalarForce<NumericType> operator+(const ScalarForce<NumericType>& scalar_force) const {
127  return ScalarForce<NumericType>{this->value + scalar_force.value};
128  }
129 
130  constexpr ScalarForce<NumericType> operator-(const ScalarForce<NumericType>& scalar_force) const {
131  return ScalarForce<NumericType>{this->value - scalar_force.value};
132  }
133 
134  constexpr ScalarForce<NumericType> operator*(const NumericType number) const {
135  return ScalarForce<NumericType>{this->value * number};
136  }
137 
139  const PlanarDirection<NumericType>& planar_direction) const;
140 
141  constexpr Force<NumericType> operator*(const Direction<NumericType>& direction) const;
142 
143  constexpr ScalarForce<NumericType> operator/(const NumericType number) const {
144  return ScalarForce<NumericType>{this->value / number};
145  }
146 
147  constexpr StaticPressure<NumericType> operator/(const Area<NumericType>& area) const;
148 
149  constexpr NumericType operator/(const ScalarForce<NumericType>& scalar_force) const noexcept {
150  return this->value / scalar_force.value;
151  }
152 
153  constexpr void operator+=(const ScalarForce<NumericType>& scalar_force) noexcept {
154  this->value += scalar_force.value;
155  }
156 
157  constexpr void operator-=(const ScalarForce<NumericType>& scalar_force) noexcept {
158  this->value -= scalar_force.value;
159  }
160 
161  constexpr void operator*=(const NumericType number) noexcept {
162  this->value *= number;
163  }
164 
165  constexpr void operator/=(const NumericType number) noexcept {
166  this->value /= number;
167  }
168 
169 private:
170  /// \brief Constructor. Constructs a scalar force with a given value expressed in the standard
171  /// force unit.
172  explicit constexpr ScalarForce(const NumericType value)
173  : DimensionalScalar<Unit::Force, NumericType>(value) {}
174 
175  template <typename OtherNumericType>
176  friend class PlanarForce;
177 
178  template <typename OtherNumericType>
179  friend class Force;
180 };
181 
182 template <typename NumericType>
183 inline constexpr bool operator==(
184  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
185  return left.Value() == right.Value();
186 }
187 
188 template <typename NumericType>
189 inline constexpr bool operator!=(
190  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
191  return left.Value() != right.Value();
192 }
193 
194 template <typename NumericType>
195 inline constexpr bool operator<(
196  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
197  return left.Value() < right.Value();
198 }
199 
200 template <typename NumericType>
201 inline constexpr bool operator>(
202  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
203  return left.Value() > right.Value();
204 }
205 
206 template <typename NumericType>
207 inline constexpr bool operator<=(
208  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
209  return left.Value() <= right.Value();
210 }
211 
212 template <typename NumericType>
213 inline constexpr bool operator>=(
214  const ScalarForce<NumericType>& left, const ScalarForce<NumericType>& right) noexcept {
215  return left.Value() >= right.Value();
216 }
217 
218 template <typename NumericType>
219 inline std::ostream& operator<<(
220  std::ostream& stream, const ScalarForce<NumericType>& scalar_force) {
221  stream << scalar_force.Print();
222  return stream;
223 }
224 
225 template <typename NumericType>
227  const NumericType number, const ScalarForce<NumericType>& scalar_force) {
228  return scalar_force * number;
229 }
230 
231 } // namespace PhQ
232 
233 namespace std {
234 
235 template <typename NumericType>
236 struct hash<PhQ::ScalarForce<NumericType>> {
237  inline size_t operator()(const PhQ::ScalarForce<NumericType>& scalar_force) const {
238  return hash<NumericType>()(scalar_force.Value());
239  }
240 };
241 
242 } // namespace std
243 
244 #endif // PHQ_SCALAR_FORCE_HPP
Surface area or cross-sectional area. Can also represent a scalar component of a vector area or the m...
Definition: Area.hpp:71
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::Force 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 direction vector. Contains three components in Cartesian coordinates: x,...
Definition: Direction.hpp:115
Three-dimensional Euclidean force vector. Contains three components in Cartesian coordinates: x,...
Definition: Force.hpp:52
two-dimensional Euclidean direction vector in the XY plane. Contains two components in Cartesian coor...
Two-dimensional Euclidean force vector in the XY plane. Contains two components in Cartesian coordina...
Definition: PlanarForce.hpp:50
Scalar force component or magnitude of a force vector. For a three-dimensional Euclidean force vector...
Definition: ScalarForce.hpp:66
constexpr ScalarForce< NumericType > operator-(const ScalarForce< NumericType > &scalar_force) const
constexpr ScalarForce< NumericType > & operator=(const ScalarForce< NumericType > &other)=default
Copy assignment operator. Assigns this scalar force by copying another one.
constexpr void operator-=(const ScalarForce< NumericType > &scalar_force) noexcept
constexpr ScalarForce(ScalarForce< NumericType > &&other) noexcept=default
Move constructor. Constructs a scalar force by moving another one.
constexpr ScalarForce< NumericType > operator/(const NumericType number) const
constexpr NumericType operator/(const ScalarForce< NumericType > &scalar_force) const noexcept
constexpr ScalarForce< NumericType > operator+(const ScalarForce< NumericType > &scalar_force) const
constexpr ScalarForce< NumericType > & operator=(const ScalarForce< OtherNumericType > &other)
Copy assignment operator. Assigns this scalar force by copying another one.
constexpr void operator+=(const ScalarForce< NumericType > &scalar_force) noexcept
constexpr ScalarForce< NumericType > operator*(const NumericType number) const
constexpr void operator*=(const NumericType number) noexcept
static constexpr ScalarForce< NumericType > Create(const NumericType value)
Statically creates a scalar force with a given value expressed in a given force unit.
ScalarForce(const NumericType value, const Unit::Force unit)
Constructor. Constructs a scalar force with a given value expressed in a given force unit.
Definition: ScalarForce.hpp:73
constexpr ScalarForce< NumericType > & operator=(ScalarForce< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this scalar force by moving another one.
~ScalarForce() noexcept=default
Destructor. Destroys this scalar force.
constexpr ScalarForce(const NumericType value)
Constructor. Constructs a scalar force with a given value expressed in the standard force unit.
ScalarForce()=default
Default constructor. Constructs a scalar force with an uninitialized value.
constexpr void operator/=(const NumericType number) noexcept
static constexpr ScalarForce< NumericType > Zero()
Statically creates a scalar force of zero.
Scalar traction component or magnitude of a traction vector. Traction is similar to pressure; however...
Static pressure. Pressure of a fluid at rest. Not to be confused with dynamic pressure,...
Force
Force units.
Definition: Force.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)