-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bi- and trilinear interpolation (#1020)
* Add interpolation * Make linter happy and remove dead code
- Loading branch information
Showing
5 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
//======================================================================================== | ||
// Parthenon performance portable AMR framework | ||
// Copyright(C) 2024 The Parthenon collaboration | ||
// Licensed under the 3-clause BSD License, see LICENSE file for details | ||
//======================================================================================== | ||
// Interpolation copied/refactored from | ||
// https://github.com/lanl/phoebus and https://github.com/lanl/spiner | ||
//======================================================================================== | ||
// © 2022. Triad National Security, LLC. All rights reserved. This | ||
// program was produced under U.S. Government contract | ||
// 89233218CNA000001 for Los Alamos National Laboratory (LANL), which | ||
// is operated by Triad National Security, LLC for the U.S. | ||
// Department of Energy/National Nuclear Security Administration. All | ||
// rights in the program are reserved by Triad National Security, LLC, | ||
// and the U.S. Department of Energy/National Nuclear Security | ||
// Administration. The Government is granted for itself and others | ||
// acting on its behalf a nonexclusive, paid-up, irrevocable worldwide | ||
// license in this material to reproduce, prepare derivative works, | ||
// distribute copies to the public, perform publicly and display | ||
// publicly, and to permit others to do so. | ||
|
||
#ifndef UTILS_INTERPOLATION_HPP_ | ||
#define UTILS_INTERPOLATION_HPP_ | ||
|
||
#include <algorithm> | ||
|
||
// Parthenon includes | ||
#include <coordinates/coordinates.hpp> | ||
#include <kokkos_abstraction.hpp> | ||
#include <parthenon/package.hpp> | ||
#include <utils/error_checking.hpp> | ||
#include <utils/robust.hpp> | ||
|
||
namespace parthenon { | ||
namespace interpolation { | ||
|
||
// From https://github.com/lanl/spiner/blob/main/spiner/regular_grid_1d.hpp | ||
// a poor-man's std::pair | ||
struct weights_t { | ||
Real first, second; | ||
KOKKOS_INLINE_FUNCTION Real &operator[](const int i) { | ||
assert(0 <= i && i <= 1); | ||
return i == 0 ? first : second; | ||
} | ||
}; | ||
|
||
// TODO(JMM): Is this interpolation::Do syntax reasonable? An | ||
// alternative path would be a class called "LCInterp with all | ||
// static functions. Then it could have an `operator()` which would | ||
// be maybe nicer? | ||
// TODO(JMM): Merge this w/ what Ben has done. | ||
namespace cent { | ||
namespace linear { | ||
|
||
/* | ||
* Get interpolation weights for linear interpolation | ||
* PARAM[IN] - x - location to interpolate to | ||
* PARAM[IN] - nx - number of points along this direction. Used for sanity checks. | ||
* PARAM[IN] - coords - parthenon coords object | ||
* PARAM[OUT] - ix - index of points to interpolate | ||
* PARAM[OUT] - w - weights | ||
*/ | ||
template <int DIR> | ||
KOKKOS_INLINE_FUNCTION void GetWeights(const Real x, const int nx, | ||
const Coordinates_t &coords, int &ix, | ||
weights_t &w) { | ||
PARTHENON_DEBUG_REQUIRE( | ||
typeid(Coordinates_t) == typeid(UniformCartesian), | ||
"Interpolation routines currently only work for UniformCartesian"); | ||
const Real min = coords.Xc<DIR>(0); // assume uniform Cartesian | ||
const Real dx = coords.CellWidthFA(DIR); | ||
ix = std::min(std::max(0, static_cast<int>(robust::ratio(x - min, dx))), nx - 2); | ||
const Real floor = min + ix * dx; | ||
w[1] = robust::ratio(x - floor, dx); | ||
w[0] = 1. - w[1]; | ||
} | ||
|
||
/* | ||
* Trilinear interpolation on a variable or meshblock pack | ||
* PARAM[IN] - b - Meshblock index | ||
* PARAM[IN] - X1, X2, X3 - Coordinate locations | ||
* PARAM[IN] - p - Variable or MeshBlockPack | ||
* PARAM[IN] - v - variable index | ||
*/ | ||
template <typename Pack> | ||
KOKKOS_INLINE_FUNCTION Real Do3D(int b, const Real X1, const Real X2, const Real X3, | ||
const Pack &p, int v) { | ||
const auto &coords = p.GetCoords(b); | ||
int ix[3]; | ||
weights_t w[3]; | ||
GetWeights<X1DIR>(X1, p.GetDim(1), coords, ix[0], w[0]); | ||
GetWeights<X2DIR>(X2, p.GetDim(2), coords, ix[1], w[1]); | ||
GetWeights<X3DIR>(X3, p.GetDim(3), coords, ix[2], w[2]); | ||
return (w[2][0] * (w[1][0] * (w[0][0] * p(b, v, ix[2], ix[1], ix[0]) + | ||
w[0][1] * p(b, v, ix[2], ix[1], ix[0] + 1)) + | ||
w[1][1] * (w[0][0] * p(b, v, ix[2], ix[1] + 1, ix[0]) + | ||
w[0][1] * p(b, v, ix[2], ix[1] + 1, ix[0] + 1))) + | ||
w[2][1] * (w[1][0] * (w[0][0] * p(b, v, ix[2] + 1, ix[1], ix[0]) + | ||
w[0][1] * p(b, v, ix[2] + 1, ix[1], ix[0] + 1)) + | ||
w[1][1] * (w[0][0] * p(b, v, ix[2] + 1, ix[1] + 1, ix[0]) + | ||
w[0][1] * p(b, v, ix[2] + 1, ix[1] + 1, ix[0] + 1)))); | ||
} | ||
|
||
/* | ||
* Bilinear interpolation on a variable or meshblock pack | ||
* PARAM[IN] - b - Meshblock index | ||
* PARAM[IN] - X1, X2 - Coordinate locations | ||
* PARAM[IN] - p - Variable or MeshBlockPack | ||
* PARAM[IN] - v - variable index | ||
*/ | ||
template <typename Pack> | ||
KOKKOS_INLINE_FUNCTION Real Do2D(int b, const Real X1, const Real X2, const Pack &p, | ||
int v) { | ||
const auto &coords = p.GetCoords(b); | ||
int ix1, ix2; | ||
weights_t w1, w2; | ||
GetWeights<X1DIR>(X1, p.GetDim(1), coords, ix1, w1); | ||
GetWeights<X2DIR>(X2, p.GetDim(2), coords, ix2, w2); | ||
return (w2[0] * (w1[0] * p(b, v, 0, ix2, ix1) + w1[1] * p(b, v, 0, ix2, ix1 + 1)) + | ||
w2[1] * | ||
(w1[0] * p(b, v, 0, ix2 + 1, ix1) + w1[1] * p(b, v, 0, ix2 + 1, ix1 + 1))); | ||
} | ||
|
||
/* | ||
* Linear interpolation on a variable or meshblock pack | ||
* PARAM[IN] - b - Meshblock index | ||
* PARAM[IN] - X1 - Coordinate location | ||
* PARAM[IN] - p - Variable or MeshBlockPack | ||
* PARAM[IN] - v - variable index | ||
*/ | ||
template <typename Pack> | ||
KOKKOS_INLINE_FUNCTION Real Do1D(int b, const Real X1, const Pack &p, int v) { | ||
const auto &coords = p.GetCoords(b); | ||
int ix; | ||
weights_t w; | ||
GetWeights<X1DIR>(X1, p.GetDim(1), coords, ix, w); | ||
return w[0] * p(b, v, 0, 0, ix) + w[1] * p(b, v, 0, 0, ix + 1); | ||
} | ||
|
||
/* | ||
* Trilinear or bilinear interpolation on a variable or meshblock pack | ||
* PARAM[IN] - axisymmetric | ||
* PARAM[IN] - b - Meshblock index | ||
* PARAM[IN] - X1, X2, X3 - Coordinate locations | ||
* PARAM[IN] - p - Variable or MeshBlockPack | ||
* PARAM[IN] - v - variable index | ||
*/ | ||
// JMM: I know this won't vectorize because of the switch, but it | ||
// probably won't anyway, since we're doing trilinear | ||
// interpolation, which will kill memory locality. Doing it this | ||
// way means we can do trilinear vs bilinear which I think is a | ||
// sufficient win at minimum code bloat. | ||
template <typename Pack> | ||
KOKKOS_INLINE_FUNCTION Real Do(int b, const Real X1, const Real X2, const Real X3, | ||
const Pack &p, int v) { | ||
if (p.GetDim(3) > 1) { | ||
return Do3D(b, X1, X2, X3, p, v); | ||
} else if (p.GetDim(2) > 1) { | ||
return Do2D(b, X1, X2, p, v); | ||
} else { // 1D | ||
return Do1D(b, X1, p, v); | ||
} | ||
} | ||
|
||
} // namespace linear | ||
} // namespace cent | ||
} // namespace interpolation | ||
} // namespace parthenon | ||
#endif // UTILS_INTERPOLATION_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//======================================================================================== | ||
// Parthenon performance portable AMR framework | ||
// Copyright(C) 2024 The Parthenon collaboration | ||
// Licensed under the 3-clause BSD License, see LICENSE file for details | ||
//======================================================================================== | ||
// Copied from https://github.com/lanl/phoebus | ||
//======================================================================================== | ||
// © 2022. Triad National Security, LLC. All rights reserved. This | ||
// program was produced under U.S. Government contract | ||
// 89233218CNA000001 for Los Alamos National Laboratory (LANL), which | ||
// is operated by Triad National Security, LLC for the U.S. | ||
// Department of Energy/National Nuclear Security Administration. All | ||
// rights in the program are reserved by Triad National Security, LLC, | ||
// and the U.S. Department of Energy/National Nuclear Security | ||
// Administration. The Government is granted for itself and others | ||
// acting on its behalf a nonexclusive, paid-up, irrevocable worldwide | ||
// license in this material to reproduce, prepare derivative works, | ||
// distribute copies to the public, perform publicly and display | ||
// publicly, and to permit others to do so. | ||
|
||
#ifndef UTILS_ROBUST_HPP_ | ||
#define UTILS_ROBUST_HPP_ | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
#include <config.hpp> | ||
#include <kokkos_abstraction.hpp> | ||
|
||
namespace parthenon { | ||
namespace robust { | ||
|
||
template <typename T = Real> | ||
KOKKOS_FORCEINLINE_FUNCTION constexpr auto LARGE() { | ||
return 0.1 * std::numeric_limits<T>::max(); | ||
} | ||
template <typename T = Real> | ||
KOKKOS_FORCEINLINE_FUNCTION constexpr auto SMALL() { | ||
return 10 * std::numeric_limits<T>::min(); | ||
} | ||
template <typename T = Real> | ||
KOKKOS_FORCEINLINE_FUNCTION constexpr auto EPS() { | ||
return 10 * std::numeric_limits<T>::epsilon(); | ||
} | ||
|
||
template <typename T> | ||
KOKKOS_FORCEINLINE_FUNCTION auto make_positive(const T val) { | ||
return std::max(val, EPS<T>()); | ||
} | ||
|
||
KOKKOS_FORCEINLINE_FUNCTION | ||
Real make_bounded(const Real val, const Real vmin, const Real vmax) { | ||
return std::min(std::max(val, vmin + EPS()), vmax * (1.0 - EPS())); | ||
} | ||
|
||
template <typename T> | ||
KOKKOS_INLINE_FUNCTION int sgn(const T &val) { | ||
return (T(0) <= val) - (val < T(0)); | ||
} | ||
template <typename A, typename B> | ||
KOKKOS_INLINE_FUNCTION auto ratio(const A &a, const B &b) { | ||
const B sgn = b >= 0 ? 1 : -1; | ||
return a / (b + sgn * SMALL<B>()); | ||
} | ||
} // namespace robust | ||
} // namespace parthenon | ||
#endif // UTILS_ROBUST_HPP_ |