Debian Codegraph @

src/atlas/interpolation/method/structured/kernels/LinearVerticalKernel.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
15 #include "atlas/grid/Stencil.h"
16 #include "atlas/grid/StencilComputer.h"
17 #include "atlas/grid/Vertical.h"
18 #include "atlas/util/Point.h"
19
20 namespace atlas {
21 namespace interpolation {
22 namespace method {
23
24 class LinearVerticalKernel {
25 grid::ComputeVerticalStencil compute_vertical_stencil_;
26 Vertical vertical_;
27 static constexpr idx_t stencil_width() { return 2; }
28 static constexpr idx_t stencil_size() { return stencil_width() * stencil_width(); }
29 idx_t first_level_;
30 idx_t last_level_;
31
32 public:
33 LinearVerticalKernel() = default;
34
35 LinearVerticalKernel(const Vertical& vertical, const eckit::Configuration&):
36 compute_vertical_stencil_(vertical, stencil_width()),
37 vertical_(vertical),
38 first_level_(vertical_.k_begin()),
39 last_level_(vertical_.k_end() - 1) {}
40 struct Weights {
41 std::array<double, 2> weights_k;
42 };
43 using Stencil = grid::VerticalStencil<2>;
44
45 template <typename stencil_t>
46 void compute_stencil(const double z, stencil_t& stencil) const {
47 compute_vertical_stencil_(z, stencil);
48 }
49
50 template <typename stencil_t, typename weights_t>
51 void compute_weights(const double z, const stencil_t& stencil, weights_t& weights) const {
52 auto& w = weights.weights_k;
53
54 std::array<double, stencil_width()> zvec;
55 for (idx_t k = 0; k < stencil_width(); ++k) {
56 zvec[k] = vertical_(stencil.k(k));
57 }
58
59 if (stencil.k_interval() == -1) {
60 // constant extrapolation
61 // lev0 lev1
62 // + |------X
63 // w=1 w=0
64 w[0] = 1.;
65 w[1] = 0.;
66 return;
67 }
68 else if (stencil.k_interval() == 1) {
69 // constant extrapolation
70 // lev(n-2) lev(n-1)
71 // X---------| +
72 // w=0 w=1
73 w[0] = 0.;
74 w[1] = 1.;
75 return;
76 }
77
78 // Linear interpolation
79 // lev(k+0) lev(k+1)
80 // | x |
81 const double alpha = (zvec[1] - z) / (zvec[1] - zvec[0]);
82 w[0] = alpha;
83 w[1] = 1. - alpha;
84 }
85
86 template <typename stencil_t, typename weights_t, typename array_t>
87 void interpolate(const stencil_t& stencil, const weights_t& weights, const array_t& input, double& output) const {
88 output = 0.;
89 const auto& w = weights.weights_k;
90 for (idx_t k = 0; k < stencil_width(); ++k) {
91 output += w[k] * input[stencil.k(k)];
92 }
93 }
94
95 template <typename array_t>
96 double operator()(const double z, const array_t& input) const {
97 Stencil stencil;
98 compute_vertical_stencil_(z, stencil);
99 Weights weights;
100 compute_weights(z, stencil, weights);
101 double output;
102 interpolate(stencil, weights, input, output);
103 return output;
104 }
105 };
106
107 } // namespace method
108 } // namespace interpolation
109 } // namespace atlas