| 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 "CubicHorizontalKernel.h" |
| 26 |
#include "CubicVerticalKernel.h" |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace interpolation { |
| 30 |
namespace method { |
| 31 |
|
| 32 |
class Cubic3DKernel { |
| 33 |
using Limiter = Cubic3DLimiter; |
| 34 |
|
| 35 |
public: |
| 36 |
Cubic3DKernel(const functionspace::StructuredColumns& fs, const util::Config& config = util::NoConfig()) { |
| 37 |
src_ = fs; |
| 38 |
ATLAS_ASSERT(src_); |
| 39 |
ATLAS_ASSERT(src_.halo() >= 2); |
| 40 |
ATLAS_ASSERT(src_.vertical().size()); |
| 41 |
horizontal_interpolation_ = CubicHorizontalKernel(src_, config); |
| 42 |
vertical_interpolation_ = CubicVerticalKernel(fs.vertical(), config); |
| 43 |
limiter_ = config.getBool("limiter", false); |
| 44 |
} |
| 45 |
|
| 46 |
private: |
| 47 |
functionspace::StructuredColumns src_; |
| 48 |
CubicHorizontalKernel horizontal_interpolation_; |
| 49 |
CubicVerticalKernel vertical_interpolation_; |
| 50 |
bool limiter_{false}; |
| 51 |
|
| 52 |
public: |
| 53 |
static std::string className() { return "Cubic3DKernel"; } |
| 54 |
static constexpr idx_t stencil_width() { return 4; } |
| 55 |
static constexpr idx_t stencil_size() { return stencil_width() * stencil_width() * stencil_width(); } |
| 56 |
static constexpr idx_t stencil_halo() { |
| 57 |
return static_cast<idx_t>(static_cast<double>(stencil_width()) / 2. + 0.5); |
| 58 |
} |
| 59 |
|
| 60 |
public: |
| 61 |
using Stencil = grid::Stencil3D<4>; |
| 62 |
struct Weights { |
| 63 |
std::array<std::array<double, 4>, 4> weights_i; |
| 64 |
std::array<double, 4> weights_j; |
| 65 |
std::array<double, 4> weights_k; |
| 66 |
friend std::ostream& operator<<(std::ostream& out, const Weights& v) { ATLAS_NOTIMPLEMENTED; } |
| 67 |
}; |
| 68 |
|
| 69 |
public: |
| 70 |
struct WorkSpace { |
| 71 |
Stencil stencil; |
| 72 |
Weights weights; |
| 73 |
}; |
| 74 |
|
| 75 |
template <typename stencil_t> |
| 76 |
void compute_stencil(double& x, const double y, const double z, stencil_t& stencil) const { |
| 77 |
horizontal_interpolation_.compute_stencil(x, y, stencil); |
| 78 |
vertical_interpolation_.compute_stencil(z, stencil); |
| 79 |
} |
| 80 |
|
| 81 |
template <typename weights_t> |
| 82 |
void compute_weights(double x, double y, double z, weights_t& weights) const { |
| 83 |
Stencil stencil; |
| 84 |
compute_stencil(x, y, z, stencil); |
| 85 |
compute_weights(x, y, z, stencil, weights); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
template <typename stencil_t, typename weights_t> |
| 90 |
void compute_weights(const double x, const double y, const double z, const stencil_t& stencil, |
| 91 |
weights_t& weights) const { |
| 92 |
horizontal_interpolation_.compute_weights(x, y, stencil, weights); |
| 93 |
vertical_interpolation_.compute_weights(z, stencil, weights); |
| 94 |
} |
| 95 |
|
| 96 |
template <typename stencil_t, typename weights_t, typename array_t> |
| 97 |
typename std::enable_if<(array_t::RANK == 2), typename array_t::value_type>::type interpolate( |
| 98 |
const stencil_t& stencil, const weights_t& weights, const array_t& input) const { |
| 99 |
using Value = typename std::remove_const<typename array_t::value_type>::type; |
| 100 |
|
| 101 |
std::array<std::array<idx_t, stencil_width()>, stencil_width()> index; |
| 102 |
const auto& wj = weights.weights_j; |
| 103 |
const auto& wk = weights.weights_k; |
| 104 |
|
| 105 |
Value output = 0.; |
| 106 |
for (idx_t j = 0; j < stencil_width(); ++j) { |
| 107 |
const auto& wi = weights.weights_i[j]; |
| 108 |
for (idx_t i = 0; i < stencil_width(); ++i) { |
| 109 |
idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 110 |
Value wij = wi[i] * wj[j]; |
| 111 |
for (idx_t k = 0; k < stencil_width(); ++k) { |
| 112 |
Value w = wij * wk[k]; |
| 113 |
output += w * input(n, stencil.k(k)); |
| 114 |
} |
| 115 |
index[j][i] = n; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (limiter_) { |
| 120 |
Limiter::limit_scalar(output, index, stencil, input); |
| 121 |
} |
| 122 |
return output; |
| 123 |
} |
| 124 |
|
| 125 |
template <typename Value> |
| 126 |
struct OutputView1D { |
| 127 |
template <typename Int> |
| 128 |
Value& operator()(Int v) { |
| 129 |
return data_[v]; |
| 130 |
} |
| 131 |
template <typename Int> |
| 132 |
Value& operator[](Int v) { |
| 133 |
return data_[v]; |
| 134 |
} |
| 135 |
static constexpr int RANK{1}; |
| 136 |
OutputView1D(Value* data): data_(data) {} |
| 137 |
using value_type = Value; |
| 138 |
|
| 139 |
Value* data_; |
| 140 |
}; |
| 141 |
|
| 142 |
template <typename Value> |
| 143 |
OutputView1D<Value> make_outputview(Value* data) const { |
| 144 |
return OutputView1D<Value>(data); |
| 145 |
} |
| 146 |
|
| 147 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 148 |
typename std::enable_if<(InputArray::RANK == 3), void>::type interpolate_vars(const stencil_t& stencil, |
| 149 |
const weights_t& weights, |
| 150 |
const InputArray& input, |
| 151 |
OutputArray& output, |
| 152 |
const idx_t nvar) const { |
| 153 |
using Value = typename InputArray::value_type; |
| 154 |
|
| 155 |
std::array<std::array<idx_t, stencil_width()>, stencil_width()> index; |
| 156 |
const auto& wj = weights.weights_j; |
| 157 |
const auto& wk = weights.weights_k; |
| 158 |
|
| 159 |
const Value* _input_; |
| 160 |
|
| 161 |
for (idx_t v = 0; v < nvar; ++v) { |
| 162 |
output[v] = 0.; |
| 163 |
} |
| 164 |
|
| 165 |
for (idx_t j = 0; j < stencil_width(); ++j) { |
| 166 |
const auto& wi = weights.weights_i[j]; |
| 167 |
for (idx_t i = 0; i < stencil_width(); ++i) { |
| 168 |
const idx_t n = src_.index(stencil.i(i, j), stencil.j(j)); |
| 169 |
const Value wij = wi[i] * wj[j]; |
| 170 |
for (idx_t k = 0; k < stencil_width(); ++k) { |
| 171 |
const Value w = wij * wk[k]; |
| 172 |
const idx_t kk = stencil.k(k); |
| 173 |
_input_ = &(input(n, kk, 0)); // Assumption that input.stride(2) == 1 |
| 174 |
for (idx_t v = 0; v < nvar; ++v) { |
| 175 |
output[v] += w * _input_[v]; |
| 176 |
} |
| 177 |
} |
| 178 |
index[j][i] = n; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (limiter_) { |
| 183 |
Limiter::limit_vars(index, stencil, input, output, nvar); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 188 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 1), void>::type interpolate( |
| 189 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, |
| 190 |
idx_t r) const { |
| 191 |
output(r) = interpolate(stencil, weights, input); |
| 192 |
} |
| 193 |
|
| 194 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 195 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 2), void>::type interpolate( |
| 196 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r, |
| 197 |
idx_t k) const { |
| 198 |
output(r, k) = interpolate(stencil, weights, input); |
| 199 |
} |
| 200 |
|
| 201 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 202 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 3), void>::type interpolate( |
| 203 |
const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r, |
| 204 |
idx_t k) const { |
| 205 |
auto output_vars = make_outputview(&output(r, k, 0)); |
| 206 |
interpolate_vars(stencil, weights, input, output_vars, output.shape(2)); |
| 207 |
} |
| 208 |
|
| 209 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 210 |
typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 3), void>::type interpolate( |
| 211 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 212 |
ATLAS_NOTIMPLEMENTED; |
| 213 |
} |
| 214 |
|
| 215 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 216 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate( |
| 217 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/) const { |
| 218 |
ATLAS_NOTIMPLEMENTED; |
| 219 |
} |
| 220 |
|
| 221 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 222 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate( |
| 223 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 224 |
ATLAS_NOTIMPLEMENTED; |
| 225 |
} |
| 226 |
|
| 227 |
template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray> |
| 228 |
typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 2), void>::type interpolate( |
| 229 |
const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const { |
| 230 |
ATLAS_NOTIMPLEMENTED; |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
} // namespace method |
| 235 |
} // namespace interpolation |
| 236 |
} // namespace atlas |