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 
58 enum Unit : integer
59 {
62 
65 
68 
71 
74 
77 
80 
83 
86 
89 
92 
95 
98 
101 
104 
107 
110 
113 
116 };
117 
118 
123 constexpr inline std::string_view getDescription( Unit unit )
124 {
125  switch( unit )
126  {
127  default: return "unknown [?]";
128  case Dimensionless: return "dimensionless [1]";
129  case Pressure: return "pressure [Pa]";
130  case Temperature: return "temperature [K]";
131  case TemperatureInC: return "temperature [C]";
132  case Distance: return "distance [m]";
133  case Time: return "time [s]";
134  case Viscosity: return "viscosity [Pa*s]";
135  case Enthalpy: return "enthalpy [J/kg]";
136  case Density: return "density [kg/m3]";
137  case Solubility: return "solubility [g/L]";
138  case Mass: return "mass [kg]";
139  case Mole: return "mole [mol]";
140  case MassRate: return "mass rate [kg/s]";
141  case MoleRate: return "mole rate [mol/s]";
142  case Transmissibility: return "transmissibility [(Pa*s*rm3/s)/Pa]";
143  case MolarVolume: return "molar volume [m3/mol]";
144  case MolarDensity: return "molar density [mol/m3]";
145  case Permeability: return "permeability [m2]";
146  }
147 }
148 
153 constexpr inline std::string_view getSymbol( Unit unit )
154 {
155  switch( unit )
156  {
157  default: return "?";
158  case Dimensionless: return "1";
159  case Pressure: return "Pa";
160  case Temperature: return "K";
161  case TemperatureInC: return "C";
162  case Distance: return "m";
163  case Time: return "s";
164  case Viscosity: return "Pa*s";
165  case Enthalpy: return "J/kg";
166  case Density: return "kg/m3";
167  case Solubility: return "g/L";
168  case Mass: return "kg";
169  case Mole: return "mol";
170  case MassRate: return "kg/s";
171  case MoleRate: return "mol/s";
172  case Transmissibility: return "(Pa*s*rm3/s)/Pa";
173  case MolarVolume: return "m3/mol";
174  case MolarDensity: return "mol/m3";
175  case Permeability: return "m2";
176  }
177 }
178 
183 constexpr inline std::string_view getVariableSymbol( Unit unit )
184 {
185  switch( unit )
186  {
187  default:
188  case Dimensionless: return "?";
189  case Pressure: return "P";
190  case Temperature: return "T";
191  case TemperatureInC: return "T";
192  case Distance: return "L";
193  case Time: return "t";
194  case Viscosity: return "mu";
195  case Enthalpy: return "H";
196  case Density: return "rho";
197  case Solubility: return "S";
198  case Mass: return "m";
199  case Mole: return "n";
200  case MassRate: return "Q_m";
201  case MoleRate: return "Q_n";
202  case Transmissibility: return "T_r";
203  case MolarVolume: return "V_m";
204  case MolarDensity: return "rho_n";
205  case Permeability: return "K";
206  }
207 }
208 
216 inline string formatValue( real64 value, Unit unit )
217 {
218  switch( unit )
219  {
220  default: return GEOS_FMT( "value of {} [?]", value );
221  case Dimensionless: return GEOS_FMT( "value of {} [1]", value );
222  case Pressure: return GEOS_FMT( "pressure of {} [Pa]", value );
223  case Temperature: return GEOS_FMT( "temperature of {} [K]", value );
224  case TemperatureInC: return GEOS_FMT( "temperature of {} [K]", convertCToK( value ) );
225  case Distance: return GEOS_FMT( "distance of {} [s]", value );
226  case Time: return GEOS_FMT( "time of {} [s]", value );
227  case Viscosity: return GEOS_FMT( "viscosity of {} [Pa*s]", value );
228  case Enthalpy: return GEOS_FMT( "enthalpy of {} [J/kg]", value );
229  case Density: return GEOS_FMT( "density of {} [kg/m3]", value );
230  case Solubility: return GEOS_FMT( "solubility of {} [g/L]", value );
231  case Mass: return GEOS_FMT( "mass of {} [kg]", value );
232  case Mole: return GEOS_FMT( "mole of {} [mol]", value );
233  case MassRate: return GEOS_FMT( "mass rate of {} [kg/s]", value );
234  case MoleRate: return GEOS_FMT( "mole rate of {} [mol/s]", value );
235  case Transmissibility: return GEOS_FMT( "transmissibility of {} [(Pa*s*rm3/s)/Pa]", value );
236  case MolarVolume: return GEOS_FMT( "molar volume of {} [m3/mol]", value );
237  case MolarDensity: return GEOS_FMT( "molar density of {} [mol/m3]", value );
238  case Permeability: return GEOS_FMT( "permeability of {} [m2]", value );
239  }
240 }
241 
242 
244 using SystemClock = std::chrono::high_resolution_clock;
245 
247 using YearDaysRatio = std::ratio< 146097, 400 >;
249 using Days = std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > >;
251 using Years = std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > >;
252 
254 static constexpr double YearDays = ( double )YearDaysRatio::num / YearDaysRatio::den;
256 static constexpr double MinuteSeconds = 60.0;
258 static constexpr double HourSeconds = 60.0 * MinuteSeconds;
260 static constexpr double DaySeconds = 24.0 * HourSeconds;
262 static constexpr double YearSeconds = YearDays * DaySeconds;
263 
264 
269 {
271  double const m_totalSeconds = 0.0;
273  int const m_years = 0;
275  int const m_days = 0;
277  int const m_hours = 0;
279  int const m_minutes = 0;
281  int const m_seconds = 0;
282 
292  TimeFormatInfo( double totalSeconds, int years, int days, int hours, int minutes, int seconds );
297  static TimeFormatInfo fromSeconds( double const seconds );
303  template< typename DURATION > static TimeFormatInfo fromDuration( DURATION duration );
304 
308  friend std::ostream & operator<<( std::ostream & os, TimeFormatInfo const & ctx );
309 
313  string toString() const;
314 
318  string toUnfoldedString() const;
319 
323  string toSecondsString() const;
324 };
325 
326 template< typename DURATION >
328 {
329  using namespace std::chrono;
330 
331  auto const totalYears = duration_cast< units::Years >( value );
332  auto const daysOut = duration_cast< units::Days >( value - totalYears );
333  auto const hoursOut = duration_cast< hours >( value - totalYears - daysOut );
334  auto const minutesOut = duration_cast< minutes >( value - totalYears - daysOut - hoursOut );
335  auto const secondsOut = duration_cast< seconds >( value - totalYears - daysOut - hoursOut - minutesOut );
336 
337  return TimeFormatInfo( duration< double >( value ).count(), int( totalYears.count() ),
338  int( daysOut.count() ), int( hoursOut.count() ),
339  int( minutesOut.count() ), int( secondsOut.count() ) );
340 }
341 
342 } // end namespace units
343 
344 } // end namespace geos
345 
346 
350 template<>
351 struct GEOS_FMT_NS::formatter< geos::units::TimeFormatInfo > : GEOS_FMT_NS::formatter< std::string >
352 {
359  auto format( geos::units::TimeFormatInfo const & durationData, format_context & ctx ) const
360  {
361  return GEOS_FMT_NS::formatter< std::string >::format( durationData.toString(), ctx );
362  }
363 };
364 
365 #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:251
std::ratio< 146097, 400 > YearDaysRatio
One year = 365.2425 days (= 146097 / 400) following the Gregorian calendar and the C++ convention.
Definition: Units.hpp:247
constexpr std::string_view getDescription(Unit unit)
Definition: Units.hpp:123
static constexpr double YearSeconds
Seconds in a year.
Definition: Units.hpp:262
constexpr std::string_view getVariableSymbol(Unit unit)
Definition: Units.hpp:183
std::chrono::high_resolution_clock SystemClock
Clock in use in GEOS to manipulate system times.
Definition: Units.hpp:244
constexpr GEOS_HOST_DEVICE double convertKToC(double kelvin)
Definition: Units.hpp:41
Unit
Enumerator of available unit types for given physical scales. Units are in SI by default.
Definition: Units.hpp:59
@ MolarDensity
Molar density in mol/m3.
Definition: Units.hpp:112
@ Transmissibility
Transmissibility in m2/s.
Definition: Units.hpp:106
@ Mass
Mass in kg.
Definition: Units.hpp:94
@ Time
Time in seconds.
Definition: Units.hpp:79
@ Distance
Distance in meter.
Definition: Units.hpp:76
@ Density
Density in kg/m³
Definition: Units.hpp:85
@ MoleRate
Mole rate in mol/s.
Definition: Units.hpp:103
@ MassRate
Mass rate in kg/s.
Definition: Units.hpp:100
@ TemperatureInC
Temperature in Celcius.
Definition: Units.hpp:73
@ Viscosity
Viscosity in Pa*s.
Definition: Units.hpp:82
@ Solubility
Solubility in g/L.
Definition: Units.hpp:91
@ Mole
Mole in mol.
Definition: Units.hpp:97
@ Pressure
Pressure in Pascal.
Definition: Units.hpp:67
@ Enthalpy
Enthalpy in J/kg.
Definition: Units.hpp:88
@ Dimensionless
Label to use when a value has not physical dimension (ratio values, propotions...)
Definition: Units.hpp:64
@ Temperature
Temperature in Kelvin.
Definition: Units.hpp:70
@ Unknown
Default label when a unit is not known for a value.
Definition: Units.hpp:61
@ MolarVolume
Molar volume in m3/mol.
Definition: Units.hpp:109
@ Permeability
Permeability in m^2.
Definition: Units.hpp:115
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:258
static constexpr double MinuteSeconds
Seconds in a minute.
Definition: Units.hpp:256
static constexpr double DaySeconds
Seconds in a day.
Definition: Units.hpp:260
constexpr std::string_view getSymbol(Unit unit)
Definition: Units.hpp:153
static constexpr double YearDays
Days in one year (following the Gregorian calendar and the C++ convention) = 365.2425 days (= 146097 ...
Definition: Units.hpp:254
string formatValue(real64 value, Unit unit)
Format the specified value coherently with the specified unit.
Definition: Units.hpp:216
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:249
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
int integer
Signed integer type.
Definition: DataTypes.hpp:81
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
auto format(geos::units::TimeFormatInfo const &durationData, format_context &ctx) const
Format the specified TimeFormatInfo to a string.
Definition: Units.hpp:359
Stores information that is useful to duration strings. Based on the geos::units time constants.
Definition: Units.hpp:269
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:281
int const m_minutes
Number of integral minutes to show.
Definition: Units.hpp:279
static TimeFormatInfo fromSeconds(double const seconds)
int const m_hours
Number of integral hours to show.
Definition: Units.hpp:277
string toSecondsString() const
double const m_totalSeconds
Total time (including the decimal part) this instance represents in seconds.
Definition: Units.hpp:271
static TimeFormatInfo fromDuration(DURATION duration)
Definition: Units.hpp:327
int const m_years
Number of integral years to show.
Definition: Units.hpp:273
int const m_days
Number of integral days to show.
Definition: Units.hpp:275
TimeFormatInfo(double totalSeconds, int years, int days, int hours, int minutes, int seconds)
Construct a TimeFormatInfo from raw data (which must be coherent)