10 #ifndef MUELU_INVERSEAPPROXIMATIONFACTORY_DEF_HPP_
11 #define MUELU_INVERSEAPPROXIMATIONFACTORY_DEF_HPP_
16 #include <Xpetra_CrsMatrixWrap.hpp>
18 #include <Xpetra_MultiVectorFactory.hpp>
31 #include "MueLu_Utilities.hpp"
36 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
43 validParamList->
set<std::string>(
"inverse: approximation type",
"diagonal",
"Method used to approximate the inverse.");
44 validParamList->
set<Magnitude>(
"inverse: drop tolerance", 0.0,
"Values below this threshold are dropped from the matrix (or fixed if diagonal fixing is active).");
45 validParamList->
set<
bool>(
"inverse: fixing",
false,
"Keep diagonal and fix small entries with 1.0");
47 return validParamList;
50 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
52 Input(currentLevel,
"A");
55 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
60 const SC one = STS::one();
64 const bool fixing = pL.
get<
bool>(
"inverse: fixing");
67 const std::string method = pL.
get<std::string>(
"inverse: approximation type");
69 "MueLu::InverseApproximationFactory::Build: Approximation type can be 'diagonal' or 'lumping' or "
70 "'sparseapproxinverse'.");
72 RCP<Matrix> A = Get<RCP<Matrix>>(currentLevel,
"A");
74 const bool isBlocked = (bA == Teuchos::null ?
false :
true);
77 if (isBlocked) A = bA->getMatrix(0, 0);
79 const Magnitude tol = pL.
get<Magnitude>(
"inverse: drop tolerance");
82 if (method ==
"diagonal") {
83 const auto diag = VectorFactory::Build(A->getRangeMap(),
true);
84 A->getLocalDiagCopy(*diag);
86 Ainv = MatrixFactory::Build(D);
87 }
else if (method ==
"lumping") {
90 Ainv = MatrixFactory::Build(D);
91 }
else if (method ==
"sparseapproxinverse") {
93 GetOStream(
Statistics1) <<
"NNZ Graph(A): " << A->getCrsGraph()->getGlobalNumEntries() <<
" , NNZ Tresholded Graph(A): " << sparsityPattern->getGlobalNumEntries() << std::endl;
94 RCP<Matrix> pAinv = GetSparseInverse(A, sparsityPattern);
96 GetOStream(
Statistics1) <<
"NNZ Ainv: " << pAinv->getGlobalNumEntries() <<
", NNZ Tresholded Ainv (parameter: " << tol <<
"): " << Ainv->getGlobalNumEntries() << std::endl;
99 GetOStream(
Statistics1) <<
"Approximate inverse calculated by: " << method <<
"." << std::endl;
100 GetOStream(
Statistics1) <<
"Ainv has " << Ainv->getGlobalNumRows() <<
"x" << Ainv->getGlobalNumCols() <<
" rows and columns." << std::endl;
102 Set(currentLevel,
"Ainv", Ainv);
105 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
109 RCP<Matrix> Ainv = MatrixFactory::Build(sparsityPattern);
113 RCP<Import> rowImport = ImportFactory::Build(sparsityPattern->getRowMap(), sparsityPattern->getColMap());
114 RCP<Matrix> A = MatrixFactory::Build(Aorg, *rowImport);
117 for (
size_t k = 0; k < sparsityPattern->getLocalNumRows(); k++) {
120 sparsityPattern->getLocalRowView(k, Ik);
126 for (
LO i = 0; i < Ik.
size(); i++) {
127 A->getLocalRowView(Ik[i], J[i], Ak[i]);
128 for (
LO j = 0; j < J[i].size(); j++)
136 for (
LO i = 0; i < Jk.
size(); i++) G.insert(std::pair<LO, LO>(Jk[i], i));
140 for (
LO i = 0; i < Ik.
size(); i++) {
141 for (
LO j = 0; j < J[i].size(); j++) {
142 localA(G.at(J[i][j]), i) = Ak[i][j];
157 const int err = qrSolver.solve();
159 "MueLu::InverseApproximationFactory::GetSparseInverse: Error in serial QR solve.");
163 Ainv->replaceLocalValues(k, Ik, Mk);
165 Ainv->fillComplete();
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Array< T > & append(const T &x)
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static RCP< Xpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > GetThresholdedGraph(const RCP< Matrix > &A, const Magnitude threshold, const GlobalOrdinal expectedNNZperRow=-1)
Threshold a graph.
iterator erase(iterator position)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Build(Level ¤tLevel) const
Build an object with this factory.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static RCP< CrsMatrixWrap > GetThresholdedMatrix(const RCP< Matrix > &Ain, const Magnitude threshold, const bool keepDiagonal=true, const GlobalOrdinal expectedNNZperRow=-1)
Threshold a matrix.
Class that holds all level-specific information.
RCP< Matrix > GetSparseInverse(const RCP< Matrix > &A, const RCP< const CrsGraph > &sparsityPattern) const
Sparse inverse calculation method.
void DeclareInput(Level ¤tLevel) const
Input.
static const RCP< const NoFactory > getRCP()
Static Get() functions.
Exception throws to report errors in the internal logical of the program.
static Teuchos::RCP< Vector > GetInverse(Teuchos::RCP< const Vector > v, Magnitude tol=Teuchos::ScalarTraits< Scalar >::eps()*100, Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero())
Return vector containing inverse of input vector.
static Teuchos::RCP< Vector > GetLumpedMatrixDiagonal(Matrix const &A, const bool doReciprocal=false, Magnitude tol=Teuchos::ScalarTraits< Scalar >::magnitude(Teuchos::ScalarTraits< Scalar >::zero()), Scalar valReplacement=Teuchos::ScalarTraits< Scalar >::zero(), const bool replaceSingleEntryRowWithZero=false, const bool useAverageAbsDiagVal=false)
Extract Matrix Diagonal of lumped matrix.