Debian Codegraph @

src/atlas/interpolation/method/structured/kernels/CubicVerticalKernel.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 #include <cmath>
13
14 #include "atlas/grid/Stencil.h"
15 #include "atlas/grid/StencilComputer.h"
16 #include "atlas/grid/Vertical.h"
17 #include "atlas/library/config.h"
18 #include "atlas/util/Point.h"
19
20 namespace atlas {
21 namespace interpolation {
22 namespace method {
23
24 class CubicVerticalKernel {
25 grid::ComputeVerticalStencil compute_vertical_stencil_;
26 Vertical vertical_;
27 static constexpr idx_t stencil_width() { return 4; }
28 static constexpr idx_t stencil_size() { return stencil_width() * stencil_width(); }
29 idx_t first_level_;
30 idx_t last_level_;
31 bool limiter_;
32
33 static constexpr bool match_IFS() { return true; }
34
35 public:
36 CubicVerticalKernel() = default;
37
38 CubicVerticalKernel(const Vertical& vertical, const eckit::Configuration& config = util::NoConfig()):
39 compute_vertical_stencil_(vertical, stencil_width()),
40 vertical_(vertical),
41 first_level_(vertical_.k_begin()),
42 last_level_(vertical_.k_end() - 1) {
43 limiter_ = config.getBool("limiter", false);
44 }
45 struct Weights {
46 std::array<double, 4> weights_k;
47 };
48 using Stencil = grid::VerticalStencil<4>;
49
50 template <typename stencil_t>
51 void compute_stencil(const double z, stencil_t& stencil) const {
52 compute_vertical_stencil_(z, stencil);
53 }
54
55 template <typename stencil_t, typename weights_t>
56 void compute_weights(const double z, const stencil_t& stencil, weights_t& weights) const {
57 auto& w = weights.weights_k;
58
59 std::array<double, 4> zvec;
60 for (idx_t k = 0; k < 4; ++k) {
61 zvec[k] = vertical_(stencil.k(k));
62 }
63
64 // auto quadratic_interpolation = [z]( const double zvec[], double w[] ) {
65 // double d01 = zvec[0] - zvec[1];
66 // double d02 = zvec[0] - zvec[2];
67 // double d12 = zvec[1] - zvec[2];
68 // double dc0 = d01 * d02;
69 // double dc1 = -d01 * d12;
70 // double d0 = z - zvec[0];
71 // double d1 = z - zvec[1];
72 // double d2 = z - zvec[2];
73 // w[0] = ( d1 * d2 ) / dc0;
74 // w[1] = ( d0 * d2 ) / dc1;
75 // w[2] = 1. - w[0] - w[1];
76 // };
77
78 auto cubic_interpolation = [z](const double zvec[], double w[]) {
79 double d01 = zvec[0] - zvec[1];
80 double d02 = zvec[0] - zvec[2];
81 double d03 = zvec[0] - zvec[3];
82 double d12 = zvec[1] - zvec[2];
83 double d13 = zvec[1] - zvec[3];
84 double d23 = zvec[2] - zvec[3];
85 double dc0 = d01 * d02 * d03;
86 double dc1 = -d01 * d12 * d13;
87 double dc2 = d02 * d12 * d23;
88
89 double d0 = z - zvec[0];
90 double d1 = z - zvec[1];
91 double d2 = z - zvec[2];
92 double d3 = z - zvec[3];
93
94 w[0] = (d1 * d2 * d3) / dc0;
95 #if defined(_CRAYC) && ATLAS_BUILD_TYPE_RELEASE
96 // prevents FE_INVALID somehow (tested with Cray 8.7)
97 ATLAS_ASSERT(!std::isnan(w[0]));
98 #endif
99 w[1] = (d0 * d2 * d3) / dc1;
100 w[2] = (d0 * d1 * d3) / dc2;
101 w[3] = 1. - w[0] - w[1] - w[2];
102 };
103
104 if (stencil.k_interval() == 1) {
105 // lev(k+0) lev(k+1) lev(k+2) lev(k+3)
106 // | | x | |
107 cubic_interpolation(zvec.data(), w.data());
108 return;
109 }
110
111 if (stencil.k_interval() == 0) {
112 if (match_IFS()) {
113 // linear interpolation
114 // lev0 lev1 lev2 lev3
115 // | + | | |
116 // w=0 w=0
117 const double alpha = (zvec[1] - z) / (zvec[1] - zvec[0]);
118 w[0] = alpha;
119 w[1] = 1. - alpha;
120 w[2] = 0.;
121 w[3] = 0.;
122 }
123 else {
124 cubic_interpolation(zvec.data(), w.data());
125 }
126 return;
127 }
128
129 if (stencil.k_interval() == 2) {
130 if (match_IFS()) {
131 // linear interpolation
132 // lev(n-4) lev(n-3) lev(n-2) lev(n-1)
133 // | | | + |
134 // w=0 w=0
135 const double alpha = (zvec[3] - z) / (zvec[3] - zvec[2]);
136 w[0] = 0.;
137 w[1] = 0.;
138 w[2] = alpha;
139 w[3] = 1. - alpha;
140 }
141 else {
142 cubic_interpolation(zvec.data(), w.data());
143 }
144 return;
145 }
146
147 if (stencil.k_interval() == -1) {
148 // constant extrapolation
149 // lev0 lev1 lev2 lev3
150 // + |------X------X------X
151 // w=1 w=0 w=0 w=0
152 w[0] = 1.;
153 w[1] = 0.;
154 w[2] = 0.;
155 w[3] = 0.;
156 return;
157 }
158
159 if (stencil.k_interval() == 3) {
160 // constant extrapolation
161 // lev(n-4) lev(n-3) lev(n-2) lev(n-1)
162 // X---------X---------X---------| +
163 // w=0 w=0 w=0 w=1
164 w[0] = 0.;
165 w[1] = 0.;
166 w[2] = 0.;
167 w[3] = 1.;
168 return;
169 }
170 ATLAS_NOTIMPLEMENTED; // should never be here
171 }
172
173 template <typename stencil_t, typename weights_t, typename array_t>
174 void interpolate(const stencil_t& stencil, const weights_t& weights, const array_t& input, double& output) const {
175 output = 0.;
176 const auto& w = weights.weights_k;
177 for (idx_t k = 0; k < stencil_width(); ++k) {
178 output += w[k] * input[stencil.k(k)];
179 }
180
181
182 if (limiter_) {
183 idx_t k = stencil.k_interval();
184 idx_t k1, k2;
185 if (k < 1) {
186 k1 = 0;
187 k2 = 1;
188 }
189 else if (k > 1) {
190 k1 = 2;
191 k2 = 3;
192 }
193 else {
194 k1 = 1;
195 k2 = 2;
196 }
197 double f1 = input[stencil.k(k1)];
198 double f2 = input[stencil.k(k2)];
199 double maxval = std::max(f1, f2);
200 double minval = std::min(f1, f2);
201 output = std::min(maxval, std::max(minval, output));
202 }
203 }
204
205 template <typename array_t>
206 double operator()(const double z, const array_t& input) const {
207 Stencil stencil;
208 compute_vertical_stencil_(z, stencil);
209 Weights weights;
210 compute_weights(z, stencil, weights);
211 double output;
212 interpolate(stencil, weights, input, output);
213 return output;
214 }
215 };
216
217 } // namespace method
218 } // namespace interpolation
219 } // namespace atlas