Debian Codegraph @

src/atlas/interpolation/method/sphericalvector/SparseMatrix.h

raw
1 /*
2 * (C) Crown Copyright 2024 Met Office
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
8 #pragma once
9
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "atlas/library/defines.h"
15 #if ATLAS_HAVE_EIGEN
16 ATLAS_SUPPRESS_WARNINGS_PUSH
17 ATLAS_SUPPRESS_WARNINGS_INTEGER_SIGN_CHANGE
18 ATLAS_SUPPRESS_WARNINGS_CODE_IS_UNREACHABLE
19 ATLAS_SUPPRESS_WARNINGS_UNUSED_BUT_SET_VARIABLE
20 #include <Eigen/Sparse>
21 ATLAS_SUPPRESS_WARNINGS_POP
22 #endif
23
24 #include "atlas/runtime/Exception.h"
25 #include "eckit/log/CodeLocation.h"
26
27 namespace atlas {
28 namespace interpolation {
29 namespace method {
30 namespace detail {
31
32 #if ATLAS_HAVE_EIGEN
33 /// @brief Wrapper class for Eigen sparse matrix
34 ///
35 /// @details Eigen sparse matrix wrapper. Allows preprocessor disabling of class
36 /// if Eigen library is not present.
37 template <typename Value>
38 class SparseMatrix {
39 public:
40 using Index = int;
41 using EigenMatrix = Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>;
42 using Triplet = Eigen::Triplet<Value, Index>;
43 using Triplets = std::vector<Triplet>;
44 using RowIter = typename EigenMatrix::InnerIterator;
45
46 SparseMatrix() = default;
47
48 SparseMatrix(Index nRows, Index nCols, const Triplets& triplets)
49 : eigenMatrix_(nRows, nCols) {
50 eigenMatrix_.setFromTriplets(triplets.begin(), triplets.end());
51 }
52
53 Index nonZeros() const { return eigenMatrix_.nonZeros(); }
54 Index rows() const { return eigenMatrix_.rows(); }
55 Index cols() const { return eigenMatrix_.cols(); }
56 RowIter rowIter(Index rowIndex) const {
57 return RowIter(eigenMatrix_, rowIndex);
58 }
59 SparseMatrix<Value> adjoint() const {
60 auto adjointMatrix = SparseMatrix{};
61 adjointMatrix.eigenMatrix_ = eigenMatrix_.adjoint();
62 return adjointMatrix;
63 }
64
65 private:
66 EigenMatrix eigenMatrix_{};
67 };
68 #else
69
70 template <typename Value>
71 class SparseMatrix {
72 public:
73 using Index = int;
74
75 class Triplet {
76 public:
77 template <typename... Args>
78 constexpr Triplet(const Args&... args) {}
79 };
80 using Triplets = std::vector<Triplet>;
81
82 class RowIter {
83 public:
84 template <typename... Args>
85 constexpr RowIter(const Args&... args) {}
86 constexpr Index row() const { return Index{}; }
87 constexpr Index col() const { return Index{}; }
88 constexpr Value value() const { return Value{}; }
89 constexpr operator bool() const { return false; }
90 constexpr RowIter& operator++() { return *this; }
91 };
92
93 constexpr SparseMatrix() = default;
94 template <typename... Args>
95 SparseMatrix(const Args&... args) {
96 throw_Exception("Atlas has been compiled without Eigen", Here());
97 }
98 constexpr Index nonZeros() const { return Index{}; }
99 constexpr Index rows() const { return Index{}; }
100 constexpr Index cols() const { return Index{}; }
101 constexpr RowIter rowIter(Index rowIndex) const { return RowIter{}; }
102 constexpr SparseMatrix<Value> adjoint() const {
103 return SparseMatrix<Value>{};
104 }
105 };
106 #endif
107
108 } // namespace detail
109 } // namespace method
110 } // namespace interpolation
111 } // namespace atlas