GEOS
Units.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_MATH_UNITS_HPP_
21 #define GEOS_MATH_UNITS_HPP_
22 
23 #include "common/DataTypes.hpp"
25 #include "common/format/Format.hpp"
26 
27 namespace geos
28 {
29 
30 namespace units
31 {
32 
34 static constexpr double DarcyToSqM = 9.869233e-13;
35 
41 inline constexpr double convertKToC( double kelvin )
42 { return kelvin - constants::zeroDegreesCelsiusInKelvin; }
48 inline constexpr double convertCToK( double celsius )
49 { return celsius + constants::zeroDegreesCelsiusInKelvin; }
50 
51 
55 enum Unit : integer
56 {
59 
62 
65 
68 
71 
74 
77 
80 
83 
86 
89 
92 
95 
98 
101 
104 };
105 
106 
111 constexpr inline std::string_view getDescription( Unit unit )
112 {
113  switch( unit )
114  {
115  default: return "unknown [?]";
116  case Dimensionless: return "dimensionless [1]";
117  case Pressure: return "pressure [Pa]";
118  case Temperature: return "temperature [K]";
119  case TemperatureInC: return "temperature [C]";
120  case Distance: return "distance [m]";
121  case Time: return "time [s]";
122  case Viscosity: return "viscosity [Pa*s]";
123  case Enthalpy: return "enthalpy [J/kg]";
124  case Density: return "density [kg/m3]";
125  case Solubility: return "solubility [g/L]";
126  case Mass: return "mass [kg]";
127  case Mole: return "mole [mol]";
128  case MassRate: return "mass rate [kg/s]";
129  case MoleRate: return "mole rate [mol/s]";
130  case Transmissibility: return "transmissibility [(Pa*s*rm3/s)/Pa]";
131  }
132 }
133 
138 constexpr inline std::string_view getSymbol( Unit unit )
139 {
140  switch( unit )
141  {
142  default: return "?";
143  case Dimensionless: return "1";
144  case Pressure: return "Pa";
145  case Temperature: return "K";
146  case TemperatureInC: return "C";
147  case Distance: return "m";
148  case Time: return "s";
149  case Viscosity: return "Pa*s";
150  case Enthalpy: return "J/kg";
151  case Density: return "kg/m3";
152  case Solubility: return "g/L";
153  case Mass: return "kg";
154  case Mole: return "mol";
155  case MassRate: return "kg/s";
156  case MoleRate: return "mol/s";
157  case Transmissibility: return "(Pa*s*rm3/s)/Pa";
158  }
159 }
160 
168 inline string formatValue( real64 value, Unit unit )
169 {
170  switch( unit )
171  {
172  default: return GEOS_FMT( "value of {} [?]", value );
173  case Dimensionless: return GEOS_FMT( "value of {} [1]", value );
174  case Pressure: return GEOS_FMT( "pressure of {} [Pa]", value );
175  case Temperature: return GEOS_FMT( "temperature of {} [K]", value );
176  case TemperatureInC: return GEOS_FMT( "temperature of {} [K]", convertCToK( value ) );
177  case Distance: return GEOS_FMT( "distance of {} [s]", value );
178  case Time: return GEOS_FMT( "time of {} [s]", value );
179  case Viscosity: return GEOS_FMT( "viscosity of {} [Pa*s]", value );
180  case Enthalpy: return GEOS_FMT( "enthalpy of {} [J/kg]", value );
181  case Density: return GEOS_FMT( "density of {} [kg/m3]", value );
182  case Solubility: return GEOS_FMT( "solubility of {} [g/L]", value );
183  case Mass: return GEOS_FMT( "mass of {} [kg]", value );
184  case Mole: return GEOS_FMT( "mole of {} [mol]", value );
185  case MassRate: return GEOS_FMT( "mass rate of {} [kg/s]", value );
186  case MoleRate: return GEOS_FMT( "mole rate of {} [mol/s]", value );
187  case Transmissibility: return GEOS_FMT( "transmissibility of {} [(Pa*s*rm3/s)/Pa]", value );
188  }
189 }
190 
191 
193 using SystemClock = std::chrono::high_resolution_clock;
194 
196 using YearDaysRatio = std::ratio< 146097, 400 >;
198 using Days = std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > >;
200 using Years = std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > >;
201 
203 static constexpr double YearDays = ( double )YearDaysRatio::num / YearDaysRatio::den;
205 static constexpr double MinuteSeconds = 60.0;
207 static constexpr double HourSeconds = 60.0 * MinuteSeconds;
209 static constexpr double DaySeconds = 24.0 * HourSeconds;
211 static constexpr double YearSeconds = YearDays * DaySeconds;
212 
213 
218 {
220  double const m_totalSeconds = 0.0;
222  int const m_years = 0;
224  int const m_days = 0;
226  int const m_hours = 0;
228  int const m_minutes = 0;
230  int const m_seconds = 0;
231 
241  TimeFormatInfo( double totalSeconds, int years, int days, int hours, int minutes, int seconds );
246  static TimeFormatInfo fromSeconds( double const seconds );
252  template< typename DURATION > static TimeFormatInfo fromDuration( DURATION duration );
253 
257  friend std::ostream & operator<<( std::ostream & os, TimeFormatInfo const & ctx );
258 
262  string toString() const;
263 
267  string toUnfoldedString() const;
268 
272  string toSecondsString() const;
273 };
274 
275 template< typename DURATION >
277 {
278  using namespace std::chrono;
279 
280  auto const totalYears = duration_cast< units::Years >( value );
281  auto const daysOut = duration_cast< units::Days >( value - totalYears );
282  auto const hoursOut = duration_cast< hours >( value - totalYears - daysOut );
283  auto const minutesOut = duration_cast< minutes >( value - totalYears - daysOut - hoursOut );
284  auto const secondsOut = duration_cast< seconds >( value - totalYears - daysOut - hoursOut - minutesOut );
285 
286  return TimeFormatInfo( duration< double >( value ).count(), int( totalYears.count() ),
287  int( daysOut.count() ), int( hoursOut.count() ),
288  int( minutesOut.count() ), int( secondsOut.count() ) );
289 }
290 
291 } // end namespace units
292 
293 } // end namespace geos
294 
295 
299 template<>
300 struct GEOS_FMT_NS::formatter< geos::units::TimeFormatInfo > : GEOS_FMT_NS::formatter< std::string >
301 {
308  auto format( geos::units::TimeFormatInfo const & durationData, format_context & ctx ) const
309  {
310  return GEOS_FMT_NS::formatter< std::string >::format( durationData.toString(), ctx );
311  }
312 };
313 
314 #endif //GEOS_MATH_PHYSICSCONSTANTS_HPP_
#define GEOS_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:49
Regroups useful constants that are globally used for math and physics computations.
constexpr GEOS_HOST_DEVICE double convertCToK(double celsius)
Definition: Units.hpp:48
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:200
std::ratio< 146097, 400 > YearDaysRatio
One year = 365.2425 days (= 146097 / 400) following the Gregorian calendar and the C++ convention.
Definition: Units.hpp:196
constexpr std::string_view getDescription(Unit unit)
Definition: Units.hpp:111
static constexpr double YearSeconds
Seconds in a year.
Definition: Units.hpp:211
std::chrono::high_resolution_clock SystemClock
Clock in use in GEOS to manipulate system times.
Definition: Units.hpp:193
constexpr GEOS_HOST_DEVICE double convertKToC(double kelvin)
Definition: Units.hpp:41
Unit
Enumerator of available unit types. Units are in SI by default.
Definition: Units.hpp:56
@ Transmissibility
Transmissibility in m2/s.
Definition: Units.hpp:103
@ Mass
Mass in kg.
Definition: Units.hpp:91
@ Time
Time in seconds.
Definition: Units.hpp:76
@ Distance
Distance in meter.
Definition: Units.hpp:73
@ Density
Density in kg/m³
Definition: Units.hpp:82
@ MoleRate
Mole rate in mol/s.
Definition: Units.hpp:100
@ MassRate
Mass rate in kg/s.
Definition: Units.hpp:97
@ TemperatureInC
Temperature in Celcius.
Definition: Units.hpp:70
@ Viscosity
Viscosity in Pa*s.
Definition: Units.hpp:79
@ Solubility
Solubility in g/L.
Definition: Units.hpp:88
@ Mole
Mole in mol.
Definition: Units.hpp:94
@ Pressure
Pressure in Pascal.
Definition: Units.hpp:64
@ Enthalpy
Enthalpy in J/kg.
Definition: Units.hpp:85
@ Dimensionless
Label to use when a value has not physical dimension (ratio values, propotions...)
Definition: Units.hpp:61
@ Temperature
Temperature in Kelvin.
Definition: Units.hpp:67
@ Unknown
Default label when a unit is not known for a value.
Definition: Units.hpp:58
static constexpr double DarcyToSqM
Darcy to m^2 conversion factor.
Definition: Units.hpp:34
static constexpr double HourSeconds
Seconds in a hour.
Definition: Units.hpp:207
static constexpr double MinuteSeconds
Seconds in a minute.
Definition: Units.hpp:205
static constexpr double DaySeconds
Seconds in a day.
Definition: Units.hpp:209
constexpr std::string_view getSymbol(Unit unit)
Definition: Units.hpp:138
static constexpr double YearDays
Days in one year (following the Gregorian calendar and the C++ convention) = 365.2425 days (= 146097 ...
Definition: Units.hpp:203
string formatValue(real64 value, Unit unit)
Format the specified value coherently with the specified unit.
Definition: Units.hpp:168
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:198
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
std::string_view string_view
String type.
Definition: DataTypes.hpp:94
auto format(geos::units::TimeFormatInfo const &durationData, format_context &ctx) const
Format the specified TimeFormatInfo to a string.
Definition: Units.hpp:308
Stores information that is useful to duration strings. Based on the geos::units time constants.
Definition: Units.hpp:218
string toUnfoldedString() const
friend std::ostream & operator<<(std::ostream &os, TimeFormatInfo const &ctx)
Insert the string representation information in the provided stream.
int const m_seconds
Number of integral seconds to show.
Definition: Units.hpp:230
int const m_minutes
Number of integral minutes to show.
Definition: Units.hpp:228
static TimeFormatInfo fromSeconds(double const seconds)
int const m_hours
Number of integral hours to show.
Definition: Units.hpp:226
string toSecondsString() const
double const m_totalSeconds
Total time (including the decimal part) this instance represents in seconds.
Definition: Units.hpp:220
static TimeFormatInfo fromDuration(DURATION duration)
Definition: Units.hpp:276
int const m_years
Number of integral years to show.
Definition: Units.hpp:222
int const m_days
Number of integral days to show.
Definition: Units.hpp:224
TimeFormatInfo(double totalSeconds, int years, int days, int hours, int minutes, int seconds)
Construct a TimeFormatInfo from raw data (which must be coherent)