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 
119 };
120 
121 
126 constexpr inline std::string_view getDescription( Unit unit )
127 {
128  switch( unit )
129  {
130  default: return "unknown [?]";
131  case Dimensionless: return "dimensionless [1]";
132  case Pressure: return "pressure [Pa]";
133  case Temperature: return "temperature [K]";
134  case TemperatureInC: return "temperature [C]";
135  case Distance: return "distance [m]";
136  case Time: return "time [s]";
137  case Viscosity: return "viscosity [Pa*s]";
138  case Enthalpy: return "enthalpy [J/kg]";
139  case Density: return "density [kg/m3]";
140  case Solubility: return "solubility [g/L]";
141  case Mass: return "mass [kg]";
142  case Mole: return "mole [mol]";
143  case MassRate: return "mass rate [kg/s]";
144  case MoleRate: return "mole rate [mol/s]";
145  case Transmissibility: return "transmissibility [(Pa*s*rm3/s)/Pa]";
146  case MolarVolume: return "molar volume [m3/mol]";
147  case MolarDensity: return "molar density [mol/m3]";
148  case Permeability: return "permeability [m2]";
149  case ReservoirVolume: return "reservoir volume [rm3]";
150  }
151 }
152 
157 constexpr inline std::string_view getSymbol( Unit unit )
158 {
159  switch( unit )
160  {
161  default: return "?";
162  case Dimensionless: return "1";
163  case Pressure: return "Pa";
164  case Temperature: return "K";
165  case TemperatureInC: return "C";
166  case Distance: return "m";
167  case Time: return "s";
168  case Viscosity: return "Pa*s";
169  case Enthalpy: return "J/kg";
170  case Density: return "kg/m3";
171  case Solubility: return "g/L";
172  case Mass: return "kg";
173  case Mole: return "mol";
174  case MassRate: return "kg/s";
175  case MoleRate: return "mol/s";
176  case Transmissibility: return "(Pa*s*rm3/s)/Pa";
177  case MolarVolume: return "m3/mol";
178  case MolarDensity: return "mol/m3";
179  case Permeability: return "m2";
180  case ReservoirVolume: return "rm3";
181  }
182 }
183 
188 constexpr inline std::string_view getVariableSymbol( Unit unit )
189 {
190  switch( unit )
191  {
192  default:
193  case Dimensionless: return "?";
194  case Pressure: return "P";
195  case Temperature: return "T";
196  case TemperatureInC: return "T";
197  case Distance: return "L";
198  case Time: return "t";
199  case Viscosity: return "mu";
200  case Enthalpy: return "H";
201  case Density: return "rho";
202  case Solubility: return "S";
203  case Mass: return "m";
204  case Mole: return "n";
205  case MassRate: return "Q_m";
206  case MoleRate: return "Q_n";
207  case Transmissibility: return "T_r";
208  case MolarVolume: return "V_m";
209  case MolarDensity: return "rho_n";
210  case Permeability: return "K";
211  case ReservoirVolume: return "V";
212  }
213 }
214 
222 inline string formatValue( real64 value, Unit unit )
223 {
224  switch( unit )
225  {
226  default: return GEOS_FMT( "value of {} [?]", value );
227  case Dimensionless: return GEOS_FMT( "value of {} [1]", value );
228  case Pressure: return GEOS_FMT( "pressure of {} [Pa]", value );
229  case Temperature: return GEOS_FMT( "temperature of {} [K]", value );
230  case TemperatureInC: return GEOS_FMT( "temperature of {} [K]", convertCToK( value ) );
231  case Distance: return GEOS_FMT( "distance of {} [s]", value );
232  case Time: return GEOS_FMT( "time of {} [s]", value );
233  case Viscosity: return GEOS_FMT( "viscosity of {} [Pa*s]", value );
234  case Enthalpy: return GEOS_FMT( "enthalpy of {} [J/kg]", value );
235  case Density: return GEOS_FMT( "density of {} [kg/m3]", value );
236  case Solubility: return GEOS_FMT( "solubility of {} [g/L]", value );
237  case Mass: return GEOS_FMT( "mass of {} [kg]", value );
238  case Mole: return GEOS_FMT( "mole of {} [mol]", value );
239  case MassRate: return GEOS_FMT( "mass rate of {} [kg/s]", value );
240  case MoleRate: return GEOS_FMT( "mole rate of {} [mol/s]", value );
241  case Transmissibility: return GEOS_FMT( "transmissibility of {} [(Pa*s*rm3/s)/Pa]", value );
242  case MolarVolume: return GEOS_FMT( "molar volume of {} [m3/mol]", value );
243  case MolarDensity: return GEOS_FMT( "molar density of {} [mol/m3]", value );
244  case Permeability: return GEOS_FMT( "permeability of {} [m2]", value );
245  case ReservoirVolume: return GEOS_FMT( "reservoir volume of {} [rm3]", value );
246  }
247 }
248 
249 
251 using SystemClock = std::chrono::high_resolution_clock;
252 
254 using YearDaysRatio = std::ratio< 146097, 400 >;
256 using Days = std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > >;
258 using Years = std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > >;
259 
261 static constexpr double YearDays = ( double )YearDaysRatio::num / YearDaysRatio::den;
263 static constexpr double MinuteSeconds = 60.0;
265 static constexpr double HourSeconds = 60.0 * MinuteSeconds;
267 static constexpr double DaySeconds = 24.0 * HourSeconds;
269 static constexpr double YearSeconds = YearDays * DaySeconds;
270 
271 
276 {
278  double const m_totalSeconds = 0.0;
280  int const m_years = 0;
282  int const m_days = 0;
284  int const m_hours = 0;
286  int const m_minutes = 0;
288  int const m_seconds = 0;
289 
299  TimeFormatInfo( double totalSeconds, int years, int days, int hours, int minutes, int seconds );
304  static TimeFormatInfo fromSeconds( double const seconds );
310  template< typename DURATION > static TimeFormatInfo fromDuration( DURATION duration );
311 
315  friend std::ostream & operator<<( std::ostream & os, TimeFormatInfo const & ctx );
316 
320  string toString() const;
321 
325  string toUnfoldedString() const;
326 
330  string toSecondsString() const;
331 };
332 
333 template< typename DURATION >
335 {
336  using namespace std::chrono;
337 
338  auto const totalYears = duration_cast< units::Years >( value );
339  auto const daysOut = duration_cast< units::Days >( value - totalYears );
340  auto const hoursOut = duration_cast< hours >( value - totalYears - daysOut );
341  auto const minutesOut = duration_cast< minutes >( value - totalYears - daysOut - hoursOut );
342  auto const secondsOut = duration_cast< seconds >( value - totalYears - daysOut - hoursOut - minutesOut );
343 
344  return TimeFormatInfo( duration< double >( value ).count(), int( totalYears.count() ),
345  int( daysOut.count() ), int( hoursOut.count() ),
346  int( minutesOut.count() ), int( secondsOut.count() ) );
347 }
348 
349 } // end namespace units
350 
351 } // end namespace geos
352 
353 
357 template<>
358 struct GEOS_FMT_NS::formatter< geos::units::TimeFormatInfo > : GEOS_FMT_NS::formatter< std::string >
359 {
366  auto format( geos::units::TimeFormatInfo const & durationData, format_context & ctx ) const
367  {
368  return GEOS_FMT_NS::formatter< std::string >::format( durationData.toString(), ctx );
369  }
370 };
371 
372 #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:258
std::ratio< 146097, 400 > YearDaysRatio
One year = 365.2425 days (= 146097 / 400) following the Gregorian calendar and the C++ convention.
Definition: Units.hpp:254
constexpr std::string_view getDescription(Unit unit)
Definition: Units.hpp:126
static constexpr double YearSeconds
Seconds in a year.
Definition: Units.hpp:269
constexpr std::string_view getVariableSymbol(Unit unit)
Definition: Units.hpp:188
std::chrono::high_resolution_clock SystemClock
Clock in use in GEOS to manipulate system times.
Definition: Units.hpp:251
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
@ ReservoirVolume
Reservoir volume in rm^3.
Definition: Units.hpp:118
@ 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:265
static constexpr double MinuteSeconds
Seconds in a minute.
Definition: Units.hpp:263
static constexpr double DaySeconds
Seconds in a day.
Definition: Units.hpp:267
constexpr std::string_view getSymbol(Unit unit)
Definition: Units.hpp:157
static constexpr double YearDays
Days in one year (following the Gregorian calendar and the C++ convention) = 365.2425 days (= 146097 ...
Definition: Units.hpp:261
string formatValue(real64 value, Unit unit)
Format the specified value coherently with the specified unit.
Definition: Units.hpp:222
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:256
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:366
Stores information that is useful to duration strings. Based on the geos::units time constants.
Definition: Units.hpp:276
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:288
int const m_minutes
Number of integral minutes to show.
Definition: Units.hpp:286
static TimeFormatInfo fromSeconds(double const seconds)
int const m_hours
Number of integral hours to show.
Definition: Units.hpp:284
string toSecondsString() const
double const m_totalSeconds
Total time (including the decimal part) this instance represents in seconds.
Definition: Units.hpp:278
static TimeFormatInfo fromDuration(DURATION duration)
Definition: Units.hpp:334
int const m_years
Number of integral years to show.
Definition: Units.hpp:280
int const m_days
Number of integral days to show.
Definition: Units.hpp:282
TimeFormatInfo(double totalSeconds, int years, int days, int hours, int minutes, int seconds)
Construct a TimeFormatInfo from raw data (which must be coherent)