Debian Codegraph @

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