Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
DimensionalDyad.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_DIMENSIONAL_DYAD_HPP
26 #define PHQ_DIMENSIONAL_DYAD_HPP
27 
28 #include <cstddef>
29 #include <functional>
30 #include <ostream>
31 #include <string>
32 
33 #include "Base.hpp"
34 #include "Dimensions.hpp"
35 #include "Dyad.hpp"
36 #include "Unit.hpp"
37 
38 namespace PhQ {
39 
40 /// \brief Abstract base class that represents any dimensional dyadic tensor physical quantity. Such
41 /// a physical quantity is composed of a value and a unit of measure where the value is a
42 /// three-dimensional dyadic tensor. The tensor may be non-symmetric.
43 /// \tparam UnitType Unit of measure enumeration type.
44 /// \tparam NumericType Floating-point numeric type: float, double, or long double. Defaults to
45 /// double if unspecified.
46 template <typename UnitType, typename NumericType = double>
48 public:
49  /// \brief Physical dimension set of this physical quantity.
50  [[nodiscard]] static constexpr const PhQ::Dimensions& Dimensions() {
51  return PhQ::RelatedDimensions<UnitType>;
52  }
53 
54  /// \brief Standard unit of measure for this physical quantity. This physical quantity's value is
55  /// stored internally in this unit of measure.
56  [[nodiscard]] static constexpr UnitType Unit() {
57  return PhQ::Standard<UnitType>;
58  }
59 
60  /// \brief Value of this physical quantity expressed in its standard unit of measure.
61  [[nodiscard]] constexpr const PhQ::Dyad<NumericType>& Value() const noexcept {
62  return value;
63  }
64 
65  /// \brief Value of this physical quantity expressed in a given unit of measure.
66  [[nodiscard]] PhQ::Dyad<NumericType> Value(const UnitType unit) const {
67  return PhQ::Convert(value, PhQ::Standard<UnitType>, unit);
68  }
69 
70  /// \brief Value of this physical quantity expressed in a given unit of measure. This method can
71  /// be evaluated statically at compile-time.
72  template <UnitType NewUnit>
73  [[nodiscard]] constexpr PhQ::Dyad<NumericType> StaticValue() const {
74  return PhQ::ConvertStatically<UnitType, PhQ::Standard<UnitType>, NewUnit>(value);
75  }
76 
77  /// \brief Returns the value of this physical quantity expressed in its standard unit of measure
78  /// as a mutable value.
79  [[nodiscard]] constexpr PhQ::Dyad<NumericType>& MutableValue() noexcept {
80  return value;
81  }
82 
83  /// \brief Sets the value of this physical quantity expressed in its standard unit of measure to
84  /// the given value.
85  constexpr void SetValue(const PhQ::Dyad<NumericType>& value) noexcept {
86  this->value = value;
87  }
88 
89  /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
90  /// in its standard unit of measure.
91  [[nodiscard]] std::string Print() const {
92  return value.Print().append(" ").append(PhQ::Abbreviation(PhQ::Standard<UnitType>));
93  }
94 
95  /// \brief Prints this physical quantity as a string. This physical quantity's value is expressed
96  /// in the given unit of measure.
97  [[nodiscard]] std::string Print(const UnitType unit) const {
98  return Value(unit).Print().append(" ").append(PhQ::Abbreviation(unit));
99  }
100 
101  /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
102  /// expressed in its standard unit of measure.
103  [[nodiscard]] std::string JSON() const {
104  return std::string{"{\"value\":"}
105  .append(value.JSON())
106  .append(R"(,"unit":")")
107  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
108  .append("\"}");
109  }
110 
111  /// \brief Serializes this physical quantity as a JSON message. This physical quantity's value is
112  /// expressed in the given unit of measure.
113  [[nodiscard]] std::string JSON(const UnitType unit) const {
114  return std::string{"{\"value\":"}
115  .append(Value(unit).JSON())
116  .append(R"(,"unit":")")
117  .append(PhQ::Abbreviation(unit))
118  .append("\"}");
119  }
120 
121  /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
122  /// expressed in its standard unit of measure.
123  [[nodiscard]] std::string XML() const {
124  return std::string{"<value>"}
125  .append(value.XML())
126  .append("</value><unit>")
127  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
128  .append("</unit>");
129  }
130 
131  /// \brief Serializes this physical quantity as an XML message. This physical quantity's value is
132  /// expressed in the given unit of measure.
133  [[nodiscard]] std::string XML(const UnitType unit) const {
134  return std::string{"<value>"}
135  .append(Value(unit).XML())
136  .append("</value><unit>")
137  .append(PhQ::Abbreviation(unit))
138  .append("</unit>");
139  }
140 
141  /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
142  /// expressed in its standard unit of measure.
143  [[nodiscard]] std::string YAML() const {
144  return std::string{"{value:"}
145  .append(value.YAML())
146  .append(",unit:\"")
147  .append(PhQ::Abbreviation(PhQ::Standard<UnitType>))
148  .append("\"}");
149  }
150 
151  /// \brief Serializes this physical quantity as a YAML message. This physical quantity's value is
152  /// expressed in the given unit of measure.
153  [[nodiscard]] std::string YAML(const UnitType unit) const {
154  return std::string{"{value:"}
155  .append(Value(unit).YAML())
156  .append(",unit:\"")
157  .append(PhQ::Abbreviation(unit))
158  .append("\"}");
159  }
160 
161 protected:
162  /// \brief Default constructor. Constructs a dimensional dyadic tensor physical quantity with an
163  /// uninitialized value.
164  DimensionalDyad() = default;
165 
166  /// \brief Constructor. Constructs a dimensional dyadic tensor physical quantity with a given
167  /// value expressed in its standard unit of measure.
168  explicit constexpr DimensionalDyad(const PhQ::Dyad<NumericType>& value) : value(value) {}
169 
170  /// \brief Constructor. Constructs a dimensional dimensional dyadic tensor physical quantity with
171  /// a given value expressed in a given unit of measure.
172  DimensionalDyad(const PhQ::Dyad<NumericType>& value, const UnitType unit) : value(value) {
173  PhQ::ConvertInPlace(this->value, unit, PhQ::Standard<UnitType>);
174  }
175 
176  /// \brief Destructor. Destroys this dimensional dyadic tensor physical quantity.
177  ~DimensionalDyad() noexcept = default;
178 
179  /// \brief Copy constructor. Constructs a dimensional dyadic tensor physical quantity by copying
180  /// another one.
181  constexpr DimensionalDyad(const DimensionalDyad<UnitType, NumericType>& other) = default;
182 
183  /// \brief Copy constructor. Constructs a dimensional dyadic tensor physical quantity by copying
184  /// another one.
185  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
186  /// automatically.
187  template <typename OtherNumericType>
188  explicit constexpr DimensionalDyad(const DimensionalDyad<UnitType, OtherNumericType>& other)
189  : value(static_cast<PhQ::Dyad<NumericType>>(other.Value())) {}
190 
191  /// \brief Move constructor. Constructs a dimensional dyadic tensor physical quantity by moving
192  /// another one.
193  constexpr DimensionalDyad(DimensionalDyad<UnitType, NumericType>&& other) noexcept = default;
194 
195  /// \brief Copy assignment operator. Assigns this dimensional dyadic tensor physical quantity by
196  /// copying another one.
198  const DimensionalDyad<UnitType, NumericType>& other) = default;
199 
200  /// \brief Copy assignment operator. Assigns this dimensional dyadic tensor physical quantity by
201  /// copying another one.
202  /// \tparam OtherNumericType Floating-point numeric type of the other physical quantity. Deduced
203  /// automatically.
204  template <typename OtherNumericType>
207  value = static_cast<PhQ::Dyad<NumericType>>(other.Value());
208  return *this;
209  }
210 
211  /// \brief Move assignment operator. Assigns this dimensional dyadic tensor physical quantity by
212  /// moving another one.
214  DimensionalDyad<UnitType, NumericType>&& other) noexcept = default;
215 
216  /// \brief Value of this physical quantity expressed in its standard unit of measure.
218 };
219 
220 } // namespace PhQ
221 
222 #endif // PHQ_DIMENSIONAL_DYAD_HPP
Abstract base class that represents any dimensional dyadic tensor physical quantity....
static constexpr UnitType Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
static constexpr const PhQ::Dimensions & Dimensions()
Physical dimension set of this physical quantity.
constexpr PhQ::Dyad< NumericType > StaticValue() const
Value of this physical quantity expressed in a given unit of measure. This method can be evaluated st...
std::string XML(const UnitType unit) const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in t...
constexpr PhQ::Dyad< NumericType > & MutableValue() noexcept
Returns the value of this physical quantity expressed in its standard unit of measure as a mutable va...
PhQ::Dyad< NumericType > value
Value of this physical quantity expressed in its standard unit of measure.
constexpr void SetValue(const PhQ::Dyad< NumericType > &value) noexcept
Sets the value of this physical quantity expressed in its standard unit of measure to the given value...
constexpr DimensionalDyad< UnitType, NumericType > & operator=(const DimensionalDyad< UnitType, NumericType > &other)=default
Copy assignment operator. Assigns this dimensional dyadic tensor physical quantity by copying another...
DimensionalDyad(const PhQ::Dyad< NumericType > &value, const UnitType unit)
Constructor. Constructs a dimensional dimensional dyadic tensor physical quantity with a given value ...
PhQ::Dyad< NumericType > Value(const UnitType unit) const
Value of this physical quantity expressed in a given unit of measure.
~DimensionalDyad() noexcept=default
Destructor. Destroys this dimensional dyadic tensor physical quantity.
constexpr DimensionalDyad(const PhQ::Dyad< NumericType > &value)
Constructor. Constructs a dimensional dyadic tensor physical quantity with a given value expressed in...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
constexpr DimensionalDyad< UnitType, NumericType > & operator=(DimensionalDyad< UnitType, NumericType > &&other) noexcept=default
Move assignment operator. Assigns this dimensional dyadic tensor physical quantity by moving another ...
std::string YAML() const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in i...
constexpr DimensionalDyad(DimensionalDyad< UnitType, NumericType > &&other) noexcept=default
Move constructor. Constructs a dimensional dyadic tensor physical quantity by moving another one.
std::string Print(const UnitType unit) const
Prints this physical quantity as a string. This physical quantity's value is expressed in the given u...
std::string JSON(const UnitType unit) const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in t...
std::string JSON() const
Serializes this physical quantity as a JSON message. This physical quantity's value is expressed in i...
DimensionalDyad()=default
Default constructor. Constructs a dimensional dyadic tensor physical quantity with an uninitialized v...
std::string YAML(const UnitType unit) const
Serializes this physical quantity as a YAML message. This physical quantity's value is expressed in t...
std::string XML() const
Serializes this physical quantity as an XML message. This physical quantity's value is expressed in i...
constexpr const PhQ::Dyad< NumericType > & Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
constexpr DimensionalDyad< UnitType, NumericType > & operator=(const DimensionalDyad< UnitType, OtherNumericType > &other)
Copy assignment operator. Assigns this dimensional dyadic tensor physical quantity by copying another...
Physical dimension set of a unit of measure or physical quantity. Composed of the seven independent b...
Definition: Dimensions.hpp:49
Three-dimensional Euclidean dyadic tensor. Contains nine components in Cartesian coordinates: xx,...
Definition: Dyad.hpp:51
Namespace that encompasses all of the Physical Quantities library's content.
NumericType Convert(const NumericType value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. Returns the converted...
Definition: Unit.hpp:210
void ConvertInPlace(NumericType &value, const Unit original_unit, const Unit new_unit)
Converts a value expressed in a given unit of measure to a new unit of measure. The conversion is per...
Definition: Unit.hpp:128
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition: Base.hpp:89