| 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 |
class CubicHorizontalLimiter { |
| 22 |
public: |
| 23 |
CubicHorizontalLimiter() = default; |
| 24 |
|
| 25 |
template <typename array_t> |
| 26 |
static void limit(typename array_t::value_type& output, const std::array<std::array<idx_t, 4>, 4>& index, |
| 27 |
const array_t& input) { |
| 28 |
using Scalar = typename array_t::value_type; |
| 29 |
// Limit output to max/min of values in stencil marked by '*' |
| 30 |
// x x x x |
| 31 |
// x *-----* x |
| 32 |
// / P | |
| 33 |
// x *------ * x |
| 34 |
// x x x x |
| 35 |
Scalar maxval = std::numeric_limits<Scalar>::lowest(); |
| 36 |
Scalar minval = std::numeric_limits<Scalar>::max(); |
| 37 |
for (idx_t j = 1; j < 3; ++j) { |
| 38 |
for (idx_t i = 1; i < 3; ++i) { |
| 39 |
idx_t n = index[j][i]; |
| 40 |
Scalar val = input[n]; |
| 41 |
maxval = std::max(maxval, val); |
| 42 |
minval = std::min(minval, val); |
| 43 |
} |
| 44 |
} |
| 45 |
if (output < minval) { |
| 46 |
output = minval; |
| 47 |
} |
| 48 |
else if (output > maxval) { |
| 49 |
output = maxval; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
template <typename Value, int Rank> |
| 54 |
static typename std::enable_if<(Rank == 1), void>::type limit(const std::array<std::array<idx_t, 4>, 4>& index, |
| 55 |
const array::ArrayView<const Value, Rank>& input, |
| 56 |
array::ArrayView<Value, Rank>& output, idx_t r) { |
| 57 |
// Limit output to max/min of values in stencil marked by '*' |
| 58 |
// x x x x |
| 59 |
// x *-----* x |
| 60 |
// / P | |
| 61 |
// x *------ * x |
| 62 |
// x x x x |
| 63 |
Value maxval = std::numeric_limits<Value>::lowest(); |
| 64 |
Value minval = std::numeric_limits<Value>::max(); |
| 65 |
for (idx_t j = 1; j < 3; ++j) { |
| 66 |
for (idx_t i = 1; i < 3; ++i) { |
| 67 |
idx_t n = index[j][i]; |
| 68 |
Value val = input[n]; |
| 69 |
maxval = std::max(maxval, val); |
| 70 |
minval = std::min(minval, val); |
| 71 |
} |
| 72 |
} |
| 73 |
if (output(r) < minval) { |
| 74 |
output(r) = minval; |
| 75 |
} |
| 76 |
else if (output(r) > maxval) { |
| 77 |
output(r) = maxval; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
template <typename Value, int Rank> |
| 83 |
static typename std::enable_if<(Rank == 2), void>::type limit(const std::array<std::array<idx_t, 4>, 4>& index, |
| 84 |
const array::ArrayView<const Value, Rank>& input, |
| 85 |
array::ArrayView<Value, Rank>& output, idx_t r) { |
| 86 |
// Limit output to max/min of values in stencil marked by '*' |
| 87 |
// x x x x |
| 88 |
// x *-----* x |
| 89 |
// / P | |
| 90 |
// x *------ * x |
| 91 |
// x x x x |
| 92 |
for (idx_t k = 0; k < output.shape(1); ++k) { |
| 93 |
Value maxval = std::numeric_limits<Value>::lowest(); |
| 94 |
Value minval = std::numeric_limits<Value>::max(); |
| 95 |
for (idx_t j = 1; j < 3; ++j) { |
| 96 |
for (idx_t i = 1; i < 3; ++i) { |
| 97 |
idx_t n = index[j][i]; |
| 98 |
Value val = input(n, k); |
| 99 |
maxval = std::max(maxval, val); |
| 100 |
minval = std::min(minval, val); |
| 101 |
} |
| 102 |
} |
| 103 |
if (output(r, k) < minval) { |
| 104 |
output(r, k) = minval; |
| 105 |
} |
| 106 |
else if (output(r, k) > maxval) { |
| 107 |
output(r, k) = maxval; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
template <typename Value, int Rank> |
| 113 |
static typename std::enable_if<(Rank == 3), void>::type limit(const std::array<std::array<idx_t, 4>, 4>& index, |
| 114 |
const array::ArrayView<const Value, Rank>& input, |
| 115 |
array::ArrayView<Value, Rank>& output, idx_t r) { |
| 116 |
// Limit output to max/min of values in stencil marked by '*' |
| 117 |
// x x x x |
| 118 |
// x *-----* x |
| 119 |
// / P | |
| 120 |
// x *------ * x |
| 121 |
// x x x x |
| 122 |
for (idx_t k = 0; k < output.shape(1); ++k) { |
| 123 |
for (idx_t l = 0; l < output.shape(2); ++l) { |
| 124 |
Value maxval = std::numeric_limits<Value>::lowest(); |
| 125 |
Value minval = std::numeric_limits<Value>::max(); |
| 126 |
for (idx_t j = 1; j < 3; ++j) { |
| 127 |
for (idx_t i = 1; i < 3; ++i) { |
| 128 |
idx_t n = index[j][i]; |
| 129 |
Value val = input(n, k, l); |
| 130 |
maxval = std::max(maxval, val); |
| 131 |
minval = std::min(minval, val); |
| 132 |
} |
| 133 |
} |
| 134 |
if (output(r, k, l) < minval) { |
| 135 |
output(r, k, l) = minval; |
| 136 |
} |
| 137 |
else if (output(r, k, l) > maxval) { |
| 138 |
output(r, k, l) = maxval; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
}; |
| 145 |
|
| 146 |
} // namespace method |
| 147 |
} // namespace interpolation |
| 148 |
} // namespace atlas |