Debian Codegraph @

src/atlas/interpolation/method/structured/kernels/CubicHorizontalKernel.h

raw
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 "CubicHorizontalLimiter.h"
17
18 #include "eckit/linalg/Triplet.h"
19
20 #include "atlas/array/ArrayView.h"
21 #include "atlas/functionspace/StructuredColumns.h"
22 #include "atlas/grid/Stencil.h"
23 #include "atlas/grid/StencilComputer.h"
24 #include "atlas/runtime/Exception.h"
25 #include "atlas/util/CoordinateEnums.h"
26 #include "atlas/util/Point.h"
27
28 namespace atlas {
29 namespace interpolation {
30 namespace method {
31
32 class CubicHorizontalKernel {
33 using Limiter = CubicHorizontalLimiter;
34 using Triplet = eckit::linalg::Triplet;
35 using Triplets = std::vector<Triplet>;
36
37 public:
38 CubicHorizontalKernel() = default;
39
40 CubicHorizontalKernel(const functionspace::StructuredColumns& fs, const util::Config& config = util::NoConfig()) {
41 src_ = fs;
42 ATLAS_ASSERT(src_);
43 ATLAS_ASSERT(src_.halo() >= 2);
44 compute_horizontal_stencil_ = grid::ComputeHorizontalStencil(src_.grid(), stencil_width());
45 limiter_ = config.getBool("limiter", false);
46 }
47
48 protected:
49 functionspace::StructuredColumns src_;
50 grid::ComputeHorizontalStencil compute_horizontal_stencil_;
51 bool limiter_{false};
52
53 public:
54 static std::string className() { return "CubicHorizontalKernel"; }
55 static constexpr idx_t stencil_width() { return 4; }
56 static constexpr idx_t stencil_size() { return stencil_width() * stencil_width(); }
57 static constexpr idx_t stencil_halo() {
58 return static_cast<idx_t>(static_cast<double>(stencil_width()) / 2. + 0.5);
59 }
60
61 public:
62 using Stencil = grid::HorizontalStencil<4>;
63 struct Weights {
64 std::array<std::array<double, 4>, 4> weights_i;
65 std::array<double, 4> weights_j;
66 };
67
68 public:
69 struct WorkSpace {
70 Stencil stencil;
71 Weights weights;
72 };
73
74 template <typename stencil_t>
75 void compute_stencil(const double x, const double y, stencil_t& stencil) const {
76 compute_horizontal_stencil_(x, y, stencil);
77 }
78
79 template <typename stencil_t>
80 void make_valid_stencil(double& x, const double y, stencil_t& stencil, bool retry = true) const {
81 for (idx_t j = 0; j < stencil_width(); ++j) {
82 idx_t imin = stencil.i(0, j);
83 idx_t imax = stencil.i(stencil_width()-1, j);
84 if (imin < src_.i_begin_halo(stencil.j(j))) {
85 if (retry ) {
86 x += 360.;
87 compute_stencil(x, y, stencil);
88 return make_valid_stencil(x, y, stencil, false);
89 }
90 else {
91 Log::error() << "Stencil out of bounds" << std::endl;
92 ATLAS_THROW_EXCEPTION("stencil out of bounds");
93 }
94 }
95 if (imax >= src_.i_end_halo(stencil.j(j))) {
96 if (retry ) {
97 x -= 360.;
98 compute_stencil(x, y, stencil);
99 return make_valid_stencil(x, y, stencil, false);
100 }
101 else {
102 Log::error() << "Stencil out of bounds" << std::endl;
103 ATLAS_THROW_EXCEPTION("Stencil out of bounds");
104 }
105 }
106 }
107
108 }
109
110 template <typename weights_t>
111 void compute_weights(const double x, const double y, weights_t& weights) const {
112 Stencil stencil;
113 compute_stencil(x, y, stencil);
114 compute_weights(x, y, stencil, weights);
115 }
116
117
118 template <typename stencil_t, typename weights_t>
119 void compute_weights(const double x, const double y, const stencil_t& stencil, weights_t& weights) const {
120 PointXY P1, P2;
121 std::array<double, 4> yvec;
122 for (idx_t j = 0; j < stencil_width(); ++j) {
123 auto& weights_i = weights.weights_i[j];
124 src_.compute_xy(stencil.i(1, j), stencil.j(j), P1);
125 src_.compute_xy(stencil.i(2, j), stencil.j(j), P2);
126 const double alpha = (P2.x() - x) / (P2.x() - P1.x());
127 const double alpha_sqr = alpha * alpha;
128 const double two_minus_alpha = 2. - alpha;
129 const double one_minus_alpha_sqr = 1. - alpha_sqr;
130 weights_i[0] = -alpha * one_minus_alpha_sqr / 6.;
131 weights_i[1] = 0.5 * alpha * (1. + alpha) * two_minus_alpha;
132 weights_i[2] = 0.5 * one_minus_alpha_sqr * two_minus_alpha;
133 weights_i[3] = 1. - weights_i[0] - weights_i[1] - weights_i[2];
134 yvec[j] = P1.y();
135 }
136 const double dl12 = yvec[0] - yvec[1];
137 const double dl13 = yvec[0] - yvec[2];
138 const double dl14 = yvec[0] - yvec[3];
139 const double dl23 = yvec[1] - yvec[2];
140 const double dl24 = yvec[1] - yvec[3];
141 const double dl34 = yvec[2] - yvec[3];
142 const double dcl1 = dl12 * dl13 * dl14;
143 const double dcl2 = -dl12 * dl23 * dl24;
144 const double dcl3 = dl13 * dl23 * dl34;
145
146 const double dl1 = y - yvec[0];
147 const double dl2 = y - yvec[1];
148 const double dl3 = y - yvec[2];
149 const double dl4 = y - yvec[3];
150
151 auto& weights_j = weights.weights_j;
152 weights_j[0] = (dl2 * dl3 * dl4) / dcl1;
153 #if defined(_CRAYC) || defined(__NVCOMPILER) && ATLAS_BUILD_TYPE_RELEASE
154 // prevents FE_INVALID somehow (tested with Cray 8.7, nvhpc-22.11)
155 ATLAS_ASSERT(!std::isnan(weights_j[0]));
156 #endif
157 weights_j[1] = (dl1 * dl3 * dl4) / dcl2;
158 weights_j[2] = (dl1 * dl2 * dl4) / dcl3;
159 weights_j[3] = 1. - weights_j[0] - weights_j[1] - weights_j[2];
160 }
161
162 template <typename stencil_t, typename weights_t, typename array_t>
163 typename array_t::value_type interpolate(const stencil_t& stencil, const weights_t& weights,
164 const array_t& input) const {
165 using Value = typename array_t::value_type;
166
167 std::array<std::array<idx_t, stencil_width()>, stencil_width()> index;
168 const auto& weights_j = weights.weights_j;
169 Value output = 0.;
170 for (idx_t j = 0; j < stencil_width(); ++j) {
171 const auto& weights_i = weights.weights_i[j];
172 for (idx_t i = 0; i < stencil_width(); ++i) {
173 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
174 Value w = weights_i[i] * weights_j[j];
175 output += w * input[n];
176 index[j][i] = n;
177 }
178 }
179
180 if (limiter_) {
181 Limiter::limit(output, index, input);
182 }
183 return output;
184 }
185
186
187 template <typename stencil_t, typename weights_t, typename Value, int Rank>
188 typename std::enable_if<(Rank == 1), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
189 const array::ArrayView<const Value, Rank>& input,
190 array::ArrayView<Value, Rank>& output, idx_t r) const {
191 std::array<std::array<idx_t, stencil_width()>, stencil_width()> index;
192 const auto& weights_j = weights.weights_j;
193 output(r) = 0.;
194 for (idx_t j = 0; j < stencil_width(); ++j) {
195 const auto& weights_i = weights.weights_i[j];
196 for (idx_t i = 0; i < stencil_width(); ++i) {
197 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
198 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
199 output(r) += w * input[n];
200 index[j][i] = n;
201 }
202 }
203
204 if (limiter_) {
205 Limiter::limit(index, input, output, r);
206 }
207 }
208
209
210 template <typename stencil_t, typename weights_t, typename Value, int Rank>
211 typename std::enable_if<(Rank == 2), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
212 const array::ArrayView<const Value, Rank>& input,
213 array::ArrayView<Value, Rank>& output, idx_t r) const {
214 std::array<std::array<idx_t, stencil_width()>, stencil_width()> index;
215 const auto& weights_j = weights.weights_j;
216 const idx_t Nk = output.shape(1);
217 for (idx_t k = 0; k < Nk; ++k) {
218 output(r, k) = 0.;
219 }
220 for (idx_t j = 0; j < stencil_width(); ++j) {
221 const auto& weights_i = weights.weights_i[j];
222 for (idx_t i = 0; i < stencil_width(); ++i) {
223 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
224 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
225 for (idx_t k = 0; k < Nk; ++k) {
226 output(r, k) += w * input(n, k);
227 }
228 index[j][i] = n;
229 }
230 }
231
232 if (limiter_) {
233 Limiter::limit(index, input, output, r);
234 }
235 }
236
237 template <typename stencil_t, typename weights_t, typename Value, int Rank>
238 typename std::enable_if<(Rank == 3), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
239 const array::ArrayView<const Value, Rank>& input,
240 array::ArrayView<Value, Rank>& output, idx_t r) const {
241 std::array<std::array<idx_t, stencil_width()>, stencil_width()> index;
242 const auto& weights_j = weights.weights_j;
243 const idx_t Nk = output.shape(1);
244 const idx_t Nl = output.shape(2);
245
246 for (idx_t k = 0; k < Nk; ++k) {
247 for (idx_t l = 0; l < Nl; ++l) {
248 output(r, k, l) = 0.;
249 }
250 }
251 for (idx_t j = 0; j < stencil_width(); ++j) {
252 const auto& weights_i = weights.weights_i[j];
253 for (idx_t i = 0; i < stencil_width(); ++i) {
254 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
255 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
256 for (idx_t k = 0; k < Nk; ++k) {
257 for (idx_t l = 0; l < Nl; ++l) {
258 output(r, k, l) += w * input(n, k, l);
259 }
260 }
261 index[j][i] = n;
262 }
263 }
264
265 if (limiter_) {
266 Limiter::limit(index, input, output, r);
267 }
268 }
269
270 template <typename array_t>
271 typename array_t::value_type operator()(double x, double y, const array_t& input) const {
272 Stencil stencil;
273 compute_stencil(x, y, stencil);
274 Weights weights;
275 compute_weights(x, y, stencil, weights);
276 make_valid_stencil(x, y, stencil);
277 return interpolate(stencil, weights, input);
278 }
279
280 template <typename array_t>
281 typename array_t::value_type interpolate(PointXY p, const array_t& input, WorkSpace& ws) const {
282 compute_stencil(p.x(), p.y(), ws.stencil);
283 compute_weights(p.x(), p.y(), ws.stencil, ws.weights);
284 make_valid_stencil(p.x(), p.y(), ws.stencil);
285 return interpolate(ws.stencil, ws.weights, input);
286 }
287
288 // Thread private workspace
289 Triplets compute_triplets(const idx_t row, const double x, const double y, WorkSpace& ws) const {
290 Triplets triplets;
291 triplets.reserve(stencil_size());
292 insert_triplets(row, x, y, triplets, ws);
293 return triplets;
294 }
295
296 Triplets reserve_triplets(size_t N) {
297 Triplets triplets;
298 triplets.reserve(N * stencil_size());
299 return triplets;
300 }
301
302 Triplets allocate_triplets(size_t N) { return Triplets(N * stencil_size()); }
303
304 void insert_triplets(const idx_t row, const PointXY& p, Triplets& triplets, WorkSpace& ws) const {
305 insert_triplets(row, p.x(), p.y(), triplets, ws);
306 }
307
308 void insert_triplets(const idx_t row, double x, double y, Triplets& triplets, WorkSpace& ws) const {
309 compute_stencil(x, y, ws.stencil);
310 compute_weights(x, y, ws.stencil, ws.weights);
311
312 make_valid_stencil(x, y, ws.stencil);
313 const auto& wj = ws.weights.weights_j;
314
315 idx_t pos = row * stencil_size();
316 for (idx_t j = 0; j < stencil_width(); ++j) {
317 const auto& wi = ws.weights.weights_i[j];
318 for (idx_t i = 0; i < stencil_width(); ++i) {
319 idx_t col = src_.index(ws.stencil.i(i, j), ws.stencil.j(j));
320 double w = wi[i] * wj[j];
321 triplets[pos++] = Triplet(row, col, w);
322 }
323 }
324 }
325 };
326
327 } // namespace method
328 } // namespace interpolation
329 } // namespace atlas