Debian Codegraph @

src/atlas/interpolation/method/structured/kernels/LinearHorizontalKernel.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 "eckit/linalg/Triplet.h"
17
18 #include "atlas/array/ArrayView.h"
19 #include "atlas/functionspace/StructuredColumns.h"
20 #include "atlas/grid/Stencil.h"
21 #include "atlas/grid/StencilComputer.h"
22 #include "atlas/runtime/Exception.h"
23 #include "atlas/util/CoordinateEnums.h"
24 #include "atlas/util/Point.h"
25
26 namespace atlas {
27 namespace interpolation {
28 namespace method {
29
30 class LinearHorizontalKernel {
31 using Triplet = eckit::linalg::Triplet;
32 using Triplets = std::vector<Triplet>;
33
34 public:
35 LinearHorizontalKernel() = default;
36
37 LinearHorizontalKernel(const functionspace::StructuredColumns& fs, const util::Config& = util::NoConfig()) {
38 src_ = fs;
39 ATLAS_ASSERT(src_);
40 compute_horizontal_stencil_ = grid::ComputeHorizontalStencil(src_.grid(), stencil_width());
41 }
42
43 private:
44 functionspace::StructuredColumns src_;
45 grid::ComputeHorizontalStencil compute_horizontal_stencil_;
46
47 public:
48 static std::string className() { return "LinearHorizontalKernel"; }
49 static constexpr idx_t stencil_width() { return 2; }
50 static constexpr idx_t stencil_size() { return stencil_width() * stencil_width(); }
51 static constexpr idx_t stencil_halo() { return 0; }
52
53 public:
54 using Stencil = grid::HorizontalStencil<2>;
55 struct Weights {
56 std::array<std::array<double, 2>, 2> weights_i;
57 std::array<double, 2> weights_j;
58 };
59
60 public:
61 struct WorkSpace {
62 Stencil stencil;
63 Weights weights;
64 };
65
66 template <typename stencil_t>
67 void compute_stencil(const double x, const double y, stencil_t& stencil) const {
68 compute_horizontal_stencil_(x, y, stencil);
69 }
70
71 template <typename stencil_t>
72 void make_valid_stencil(double& x, const double y, stencil_t& stencil, bool retry = true) const {
73 for (idx_t j = 0; j < stencil_width(); ++j) {
74 idx_t imin = stencil.i(0, j);
75 idx_t imax = stencil.i(stencil_width()-1, j);
76 if (imin < src_.i_begin_halo(stencil.j(j))) {
77 if (retry ) {
78 x += 360.;
79 compute_stencil(x, y, stencil);
80 return make_valid_stencil(x, y, stencil, false);
81 }
82 else {
83 Log::error() << "Stencil out of bounds" << std::endl;
84 ATLAS_THROW_EXCEPTION("stencil out of bounds");
85 }
86 }
87 if (imax >= src_.i_end_halo(stencil.j(j))) {
88 if (retry ) {
89 x -= 360.;
90 compute_stencil(x, y, stencil);
91 return make_valid_stencil(x, y, stencil, false);
92 }
93 else {
94 Log::error() << "Stencil out of bounds" << std::endl;
95 ATLAS_THROW_EXCEPTION("Stencil out of bounds");
96 }
97 }
98 }
99 }
100
101
102 template <typename weights_t>
103 void compute_weights(const double x, const double y, weights_t& weights) const {
104 Stencil stencil;
105 compute_stencil(x, y, stencil);
106 compute_weights(x, y, stencil, weights);
107 }
108
109
110 template <typename stencil_t, typename weights_t>
111 void compute_weights(const double x, const double y, const stencil_t& stencil, weights_t& weights) const {
112 PointXY P1, P2;
113 std::array<double, stencil_width()> yvec;
114 // Compute weights for each constant-Y
115 for (idx_t j = 0; j < stencil_width(); ++j) {
116 auto& weights_i = weights.weights_i[j];
117 src_.compute_xy(stencil.i(0, j), stencil.j(j), P1);
118 src_.compute_xy(stencil.i(1, j), stencil.j(j), P2);
119 const double alpha = (P2.x() - x) / (P2.x() - P1.x());
120 weights_i[0] = alpha;
121 weights_i[1] = 1. - alpha;
122 yvec[j] = P1.y();
123 }
124 // Compute weights in y-direction
125 {
126 auto& weights_j = weights.weights_j;
127 const double alpha = (yvec[1] - y) / (yvec[1] - yvec[0]);
128 weights_j[0] = alpha;
129 weights_j[1] = 1. - alpha;
130 }
131 }
132
133 template <typename stencil_t, typename weights_t, typename array_t>
134 typename array_t::value_type interpolate(const stencil_t& stencil, const weights_t& weights,
135 const array_t& input) const {
136 using Value = typename array_t::value_type;
137
138 const auto& weights_j = weights.weights_j;
139 Value output = 0.;
140 for (idx_t j = 0; j < stencil_width(); ++j) {
141 const auto& weights_i = weights.weights_i[j];
142 for (idx_t i = 0; i < stencil_width(); ++i) {
143 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
144 Value w = weights_i[i] * weights_j[j];
145 output += w * input[n];
146 }
147 }
148
149 return output;
150 }
151
152 template <typename stencil_t, typename weights_t, typename Value, int Rank>
153 typename std::enable_if<(Rank == 1), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
154 const array::ArrayView<const Value, Rank>& input,
155 array::ArrayView<Value, Rank>& output, idx_t r) const {
156 const auto& weights_j = weights.weights_j;
157 output(r) = 0.;
158 for (idx_t j = 0; j < stencil_width(); ++j) {
159 const auto& weights_i = weights.weights_i[j];
160 for (idx_t i = 0; i < stencil_width(); ++i) {
161 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
162 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
163 output(r) += w * input[n];
164 }
165 }
166 }
167
168 template <typename stencil_t, typename weights_t, typename Value, int Rank>
169 typename std::enable_if<(Rank == 2), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
170 const array::ArrayView<const Value, Rank>& input,
171 array::ArrayView<Value, Rank>& output, idx_t r) const {
172 const auto& weights_j = weights.weights_j;
173 const idx_t Nk = output.shape(1);
174 for (idx_t k = 0; k < Nk; ++k) {
175 output(r, k) = 0.;
176 }
177 for (idx_t j = 0; j < stencil_width(); ++j) {
178 const auto& weights_i = weights.weights_i[j];
179 for (idx_t i = 0; i < stencil_width(); ++i) {
180 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
181 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
182 for (idx_t k = 0; k < Nk; ++k) {
183 output(r, k) += w * input(n, k);
184 }
185 }
186 }
187 }
188
189 template <typename stencil_t, typename weights_t, typename Value, int Rank>
190 typename std::enable_if<(Rank == 3), void>::type interpolate(const stencil_t& stencil, const weights_t& weights,
191 const array::ArrayView<const Value, Rank>& input,
192 array::ArrayView<Value, Rank>& output, idx_t r) const {
193 const auto& weights_j = weights.weights_j;
194 const idx_t Nk = output.shape(1);
195 const idx_t Nl = output.shape(2);
196 for (idx_t k = 0; k < Nk; ++k) {
197 for (idx_t l = 0; l < Nl; ++l) {
198 output(r, k, l) = 0.;
199 }
200 }
201 for (idx_t j = 0; j < stencil_width(); ++j) {
202 const auto& weights_i = weights.weights_i[j];
203 for (idx_t i = 0; i < stencil_width(); ++i) {
204 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
205 Value w = static_cast<Value>(weights_i[i] * weights_j[j]);
206 for (idx_t k = 0; k < Nk; ++k) {
207 for (idx_t l = 0; l < Nl; ++l) {
208 output(r, k, l) += w * input(n, k, l);
209 }
210 }
211 }
212 }
213 }
214
215 template <typename array_t>
216 typename array_t::value_type operator()(double x, double y, const array_t& input) const {
217 Stencil stencil;
218 Weights weights;
219 compute_stencil(x, y, stencil);
220 compute_weights(x, y, stencil, weights);
221 make_valid_stencil(x, y, stencil);
222 return interpolate(stencil, weights, input);
223 }
224
225 template <typename array_t>
226 typename array_t::value_type interpolate(PointXY p, const array_t& input, WorkSpace& ws) const {
227 compute_stencil(p.x(), p.y(), ws.stencil);
228 compute_weights(p.x(), p.y(), ws.stencil, ws.weights);
229 make_valid_stencil(p.x(), p.y(), ws.stencil);
230 return interpolate(ws.stencil, ws.weights, input);
231 }
232
233 // Thread private workspace
234 Triplets compute_triplets(const idx_t row, const double x, const double y, WorkSpace& ws) const {
235 Triplets triplets;
236 triplets.reserve(stencil_size());
237 insert_triplets(row, x, y, triplets, ws);
238 return triplets;
239 }
240
241 Triplets reserve_triplets(size_t N) {
242 Triplets triplets;
243 triplets.reserve(N * stencil_size());
244 return triplets;
245 }
246
247 Triplets allocate_triplets(size_t N) { return Triplets(N * stencil_size()); }
248
249 void insert_triplets(const idx_t row, const PointXY& p, Triplets& triplets, WorkSpace& ws) const {
250 insert_triplets(row, p.x(), p.y(), triplets, ws);
251 }
252
253 void insert_triplets(const idx_t row, double x, double y, Triplets& triplets, WorkSpace& ws) const {
254 compute_stencil(x, y, ws.stencil);
255 compute_weights(x, y, ws.stencil, ws.weights);
256
257 make_valid_stencil(x, y, ws.stencil);
258 const auto& wj = ws.weights.weights_j;
259
260 idx_t pos = row * stencil_size();
261 for (idx_t j = 0; j < stencil_width(); ++j) {
262 const auto& wi = ws.weights.weights_i[j];
263 for (idx_t i = 0; i < stencil_width(); ++i) {
264 idx_t col = src_.index(ws.stencil.i(i, j), ws.stencil.j(j));
265 double w = wi[i] * wj[j];
266 triplets[pos++] = Triplet(row, col, w);
267 }
268 }
269 }
270 };
271
272 } // namespace method
273 } // namespace interpolation
274 } // namespace atlas