Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Volume.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_UNIT_VOLUME_HPP
26 #define PHQ_UNIT_VOLUME_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <map>
32 #include <ostream>
33 #include <string_view>
34 #include <unordered_map>
35 
36 #include "../Base.hpp"
37 #include "../Dimension/ElectricCurrent.hpp"
38 #include "../Dimension/Length.hpp"
39 #include "../Dimension/LuminousIntensity.hpp"
40 #include "../Dimension/Mass.hpp"
41 #include "../Dimension/SubstanceAmount.hpp"
42 #include "../Dimension/Temperature.hpp"
43 #include "../Dimension/Time.hpp"
44 #include "../Dimensions.hpp"
45 #include "../Unit.hpp"
46 #include "../UnitSystem.hpp"
47 
48 namespace PhQ {
49 
50 namespace Unit {
51 
52 /// \brief Volume units.
53 enum class Volume : int8_t {
54  /// \brief Cubic metre (m^3) volume unit.
55  CubicMetre,
56 
57  /// \brief Cubic nautical mile (nmi^3) volume unit.
59 
60  /// \brief Cubic mile (mi^3) volume unit.
61  CubicMile,
62 
63  /// \brief Cubic kilometre (km^3) volume unit.
65 
66  /// \brief Cubic yard (yd^3) volume unit.
67  CubicYard,
68 
69  /// \brief Cubic foot (ft^3) volume unit.
70  CubicFoot,
71 
72  /// \brief Cubic decimetre (dm^3) volume unit.
74 
75  /// \brief Litre (L) volume unit.
76  Litre,
77 
78  /// \brief Cubic inch (in^3) volume unit.
79  CubicInch,
80 
81  /// \brief Cubic centimetre (cm^3) volume unit.
83 
84  /// \brief Millilitre (mL) volume unit.
85  Millilitre,
86 
87  /// \brief Cubic millimetre (mm^3) volume unit.
89 
90  /// \brief Cubic milliinch (mil^3) volume unit.
92 
93  /// \brief Cubic micrometre (μm^3) volume unit.
95 
96  /// \brief Cubic microinch (μin^3) volume unit.
98 };
99 
100 } // namespace Unit
101 
102 /// \brief Standard volume unit: cubic metre (m^3).
103 template <>
104 inline constexpr const Unit::Volume Standard<Unit::Volume>{Unit::Volume::CubicMetre};
105 
106 /// \brief Physical dimension set of volume units.
107 template <>
108 inline constexpr const Dimensions RelatedDimensions<Unit::Volume>{
109  Dimensions{Dimension::Time{0}, Dimension::Length{3}, Dimension::Mass{0},
111  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
112 };
113 
114 inline std::ostream& operator<<(std::ostream& stream, const Unit::Volume unit) {
115  stream << Abbreviation(unit);
116  return stream;
117 }
118 
119 namespace Internal {
120 
121 template <>
122 inline const std::map<UnitSystem, Unit::Volume> ConsistentUnits<Unit::Volume>{
127 };
128 
129 template <>
130 inline const std::map<Unit::Volume, UnitSystem> RelatedUnitSystems<Unit::Volume>{
135 };
136 
137 // clang-format off
138 
139 template <>
140 inline const std::map<Unit::Volume, std::string_view> Abbreviations<Unit::Volume>{
141  {Unit::Volume::CubicMetre, "m^3" },
143  {Unit::Volume::CubicMile, "mi^3" },
144  {Unit::Volume::CubicKilometre, "km^3" },
145  {Unit::Volume::CubicYard, "yd^3" },
146  {Unit::Volume::CubicFoot, "ft^3" },
147  {Unit::Volume::CubicDecimetre, "dm^3" },
148  {Unit::Volume::Litre, "L" },
149  {Unit::Volume::CubicInch, "in^3" },
151  {Unit::Volume::Millilitre, "mL" },
153  {Unit::Volume::CubicMilliinch, "mil^3"},
154  {Unit::Volume::CubicMicrometre, "μm^3" },
155  {Unit::Volume::CubicMicroinch, "μin^3"},
156 };
157 
158 template <>
159 inline const std::unordered_map<std::string_view, Unit::Volume> Spellings<Unit::Volume>{
160  {"m^3", Unit::Volume::CubicMetre },
161  {"m3", Unit::Volume::CubicMetre },
164  {"mi^3", Unit::Volume::CubicMile },
165  {"mi3", Unit::Volume::CubicMile },
166  {"km^3", Unit::Volume::CubicKilometre },
168  {"yd^3", Unit::Volume::CubicYard },
169  {"yd3", Unit::Volume::CubicYard },
170  {"ft^3", Unit::Volume::CubicFoot },
171  {"ft3", Unit::Volume::CubicFoot },
172  {"dm^3", Unit::Volume::CubicDecimetre },
174  {"L", Unit::Volume::Litre },
175  {"in^3", Unit::Volume::CubicInch },
176  {"in3", Unit::Volume::CubicInch },
179  {"mL", Unit::Volume::Millilitre },
182  {"millinch^3", Unit::Volume::CubicMilliinch },
183  {"millinch3", Unit::Volume::CubicMilliinch },
184  {"milliinch^3", Unit::Volume::CubicMilliinch },
185  {"milliinch3", Unit::Volume::CubicMilliinch },
186  {"mil^3", Unit::Volume::CubicMilliinch },
187  {"mil3", Unit::Volume::CubicMilliinch },
188  {"thou^3", Unit::Volume::CubicMilliinch },
189  {"thou3", Unit::Volume::CubicMilliinch },
190  {"μm^3", Unit::Volume::CubicMicrometre },
194  {"μin^3", Unit::Volume::CubicMicroinch },
195  {"μin3", Unit::Volume::CubicMicroinch },
196  {"uin^3", Unit::Volume::CubicMicroinch },
197  {"uin3", Unit::Volume::CubicMicroinch },
198 };
199 
200 // clang-format on
201 
202 template <>
203 template <typename NumericType>
204 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMetre>::FromStandard(
205  NumericType& /*value*/) noexcept {}
206 
207 template <>
208 template <typename NumericType>
209 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMetre>::ToStandard(
210  NumericType& /*value*/) noexcept {}
211 
212 template <>
213 template <typename NumericType>
214 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicNauticalMile>::FromStandard(
215  NumericType& value) noexcept {
216  value /= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L)
217  * static_cast<NumericType>(1852.0L);
218 }
219 
220 template <>
221 template <typename NumericType>
222 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicNauticalMile>::ToStandard(
223  NumericType& value) noexcept {
224  value *= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L)
225  * static_cast<NumericType>(1852.0L);
226 }
227 
228 template <>
229 template <typename NumericType>
230 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMile>::FromStandard(
231  NumericType& value) noexcept {
232  value /= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L)
233  * static_cast<NumericType>(1609.344L);
234 }
235 
236 template <>
237 template <typename NumericType>
238 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMile>::ToStandard(
239  NumericType& value) noexcept {
240  value *= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L)
241  * static_cast<NumericType>(1609.344L);
242 }
243 
244 template <>
245 template <typename NumericType>
246 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicKilometre>::FromStandard(
247  NumericType& value) noexcept {
248  value *= static_cast<NumericType>(1.0E-9L);
249 }
250 
251 template <>
252 template <typename NumericType>
253 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicKilometre>::ToStandard(
254  NumericType& value) noexcept {
255  value *= static_cast<NumericType>(1.0E9L);
256 }
257 
258 template <>
259 template <typename NumericType>
260 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicYard>::FromStandard(
261  NumericType& value) noexcept {
262  value /= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L)
263  * static_cast<NumericType>(0.9144L);
264 }
265 
266 template <>
267 template <typename NumericType>
268 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicYard>::ToStandard(
269  NumericType& value) noexcept {
270  value *= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L)
271  * static_cast<NumericType>(0.9144L);
272 }
273 
274 template <>
275 template <typename NumericType>
276 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicFoot>::FromStandard(
277  NumericType& value) noexcept {
278  value /= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
279  * static_cast<NumericType>(0.3048L);
280 }
281 
282 template <>
283 template <typename NumericType>
284 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicFoot>::ToStandard(
285  NumericType& value) noexcept {
286  value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
287  * static_cast<NumericType>(0.3048L);
288 }
289 
290 template <>
291 template <typename NumericType>
292 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicDecimetre>::FromStandard(
293  NumericType& value) noexcept {
294  value *= static_cast<NumericType>(1000.0L);
295 }
296 
297 template <>
298 template <typename NumericType>
299 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicDecimetre>::ToStandard(
300  NumericType& value) noexcept {
301  value *= static_cast<NumericType>(0.001L);
302 }
303 
304 template <>
305 template <typename NumericType>
306 inline constexpr void Conversion<Unit::Volume, Unit::Volume::Litre>::FromStandard(
307  NumericType& value) noexcept {
308  value *= static_cast<NumericType>(1000.0L);
309 }
310 
311 template <>
312 template <typename NumericType>
313 inline constexpr void Conversion<Unit::Volume, Unit::Volume::Litre>::ToStandard(
314  NumericType& value) noexcept {
315  value *= static_cast<NumericType>(0.001L);
316 }
317 
318 template <>
319 template <typename NumericType>
320 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicInch>::FromStandard(
321  NumericType& value) noexcept {
322  value /= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
323  * static_cast<NumericType>(0.0254L);
324 }
325 
326 template <>
327 template <typename NumericType>
328 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicInch>::ToStandard(
329  NumericType& value) noexcept {
330  value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
331  * static_cast<NumericType>(0.0254L);
332 }
333 
334 template <>
335 template <typename NumericType>
336 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicCentimetre>::FromStandard(
337  NumericType& value) noexcept {
338  value *= static_cast<NumericType>(1.0E6L);
339 }
340 
341 template <>
342 template <typename NumericType>
343 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicCentimetre>::ToStandard(
344  NumericType& value) noexcept {
345  value *= static_cast<NumericType>(1.0E-6L);
346 }
347 
348 template <>
349 template <typename NumericType>
350 inline constexpr void Conversion<Unit::Volume, Unit::Volume::Millilitre>::FromStandard(
351  NumericType& value) noexcept {
352  value *= static_cast<NumericType>(1.0E6L);
353 }
354 
355 template <>
356 template <typename NumericType>
357 inline constexpr void Conversion<Unit::Volume, Unit::Volume::Millilitre>::ToStandard(
358  NumericType& value) noexcept {
359  value *= static_cast<NumericType>(1.0E-6L);
360 }
361 
362 template <>
363 template <typename NumericType>
364 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMillimetre>::FromStandard(
365  NumericType& value) noexcept {
366  value *= static_cast<NumericType>(1.0E9L);
367 }
368 
369 template <>
370 template <typename NumericType>
371 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMillimetre>::ToStandard(
372  NumericType& value) noexcept {
373  value *= static_cast<NumericType>(1.0E-9L);
374 }
375 
376 template <>
377 template <typename NumericType>
378 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMilliinch>::FromStandard(
379  NumericType& value) noexcept {
380  value /= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L)
381  * static_cast<NumericType>(0.0000254L);
382 }
383 
384 template <>
385 template <typename NumericType>
386 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMilliinch>::ToStandard(
387  NumericType& value) noexcept {
388  value *= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L)
389  * static_cast<NumericType>(0.0000254L);
390 }
391 
392 template <>
393 template <typename NumericType>
394 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicrometre>::FromStandard(
395  NumericType& value) noexcept {
396  value *= static_cast<NumericType>(1.0E18L);
397 }
398 
399 template <>
400 template <typename NumericType>
401 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicrometre>::ToStandard(
402  NumericType& value) noexcept {
403  value *= static_cast<NumericType>(1.0E-18L);
404 }
405 
406 template <>
407 template <typename NumericType>
408 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicroinch>::FromStandard(
409  NumericType& value) noexcept {
410  value /= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L)
411  * static_cast<NumericType>(0.0000000254L);
412 }
413 
414 template <>
415 template <typename NumericType>
416 inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicroinch>::ToStandard(
417  NumericType& value) noexcept {
418  value *= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L)
419  * static_cast<NumericType>(0.0000000254L);
420 }
421 
422 template <typename NumericType>
423 inline const std::map<Unit::Volume, std::function<void(NumericType* const, const std::size_t size)>>
424  MapOfConversionsFromStandard<Unit::Volume, NumericType>{
426  Conversions<Unit::Volume, Unit::Volume::CubicMetre>::FromStandard<NumericType> },
428  Conversions<Unit::Volume, Unit::Volume::CubicNauticalMile>::FromStandard<NumericType>},
430  Conversions<Unit::Volume, Unit::Volume::CubicMile>::FromStandard<NumericType> },
432  Conversions<Unit::Volume, Unit::Volume::CubicKilometre>::FromStandard<NumericType> },
434  Conversions<Unit::Volume, Unit::Volume::CubicYard>::FromStandard<NumericType> },
436  Conversions<Unit::Volume, Unit::Volume::CubicFoot>::FromStandard<NumericType> },
438  Conversions<Unit::Volume, Unit::Volume::CubicDecimetre>::FromStandard<NumericType> },
440  Conversions<Unit::Volume, Unit::Volume::Litre>::FromStandard<NumericType> },
442  Conversions<Unit::Volume, Unit::Volume::CubicInch>::FromStandard<NumericType> },
444  Conversions<Unit::Volume, Unit::Volume::CubicCentimetre>::FromStandard<NumericType> },
446  Conversions<Unit::Volume, Unit::Volume::Millilitre>::FromStandard<NumericType> },
448  Conversions<Unit::Volume, Unit::Volume::CubicMillimetre>::FromStandard<NumericType> },
450  Conversions<Unit::Volume, Unit::Volume::CubicMilliinch>::FromStandard<NumericType> },
452  Conversions<Unit::Volume, Unit::Volume::CubicMicrometre>::FromStandard<NumericType> },
454  Conversions<Unit::Volume, Unit::Volume::CubicMicroinch>::FromStandard<NumericType> },
455 };
456 
457 template <typename NumericType>
458 inline const std::map<Unit::Volume,
459  std::function<void(NumericType* values, const std::size_t size)>>
460  MapOfConversionsToStandard<Unit::Volume, NumericType>{
462  Conversions<Unit::Volume, Unit::Volume::CubicMetre>::ToStandard<NumericType> },
464  Conversions<Unit::Volume, Unit::Volume::CubicNauticalMile>::ToStandard<NumericType>},
466  Conversions<Unit::Volume, Unit::Volume::CubicMile>::ToStandard<NumericType> },
468  Conversions<Unit::Volume, Unit::Volume::CubicKilometre>::ToStandard<NumericType> },
470  Conversions<Unit::Volume, Unit::Volume::CubicYard>::ToStandard<NumericType> },
472  Conversions<Unit::Volume, Unit::Volume::CubicFoot>::ToStandard<NumericType> },
474  Conversions<Unit::Volume, Unit::Volume::CubicDecimetre>::ToStandard<NumericType> },
476  Conversions<Unit::Volume, Unit::Volume::Litre>::ToStandard<NumericType> },
478  Conversions<Unit::Volume, Unit::Volume::CubicInch>::ToStandard<NumericType> },
480  Conversions<Unit::Volume, Unit::Volume::CubicCentimetre>::ToStandard<NumericType> },
482  Conversions<Unit::Volume, Unit::Volume::Millilitre>::ToStandard<NumericType> },
484  Conversions<Unit::Volume, Unit::Volume::CubicMillimetre>::ToStandard<NumericType> },
486  Conversions<Unit::Volume, Unit::Volume::CubicMilliinch>::ToStandard<NumericType> },
488  Conversions<Unit::Volume, Unit::Volume::CubicMicrometre>::ToStandard<NumericType> },
490  Conversions<Unit::Volume, Unit::Volume::CubicMicroinch>::ToStandard<NumericType> },
491 };
492 
493 } // namespace Internal
494 
495 } // namespace PhQ
496 
497 #endif // PHQ_UNIT_VOLUME_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
Volume
Volume units.
Definition: Volume.hpp:53
@ CubicKilometre
Cubic kilometre (km^3) volume unit.
@ Litre
Litre (L) volume unit.
@ CubicNauticalMile
Cubic nautical mile (nmi^3) volume unit.
@ CubicMicrometre
Cubic micrometre (μm^3) volume unit.
@ CubicYard
Cubic yard (yd^3) volume unit.
@ Millilitre
Millilitre (mL) volume unit.
@ CubicFoot
Cubic foot (ft^3) volume unit.
@ CubicDecimetre
Cubic decimetre (dm^3) volume unit.
@ CubicCentimetre
Cubic centimetre (cm^3) volume unit.
@ CubicMicroinch
Cubic microinch (μin^3) volume unit.
@ CubicInch
Cubic inch (in^3) volume unit.
@ CubicMilliinch
Cubic milliinch (mil^3) volume unit.
@ CubicMetre
Cubic metre (m^3) volume unit.
@ CubicMillimetre
Cubic millimetre (mm^3) volume unit.
@ CubicMile
Cubic mile (mi^3) volume unit.
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Definition: Temperature.hpp:55
Time
Time units.
Definition: Time.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.
@ FootPoundSecondRankine
Foot-pound-second-rankine (ft·lbf·s·°R) system.
@ MillimetreGramSecondKelvin
Millimetre-gram-second-kelvin (mm·g·s·K) system.
@ MetreKilogramSecondKelvin
Metre-kilogram-second-kelvin (m·kg·s·K) system.
@ InchPoundSecondRankine
Inch-pound-second-rankine (in·lbf·s·°R) system.
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
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)