ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SBMAMRTime.H
Go to the documentation of this file.
1 #ifndef ERF_SBM_AMR_TIME_H_
2 #define ERF_SBM_AMR_TIME_H_
3 
4 #include <algorithm>
5 #include <cmath>
6 #include <limits>
7 
8 namespace erf_sbm {
9 
10 /** Compare values carrying one semantic time identity without admitting a
11  * neighboring physical substep. Callers must provide the smallest physical
12  * step represented by the comparison.
13  */
14 [[nodiscard]] inline bool semantic_time_matches (const double lhs,
15  const double rhs,
16  const double dt_min) noexcept
17 {
18  if (!std::isfinite(lhs) || !std::isfinite(rhs) ||
19  !std::isfinite(dt_min) || !(dt_min > 0.0)) {
20  return false;
21  }
22  const double scale = std::max({1.0, std::abs(lhs), std::abs(rhs)});
23  const double roundoff =
24  16.0 * std::numeric_limits<double>::epsilon() * scale;
25  const double tolerance = std::min(roundoff, dt_min / 32.0);
26  return std::abs(lhs - rhs) <= tolerance;
27 }
28 
29 /** Transfer tuples without a physical-step scale must share the exact finite
30  * timestamp supplied by their owner.
31  */
32 [[nodiscard]] inline bool semantic_time_matches_exact (const double lhs,
33  const double rhs) noexcept
34 {
35  return std::isfinite(lhs) && std::isfinite(rhs) && lhs == rhs;
36 }
37 
38 /** Whether a level's accepted state is at the host level time: to roundoff of
39  * the level's step, or exactly before any step has been taken.
40  */
41 [[nodiscard]] inline bool level_time_matches (const double state_time,
42  const double level_time,
43  const double level_dt) noexcept
44 {
45  return level_dt > 0.0
46  ? semantic_time_matches(state_time, level_time, level_dt)
47  : semantic_time_matches_exact(state_time, level_time);
48 }
49 
50 } // namespace erf_sbm
51 
52 #endif
Definition: ERF.H:128
bool level_time_matches(const double state_time, const double level_time, const double level_dt) noexcept
Definition: ERF_SBMAMRTime.H:41
bool semantic_time_matches(const double lhs, const double rhs, const double dt_min) noexcept
Definition: ERF_SBMAMRTime.H:14
bool semantic_time_matches_exact(const double lhs, const double rhs) noexcept
Definition: ERF_SBMAMRTime.H:32
real(c_double), parameter epsilon
Definition: ERF_module_model_constants.F90:12