forked from ecmwf/atlas
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
230 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/atlas/interpolation/method/sphericalvector/SparseMatrix.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* (C) Crown Copyright 2023 Met Office | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "atlas/library/defines.h" | ||
#if ATLAS_HAVE_EIGEN | ||
#include <Eigen/Sparse> | ||
#endif | ||
|
||
#include "atlas/runtime/Exception.h" | ||
|
||
namespace atlas { | ||
namespace interpolation { | ||
namespace method { | ||
namespace detail { | ||
|
||
#if ATLAS_HAVE_EIGEN | ||
/// @brief Wrapper class for Eigen sparse matrix | ||
/// | ||
/// @details Adapts the Eigen sparse matrix interface to be more in line with | ||
/// eckit::linalg::SparseMatrix. Also allows preprocessor disabling of | ||
/// class is Eigen library is not present. | ||
template <typename Value> | ||
class SparseMatrix { | ||
using EigenMatrix = Eigen::SparseMatrix<Value, Eigen::RowMajor>; | ||
|
||
public: | ||
using Index = typename EigenMatrix::StorageIndex; | ||
using Size = typename EigenMatrix::Index; | ||
using Triplets = std::vector<Eigen::Triplet<Value>>; | ||
|
||
SparseMatrix(Index nRows, Index nCols, const Triplets& triplets) | ||
: eigenMatrix_(nRows, nCols) { | ||
eigenMatrix_.setFromTriplets(triplets.begin(), triplets.end()); | ||
} | ||
|
||
Size nonZeros() const { return eigenMatrix_.nonZeros(); } | ||
Size rows() const { return eigenMatrix_.rows(); } | ||
Size cols() const { return eigenMatrix_.cols(); } | ||
const Index* outer() { return eigenMatrix_.outerIndexPtr(); } | ||
const Index* inner() { return eigenMatrix_.innerIndexPtr(); } | ||
const Value* data() { return eigenMatrix_.valuePtr(); } | ||
|
||
private: | ||
EigenMatrix eigenMatrix_{}; | ||
}; | ||
#else | ||
|
||
template <typename Value> | ||
class SparseMatrix { | ||
public: | ||
class Triplet { | ||
public: | ||
template<typename... Args> | ||
Triplet(const Args&... args) {} | ||
}; | ||
using Index = int; | ||
using Size = long int; | ||
using Triplets = std::vector<Triplet>; | ||
|
||
template<typename... Args> | ||
SparseMatrix(const Args&... args) { | ||
ATLAS_THROW_EXCEPTION("Atlas has been compiled without Eigen"); | ||
} | ||
constexpr Size nonZeros() const { return 0; } | ||
constexpr Size rows() const { return 0; } | ||
constexpr Size cols() const { return 0; } | ||
constexpr const Index* outer() { return nullptr; } | ||
constexpr const Index* inner() { return nullptr; } | ||
constexpr const Value* data() { return nullptr; } | ||
|
||
}; | ||
#endif | ||
|
||
} // namespace detail | ||
} // namespace method | ||
} // namespace interpolation | ||
} // namespace atlas |
Oops, something went wrong.