| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. and Interpolation |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <cmath> |
| 14 |
#include <limits> |
| 15 |
|
| 16 |
#include "atlas/array/ArrayView.h" |
| 17 |
#include "atlas/functionspace/StructuredColumns.h" |
| 18 |
#include "atlas/grid/Stencil.h" |
| 19 |
#include "atlas/grid/StencilComputer.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
#include "atlas/util/CoordinateEnums.h" |
| 22 |
#include "atlas/util/Point.h" |
| 23 |
|
| 24 |
#include "Cubic3DLimiter.h" |
| 25 |
#include "CubicVerticalKernel.h" |
| 26 |
#include "LinearHorizontalKernel.h" |
| 27 |
#include "QuasiCubicHorizontalKernel.h" |
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
namespace interpolation { |
| 31 |
namespace method { |
| 32 |
|
| 33 |
struct QuasiCubicLinearPoints { |
| 34 |
constexpr QuasiCubicLinearPoints() {} |
| 35 |
static constexpr std::array<idx_t, 2> j{{1, 2}}; |
| 36 |
static constexpr std::array<idx_t, 2> jj{{0, 3}}; |
| 37 |
static constexpr std::array<idx_t, 2> jw{{4, 5}}; |
| 38 |
static constexpr std::array<idx_t, 2> i{{1, 2}}; |
| 39 |
static constexpr std::array<idx_t, 2> ii{{0, 3}}; |
| 40 |
}; |
| 41 |
|
| 42 |
class QuasiCubic3DKernel { |
| 43 |
using Limiter = Cubic3DLimiter; |
| 44 |
|
| 45 |
public: |
| 46 |
QuasiCubic3DKernel(const functionspace::StructuredColumns& fs, const util::Config& config = util::NoConfig()) { |
| 47 |
src_ = fs; |
| 48 |
ATLAS_ASSERT(src_); |
| 49 |
ATLAS_ASSERT(src_.halo() >= 2); |
| 50 |
ATLAS_ASSERT(src_.vertical().size()); |
| 51 |
quasi_cubic_horizontal_interpolation_ = QuasiCubicHorizontalKernel(src_, config); |
| 52 |
linear_horizontal_interpolation_ = LinearHorizontalKernel(src_, config); |
| 53 |
vertical_interpolation_ = CubicVerticalKernel(fs.vertical(), config); |
| 54 |
limiter_ = config.getBool("limiter", false); |
| 55 |
} |
| 56 |
|
| 57 |
private: |
| 58 |
functionspace::StructuredColumns src_; |
| 59 |
QuasiCubicHorizontalKernel quasi_cubic_horizontal_interpolation_; |
| 60 |
LinearHorizontalKernel linear_horizontal_interpolation_; |
| 61 |
CubicVerticalKernel vertical_interpolation_; |
| 62 |
bool limiter_{false}; |
| 63 |
|
| 64 |
public: |
| 65 |
static std::string className() { return "QuasiCubic3DKernel"; } |
| 66 |
static constexpr idx_t stencil_width() { return 4; } |
| 67 |
static constexpr idx_t stencil_size() { return 32; } |
| 68 |
static constexpr idx_t stencil_halo() { |
| 69 |
return static_cast<idx_t>(static_cast<double>(stencil_width()) / 2. + 0.5); |
| 70 |
} |
| 71 |
|
| 72 |
public: |
| 73 |
using Stencil = grid::Stencil3D<4>; |
| 74 |
struct Weights { |
| 75 |
std::array<std::array<double, 4>, 4> weights_i; |
| 76 |
std::array<double, 6> weights_j; |
| 77 |
std::array<double, 4> weights_k; |
| 78 |
struct LinearWeights { |
| 79 |
std::array<std::array<double, 2>, 2> weights_i; |
| 80 |
std::array<double, 2> weights_j; |
| 81 |
} linear; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
public: |
| 86 |
struct WorkSpace { |
| 87 |
Stencil stencil; |
| 88 |
Weights weights; |
| 89 |
}; |
| 90 |
|
| 91 |
template <typename stencil_t> |
| 92 |
void compute_stencil(double& x, const double y, const double z, stencil_t& stencil) const { |
| 93 |
quasi_cubic_horizontal_interpolation_.compute_stencil(x, y, stencil); |
| 94 |
vertical_interpolation_.compute_stencil(z, stencil); |
| 95 |
} |
| 96 |
|
| 97 |
template <typename weights_t> |
| 98 |
void compute_weights(double x, double y, double z, weights_t& weights) const { |
| 99 |
Stencil stencil; |
| 100 |
compute_stencil(x, y, z, stencil); |
| 101 |
compute_weights(x, y, z, stencil, weights); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
template <typename stencil_t, typename weights_t> |
| 106 |
void compute_weights(const double x, const double y, const double z, const stencil_t& stencil, |
| 107 |
weights_t& weights) const { |
| 108 |
quasi_cubic_horizontal_interpolation_.compute_weights(x, y, stencil, weights); |
| 109 |
|
| 110 |
|
| 111 |
// Insert more linear weights in available slots (weights_i[0], weights_i[3], weights_j[4], weights_j[5]) |
| 112 |
{ |
| 113 |
PointXY P1, P2; |
| 114 |
std::array<double, 2> yvec; |
| 115 |
constexpr QuasiCubicLinearPoints pts{}; |
| 116 |
// Top and bottom row x-direction |
| 117 |
for (idx_t l = 0; l < 2; ++l) { |
| 118 |
idx_t j = pts.j[l]; // index in stencil |
| 119 |
idx_t jj = pts.jj[l]; // row index in weights_i |
| 120 |
auto& weights_i = weights.weights_i[jj]; |
| 121 |
src_.compute_xy(stencil.i(pts.i[0], j), stencil.j(j), P1); |
| 122 |
src_.compute_xy(stencil.i(pts.i[1], j), stencil.j(j), P2); |
| 123 |
const double alpha = (P2.x() - x) / (P2.x() - P1.x()); |
| 124 |
weights_i[pts.ii[0]] = alpha; |
| 125 |
weights_i[pts.ii[1]] = 1. - alpha; |
| 126 |
yvec[l] = P1.y(); |
| 127 |
} |
| 128 |
// Compute weights in y-direction |
| 129 |
{ |
| 130 |
auto& weights_j = weights.weights_j; |
| 131 |
const double alpha = (yvec[1] - y) / (yvec[1] - yvec[0]); |
| 132 |
weights_j[4] = alpha; |
| 133 |
weights_j[5] = 1. - alpha; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
vertical_interpolation_.compute_weights(z, stencil, weights); |
| 138 |
} |
| 139 |
|
| 140 |
template <typename stencil_t, typename weights_t, typename array_t> |
| 141 |
typename std::enable_if<(array_t::RANK == 2), typename array_t::value_type>::type interpolate( |
| 142 |
const stencil_t& stencil, const weights_t& weights, const array_t& input) const { |
| 143 |
using Value = typename std::remove_const<typename array_t::value_type>::type; |
| 144 |
|
| 145 |
std::array<std::array<idx_t, stencil_width()>, stencil_width()> index; |
| 146 |
const auto& wk = weights.weights_k; |
| 147 |
|
| 148 |
Value output = 0.; |
| 149 |
|
| 150 |
// Horizontally quasi-cubic part for inner levels ( k = {1,2} ) |
| 151 |
{ |
| 152 |
// Inner levels, inner rows (cubic in i, cubic in j) --> 16 points |
| 153 |
const auto& wj = weights.weights_j; |
| 154 |
for (idx_t j = 1; j < 3; ++j) { // j = {1,2} |
| 155 |
const auto& wi = weights.weights_i[j]; |
| 156 |
for (idx_t i = 0; i < 4; ++i) { // i = {0,1,2,3} |
| 157 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 158 |
Value wij = wi[i] * wj[j]; |
| 159 |
for (idx_t k = 1; k < 3; ++k) { // k = {1,2} |
| 160 |
Value w = wij * wk[k]; |
| 161 |
output += w * input(n, stencil.k(k)); |
| 162 |
} |
| 163 |
index[j][i] = n; |
| 164 |
} |
| 165 |
} |
| 166 |
// Inner levels, outer rows: (linear in i, cubic in j) --> 8 points |
| 167 |
for (idx_t j = 0; j < 4; j += 3) { // j = {0,3} |
| 168 |
const auto& wi = weights.weights_i[j]; |
| 169 |
for (idx_t i = 1; i < 3; ++i) { // i = {1,2} |
| 170 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 171 |
Value wij = wi[i] * wj[j]; |
| 172 |
for (idx_t k = 1; k < 3; ++k) { // k = {1,2} |
| 173 |
Value w = wij * wk[k]; |
| 174 |
output += w * input(n, stencil.k(k)); |
| 175 |
} |
| 176 |
index[j][i] = n; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
// Horizontally Linear part for outer levels ( k = {0,3} ) |
| 181 |
{ |
| 182 |
constexpr QuasiCubicLinearPoints pts{}; |
| 183 |
// Outer levels: (linear in i, linear in j) -- > 8 points |
| 184 |
for (idx_t m = 0; m < 2; ++m) { |
| 185 |
idx_t j = pts.j[m]; // index in stencil ( j = {1,2} ) |
| 186 |
idx_t jj = pts.jj[m]; // row index in weights_i ( jj = {0,3} ) |
| 187 |
idx_t jw = pts.jw[m]; // jw = {4,5}; |
| 188 |
const auto& wi = weights.weights_i[jj]; |
| 189 |
const double wj = weights.weights_j[jw]; |
| 190 |
for (idx_t l = 0; l < 2; ++l) { |
| 191 |
idx_t i = pts.i[l]; // i = {1,2} |
| 192 |
idx_t ii = pts.ii[l]; // ii = {0,3} |
| 193 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 194 |
Value wij = wi[ii] * wj; |
| 195 |
for (idx_t k = 0; k < 4; k += 3) { // k = {0,3} |
| 196 |
Value w = wij * wk[k]; |
| 197 |
output += w * input(n, stencil.k(k)); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if (limiter_) { |
| 205 |
Limiter::limit_scalar(output, index, stencil, input); |
| 206 |
} |
| 207 |
return output; |
| 208 |
} |
| 209 |
|
| 210 |
template <typename Value> |
| 211 |
struct OutputView1D { |
| 212 |
template <typename Int> |
| 213 |
Value& operator()(Int v) { |
| 214 |
return data_[v]; |
| 215 |
} |
| 216 |
template <typename Int> |
| 217 |
Value& operator[](Int v) { |
| 218 |
return data_[v]; |
| 219 |
} |
| 220 |
static constexpr int RANK{1}; |
| 221 |
OutputView1D(Value* data): data_(data) {} |
| 222 |
using value_type = Value; |
| 223 |
|
| 224 |
Value* data_; |
| 225 |
}; |
| 226 |
|
| 227 |
template <typename Value> |
| 228 |
OutputView1D<Value> make_outputview(Value* data) const { |
| 229 |
return OutputView1D<Value>(data); |
| 230 |
} |
| 231 |
|
| 232 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 233 |
typename std::enable_if<(InputArray::RANK == 3), void>::type interpolate_vars(const stencil_t& stencil, |
| 234 |
const weights_t& weights, |
| 235 |
const InputArray& input, |
| 236 |
OutputArray& output, |
| 237 |
const idx_t nvar) const { |
| 238 |
using Value = typename OutputArray::value_type; |
| 239 |
|
| 240 |
std::array<std::array<idx_t, stencil_width()>, stencil_width()> index; |
| 241 |
const auto& wk = weights.weights_k; |
| 242 |
|
| 243 |
const Value* _input_; |
| 244 |
|
| 245 |
for (idx_t v = 0; v < nvar; ++v) { |
| 246 |
output[v] = 0.; |
| 247 |
} |
| 248 |
|
| 249 |
// Horizontally quasi-cubic part for inner levels ( k = {1,2} ) |
| 250 |
{ |
| 251 |
// Inner levels, inner rows (cubic in i, cubic in j) --> 16 points |
| 252 |
const auto& wj = weights.weights_j; |
| 253 |
for (idx_t j = 1; j < 3; ++j) { // j = {1,2} |
| 254 |
const auto& wi = weights.weights_i[j]; |
| 255 |
for (idx_t i = 0; i < 4; ++i) { // i = {0,1,2,3} |
| 256 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 257 |
Value wij = wi[i] * wj[j]; |
| 258 |
for (idx_t k = 1; k < 3; ++k) { // k = {1,2} |
| 259 |
Value w = wij * wk[k]; |
| 260 |
const idx_t kk = stencil.k(k); |
| 261 |
_input_ = &(input(n, kk, 0)); // Assumption that input.stride(2) == 1 |
| 262 |
for (idx_t v = 0; v < nvar; ++v) { |
| 263 |
output[v] += w * _input_[v]; |
| 264 |
} |
| 265 |
} |
| 266 |
index[j][i] = n; |
| 267 |
} |
| 268 |
} |
| 269 |
// Inner levels, outer rows: (linear in i, cubic in j) --> 8 points |
| 270 |
for (idx_t j = 0; j < 4; j += 3) { // j = {0,3} |
| 271 |
const auto& wi = weights.weights_i[j]; |
| 272 |
for (idx_t i = 1; i < 3; ++i) { // i = {1,2} |
| 273 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 274 |
Value wij = wi[i] * wj[j]; |
| 275 |
for (idx_t k = 1; k < 3; ++k) { // k = {1,2} |
| 276 |
Value w = wij * wk[k]; |
| 277 |
const idx_t kk = stencil.k(k); |
| 278 |
_input_ = &(input(n, kk, 0)); // Assumption that input.stride(2) == 1 |
| 279 |
for (idx_t v = 0; v < nvar; ++v) { |
| 280 |
output[v] += w * _input_[v]; |
| 281 |
} |
| 282 |
} |
| 283 |
index[j][i] = n; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
// Horizontally Linear part for outer levels ( k = {0,3} ) |
| 288 |
{ |
| 289 |
constexpr QuasiCubicLinearPoints pts{}; |
| 290 |
// Outer levels: (linear in i, linear in j) -- > 8 points |
| 291 |
for (idx_t m = 0; m < 2; ++m) { |
| 292 |
idx_t j = pts.j[m]; // index in stencil ( j = {1,2} ) |
| 293 |
idx_t jj = pts.jj[m]; // row index in weights_i ( jj = {0,3} ) |
| 294 |
idx_t jw = pts.jw[m]; // jw = {4,5}; |
| 295 |
const auto& wi = weights.weights_i[jj]; |
| 296 |
const double wj = weights.weights_j[jw]; |
| 297 |
for (idx_t l = 0; l < 2; ++l) { |
| 298 |
idx_t i = pts.i[l]; // i = {1,2} |
| 299 |
idx_t ii = pts.ii[l]; // ii = {0,3} |
| 300 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 301 |
Value wij = wi[ii] * wj; |
| 302 |
for (idx_t k = 0; k < 4; k += 3) { // k = {0,3} |
| 303 |
Value w = wij * wk[k]; |
| 304 |
const idx_t kk = stencil.k(k); |
| 305 |
_input_ = &(input(n, kk, 0)); // Assumption that input.stride(2) == 1 |
| 306 |
for (idx_t v = 0; v < nvar; ++v) { |
| 307 |
output[v] += w * _input_[v]; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
if (limiter_) { |
| 316 |
Limiter::limit_vars(index, stencil, input, output, nvar); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 322 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 1), void>::type interpolate( |
| 323 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, |
| 324 |
idx_t r) const { |
| 325 |
output(r) = interpolate(stencil, weights, input); |
| 326 |
} |
| 327 |
|
| 328 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 329 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 2), void>::type interpolate( |
| 330 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r, |
| 331 |
idx_t k) const { |
| 332 |
output(r, k) = interpolate(stencil, weights, input); |
| 333 |
} |
| 334 |
|
| 335 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 336 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 3), void>::type interpolate( |
| 337 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r, |
| 338 |
idx_t k) const { |
| 339 |
auto output_vars = make_outputview(&output(r, k, 0)); |
| 340 |
interpolate_vars(stencil, weights, input, output_vars, output.shape(2)); |
| 341 |
} |
| 342 |
|
| 343 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 344 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 3), void>::type interpolate( |
| 345 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 346 |
ATLAS_NOTIMPLEMENTED; |
| 347 |
} |
| 348 |
|
| 349 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 350 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate( |
| 351 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/) const { |
| 352 |
ATLAS_NOTIMPLEMENTED; |
| 353 |
} |
| 354 |
|
| 355 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 356 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate( |
| 357 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 358 |
ATLAS_NOTIMPLEMENTED; |
| 359 |
} |
| 360 |
|
| 361 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 362 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 2), void>::type interpolate( |
| 363 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 364 |
ATLAS_NOTIMPLEMENTED; |
| 365 |
} |
| 366 |
}; // namespace method |
| 367 |
|
| 368 |
} // namespace method |
| 369 |
} // namespace interpolation |
| 370 |
} // namespace atlas |