Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanKKrueger committed Oct 23, 2024
1 parent 0486a9d commit aba2fd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 16 additions & 11 deletions spiner/regular_grid_1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ template <typename T = Real, typename Transform = TransformLinear,
typename std::enable_if<std::is_arithmetic<T>::value, bool>::type =
true>
class RegularGrid1D {
public:
public:
using ValueType = T;
static constexpr T rNaN = std::numeric_limits<T>::signaling_NaN();
static constexpr int iNaN = std::numeric_limits<int>::signaling_NaN();
Expand All @@ -71,14 +71,6 @@ class RegularGrid1D {
PORTABLE_ALWAYS_REQUIRE(xmin_ < xmax_ && N_ > 0, "Valid grid");
}

// TODO: min() and x(0) won't necessarily match.
// max() and x(nPoints-1) won't necessarily match.
// Should we do anything about this?
// Translate between x coordinate and index
PORTABLE_INLINE_FUNCTION T x(const int i) const {
return Transform::reverse(u(i));
}

// Returns closest index and weights for interpolation
PORTABLE_INLINE_FUNCTION void weights(const T &x, int &ix, weights_t<T> &w) const {
const T u = Transform::forward(x);
Expand All @@ -97,7 +89,7 @@ class RegularGrid1D {
return w[0] * A(ix) + w[1] * A(ix + 1);
}

// utitilies
// (in)equality comparison
PORTABLE_INLINE_FUNCTION bool
operator==(const RegularGrid1D<T, Transform> &other) const {
return (umin_ == other.umin_ &&
Expand All @@ -110,6 +102,7 @@ class RegularGrid1D {
return !(*this == other);
}

// queries
PORTABLE_INLINE_FUNCTION T min() const { return xmin_; }
PORTABLE_INLINE_FUNCTION T max() const { return xmax_; }
PORTABLE_INLINE_FUNCTION size_t nPoints() const { return N_; }
Expand All @@ -123,7 +116,19 @@ class RegularGrid1D {
std::isnan((T)N_));
}
PORTABLE_INLINE_FUNCTION bool isWellFormed() const { return !isnan(); }
// TODO: min() and x(0) won't necessarily match.
// max() and x(nPoints-1) won't necessarily match.
// Should we do anything about this?
// The easiest fix: if i == 0 return xmin_ and similar for xmax_
// Translate between x coordinate and index
PORTABLE_INLINE_FUNCTION T x(const int i) const {
return Transform::reverse(u(i));
}
PORTABLE_INLINE_FUNCTION int index(const T x) const {
return index_u(Transform::forward(x));
}

// HDF
#ifdef SPINER_USE_HDF
inline herr_t saveHDF(hid_t loc, const std::string &name) const {
static_assert(
Expand Down Expand Up @@ -163,7 +168,7 @@ class RegularGrid1D {
}
#endif

private:
private:
// Forces x in the interval
PORTABLE_INLINE_FUNCTION int bound(int ix) const {
#ifndef SPINER_DISABLE_BOUNDS_CHECKS
Expand Down
4 changes: 0 additions & 4 deletions test/regular_grid_1d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,12 @@ TEST_CASE("RegularGrid1D with transformations",
// Check all fixed points (lin)
for (std::size_t n = 0; n < N; ++n) {
const double xx = 102.3 * double(n) + 1;
const double uu = xx;
CHECK_THAT(glin.u(n), WithinRel(uu, 1.0e-12));
CHECK_THAT(glin.x(n), WithinRel(xx, 1.0e-12));
}

// Check all fixed points (log)
for (std::size_t n = 0; n < N; ++n) {
const double xx = std::pow(double(2), n);
const double uu = std::log(xx);
CHECK_THAT(glog.u(n), WithinRel(uu, 1.0e-12));
CHECK_THAT(glog.x(n), WithinRel(xx, 1.0e-12));
}

Expand Down

0 comments on commit aba2fd4

Please sign in to comment.