Skip to content

Commit

Permalink
Add soft equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
brryan committed Sep 21, 2024
1 parent fda5ac9 commit 925052b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ namespace Globals {

int nghost;

bool is_restart;

// all of these global variables are set at the start of main():
int my_rank; // MPI rank of this process
int nranks; // total number of MPI ranks
bool is_restart; // Whether this simulation is restarted from a checkpoint file

// sparse configuration values that are needed in various places
SparseConfig sparse_config;
Expand Down
18 changes: 18 additions & 0 deletions src/utils/robust.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ KOKKOS_INLINE_FUNCTION auto ratio(const A &a, const B &b) {
const B sgn = b >= 0 ? 1 : -1;
return a / (b + sgn * SMALL<B>());
}

KOKKOS_FORCEINLINE_FUNCTION
bool soft_equiv(const Real &val, const Real &ref,
const Real eps = 10. * std::numeric_limits<Real>::epsilon(),
const bool pass_on_small = true) {
if (std::fabs(val - ref) < precision * fabs(ref)) {
// Return true equivalence if value and reference differ by less than precision
return true;
} else {
if (std::fabs(ref) < std::numeric_limits<Real>::min() && pass_on_small) {
// Optionally return true if reference value is close to zero
return true;
} else {
return false;
}
}
}

} // namespace robust
} // namespace parthenon
#endif // UTILS_ROBUST_HPP_

0 comments on commit 925052b

Please sign in to comment.