Debian Codegraph @

src/atlas/interpolation/method/structured/kernels/Linear3DKernel.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 "atlas/array/ArrayView.h"
17 #include "atlas/functionspace/StructuredColumns.h"
18 #include "atlas/grid/Stencil.h"
19 #include "atlas/grid/StencilComputer.h"
20 #include "atlas/runtime/Exception.h"
21 #include "atlas/util/CoordinateEnums.h"
22 #include "atlas/util/Point.h"
23
24 #include "LinearHorizontalKernel.h"
25 #include "LinearVerticalKernel.h"
26
27 namespace atlas {
28 namespace interpolation {
29 namespace method {
30
31 class Linear3DKernel {
32 public:
33 Linear3DKernel(const functionspace::StructuredColumns& fs, const util::Config& config = util::NoConfig()) {
34 src_ = fs;
35 ATLAS_ASSERT(src_);
36 ATLAS_ASSERT(src_.halo() >= 0);
37 ATLAS_ASSERT(src_.vertical().size());
38 horizontal_interpolation_ = LinearHorizontalKernel(src_, config);
39 vertical_interpolation_ = LinearVerticalKernel(fs.vertical(), config);
40 }
41
42 private:
43 functionspace::StructuredColumns src_;
44 LinearHorizontalKernel horizontal_interpolation_;
45 LinearVerticalKernel vertical_interpolation_;
46
47 public:
48 static std::string className() { return "Linear3DKernel"; }
49 static constexpr idx_t stencil_width() { return 2; }
50 static constexpr idx_t stencil_size() { return stencil_width() * stencil_width() * stencil_width(); }
51 static constexpr idx_t stencil_halo() { return 0; }
52
53 public:
54 using Stencil = grid::Stencil3D<2>;
55 struct Weights {
56 std::array<std::array<double, 2>, 2> weights_i;
57 std::array<double, 2> weights_j;
58 std::array<double, 2> weights_k;
59 };
60
61 public:
62 struct WorkSpace {
63 Stencil stencil;
64 Weights weights;
65 };
66
67 template <typename stencil_t>
68 void compute_stencil(double& x, const double y, const double z, stencil_t& stencil) const {
69 horizontal_interpolation_.compute_stencil(x, y, stencil);
70 vertical_interpolation_.compute_stencil(z, stencil);
71 }
72
73 template <typename weights_t>
74 void compute_weights(double x, double y, const double z, weights_t& weights) const {
75 Stencil stencil;
76 compute_stencil(x, y, z, stencil);
77 compute_weights(x, y, z, stencil, weights);
78 }
79
80
81 template <typename stencil_t, typename weights_t>
82 void compute_weights(const double x, const double y, const double z, const stencil_t& stencil,
83 weights_t& weights) const {
84 horizontal_interpolation_.compute_weights(x, y, stencil, weights);
85 vertical_interpolation_.compute_weights(z, stencil, weights);
86 }
87
88 template <typename stencil_t, typename weights_t, typename array_t>
89 typename std::enable_if<(array_t::RANK == 2), typename array_t::value_type>::type interpolate(
90 const stencil_t& stencil, const weights_t& weights, const array_t& input) const {
91 using Value = typename std::remove_const<typename array_t::value_type>::type;
92
93 std::array<std::array<idx_t, stencil_width()>, stencil_width()> index;
94 const auto& wj = weights.weights_j;
95 const auto& wk = weights.weights_k;
96
97 Value output = 0.;
98 for (idx_t j = 0; j < stencil_width(); ++j) {
99 const auto& wi = weights.weights_i[j];
100 for (idx_t i = 0; i < stencil_width(); ++i) {
101 idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
102 Value wij = wi[i] * wj[j];
103 for (idx_t k = 0; k < stencil_width(); ++k) {
104 Value w = wij * wk[k];
105 output += w * input(n, stencil.k(k));
106 }
107 index[j][i] = n;
108 }
109 }
110 return output;
111 }
112
113 template <typename Value>
114 struct OutputView1D {
115 template <typename Int>
116 Value& operator()(Int v) {
117 return data_[v];
118 }
119 template <typename Int>
120 Value& operator[](Int v) {
121 return data_[v];
122 }
123 static constexpr int RANK{1};
124 OutputView1D(Value* data): data_(data) {}
125 using value_type = Value;
126
127 Value* data_;
128 };
129
130 template <typename Value>
131 OutputView1D<Value> make_outputview(Value* data) const {
132 return OutputView1D<Value>(data);
133 }
134
135 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
136 typename std::enable_if<(InputArray::RANK == 3), void>::type interpolate_vars(const stencil_t& stencil,
137 const weights_t& weights,
138 const InputArray& input,
139 OutputArray& output,
140 const idx_t nvar) const {
141 using Value = typename InputArray::value_type;
142
143 const auto& wj = weights.weights_j;
144 const auto& wk = weights.weights_k;
145
146 const Value* _input_;
147
148 for (idx_t v = 0; v < nvar; ++v) {
149 output[v] = 0.;
150 }
151
152 for (idx_t j = 0; j < stencil_width(); ++j) {
153 const auto& wi = weights.weights_i[j];
154 for (idx_t i = 0; i < stencil_width(); ++i) {
155 const idx_t n = src_.index(stencil.i(i, j), stencil.j(j));
156 const Value wij = wi[i] * wj[j];
157 for (idx_t k = 0; k < stencil_width(); ++k) {
158 const Value w = wij * wk[k];
159 const idx_t kk = stencil.k(k);
160 _input_ = &(input(n, kk, 0)); // Assumption that input.stride(2) == 1
161 for (idx_t v = 0; v < nvar; ++v) {
162 output[v] += w * _input_[v];
163 }
164 }
165 }
166 }
167 }
168
169 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
170 typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 1), void>::type interpolate(
171 const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output,
172 idx_t r) const {
173 output(r) = interpolate(stencil, weights, input);
174 }
175
176 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
177 typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 2), void>::type interpolate(
178 const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r,
179 idx_t k) const {
180 output(r, k) = interpolate(stencil, weights, input);
181 }
182
183 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
184 typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 3), void>::type interpolate(
185 const stencil_t& stencil, const weights_t& weights, const InputArray& input, OutputArray& output, idx_t r,
186 idx_t k) const {
187 auto output_vars = make_outputview(&output(r, k, 0));
188 interpolate_vars(stencil, weights, input, output_vars, output.shape(2));
189 }
190
191 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
192 typename std::enable_if<(InputArray::RANK == 2 && OutputArray::RANK == 3), void>::type interpolate(
193 const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const {
194 ATLAS_NOTIMPLEMENTED;
195 }
196
197 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
198 typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate(
199 const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/) const {
200 ATLAS_NOTIMPLEMENTED;
201 }
202
203 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
204 typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 1), void>::type interpolate(
205 const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const {
206 ATLAS_NOTIMPLEMENTED;
207 }
208
209 template <typename stencil_t, typename weights_t, typename InputArray, typename OutputArray>
210 typename std::enable_if<(InputArray::RANK == 3 && OutputArray::RANK == 2), void>::type interpolate(
211 const stencil_t&, const weights_t&, const InputArray&, OutputArray&, idx_t /*r*/, idx_t /*k*/) const {
212 ATLAS_NOTIMPLEMENTED;
213 }
214 };
215
216 } // namespace method
217 } // namespace interpolation
218 } // namespace atlas