GEOSX
Units.hpp
Go to the documentation of this file.
1 /*
2  * -------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_MATH_UNITS_HPP_
20 #define GEOS_MATH_UNITS_HPP_
21 
22 #include "common/DataTypes.hpp"
24 
25 namespace geos
26 {
27 
28 namespace units
29 {
30 
31 
37 inline constexpr double convertKToC( double kelvin )
38 { return kelvin - constants::zeroDegreesCelsiusInKelvin; }
44 inline constexpr double convertCToK( double celsius )
45 { return celsius + constants::zeroDegreesCelsiusInKelvin; }
46 
47 
51 enum Unit : integer
52 {
55 
58 
61 
64 
67 
70 
73 
76 
79 
82 
85 };
86 
87 
92 constexpr inline std::string_view getDescription( Unit unit )
93 {
94  switch( unit )
95  {
96  default: return "unknown [?]";
97  case Dimensionless: return "dimensionless [1]";
98  case Pressure: return "pressure [Pa]";
99  case Temperature: return "temperature [K]";
100  case TemperatureInC: return "temperature [C]";
101  case Distance: return "distance [m]";
102  case Time: return "time [s]";
103  case Viscosity: return "viscosity [Pa*s]";
104  case Enthalpy: return "enthalpy [J/kg]";
105  case Density: return "density [kg/m3]";
106  case Solubility: return "solubility [g/L]";
107  }
108 }
109 
114 constexpr inline std::string_view getSymbol( Unit unit )
115 {
116  switch( unit )
117  {
118  default: return "?";
119  case Dimensionless: return "1";
120  case Pressure: return "Pa";
121  case Temperature: return "K";
122  case TemperatureInC: return "C";
123  case Distance: return "m";
124  case Time: return "s";
125  }
126 }
127 
135 inline string formatValue( real64 value, Unit unit )
136 {
137  switch( unit )
138  {
139  default: return GEOS_FMT( "value of {} [?]", value );
140  case Dimensionless: return GEOS_FMT( "value of {} [1]", value );
141  case Pressure: return GEOS_FMT( "pressure of {} [Pa]", value );
142  case Temperature: return GEOS_FMT( "temperature of {} [K]", value );
143  case TemperatureInC: return GEOS_FMT( "temperature of {} [K]", convertCToK( value ) );
144  case Distance: return GEOS_FMT( "distance of {} [s]", value );
145  case Time: return GEOS_FMT( "time of {} [s]", value );
146  }
147 }
148 
149 
151 using SystemClock = std::chrono::system_clock;
152 
154 using YearDaysRatio = std::ratio< 146097, 400 >;
156 using Days = std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > >;
158 using Years = std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > >;
159 
161 static constexpr double YearDays = ( double )YearDaysRatio::num / YearDaysRatio::den;
163 static constexpr double MinuteSeconds = 60.0;
165 static constexpr double HourSeconds = 60.0 * MinuteSeconds;
167 static constexpr double DaySeconds = 24.0 * HourSeconds;
169 static constexpr double YearSeconds = YearDays * DaySeconds;
170 
171 
176 {
178  double m_totalSeconds = 0.0;
180  int m_years = 0;
182  int m_days = 0;
184  int m_hours = 0;
186  int m_minutes = 0;
188  int m_seconds = 0;
189 
190 
200  TimeFormatInfo( double totalSeconds, int years, int days, int hours, int minutes, int seconds );
205  static TimeFormatInfo fromSeconds( double const seconds );
211  template< typename DURATION > static TimeFormatInfo fromDuration( DURATION duration );
212 
216  friend std::ostream & operator<<( std::ostream & os, TimeFormatInfo const & ctx );
217 
221  string toString() const;
222 };
223 
224 
225 } // end namespace units
226 
227 } // end namespace geos
228 
229 
233 template<>
234 struct GEOS_FMT_NS::formatter< geos::units::TimeFormatInfo > : GEOS_FMT_NS::formatter< std::string >
235 {
242  auto format( geos::units::TimeFormatInfo const & durationData, format_context & ctx )
243  {
244  return GEOS_FMT_NS::formatter< std::string >::format( durationData.toString(), ctx );
245  }
246 };
247 
248 #endif //GEOS_MATH_PHYSICSCONSTANTS_HPP_
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:48
Regroups useful constants that are globally used for math and physics computations.
constexpr GEOS_HOST_DEVICE double convertCToK(double celsius)
Definition: Units.hpp:44
std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > > Years
Year helper duration type, equivalent to C++20 std::chrono::years.
Definition: Units.hpp:158
std::ratio< 146097, 400 > YearDaysRatio
One year = 365.2425 days (= 146097 / 400) following the Gregorian calendar and the C++ convention.
Definition: Units.hpp:154
constexpr std::string_view getDescription(Unit unit)
Definition: Units.hpp:92
static constexpr double YearSeconds
Seconds in a year.
Definition: Units.hpp:169
constexpr GEOS_HOST_DEVICE double convertKToC(double kelvin)
Definition: Units.hpp:37
std::chrono::system_clock SystemClock
Clock in use in GEOS to manipulate system times.
Definition: Units.hpp:151
Unit
Enumerator of available unit types. Units are in SI by default.
Definition: Units.hpp:52
@ Time
Time in seconds.
Definition: Units.hpp:72
@ Distance
Distance in meter.
Definition: Units.hpp:69
@ Density
Density in kg/m³
Definition: Units.hpp:78
@ TemperatureInC
Temperature in Celcius.
Definition: Units.hpp:66
@ Viscosity
Viscosity in Pa*s.
Definition: Units.hpp:75
@ Solubility
Solubility in g/L.
Definition: Units.hpp:84
@ Pressure
Pressure in Pascal.
Definition: Units.hpp:60
@ Enthalpy
Enthalpy in J/kg.
Definition: Units.hpp:81
@ Dimensionless
Label to use when a value has not physical dimension (ratio values, propotions...)
Definition: Units.hpp:57
@ Temperature
Temperature in Kelvin.
Definition: Units.hpp:63
@ Unknown
Default label when a unit is not known for a value.
Definition: Units.hpp:54
static constexpr double HourSeconds
Seconds in a hour.
Definition: Units.hpp:165
static constexpr double MinuteSeconds
Seconds in a minute.
Definition: Units.hpp:163
static constexpr double DaySeconds
Seconds in a day.
Definition: Units.hpp:167
constexpr std::string_view getSymbol(Unit unit)
Definition: Units.hpp:114
static constexpr double YearDays
Days in one year (following the Gregorian calendar and the C++ convention) = 365.2425 days (= 146097 ...
Definition: Units.hpp:161
string formatValue(real64 value, Unit unit)
Format the specified value coherently with the specified unit.
Definition: Units.hpp:135
std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > > Days
Day helper duration type, equivalent to C++20 std::chrono::days.
Definition: Units.hpp:156
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
std::string_view string_view
String type.
Definition: DataTypes.hpp:134
auto format(geos::units::TimeFormatInfo const &durationData, format_context &ctx)
Format the specified TimeFormatInfo to a string.
Definition: Units.hpp:242
Stores information that is useful to duration strings. Based on the geos::units time constants.
Definition: Units.hpp:176
friend std::ostream & operator<<(std::ostream &os, TimeFormatInfo const &ctx)
Insert the string representation information in the provided stream.
int m_hours
Number of integral hours to show.
Definition: Units.hpp:184
int m_years
Number of integral years to show.
Definition: Units.hpp:180
static TimeFormatInfo fromSeconds(double const seconds)
int m_days
Number of integral days to show.
Definition: Units.hpp:182
int m_minutes
Number of integral minutes to show.
Definition: Units.hpp:186
int m_seconds
Number of integral seconds to show.
Definition: Units.hpp:188
double m_totalSeconds
Total time (including the decimal part) this instance represents in seconds.
Definition: Units.hpp:178
TimeFormatInfo(double totalSeconds, int years, int days, int hours, int minutes, int seconds)
Construct a TimeFormatInfo from raw data (which must be coherent)
static TimeFormatInfo fromDuration(DURATION duration)