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 Total, S.A
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 
33 
39 inline constexpr double convertKToC( double kelvin )
40 { return kelvin - constants::zeroDegreesCelsiusInKelvin; }
46 inline constexpr double convertCToK( double celsius )
47 { return celsius + constants::zeroDegreesCelsiusInKelvin; }
48 
49 
53 enum Unit : integer
54 {
57 
60 
63 
66 
69 
72 
75 
78 
81 
84 
87 
90 
93 
96 
99 
102 };
103 
104 
109 constexpr inline std::string_view getDescription( Unit unit )
110 {
111  switch( unit )
112  {
113  default: return "unknown [?]";
114  case Dimensionless: return "dimensionless [1]";
115  case Pressure: return "pressure [Pa]";
116  case Temperature: return "temperature [K]";
117  case TemperatureInC: return "temperature [C]";
118  case Distance: return "distance [m]";
119  case Time: return "time [s]";
120  case Viscosity: return "viscosity [Pa*s]";
121  case Enthalpy: return "enthalpy [J/kg]";
122  case Density: return "density [kg/m3]";
123  case Solubility: return "solubility [g/L]";
124  case Mass: return "mass [kg]";
125  case Mole: return "mole [mol]";
126  case MassRate: return "mass rate [kg/s]";
127  case MoleRate: return "mole rate [mol/s]";
128  case Transmissibility: return "transmissibility [(Pa*s*rm3/s)/Pa]";
129  }
130 }
131 
136 constexpr inline std::string_view getSymbol( Unit unit )
137 {
138  switch( unit )
139  {
140  default: return "?";
141  case Dimensionless: return "1";
142  case Pressure: return "Pa";
143  case Temperature: return "K";
144  case TemperatureInC: return "C";
145  case Distance: return "m";
146  case Time: return "s";
147  case Viscosity: return "Pa*s";
148  case Enthalpy: return "J/kg";
149  case Density: return "kg/m3";
150  case Solubility: return "g/L";
151  case Mass: return "kg";
152  case Mole: return "mol";
153  case MassRate: return "kg/s";
154  case MoleRate: return "mol/s";
155  case Transmissibility: return "(Pa*s*rm3/s)/Pa";
156  }
157 }
158 
166 inline string formatValue( real64 value, Unit unit )
167 {
168  switch( unit )
169  {
170  default: return GEOS_FMT( "value of {} [?]", value );
171  case Dimensionless: return GEOS_FMT( "value of {} [1]", value );
172  case Pressure: return GEOS_FMT( "pressure of {} [Pa]", value );
173  case Temperature: return GEOS_FMT( "temperature of {} [K]", value );
174  case TemperatureInC: return GEOS_FMT( "temperature of {} [K]", convertCToK( value ) );
175  case Distance: return GEOS_FMT( "distance of {} [s]", value );
176  case Time: return GEOS_FMT( "time of {} [s]", value );
177  case Viscosity: return GEOS_FMT( "viscosity of {} [Pa*s]", value );
178  case Enthalpy: return GEOS_FMT( "enthalpy of {} [J/kg]", value );
179  case Density: return GEOS_FMT( "density of {} [kg/m3]", value );
180  case Solubility: return GEOS_FMT( "solubility of {} [g/L]", value );
181  case Mass: return GEOS_FMT( "mass of {} [kg]", value );
182  case Mole: return GEOS_FMT( "mole of {} [mol]", value );
183  case MassRate: return GEOS_FMT( "mass rate of {} [kg/s]", value );
184  case MoleRate: return GEOS_FMT( "mole rate of {} [mol/s]", value );
185  case Transmissibility: return GEOS_FMT( "transmissibility of {} [(Pa*s*rm3/s)/Pa]", value );
186  }
187 }
188 
189 
191 using SystemClock = std::chrono::high_resolution_clock;
192 
194 using YearDaysRatio = std::ratio< 146097, 400 >;
196 using Days = std::chrono::duration< int64_t, std::ratio_multiply< std::ratio< 24 >, std::chrono::hours::period > >;
198 using Years = std::chrono::duration< int64_t, std::ratio_multiply< YearDaysRatio, Days::period > >;
199 
201 static constexpr double YearDays = ( double )YearDaysRatio::num / YearDaysRatio::den;
203 static constexpr double MinuteSeconds = 60.0;
205 static constexpr double HourSeconds = 60.0 * MinuteSeconds;
207 static constexpr double DaySeconds = 24.0 * HourSeconds;
209 static constexpr double YearSeconds = YearDays * DaySeconds;
210 
211 
216 {
218  double const m_totalSeconds = 0.0;
220  int const m_years = 0;
222  int const m_days = 0;
224  int const m_hours = 0;
226  int const m_minutes = 0;
228  int const m_seconds = 0;
229 
239  TimeFormatInfo( double totalSeconds, int years, int days, int hours, int minutes, int seconds );
244  static TimeFormatInfo fromSeconds( double const seconds );
250  template< typename DURATION > static TimeFormatInfo fromDuration( DURATION duration );
251 
255  friend std::ostream & operator<<( std::ostream & os, TimeFormatInfo const & ctx );
256 
260  string toString() const;
261 
265  string toUnfoldedString() const;
266 
270  string toSecondsString() const;
271 };
272 
273 template< typename DURATION >
275 {
276  using namespace std::chrono;
277 
278  auto const totalYears = duration_cast< units::Years >( value );
279  auto const daysOut = duration_cast< units::Days >( value - totalYears );
280  auto const hoursOut = duration_cast< hours >( value - totalYears - daysOut );
281  auto const minutesOut = duration_cast< minutes >( value - totalYears - daysOut - hoursOut );
282  auto const secondsOut = duration_cast< seconds >( value - totalYears - daysOut - hoursOut - minutesOut );
283 
284  return TimeFormatInfo( duration< double >( value ).count(), int( totalYears.count() ),
285  int( daysOut.count() ), int( hoursOut.count() ),
286  int( minutesOut.count() ), int( secondsOut.count() ) );
287 }
288 
289 } // end namespace units
290 
291 } // end namespace geos
292 
293 
297 template<>
298 struct GEOS_FMT_NS::formatter< geos::units::TimeFormatInfo > : GEOS_FMT_NS::formatter< std::string >
299 {
306  auto format( geos::units::TimeFormatInfo const & durationData, format_context & ctx ) const
307  {
308  return GEOS_FMT_NS::formatter< std::string >::format( durationData.toString(), ctx );
309  }
310 };
311 
312 #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:46
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:198
std::ratio< 146097, 400 > YearDaysRatio
One year = 365.2425 days (= 146097 / 400) following the Gregorian calendar and the C++ convention.
Definition: Units.hpp:194
constexpr std::string_view getDescription(Unit unit)
Definition: Units.hpp:109
static constexpr double YearSeconds
Seconds in a year.
Definition: Units.hpp:209
std::chrono::high_resolution_clock SystemClock
Clock in use in GEOS to manipulate system times.
Definition: Units.hpp:191
constexpr GEOS_HOST_DEVICE double convertKToC(double kelvin)
Definition: Units.hpp:39
Unit
Enumerator of available unit types. Units are in SI by default.
Definition: Units.hpp:54
@ Transmissibility
Transmissibility in m2/s.
Definition: Units.hpp:101
@ Mass
Mass in kg.
Definition: Units.hpp:89
@ Time
Time in seconds.
Definition: Units.hpp:74
@ Distance
Distance in meter.
Definition: Units.hpp:71
@ Density
Density in kg/m³
Definition: Units.hpp:80
@ MoleRate
Mole rate in mol/s.
Definition: Units.hpp:98
@ MassRate
Mass rate in kg/s.
Definition: Units.hpp:95
@ TemperatureInC
Temperature in Celcius.
Definition: Units.hpp:68
@ Viscosity
Viscosity in Pa*s.
Definition: Units.hpp:77
@ Solubility
Solubility in g/L.
Definition: Units.hpp:86
@ Mole
Mole in mol.
Definition: Units.hpp:92
@ Pressure
Pressure in Pascal.
Definition: Units.hpp:62
@ Enthalpy
Enthalpy in J/kg.
Definition: Units.hpp:83
@ Dimensionless
Label to use when a value has not physical dimension (ratio values, propotions...)
Definition: Units.hpp:59
@ Temperature
Temperature in Kelvin.
Definition: Units.hpp:65
@ Unknown
Default label when a unit is not known for a value.
Definition: Units.hpp:56
static constexpr double HourSeconds
Seconds in a hour.
Definition: Units.hpp:205
static constexpr double MinuteSeconds
Seconds in a minute.
Definition: Units.hpp:203
static constexpr double DaySeconds
Seconds in a day.
Definition: Units.hpp:207
constexpr std::string_view getSymbol(Unit unit)
Definition: Units.hpp:136
static constexpr double YearDays
Days in one year (following the Gregorian calendar and the C++ convention) = 365.2425 days (= 146097 ...
Definition: Units.hpp:201
string formatValue(real64 value, Unit unit)
Format the specified value coherently with the specified unit.
Definition: Units.hpp:166
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:196
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:306
Stores information that is useful to duration strings. Based on the geos::units time constants.
Definition: Units.hpp:216
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:228
int const m_minutes
Number of integral minutes to show.
Definition: Units.hpp:226
static TimeFormatInfo fromSeconds(double const seconds)
int const m_hours
Number of integral hours to show.
Definition: Units.hpp:224
string toSecondsString() const
double const m_totalSeconds
Total time (including the decimal part) this instance represents in seconds.
Definition: Units.hpp:218
static TimeFormatInfo fromDuration(DURATION duration)
Definition: Units.hpp:274
int const m_years
Number of integral years to show.
Definition: Units.hpp:220
int const m_days
Number of integral days to show.
Definition: Units.hpp:222
TimeFormatInfo(double totalSeconds, int years, int days, int hours, int minutes, int seconds)
Construct a TimeFormatInfo from raw data (which must be coherent)