Debian Codegraph @

src/atlas/interpolation/nonlinear/NonLinear.h

raw
1 /*
2 * (C) Copyright 1996- 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 *
7 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation nor
9 * does it submit to any jurisdiction.
10 */
11
12
13 #pragma once
14
15 #include <string>
16 #include <type_traits>
17
18 #include "eckit/config/Parametrisation.h"
19 #include "eckit/linalg/SparseMatrix.h"
20
21 #include "atlas/array.h"
22 #include "atlas/field/Field.h"
23 #include "atlas/runtime/Exception.h"
24 #include "atlas/util/Factory.h"
25 #include "atlas/util/ObjectHandle.h"
26
27
28 namespace atlas {
29 namespace interpolation {
30 namespace nonlinear {
31
32
33 /**
34 * @brief NonLinear class applies non-linear corrections to an interpolation matrix, given a field with missing values.
35 * The interpolatation are re-weighted to factor those values out of the resulting field.
36 */
37 class NonLinear : public util::Object {
38 public:
39 using Config = eckit::Parametrisation;
40 using Matrix = eckit::linalg::SparseMatrix;
41 using Scalar = eckit::linalg::Scalar;
42 using Size = eckit::linalg::Size;
43
44 /**
45 * @brief ctor
46 */
47 NonLinear() = default;
48
49 /**
50 * @brief dtor
51 */
52 virtual ~NonLinear() = default;
53
54 /**
55 * @bried if NonLinear applies to field
56 * @param [in] f field
57 * @return if NonLinear applies to field
58 */
59 virtual bool applicable(const Field& f) const = 0;
60
61 /**
62 * @brief Apply non-linear corrections to interpolation matrix
63 * @param [inout] W interpolation matrix
64 * @param [in] f field with missing values information
65 * @return if W was modified
66 */
67 virtual bool execute(Matrix& W, const Field& f) const = 0;
68
69 /**
70 * @brief Apply non-linear corrections to interpolation matrix
71 * @param [inout] W interpolation matrix
72 * @param [in] a array with possibly missing values
73 * @param [in] c configuration with missing values diagnostic information
74 * @return if W was modified
75 */
76 virtual bool execute(Matrix& W, const array::Array& a, const Config& c) const = 0;
77
78 protected:
79 template <typename Value, int Rank>
80 static array::ArrayView<typename std::add_const<Value>::type, Rank> make_view_array_values(const array::Array& array) {
81 if( array.datatype().kind() != array::DataType::kind<Value>() ) {
82 throw_AssertionFailed("Array(DataType:" + array.datatype().str() + ") is not of required DataType", Here());
83 }
84 return array::make_view<typename std::add_const<Value>::type, Rank>(array);
85 }
86 };
87
88
89 class NonLinearFactory : public util::Factory<NonLinearFactory> {
90 public:
91 using Config = NonLinear::Config;
92
93 static std::string className() { return "NonLinearFactory"; }
94 static const NonLinear* build(const std::string&, const Config&);
95 using Factory::Factory;
96
97 private:
98 virtual const NonLinear* make(const Config&) = 0;
99 };
100
101
102 template <class T>
103 class NonLinearFactoryBuilder : public NonLinearFactory {
104 private:
105 virtual const NonLinear* make(const Config& /*config*/) override { return new T(/*config*/); }
106
107 public:
108 using NonLinearFactory::NonLinearFactory;
109 };
110
111
112 } // namespace nonlinear
113 } // namespace interpolation
114 } // namespace atlas