| 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 <array> |
| 14 |
#include <cmath> |
| 15 |
#include <limits> |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace interpolation { |
| 19 |
namespace method { |
| 20 |
|
| 21 |
|
| 22 |
class Cubic3DLimiter { |
| 23 |
public: |
| 24 |
Cubic3DLimiter() = default; |
| 25 |
|
| 26 |
template <typename array_t, typename stencil_t> |
| 27 |
static typename std::enable_if<(array_t::RANK == 2), void>::type limit_scalar( |
| 28 |
typename std::remove_const<typename array_t::value_type>::type& output, |
| 29 |
const std::array<std::array<idx_t, 4>, 4>& index, const stencil_t& stencil, const array_t& input) { |
| 30 |
using Scalar = typename std::remove_const<typename array_t::value_type>::type; |
| 31 |
// Limit output to max/min of values in stencil marked by '*' |
| 32 |
// x x x x |
| 33 |
// x *-----* x |
| 34 |
// / P | |
| 35 |
// x *------ * x |
| 36 |
// x x x x |
| 37 |
idx_t k = stencil.k_interval(); |
| 38 |
idx_t k1, k2; |
| 39 |
if (k < 1) { |
| 40 |
k1 = stencil.k(0); |
| 41 |
k2 = stencil.k(1); |
| 42 |
} |
| 43 |
else if (k > 1) { |
| 44 |
k1 = stencil.k(2); |
| 45 |
k2 = stencil.k(3); |
| 46 |
} |
| 47 |
else { |
| 48 |
k1 = stencil.k(1); |
| 49 |
k2 = stencil.k(2); |
| 50 |
} |
| 51 |
|
| 52 |
Scalar maxval = std::numeric_limits<Scalar>::lowest(); |
| 53 |
Scalar minval = std::numeric_limits<Scalar>::max(); |
| 54 |
for (idx_t j = 1; j < 3; ++j) { |
| 55 |
for (idx_t i = 1; i < 3; ++i) { |
| 56 |
idx_t n = index[j][i]; |
| 57 |
|
| 58 |
Scalar f1 = input(n, k1); |
| 59 |
Scalar f2 = input(n, k2); |
| 60 |
|
| 61 |
maxval = std::max(maxval, f1); |
| 62 |
maxval = std::max(maxval, f2); |
| 63 |
minval = std::min(minval, f1); |
| 64 |
minval = std::min(minval, f2); |
| 65 |
} |
| 66 |
} |
| 67 |
if (output < minval) { |
| 68 |
output = minval; |
| 69 |
} |
| 70 |
else if (output > maxval) { |
| 71 |
output = maxval; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
template <typename InputArray, typename OutputArray, typename stencil_t> |
| 76 |
static typename std::enable_if<(InputArray::RANK == 3), void>::type limit_vars( |
| 77 |
const std::array<std::array<idx_t, 4>, 4>& index, const stencil_t& stencil, const InputArray& input, |
| 78 |
OutputArray& output, const idx_t nvar) { |
| 79 |
// Limit output to max/min of values in stencil marked by '*' |
| 80 |
// x x x x |
| 81 |
// x *-----* x |
| 82 |
// / P | |
| 83 |
// x *------ * x |
| 84 |
// x x x x |
| 85 |
|
| 86 |
using Value = typename OutputArray::value_type; |
| 87 |
|
| 88 |
const idx_t k = stencil.k_interval(); |
| 89 |
idx_t k1, k2; |
| 90 |
if (k < 1) { |
| 91 |
k1 = stencil.k(0); |
| 92 |
k2 = stencil.k(1); |
| 93 |
} |
| 94 |
else if (k > 1) { |
| 95 |
k1 = stencil.k(2); |
| 96 |
k2 = stencil.k(3); |
| 97 |
} |
| 98 |
else { |
| 99 |
k1 = stencil.k(1); |
| 100 |
k2 = stencil.k(2); |
| 101 |
} |
| 102 |
|
| 103 |
for (idx_t v = 0; v < nvar; ++v) { |
| 104 |
Value limited = output[v]; |
| 105 |
Value maxval = std::numeric_limits<Value>::lowest(); |
| 106 |
Value minval = std::numeric_limits<Value>::max(); |
| 107 |
for (idx_t j = 1; j < 3; ++j) { |
| 108 |
for (idx_t i = 1; i < 3; ++i) { |
| 109 |
idx_t n = index[j][i]; |
| 110 |
|
| 111 |
Value f1 = input(n, k1, v); |
| 112 |
Value f2 = input(n, k2, v); |
| 113 |
|
| 114 |
maxval = std::max(maxval, f1); |
| 115 |
maxval = std::max(maxval, f2); |
| 116 |
minval = std::min(minval, f1); |
| 117 |
minval = std::min(minval, f2); |
| 118 |
} |
| 119 |
} |
| 120 |
if (limited < minval) { |
| 121 |
limited = minval; |
| 122 |
} |
| 123 |
else if (limited > maxval) { |
| 124 |
limited = maxval; |
| 125 |
} |
| 126 |
output[v] = limited; |
| 127 |
} |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace method |
| 132 |
} // namespace interpolation |
| 133 |
} // namespace atlas |